NuPlayer.h revision 6ffb1fd67eb8f00f130a6db914ba42a8432aec70
1/* 2 * Copyright (C) 2010 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 NU_PLAYER_H_ 18 19#define NU_PLAYER_H_ 20 21#include <media/MediaPlayerInterface.h> 22#include <media/stagefright/foundation/AHandler.h> 23#include <media/stagefright/NativeWindowWrapper.h> 24 25namespace android { 26 27struct ABuffer; 28struct AMessage; 29struct MetaData; 30struct NuPlayerDriver; 31 32struct NuPlayer : public AHandler { 33 NuPlayer(); 34 35 void setUID(uid_t uid); 36 37 void setDriver(const wp<NuPlayerDriver> &driver); 38 39 void setDataSourceAsync(const sp<IStreamSource> &source); 40 41 void setDataSourceAsync( 42 const sp<IMediaHTTPService> &httpService, 43 const char *url, 44 const KeyedVector<String8, String8> *headers); 45 46 void setDataSourceAsync(int fd, int64_t offset, int64_t length); 47 48 void prepareAsync(); 49 50 void setVideoSurfaceTextureAsync( 51 const sp<IGraphicBufferProducer> &bufferProducer); 52 53 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink); 54 void start(); 55 56 void pause(); 57 58 // Will notify the driver through "notifyResetComplete" once finished. 59 void resetAsync(); 60 61 // Will notify the driver through "notifySeekComplete" once finished 62 // and needNotify is true. 63 void seekToAsync(int64_t seekTimeUs, bool needNotify = false); 64 65 status_t setVideoScalingMode(int32_t mode); 66 status_t getTrackInfo(Parcel* reply) const; 67 status_t getSelectedTrack(int32_t type, Parcel* reply) const; 68 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs); 69 status_t getCurrentPosition(int64_t *mediaUs); 70 void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped); 71 72 sp<MetaData> getFileMeta(); 73 74 static const size_t kAggregateBufferSizeBytes; 75 76protected: 77 virtual ~NuPlayer(); 78 79 virtual void onMessageReceived(const sp<AMessage> &msg); 80 81public: 82 struct NuPlayerStreamListener; 83 struct Source; 84 85private: 86 struct Decoder; 87 struct DecoderPassThrough; 88 struct CCDecoder; 89 struct GenericSource; 90 struct HTTPLiveSource; 91 struct Renderer; 92 struct RTSPSource; 93 struct StreamingSource; 94 struct Action; 95 struct SeekAction; 96 struct SetSurfaceAction; 97 struct FlushDecoderAction; 98 struct PostMessageAction; 99 struct SimpleAction; 100 101 enum { 102 kWhatSetDataSource = '=DaS', 103 kWhatPrepare = 'prep', 104 kWhatSetVideoNativeWindow = '=NaW', 105 kWhatSetAudioSink = '=AuS', 106 kWhatMoreDataQueued = 'more', 107 kWhatStart = 'strt', 108 kWhatScanSources = 'scan', 109 kWhatVideoNotify = 'vidN', 110 kWhatAudioNotify = 'audN', 111 kWhatClosedCaptionNotify = 'capN', 112 kWhatRendererNotify = 'renN', 113 kWhatReset = 'rset', 114 kWhatSeek = 'seek', 115 kWhatPause = 'paus', 116 kWhatResume = 'rsme', 117 kWhatPollDuration = 'polD', 118 kWhatSourceNotify = 'srcN', 119 kWhatGetTrackInfo = 'gTrI', 120 kWhatGetSelectedTrack = 'gSel', 121 kWhatSelectTrack = 'selT', 122 }; 123 124 wp<NuPlayerDriver> mDriver; 125 bool mUIDValid; 126 uid_t mUID; 127 sp<Source> mSource; 128 uint32_t mSourceFlags; 129 sp<NativeWindowWrapper> mNativeWindow; 130 sp<MediaPlayerBase::AudioSink> mAudioSink; 131 sp<Decoder> mVideoDecoder; 132 bool mVideoIsAVC; 133 bool mOffloadAudio; 134 sp<Decoder> mAudioDecoder; 135 sp<CCDecoder> mCCDecoder; 136 sp<Renderer> mRenderer; 137 sp<ALooper> mRendererLooper; 138 int32_t mAudioDecoderGeneration; 139 int32_t mVideoDecoderGeneration; 140 int32_t mRendererGeneration; 141 142 List<sp<Action> > mDeferredActions; 143 144 bool mAudioEOS; 145 bool mVideoEOS; 146 147 bool mScanSourcesPending; 148 int32_t mScanSourcesGeneration; 149 150 int32_t mPollDurationGeneration; 151 int32_t mTimedTextGeneration; 152 153 enum FlushStatus { 154 NONE, 155 FLUSHING_DECODER, 156 FLUSHING_DECODER_SHUTDOWN, 157 SHUTTING_DOWN_DECODER, 158 FLUSHED, 159 SHUT_DOWN, 160 }; 161 162 enum FlushCommand { 163 FLUSH_CMD_NONE, 164 FLUSH_CMD_FLUSH, 165 FLUSH_CMD_SHUTDOWN, 166 }; 167 168 // Once the current flush is complete this indicates whether the 169 // notion of time has changed. 170 bool mTimeDiscontinuityPending; 171 172 // Status of flush responses from the decoder and renderer. 173 bool mFlushComplete[2][2]; 174 175 // Used by feedDecoderInputData to aggregate small buffers into 176 // one large buffer. 177 sp<ABuffer> mPendingAudioAccessUnit; 178 status_t mPendingAudioErr; 179 sp<ABuffer> mAggregateBuffer; 180 181 FlushStatus mFlushingAudio; 182 FlushStatus mFlushingVideo; 183 184 int64_t mNumFramesTotal, mNumFramesDropped; 185 186 int32_t mVideoScalingMode; 187 188 bool mStarted; 189 190 inline const sp<Decoder> &getDecoder(bool audio) { 191 return audio ? mAudioDecoder : mVideoDecoder; 192 } 193 194 inline void clearFlushComplete() { 195 mFlushComplete[0][0] = false; 196 mFlushComplete[0][1] = false; 197 mFlushComplete[1][0] = false; 198 mFlushComplete[1][1] = false; 199 } 200 201 void openAudioSink(const sp<AMessage> &format, bool offloadOnly); 202 void closeAudioSink(); 203 204 status_t instantiateDecoder(bool audio, sp<Decoder> *decoder); 205 206 void updateVideoSize( 207 const sp<AMessage> &inputFormat, 208 const sp<AMessage> &outputFormat = NULL); 209 210 status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg); 211 void renderBuffer(bool audio, const sp<AMessage> &msg); 212 213 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL); 214 215 void handleFlushComplete(bool audio, bool isDecoder); 216 void finishFlushIfPossible(); 217 218 void onStart(); 219 void onResume(); 220 221 bool audioDecoderStillNeeded(); 222 223 void flushDecoder( 224 bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL); 225 void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format); 226 227 void postScanSources(); 228 229 void schedulePollDuration(); 230 void cancelPollDuration(); 231 232 void processDeferredActions(); 233 234 void performSeek(int64_t seekTimeUs, bool needNotify); 235 void performDecoderFlush(FlushCommand audio, FlushCommand video); 236 void performReset(); 237 void performScanSources(); 238 void performSetSurface(const sp<NativeWindowWrapper> &wrapper); 239 240 void onSourceNotify(const sp<AMessage> &msg); 241 void onClosedCaptionNotify(const sp<AMessage> &msg); 242 243 void queueDecoderShutdown( 244 bool audio, bool video, const sp<AMessage> &reply); 245 246 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex); 247 void sendTimedTextData(const sp<ABuffer> &buffer); 248 249 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const; 250 251 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer); 252}; 253 254} // namespace android 255 256#endif // NU_PLAYER_H_ 257