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