James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 PREVIEW_PLAYER_BASE_H_ |
| 18 | |
| 19 | #define PREVIEW_PLAYER_BASE_H_ |
| 20 | |
| 21 | #include "HTTPBase.h" |
| 22 | #include "TimedEventQueue.h" |
| 23 | |
| 24 | #include <media/MediaPlayerInterface.h> |
| 25 | #include <media/stagefright/DataSource.h> |
Chih-Chung Chang | 7efb8ef | 2011-07-22 09:01:36 +0800 | [diff] [blame] | 26 | #include <media/stagefright/MediaSource.h> |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 27 | #include <media/stagefright/OMXClient.h> |
| 28 | #include <media/stagefright/TimeSource.h> |
| 29 | #include <utils/threads.h> |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | struct AudioPlayerBase; |
| 34 | struct DataSource; |
| 35 | struct MediaBuffer; |
| 36 | struct MediaExtractor; |
| 37 | struct MediaSource; |
| 38 | struct NuCachedSource2; |
| 39 | struct ISurfaceTexture; |
| 40 | |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 41 | struct AwesomeRenderer : public RefBase { |
| 42 | AwesomeRenderer() {} |
| 43 | |
| 44 | virtual void render(MediaBuffer *buffer) = 0; |
| 45 | |
| 46 | private: |
| 47 | AwesomeRenderer(const AwesomeRenderer &); |
| 48 | AwesomeRenderer &operator=(const AwesomeRenderer &); |
| 49 | }; |
| 50 | |
| 51 | struct PreviewPlayerBase { |
| 52 | PreviewPlayerBase(); |
| 53 | ~PreviewPlayerBase(); |
| 54 | |
| 55 | void setListener(const wp<MediaPlayerBase> &listener); |
| 56 | |
| 57 | status_t setDataSource( |
| 58 | const char *uri, |
| 59 | const KeyedVector<String8, String8> *headers = NULL); |
| 60 | |
| 61 | status_t setDataSource(int fd, int64_t offset, int64_t length); |
| 62 | |
| 63 | status_t setDataSource(const sp<IStreamSource> &source); |
| 64 | |
| 65 | void reset(); |
| 66 | |
| 67 | status_t prepare(); |
| 68 | status_t prepare_l(); |
| 69 | status_t prepareAsync(); |
| 70 | status_t prepareAsync_l(); |
| 71 | |
| 72 | status_t play(); |
| 73 | status_t pause(); |
| 74 | |
| 75 | bool isPlaying() const; |
| 76 | |
| 77 | void setSurface(const sp<Surface> &surface); |
| 78 | void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture); |
| 79 | void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink); |
| 80 | status_t setLooping(bool shouldLoop); |
| 81 | |
| 82 | status_t getDuration(int64_t *durationUs); |
| 83 | status_t getPosition(int64_t *positionUs); |
| 84 | |
| 85 | status_t setParameter(int key, const Parcel &request); |
| 86 | status_t getParameter(int key, Parcel *reply); |
| 87 | |
| 88 | status_t seekTo(int64_t timeUs); |
| 89 | |
| 90 | // This is a mask of MediaExtractor::Flags. |
| 91 | uint32_t flags() const; |
| 92 | |
| 93 | void postAudioEOS(int64_t delayUs = 0ll); |
| 94 | void postAudioSeekComplete(); |
| 95 | |
| 96 | private: |
| 97 | friend struct AwesomeEvent; |
| 98 | friend struct PreviewPlayer; |
| 99 | |
| 100 | enum { |
| 101 | PLAYING = 1, |
| 102 | LOOPING = 2, |
| 103 | FIRST_FRAME = 4, |
| 104 | PREPARING = 8, |
| 105 | PREPARED = 16, |
| 106 | AT_EOS = 32, |
| 107 | PREPARE_CANCELLED = 64, |
| 108 | CACHE_UNDERRUN = 128, |
| 109 | AUDIO_AT_EOS = 256, |
| 110 | VIDEO_AT_EOS = 512, |
| 111 | AUTO_LOOPING = 1024, |
| 112 | |
| 113 | // We are basically done preparing but are currently buffering |
| 114 | // sufficient data to begin playback and finish the preparation phase |
| 115 | // for good. |
| 116 | PREPARING_CONNECTED = 2048, |
| 117 | |
| 118 | // We're triggering a single video event to display the first frame |
| 119 | // after the seekpoint. |
| 120 | SEEK_PREVIEW = 4096, |
| 121 | |
| 122 | AUDIO_RUNNING = 8192, |
| 123 | AUDIOPLAYER_STARTED = 16384, |
| 124 | |
| 125 | INCOGNITO = 32768, |
| 126 | }; |
| 127 | |
| 128 | mutable Mutex mLock; |
| 129 | Mutex mMiscStateLock; |
| 130 | |
| 131 | OMXClient mClient; |
| 132 | TimedEventQueue mQueue; |
| 133 | bool mQueueStarted; |
| 134 | wp<MediaPlayerBase> mListener; |
| 135 | |
| 136 | sp<Surface> mSurface; |
| 137 | sp<ANativeWindow> mNativeWindow; |
| 138 | sp<MediaPlayerBase::AudioSink> mAudioSink; |
| 139 | |
| 140 | SystemTimeSource mSystemTimeSource; |
| 141 | TimeSource *mTimeSource; |
| 142 | |
| 143 | String8 mUri; |
| 144 | KeyedVector<String8, String8> mUriHeaders; |
| 145 | |
| 146 | sp<DataSource> mFileSource; |
| 147 | |
| 148 | sp<MediaSource> mVideoTrack; |
| 149 | sp<MediaSource> mVideoSource; |
| 150 | sp<AwesomeRenderer> mVideoRenderer; |
| 151 | bool mVideoRendererIsPreview; |
| 152 | |
| 153 | sp<MediaSource> mAudioTrack; |
| 154 | sp<MediaSource> mAudioSource; |
| 155 | AudioPlayerBase *mAudioPlayer; |
| 156 | int64_t mDurationUs; |
| 157 | |
| 158 | int32_t mDisplayWidth; |
| 159 | int32_t mDisplayHeight; |
| 160 | |
| 161 | uint32_t mFlags; |
| 162 | uint32_t mExtractorFlags; |
| 163 | |
| 164 | int64_t mTimeSourceDeltaUs; |
| 165 | int64_t mVideoTimeUs; |
| 166 | |
| 167 | enum SeekType { |
| 168 | NO_SEEK, |
| 169 | SEEK, |
| 170 | SEEK_VIDEO_ONLY |
| 171 | }; |
| 172 | SeekType mSeeking; |
| 173 | |
| 174 | bool mSeekNotificationSent; |
| 175 | int64_t mSeekTimeUs; |
| 176 | |
| 177 | int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown. |
| 178 | |
| 179 | bool mWatchForAudioSeekComplete; |
| 180 | bool mWatchForAudioEOS; |
| 181 | |
| 182 | sp<TimedEventQueue::Event> mVideoEvent; |
| 183 | bool mVideoEventPending; |
| 184 | sp<TimedEventQueue::Event> mStreamDoneEvent; |
| 185 | bool mStreamDoneEventPending; |
| 186 | sp<TimedEventQueue::Event> mBufferingEvent; |
| 187 | bool mBufferingEventPending; |
| 188 | sp<TimedEventQueue::Event> mCheckAudioStatusEvent; |
| 189 | bool mAudioStatusEventPending; |
| 190 | sp<TimedEventQueue::Event> mVideoLagEvent; |
| 191 | bool mVideoLagEventPending; |
| 192 | |
| 193 | sp<TimedEventQueue::Event> mAsyncPrepareEvent; |
| 194 | Condition mPreparedCondition; |
| 195 | bool mIsAsyncPrepare; |
| 196 | status_t mPrepareResult; |
| 197 | status_t mStreamDoneStatus; |
| 198 | |
| 199 | void postVideoEvent_l(int64_t delayUs = -1); |
| 200 | void postBufferingEvent_l(); |
| 201 | void postStreamDoneEvent_l(status_t status); |
| 202 | void postCheckAudioStatusEvent_l(int64_t delayUs); |
| 203 | void postVideoLagEvent_l(); |
| 204 | status_t play_l(); |
| 205 | |
| 206 | MediaBuffer *mVideoBuffer; |
| 207 | |
| 208 | sp<HTTPBase> mConnectingDataSource; |
| 209 | sp<NuCachedSource2> mCachedSource; |
| 210 | |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 211 | int64_t mLastVideoTimeUs; |
| 212 | |
Chih-Chung Chang | 7efb8ef | 2011-07-22 09:01:36 +0800 | [diff] [blame] | 213 | ARect mCropRect; |
| 214 | int32_t mGivenWidth, mGivenHeight; |
Chih-Chung Chang | 7efb8ef | 2011-07-22 09:01:36 +0800 | [diff] [blame] | 215 | |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 216 | status_t setDataSource_l( |
| 217 | const char *uri, |
| 218 | const KeyedVector<String8, String8> *headers = NULL); |
| 219 | |
| 220 | status_t setDataSource_l(const sp<DataSource> &dataSource); |
| 221 | status_t setDataSource_l(const sp<MediaExtractor> &extractor); |
| 222 | void reset_l(); |
| 223 | status_t seekTo_l(int64_t timeUs); |
| 224 | status_t pause_l(bool at_eos = false); |
| 225 | void initRenderer_l(); |
| 226 | void notifyVideoSize_l(); |
| 227 | void seekAudioIfNecessary_l(); |
| 228 | |
| 229 | void cancelPlayerEvents(bool keepBufferingGoing = false); |
| 230 | |
| 231 | void setAudioSource(sp<MediaSource> source); |
| 232 | status_t initAudioDecoder(); |
| 233 | |
| 234 | void setVideoSource(sp<MediaSource> source); |
| 235 | status_t initVideoDecoder(uint32_t flags = 0); |
| 236 | |
| 237 | void onStreamDone(); |
| 238 | |
| 239 | void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0); |
| 240 | |
| 241 | void onVideoEvent(); |
| 242 | void onBufferingUpdate(); |
| 243 | void onCheckAudioStatus(); |
| 244 | void onPrepareAsyncEvent(); |
| 245 | void abortPrepare(status_t err); |
| 246 | void finishAsyncPrepare_l(); |
| 247 | void onVideoLagUpdate(); |
| 248 | |
| 249 | bool getCachedDuration_l(int64_t *durationUs, bool *eos); |
| 250 | |
| 251 | status_t finishSetDataSource_l(); |
| 252 | |
| 253 | static bool ContinuePreparation(void *cookie); |
| 254 | |
James Dong | c9dedc4 | 2011-05-01 12:36:22 -0700 | [diff] [blame] | 255 | bool getBitrate(int64_t *bitrate); |
| 256 | |
| 257 | void finishSeekIfNecessary(int64_t videoTimeUs); |
| 258 | void ensureCacheIsFetching_l(); |
| 259 | |
| 260 | status_t startAudioPlayer_l(); |
| 261 | |
| 262 | void shutdownVideoDecoder_l(); |
| 263 | void setNativeWindow_l(const sp<ANativeWindow> &native); |
| 264 | |
| 265 | PreviewPlayerBase(const PreviewPlayerBase &); |
| 266 | PreviewPlayerBase &operator=(const PreviewPlayerBase &); |
| 267 | }; |
| 268 | |
| 269 | } // namespace android |
| 270 | |
| 271 | #endif // PREVIEW_PLAYER_BASE_H_ |
| 272 | |