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/AudioSystem.h>
     22 #include <media/stagefright/MediaSource.h>
     23 
     24 namespace android {
     25 
     26 class AudioRecord;
     27 struct MediaBufferGroup;
     28 
     29 struct AudioSource : public MediaSource {
     30     // Note that the "channels" parameter is _not_ the number of channels,
     31     // but a bitmask of AudioSystem::audio_channels constants.
     32     AudioSource(
     33             int inputSource, uint32_t sampleRate,
     34             uint32_t channels = AudioSystem::CHANNEL_IN_MONO);
     35 
     36     status_t initCheck() const;
     37 
     38     virtual status_t start(MetaData *params = NULL);
     39     virtual status_t stop();
     40     virtual sp<MetaData> getFormat();
     41 
     42     // Returns the maximum amplitude since last call.
     43     int16_t getMaxAmplitude();
     44 
     45     virtual status_t read(
     46             MediaBuffer **buffer, const ReadOptions *options = NULL);
     47 
     48 protected:
     49     virtual ~AudioSource();
     50 
     51 private:
     52     enum {
     53         kMaxBufferSize = 2048,
     54 
     55         // After the initial mute, we raise the volume linearly
     56         // over kAutoRampDurationUs.
     57         kAutoRampDurationUs = 300000,
     58 
     59         // This is the initial mute duration to suppress
     60         // the video recording signal tone
     61         kAutoRampStartUs = 700000,
     62       };
     63 
     64     AudioRecord *mRecord;
     65     status_t mInitCheck;
     66     bool mStarted;
     67 
     68     bool mCollectStats;
     69     bool mTrackMaxAmplitude;
     70     int64_t mStartTimeUs;
     71     int16_t mMaxAmplitude;
     72     int64_t mPrevSampleTimeUs;
     73     int64_t mTotalLostFrames;
     74     int64_t mPrevLostBytes;
     75     int64_t mInitialReadTimeUs;
     76 
     77     MediaBufferGroup *mGroup;
     78 
     79     void trackMaxAmplitude(int16_t *data, int nSamples);
     80 
     81     // This is used to raise the volume from mute to the
     82     // actual level linearly.
     83     void rampVolume(
     84         int32_t startFrame, int32_t rampDurationFrames,
     85         uint8_t *data,   size_t bytes);
     86 
     87     AudioSource(const AudioSource &);
     88     AudioSource &operator=(const AudioSource &);
     89 };
     90 
     91 }  // namespace android
     92 
     93 #endif  // AUDIO_SOURCE_H_
     94