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 25#include <gui/IGraphicBufferConsumer.h> 26 27namespace android { 28 29struct ALooper; 30struct AMessage; 31struct AReplyToken; 32class IGraphicBufferProducer; 33struct MediaCodec; 34class MetaData; 35 36struct MediaCodecSource : public MediaSource, 37 public MediaBufferObserver { 38 enum FlagBits { 39 FLAG_USE_SURFACE_INPUT = 1, 40 FLAG_PREFER_SOFTWARE_CODEC = 4, // used for testing only 41 }; 42 43 static sp<MediaCodecSource> Create( 44 const sp<ALooper> &looper, 45 const sp<AMessage> &format, 46 const sp<MediaSource> &source, 47 const sp<IGraphicBufferConsumer> &consumer = NULL, 48 uint32_t flags = 0); 49 50 bool isVideo() const { return mIsVideo; } 51 sp<IGraphicBufferProducer> getGraphicBufferProducer(); 52 status_t setInputBufferTimeOffset(int64_t timeOffsetUs); 53 int64_t getFirstSampleSystemTimeUs(); 54 55 // MediaSource 56 virtual status_t start(MetaData *params = NULL); 57 virtual status_t stop(); 58 virtual status_t pause(); 59 virtual sp<MetaData> getFormat(); 60 virtual status_t read( 61 MediaBuffer **buffer, 62 const ReadOptions *options = NULL); 63 64 // MediaBufferObserver 65 virtual void signalBufferReturned(MediaBuffer *buffer); 66 67 // for AHandlerReflector 68 void onMessageReceived(const sp<AMessage> &msg); 69 70protected: 71 virtual ~MediaCodecSource(); 72 73private: 74 struct Puller; 75 76 enum { 77 kWhatPullerNotify, 78 kWhatEncoderActivity, 79 kWhatStart, 80 kWhatStop, 81 kWhatPause, 82 kWhatSetInputBufferTimeOffset, 83 kWhatGetFirstSampleSystemTimeUs, 84 kWhatStopStalled, 85 }; 86 87 MediaCodecSource( 88 const sp<ALooper> &looper, 89 const sp<AMessage> &outputFormat, 90 const sp<MediaSource> &source, 91 const sp<IGraphicBufferConsumer> &consumer, 92 uint32_t flags = 0); 93 94 status_t onStart(MetaData *params); 95 void onPause(); 96 status_t init(); 97 status_t initEncoder(); 98 void releaseEncoder(); 99 status_t feedEncoderInputBuffers(); 100 void suspend(); 101 void resume(int64_t skipFramesBeforeUs = -1ll); 102 void signalEOS(status_t err = ERROR_END_OF_STREAM); 103 bool reachedEOS(); 104 status_t postSynchronouslyAndReturnError(const sp<AMessage> &msg); 105 106 sp<ALooper> mLooper; 107 sp<ALooper> mCodecLooper; 108 sp<AHandlerReflector<MediaCodecSource> > mReflector; 109 sp<AMessage> mOutputFormat; 110 Mutexed<sp<MetaData>> mMeta; 111 sp<Puller> mPuller; 112 sp<MediaCodec> mEncoder; 113 uint32_t mFlags; 114 List<sp<AReplyToken>> mStopReplyIDQueue; 115 bool mIsVideo; 116 bool mStarted; 117 bool mStopping; 118 bool mDoMoreWorkPending; 119 bool mSetEncoderFormat; 120 int32_t mEncoderFormat; 121 int32_t mEncoderDataSpace; 122 sp<AMessage> mEncoderActivityNotify; 123 sp<IGraphicBufferProducer> mGraphicBufferProducer; 124 sp<IGraphicBufferConsumer> mGraphicBufferConsumer; 125 List<MediaBuffer *> mInputBufferQueue; 126 List<size_t> mAvailEncoderInputIndices; 127 List<int64_t> mDecodingTimeQueue; // decoding time (us) for video 128 int64_t mInputBufferTimeOffsetUs; 129 int64_t mFirstSampleSystemTimeUs; 130 bool mPausePending; 131 132 // audio drift time 133 int64_t mFirstSampleTimeUs; 134 List<int64_t> mDriftTimeQueue; 135 136 struct Output { 137 Output(); 138 List<MediaBuffer*> mBufferQueue; 139 bool mEncoderReachedEOS; 140 status_t mErrorCode; 141 Condition mCond; 142 }; 143 Mutexed<Output> mOutput; 144 145 int32_t mGeneration; 146 147 DISALLOW_EVIL_CONSTRUCTORS(MediaCodecSource); 148}; 149 150} // namespace android 151 152#endif /* MediaCodecSource_H_ */ 153