Home | History | Annotate | Download | only in include
      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/foundation/Mutexed.h>
     23 #include <media/stagefright/MediaSource.h>
     24 #include <media/stagefright/PersistentSurface.h>
     25 
     26 namespace android {
     27 
     28 struct ALooper;
     29 struct AMessage;
     30 struct AReplyToken;
     31 class IGraphicBufferProducer;
     32 struct MediaCodec;
     33 class MetaData;
     34 
     35 struct MediaCodecSource : public MediaSource,
     36                           public MediaBufferObserver {
     37     enum FlagBits {
     38         FLAG_USE_SURFACE_INPUT      = 1,
     39         FLAG_PREFER_SOFTWARE_CODEC  = 4,  // used for testing only
     40     };
     41 
     42     static sp<MediaCodecSource> Create(
     43             const sp<ALooper> &looper,
     44             const sp<AMessage> &format,
     45             const sp<MediaSource> &source,
     46             const sp<PersistentSurface> &persistentSurface = NULL,
     47             uint32_t flags = 0);
     48 
     49     bool isVideo() const { return mIsVideo; }
     50     sp<IGraphicBufferProducer> getGraphicBufferProducer();
     51     status_t setInputBufferTimeOffset(int64_t timeOffsetUs);
     52     int64_t getFirstSampleSystemTimeUs();
     53 
     54     // MediaSource
     55     virtual status_t start(MetaData *params = NULL);
     56     virtual status_t stop();
     57     virtual status_t pause(MetaData *params = NULL);
     58     virtual sp<MetaData> getFormat();
     59     virtual status_t read(
     60             MediaBuffer **buffer,
     61             const ReadOptions *options = NULL);
     62 
     63     // MediaBufferObserver
     64     virtual void signalBufferReturned(MediaBuffer *buffer);
     65 
     66     // for AHandlerReflector
     67     void onMessageReceived(const sp<AMessage> &msg);
     68 
     69     // Set GraphicBufferSource stop time. GraphicBufferSource will stop
     70     // after receiving a buffer with timestamp larger or equal than stopTimeUs.
     71     // All the buffers with timestamp larger or equal to stopTimeUs will be
     72     // discarded. stopTimeUs uses SYSTEM_TIME_MONOTONIC time base.
     73     status_t setStopStimeUs(int64_t stopTimeUs);
     74 
     75 protected:
     76     virtual ~MediaCodecSource();
     77 
     78 private:
     79     struct Puller;
     80 
     81     enum {
     82         kWhatPullerNotify,
     83         kWhatEncoderActivity,
     84         kWhatStart,
     85         kWhatStop,
     86         kWhatPause,
     87         kWhatSetInputBufferTimeOffset,
     88         kWhatSetStopTimeOffset,
     89         kWhatGetFirstSampleSystemTimeUs,
     90         kWhatStopStalled,
     91     };
     92 
     93     MediaCodecSource(
     94             const sp<ALooper> &looper,
     95             const sp<AMessage> &outputFormat,
     96             const sp<MediaSource> &source,
     97             const sp<PersistentSurface> &persistentSurface,
     98             uint32_t flags = 0);
     99 
    100     status_t onStart(MetaData *params);
    101 
    102     // Pause the source at pauseStartTimeUs. For non-surface input,
    103     // buffers will be dropped immediately. For surface input, buffers
    104     // with timestamp smaller than pauseStartTimeUs will still be encoded.
    105     // Buffers with timestamp larger or queal to pauseStartTimeUs will be
    106     // dropped. pauseStartTimeUs uses SYSTEM_TIME_MONOTONIC time base.
    107     void onPause(int64_t pauseStartTimeUs);
    108 
    109     status_t init();
    110     status_t initEncoder();
    111     void releaseEncoder();
    112     status_t feedEncoderInputBuffers();
    113     // Resume GraphicBufferSource at resumeStartTimeUs. Buffers
    114     // from GraphicBufferSource with timestamp larger or equal to
    115     // resumeStartTimeUs will be encoded. resumeStartTimeUs uses
    116     // SYSTEM_TIME_MONOTONIC time base.
    117     void resume(int64_t resumeStartTimeUs = -1ll);
    118     void signalEOS(status_t err = ERROR_END_OF_STREAM);
    119     bool reachedEOS();
    120     status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg);
    121 
    122     sp<ALooper> mLooper;
    123     sp<ALooper> mCodecLooper;
    124     sp<AHandlerReflector<MediaCodecSource> > mReflector;
    125     sp<AMessage> mOutputFormat;
    126     Mutexed<sp<MetaData>> mMeta;
    127     sp<Puller> mPuller;
    128     sp<MediaCodec> mEncoder;
    129     uint32_t mFlags;
    130     List<sp<AReplyToken>> mStopReplyIDQueue;
    131     bool mIsVideo;
    132     bool mStarted;
    133     bool mStopping;
    134     bool mDoMoreWorkPending;
    135     bool mSetEncoderFormat;
    136     int32_t mEncoderFormat;
    137     int32_t mEncoderDataSpace;
    138     sp<AMessage> mEncoderActivityNotify;
    139     sp<IGraphicBufferProducer> mGraphicBufferProducer;
    140     sp<PersistentSurface> mPersistentSurface;
    141     List<MediaBuffer *> mInputBufferQueue;
    142     List<size_t> mAvailEncoderInputIndices;
    143     List<int64_t> mDecodingTimeQueue; // decoding time (us) for video
    144     int64_t mInputBufferTimeOffsetUs;
    145     int64_t mFirstSampleSystemTimeUs;
    146     bool mPausePending;
    147 
    148     // audio drift time
    149     int64_t mFirstSampleTimeUs;
    150     List<int64_t> mDriftTimeQueue;
    151 
    152     struct Output {
    153         Output();
    154         List<MediaBuffer*> mBufferQueue;
    155         bool mEncoderReachedEOS;
    156         status_t mErrorCode;
    157         Condition mCond;
    158     };
    159     Mutexed<Output> mOutput;
    160 
    161     int32_t mGeneration;
    162 
    163     DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource);
    164 };
    165 
    166 } // namespace android
    167 
    168 #endif /* MediaCodecSource_H_ */
    169