NuPlayer.h revision 8a048338d9291b2db1b3a325fff58cb1aa69f04d
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/AudioResamplerPublic.h> 22#include <media/MediaPlayerInterface.h> 23#include <media/stagefright/foundation/AHandler.h> 24 25namespace android { 26 27struct ABuffer; 28struct AMessage; 29struct AudioPlaybackRate; 30struct AVSyncSettings; 31class IDataSource; 32class MetaData; 33struct NuPlayerDriver; 34 35struct NuPlayer : public AHandler { 36 NuPlayer(); 37 38 void setUID(uid_t uid); 39 40 void setDriver(const wp<NuPlayerDriver> &driver); 41 42 void setDataSourceAsync(const sp<IStreamSource> &source); 43 44 void setDataSourceAsync( 45 const sp<IMediaHTTPService> &httpService, 46 const char *url, 47 const KeyedVector<String8, String8> *headers); 48 49 void setDataSourceAsync(int fd, int64_t offset, int64_t length); 50 51 void setDataSourceAsync(const sp<DataSource> &source); 52 53 void prepareAsync(); 54 55 void setVideoSurfaceTextureAsync( 56 const sp<IGraphicBufferProducer> &bufferProducer); 57 58 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink); 59 status_t setPlaybackSettings(const AudioPlaybackRate &rate); 60 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */); 61 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint); 62 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */); 63 64 void start(); 65 66 void pause(); 67 68 // Will notify the driver through "notifyResetComplete" once finished. 69 void resetAsync(); 70 71 // Will notify the driver through "notifySeekComplete" once finished 72 // and needNotify is true. 73 void seekToAsync(int64_t seekTimeUs, bool needNotify = false); 74 75 status_t setVideoScalingMode(int32_t mode); 76 status_t getTrackInfo(Parcel* reply) const; 77 status_t getSelectedTrack(int32_t type, Parcel* reply) const; 78 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs); 79 status_t getCurrentPosition(int64_t *mediaUs); 80 void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped); 81 82 sp<MetaData> getFileMeta(); 83 84protected: 85 virtual ~NuPlayer(); 86 87 virtual void onMessageReceived(const sp<AMessage> &msg); 88 89public: 90 struct NuPlayerStreamListener; 91 struct Source; 92 93private: 94 struct Decoder; 95 struct DecoderBase; 96 struct DecoderPassThrough; 97 struct CCDecoder; 98 struct GenericSource; 99 struct HTTPLiveSource; 100 struct Renderer; 101 struct RTSPSource; 102 struct StreamingSource; 103 struct Action; 104 struct SeekAction; 105 struct SetSurfaceAction; 106 struct ResumeDecoderAction; 107 struct FlushDecoderAction; 108 struct PostMessageAction; 109 struct SimpleAction; 110 111 enum { 112 kWhatSetDataSource = '=DaS', 113 kWhatPrepare = 'prep', 114 kWhatSetVideoSurface = '=VSu', 115 kWhatSetAudioSink = '=AuS', 116 kWhatMoreDataQueued = 'more', 117 kWhatConfigPlayback = 'cfPB', 118 kWhatConfigSync = 'cfSy', 119 kWhatGetPlaybackSettings = 'gPbS', 120 kWhatGetSyncSettings = 'gSyS', 121 kWhatStart = 'strt', 122 kWhatScanSources = 'scan', 123 kWhatVideoNotify = 'vidN', 124 kWhatAudioNotify = 'audN', 125 kWhatClosedCaptionNotify = 'capN', 126 kWhatRendererNotify = 'renN', 127 kWhatReset = 'rset', 128 kWhatSeek = 'seek', 129 kWhatPause = 'paus', 130 kWhatResume = 'rsme', 131 kWhatPollDuration = 'polD', 132 kWhatSourceNotify = 'srcN', 133 kWhatGetTrackInfo = 'gTrI', 134 kWhatGetSelectedTrack = 'gSel', 135 kWhatSelectTrack = 'selT', 136 }; 137 138 wp<NuPlayerDriver> mDriver; 139 bool mUIDValid; 140 uid_t mUID; 141 sp<Source> mSource; 142 uint32_t mSourceFlags; 143 sp<Surface> mSurface; 144 sp<MediaPlayerBase::AudioSink> mAudioSink; 145 sp<DecoderBase> mVideoDecoder; 146 bool mOffloadAudio; 147 sp<DecoderBase> mAudioDecoder; 148 sp<CCDecoder> mCCDecoder; 149 sp<Renderer> mRenderer; 150 sp<ALooper> mRendererLooper; 151 int32_t mAudioDecoderGeneration; 152 int32_t mVideoDecoderGeneration; 153 int32_t mRendererGeneration; 154 155 List<sp<Action> > mDeferredActions; 156 157 bool mAudioEOS; 158 bool mVideoEOS; 159 160 bool mScanSourcesPending; 161 int32_t mScanSourcesGeneration; 162 163 int32_t mPollDurationGeneration; 164 int32_t mTimedTextGeneration; 165 166 enum FlushStatus { 167 NONE, 168 FLUSHING_DECODER, 169 FLUSHING_DECODER_SHUTDOWN, 170 SHUTTING_DOWN_DECODER, 171 FLUSHED, 172 SHUT_DOWN, 173 }; 174 175 enum FlushCommand { 176 FLUSH_CMD_NONE, 177 FLUSH_CMD_FLUSH, 178 FLUSH_CMD_SHUTDOWN, 179 }; 180 181 // Status of flush responses from the decoder and renderer. 182 bool mFlushComplete[2][2]; 183 184 FlushStatus mFlushingAudio; 185 FlushStatus mFlushingVideo; 186 187 // Status of flush responses from the decoder and renderer. 188 bool mResumePending; 189 190 int32_t mVideoScalingMode; 191 192 AudioPlaybackRate mPlaybackSettings; 193 AVSyncSettings mSyncSettings; 194 float mVideoFpsHint; 195 bool mStarted; 196 197 // Actual pause state, either as requested by client or due to buffering. 198 bool mPaused; 199 200 // Pause state as requested by client. Note that if mPausedByClient is 201 // true, mPaused is always true; if mPausedByClient is false, mPaused could 202 // still become true, when we pause internally due to buffering. 203 bool mPausedByClient; 204 205 // Pause state as requested by source (internally) due to buffering 206 bool mPausedForBuffering; 207 208 inline const sp<DecoderBase> &getDecoder(bool audio) { 209 return audio ? mAudioDecoder : mVideoDecoder; 210 } 211 212 inline void clearFlushComplete() { 213 mFlushComplete[0][0] = false; 214 mFlushComplete[0][1] = false; 215 mFlushComplete[1][0] = false; 216 mFlushComplete[1][1] = false; 217 } 218 219 void tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo); 220 void closeAudioSink(); 221 222 status_t instantiateDecoder(bool audio, sp<DecoderBase> *decoder); 223 224 status_t onInstantiateSecureDecoders(); 225 226 void updateVideoSize( 227 const sp<AMessage> &inputFormat, 228 const sp<AMessage> &outputFormat = NULL); 229 230 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL); 231 232 void handleFlushComplete(bool audio, bool isDecoder); 233 void finishFlushIfPossible(); 234 235 void onStart(); 236 void onResume(); 237 void onPause(); 238 239 bool audioDecoderStillNeeded(); 240 241 void flushDecoder(bool audio, bool needShutdown); 242 243 void finishResume(); 244 245 void postScanSources(); 246 247 void schedulePollDuration(); 248 void cancelPollDuration(); 249 250 void processDeferredActions(); 251 252 void performSeek(int64_t seekTimeUs, bool needNotify); 253 void performDecoderFlush(FlushCommand audio, FlushCommand video); 254 void performReset(); 255 void performScanSources(); 256 void performSetSurface(const sp<Surface> &wrapper); 257 void performResumeDecoders(bool needNotify); 258 259 void onSourceNotify(const sp<AMessage> &msg); 260 void onClosedCaptionNotify(const sp<AMessage> &msg); 261 262 void queueDecoderShutdown( 263 bool audio, bool video, const sp<AMessage> &reply); 264 265 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex); 266 void sendTimedMetaData(const sp<ABuffer> &buffer); 267 void sendTimedTextData(const sp<ABuffer> &buffer); 268 269 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const; 270 271 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer); 272}; 273 274} // namespace android 275 276#endif // NU_PLAYER_H_ 277