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