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 
29 #include <vector>
30 
31 namespace android {
32 
33 using content::AttributionSourceState;
34 
35 class AudioRecord;
36 
37 struct AudioSource : public MediaSource,
38                      public MediaBufferObserver,
39                      public AudioRecord::IAudioRecordCallback {
40     // Note that the "channels" parameter _is_ the number of channels,
41     // _not_ a bitmask of audio_channels_t constants.
42     AudioSource(
43         const audio_attributes_t *attr,
44         const AttributionSourceState& attributionSource,
45         uint32_t sampleRate,
46         uint32_t channels,
47         uint32_t outSampleRate = 0,
48         audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
49         audio_microphone_direction_t selectedMicDirection = MIC_DIRECTION_UNSPECIFIED,
50         float selectedMicFieldDimension = MIC_FIELD_DIMENSION_NORMAL);
51 
52     // Legacy constructor kept for vendor dependencies
53     AudioSource(
54         const audio_attributes_t *attr,
55         const String16 &opPackageName,
56         uint32_t sampleRate,
57         uint32_t channels,
58         uint32_t outSampleRate = 0,
59         uid_t uid = -1,
60         pid_t pid = -1,
61         audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
62         audio_microphone_direction_t selectedMicDirection = MIC_DIRECTION_UNSPECIFIED,
63         float selectedMicFieldDimension = MIC_FIELD_DIMENSION_NORMAL);
64 
65     status_t initCheck() const;
66 
67     virtual status_t start(MetaData *params = NULL);
stopAudioSource68     virtual status_t stop() { return reset(); }
69     virtual sp<MetaData> getFormat();
70 
71     // Returns the maximum amplitude since last call.
72     int16_t getMaxAmplitude();
73 
74     virtual status_t read(
75             MediaBufferBase **buffer, const ReadOptions *options = NULL);
76     virtual status_t setStopTimeUs(int64_t stopTimeUs);
77 
78     virtual void signalBufferReturned(MediaBufferBase *buffer);
79 
80     status_t setInputDevice(audio_port_handle_t deviceId);
81     status_t getRoutedDeviceId(audio_port_handle_t* deviceId);
82     status_t addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback);
83     status_t removeAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback);
84 
85     status_t getActiveMicrophones(std::vector<media::MicrophoneInfoFw>* activeMicrophones);
86     status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction);
87     status_t setPreferredMicrophoneFieldDimension(float zoom);
88 
89     status_t getPortId(audio_port_handle_t *portId) const;
90 
91 protected:
92     virtual ~AudioSource();
93 
94 private:
95     enum {
96         kMaxBufferSize = 2048,
97 
98         // After the initial mute, we raise the volume linearly
99         // over kAutoRampDurationUs.
100         kAutoRampDurationUs = 300000,
101 
102         // This is the initial mute duration to suppress
103         // the video recording signal tone
104         kAutoRampStartUs = 0,
105     };
106 
107     Mutex mLock;
108     Condition mFrameAvailableCondition;
109     Condition mFrameEncodingCompletionCondition;
110 
111     sp<AudioRecord> mRecord;
112     status_t mInitCheck;
113     bool mStarted;
114     int32_t mSampleRate;
115     int32_t mOutSampleRate;
116 
117     bool mTrackMaxAmplitude;
118     int64_t mStartTimeUs;
119     int64_t mStopSystemTimeUs;
120     int64_t mLastFrameTimestampUs;
121     int16_t mMaxAmplitude;
122     int64_t mPrevSampleTimeUs;
123     int64_t mInitialReadTimeUs;
124     int64_t mNumFramesReceived;
125     int64_t mNumFramesSkipped;
126     int64_t mNumFramesLost;
127     int64_t mNumClientOwnedBuffers;
128     bool mNoMoreFramesToRead;
129 
130     List<MediaBuffer * > mBuffersReceived;
131 
132     void trackMaxAmplitude(int16_t *data, int nSamples);
133 
134     // This is used to raise the volume from mute to the
135     // actual level linearly.
136     void rampVolume(
137         int32_t startFrame, int32_t rampDurationFrames,
138         uint8_t *data,   size_t bytes);
139 
140     void queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs);
141     void releaseQueuedFrames_l();
142     void waitOutstandingEncodingFrames_l();
143     status_t reset();
144 
145     // IAudioRecordCallback implementation
146     size_t onMoreData(const AudioRecord::Buffer&) override;
147     void onOverrun() override;
148 
149     AudioSource(const AudioSource &);
150     AudioSource &operator=(const AudioSource &);
151 
152     void set(
153         const audio_attributes_t *attr,
154         const AttributionSourceState& attributionSource,
155         uint32_t sampleRate,
156         uint32_t channels,
157         uint32_t outSampleRate = 0,
158         audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
159         audio_microphone_direction_t selectedMicDirection = MIC_DIRECTION_UNSPECIFIED,
160         float selectedMicFieldDimension = MIC_FIELD_DIMENSION_NORMAL);
161 };
162 
163 }  // namespace android
164 
165 #endif  // AUDIO_SOURCE_H_
166