NuPlayer.h revision 9421174a2f002fef31b330fb04e00105a905dca4
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); 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 ShutdownDecoderAction; 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 // Once the current flush is complete this indicates whether the 163 // notion of time has changed. 164 bool mTimeDiscontinuityPending; 165 166 // Status of flush responses from the decoder and renderer. 167 bool mFlushComplete[2][2]; 168 169 // Used by feedDecoderInputData to aggregate small buffers into 170 // one large buffer. 171 sp<ABuffer> mPendingAudioAccessUnit; 172 status_t mPendingAudioErr; 173 sp<ABuffer> mAggregateBuffer; 174 175 FlushStatus mFlushingAudio; 176 FlushStatus mFlushingVideo; 177 178 int64_t mSkipRenderingAudioUntilMediaTimeUs; 179 int64_t mSkipRenderingVideoUntilMediaTimeUs; 180 181 int64_t mNumFramesTotal, mNumFramesDropped; 182 183 int32_t mVideoScalingMode; 184 185 bool mStarted; 186 187 inline const sp<Decoder> &getDecoder(bool audio) { 188 return audio ? mAudioDecoder : mVideoDecoder; 189 } 190 191 inline void clearFlushComplete() { 192 mFlushComplete[0][0] = false; 193 mFlushComplete[0][1] = false; 194 mFlushComplete[1][0] = false; 195 mFlushComplete[1][1] = false; 196 } 197 198 void openAudioSink(const sp<AMessage> &format, bool offloadOnly); 199 void closeAudioSink(); 200 201 status_t instantiateDecoder(bool audio, sp<Decoder> *decoder); 202 203 void updateVideoSize( 204 const sp<AMessage> &inputFormat, 205 const sp<AMessage> &outputFormat = NULL); 206 207 status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg); 208 void renderBuffer(bool audio, const sp<AMessage> &msg); 209 210 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL); 211 212 void handleFlushComplete(bool audio, bool isDecoder); 213 void finishFlushIfPossible(); 214 215 void onStart(); 216 void onResume(); 217 218 bool audioDecoderStillNeeded(); 219 220 void flushDecoder( 221 bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL); 222 void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format); 223 224 void postScanSources(); 225 226 void schedulePollDuration(); 227 void cancelPollDuration(); 228 229 void processDeferredActions(); 230 231 void performSeek(int64_t seekTimeUs, bool needNotify); 232 void performDecoderFlush(); 233 void performDecoderShutdown(bool audio, bool video); 234 void performReset(); 235 void performScanSources(); 236 void performSetSurface(const sp<NativeWindowWrapper> &wrapper); 237 238 void onSourceNotify(const sp<AMessage> &msg); 239 void onClosedCaptionNotify(const sp<AMessage> &msg); 240 241 void queueDecoderShutdown( 242 bool audio, bool video, const sp<AMessage> &reply); 243 244 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex); 245 void sendTimedTextData(const sp<ABuffer> &buffer); 246 247 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const; 248 249 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer); 250}; 251 252} // namespace android 253 254#endif // NU_PLAYER_H_ 255