Home | History | Annotate | Download | only in stagefright
      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 namespace android {
     30 
     31 class AudioRecord;
     32 
     33 struct 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             uint32_t sampleRate,
     39             uint32_t channels = 1);
     40 
     41     status_t initCheck() const;
     42 
     43     virtual status_t start(MetaData *params = NULL);
     44     virtual status_t stop() { return reset(); }
     45     virtual sp<MetaData> getFormat();
     46 
     47     // Returns the maximum amplitude since last call.
     48     int16_t getMaxAmplitude();
     49 
     50     virtual status_t read(
     51             MediaBuffer **buffer, const ReadOptions *options = NULL);
     52 
     53     status_t dataCallback(const AudioRecord::Buffer& buffer);
     54     virtual void signalBufferReturned(MediaBuffer *buffer);
     55 
     56 protected:
     57     virtual ~AudioSource();
     58 
     59 private:
     60     enum {
     61         kMaxBufferSize = 2048,
     62 
     63         // After the initial mute, we raise the volume linearly
     64         // over kAutoRampDurationUs.
     65         kAutoRampDurationUs = 300000,
     66 
     67         // This is the initial mute duration to suppress
     68         // the video recording signal tone
     69         kAutoRampStartUs = 0,
     70     };
     71 
     72     Mutex mLock;
     73     Condition mFrameAvailableCondition;
     74     Condition mFrameEncodingCompletionCondition;
     75 
     76     sp<AudioRecord> mRecord;
     77     status_t mInitCheck;
     78     bool mStarted;
     79     int32_t mSampleRate;
     80 
     81     bool mTrackMaxAmplitude;
     82     int64_t mStartTimeUs;
     83     int16_t mMaxAmplitude;
     84     int64_t mPrevSampleTimeUs;
     85     int64_t mInitialReadTimeUs;
     86     int64_t mNumFramesReceived;
     87     int64_t mNumClientOwnedBuffers;
     88 
     89     List<MediaBuffer * > mBuffersReceived;
     90 
     91     void trackMaxAmplitude(int16_t *data, int nSamples);
     92 
     93     // This is used to raise the volume from mute to the
     94     // actual level linearly.
     95     void rampVolume(
     96         int32_t startFrame, int32_t rampDurationFrames,
     97         uint8_t *data,   size_t bytes);
     98 
     99     void queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs);
    100     void releaseQueuedFrames_l();
    101     void waitOutstandingEncodingFrames_l();
    102     status_t reset();
    103 
    104     AudioSource(const AudioSource &);
    105     AudioSource &operator=(const AudioSource &);
    106 };
    107 
    108 }  // namespace android
    109 
    110 #endif  // AUDIO_SOURCE_H_
    111