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 "WifiDisplaySource.h"
     22 
     23 #include <media/stagefright/foundation/AHandler.h>
     24 
     25 namespace android {
     26 
     27 struct ABuffer;
     28 struct MediaCodec;
     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     Converter(
     37             const sp<AMessage> &notify,
     38             const sp<ALooper> &codecLooper,
     39             const sp<AMessage> &format,
     40             bool usePCMAudio);
     41 
     42     status_t initCheck() const;
     43 
     44     size_t getInputBufferCount() const;
     45 
     46     sp<AMessage> getOutputFormat() const;
     47     bool needToManuallyPrependSPSPPS() const;
     48 
     49     void feedAccessUnit(const sp<ABuffer> &accessUnit);
     50     void signalEOS();
     51 
     52     void requestIDRFrame();
     53 
     54     void dropAFrame();
     55 
     56     enum {
     57         kWhatAccessUnit,
     58         kWhatEOS,
     59         kWhatError,
     60     };
     61 
     62     enum {
     63         kWhatDoMoreWork,
     64         kWhatRequestIDRFrame,
     65         kWhatShutdown,
     66         kWhatMediaPullerNotify,
     67         kWhatEncoderActivity,
     68         kWhatDropAFrame,
     69     };
     70 
     71     void shutdownAsync();
     72 
     73     int32_t getVideoBitrate() const;
     74     void setVideoBitrate(int32_t bitrate);
     75 
     76     static int32_t GetInt32Property(const char *propName, int32_t defaultValue);
     77 
     78 protected:
     79     virtual ~Converter();
     80     virtual void onMessageReceived(const sp<AMessage> &msg);
     81 
     82 private:
     83     status_t mInitCheck;
     84     sp<AMessage> mNotify;
     85     sp<ALooper> mCodecLooper;
     86     sp<AMessage> mInputFormat;
     87     bool mIsVideo;
     88     bool mIsPCMAudio;
     89     sp<AMessage> mOutputFormat;
     90     bool mNeedToManuallyPrependSPSPPS;
     91 
     92     sp<MediaCodec> mEncoder;
     93     sp<AMessage> mEncoderActivityNotify;
     94 
     95     Vector<sp<ABuffer> > mEncoderInputBuffers;
     96     Vector<sp<ABuffer> > mEncoderOutputBuffers;
     97 
     98     List<size_t> mAvailEncoderInputIndices;
     99 
    100     List<sp<ABuffer> > mInputBufferQueue;
    101 
    102     bool mDoMoreWorkPending;
    103 
    104 #if ENABLE_SILENCE_DETECTION
    105     int64_t mFirstSilentFrameUs;
    106     bool mInSilentMode;
    107 #endif
    108 
    109     sp<ABuffer> mPartialAudioAU;
    110 
    111     int32_t mPrevVideoBitrate;
    112 
    113     int32_t mNumFramesToDrop;
    114 
    115     status_t initEncoder();
    116     void releaseEncoder();
    117 
    118     status_t feedEncoderInputBuffers();
    119 
    120     void scheduleDoMoreWork();
    121     status_t doMoreWork();
    122 
    123     void notifyError(status_t err);
    124 
    125     // Packetizes raw PCM audio data available in mInputBufferQueue
    126     // into a format suitable for transport stream inclusion and
    127     // notifies the observer.
    128     status_t feedRawAudioInputBuffers();
    129 
    130     static bool IsSilence(const sp<ABuffer> &accessUnit);
    131 
    132     DISALLOW_EVIL_CONSTRUCTORS(Converter);
    133 };
    134 
    135 }  // namespace android
    136 
    137 #endif  // CONVERTER_H_
    138