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 32 namespace android { 33 34 class AudioRecord; 35 36 struct 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 audio_microphone_direction_t selectedMicDirection = MIC_DIRECTION_UNSPECIFIED, 49 float selectedMicFieldDimension = MIC_FIELD_DIMENSION_NORMAL); 50 51 status_t initCheck() const; 52 53 virtual status_t start(MetaData *params = NULL); stopAudioSource54 virtual status_t stop() { return reset(); } 55 virtual sp<MetaData> getFormat(); 56 57 // Returns the maximum amplitude since last call. 58 int16_t getMaxAmplitude(); 59 60 virtual status_t read( 61 MediaBufferBase **buffer, const ReadOptions *options = NULL); 62 virtual status_t setStopTimeUs(int64_t stopTimeUs); 63 64 status_t dataCallback(const AudioRecord::Buffer& buffer); 65 virtual void signalBufferReturned(MediaBufferBase *buffer); 66 67 status_t setInputDevice(audio_port_handle_t deviceId); 68 status_t getRoutedDeviceId(audio_port_handle_t* deviceId); 69 status_t addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback); 70 status_t removeAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback); 71 72 status_t getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones); 73 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction); 74 status_t setPreferredMicrophoneFieldDimension(float zoom); 75 76 status_t getPortId(audio_port_handle_t *portId) const; 77 78 protected: 79 virtual ~AudioSource(); 80 81 private: 82 enum { 83 kMaxBufferSize = 2048, 84 85 // After the initial mute, we raise the volume linearly 86 // over kAutoRampDurationUs. 87 kAutoRampDurationUs = 300000, 88 89 // This is the initial mute duration to suppress 90 // the video recording signal tone 91 kAutoRampStartUs = 0, 92 }; 93 94 Mutex mLock; 95 Condition mFrameAvailableCondition; 96 Condition mFrameEncodingCompletionCondition; 97 98 sp<AudioRecord> mRecord; 99 status_t mInitCheck; 100 bool mStarted; 101 int32_t mSampleRate; 102 int32_t mOutSampleRate; 103 104 bool mTrackMaxAmplitude; 105 int64_t mStartTimeUs; 106 int64_t mStopSystemTimeUs; 107 int64_t mLastFrameTimestampUs; 108 int16_t mMaxAmplitude; 109 int64_t mPrevSampleTimeUs; 110 int64_t mInitialReadTimeUs; 111 int64_t mNumFramesReceived; 112 int64_t mNumFramesSkipped; 113 int64_t mNumFramesLost; 114 int64_t mNumClientOwnedBuffers; 115 bool mNoMoreFramesToRead; 116 117 List<MediaBuffer * > mBuffersReceived; 118 119 void trackMaxAmplitude(int16_t *data, int nSamples); 120 121 // This is used to raise the volume from mute to the 122 // actual level linearly. 123 void rampVolume( 124 int32_t startFrame, int32_t rampDurationFrames, 125 uint8_t *data, size_t bytes); 126 127 void queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs); 128 void releaseQueuedFrames_l(); 129 void waitOutstandingEncodingFrames_l(); 130 status_t reset(); 131 132 AudioSource(const AudioSource &); 133 AudioSource &operator=(const AudioSource &); 134 }; 135 136 } // namespace android 137 138 #endif // AUDIO_SOURCE_H_ 139