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/MediaSource.h>
24#include <media/MicrophoneInfo.h>
25#include <media/stagefright/MediaBuffer.h>
26#include <utils/List.h>
27
28#include <system/audio.h>
29
30#include <vector>
31
32namespace android {
33
34class AudioRecord;
35
36struct AudioSource : public MediaSource, public MediaBufferObserver {
37    // Note that the "channels" parameter _is_ the number of channels,
38    // _not_ a bitmask of audio_channels_t constants.
39    AudioSource(
40            audio_source_t inputSource,
41            const String16 &opPackageName,
42            uint32_t sampleRate,
43            uint32_t channels,
44            uint32_t outSampleRate = 0,
45            uid_t uid = -1,
46            pid_t pid = -1,
47            audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE);
48
49    status_t initCheck() const;
50
51    virtual status_t start(MetaData *params = NULL);
52    virtual status_t stop() { return reset(); }
53    virtual sp<MetaData> getFormat();
54
55    // Returns the maximum amplitude since last call.
56    int16_t getMaxAmplitude();
57
58    virtual status_t read(
59            MediaBufferBase **buffer, const ReadOptions *options = NULL);
60    virtual status_t setStopTimeUs(int64_t stopTimeUs);
61
62    status_t dataCallback(const AudioRecord::Buffer& buffer);
63    virtual void signalBufferReturned(MediaBufferBase *buffer);
64
65    status_t setInputDevice(audio_port_handle_t deviceId);
66    status_t getRoutedDeviceId(audio_port_handle_t* deviceId);
67    status_t addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback);
68    status_t removeAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback);
69
70    status_t getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones);
71
72
73protected:
74    virtual ~AudioSource();
75
76private:
77    enum {
78        kMaxBufferSize = 2048,
79
80        // After the initial mute, we raise the volume linearly
81        // over kAutoRampDurationUs.
82        kAutoRampDurationUs = 300000,
83
84        // This is the initial mute duration to suppress
85        // the video recording signal tone
86        kAutoRampStartUs = 0,
87    };
88
89    Mutex mLock;
90    Condition mFrameAvailableCondition;
91    Condition mFrameEncodingCompletionCondition;
92
93    sp<AudioRecord> mRecord;
94    status_t mInitCheck;
95    bool mStarted;
96    int32_t mSampleRate;
97    int32_t mOutSampleRate;
98
99    bool mTrackMaxAmplitude;
100    int64_t mStartTimeUs;
101    int64_t mStopSystemTimeUs;
102    int64_t mLastFrameTimestampUs;
103    int16_t mMaxAmplitude;
104    int64_t mPrevSampleTimeUs;
105    int64_t mInitialReadTimeUs;
106    int64_t mNumFramesReceived;
107    int64_t mNumFramesSkipped;
108    int64_t mNumFramesLost;
109    int64_t mNumClientOwnedBuffers;
110    bool mNoMoreFramesToRead;
111
112    List<MediaBuffer * > mBuffersReceived;
113
114    void trackMaxAmplitude(int16_t *data, int nSamples);
115
116    // This is used to raise the volume from mute to the
117    // actual level linearly.
118    void rampVolume(
119        int32_t startFrame, int32_t rampDurationFrames,
120        uint8_t *data,   size_t bytes);
121
122    void queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs);
123    void releaseQueuedFrames_l();
124    void waitOutstandingEncodingFrames_l();
125    status_t reset();
126
127    AudioSource(const AudioSource &);
128    AudioSource &operator=(const AudioSource &);
129};
130
131}  // namespace android
132
133#endif  // AUDIO_SOURCE_H_
134