AwesomePlayer.h revision a0b1d4b161599c2bb2a47119e50c51e75bbe980e
1/* 2 * Copyright (C) 2009 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 AWESOME_PLAYER_H_ 18 19#define AWESOME_PLAYER_H_ 20 21#include "HTTPBase.h" 22#include "TimedEventQueue.h" 23 24#include <media/MediaPlayerInterface.h> 25#include <media/stagefright/DataSource.h> 26#include <media/stagefright/OMXClient.h> 27#include <media/stagefright/TimeSource.h> 28#include <utils/threads.h> 29#include <drm/DrmManagerClient.h> 30 31namespace android { 32 33struct AudioPlayer; 34struct DataSource; 35struct MediaBuffer; 36struct MediaExtractor; 37struct MediaSource; 38struct NuCachedSource2; 39struct ISurfaceTexture; 40 41struct ALooper; 42struct ARTSPController; 43 44class DrmManagerClinet; 45class DecryptHandle; 46 47class TimedTextPlayer; 48struct WVMExtractor; 49 50struct AwesomeRenderer : public RefBase { 51 AwesomeRenderer() {} 52 53 virtual void render(MediaBuffer *buffer) = 0; 54 55private: 56 AwesomeRenderer(const AwesomeRenderer &); 57 AwesomeRenderer &operator=(const AwesomeRenderer &); 58}; 59 60struct AwesomePlayer { 61 AwesomePlayer(); 62 ~AwesomePlayer(); 63 64 void setListener(const wp<MediaPlayerBase> &listener); 65 66 status_t setDataSource( 67 const char *uri, 68 const KeyedVector<String8, String8> *headers = NULL); 69 70 status_t setDataSource(int fd, int64_t offset, int64_t length); 71 72 status_t setDataSource(const sp<IStreamSource> &source); 73 74 void reset(); 75 76 status_t prepare(); 77 status_t prepare_l(); 78 status_t prepareAsync(); 79 status_t prepareAsync_l(); 80 81 status_t play(); 82 status_t pause(); 83 84 bool isPlaying() const; 85 86 void setSurface(const sp<Surface> &surface); 87 void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture); 88 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink); 89 status_t setLooping(bool shouldLoop); 90 91 status_t getDuration(int64_t *durationUs); 92 status_t getPosition(int64_t *positionUs); 93 94 status_t setParameter(int key, const Parcel &request); 95 status_t getParameter(int key, Parcel *reply); 96 status_t setCacheStatCollectFreq(const Parcel &request); 97 98 status_t seekTo(int64_t timeUs); 99 100 // This is a mask of MediaExtractor::Flags. 101 uint32_t flags() const; 102 103 void postAudioEOS(int64_t delayUs = 0ll); 104 void postAudioSeekComplete(); 105 106 status_t setTimedTextTrackIndex(int32_t index); 107 108 status_t dump(int fd, const Vector<String16> &args) const; 109 110private: 111 friend struct AwesomeEvent; 112 friend struct PreviewPlayer; 113 114 enum { 115 PLAYING = 0x01, 116 LOOPING = 0x02, 117 FIRST_FRAME = 0x04, 118 PREPARING = 0x08, 119 PREPARED = 0x10, 120 AT_EOS = 0x20, 121 PREPARE_CANCELLED = 0x40, 122 CACHE_UNDERRUN = 0x80, 123 AUDIO_AT_EOS = 0x0100, 124 VIDEO_AT_EOS = 0x0200, 125 AUTO_LOOPING = 0x0400, 126 127 // We are basically done preparing but are currently buffering 128 // sufficient data to begin playback and finish the preparation phase 129 // for good. 130 PREPARING_CONNECTED = 0x0800, 131 132 // We're triggering a single video event to display the first frame 133 // after the seekpoint. 134 SEEK_PREVIEW = 0x1000, 135 136 AUDIO_RUNNING = 0x2000, 137 AUDIOPLAYER_STARTED = 0x4000, 138 139 INCOGNITO = 0x8000, 140 141 TEXT_RUNNING = 0x10000, 142 TEXTPLAYER_STARTED = 0x20000, 143 }; 144 145 mutable Mutex mLock; 146 Mutex mMiscStateLock; 147 mutable Mutex mStatsLock; 148 149 OMXClient mClient; 150 TimedEventQueue mQueue; 151 bool mQueueStarted; 152 wp<MediaPlayerBase> mListener; 153 154 sp<Surface> mSurface; 155 sp<ANativeWindow> mNativeWindow; 156 sp<MediaPlayerBase::AudioSink> mAudioSink; 157 158 SystemTimeSource mSystemTimeSource; 159 TimeSource *mTimeSource; 160 161 String8 mUri; 162 KeyedVector<String8, String8> mUriHeaders; 163 164 sp<DataSource> mFileSource; 165 166 sp<MediaSource> mVideoTrack; 167 sp<MediaSource> mVideoSource; 168 sp<AwesomeRenderer> mVideoRenderer; 169 bool mVideoRendererIsPreview; 170 171 sp<MediaSource> mAudioTrack; 172 sp<MediaSource> mAudioSource; 173 AudioPlayer *mAudioPlayer; 174 int64_t mDurationUs; 175 176 int32_t mDisplayWidth; 177 int32_t mDisplayHeight; 178 179 uint32_t mFlags; 180 uint32_t mExtractorFlags; 181 182 int64_t mTimeSourceDeltaUs; 183 int64_t mVideoTimeUs; 184 185 enum SeekType { 186 NO_SEEK, 187 SEEK, 188 SEEK_VIDEO_ONLY 189 }; 190 SeekType mSeeking; 191 192 bool mSeekNotificationSent; 193 int64_t mSeekTimeUs; 194 195 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown. 196 197 bool mWatchForAudioSeekComplete; 198 bool mWatchForAudioEOS; 199 200 sp<TimedEventQueue::Event> mVideoEvent; 201 bool mVideoEventPending; 202 sp<TimedEventQueue::Event> mStreamDoneEvent; 203 bool mStreamDoneEventPending; 204 sp<TimedEventQueue::Event> mBufferingEvent; 205 bool mBufferingEventPending; 206 sp<TimedEventQueue::Event> mCheckAudioStatusEvent; 207 bool mAudioStatusEventPending; 208 sp<TimedEventQueue::Event> mVideoLagEvent; 209 bool mVideoLagEventPending; 210 211 sp<TimedEventQueue::Event> mAsyncPrepareEvent; 212 Condition mPreparedCondition; 213 bool mIsAsyncPrepare; 214 status_t mPrepareResult; 215 status_t mStreamDoneStatus; 216 217 void postVideoEvent_l(int64_t delayUs = -1); 218 void postBufferingEvent_l(); 219 void postStreamDoneEvent_l(status_t status); 220 void postCheckAudioStatusEvent_l(int64_t delayUs); 221 void postVideoLagEvent_l(); 222 status_t play_l(); 223 224 MediaBuffer *mVideoBuffer; 225 226 sp<HTTPBase> mConnectingDataSource; 227 sp<NuCachedSource2> mCachedSource; 228 229 sp<ALooper> mLooper; 230 sp<ARTSPController> mRTSPController; 231 sp<ARTSPController> mConnectingRTSPController; 232 233 DrmManagerClient *mDrmManagerClient; 234 sp<DecryptHandle> mDecryptHandle; 235 236 int64_t mLastVideoTimeUs; 237 TimedTextPlayer *mTextPlayer; 238 mutable Mutex mTimedTextLock; 239 240 sp<WVMExtractor> mWVMExtractor; 241 242 status_t setDataSource_l( 243 const char *uri, 244 const KeyedVector<String8, String8> *headers = NULL); 245 246 status_t setDataSource_l(const sp<DataSource> &dataSource); 247 status_t setDataSource_l(const sp<MediaExtractor> &extractor); 248 void reset_l(); 249 status_t seekTo_l(int64_t timeUs); 250 status_t pause_l(bool at_eos = false); 251 void initRenderer_l(); 252 void notifyVideoSize_l(); 253 void seekAudioIfNecessary_l(); 254 255 void cancelPlayerEvents(bool keepBufferingGoing = false); 256 257 void setAudioSource(sp<MediaSource> source); 258 status_t initAudioDecoder(); 259 260 void setVideoSource(sp<MediaSource> source); 261 status_t initVideoDecoder(uint32_t flags = 0); 262 263 void addTextSource(sp<MediaSource> source); 264 265 void onStreamDone(); 266 267 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0); 268 269 void onVideoEvent(); 270 void onBufferingUpdate(); 271 void onCheckAudioStatus(); 272 void onPrepareAsyncEvent(); 273 void abortPrepare(status_t err); 274 void finishAsyncPrepare_l(); 275 void onVideoLagUpdate(); 276 277 bool getCachedDuration_l(int64_t *durationUs, bool *eos); 278 279 status_t finishSetDataSource_l(); 280 281 static bool ContinuePreparation(void *cookie); 282 283 static void OnRTSPSeekDoneWrapper(void *cookie); 284 void onRTSPSeekDone(); 285 286 bool getBitrate(int64_t *bitrate); 287 288 void finishSeekIfNecessary(int64_t videoTimeUs); 289 void ensureCacheIsFetching_l(); 290 291 status_t startAudioPlayer_l(); 292 void postAudioSeekComplete_l(); 293 294 void shutdownVideoDecoder_l(); 295 void setNativeWindow_l(const sp<ANativeWindow> &native); 296 297 bool isStreamingHTTP() const; 298 void sendCacheStats(); 299 300 enum FlagMode { 301 SET, 302 CLEAR, 303 ASSIGN 304 }; 305 void modifyFlags(unsigned value, FlagMode mode); 306 307 struct TrackStat { 308 String8 mMIME; 309 String8 mDecoderName; 310 }; 311 312 // protected by mStatsLock 313 struct Stats { 314 int mFd; 315 String8 mURI; 316 int64_t mBitrate; 317 ssize_t mAudioTrackIndex; 318 ssize_t mVideoTrackIndex; 319 int64_t mNumVideoFramesDecoded; 320 int64_t mNumVideoFramesDropped; 321 int32_t mVideoWidth; 322 int32_t mVideoHeight; 323 uint32_t mFlags; 324 Vector<TrackStat> mTracks; 325 } mStats; 326 327 AwesomePlayer(const AwesomePlayer &); 328 AwesomePlayer &operator=(const AwesomePlayer &); 329}; 330 331} // namespace android 332 333#endif // AWESOME_PLAYER_H_ 334 335