AwesomePlayer.h revision 1b78c4b1484c7d4c12b9a87329dc8d4b6e8c0c37
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 <media/stagefright/MetaData.h> 29#include <utils/threads.h> 30#include <drm/DrmManagerClient.h> 31 32namespace android { 33 34struct AudioPlayer; 35struct ClockEstimator; 36struct DataSource; 37struct MediaBuffer; 38struct MediaExtractor; 39struct MediaSource; 40struct NuCachedSource2; 41struct IGraphicBufferProducer; 42 43class DrmManagerClinet; 44class DecryptHandle; 45 46class TimedTextDriver; 47struct WVMExtractor; 48 49struct AwesomeRenderer : public RefBase { 50 AwesomeRenderer() {} 51 52 virtual void render(MediaBuffer *buffer) = 0; 53 54private: 55 AwesomeRenderer(const AwesomeRenderer &); 56 AwesomeRenderer &operator=(const AwesomeRenderer &); 57}; 58 59struct AwesomePlayer { 60 AwesomePlayer(); 61 ~AwesomePlayer(); 62 63 void setListener(const wp<MediaPlayerBase> &listener); 64 void setUID(uid_t uid); 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 status_t setSurfaceTexture(const sp<IGraphicBufferProducer> &bufferProducer); 87 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink); 88 status_t setLooping(bool shouldLoop); 89 90 status_t getDuration(int64_t *durationUs); 91 status_t getPosition(int64_t *positionUs); 92 93 status_t setParameter(int key, const Parcel &request); 94 status_t getParameter(int key, Parcel *reply); 95 status_t invoke(const Parcel &request, 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 void postAudioTearDown(); 106 status_t dump(int fd, const Vector<String16> &args) const; 107 108private: 109 friend struct AwesomeEvent; 110 friend struct PreviewPlayer; 111 112 enum { 113 PLAYING = 0x01, 114 LOOPING = 0x02, 115 FIRST_FRAME = 0x04, 116 PREPARING = 0x08, 117 PREPARED = 0x10, 118 AT_EOS = 0x20, 119 PREPARE_CANCELLED = 0x40, 120 CACHE_UNDERRUN = 0x80, 121 AUDIO_AT_EOS = 0x0100, 122 VIDEO_AT_EOS = 0x0200, 123 AUTO_LOOPING = 0x0400, 124 125 // We are basically done preparing but are currently buffering 126 // sufficient data to begin playback and finish the preparation phase 127 // for good. 128 PREPARING_CONNECTED = 0x0800, 129 130 // We're triggering a single video event to display the first frame 131 // after the seekpoint. 132 SEEK_PREVIEW = 0x1000, 133 134 AUDIO_RUNNING = 0x2000, 135 AUDIOPLAYER_STARTED = 0x4000, 136 137 INCOGNITO = 0x8000, 138 139 TEXT_RUNNING = 0x10000, 140 TEXTPLAYER_INITIALIZED = 0x20000, 141 142 SLOW_DECODER_HACK = 0x40000, 143 }; 144 145 mutable Mutex mLock; 146 Mutex mMiscStateLock; 147 mutable Mutex mStatsLock; 148 Mutex mAudioLock; 149 150 OMXClient mClient; 151 TimedEventQueue mQueue; 152 bool mQueueStarted; 153 wp<MediaPlayerBase> mListener; 154 bool mUIDValid; 155 uid_t mUID; 156 157 sp<ANativeWindow> mNativeWindow; 158 sp<MediaPlayerBase::AudioSink> mAudioSink; 159 160 SystemTimeSource mSystemTimeSource; 161 TimeSource *mTimeSource; 162 163 String8 mUri; 164 KeyedVector<String8, String8> mUriHeaders; 165 166 sp<DataSource> mFileSource; 167 168 sp<MediaSource> mVideoTrack; 169 sp<MediaSource> mVideoSource; 170 sp<AwesomeRenderer> mVideoRenderer; 171 bool mVideoRenderingStarted; 172 bool mVideoRendererIsPreview; 173 int32_t mMediaRenderingStartGeneration; 174 int32_t mStartGeneration; 175 176 ssize_t mActiveAudioTrackIndex; 177 sp<MediaSource> mAudioTrack; 178 sp<MediaSource> mOmxSource; 179 sp<MediaSource> mAudioSource; 180 AudioPlayer *mAudioPlayer; 181 int64_t mDurationUs; 182 183 int32_t mDisplayWidth; 184 int32_t mDisplayHeight; 185 int32_t mVideoScalingMode; 186 187 uint32_t mFlags; 188 uint32_t mExtractorFlags; 189 uint32_t mSinceLastDropped; 190 191 int64_t mTimeSourceDeltaUs; 192 int64_t mVideoTimeUs; 193 194 enum SeekType { 195 NO_SEEK, 196 SEEK, 197 SEEK_VIDEO_ONLY 198 }; 199 SeekType mSeeking; 200 201 bool mSeekNotificationSent; 202 int64_t mSeekTimeUs; 203 204 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown. 205 206 bool mWatchForAudioSeekComplete; 207 bool mWatchForAudioEOS; 208 209 sp<TimedEventQueue::Event> mVideoEvent; 210 bool mVideoEventPending; 211 sp<TimedEventQueue::Event> mStreamDoneEvent; 212 bool mStreamDoneEventPending; 213 sp<TimedEventQueue::Event> mBufferingEvent; 214 bool mBufferingEventPending; 215 sp<TimedEventQueue::Event> mCheckAudioStatusEvent; 216 bool mAudioStatusEventPending; 217 sp<TimedEventQueue::Event> mVideoLagEvent; 218 bool mVideoLagEventPending; 219 sp<TimedEventQueue::Event> mAudioTearDownEvent; 220 bool mAudioTearDownEventPending; 221 sp<TimedEventQueue::Event> mAsyncPrepareEvent; 222 Condition mPreparedCondition; 223 bool mIsAsyncPrepare; 224 status_t mPrepareResult; 225 status_t mStreamDoneStatus; 226 227 void postVideoEvent_l(int64_t delayUs = -1); 228 void postBufferingEvent_l(); 229 void postStreamDoneEvent_l(status_t status); 230 void postCheckAudioStatusEvent(int64_t delayUs); 231 void postVideoLagEvent_l(); 232 void postAudioTearDownEvent(int64_t delayUs); 233 234 status_t play_l(); 235 236 MediaBuffer *mVideoBuffer; 237 238 sp<ClockEstimator> mClockEstimator; 239 sp<HTTPBase> mConnectingDataSource; 240 sp<NuCachedSource2> mCachedSource; 241 242 DrmManagerClient *mDrmManagerClient; 243 sp<DecryptHandle> mDecryptHandle; 244 245 int64_t mLastVideoTimeUs; 246 TimedTextDriver *mTextDriver; 247 248 sp<WVMExtractor> mWVMExtractor; 249 sp<MediaExtractor> mExtractor; 250 251 status_t setDataSource_l( 252 const char *uri, 253 const KeyedVector<String8, String8> *headers = NULL); 254 255 status_t setDataSource_l(const sp<DataSource> &dataSource); 256 status_t setDataSource_l(const sp<MediaExtractor> &extractor); 257 void reset_l(); 258 status_t seekTo_l(int64_t timeUs); 259 status_t pause_l(bool at_eos = false); 260 void initRenderer_l(); 261 void notifyVideoSize_l(); 262 void seekAudioIfNecessary_l(); 263 264 void cancelPlayerEvents(bool keepNotifications = false); 265 266 void setAudioSource(sp<MediaSource> source); 267 status_t initAudioDecoder(); 268 269 270 void setVideoSource(sp<MediaSource> source); 271 status_t initVideoDecoder(uint32_t flags = 0); 272 273 void addTextSource_l(size_t trackIndex, const sp<MediaSource>& source); 274 275 void onStreamDone(); 276 277 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0); 278 279 void onVideoEvent(); 280 void onBufferingUpdate(); 281 void onCheckAudioStatus(); 282 void onPrepareAsyncEvent(); 283 void abortPrepare(status_t err); 284 void finishAsyncPrepare_l(); 285 void onVideoLagUpdate(); 286 void onAudioTearDownEvent(); 287 288 void beginPrepareAsync_l(); 289 290 bool getCachedDuration_l(int64_t *durationUs, bool *eos); 291 292 status_t finishSetDataSource_l(); 293 294 static bool ContinuePreparation(void *cookie); 295 296 bool getBitrate(int64_t *bitrate); 297 298 int64_t estimateRealTimeUs(TimeSource *ts, int64_t systemTimeUs); 299 void finishSeekIfNecessary(int64_t videoTimeUs); 300 void ensureCacheIsFetching_l(); 301 302 void notifyIfMediaStarted_l(); 303 void createAudioPlayer_l(); 304 status_t startAudioPlayer_l(bool sendErrorNotification = true); 305 306 void shutdownVideoDecoder_l(); 307 status_t setNativeWindow_l(const sp<ANativeWindow> &native); 308 309 bool isStreamingHTTP() const; 310 void sendCacheStats(); 311 void checkDrmStatus(const sp<DataSource>& dataSource); 312 313 enum FlagMode { 314 SET, 315 CLEAR, 316 ASSIGN 317 }; 318 void modifyFlags(unsigned value, FlagMode mode); 319 320 struct TrackStat { 321 String8 mMIME; 322 String8 mDecoderName; 323 }; 324 325 // protected by mStatsLock 326 struct Stats { 327 int mFd; 328 String8 mURI; 329 int64_t mBitrate; 330 331 // FIXME: 332 // These two indices are just 0 or 1 for now 333 // They are not representing the actual track 334 // indices in the stream. 335 ssize_t mAudioTrackIndex; 336 ssize_t mVideoTrackIndex; 337 338 int64_t mNumVideoFramesDecoded; 339 int64_t mNumVideoFramesDropped; 340 int32_t mVideoWidth; 341 int32_t mVideoHeight; 342 uint32_t mFlags; 343 Vector<TrackStat> mTracks; 344 } mStats; 345 346 bool mOffloadAudio; 347 bool mAudioTearDown; 348 bool mAudioTearDownWasPlaying; 349 int64_t mAudioTearDownPosition; 350 351 status_t setVideoScalingMode(int32_t mode); 352 status_t setVideoScalingMode_l(int32_t mode); 353 status_t getTrackInfo(Parcel* reply) const; 354 355 status_t selectAudioTrack_l(const sp<MediaSource>& source, size_t trackIndex); 356 357 // when select is true, the given track is selected. 358 // otherwise, the given track is unselected. 359 status_t selectTrack(size_t trackIndex, bool select); 360 361 size_t countTracks() const; 362 363 AwesomePlayer(const AwesomePlayer &); 364 AwesomePlayer &operator=(const AwesomePlayer &); 365}; 366 367} // namespace android 368 369#endif // AWESOME_PLAYER_H_ 370