Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 21 | #include <media/AudioRecord.h> |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 22 | #include <media/AudioSystem.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 23 | #include <media/stagefright/MediaSource.h> |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 24 | #include <media/stagefright/MediaBuffer.h> |
| 25 | #include <utils/List.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class AudioRecord; |
| 30 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 31 | struct AudioSource : public MediaSource, public MediaBufferObserver { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 32 | // Note that the "channels" parameter is _not_ the number of channels, |
| 33 | // but a bitmask of AudioSystem::audio_channels constants. |
| 34 | AudioSource( |
| 35 | int inputSource, uint32_t sampleRate, |
| 36 | uint32_t channels = AudioSystem::CHANNEL_IN_MONO); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 37 | |
| 38 | status_t initCheck() const; |
| 39 | |
| 40 | virtual status_t start(MetaData *params = NULL); |
| 41 | virtual status_t stop(); |
| 42 | virtual sp<MetaData> getFormat(); |
| 43 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 44 | // Returns the maximum amplitude since last call. |
| 45 | int16_t getMaxAmplitude(); |
| 46 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 47 | virtual status_t read( |
| 48 | MediaBuffer **buffer, const ReadOptions *options = NULL); |
| 49 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 50 | status_t dataCallbackTimestamp(const AudioRecord::Buffer& buffer, int64_t timeUs); |
| 51 | virtual void signalBufferReturned(MediaBuffer *buffer); |
| 52 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 53 | protected: |
| 54 | virtual ~AudioSource(); |
| 55 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 56 | private: |
James Dong | d7f1c3d | 2010-08-26 16:28:17 -0700 | [diff] [blame] | 57 | enum { |
| 58 | kMaxBufferSize = 2048, |
| 59 | |
| 60 | // After the initial mute, we raise the volume linearly |
| 61 | // over kAutoRampDurationUs. |
James Dong | cbeebb1 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 62 | kAutoRampDurationUs = 300000, |
James Dong | d7f1c3d | 2010-08-26 16:28:17 -0700 | [diff] [blame] | 63 | |
| 64 | // This is the initial mute duration to suppress |
| 65 | // the video recording signal tone |
James Dong | cbeebb1 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 66 | kAutoRampStartUs = 0, |
| 67 | }; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 68 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 69 | Mutex mLock; |
| 70 | Condition mFrameAvailableCondition; |
| 71 | Condition mFrameEncodingCompletionCondition; |
| 72 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 73 | AudioRecord *mRecord; |
| 74 | status_t mInitCheck; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 75 | bool mStarted; |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 76 | int32_t mSampleRate; |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 77 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 78 | bool mTrackMaxAmplitude; |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 79 | int64_t mStartTimeUs; |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 80 | int16_t mMaxAmplitude; |
James Dong | c3ae937 | 2010-07-29 18:06:08 -0700 | [diff] [blame] | 81 | int64_t mPrevSampleTimeUs; |
James Dong | 4c23815 | 2010-09-01 18:48:35 -0700 | [diff] [blame] | 82 | int64_t mInitialReadTimeUs; |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 83 | int64_t mNumFramesReceived; |
| 84 | int64_t mNumClientOwnedBuffers; |
James Dong | dae9fd3 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 85 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 86 | List<MediaBuffer * > mBuffersReceived; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 87 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 88 | void trackMaxAmplitude(int16_t *data, int nSamples); |
| 89 | |
James Dong | d7f1c3d | 2010-08-26 16:28:17 -0700 | [diff] [blame] | 90 | // This is used to raise the volume from mute to the |
| 91 | // actual level linearly. |
| 92 | void rampVolume( |
| 93 | int32_t startFrame, int32_t rampDurationFrames, |
| 94 | uint8_t *data, size_t bytes); |
| 95 | |
James Dong | d33a4cc | 2011-02-15 10:08:07 -0800 | [diff] [blame] | 96 | void releaseQueuedFrames_l(); |
| 97 | void waitOutstandingEncodingFrames_l(); |
| 98 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 99 | AudioSource(const AudioSource &); |
| 100 | AudioSource &operator=(const AudioSource &); |
| 101 | }; |
| 102 | |
| 103 | } // namespace android |
| 104 | |
| 105 | #endif // AUDIO_SOURCE_H_ |