AwesomePlayer.h revision 0a5baa9b411fe086013d2a5e9126ed63fbad046c
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 "NuHTTPDataSource.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 <utils/threads.h> 28 29namespace android { 30 31struct AudioPlayer; 32struct DataSource; 33struct MediaBuffer; 34struct MediaExtractor; 35struct MediaSource; 36struct TimeSource; 37struct NuCachedSource2; 38 39struct ALooper; 40struct ARTSPController; 41 42struct AwesomeRenderer : public RefBase { 43 AwesomeRenderer() {} 44 45 virtual void render(MediaBuffer *buffer) = 0; 46 47private: 48 AwesomeRenderer(const AwesomeRenderer &); 49 AwesomeRenderer &operator=(const AwesomeRenderer &); 50}; 51 52struct AwesomePlayer { 53 AwesomePlayer(); 54 ~AwesomePlayer(); 55 56 void setListener(const wp<MediaPlayerBase> &listener); 57 58 status_t setDataSource( 59 const char *uri, 60 const KeyedVector<String8, String8> *headers = NULL); 61 62 status_t setDataSource(int fd, int64_t offset, int64_t length); 63 64 void reset(); 65 66 status_t prepare(); 67 status_t prepare_l(); 68 status_t prepareAsync(); 69 status_t prepareAsync_l(); 70 71 status_t play(); 72 status_t pause(); 73 74 bool isPlaying() const; 75 76 void setISurface(const sp<ISurface> &isurface); 77 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink); 78 status_t setLooping(bool shouldLoop); 79 80 status_t getDuration(int64_t *durationUs); 81 status_t getPosition(int64_t *positionUs); 82 83 status_t seekTo(int64_t timeUs); 84 85 status_t getVideoDimensions(int32_t *width, int32_t *height) const; 86 87 status_t suspend(); 88 status_t resume(); 89 90 // This is a mask of MediaExtractor::Flags. 91 uint32_t flags() const; 92 93private: 94 friend struct AwesomeEvent; 95 96 enum { 97 PLAYING = 1, 98 LOOPING = 2, 99 FIRST_FRAME = 4, 100 PREPARING = 8, 101 PREPARED = 16, 102 AT_EOS = 32, 103 PREPARE_CANCELLED = 64, 104 CACHE_UNDERRUN = 128, 105 }; 106 107 mutable Mutex mLock; 108 Mutex mMiscStateLock; 109 110 OMXClient mClient; 111 TimedEventQueue mQueue; 112 bool mQueueStarted; 113 wp<MediaPlayerBase> mListener; 114 115 sp<ISurface> mISurface; 116 sp<MediaPlayerBase::AudioSink> mAudioSink; 117 118 TimeSource *mTimeSource; 119 120 String8 mUri; 121 KeyedVector<String8, String8> mUriHeaders; 122 123 sp<DataSource> mFileSource; 124 125 sp<MediaSource> mVideoTrack; 126 sp<MediaSource> mVideoSource; 127 sp<AwesomeRenderer> mVideoRenderer; 128 bool mVideoRendererIsPreview; 129 130 sp<MediaSource> mAudioTrack; 131 sp<MediaSource> mAudioSource; 132 AudioPlayer *mAudioPlayer; 133 int64_t mDurationUs; 134 135 uint32_t mFlags; 136 uint32_t mExtractorFlags; 137 138 int32_t mVideoWidth, mVideoHeight; 139 int64_t mTimeSourceDeltaUs; 140 int64_t mVideoTimeUs; 141 142 bool mSeeking; 143 bool mSeekNotificationSent; 144 int64_t mSeekTimeUs; 145 146 bool mWatchForAudioSeekComplete; 147 bool mWatchForAudioEOS; 148 149 sp<TimedEventQueue::Event> mVideoEvent; 150 bool mVideoEventPending; 151 sp<TimedEventQueue::Event> mStreamDoneEvent; 152 bool mStreamDoneEventPending; 153 sp<TimedEventQueue::Event> mBufferingEvent; 154 bool mBufferingEventPending; 155 sp<TimedEventQueue::Event> mCheckAudioStatusEvent; 156 bool mAudioStatusEventPending; 157 158 sp<TimedEventQueue::Event> mAsyncPrepareEvent; 159 Condition mPreparedCondition; 160 bool mIsAsyncPrepare; 161 status_t mPrepareResult; 162 status_t mStreamDoneStatus; 163 164 void postVideoEvent_l(int64_t delayUs = -1); 165 void postBufferingEvent_l(); 166 void postStreamDoneEvent_l(status_t status); 167 void postCheckAudioStatusEvent_l(); 168 status_t play_l(); 169 170 MediaBuffer *mLastVideoBuffer; 171 MediaBuffer *mVideoBuffer; 172 173 sp<NuHTTPDataSource> mConnectingDataSource; 174 sp<NuCachedSource2> mCachedSource; 175 176 sp<ALooper> mLooper; 177 sp<ARTSPController> mRTSPController; 178 179 struct SuspensionState { 180 String8 mUri; 181 KeyedVector<String8, String8> mUriHeaders; 182 sp<DataSource> mFileSource; 183 184 uint32_t mFlags; 185 int64_t mPositionUs; 186 187 void *mLastVideoFrame; 188 size_t mLastVideoFrameSize; 189 int32_t mColorFormat; 190 int32_t mVideoWidth, mVideoHeight; 191 int32_t mDecodedWidth, mDecodedHeight; 192 193 SuspensionState() 194 : mLastVideoFrame(NULL) { 195 } 196 197 ~SuspensionState() { 198 if (mLastVideoFrame) { 199 free(mLastVideoFrame); 200 mLastVideoFrame = NULL; 201 } 202 } 203 } *mSuspensionState; 204 205 status_t setDataSource_l( 206 const char *uri, 207 const KeyedVector<String8, String8> *headers = NULL); 208 209 status_t setDataSource_l(const sp<DataSource> &dataSource); 210 status_t setDataSource_l(const sp<MediaExtractor> &extractor); 211 void reset_l(); 212 status_t seekTo_l(int64_t timeUs); 213 status_t pause_l(); 214 void initRenderer_l(); 215 void seekAudioIfNecessary_l(); 216 217 void cancelPlayerEvents(bool keepBufferingGoing = false); 218 219 void setAudioSource(sp<MediaSource> source); 220 status_t initAudioDecoder(); 221 222 void setVideoSource(sp<MediaSource> source); 223 status_t initVideoDecoder(); 224 225 void onStreamDone(); 226 227 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0); 228 229 void onVideoEvent(); 230 void onBufferingUpdate(); 231 void onCheckAudioStatus(); 232 void onPrepareAsyncEvent(); 233 void abortPrepare(status_t err); 234 235 status_t finishSetDataSource_l(); 236 237 static bool ContinuePreparation(void *cookie); 238 239 AwesomePlayer(const AwesomePlayer &); 240 AwesomePlayer &operator=(const AwesomePlayer &); 241}; 242 243} // namespace android 244 245#endif // AWESOME_PLAYER_H_ 246 247