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