Home | History | Annotate | Download | only in stagefright
      1 /*
      2  * Copyright (C) 2014 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 MediaCodecSource_H_
     18 #define MediaCodecSource_H_
     19 
     20 #include <media/stagefright/foundation/ABase.h>
     21 #include <media/stagefright/foundation/AHandlerReflector.h>
     22 #include <media/stagefright/MediaSource.h>
     23 
     24 namespace android {
     25 
     26 class ALooper;
     27 class AMessage;
     28 class IGraphicBufferProducer;
     29 class MediaCodec;
     30 class MetaData;
     31 
     32 struct MediaCodecSource : public MediaSource,
     33                           public MediaBufferObserver {
     34     enum FlagBits {
     35         FLAG_USE_SURFACE_INPUT      = 1,
     36         FLAG_USE_METADATA_INPUT     = 2,
     37     };
     38 
     39     static sp<MediaCodecSource> Create(
     40             const sp<ALooper> &looper,
     41             const sp<AMessage> &format,
     42             const sp<MediaSource> &source,
     43             uint32_t flags = 0);
     44 
     45     bool isVideo() const { return mIsVideo; }
     46     sp<IGraphicBufferProducer> getGraphicBufferProducer();
     47 
     48     // MediaSource
     49     virtual status_t start(MetaData *params = NULL);
     50     virtual status_t stop();
     51     virtual status_t pause();
     52     virtual sp<MetaData> getFormat() { return mMeta; }
     53     virtual status_t read(
     54             MediaBuffer **buffer,
     55             const ReadOptions *options = NULL);
     56 
     57     // MediaBufferObserver
     58     virtual void signalBufferReturned(MediaBuffer *buffer);
     59 
     60     // for AHandlerReflector
     61     void onMessageReceived(const sp<AMessage> &msg);
     62 
     63 protected:
     64     virtual ~MediaCodecSource();
     65 
     66 private:
     67     struct Puller;
     68 
     69     enum {
     70         kWhatPullerNotify,
     71         kWhatEncoderActivity,
     72         kWhatStart,
     73         kWhatStop,
     74         kWhatPause,
     75     };
     76 
     77     MediaCodecSource(
     78             const sp<ALooper> &looper,
     79             const sp<AMessage> &outputFormat,
     80             const sp<MediaSource> &source,
     81             uint32_t flags = 0);
     82 
     83     status_t onStart(MetaData *params);
     84     status_t init();
     85     status_t initEncoder();
     86     void releaseEncoder();
     87     status_t feedEncoderInputBuffers();
     88     void scheduleDoMoreWork();
     89     status_t doMoreWork(int32_t numInput, int32_t numOutput);
     90     void suspend();
     91     void resume(int64_t skipFramesBeforeUs = -1ll);
     92     void signalEOS(status_t err = ERROR_END_OF_STREAM);
     93     bool reachedEOS();
     94     status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
     95 
     96     sp<ALooper> mLooper;
     97     sp<ALooper> mCodecLooper;
     98     sp<AHandlerReflector<MediaCodecSource> > mReflector;
     99     sp<AMessage> mOutputFormat;
    100     sp<MetaData> mMeta;
    101     sp<Puller> mPuller;
    102     sp<MediaCodec> mEncoder;
    103     uint32_t mFlags;
    104     List<uint32_t> mStopReplyIDQueue;
    105     bool mIsVideo;
    106     bool mStarted;
    107     bool mStopping;
    108     bool mDoMoreWorkPending;
    109     sp<AMessage> mEncoderActivityNotify;
    110     sp<IGraphicBufferProducer> mGraphicBufferProducer;
    111     Vector<sp<ABuffer> > mEncoderInputBuffers;
    112     Vector<sp<ABuffer> > mEncoderOutputBuffers;
    113     List<MediaBuffer *> mInputBufferQueue;
    114     List<size_t> mAvailEncoderInputIndices;
    115     List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
    116 
    117     // audio drift time
    118     int64_t mFirstSampleTimeUs;
    119     List<int64_t> mDriftTimeQueue;
    120 
    121     // following variables are protected by mOutputBufferLock
    122     Mutex mOutputBufferLock;
    123     Condition mOutputBufferCond;
    124     List<MediaBuffer*> mOutputBufferQueue;
    125     bool mEncoderReachedEOS;
    126     status_t mErrorCode;
    127 
    128     DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource);
    129 };
    130 
    131 } // namespace android
    132 
    133 #endif /* MediaCodecSource_H_ */
    134