blob: dbcab6813bdfc66f7ebbb8c7b0613708bba392e4 [file] [log] [blame]
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001/*
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08002 * 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
James Dong00f742c2012-01-13 17:34:42 -080017// #define LOG_NDEBUG 0
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080018#define LOG_TAG "DummyAudioSource"
James Dong00f742c2012-01-13 17:34:42 -080019#include <utils/Log.h>
James Dongc4689fa2012-02-08 13:51:46 -080020#include <media/stagefright/foundation/ADebug.h>
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080021#include <media/stagefright/MediaErrors.h>
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080022#include <media/stagefright/MediaDefs.h>
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080023#include <media/stagefright/MediaBufferGroup.h>
24#include <media/stagefright/MetaData.h>
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080025#include "DummyAudioSource.h"
26
27
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080028namespace android {
29
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080030//static
James Dong00f742c2012-01-13 17:34:42 -080031sp<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
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080041}
42
James Dong00f742c2012-01-13 17:34:42 -080043DummyAudioSource::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) {
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080053
James Dong00f742c2012-01-13 17:34:42 -080054 mNumberOfSamplePerFrame = (int32_t)
55 ((1L * mSamplingRate * mFrameDurationUs)/1000000);
56 mNumberOfSamplePerFrame = mNumberOfSamplePerFrame * mChannelCount;
Basavapatna Dattaguru100d0182011-03-04 09:48:24 -080057
James Dong00f742c2012-01-13 17:34:42 -080058 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
67DummyAudioSource::~DummyAudioSource() {
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080068 /* Do nothing here? */
James Dong00f742c2012-01-13 17:34:42 -080069 ALOGV("~DummyAudioSource");
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080070}
71
James Dong00f742c2012-01-13 17:34:42 -080072void DummyAudioSource::setDuration(int64_t audioDurationUs) {
73 ALOGV("setDuration: %lld us added to %lld us",
74 audioDurationUs, mAudioDurationUs);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080075
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -080076 Mutex::Autolock autoLock(mLock);
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -080077 mAudioDurationUs += audioDurationUs;
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -080078}
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080079
80status_t DummyAudioSource::start(MetaData *params) {
James Dong00f742c2012-01-13 17:34:42 -080081 ALOGV("start: E");
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080082 status_t err = OK;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080083
84 mTimeStampUs = 0;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080085
86 mBufferGroup = new MediaBufferGroup;
87 mBufferGroup->add_buffer(
88 new MediaBuffer(mNumberOfSamplePerFrame * sizeof(int16_t)));
89
James Dong00f742c2012-01-13 17:34:42 -080090 ALOGV("start: X");
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080091
92 return err;
93}
94
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080095status_t DummyAudioSource::stop() {
James Dong00f742c2012-01-13 17:34:42 -080096 ALOGV("stop");
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080097
98 delete mBufferGroup;
99 mBufferGroup = NULL;
100
James Dong00f742c2012-01-13 17:34:42 -0800101 return OK;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800102}
103
104
105sp<MetaData> DummyAudioSource::getFormat() {
James Dong00f742c2012-01-13 17:34:42 -0800106 ALOGV("getFormat");
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800107
108 sp<MetaData> meta = new MetaData;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800109 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
110 meta->setInt32(kKeyChannelCount, mChannelCount);
111 meta->setInt32(kKeySampleRate, mSamplingRate);
112 meta->setInt64(kKeyDuration, mFrameDurationUs);
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800113 meta->setCString(kKeyDecoderComponent, "DummyAudioSource");
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800114
115 return meta;
116}
117
James Dong00f742c2012-01-13 17:34:42 -0800118status_t DummyAudioSource::read(
119 MediaBuffer **out, const MediaSource::ReadOptions *options) {
120
121 ALOGV("read: E");
122
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800123 int64_t seekTimeUs;
124 ReadOptions::SeekMode mode;
125
126 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
127 CHECK(seekTimeUs >= 0);
128 mTimeStampUs = seekTimeUs;
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800129 }
James Dong00f742c2012-01-13 17:34:42 -0800130
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800131 {
132 Mutex::Autolock autoLock(mLock);
133 if (mTimeStampUs >= mAudioDurationUs) {
James Dong00f742c2012-01-13 17:34:42 -0800134 ALOGI("read: EOS reached %lld > %lld",
135 mTimeStampUs, mAudioDurationUs);
136
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800137 *out = NULL;
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800138 return ERROR_END_OF_STREAM;
139 }
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800140 }
141
James Dong00f742c2012-01-13 17:34:42 -0800142 MediaBuffer *buffer;
143 status_t err = mBufferGroup->acquire_buffer(&buffer);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800144 if (err != OK) {
James Dong00f742c2012-01-13 17:34:42 -0800145 ALOGE("Failed to acquire buffer from mBufferGroup: %d", err);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800146 return err;
147 }
148
James Dong2dbef652011-05-02 18:12:22 -0700149 memset((uint8_t *) buffer->data() + buffer->range_offset(),
150 0, mNumberOfSamplePerFrame << 1);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800151 buffer->set_range(buffer->range_offset(), (mNumberOfSamplePerFrame << 1));
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800152 buffer->meta_data()->setInt64(kKeyTime, mTimeStampUs);
James Dong00f742c2012-01-13 17:34:42 -0800153
154 ALOGV("read: offset = %d, size = %d, mTimeStampUs = %lld",
155 buffer->range_offset(), buffer->size(), mTimeStampUs);
156
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800157 mTimeStampUs = mTimeStampUs + mFrameDurationUs;
158 *out = buffer;
James Dong00f742c2012-01-13 17:34:42 -0800159
160 return OK;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800161}
162
163}// namespace android