summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/lvpp/DummyAudioSource.cpp
blob: db1b573247df98bd379bc00e16c7f5f77d462790 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * 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 1
#define LOG_TAG "DummyAudioSource"
#include "utils/Log.h"

#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaDebug.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/MediaBufferGroup.h>
#include <media/stagefright/MetaData.h>


#include "DummyAudioSource.h"



/* Android includes*/
#include <utils/Log.h>
#include <memory.h>


/*---------------------*/
/*  DEBUG LEVEL SETUP  */
/*---------------------*/
#define LOG1 LOGE    /*ERRORS Logging*/
#define LOG2 LOGV    /*WARNING Logging*/
#define LOG3 //LOGV    /*COMMENTS Logging*/

/*---------------------*/
/*  CONSTANTS          */
/*---------------------*/


namespace android {


/*---------------------*/
/*  SEEK SOURCE        */
/*---------------------*/

//static
sp<DummyAudioSource> DummyAudioSource::Create(int32_t samplingRate,
                                              int32_t channelCount,
                                              int64_t frameDurationUs,
                                              int64_t audioDurationUs) {
    LOG2("DummyAudioSource::Create ");
    sp<DummyAudioSource> aSource = new DummyAudioSource (samplingRate,
                                                         channelCount,
                                                         frameDurationUs,
                                                         audioDurationUs);
    return aSource;
}


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){
    LOG2("DummyAudioSource::DummyAudioSource constructor START");
    /* Do nothing here? */
    LOG2("DummyAudioSource::DummyAudioSource");
    LOG2("DummyAudioSource:: mSamplingRate = %d",samplingRate);
    LOG2("DummyAudioSource:: mChannelCount = %d",channelCount);
    LOG2("DummyAudioSource:: frameDurationUs = %lld",frameDurationUs);
    LOG2("DummyAudioSource:: mAudioDurationUs = %lld",mAudioDurationUs);

    LOG2("DummyAudioSource::DummyAudioSource constructor END");
}


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

void DummyAudioSource::setDuration (int64_t audioDurationUs) {
    Mutex::Autolock autoLock(mLock);
    LOG2("SetDuration %lld", mAudioDurationUs);
    mAudioDurationUs += audioDurationUs;
    LOG2("SetDuration %lld", mAudioDurationUs);
}

status_t DummyAudioSource::start(MetaData *params) {
    status_t err = OK;
    LOG2("DummyAudioSource::start START");

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

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

    LOG2("DummyAudioSource:: mNumberOfSamplePerFrame = %d",mNumberOfSamplePerFrame);
    LOG2("DummyAudioSource::start END");

    return err;
}


status_t DummyAudioSource::stop() {
    status_t err = OK;

    LOG2("DummyAudioSource::stop START");

    delete mBufferGroup;
    mBufferGroup = NULL;


    LOG2("DummyAudioSource::stop END");

    return err;
}


sp<MetaData> DummyAudioSource::getFormat() {
    LOG2("DummyAudioSource::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) {
    status_t err            = OK;
    //LOG2("DummyAudioSource::read START");
    MediaBuffer *buffer;
    int64_t seekTimeUs;
    ReadOptions::SeekMode mode;

    if (options && options->getSeekTo(&seekTimeUs, &mode)) {
        CHECK(seekTimeUs >= 0);
        mTimeStampUs = seekTimeUs;
    }
    {
        Mutex::Autolock autoLock(mLock);
        if (mTimeStampUs >= mAudioDurationUs) {
            *out = NULL;
            LOGI("EOS reached");
            return ERROR_END_OF_STREAM;
        }
    }

    err = mBufferGroup->acquire_buffer(&buffer);
    if (err != OK) {
        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);
    LOG2("DummyAudioSource::read  Buffer_offset  = %d,"
            "Buffer_Size = %d, mTimeStampUs = %lld",
             buffer->range_offset(),buffer->size(),mTimeStampUs);
    mTimeStampUs = mTimeStampUs + mFrameDurationUs;
    *out = buffer;
    return err;
}

}// namespace android