AudioSource.h revision b44c9d2bdc0d5b9cb03254022a58e017b516e9e6
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 _not_ the number of channels,
35    // but a bitmask of audio_channels_t constants.
36    AudioSource(
37            audio_source_t inputSource, uint32_t sampleRate,
38            uint32_t channels = AUDIO_CHANNEL_IN_MONO);
39
40    status_t initCheck() const;
41
42    virtual status_t start(MetaData *params = NULL);
43    virtual status_t stop() { return reset(); }
44    virtual sp<MetaData> getFormat();
45
46    // Returns the maximum amplitude since last call.
47    int16_t getMaxAmplitude();
48
49    virtual status_t read(
50            MediaBuffer **buffer, const ReadOptions *options = NULL);
51
52    status_t dataCallbackTimestamp(const AudioRecord::Buffer& buffer, int64_t timeUs);
53    virtual void signalBufferReturned(MediaBuffer *buffer);
54
55protected:
56    virtual ~AudioSource();
57
58private:
59    enum {
60        kMaxBufferSize = 2048,
61
62        // After the initial mute, we raise the volume linearly
63        // over kAutoRampDurationUs.
64        kAutoRampDurationUs = 300000,
65
66        // This is the initial mute duration to suppress
67        // the video recording signal tone
68        kAutoRampStartUs = 0,
69    };
70
71    Mutex mLock;
72    Condition mFrameAvailableCondition;
73    Condition mFrameEncodingCompletionCondition;
74
75    AudioRecord *mRecord;
76    status_t mInitCheck;
77    bool mStarted;
78    int32_t mSampleRate;
79
80    bool mTrackMaxAmplitude;
81    int64_t mStartTimeUs;
82    int16_t mMaxAmplitude;
83    int64_t mPrevSampleTimeUs;
84    int64_t mInitialReadTimeUs;
85    int64_t mNumFramesReceived;
86    int64_t mNumClientOwnedBuffers;
87
88    List<MediaBuffer * > mBuffersReceived;
89
90    void trackMaxAmplitude(int16_t *data, int nSamples);
91
92    // This is used to raise the volume from mute to the
93    // actual level linearly.
94    void rampVolume(
95        int32_t startFrame, int32_t rampDurationFrames,
96        uint8_t *data,   size_t bytes);
97
98    void releaseQueuedFrames_l();
99    void waitOutstandingEncodingFrames_l();
100    status_t reset();
101
102    AudioSource(const AudioSource &);
103    AudioSource &operator=(const AudioSource &);
104};
105
106}  // namespace android
107
108#endif  // AUDIO_SOURCE_H_
109