Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "AwesomePlayer" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | 4ab5a6f | 2010-02-11 11:00:26 -0800 | [diff] [blame] | 21 | #include <dlfcn.h> |
| 22 | |
Andreas Huber | 7a747b8 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 23 | #include "include/ARTSPController.h" |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 24 | #include "include/AwesomePlayer.h" |
Andreas Huber | 1314e73 | 2009-12-14 14:18:22 -0800 | [diff] [blame] | 25 | #include "include/SoftwareRenderer.h" |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 26 | #include "include/NuCachedSource2.h" |
| 27 | #include "include/ThrottledSource.h" |
Andreas Huber | 54d0972 | 2010-10-12 11:34:37 -0700 | [diff] [blame] | 28 | #include "include/MPEG2TSExtractor.h" |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 29 | |
Andreas Huber | a67d538 | 2009-12-10 15:32:12 -0800 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 31 | #include <media/stagefright/foundation/hexdump.h> |
| 32 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 33 | #include <media/stagefright/AudioPlayer.h> |
| 34 | #include <media/stagefright/DataSource.h> |
| 35 | #include <media/stagefright/FileSource.h> |
| 36 | #include <media/stagefright/MediaBuffer.h> |
Andreas Huber | c79827a | 2010-01-05 10:54:55 -0800 | [diff] [blame] | 37 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 38 | #include <media/stagefright/MediaExtractor.h> |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 39 | #include <media/stagefright/MediaSource.h> |
| 40 | #include <media/stagefright/MetaData.h> |
| 41 | #include <media/stagefright/OMXCodec.h> |
Andreas Huber | c79827a | 2010-01-05 10:54:55 -0800 | [diff] [blame] | 42 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 43 | #include <surfaceflinger/Surface.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 44 | |
Andreas Huber | 7a747b8 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 45 | #include <media/stagefright/foundation/ALooper.h> |
Andreas Huber | ae9d507 | 2010-12-06 10:36:06 -0800 | [diff] [blame] | 46 | #include <media/stagefright/foundation/AMessage.h> |
Andreas Huber | 202348e | 2010-06-07 14:35:29 -0700 | [diff] [blame] | 47 | |
Andreas Huber | 6a1f5f9 | 2010-11-15 09:03:03 -0800 | [diff] [blame] | 48 | #define USE_SURFACE_ALLOC 1 |
pmehendale | 28f939d | 2011-01-06 14:43:49 -0800 | [diff] [blame] | 49 | #define FRAME_DROP_FREQ 7 |
Andreas Huber | 6a1f5f9 | 2010-11-15 09:03:03 -0800 | [diff] [blame] | 50 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 51 | namespace android { |
| 52 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 53 | static int64_t kLowWaterMarkUs = 2000000ll; // 2secs |
| 54 | static int64_t kHighWaterMarkUs = 10000000ll; // 10secs |
Andreas Huber | b2934b1 | 2011-02-08 10:18:41 -0800 | [diff] [blame] | 55 | static int64_t kHighWaterMarkRTSPUs = 4000000ll; // 4secs |
Andreas Huber | 9634665 | 2011-01-19 15:07:19 -0800 | [diff] [blame] | 56 | static const size_t kLowWaterMarkBytes = 40000; |
| 57 | static const size_t kHighWaterMarkBytes = 200000; |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 58 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 59 | struct AwesomeEvent : public TimedEventQueue::Event { |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 60 | AwesomeEvent( |
| 61 | AwesomePlayer *player, |
| 62 | void (AwesomePlayer::*method)()) |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 63 | : mPlayer(player), |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 64 | mMethod(method) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | protected: |
| 68 | virtual ~AwesomeEvent() {} |
| 69 | |
| 70 | virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) { |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 71 | (mPlayer->*mMethod)(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | private: |
| 75 | AwesomePlayer *mPlayer; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 76 | void (AwesomePlayer::*mMethod)(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 77 | |
| 78 | AwesomeEvent(const AwesomeEvent &); |
| 79 | AwesomeEvent &operator=(const AwesomeEvent &); |
| 80 | }; |
| 81 | |
Andreas Huber | 1314e73 | 2009-12-14 14:18:22 -0800 | [diff] [blame] | 82 | struct AwesomeLocalRenderer : public AwesomeRenderer { |
| 83 | AwesomeLocalRenderer( |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 84 | const sp<Surface> &surface, const sp<MetaData> &meta) |
| 85 | : mTarget(new SoftwareRenderer(surface, meta)) { |
Andreas Huber | 1314e73 | 2009-12-14 14:18:22 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | virtual void render(MediaBuffer *buffer) { |
Andreas Huber | 7b73cfc | 2010-02-12 14:40:08 -0800 | [diff] [blame] | 89 | render((const uint8_t *)buffer->data() + buffer->range_offset(), |
| 90 | buffer->range_length()); |
| 91 | } |
| 92 | |
| 93 | void render(const void *data, size_t size) { |
| 94 | mTarget->render(data, size, NULL); |
Andreas Huber | 1314e73 | 2009-12-14 14:18:22 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | protected: |
| 98 | virtual ~AwesomeLocalRenderer() { |
| 99 | delete mTarget; |
| 100 | mTarget = NULL; |
| 101 | } |
| 102 | |
| 103 | private: |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 104 | SoftwareRenderer *mTarget; |
Andreas Huber | 4ab5a6f | 2010-02-11 11:00:26 -0800 | [diff] [blame] | 105 | |
Andreas Huber | 1314e73 | 2009-12-14 14:18:22 -0800 | [diff] [blame] | 106 | AwesomeLocalRenderer(const AwesomeLocalRenderer &); |
| 107 | AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);; |
| 108 | }; |
| 109 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 110 | struct AwesomeNativeWindowRenderer : public AwesomeRenderer { |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 111 | AwesomeNativeWindowRenderer( |
| 112 | const sp<ANativeWindow> &nativeWindow, |
| 113 | int32_t rotationDegrees) |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 114 | : mNativeWindow(nativeWindow) { |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 115 | applyRotation(rotationDegrees); |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | virtual void render(MediaBuffer *buffer) { |
| 119 | status_t err = mNativeWindow->queueBuffer( |
| 120 | mNativeWindow.get(), buffer->graphicBuffer().get()); |
| 121 | if (err != 0) { |
| 122 | LOGE("queueBuffer failed with error %s (%d)", strerror(-err), |
| 123 | -err); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | sp<MetaData> metaData = buffer->meta_data(); |
| 128 | metaData->setInt32(kKeyRendered, 1); |
| 129 | } |
| 130 | |
| 131 | protected: |
| 132 | virtual ~AwesomeNativeWindowRenderer() {} |
| 133 | |
| 134 | private: |
| 135 | sp<ANativeWindow> mNativeWindow; |
| 136 | |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 137 | void applyRotation(int32_t rotationDegrees) { |
| 138 | uint32_t transform; |
| 139 | switch (rotationDegrees) { |
| 140 | case 0: transform = 0; break; |
| 141 | case 90: transform = HAL_TRANSFORM_ROT_90; break; |
| 142 | case 180: transform = HAL_TRANSFORM_ROT_180; break; |
| 143 | case 270: transform = HAL_TRANSFORM_ROT_270; break; |
| 144 | default: transform = 0; break; |
| 145 | } |
| 146 | |
| 147 | if (transform) { |
| 148 | CHECK_EQ(0, native_window_set_buffers_transform( |
| 149 | mNativeWindow.get(), transform)); |
| 150 | } |
| 151 | } |
| 152 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 153 | AwesomeNativeWindowRenderer(const AwesomeNativeWindowRenderer &); |
| 154 | AwesomeNativeWindowRenderer &operator=( |
| 155 | const AwesomeNativeWindowRenderer &); |
| 156 | }; |
| 157 | |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 158 | //////////////////////////////////////////////////////////////////////////////// |
| 159 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 160 | AwesomePlayer::AwesomePlayer() |
Andreas Huber | 406a18b | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 161 | : mQueueStarted(false), |
| 162 | mTimeSource(NULL), |
Andreas Huber | 7b73cfc | 2010-02-12 14:40:08 -0800 | [diff] [blame] | 163 | mVideoRendererIsPreview(false), |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 164 | mAudioPlayer(NULL), |
James Dong | 08adfd2 | 2011-01-16 11:30:13 -0800 | [diff] [blame] | 165 | mDisplayWidth(0), |
| 166 | mDisplayHeight(0), |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 167 | mFlags(0), |
Andreas Huber | 62f7ffe | 2010-05-06 10:18:05 -0700 | [diff] [blame] | 168 | mExtractorFlags(0), |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 169 | mVideoBuffer(NULL), |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 170 | mDecryptHandle(NULL) { |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 171 | CHECK_EQ(mClient.connect(), (status_t)OK); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 172 | |
| 173 | DataSource::RegisterDefaultSniffers(); |
| 174 | |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 175 | mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 176 | mVideoEventPending = false; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 177 | mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 178 | mStreamDoneEventPending = false; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 179 | mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate); |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 180 | mBufferingEventPending = false; |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 181 | mVideoLagEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoLagUpdate); |
| 182 | mVideoEventPending = false; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 183 | |
| 184 | mCheckAudioStatusEvent = new AwesomeEvent( |
| 185 | this, &AwesomePlayer::onCheckAudioStatus); |
| 186 | |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 187 | mAudioStatusEventPending = false; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 188 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 189 | reset(); |
| 190 | } |
| 191 | |
| 192 | AwesomePlayer::~AwesomePlayer() { |
Andreas Huber | 406a18b | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 193 | if (mQueueStarted) { |
| 194 | mQueue.stop(); |
| 195 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 196 | |
| 197 | reset(); |
| 198 | |
| 199 | mClient.disconnect(); |
| 200 | } |
| 201 | |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 202 | void AwesomePlayer::cancelPlayerEvents(bool keepBufferingGoing) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 203 | mQueue.cancelEvent(mVideoEvent->eventID()); |
| 204 | mVideoEventPending = false; |
| 205 | mQueue.cancelEvent(mStreamDoneEvent->eventID()); |
| 206 | mStreamDoneEventPending = false; |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 207 | mQueue.cancelEvent(mCheckAudioStatusEvent->eventID()); |
| 208 | mAudioStatusEventPending = false; |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 209 | mQueue.cancelEvent(mVideoLagEvent->eventID()); |
| 210 | mVideoLagEventPending = false; |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 211 | |
| 212 | if (!keepBufferingGoing) { |
| 213 | mQueue.cancelEvent(mBufferingEvent->eventID()); |
| 214 | mBufferingEventPending = false; |
| 215 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Andreas Huber | a3f4384 | 2010-01-21 10:28:45 -0800 | [diff] [blame] | 218 | void AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 219 | Mutex::Autolock autoLock(mLock); |
| 220 | mListener = listener; |
| 221 | } |
| 222 | |
Andreas Huber | 433c9ac | 2010-01-27 16:49:05 -0800 | [diff] [blame] | 223 | status_t AwesomePlayer::setDataSource( |
| 224 | const char *uri, const KeyedVector<String8, String8> *headers) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 225 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 226 | return setDataSource_l(uri, headers); |
| 227 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 228 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 229 | status_t AwesomePlayer::setDataSource_l( |
| 230 | const char *uri, const KeyedVector<String8, String8> *headers) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 231 | reset_l(); |
| 232 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 233 | mUri = uri; |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 234 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 235 | if (headers) { |
| 236 | mUriHeaders = *headers; |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 239 | // The actual work will be done during preparation in the call to |
| 240 | // ::finishSetDataSource_l to avoid blocking the calling thread in |
| 241 | // setDataSource for any significant time. |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 242 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 243 | return OK; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | status_t AwesomePlayer::setDataSource( |
| 247 | int fd, int64_t offset, int64_t length) { |
| 248 | Mutex::Autolock autoLock(mLock); |
| 249 | |
| 250 | reset_l(); |
| 251 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 252 | sp<DataSource> dataSource = new FileSource(fd, offset, length); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 253 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 254 | status_t err = dataSource->initCheck(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 255 | |
| 256 | if (err != OK) { |
| 257 | return err; |
| 258 | } |
| 259 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 260 | mFileSource = dataSource; |
| 261 | |
| 262 | return setDataSource_l(dataSource); |
| 263 | } |
| 264 | |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 265 | status_t AwesomePlayer::setDataSource(const sp<IStreamSource> &source) { |
Andreas Huber | d936726 | 2010-12-16 10:16:36 -0800 | [diff] [blame] | 266 | return INVALID_OPERATION; |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 269 | status_t AwesomePlayer::setDataSource_l( |
| 270 | const sp<DataSource> &dataSource) { |
| 271 | sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 272 | |
| 273 | if (extractor == NULL) { |
| 274 | return UNKNOWN_ERROR; |
| 275 | } |
| 276 | |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 277 | dataSource->getDrmInfo(&mDecryptHandle, &mDrmManagerClient); |
Gloria Wang | adc4d9c | 2011-02-08 13:24:08 -0800 | [diff] [blame] | 278 | if (mDecryptHandle != NULL) { |
| 279 | CHECK(mDrmManagerClient); |
| 280 | if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) { |
| 281 | notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_NO_LICENSE); |
| 282 | } |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 285 | return setDataSource_l(extractor); |
| 286 | } |
| 287 | |
| 288 | status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) { |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 289 | // Attempt to approximate overall stream bitrate by summing all |
| 290 | // tracks' individual bitrates, if not all of them advertise bitrate, |
| 291 | // we have to fail. |
| 292 | |
| 293 | int64_t totalBitRate = 0; |
| 294 | |
| 295 | for (size_t i = 0; i < extractor->countTracks(); ++i) { |
| 296 | sp<MetaData> meta = extractor->getTrackMetaData(i); |
| 297 | |
| 298 | int32_t bitrate; |
| 299 | if (!meta->findInt32(kKeyBitRate, &bitrate)) { |
| 300 | totalBitRate = -1; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | totalBitRate += bitrate; |
| 305 | } |
| 306 | |
| 307 | mBitrate = totalBitRate; |
| 308 | |
| 309 | LOGV("mBitrate = %lld bits/sec", mBitrate); |
| 310 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 311 | bool haveAudio = false; |
| 312 | bool haveVideo = false; |
| 313 | for (size_t i = 0; i < extractor->countTracks(); ++i) { |
| 314 | sp<MetaData> meta = extractor->getTrackMetaData(i); |
| 315 | |
| 316 | const char *mime; |
| 317 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 318 | |
| 319 | if (!haveVideo && !strncasecmp(mime, "video/", 6)) { |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 320 | setVideoSource(extractor->getTrack(i)); |
| 321 | haveVideo = true; |
James Dong | 08adfd2 | 2011-01-16 11:30:13 -0800 | [diff] [blame] | 322 | |
| 323 | // Set the presentation/display size |
| 324 | int32_t displayWidth, displayHeight; |
| 325 | bool success = meta->findInt32(kKeyDisplayWidth, &displayWidth); |
| 326 | if (success) { |
| 327 | success = meta->findInt32(kKeyDisplayHeight, &displayHeight); |
| 328 | } |
| 329 | if (success) { |
| 330 | mDisplayWidth = displayWidth; |
| 331 | mDisplayHeight = displayHeight; |
| 332 | } |
| 333 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 334 | } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) { |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 335 | setAudioSource(extractor->getTrack(i)); |
| 336 | haveAudio = true; |
Andreas Huber | 9fee0b2 | 2010-09-03 14:09:21 -0700 | [diff] [blame] | 337 | |
Andreas Huber | 1913c1a | 2010-10-04 11:09:31 -0700 | [diff] [blame] | 338 | if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) { |
| 339 | // Only do this for vorbis audio, none of the other audio |
| 340 | // formats even support this ringtone specific hack and |
| 341 | // retrieving the metadata on some extractors may turn out |
| 342 | // to be very expensive. |
| 343 | sp<MetaData> fileMeta = extractor->getMetaData(); |
| 344 | int32_t loop; |
| 345 | if (fileMeta != NULL |
| 346 | && fileMeta->findInt32(kKeyAutoLoop, &loop) && loop != 0) { |
| 347 | mFlags |= AUTO_LOOPING; |
| 348 | } |
Andreas Huber | 9fee0b2 | 2010-09-03 14:09:21 -0700 | [diff] [blame] | 349 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (haveAudio && haveVideo) { |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
Andreas Huber | 62f7ffe | 2010-05-06 10:18:05 -0700 | [diff] [blame] | 357 | if (!haveAudio && !haveVideo) { |
| 358 | return UNKNOWN_ERROR; |
| 359 | } |
| 360 | |
| 361 | mExtractorFlags = extractor->flags(); |
| 362 | |
| 363 | return OK; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void AwesomePlayer::reset() { |
| 367 | Mutex::Autolock autoLock(mLock); |
| 368 | reset_l(); |
| 369 | } |
| 370 | |
| 371 | void AwesomePlayer::reset_l() { |
James Dong | 08adfd2 | 2011-01-16 11:30:13 -0800 | [diff] [blame] | 372 | mDisplayWidth = 0; |
| 373 | mDisplayHeight = 0; |
Andreas Huber | 90c6565 | 2011-01-04 10:19:13 -0800 | [diff] [blame] | 374 | |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 375 | if (mDecryptHandle != NULL) { |
| 376 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 377 | Playback::STOP, 0); |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 378 | mDecryptHandle = NULL; |
| 379 | mDrmManagerClient = NULL; |
| 380 | } |
| 381 | |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 382 | if (mFlags & PREPARING) { |
| 383 | mFlags |= PREPARE_CANCELLED; |
| 384 | if (mConnectingDataSource != NULL) { |
| 385 | LOGI("interrupting the connection process"); |
| 386 | mConnectingDataSource->disconnect(); |
Andreas Huber | cb21879 | 2011-02-14 14:14:23 -0800 | [diff] [blame] | 387 | } else if (mConnectingRTSPController != NULL) { |
| 388 | LOGI("interrupting the connection process"); |
| 389 | mConnectingRTSPController->disconnect(); |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 390 | } |
Andreas Huber | eaf2c5a | 2010-10-19 12:18:51 -0700 | [diff] [blame] | 391 | |
| 392 | if (mFlags & PREPARING_CONNECTED) { |
| 393 | // We are basically done preparing, we're just buffering |
| 394 | // enough data to start playback, we can safely interrupt that. |
| 395 | finishAsyncPrepare_l(); |
| 396 | } |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 399 | while (mFlags & PREPARING) { |
| 400 | mPreparedCondition.wait(mLock); |
| 401 | } |
| 402 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 403 | cancelPlayerEvents(); |
| 404 | |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 405 | mCachedSource.clear(); |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 406 | mAudioTrack.clear(); |
| 407 | mVideoTrack.clear(); |
| 408 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 409 | // Shutdown audio first, so that the respone to the reset request |
| 410 | // appears to happen instantaneously as far as the user is concerned |
| 411 | // If we did this later, audio would continue playing while we |
| 412 | // shutdown the video-related resources and the player appear to |
| 413 | // not be as responsive to a reset request. |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 414 | if (mAudioPlayer == NULL && mAudioSource != NULL) { |
| 415 | // If we had an audio player, it would have effectively |
| 416 | // taken possession of the audio source and stopped it when |
| 417 | // _it_ is stopped. Otherwise this is still our responsibility. |
| 418 | mAudioSource->stop(); |
| 419 | } |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 420 | mAudioSource.clear(); |
| 421 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 422 | mTimeSource = NULL; |
| 423 | |
| 424 | delete mAudioPlayer; |
| 425 | mAudioPlayer = NULL; |
| 426 | |
Andreas Huber | 3522b5a5 | 2010-01-22 14:36:53 -0800 | [diff] [blame] | 427 | mVideoRenderer.clear(); |
| 428 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 429 | if (mVideoBuffer) { |
| 430 | mVideoBuffer->release(); |
| 431 | mVideoBuffer = NULL; |
| 432 | } |
| 433 | |
Andreas Huber | e0dd7d3 | 2010-08-24 14:33:58 -0700 | [diff] [blame] | 434 | if (mRTSPController != NULL) { |
| 435 | mRTSPController->disconnect(); |
| 436 | mRTSPController.clear(); |
| 437 | } |
| 438 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 439 | if (mVideoSource != NULL) { |
| 440 | mVideoSource->stop(); |
Andreas Huber | 98b48de | 2010-01-29 10:10:22 -0800 | [diff] [blame] | 441 | |
| 442 | // The following hack is necessary to ensure that the OMX |
| 443 | // component is completely released by the time we may try |
| 444 | // to instantiate it again. |
| 445 | wp<MediaSource> tmp = mVideoSource; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 446 | mVideoSource.clear(); |
Andreas Huber | 98b48de | 2010-01-29 10:10:22 -0800 | [diff] [blame] | 447 | while (tmp.promote() != NULL) { |
| 448 | usleep(1000); |
| 449 | } |
| 450 | IPCThreadState::self()->flushCommands(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 453 | mDurationUs = -1; |
| 454 | mFlags = 0; |
Andreas Huber | 62f7ffe | 2010-05-06 10:18:05 -0700 | [diff] [blame] | 455 | mExtractorFlags = 0; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 456 | mTimeSourceDeltaUs = 0; |
| 457 | mVideoTimeUs = 0; |
| 458 | |
| 459 | mSeeking = false; |
Andreas Huber | 8e2b941 | 2010-03-31 09:40:15 -0700 | [diff] [blame] | 460 | mSeekNotificationSent = false; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 461 | mSeekTimeUs = 0; |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 462 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 463 | mUri.setTo(""); |
| 464 | mUriHeaders.clear(); |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 465 | |
| 466 | mFileSource.clear(); |
| 467 | |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 468 | mBitrate = -1; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 471 | void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) { |
Andreas Huber | a3f4384 | 2010-01-21 10:28:45 -0800 | [diff] [blame] | 472 | if (mListener != NULL) { |
| 473 | sp<MediaPlayerBase> listener = mListener.promote(); |
| 474 | |
| 475 | if (listener != NULL) { |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 476 | listener->sendEvent(msg, ext1, ext2); |
Andreas Huber | a3f4384 | 2010-01-21 10:28:45 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 481 | bool AwesomePlayer::getBitrate(int64_t *bitrate) { |
James Dong | b1262a8 | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 482 | off64_t size; |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 483 | if (mDurationUs >= 0 && mCachedSource != NULL |
| 484 | && mCachedSource->getSize(&size) == OK) { |
| 485 | *bitrate = size * 8000000ll / mDurationUs; // in bits/sec |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | if (mBitrate >= 0) { |
| 490 | *bitrate = mBitrate; |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | *bitrate = 0; |
| 495 | |
| 496 | return false; |
| 497 | } |
| 498 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 499 | // Returns true iff cached duration is available/applicable. |
| 500 | bool AwesomePlayer::getCachedDuration_l(int64_t *durationUs, bool *eos) { |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 501 | int64_t bitrate; |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 502 | |
| 503 | if (mRTSPController != NULL) { |
| 504 | *durationUs = mRTSPController->getQueueDurationUs(eos); |
| 505 | return true; |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 506 | } else if (mCachedSource != NULL && getBitrate(&bitrate)) { |
Bryan Mawhinney | e26275b | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 507 | status_t finalStatus; |
| 508 | size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 509 | *durationUs = cachedDataRemaining * 8000000ll / bitrate; |
Bryan Mawhinney | e26275b | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 510 | *eos = (finalStatus != OK); |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 511 | return true; |
| 512 | } |
| 513 | |
| 514 | return false; |
| 515 | } |
| 516 | |
Andreas Huber | 10b920c | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 517 | void AwesomePlayer::ensureCacheIsFetching_l() { |
| 518 | if (mCachedSource != NULL) { |
| 519 | mCachedSource->resumeFetchingIfNecessary(); |
| 520 | } |
| 521 | } |
| 522 | |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 523 | void AwesomePlayer::onVideoLagUpdate() { |
| 524 | Mutex::Autolock autoLock(mLock); |
| 525 | if (!mVideoLagEventPending) { |
| 526 | return; |
| 527 | } |
| 528 | mVideoLagEventPending = false; |
| 529 | |
| 530 | int64_t audioTimeUs = mAudioPlayer->getMediaTimeUs(); |
| 531 | int64_t videoLateByUs = audioTimeUs - mVideoTimeUs; |
| 532 | |
| 533 | if (videoLateByUs > 300000ll) { |
| 534 | LOGV("video late by %lld ms.", videoLateByUs / 1000ll); |
| 535 | |
| 536 | notifyListener_l( |
| 537 | MEDIA_INFO, |
| 538 | MEDIA_INFO_VIDEO_TRACK_LAGGING, |
| 539 | videoLateByUs / 1000ll); |
| 540 | } |
| 541 | |
| 542 | postVideoLagEvent_l(); |
| 543 | } |
| 544 | |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 545 | void AwesomePlayer::onBufferingUpdate() { |
| 546 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | c0178f1 | 2010-02-17 15:58:57 -0800 | [diff] [blame] | 547 | if (!mBufferingEventPending) { |
| 548 | return; |
| 549 | } |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 550 | mBufferingEventPending = false; |
| 551 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 552 | if (mCachedSource != NULL) { |
Bryan Mawhinney | e26275b | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 553 | status_t finalStatus; |
| 554 | size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); |
| 555 | bool eos = (finalStatus != OK); |
Andreas Huber | 4d8f66b | 2010-09-01 15:05:28 -0700 | [diff] [blame] | 556 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 557 | if (eos) { |
Bryan Mawhinney | e26275b | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 558 | if (finalStatus == ERROR_END_OF_STREAM) { |
| 559 | notifyListener_l(MEDIA_BUFFERING_UPDATE, 100); |
| 560 | } |
Andreas Huber | 05f6787 | 2010-10-04 11:36:39 -0700 | [diff] [blame] | 561 | if (mFlags & PREPARING) { |
| 562 | LOGV("cache has reached EOS, prepare is done."); |
| 563 | finishAsyncPrepare_l(); |
| 564 | } |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 565 | } else { |
Andreas Huber | 4deb3eb | 2010-10-12 16:55:11 -0700 | [diff] [blame] | 566 | int64_t bitrate; |
| 567 | if (getBitrate(&bitrate)) { |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 568 | size_t cachedSize = mCachedSource->cachedSize(); |
| 569 | int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate; |
| 570 | |
| 571 | int percentage = 100.0 * (double)cachedDurationUs / mDurationUs; |
| 572 | if (percentage > 100) { |
| 573 | percentage = 100; |
| 574 | } |
| 575 | |
| 576 | notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage); |
| 577 | } else { |
| 578 | // We don't know the bitrate of the stream, use absolute size |
| 579 | // limits to maintain the cache. |
| 580 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 581 | if ((mFlags & PLAYING) && !eos |
| 582 | && (cachedDataRemaining < kLowWaterMarkBytes)) { |
| 583 | LOGI("cache is running low (< %d) , pausing.", |
| 584 | kLowWaterMarkBytes); |
| 585 | mFlags |= CACHE_UNDERRUN; |
| 586 | pause_l(); |
Andreas Huber | 10b920c | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 587 | ensureCacheIsFetching_l(); |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 588 | notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START); |
| 589 | } else if (eos || cachedDataRemaining > kHighWaterMarkBytes) { |
| 590 | if (mFlags & CACHE_UNDERRUN) { |
| 591 | LOGI("cache has filled up (> %d), resuming.", |
| 592 | kHighWaterMarkBytes); |
| 593 | mFlags &= ~CACHE_UNDERRUN; |
| 594 | play_l(); |
| 595 | notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END); |
| 596 | } else if (mFlags & PREPARING) { |
| 597 | LOGV("cache has filled up (> %d), prepare is done", |
| 598 | kHighWaterMarkBytes); |
| 599 | finishAsyncPrepare_l(); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | int64_t cachedDurationUs; |
| 607 | bool eos; |
| 608 | if (getCachedDuration_l(&cachedDurationUs, &eos)) { |
Andreas Huber | 10b920c | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 609 | LOGV("cachedDurationUs = %.2f secs, eos=%d", |
| 610 | cachedDurationUs / 1E6, eos); |
| 611 | |
Andreas Huber | b2934b1 | 2011-02-08 10:18:41 -0800 | [diff] [blame] | 612 | int64_t highWaterMarkUs = |
| 613 | (mRTSPController != NULL) ? kHighWaterMarkRTSPUs : kHighWaterMarkUs; |
| 614 | |
Andreas Huber | 4d8f66b | 2010-09-01 15:05:28 -0700 | [diff] [blame] | 615 | if ((mFlags & PLAYING) && !eos |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 616 | && (cachedDurationUs < kLowWaterMarkUs)) { |
| 617 | LOGI("cache is running low (%.2f secs) , pausing.", |
| 618 | cachedDurationUs / 1E6); |
Andreas Huber | 4d8f66b | 2010-09-01 15:05:28 -0700 | [diff] [blame] | 619 | mFlags |= CACHE_UNDERRUN; |
| 620 | pause_l(); |
Andreas Huber | 10b920c | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 621 | ensureCacheIsFetching_l(); |
Andreas Huber | 4d8f66b | 2010-09-01 15:05:28 -0700 | [diff] [blame] | 622 | notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START); |
Andreas Huber | b2934b1 | 2011-02-08 10:18:41 -0800 | [diff] [blame] | 623 | } else if (eos || cachedDurationUs > highWaterMarkUs) { |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 624 | if (mFlags & CACHE_UNDERRUN) { |
| 625 | LOGI("cache has filled up (%.2f secs), resuming.", |
| 626 | cachedDurationUs / 1E6); |
| 627 | mFlags &= ~CACHE_UNDERRUN; |
| 628 | play_l(); |
| 629 | notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END); |
| 630 | } else if (mFlags & PREPARING) { |
| 631 | LOGV("cache has filled up (%.2f secs), prepare is done", |
| 632 | cachedDurationUs / 1E6); |
| 633 | finishAsyncPrepare_l(); |
Andreas Huber | c23296e | 2010-08-25 12:31:48 -0700 | [diff] [blame] | 634 | } |
Andreas Huber | c23296e | 2010-08-25 12:31:48 -0700 | [diff] [blame] | 635 | } |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 636 | } |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 637 | |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 638 | postBufferingEvent_l(); |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 641 | void AwesomePlayer::onStreamDone() { |
| 642 | // Posted whenever any stream finishes playing. |
| 643 | |
| 644 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | c0178f1 | 2010-02-17 15:58:57 -0800 | [diff] [blame] | 645 | if (!mStreamDoneEventPending) { |
| 646 | return; |
| 647 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 648 | mStreamDoneEventPending = false; |
| 649 | |
Andreas Huber | d6c421f | 2011-02-16 09:05:38 -0800 | [diff] [blame] | 650 | if (mStreamDoneStatus != ERROR_END_OF_STREAM) { |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 651 | LOGV("MEDIA_ERROR %d", mStreamDoneStatus); |
| 652 | |
| 653 | notifyListener_l( |
| 654 | MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus); |
| 655 | |
Andreas Huber | c743f45 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 656 | pause_l(true /* at eos */); |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 657 | |
| 658 | mFlags |= AT_EOS; |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | const bool allDone = |
| 663 | (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS)) |
| 664 | && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS)); |
| 665 | |
| 666 | if (!allDone) { |
| 667 | return; |
| 668 | } |
| 669 | |
Andreas Huber | 9fee0b2 | 2010-09-03 14:09:21 -0700 | [diff] [blame] | 670 | if (mFlags & (LOOPING | AUTO_LOOPING)) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 671 | seekTo_l(0); |
| 672 | |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 673 | if (mVideoSource != NULL) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 674 | postVideoEvent_l(); |
| 675 | } |
| 676 | } else { |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 677 | LOGV("MEDIA_PLAYBACK_COMPLETE"); |
| 678 | notifyListener_l(MEDIA_PLAYBACK_COMPLETE); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 679 | |
Andreas Huber | c743f45 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 680 | pause_l(true /* at eos */); |
Andreas Huber | 406a18b | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 681 | |
| 682 | mFlags |= AT_EOS; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | status_t AwesomePlayer::play() { |
| 687 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 688 | |
| 689 | mFlags &= ~CACHE_UNDERRUN; |
| 690 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 691 | return play_l(); |
| 692 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 693 | |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 694 | status_t AwesomePlayer::play_l() { |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 695 | mFlags &= ~SEEK_PREVIEW; |
| 696 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 697 | if (mFlags & PLAYING) { |
| 698 | return OK; |
| 699 | } |
| 700 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 701 | if (!(mFlags & PREPARED)) { |
| 702 | status_t err = prepare_l(); |
| 703 | |
| 704 | if (err != OK) { |
| 705 | return err; |
| 706 | } |
| 707 | } |
| 708 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 709 | mFlags |= PLAYING; |
| 710 | mFlags |= FIRST_FRAME; |
| 711 | |
Andreas Huber | c1d5c92 | 2009-12-10 15:49:04 -0800 | [diff] [blame] | 712 | bool deferredAudioSeek = false; |
| 713 | |
Gloria Wang | 95dbffb | 2010-11-04 17:38:39 -0700 | [diff] [blame] | 714 | if (mDecryptHandle != NULL) { |
| 715 | int64_t position; |
| 716 | getPosition(&position); |
| 717 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 718 | Playback::START, position / 1000); |
| 719 | } |
| 720 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 721 | if (mAudioSource != NULL) { |
| 722 | if (mAudioPlayer == NULL) { |
| 723 | if (mAudioSink != NULL) { |
Andreas Huber | 2b359ed | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 724 | mAudioPlayer = new AudioPlayer(mAudioSink, this); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 725 | mAudioPlayer->setSource(mAudioSource); |
Andreas Huber | dc9927d | 2010-03-08 15:46:13 -0800 | [diff] [blame] | 726 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 727 | mTimeSource = mAudioPlayer; |
| 728 | |
Andreas Huber | c1d5c92 | 2009-12-10 15:49:04 -0800 | [diff] [blame] | 729 | deferredAudioSeek = true; |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 730 | |
| 731 | mWatchForAudioSeekComplete = false; |
| 732 | mWatchForAudioEOS = true; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 733 | } |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | CHECK(!(mFlags & AUDIO_RUNNING)); |
| 737 | |
| 738 | if (mVideoSource == NULL) { |
| 739 | status_t err = startAudioPlayer_l(); |
| 740 | |
| 741 | if (err != OK) { |
| 742 | delete mAudioPlayer; |
| 743 | mAudioPlayer = NULL; |
| 744 | |
| 745 | mFlags &= ~(PLAYING | FIRST_FRAME); |
| 746 | |
| 747 | if (mDecryptHandle != NULL) { |
| 748 | mDrmManagerClient->setPlaybackStatus( |
| 749 | mDecryptHandle, Playback::STOP, 0); |
| 750 | } |
| 751 | |
| 752 | return err; |
| 753 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | if (mTimeSource == NULL && mAudioPlayer == NULL) { |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 758 | mTimeSource = &mSystemTimeSource; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | if (mVideoSource != NULL) { |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 762 | // Kick off video playback |
| 763 | postVideoEvent_l(); |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 764 | |
| 765 | if (mAudioSource != NULL && mVideoSource != NULL) { |
| 766 | postVideoLagEvent_l(); |
| 767 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Andreas Huber | c1d5c92 | 2009-12-10 15:49:04 -0800 | [diff] [blame] | 770 | if (deferredAudioSeek) { |
| 771 | // If there was a seek request while we were paused |
| 772 | // and we're just starting up again, honor the request now. |
| 773 | seekAudioIfNecessary_l(); |
| 774 | } |
| 775 | |
Andreas Huber | 406a18b | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 776 | if (mFlags & AT_EOS) { |
| 777 | // Legacy behaviour, if a stream finishes playing and then |
| 778 | // is started again, we play from the start... |
| 779 | seekTo_l(0); |
| 780 | } |
| 781 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 782 | return OK; |
| 783 | } |
| 784 | |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 785 | status_t AwesomePlayer::startAudioPlayer_l() { |
| 786 | CHECK(!(mFlags & AUDIO_RUNNING)); |
| 787 | |
| 788 | if (mAudioSource == NULL || mAudioPlayer == NULL) { |
| 789 | return OK; |
| 790 | } |
| 791 | |
| 792 | if (!(mFlags & AUDIOPLAYER_STARTED)) { |
| 793 | mFlags |= AUDIOPLAYER_STARTED; |
| 794 | |
| 795 | // We've already started the MediaSource in order to enable |
| 796 | // the prefetcher to read its data. |
| 797 | status_t err = mAudioPlayer->start( |
| 798 | true /* sourceAlreadyStarted */); |
| 799 | |
| 800 | if (err != OK) { |
| 801 | notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); |
| 802 | return err; |
| 803 | } |
| 804 | } else { |
| 805 | mAudioPlayer->resume(); |
| 806 | } |
| 807 | |
| 808 | mFlags |= AUDIO_RUNNING; |
| 809 | |
| 810 | mWatchForAudioEOS = true; |
| 811 | |
| 812 | return OK; |
| 813 | } |
| 814 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 815 | void AwesomePlayer::notifyVideoSize_l() { |
| 816 | sp<MetaData> meta = mVideoSource->getFormat(); |
| 817 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 818 | int32_t cropLeft, cropTop, cropRight, cropBottom; |
| 819 | if (!meta->findRect( |
| 820 | kKeyCropRect, &cropLeft, &cropTop, &cropRight, &cropBottom)) { |
| 821 | int32_t width, height; |
| 822 | CHECK(meta->findInt32(kKeyWidth, &width)); |
| 823 | CHECK(meta->findInt32(kKeyHeight, &height)); |
| 824 | |
| 825 | cropLeft = cropTop = 0; |
| 826 | cropRight = width - 1; |
| 827 | cropBottom = height - 1; |
| 828 | |
| 829 | LOGV("got dimensions only %d x %d", width, height); |
| 830 | } else { |
| 831 | LOGV("got crop rect %d, %d, %d, %d", |
| 832 | cropLeft, cropTop, cropRight, cropBottom); |
| 833 | } |
| 834 | |
| 835 | int32_t usableWidth = cropRight - cropLeft + 1; |
| 836 | int32_t usableHeight = cropBottom - cropTop + 1; |
James Dong | 08adfd2 | 2011-01-16 11:30:13 -0800 | [diff] [blame] | 837 | if (mDisplayWidth != 0) { |
| 838 | usableWidth = mDisplayWidth; |
| 839 | } |
| 840 | if (mDisplayHeight != 0) { |
| 841 | usableHeight = mDisplayHeight; |
| 842 | } |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 843 | |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 844 | int32_t rotationDegrees; |
| 845 | if (!mVideoTrack->getFormat()->findInt32( |
| 846 | kKeyRotation, &rotationDegrees)) { |
| 847 | rotationDegrees = 0; |
| 848 | } |
| 849 | |
| 850 | if (rotationDegrees == 90 || rotationDegrees == 270) { |
| 851 | notifyListener_l( |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 852 | MEDIA_SET_VIDEO_SIZE, usableHeight, usableWidth); |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 853 | } else { |
| 854 | notifyListener_l( |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 855 | MEDIA_SET_VIDEO_SIZE, usableWidth, usableHeight); |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 856 | } |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 859 | void AwesomePlayer::initRenderer_l() { |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 860 | if (mSurface == NULL) { |
| 861 | return; |
| 862 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 863 | |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 864 | sp<MetaData> meta = mVideoSource->getFormat(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 865 | |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 866 | int32_t format; |
| 867 | const char *component; |
| 868 | int32_t decodedWidth, decodedHeight; |
| 869 | CHECK(meta->findInt32(kKeyColorFormat, &format)); |
| 870 | CHECK(meta->findCString(kKeyDecoderComponent, &component)); |
| 871 | CHECK(meta->findInt32(kKeyWidth, &decodedWidth)); |
| 872 | CHECK(meta->findInt32(kKeyHeight, &decodedHeight)); |
Andreas Huber | a67d538 | 2009-12-10 15:32:12 -0800 | [diff] [blame] | 873 | |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 874 | int32_t rotationDegrees; |
| 875 | if (!mVideoTrack->getFormat()->findInt32( |
| 876 | kKeyRotation, &rotationDegrees)) { |
| 877 | rotationDegrees = 0; |
| 878 | } |
| 879 | |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 880 | mVideoRenderer.clear(); |
Andreas Huber | a67d538 | 2009-12-10 15:32:12 -0800 | [diff] [blame] | 881 | |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 882 | // Must ensure that mVideoRenderer's destructor is actually executed |
| 883 | // before creating a new one. |
| 884 | IPCThreadState::self()->flushCommands(); |
| 885 | |
| 886 | if (USE_SURFACE_ALLOC && strncmp(component, "OMX.", 4) == 0) { |
| 887 | // Hardware decoders avoid the CPU color conversion by decoding |
| 888 | // directly to ANativeBuffers, so we must use a renderer that |
| 889 | // just pushes those buffers to the ANativeWindow. |
Andreas Huber | 940c866 | 2010-11-16 15:26:30 -0800 | [diff] [blame] | 890 | mVideoRenderer = |
| 891 | new AwesomeNativeWindowRenderer(mSurface, rotationDegrees); |
Andreas Huber | c23dabf | 2010-11-16 13:05:53 -0800 | [diff] [blame] | 892 | } else { |
| 893 | // Other decoders are instantiated locally and as a consequence |
| 894 | // allocate their buffers in local address space. This renderer |
| 895 | // then performs a color conversion and copy to get the data |
| 896 | // into the ANativeBuffer. |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 897 | mVideoRenderer = new AwesomeLocalRenderer(mSurface, meta); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| 901 | status_t AwesomePlayer::pause() { |
| 902 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 903 | |
| 904 | mFlags &= ~CACHE_UNDERRUN; |
| 905 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 906 | return pause_l(); |
| 907 | } |
| 908 | |
Andreas Huber | c743f45 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 909 | status_t AwesomePlayer::pause_l(bool at_eos) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 910 | if (!(mFlags & PLAYING)) { |
| 911 | return OK; |
| 912 | } |
| 913 | |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 914 | cancelPlayerEvents(true /* keepBufferingGoing */); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 915 | |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 916 | if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { |
Andreas Huber | c743f45 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 917 | if (at_eos) { |
| 918 | // If we played the audio stream to completion we |
| 919 | // want to make sure that all samples remaining in the audio |
| 920 | // track's queue are played out. |
| 921 | mAudioPlayer->pause(true /* playPendingSamples */); |
| 922 | } else { |
| 923 | mAudioPlayer->pause(); |
| 924 | } |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 925 | |
| 926 | mFlags &= ~AUDIO_RUNNING; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | mFlags &= ~PLAYING; |
| 930 | |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 931 | if (mDecryptHandle != NULL) { |
| 932 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 933 | Playback::PAUSE, 0); |
| 934 | } |
| 935 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 936 | return OK; |
| 937 | } |
| 938 | |
| 939 | bool AwesomePlayer::isPlaying() const { |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 940 | return (mFlags & PLAYING) || (mFlags & CACHE_UNDERRUN); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 943 | void AwesomePlayer::setSurface(const sp<Surface> &surface) { |
| 944 | Mutex::Autolock autoLock(mLock); |
| 945 | |
| 946 | mSurface = surface; |
| 947 | } |
| 948 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 949 | void AwesomePlayer::setAudioSink( |
| 950 | const sp<MediaPlayerBase::AudioSink> &audioSink) { |
| 951 | Mutex::Autolock autoLock(mLock); |
| 952 | |
| 953 | mAudioSink = audioSink; |
| 954 | } |
| 955 | |
| 956 | status_t AwesomePlayer::setLooping(bool shouldLoop) { |
| 957 | Mutex::Autolock autoLock(mLock); |
| 958 | |
| 959 | mFlags = mFlags & ~LOOPING; |
| 960 | |
| 961 | if (shouldLoop) { |
| 962 | mFlags |= LOOPING; |
| 963 | } |
| 964 | |
| 965 | return OK; |
| 966 | } |
| 967 | |
| 968 | status_t AwesomePlayer::getDuration(int64_t *durationUs) { |
Andreas Huber | 252573c | 2010-03-26 10:17:17 -0700 | [diff] [blame] | 969 | Mutex::Autolock autoLock(mMiscStateLock); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 970 | |
| 971 | if (mDurationUs < 0) { |
| 972 | return UNKNOWN_ERROR; |
| 973 | } |
| 974 | |
| 975 | *durationUs = mDurationUs; |
| 976 | |
| 977 | return OK; |
| 978 | } |
| 979 | |
| 980 | status_t AwesomePlayer::getPosition(int64_t *positionUs) { |
Andreas Huber | eeb97d9 | 2010-08-27 13:29:08 -0700 | [diff] [blame] | 981 | if (mRTSPController != NULL) { |
| 982 | *positionUs = mRTSPController->getNormalPlayTimeUs(); |
| 983 | } |
| 984 | else if (mSeeking) { |
Andreas Huber | ddb709c | 2010-04-07 10:24:35 -0700 | [diff] [blame] | 985 | *positionUs = mSeekTimeUs; |
| 986 | } else if (mVideoSource != NULL) { |
Andreas Huber | 252573c | 2010-03-26 10:17:17 -0700 | [diff] [blame] | 987 | Mutex::Autolock autoLock(mMiscStateLock); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 988 | *positionUs = mVideoTimeUs; |
| 989 | } else if (mAudioPlayer != NULL) { |
| 990 | *positionUs = mAudioPlayer->getMediaTimeUs(); |
| 991 | } else { |
| 992 | *positionUs = 0; |
| 993 | } |
| 994 | |
| 995 | return OK; |
| 996 | } |
| 997 | |
| 998 | status_t AwesomePlayer::seekTo(int64_t timeUs) { |
Andreas Huber | 10b9b3f | 2010-10-08 10:16:24 -0700 | [diff] [blame] | 999 | if (mExtractorFlags & MediaExtractor::CAN_SEEK) { |
Andreas Huber | 62f7ffe | 2010-05-06 10:18:05 -0700 | [diff] [blame] | 1000 | Mutex::Autolock autoLock(mLock); |
| 1001 | return seekTo_l(timeUs); |
| 1002 | } |
| 1003 | |
| 1004 | return OK; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Andreas Huber | 0c46b69 | 2010-10-08 15:21:08 -0700 | [diff] [blame] | 1007 | // static |
| 1008 | void AwesomePlayer::OnRTSPSeekDoneWrapper(void *cookie) { |
| 1009 | static_cast<AwesomePlayer *>(cookie)->onRTSPSeekDone(); |
| 1010 | } |
| 1011 | |
| 1012 | void AwesomePlayer::onRTSPSeekDone() { |
| 1013 | notifyListener_l(MEDIA_SEEK_COMPLETE); |
| 1014 | mSeekNotificationSent = true; |
| 1015 | } |
| 1016 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1017 | status_t AwesomePlayer::seekTo_l(int64_t timeUs) { |
Andreas Huber | e0dd7d3 | 2010-08-24 14:33:58 -0700 | [diff] [blame] | 1018 | if (mRTSPController != NULL) { |
Andreas Huber | 0c46b69 | 2010-10-08 15:21:08 -0700 | [diff] [blame] | 1019 | mRTSPController->seekAsync(timeUs, OnRTSPSeekDoneWrapper, this); |
Andreas Huber | e0dd7d3 | 2010-08-24 14:33:58 -0700 | [diff] [blame] | 1020 | return OK; |
| 1021 | } |
| 1022 | |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1023 | if (mFlags & CACHE_UNDERRUN) { |
| 1024 | mFlags &= ~CACHE_UNDERRUN; |
| 1025 | play_l(); |
| 1026 | } |
| 1027 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1028 | mSeeking = true; |
Andreas Huber | 8e2b941 | 2010-03-31 09:40:15 -0700 | [diff] [blame] | 1029 | mSeekNotificationSent = false; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1030 | mSeekTimeUs = timeUs; |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 1031 | mFlags &= ~(AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1032 | |
| 1033 | seekAudioIfNecessary_l(); |
| 1034 | |
Andreas Huber | 8e2b941 | 2010-03-31 09:40:15 -0700 | [diff] [blame] | 1035 | if (!(mFlags & PLAYING)) { |
| 1036 | LOGV("seeking while paused, sending SEEK_COMPLETE notification" |
| 1037 | " immediately."); |
| 1038 | |
| 1039 | notifyListener_l(MEDIA_SEEK_COMPLETE); |
| 1040 | mSeekNotificationSent = true; |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 1041 | |
| 1042 | if ((mFlags & PREPARED) && mVideoSource != NULL) { |
| 1043 | mFlags |= SEEK_PREVIEW; |
| 1044 | postVideoEvent_l(); |
| 1045 | } |
Andreas Huber | 8e2b941 | 2010-03-31 09:40:15 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1048 | return OK; |
| 1049 | } |
| 1050 | |
| 1051 | void AwesomePlayer::seekAudioIfNecessary_l() { |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 1052 | if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1053 | mAudioPlayer->seekTo(mSeekTimeUs); |
| 1054 | |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1055 | mWatchForAudioSeekComplete = true; |
| 1056 | mWatchForAudioEOS = true; |
Andreas Huber | 8e2b941 | 2010-03-31 09:40:15 -0700 | [diff] [blame] | 1057 | mSeekNotificationSent = false; |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (mDecryptHandle != NULL) { |
| 1060 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 1061 | Playback::PAUSE, 0); |
| 1062 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 1063 | Playback::START, mSeekTimeUs / 1000); |
| 1064 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1068 | void AwesomePlayer::setAudioSource(sp<MediaSource> source) { |
| 1069 | CHECK(source != NULL); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1070 | |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1071 | mAudioTrack = source; |
| 1072 | } |
| 1073 | |
| 1074 | status_t AwesomePlayer::initAudioDecoder() { |
| 1075 | sp<MetaData> meta = mAudioTrack->getFormat(); |
Andreas Huber | c79827a | 2010-01-05 10:54:55 -0800 | [diff] [blame] | 1076 | |
| 1077 | const char *mime; |
| 1078 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 1079 | |
| 1080 | if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) { |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1081 | mAudioSource = mAudioTrack; |
Andreas Huber | c79827a | 2010-01-05 10:54:55 -0800 | [diff] [blame] | 1082 | } else { |
| 1083 | mAudioSource = OMXCodec::Create( |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1084 | mClient.interface(), mAudioTrack->getFormat(), |
Andreas Huber | c79827a | 2010-01-05 10:54:55 -0800 | [diff] [blame] | 1085 | false, // createEncoder |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1086 | mAudioTrack); |
Andreas Huber | c79827a | 2010-01-05 10:54:55 -0800 | [diff] [blame] | 1087 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1088 | |
| 1089 | if (mAudioSource != NULL) { |
| 1090 | int64_t durationUs; |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1091 | if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { |
Andreas Huber | 252573c | 2010-03-26 10:17:17 -0700 | [diff] [blame] | 1092 | Mutex::Autolock autoLock(mMiscStateLock); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1093 | if (mDurationUs < 0 || durationUs > mDurationUs) { |
| 1094 | mDurationUs = durationUs; |
| 1095 | } |
| 1096 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1097 | |
Andreas Huber | 3c78a1b | 2010-05-13 09:15:21 -0700 | [diff] [blame] | 1098 | status_t err = mAudioSource->start(); |
| 1099 | |
| 1100 | if (err != OK) { |
| 1101 | mAudioSource.clear(); |
| 1102 | return err; |
| 1103 | } |
Andreas Huber | d0332ad | 2010-04-12 16:05:57 -0700 | [diff] [blame] | 1104 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) { |
| 1105 | // For legacy reasons we're simply going to ignore the absence |
| 1106 | // of an audio decoder for QCELP instead of aborting playback |
| 1107 | // altogether. |
| 1108 | return OK; |
| 1109 | } |
Andreas Huber | dc9927d | 2010-03-08 15:46:13 -0800 | [diff] [blame] | 1110 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1111 | return mAudioSource != NULL ? OK : UNKNOWN_ERROR; |
| 1112 | } |
| 1113 | |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1114 | void AwesomePlayer::setVideoSource(sp<MediaSource> source) { |
| 1115 | CHECK(source != NULL); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1116 | |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1117 | mVideoTrack = source; |
| 1118 | } |
| 1119 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 1120 | status_t AwesomePlayer::initVideoDecoder(uint32_t flags) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1121 | mVideoSource = OMXCodec::Create( |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1122 | mClient.interface(), mVideoTrack->getFormat(), |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1123 | false, // createEncoder |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1124 | mVideoTrack, |
Andreas Huber | 6a1f5f9 | 2010-11-15 09:03:03 -0800 | [diff] [blame] | 1125 | NULL, flags, USE_SURFACE_ALLOC ? mSurface : NULL); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1126 | |
| 1127 | if (mVideoSource != NULL) { |
| 1128 | int64_t durationUs; |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1129 | if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { |
Andreas Huber | 252573c | 2010-03-26 10:17:17 -0700 | [diff] [blame] | 1130 | Mutex::Autolock autoLock(mMiscStateLock); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1131 | if (mDurationUs < 0 || durationUs > mDurationUs) { |
| 1132 | mDurationUs = durationUs; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Andreas Huber | 1919e5a | 2010-05-20 10:37:06 -0700 | [diff] [blame] | 1136 | status_t err = mVideoSource->start(); |
| 1137 | |
| 1138 | if (err != OK) { |
| 1139 | mVideoSource.clear(); |
| 1140 | return err; |
| 1141 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | return mVideoSource != NULL ? OK : UNKNOWN_ERROR; |
| 1145 | } |
| 1146 | |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1147 | void AwesomePlayer::finishSeekIfNecessary(int64_t videoTimeUs) { |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 1148 | if (!mSeeking || (mFlags & SEEK_PREVIEW)) { |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | if (mAudioPlayer != NULL) { |
Jamie Gennis | 6913c61 | 2010-10-20 15:55:43 -0700 | [diff] [blame] | 1153 | LOGV("seeking audio to %lld us (%.2f secs).", videoTimeUs, videoTimeUs / 1E6); |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1154 | |
| 1155 | // If we don't have a video time, seek audio to the originally |
| 1156 | // requested seek time instead. |
| 1157 | |
| 1158 | mAudioPlayer->seekTo(videoTimeUs < 0 ? mSeekTimeUs : videoTimeUs); |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1159 | mWatchForAudioSeekComplete = true; |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1160 | } else if (!mSeekNotificationSent) { |
| 1161 | // If we're playing video only, report seek complete now, |
| 1162 | // otherwise audio player will notify us later. |
| 1163 | notifyListener_l(MEDIA_SEEK_COMPLETE); |
| 1164 | } |
| 1165 | |
| 1166 | mFlags |= FIRST_FRAME; |
| 1167 | mSeeking = false; |
| 1168 | mSeekNotificationSent = false; |
Gloria Wang | 3f9a819 | 2010-10-29 14:50:17 -0700 | [diff] [blame] | 1169 | |
| 1170 | if (mDecryptHandle != NULL) { |
| 1171 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 1172 | Playback::PAUSE, 0); |
| 1173 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, |
| 1174 | Playback::START, videoTimeUs / 1000); |
| 1175 | } |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1178 | void AwesomePlayer::onVideoEvent() { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1179 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | ba7ec91 | 2010-02-12 10:42:02 -0800 | [diff] [blame] | 1180 | if (!mVideoEventPending) { |
| 1181 | // The event has been cancelled in reset_l() but had already |
| 1182 | // been scheduled for execution at that time. |
| 1183 | return; |
| 1184 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1185 | mVideoEventPending = false; |
| 1186 | |
| 1187 | if (mSeeking) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1188 | if (mVideoBuffer) { |
| 1189 | mVideoBuffer->release(); |
| 1190 | mVideoBuffer = NULL; |
| 1191 | } |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1192 | |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 1193 | if (mCachedSource != NULL && mAudioSource != NULL |
| 1194 | && !(mFlags & SEEK_PREVIEW)) { |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1195 | // We're going to seek the video source first, followed by |
| 1196 | // the audio source. |
| 1197 | // In order to avoid jumps in the DataSource offset caused by |
| 1198 | // the audio codec prefetching data from the old locations |
| 1199 | // while the video codec is already reading data from the new |
| 1200 | // locations, we'll "pause" the audio source, causing it to |
| 1201 | // stop reading input data until a subsequent seek. |
| 1202 | |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 1203 | if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1204 | mAudioPlayer->pause(); |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 1205 | |
| 1206 | mFlags &= ~AUDIO_RUNNING; |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1207 | } |
| 1208 | mAudioSource->pause(); |
| 1209 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | if (!mVideoBuffer) { |
| 1213 | MediaSource::ReadOptions options; |
| 1214 | if (mSeeking) { |
| 1215 | LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6); |
| 1216 | |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 1217 | options.setSeekTo( |
| 1218 | mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST_SYNC); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1219 | } |
| 1220 | for (;;) { |
| 1221 | status_t err = mVideoSource->read(&mVideoBuffer, &options); |
Andreas Huber | b1f5ee4 | 2009-12-14 15:34:11 -0800 | [diff] [blame] | 1222 | options.clearSeekTo(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1223 | |
| 1224 | if (err != OK) { |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 1225 | CHECK(mVideoBuffer == NULL); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1226 | |
| 1227 | if (err == INFO_FORMAT_CHANGED) { |
| 1228 | LOGV("VideoSource signalled format change."); |
| 1229 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1230 | notifyVideoSize_l(); |
| 1231 | |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 1232 | if (mVideoRenderer != NULL) { |
Andreas Huber | 7b73cfc | 2010-02-12 14:40:08 -0800 | [diff] [blame] | 1233 | mVideoRendererIsPreview = false; |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 1234 | initRenderer_l(); |
| 1235 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1236 | continue; |
| 1237 | } |
| 1238 | |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1239 | // So video playback is complete, but we may still have |
| 1240 | // a seek request pending that needs to be applied |
| 1241 | // to the audio track. |
| 1242 | if (mSeeking) { |
| 1243 | LOGV("video stream ended while seeking!"); |
| 1244 | } |
| 1245 | finishSeekIfNecessary(-1); |
| 1246 | |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 1247 | mFlags |= VIDEO_AT_EOS; |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 1248 | postStreamDoneEvent_l(err); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | |
Andreas Huber | a67d538 | 2009-12-10 15:32:12 -0800 | [diff] [blame] | 1252 | if (mVideoBuffer->range_length() == 0) { |
Andreas Huber | 6ddcf01 | 2009-12-10 15:32:12 -0800 | [diff] [blame] | 1253 | // Some decoders, notably the PV AVC software decoder |
| 1254 | // return spurious empty buffers that we just want to ignore. |
| 1255 | |
Andreas Huber | a67d538 | 2009-12-10 15:32:12 -0800 | [diff] [blame] | 1256 | mVideoBuffer->release(); |
| 1257 | mVideoBuffer = NULL; |
| 1258 | continue; |
| 1259 | } |
| 1260 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | int64_t timeUs; |
| 1266 | CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 1267 | |
Andreas Huber | 252573c | 2010-03-26 10:17:17 -0700 | [diff] [blame] | 1268 | { |
| 1269 | Mutex::Autolock autoLock(mMiscStateLock); |
| 1270 | mVideoTimeUs = timeUs; |
| 1271 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1272 | |
Andreas Huber | 614d22c | 2010-10-29 15:47:52 -0700 | [diff] [blame] | 1273 | bool wasSeeking = mSeeking; |
Andreas Huber | 4d450a8 | 2010-10-19 09:34:44 -0700 | [diff] [blame] | 1274 | finishSeekIfNecessary(timeUs); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1275 | |
Andreas Huber | ce8a101 | 2011-02-11 13:09:36 -0800 | [diff] [blame] | 1276 | if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) { |
| 1277 | status_t err = startAudioPlayer_l(); |
| 1278 | if (err != OK) { |
| 1279 | LOGE("Startung the audio player failed w/ err %d", err); |
| 1280 | return; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 1284 | TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource; |
| 1285 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1286 | if (mFlags & FIRST_FRAME) { |
| 1287 | mFlags &= ~FIRST_FRAME; |
pmehendale | 28f939d | 2011-01-06 14:43:49 -0800 | [diff] [blame] | 1288 | mSinceLastDropped = 0; |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 1289 | mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | int64_t realTimeUs, mediaTimeUs; |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 1293 | if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1294 | && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) { |
| 1295 | mTimeSourceDeltaUs = realTimeUs - mediaTimeUs; |
| 1296 | } |
| 1297 | |
Andreas Huber | b2934b1 | 2011-02-08 10:18:41 -0800 | [diff] [blame] | 1298 | if (!wasSeeking) { |
Andreas Huber | 614d22c | 2010-10-29 15:47:52 -0700 | [diff] [blame] | 1299 | // Let's display the first frame after seeking right away. |
Andreas Huber | f88f844 | 2010-08-10 11:18:36 -0700 | [diff] [blame] | 1300 | |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 1301 | int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1302 | |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 1303 | int64_t latenessUs = nowUs - timeUs; |
| 1304 | |
| 1305 | if (latenessUs > 40000) { |
| 1306 | // We're more than 40ms late. |
| 1307 | LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6); |
| 1308 | if ( mSinceLastDropped > FRAME_DROP_FREQ) |
| 1309 | { |
| 1310 | LOGV("we're late by %lld us (%.2f secs) dropping one after %d frames", latenessUs, latenessUs / 1E6, mSinceLastDropped); |
| 1311 | mSinceLastDropped = 0; |
| 1312 | mVideoBuffer->release(); |
| 1313 | mVideoBuffer = NULL; |
| 1314 | |
| 1315 | postVideoEvent_l(); |
| 1316 | return; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | if (latenessUs < -10000) { |
| 1321 | // We're more than 10ms early. |
| 1322 | |
| 1323 | postVideoEvent_l(10000); |
pmehendale | 28f939d | 2011-01-06 14:43:49 -0800 | [diff] [blame] | 1324 | return; |
| 1325 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Andreas Huber | 7b73cfc | 2010-02-12 14:40:08 -0800 | [diff] [blame] | 1328 | if (mVideoRendererIsPreview || mVideoRenderer == NULL) { |
| 1329 | mVideoRendererIsPreview = false; |
| 1330 | |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 1331 | initRenderer_l(); |
| 1332 | } |
| 1333 | |
| 1334 | if (mVideoRenderer != NULL) { |
pmehendale | 28f939d | 2011-01-06 14:43:49 -0800 | [diff] [blame] | 1335 | mSinceLastDropped++; |
Andreas Huber | 7085b684 | 2010-02-03 16:02:02 -0800 | [diff] [blame] | 1336 | mVideoRenderer->render(mVideoBuffer); |
| 1337 | } |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1338 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1339 | mVideoBuffer->release(); |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1340 | mVideoBuffer = NULL; |
| 1341 | |
Andreas Huber | 0a84f4f | 2011-02-07 11:43:12 -0800 | [diff] [blame] | 1342 | if (wasSeeking && (mFlags & SEEK_PREVIEW)) { |
| 1343 | mFlags &= ~SEEK_PREVIEW; |
| 1344 | return; |
| 1345 | } |
| 1346 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1347 | postVideoEvent_l(); |
| 1348 | } |
| 1349 | |
| 1350 | void AwesomePlayer::postVideoEvent_l(int64_t delayUs) { |
| 1351 | if (mVideoEventPending) { |
| 1352 | return; |
| 1353 | } |
| 1354 | |
| 1355 | mVideoEventPending = true; |
| 1356 | mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs); |
| 1357 | } |
| 1358 | |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 1359 | void AwesomePlayer::postStreamDoneEvent_l(status_t status) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1360 | if (mStreamDoneEventPending) { |
| 1361 | return; |
| 1362 | } |
| 1363 | mStreamDoneEventPending = true; |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 1364 | |
| 1365 | mStreamDoneStatus = status; |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1366 | mQueue.postEvent(mStreamDoneEvent); |
| 1367 | } |
| 1368 | |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 1369 | void AwesomePlayer::postBufferingEvent_l() { |
Andreas Huber | b9e6383 | 2010-01-26 16:20:10 -0800 | [diff] [blame] | 1370 | if (mBufferingEventPending) { |
| 1371 | return; |
| 1372 | } |
| 1373 | mBufferingEventPending = true; |
| 1374 | mQueue.postEventWithDelay(mBufferingEvent, 1000000ll); |
| 1375 | } |
| 1376 | |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 1377 | void AwesomePlayer::postVideoLagEvent_l() { |
| 1378 | if (mVideoLagEventPending) { |
| 1379 | return; |
| 1380 | } |
| 1381 | mVideoLagEventPending = true; |
| 1382 | mQueue.postEventWithDelay(mVideoLagEvent, 1000000ll); |
| 1383 | } |
| 1384 | |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1385 | void AwesomePlayer::postCheckAudioStatusEvent_l() { |
| 1386 | if (mAudioStatusEventPending) { |
| 1387 | return; |
| 1388 | } |
| 1389 | mAudioStatusEventPending = true; |
Andreas Huber | 2b359ed | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 1390 | mQueue.postEvent(mCheckAudioStatusEvent); |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | void AwesomePlayer::onCheckAudioStatus() { |
| 1394 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | c0178f1 | 2010-02-17 15:58:57 -0800 | [diff] [blame] | 1395 | if (!mAudioStatusEventPending) { |
| 1396 | // Event was dispatched and while we were blocking on the mutex, |
| 1397 | // has already been cancelled. |
| 1398 | return; |
| 1399 | } |
| 1400 | |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1401 | mAudioStatusEventPending = false; |
| 1402 | |
| 1403 | if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) { |
| 1404 | mWatchForAudioSeekComplete = false; |
Andreas Huber | 8e2b941 | 2010-03-31 09:40:15 -0700 | [diff] [blame] | 1405 | |
| 1406 | if (!mSeekNotificationSent) { |
| 1407 | notifyListener_l(MEDIA_SEEK_COMPLETE); |
| 1408 | mSeekNotificationSent = true; |
| 1409 | } |
Andreas Huber | ddb709c | 2010-04-07 10:24:35 -0700 | [diff] [blame] | 1410 | |
| 1411 | mSeeking = false; |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 1414 | status_t finalStatus; |
| 1415 | if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) { |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1416 | mWatchForAudioEOS = false; |
Andreas Huber | 971305d | 2010-07-07 13:35:27 -0700 | [diff] [blame] | 1417 | mFlags |= AUDIO_AT_EOS; |
| 1418 | mFlags |= FIRST_FRAME; |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 1419 | postStreamDoneEvent_l(finalStatus); |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1420 | } |
Andreas Huber | 70d10c0 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1423 | status_t AwesomePlayer::prepare() { |
| 1424 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1425 | return prepare_l(); |
| 1426 | } |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1427 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1428 | status_t AwesomePlayer::prepare_l() { |
| 1429 | if (mFlags & PREPARED) { |
| 1430 | return OK; |
| 1431 | } |
| 1432 | |
| 1433 | if (mFlags & PREPARING) { |
| 1434 | return UNKNOWN_ERROR; |
| 1435 | } |
| 1436 | |
| 1437 | mIsAsyncPrepare = false; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1438 | status_t err = prepareAsync_l(); |
| 1439 | |
| 1440 | if (err != OK) { |
| 1441 | return err; |
| 1442 | } |
| 1443 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1444 | while (mFlags & PREPARING) { |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1445 | mPreparedCondition.wait(mLock); |
| 1446 | } |
| 1447 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1448 | return mPrepareResult; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | status_t AwesomePlayer::prepareAsync() { |
| 1452 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1453 | |
| 1454 | if (mFlags & PREPARING) { |
| 1455 | return UNKNOWN_ERROR; // async prepare already pending |
| 1456 | } |
| 1457 | |
| 1458 | mIsAsyncPrepare = true; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1459 | return prepareAsync_l(); |
| 1460 | } |
| 1461 | |
| 1462 | status_t AwesomePlayer::prepareAsync_l() { |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1463 | if (mFlags & PREPARING) { |
| 1464 | return UNKNOWN_ERROR; // async prepare already pending |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Andreas Huber | 406a18b | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 1467 | if (!mQueueStarted) { |
| 1468 | mQueue.start(); |
| 1469 | mQueueStarted = true; |
| 1470 | } |
| 1471 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1472 | mFlags |= PREPARING; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1473 | mAsyncPrepareEvent = new AwesomeEvent( |
| 1474 | this, &AwesomePlayer::onPrepareAsyncEvent); |
| 1475 | |
| 1476 | mQueue.postEvent(mAsyncPrepareEvent); |
| 1477 | |
| 1478 | return OK; |
| 1479 | } |
| 1480 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1481 | status_t AwesomePlayer::finishSetDataSource_l() { |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 1482 | sp<DataSource> dataSource; |
| 1483 | |
| 1484 | if (!strncasecmp("http://", mUri.string(), 7)) { |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1485 | mConnectingDataSource = new NuHTTPDataSource; |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 1486 | |
| 1487 | mLock.unlock(); |
Andreas Huber | 3a53dc5 | 2010-06-11 09:57:46 -0700 | [diff] [blame] | 1488 | status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders); |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 1489 | mLock.lock(); |
| 1490 | |
| 1491 | if (err != OK) { |
| 1492 | mConnectingDataSource.clear(); |
| 1493 | |
| 1494 | LOGI("mConnectingDataSource->connect() returned %d", err); |
| 1495 | return err; |
| 1496 | } |
| 1497 | |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1498 | #if 0 |
| 1499 | mCachedSource = new NuCachedSource2( |
| 1500 | new ThrottledSource( |
| 1501 | mConnectingDataSource, 50 * 1024 /* bytes/sec */)); |
| 1502 | #else |
| 1503 | mCachedSource = new NuCachedSource2(mConnectingDataSource); |
| 1504 | #endif |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 1505 | mConnectingDataSource.clear(); |
Andreas Huber | 4d61f60 | 2010-06-10 11:17:50 -0700 | [diff] [blame] | 1506 | |
| 1507 | dataSource = mCachedSource; |
Andreas Huber | 9634665 | 2011-01-19 15:07:19 -0800 | [diff] [blame] | 1508 | |
| 1509 | // We're going to prefill the cache before trying to instantiate |
| 1510 | // the extractor below, as the latter is an operation that otherwise |
| 1511 | // could block on the datasource for a significant amount of time. |
| 1512 | // During that time we'd be unable to abort the preparation phase |
| 1513 | // without this prefill. |
| 1514 | |
| 1515 | mLock.unlock(); |
| 1516 | |
| 1517 | for (;;) { |
| 1518 | status_t finalStatus; |
| 1519 | size_t cachedDataRemaining = |
| 1520 | mCachedSource->approxDataRemaining(&finalStatus); |
| 1521 | |
| 1522 | if (finalStatus != OK || cachedDataRemaining >= kHighWaterMarkBytes |
| 1523 | || (mFlags & PREPARE_CANCELLED)) { |
| 1524 | break; |
| 1525 | } |
| 1526 | |
| 1527 | usleep(200000); |
| 1528 | } |
| 1529 | |
| 1530 | mLock.lock(); |
| 1531 | |
| 1532 | if (mFlags & PREPARE_CANCELLED) { |
| 1533 | LOGI("Prepare cancelled while waiting for initial cache fill."); |
| 1534 | return UNKNOWN_ERROR; |
| 1535 | } |
Andreas Huber | 7a747b8 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 1536 | } else if (!strncasecmp("rtsp://", mUri.string(), 7)) { |
| 1537 | if (mLooper == NULL) { |
| 1538 | mLooper = new ALooper; |
Andreas Huber | c4e0b70 | 2010-08-27 15:21:07 -0700 | [diff] [blame] | 1539 | mLooper->setName("rtsp"); |
Andreas Huber | 7a747b8 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 1540 | mLooper->start(); |
| 1541 | } |
| 1542 | mRTSPController = new ARTSPController(mLooper); |
Andreas Huber | cb21879 | 2011-02-14 14:14:23 -0800 | [diff] [blame] | 1543 | mConnectingRTSPController = mRTSPController; |
| 1544 | |
| 1545 | mLock.unlock(); |
Andreas Huber | 7a747b8 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 1546 | status_t err = mRTSPController->connect(mUri.string()); |
Andreas Huber | cb21879 | 2011-02-14 14:14:23 -0800 | [diff] [blame] | 1547 | mLock.lock(); |
| 1548 | |
| 1549 | mConnectingRTSPController.clear(); |
Andreas Huber | 202348e | 2010-06-07 14:35:29 -0700 | [diff] [blame] | 1550 | |
Andreas Huber | 7a747b8 | 2010-06-07 15:19:40 -0700 | [diff] [blame] | 1551 | LOGI("ARTSPController::connect returned %d", err); |
| 1552 | |
| 1553 | if (err != OK) { |
| 1554 | mRTSPController.clear(); |
| 1555 | return err; |
| 1556 | } |
| 1557 | |
| 1558 | sp<MediaExtractor> extractor = mRTSPController.get(); |
Andreas Huber | 202348e | 2010-06-07 14:35:29 -0700 | [diff] [blame] | 1559 | return setDataSource_l(extractor); |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 1560 | } else { |
| 1561 | dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders); |
| 1562 | } |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1563 | |
| 1564 | if (dataSource == NULL) { |
| 1565 | return UNKNOWN_ERROR; |
| 1566 | } |
| 1567 | |
| 1568 | sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); |
| 1569 | |
| 1570 | if (extractor == NULL) { |
| 1571 | return UNKNOWN_ERROR; |
| 1572 | } |
| 1573 | |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 1574 | dataSource->getDrmInfo(&mDecryptHandle, &mDrmManagerClient); |
Gloria Wang | c2c22e7 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 1575 | if (mDecryptHandle != NULL) { |
Gloria Wang | adc4d9c | 2011-02-08 13:24:08 -0800 | [diff] [blame] | 1576 | CHECK(mDrmManagerClient); |
Gloria Wang | c2c22e7 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 1577 | if (RightsStatus::RIGHTS_VALID == mDecryptHandle->status) { |
| 1578 | if (DecryptApiType::WV_BASED == mDecryptHandle->decryptApiType) { |
| 1579 | LOGD("Setting mCachedSource to NULL for WVM\n"); |
| 1580 | mCachedSource.clear(); |
| 1581 | } |
| 1582 | } else { |
| 1583 | notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_NO_LICENSE); |
| 1584 | } |
Gloria Wang | d577091 | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1587 | return setDataSource_l(extractor); |
| 1588 | } |
| 1589 | |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1590 | void AwesomePlayer::abortPrepare(status_t err) { |
| 1591 | CHECK(err != OK); |
| 1592 | |
| 1593 | if (mIsAsyncPrepare) { |
| 1594 | notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); |
| 1595 | } |
| 1596 | |
| 1597 | mPrepareResult = err; |
Andreas Huber | eaf2c5a | 2010-10-19 12:18:51 -0700 | [diff] [blame] | 1598 | mFlags &= ~(PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED); |
Andreas Huber | 3ac94ef | 2010-03-05 10:42:10 -0800 | [diff] [blame] | 1599 | mAsyncPrepareEvent = NULL; |
| 1600 | mPreparedCondition.broadcast(); |
| 1601 | } |
| 1602 | |
Andreas Huber | f71daba | 2010-03-24 09:24:40 -0700 | [diff] [blame] | 1603 | // static |
| 1604 | bool AwesomePlayer::ContinuePreparation(void *cookie) { |
| 1605 | AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie); |
| 1606 | |
| 1607 | return (me->mFlags & PREPARE_CANCELLED) == 0; |
| 1608 | } |
| 1609 | |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1610 | void AwesomePlayer::onPrepareAsyncEvent() { |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 1611 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1612 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 1613 | if (mFlags & PREPARE_CANCELLED) { |
| 1614 | LOGI("prepare was cancelled before doing anything"); |
| 1615 | abortPrepare(UNKNOWN_ERROR); |
| 1616 | return; |
| 1617 | } |
| 1618 | |
| 1619 | if (mUri.size() > 0) { |
| 1620 | status_t err = finishSetDataSource_l(); |
| 1621 | |
| 1622 | if (err != OK) { |
| 1623 | abortPrepare(err); |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 1624 | return; |
| 1625 | } |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 1628 | if (mVideoTrack != NULL && mVideoSource == NULL) { |
| 1629 | status_t err = initVideoDecoder(); |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1630 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 1631 | if (err != OK) { |
| 1632 | abortPrepare(err); |
| 1633 | return; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | if (mAudioTrack != NULL && mAudioSource == NULL) { |
| 1638 | status_t err = initAudioDecoder(); |
| 1639 | |
| 1640 | if (err != OK) { |
| 1641 | abortPrepare(err); |
| 1642 | return; |
| 1643 | } |
| 1644 | } |
| 1645 | |
Andreas Huber | eaf2c5a | 2010-10-19 12:18:51 -0700 | [diff] [blame] | 1646 | mFlags |= PREPARING_CONNECTED; |
| 1647 | |
Andreas Huber | 87ab9cd | 2010-09-03 13:20:33 -0700 | [diff] [blame] | 1648 | if (mCachedSource != NULL || mRTSPController != NULL) { |
| 1649 | postBufferingEvent_l(); |
| 1650 | } else { |
| 1651 | finishAsyncPrepare_l(); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | void AwesomePlayer::finishAsyncPrepare_l() { |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1656 | if (mIsAsyncPrepare) { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1657 | if (mVideoSource == NULL) { |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1658 | notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0); |
| 1659 | } else { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1660 | notifyVideoSize_l(); |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | notifyListener_l(MEDIA_PREPARED); |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1666 | mPrepareResult = OK; |
Andreas Huber | eaf2c5a | 2010-10-19 12:18:51 -0700 | [diff] [blame] | 1667 | mFlags &= ~(PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED); |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1668 | mFlags |= PREPARED; |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1669 | mAsyncPrepareEvent = NULL; |
Andreas Huber | ffdf478 | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 1670 | mPreparedCondition.broadcast(); |
Andreas Huber | 6be780e | 2010-02-08 14:40:30 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
Andreas Huber | 62f7ffe | 2010-05-06 10:18:05 -0700 | [diff] [blame] | 1673 | uint32_t AwesomePlayer::flags() const { |
| 1674 | return mExtractorFlags; |
| 1675 | } |
| 1676 | |
Andreas Huber | 2b359ed | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 1677 | void AwesomePlayer::postAudioEOS() { |
| 1678 | postCheckAudioStatusEvent_l(); |
| 1679 | } |
| 1680 | |
| 1681 | void AwesomePlayer::postAudioSeekComplete() { |
| 1682 | postCheckAudioStatusEvent_l(); |
| 1683 | } |
| 1684 | |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 1685 | } // namespace android |