• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "DummyAudioSource"
19 #include <utils/Log.h>
20 #include <media/stagefright/foundation/ADebug.h>
21 #include <media/stagefright/MediaErrors.h>
22 #include <media/stagefright/MediaDefs.h>
23 #include <media/stagefright/MediaBufferGroup.h>
24 #include <media/stagefright/MetaData.h>
25 #include "DummyAudioSource.h"
26 
27 
28 namespace android {
29 
30 //static
Create(int32_t samplingRate,int32_t channelCount,int64_t frameDurationUs,int64_t audioDurationUs)31 sp<DummyAudioSource> DummyAudioSource::Create(
32         int32_t samplingRate, int32_t channelCount,
33         int64_t frameDurationUs, int64_t audioDurationUs) {
34 
35     ALOGV("Create ");
36     return new DummyAudioSource(samplingRate,
37                                 channelCount,
38                                 frameDurationUs,
39                                 audioDurationUs);
40 
41 }
42 
DummyAudioSource(int32_t samplingRate,int32_t channelCount,int64_t frameDurationUs,int64_t audioDurationUs)43 DummyAudioSource::DummyAudioSource(
44         int32_t samplingRate, int32_t channelCount,
45         int64_t frameDurationUs, int64_t audioDurationUs)
46     : mSamplingRate(samplingRate),
47       mChannelCount(channelCount),
48       mFrameDurationUs(frameDurationUs),
49       mNumberOfSamplePerFrame(0),
50       mAudioDurationUs(audioDurationUs),
51       mTimeStampUs(0),
52       mBufferGroup(NULL) {
53 
54     mNumberOfSamplePerFrame = (int32_t)
55             ((1L * mSamplingRate * mFrameDurationUs)/1000000);
56     mNumberOfSamplePerFrame = mNumberOfSamplePerFrame  * mChannelCount;
57 
58     ALOGV("Constructor: E");
59     ALOGV("samplingRate = %d", samplingRate);
60     ALOGV("channelCount = %d", channelCount);
61     ALOGV("frameDurationUs = %lld", frameDurationUs);
62     ALOGV("audioDurationUs = %lld", audioDurationUs);
63     ALOGV("mNumberOfSamplePerFrame = %d", mNumberOfSamplePerFrame);
64     ALOGV("Constructor: X");
65 }
66 
~DummyAudioSource()67 DummyAudioSource::~DummyAudioSource() {
68     /* Do nothing here? */
69     ALOGV("~DummyAudioSource");
70 }
71 
setDuration(int64_t audioDurationUs)72 void DummyAudioSource::setDuration(int64_t audioDurationUs) {
73     ALOGV("setDuration: %lld us added to %lld us",
74         audioDurationUs, mAudioDurationUs);
75 
76     Mutex::Autolock autoLock(mLock);
77     mAudioDurationUs += audioDurationUs;
78 }
79 
start(MetaData * params)80 status_t DummyAudioSource::start(MetaData *params) {
81     ALOGV("start: E");
82     status_t err = OK;
83 
84     mTimeStampUs = 0;
85 
86     mBufferGroup = new MediaBufferGroup;
87     mBufferGroup->add_buffer(
88             new MediaBuffer(mNumberOfSamplePerFrame * sizeof(int16_t)));
89 
90     ALOGV("start: X");
91 
92     return err;
93 }
94 
stop()95 status_t DummyAudioSource::stop() {
96     ALOGV("stop");
97 
98     delete mBufferGroup;
99     mBufferGroup = NULL;
100 
101     return OK;
102 }
103 
104 
getFormat()105 sp<MetaData> DummyAudioSource::getFormat() {
106     ALOGV("getFormat");
107 
108     sp<MetaData> meta = new MetaData;
109     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
110     meta->setInt32(kKeyChannelCount, mChannelCount);
111     meta->setInt32(kKeySampleRate, mSamplingRate);
112     meta->setInt64(kKeyDuration, mFrameDurationUs);
113     meta->setCString(kKeyDecoderComponent, "DummyAudioSource");
114 
115     return meta;
116 }
117 
read(MediaBuffer ** out,const MediaSource::ReadOptions * options)118 status_t DummyAudioSource::read(
119         MediaBuffer **out, const MediaSource::ReadOptions *options) {
120 
121     ALOGV("read: E");
122 
123     int64_t seekTimeUs;
124     ReadOptions::SeekMode mode;
125 
126     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
127         CHECK(seekTimeUs >= 0);
128         mTimeStampUs = seekTimeUs;
129     }
130 
131     {
132         Mutex::Autolock autoLock(mLock);
133         if (mTimeStampUs >= mAudioDurationUs) {
134             ALOGI("read: EOS reached %lld > %lld",
135                 mTimeStampUs, mAudioDurationUs);
136 
137             *out = NULL;
138             return ERROR_END_OF_STREAM;
139         }
140     }
141 
142     MediaBuffer *buffer;
143     status_t err = mBufferGroup->acquire_buffer(&buffer);
144     if (err != OK) {
145         ALOGE("Failed to acquire buffer from mBufferGroup: %d", err);
146         return err;
147     }
148 
149     memset((uint8_t *) buffer->data() + buffer->range_offset(),
150             0, mNumberOfSamplePerFrame << 1);
151     buffer->set_range(buffer->range_offset(), (mNumberOfSamplePerFrame << 1));
152     buffer->meta_data()->setInt64(kKeyTime, mTimeStampUs);
153 
154     ALOGV("read: offset  = %d, size = %d, mTimeStampUs = %lld",
155              buffer->range_offset(), buffer->size(), mTimeStampUs);
156 
157     mTimeStampUs = mTimeStampUs + mFrameDurationUs;
158     *out = buffer;
159 
160     return OK;
161 }
162 
163 }// namespace android
164