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 virtual status_t setStopTimeUs(int64_t stopTimeUs); 57 58 status_t dataCallback(const AudioRecord::Buffer& buffer); 59 virtual void signalBufferReturned(MediaBuffer *buffer); 60 61protected: 62 virtual ~AudioSource(); 63 64private: 65 enum { 66 kMaxBufferSize = 2048, 67 68 // After the initial mute, we raise the volume linearly 69 // over kAutoRampDurationUs. 70 kAutoRampDurationUs = 300000, 71 72 // This is the initial mute duration to suppress 73 // the video recording signal tone 74 kAutoRampStartUs = 0, 75 }; 76 77 Mutex mLock; 78 Condition mFrameAvailableCondition; 79 Condition mFrameEncodingCompletionCondition; 80 81 sp<AudioRecord> mRecord; 82 status_t mInitCheck; 83 bool mStarted; 84 int32_t mSampleRate; 85 int32_t mOutSampleRate; 86 87 bool mTrackMaxAmplitude; 88 int64_t mStartTimeUs; 89 int64_t mStopSystemTimeUs; 90 int64_t mLastFrameTimestampUs; 91 int16_t mMaxAmplitude; 92 int64_t mPrevSampleTimeUs; 93 int64_t mInitialReadTimeUs; 94 int64_t mNumFramesReceived; 95 int64_t mNumFramesSkipped; 96 int64_t mNumFramesLost; 97 int64_t mNumClientOwnedBuffers; 98 bool mNoMoreFramesToRead; 99 100 List<MediaBuffer * > mBuffersReceived; 101 102 void trackMaxAmplitude(int16_t *data, int nSamples); 103 104 // This is used to raise the volume from mute to the 105 // actual level linearly. 106 void rampVolume( 107 int32_t startFrame, int32_t rampDurationFrames, 108 uint8_t *data, size_t bytes); 109 110 void queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs); 111 void releaseQueuedFrames_l(); 112 void waitOutstandingEncodingFrames_l(); 113 status_t reset(); 114 115 AudioSource(const AudioSource &); 116 AudioSource &operator=(const AudioSource &); 117}; 118 119} // namespace android 120 121#endif // AUDIO_SOURCE_H_ 122