AudioSource.h revision 082830f92373a1b9e512dbbfb940187ffa1c2c6f
1/*
2 * Copyright (C) 2009 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#ifndef AUDIO_SOURCE_H_
18
19#define AUDIO_SOURCE_H_
20
21#include <media/AudioRecord.h>
22#include <media/AudioSystem.h>
23#include <media/stagefright/MediaSource.h>
24#include <media/stagefright/MediaBuffer.h>
25#include <utils/List.h>
26
27#include <system/audio.h>
28
29namespace android {
30
31class AudioRecord;
32
33struct AudioSource : public MediaSource, public MediaBufferObserver {
34    // Note that the "channels" parameter _is_ the number of channels,
35    // _not_ a bitmask of audio_channels_t constants.
36    AudioSource(
37            audio_source_t inputSource,
38            uint32_t sampleRate,
39            uint32_t channels = 1);
40
41    status_t initCheck() const;
42
43    virtual status_t start(MetaData *params = NULL);
44    virtual status_t stop() { return reset(); }
45    virtual sp<MetaData> getFormat();
46
47    // Returns the maximum amplitude since last call.
48    int16_t getMaxAmplitude();
49
50    virtual status_t read(
51            MediaBuffer **buffer, const ReadOptions *options = NULL);
52
53    status_t dataCallback(const AudioRecord::Buffer& buffer);
54    virtual void signalBufferReturned(MediaBuffer *buffer);
55
56    // If useLooperTime == true, buffers will carry absolute timestamps
57    // as returned by ALooper::GetNowUs(), otherwise systemTime() is used
58    // and buffers contain timestamps relative to start time.
59    // The default is to _not_ use looper time.
60    void setUseLooperTime(bool useLooperTime);
61
62protected:
63    virtual ~AudioSource();
64
65private:
66    enum {
67        kMaxBufferSize = 2048,
68
69        // After the initial mute, we raise the volume linearly
70        // over kAutoRampDurationUs.
71        kAutoRampDurationUs = 300000,
72
73        // This is the initial mute duration to suppress
74        // the video recording signal tone
75        kAutoRampStartUs = 0,
76    };
77
78    Mutex mLock;
79    Condition mFrameAvailableCondition;
80    Condition mFrameEncodingCompletionCondition;
81
82    AudioRecord *mRecord;
83    status_t mInitCheck;
84    bool mStarted;
85    int32_t mSampleRate;
86
87    bool mTrackMaxAmplitude;
88    int64_t mStartTimeUs;
89    int16_t mMaxAmplitude;
90    int64_t mPrevSampleTimeUs;
91    int64_t mInitialReadTimeUs;
92    int64_t mNumFramesReceived;
93    int64_t mNumClientOwnedBuffers;
94
95    List<MediaBuffer * > mBuffersReceived;
96
97    bool mUseLooperTime;
98
99    void trackMaxAmplitude(int16_t *data, int nSamples);
100
101    // This is used to raise the volume from mute to the
102    // actual level linearly.
103    void rampVolume(
104        int32_t startFrame, int32_t rampDurationFrames,
105        uint8_t *data,   size_t bytes);
106
107    void queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs);
108    void releaseQueuedFrames_l();
109    void waitOutstandingEncodingFrames_l();
110    status_t reset();
111
112    AudioSource(const AudioSource &);
113    AudioSource &operator=(const AudioSource &);
114};
115
116}  // namespace android
117
118#endif  // AUDIO_SOURCE_H_
119