blob: 9e6f0e2277928dace011c9a18bc6ccb2ae4aaddf [file] [log] [blame]
Andreas Hubere46b7be2009-07-14 16:56:47 -07001/*
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 Dongd33a4cc2011-02-15 10:08:07 -080021#include <media/AudioRecord.h>
Andreas Huber07bf09d2010-01-25 14:27:12 -080022#include <media/AudioSystem.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070023#include <media/stagefright/MediaSource.h>
James Dongd33a4cc2011-02-15 10:08:07 -080024#include <media/stagefright/MediaBuffer.h>
25#include <utils/List.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070026
27namespace android {
28
29class AudioRecord;
30
James Dongd33a4cc2011-02-15 10:08:07 -080031struct AudioSource : public MediaSource, public MediaBufferObserver {
Andreas Huber07bf09d2010-01-25 14:27:12 -080032 // 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 Hubere46b7be2009-07-14 16:56:47 -070037
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 Dong57e7f832010-06-24 19:55:31 -070044 // Returns the maximum amplitude since last call.
45 int16_t getMaxAmplitude();
46
Andreas Hubere46b7be2009-07-14 16:56:47 -070047 virtual status_t read(
48 MediaBuffer **buffer, const ReadOptions *options = NULL);
49
James Dongd33a4cc2011-02-15 10:08:07 -080050 status_t dataCallbackTimestamp(const AudioRecord::Buffer& buffer, int64_t timeUs);
51 virtual void signalBufferReturned(MediaBuffer *buffer);
52
Andreas Huber07bf09d2010-01-25 14:27:12 -080053protected:
54 virtual ~AudioSource();
55
Andreas Hubere46b7be2009-07-14 16:56:47 -070056private:
James Dongd7f1c3d2010-08-26 16:28:17 -070057 enum {
58 kMaxBufferSize = 2048,
59
60 // After the initial mute, we raise the volume linearly
61 // over kAutoRampDurationUs.
James Dongcbeebb12011-02-16 12:28:26 -080062 kAutoRampDurationUs = 300000,
James Dongd7f1c3d2010-08-26 16:28:17 -070063
64 // This is the initial mute duration to suppress
65 // the video recording signal tone
James Dongcbeebb12011-02-16 12:28:26 -080066 kAutoRampStartUs = 0,
67 };
Andreas Huber07bf09d2010-01-25 14:27:12 -080068
James Dongd33a4cc2011-02-15 10:08:07 -080069 Mutex mLock;
70 Condition mFrameAvailableCondition;
71 Condition mFrameEncodingCompletionCondition;
72
Andreas Hubere46b7be2009-07-14 16:56:47 -070073 AudioRecord *mRecord;
74 status_t mInitCheck;
Andreas Huber07bf09d2010-01-25 14:27:12 -080075 bool mStarted;
James Dongd33a4cc2011-02-15 10:08:07 -080076 int32_t mSampleRate;
James Dongdae9fd32010-06-04 13:59:27 -070077
James Dong57e7f832010-06-24 19:55:31 -070078 bool mTrackMaxAmplitude;
James Dong36e573b2010-06-19 09:04:18 -070079 int64_t mStartTimeUs;
James Dong57e7f832010-06-24 19:55:31 -070080 int16_t mMaxAmplitude;
James Dongc3ae9372010-07-29 18:06:08 -070081 int64_t mPrevSampleTimeUs;
James Dong4c238152010-09-01 18:48:35 -070082 int64_t mInitialReadTimeUs;
James Dongd33a4cc2011-02-15 10:08:07 -080083 int64_t mNumFramesReceived;
84 int64_t mNumClientOwnedBuffers;
James Dongdae9fd32010-06-04 13:59:27 -070085
James Dongd33a4cc2011-02-15 10:08:07 -080086 List<MediaBuffer * > mBuffersReceived;
Andreas Hubere46b7be2009-07-14 16:56:47 -070087
James Dong57e7f832010-06-24 19:55:31 -070088 void trackMaxAmplitude(int16_t *data, int nSamples);
89
James Dongd7f1c3d2010-08-26 16:28:17 -070090 // 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 Dongd33a4cc2011-02-15 10:08:07 -080096 void releaseQueuedFrames_l();
97 void waitOutstandingEncodingFrames_l();
98
Andreas Hubere46b7be2009-07-14 16:56:47 -070099 AudioSource(const AudioSource &);
100 AudioSource &operator=(const AudioSource &);
101};
102
103} // namespace android
104
105#endif // AUDIO_SOURCE_H_