summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/lvpp/DummyAudioSource.cpp
blob: dbcab6813bdfc66f7ebbb8c7b0613708bba392e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// #define LOG_NDEBUG 0
#define LOG_TAG "DummyAudioSource"
#include <utils/Log.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaBufferGroup.h>
#include <media/stagefright/MetaData.h>
#include "DummyAudioSource.h"


namespace android {

//static
sp<DummyAudioSource> DummyAudioSource::Create(
        int32_t samplingRate, int32_t channelCount,
        int64_t frameDurationUs, int64_t audioDurationUs) {

    ALOGV("Create ");
    return new DummyAudioSource(samplingRate,
                                channelCount,
                                frameDurationUs,
                                audioDurationUs);

}

DummyAudioSource::DummyAudioSource(
        int32_t samplingRate, int32_t channelCount,
        int64_t frameDurationUs, int64_t audioDurationUs)
    : mSamplingRate(samplingRate),
      mChannelCount(channelCount),
      mFrameDurationUs(frameDurationUs),
      mNumberOfSamplePerFrame(0),
      mAudioDurationUs(audioDurationUs),
      mTimeStampUs(0),
      mBufferGroup(NULL) {

    mNumberOfSamplePerFrame = (int32_t)
            ((1L * mSamplingRate * mFrameDurationUs)/1000000);
    mNumberOfSamplePerFrame = mNumberOfSamplePerFrame  * mChannelCount;

    ALOGV("Constructor: E");
    ALOGV("samplingRate = %d", samplingRate);
    ALOGV("channelCount = %d", channelCount);
    ALOGV("frameDurationUs = %lld", frameDurationUs);
    ALOGV("audioDurationUs = %lld", audioDurationUs);
    ALOGV("mNumberOfSamplePerFrame = %d", mNumberOfSamplePerFrame);
    ALOGV("Constructor: X");
}

DummyAudioSource::~DummyAudioSource() {
    /* Do nothing here? */
    ALOGV("~DummyAudioSource");
}

void DummyAudioSource::setDuration(int64_t audioDurationUs) {
    ALOGV("setDuration: %lld us added to %lld us",
        audioDurationUs, mAudioDurationUs);

    Mutex::Autolock autoLock(mLock);
    mAudioDurationUs += audioDurationUs;
}

status_t DummyAudioSource::start(MetaData *params) {
    ALOGV("start: E");
    status_t err = OK;

    mTimeStampUs = 0;

    mBufferGroup = new MediaBufferGroup;
    mBufferGroup->add_buffer(
            new MediaBuffer(mNumberOfSamplePerFrame * sizeof(int16_t)));

    ALOGV("start: X");

    return err;
}

status_t DummyAudioSource::stop() {
    ALOGV("stop");

    delete mBufferGroup;
    mBufferGroup = NULL;

    return OK;
}


sp<MetaData> DummyAudioSource::getFormat() {
    ALOGV("getFormat");

    sp<MetaData> meta = new MetaData;
    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
    meta->setInt32(kKeyChannelCount, mChannelCount);
    meta->setInt32(kKeySampleRate, mSamplingRate);
    meta->setInt64(kKeyDuration, mFrameDurationUs);
    meta->setCString(kKeyDecoderComponent, "DummyAudioSource");

    return meta;
}

status_t DummyAudioSource::read(
        MediaBuffer **out, const MediaSource::ReadOptions *options) {

    ALOGV("read: E");

    int64_t seekTimeUs;
    ReadOptions::SeekMode mode;

    if (options && options->getSeekTo(&seekTimeUs, &mode)) {
        CHECK(seekTimeUs >= 0);
        mTimeStampUs = seekTimeUs;
    }

    {
        Mutex::Autolock autoLock(mLock);
        if (mTimeStampUs >= mAudioDurationUs) {
            ALOGI("read: EOS reached %lld > %lld",
                mTimeStampUs, mAudioDurationUs);

            *out = NULL;
            return ERROR_END_OF_STREAM;
        }
    }

    MediaBuffer *buffer;
    status_t err = mBufferGroup->acquire_buffer(&buffer);
    if (err != OK) {
        ALOGE("Failed to acquire buffer from mBufferGroup: %d", err);
        return err;
    }

    memset((uint8_t *) buffer->data() + buffer->range_offset(),
            0, mNumberOfSamplePerFrame << 1);
    buffer->set_range(buffer->range_offset(), (mNumberOfSamplePerFrame << 1));
    buffer->meta_data()->setInt64(kKeyTime, mTimeStampUs);

    ALOGV("read: offset  = %d, size = %d, mTimeStampUs = %lld",
             buffer->range_offset(), buffer->size(), mTimeStampUs);

    mTimeStampUs = mTimeStampUs + mFrameDurationUs;
    *out = buffer;

    return OK;
}

}// namespace android