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