Home | History | Annotate | Download | only in source
      1 /*
      2  * Copyright 2012, 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 CONVERTER_H_
     18 
     19 #define CONVERTER_H_
     20 
     21 #include <media/stagefright/foundation/AHandler.h>
     22 
     23 namespace android {
     24 
     25 struct ABuffer;
     26 class IGraphicBufferProducer;
     27 struct MediaCodec;
     28 class MediaCodecBuffer;
     29 
     30 #define ENABLE_SILENCE_DETECTION        0
     31 
     32 // Utility class that receives media access units and converts them into
     33 // media access unit of a different format.
     34 // Right now this'll convert raw video into H.264 and raw audio into AAC.
     35 struct Converter : public AHandler {
     36     enum {
     37         kWhatAccessUnit,
     38         kWhatEOS,
     39         kWhatError,
     40         kWhatShutdownCompleted,
     41     };
     42 
     43     enum FlagBits {
     44         FLAG_USE_SURFACE_INPUT          = 1,
     45         FLAG_PREPEND_CSD_IF_NECESSARY   = 2,
     46     };
     47     Converter(const sp<AMessage> &notify,
     48               const sp<ALooper> &codecLooper,
     49               const sp<AMessage> &outputFormat,
     50               uint32_t flags = 0);
     51 
     52     status_t init();
     53 
     54     sp<IGraphicBufferProducer> getGraphicBufferProducer();
     55 
     56     size_t getInputBufferCount() const;
     57 
     58     sp<AMessage> getOutputFormat() const;
     59     bool needToManuallyPrependSPSPPS() const;
     60 
     61     void feedAccessUnit(const sp<ABuffer> &accessUnit);
     62     void signalEOS();
     63 
     64     void requestIDRFrame();
     65 
     66     void dropAFrame();
     67     void suspendEncoding(bool suspend);
     68 
     69     void shutdownAsync();
     70 
     71     int32_t getVideoBitrate() const;
     72     void setVideoBitrate(int32_t bitrate);
     73 
     74     static int32_t GetInt32Property(const char *propName, int32_t defaultValue);
     75 
     76     enum {
     77         // MUST not conflict with private enums below.
     78         kWhatMediaPullerNotify = 'pulN',
     79     };
     80 
     81 protected:
     82     virtual ~Converter();
     83     virtual void onMessageReceived(const sp<AMessage> &msg);
     84 
     85 private:
     86     enum {
     87         kWhatDoMoreWork,
     88         kWhatRequestIDRFrame,
     89         kWhatSuspendEncoding,
     90         kWhatShutdown,
     91         kWhatEncoderActivity,
     92         kWhatDropAFrame,
     93         kWhatReleaseOutputBuffer,
     94     };
     95 
     96     sp<AMessage> mNotify;
     97     sp<ALooper> mCodecLooper;
     98     sp<AMessage> mOutputFormat;
     99     uint32_t mFlags;
    100     bool mIsVideo;
    101     bool mIsH264;
    102     bool mIsPCMAudio;
    103     bool mNeedToManuallyPrependSPSPPS;
    104 
    105     sp<MediaCodec> mEncoder;
    106     sp<AMessage> mEncoderActivityNotify;
    107 
    108     sp<IGraphicBufferProducer> mGraphicBufferProducer;
    109 
    110     Vector<sp<MediaCodecBuffer> > mEncoderInputBuffers;
    111     Vector<sp<MediaCodecBuffer> > mEncoderOutputBuffers;
    112 
    113     List<size_t> mAvailEncoderInputIndices;
    114 
    115     List<sp<ABuffer> > mInputBufferQueue;
    116 
    117     sp<ABuffer> mCSD0;
    118 
    119     bool mDoMoreWorkPending;
    120 
    121 #if ENABLE_SILENCE_DETECTION
    122     int64_t mFirstSilentFrameUs;
    123     bool mInSilentMode;
    124 #endif
    125 
    126     sp<ABuffer> mPartialAudioAU;
    127 
    128     int32_t mPrevVideoBitrate;
    129 
    130     int32_t mNumFramesToDrop;
    131     bool mEncodingSuspended;
    132 
    133     status_t initEncoder();
    134     void releaseEncoder();
    135 
    136     status_t feedEncoderInputBuffers();
    137 
    138     void scheduleDoMoreWork();
    139     status_t doMoreWork();
    140 
    141     void notifyError(status_t err);
    142 
    143     // Packetizes raw PCM audio data available in mInputBufferQueue
    144     // into a format suitable for transport stream inclusion and
    145     // notifies the observer.
    146     status_t feedRawAudioInputBuffers();
    147 
    148     static bool IsSilence(const sp<ABuffer> &accessUnit);
    149 
    150     sp<ABuffer> prependCSD(const sp<ABuffer> &accessUnit) const;
    151 
    152     DISALLOW_EVIL_CONSTRUCTORS(Converter);
    153 };
    154 
    155 }  // namespace android
    156 
    157 #endif  // CONVERTER_H_
    158