Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "NuPlayer" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "NuPlayer.h" |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 22 | |
| 23 | #include "HTTPLiveSource.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 24 | #include "NuPlayerDecoder.h" |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 25 | #include "NuPlayerDecoderPassThrough.h" |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 26 | #include "NuPlayerDriver.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 27 | #include "NuPlayerRenderer.h" |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 28 | #include "NuPlayerSource.h" |
Andreas Huber | 2bfdd42 | 2011-10-11 15:24:07 -0700 | [diff] [blame] | 29 | #include "RTSPSource.h" |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 30 | #include "StreamingSource.h" |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 31 | #include "GenericSource.h" |
Robert Shih | d3b0bbb | 2014-07-23 15:00:25 -0700 | [diff] [blame] | 32 | #include "TextDescriptions.h" |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 33 | |
| 34 | #include "ATSParser.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 35 | |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 36 | #include <media/stagefright/foundation/hexdump.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 37 | #include <media/stagefright/foundation/ABuffer.h> |
| 38 | #include <media/stagefright/foundation/ADebug.h> |
| 39 | #include <media/stagefright/foundation/AMessage.h> |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 40 | #include <media/stagefright/MediaBuffer.h> |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 41 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 42 | #include <media/stagefright/MediaErrors.h> |
| 43 | #include <media/stagefright/MetaData.h> |
Andy McFadden | 8ba0102 | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 44 | #include <gui/IGraphicBufferProducer.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 45 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 46 | #include "avc_utils.h" |
| 47 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 48 | #include "ESDS.h" |
| 49 | #include <media/stagefright/Utils.h> |
| 50 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 51 | namespace android { |
| 52 | |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 53 | // TODO optimize buffer size for power consumption |
| 54 | // The offload read buffer size is 32 KB but 24 KB uses less power. |
| 55 | const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024; |
| 56 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 57 | struct NuPlayer::Action : public RefBase { |
| 58 | Action() {} |
| 59 | |
| 60 | virtual void execute(NuPlayer *player) = 0; |
| 61 | |
| 62 | private: |
| 63 | DISALLOW_EVIL_CONSTRUCTORS(Action); |
| 64 | }; |
| 65 | |
| 66 | struct NuPlayer::SeekAction : public Action { |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 67 | SeekAction(int64_t seekTimeUs, bool needNotify) |
| 68 | : mSeekTimeUs(seekTimeUs), |
| 69 | mNeedNotify(needNotify) { |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | virtual void execute(NuPlayer *player) { |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 73 | player->performSeek(mSeekTimeUs, mNeedNotify); |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | private: |
| 77 | int64_t mSeekTimeUs; |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 78 | bool mNeedNotify; |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 79 | |
| 80 | DISALLOW_EVIL_CONSTRUCTORS(SeekAction); |
| 81 | }; |
| 82 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 83 | struct NuPlayer::SetSurfaceAction : public Action { |
| 84 | SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper) |
| 85 | : mWrapper(wrapper) { |
| 86 | } |
| 87 | |
| 88 | virtual void execute(NuPlayer *player) { |
| 89 | player->performSetSurface(mWrapper); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | sp<NativeWindowWrapper> mWrapper; |
| 94 | |
| 95 | DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction); |
| 96 | }; |
| 97 | |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 98 | struct NuPlayer::ShutdownDecoderAction : public Action { |
| 99 | ShutdownDecoderAction(bool audio, bool video) |
| 100 | : mAudio(audio), |
| 101 | mVideo(video) { |
| 102 | } |
| 103 | |
| 104 | virtual void execute(NuPlayer *player) { |
| 105 | player->performDecoderShutdown(mAudio, mVideo); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | bool mAudio; |
| 110 | bool mVideo; |
| 111 | |
| 112 | DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction); |
| 113 | }; |
| 114 | |
| 115 | struct NuPlayer::PostMessageAction : public Action { |
| 116 | PostMessageAction(const sp<AMessage> &msg) |
| 117 | : mMessage(msg) { |
| 118 | } |
| 119 | |
| 120 | virtual void execute(NuPlayer *) { |
| 121 | mMessage->post(); |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | sp<AMessage> mMessage; |
| 126 | |
| 127 | DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction); |
| 128 | }; |
| 129 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 130 | // Use this if there's no state necessary to save in order to execute |
| 131 | // the action. |
| 132 | struct NuPlayer::SimpleAction : public Action { |
| 133 | typedef void (NuPlayer::*ActionFunc)(); |
| 134 | |
| 135 | SimpleAction(ActionFunc func) |
| 136 | : mFunc(func) { |
| 137 | } |
| 138 | |
| 139 | virtual void execute(NuPlayer *player) { |
| 140 | (player->*mFunc)(); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | ActionFunc mFunc; |
| 145 | |
| 146 | DISALLOW_EVIL_CONSTRUCTORS(SimpleAction); |
| 147 | }; |
| 148 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 149 | //////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | NuPlayer::NuPlayer() |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 152 | : mUIDValid(false), |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 153 | mSourceFlags(0), |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 154 | mVideoIsAVC(false), |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 155 | mOffloadAudio(false), |
Wei Jia | 88703c3 | 2014-08-06 11:24:07 -0700 | [diff] [blame] | 156 | mAudioDecoderGeneration(0), |
| 157 | mVideoDecoderGeneration(0), |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 158 | mRendererGeneration(0), |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 159 | mAudioEOS(false), |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 160 | mVideoEOS(false), |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 161 | mScanSourcesPending(false), |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 162 | mScanSourcesGeneration(0), |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 163 | mPollDurationGeneration(0), |
Robert Shih | d3b0bbb | 2014-07-23 15:00:25 -0700 | [diff] [blame] | 164 | mTimedTextGeneration(0), |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 165 | mTimeDiscontinuityPending(false), |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 166 | mFlushingAudio(NONE), |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 167 | mFlushingVideo(NONE), |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 168 | mSkipRenderingAudioUntilMediaTimeUs(-1ll), |
| 169 | mSkipRenderingVideoUntilMediaTimeUs(-1ll), |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 170 | mNumFramesTotal(0ll), |
James Dong | 0d268a3 | 2012-08-31 12:18:27 -0700 | [diff] [blame] | 171 | mNumFramesDropped(0ll), |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 172 | mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), |
| 173 | mStarted(false) { |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 174 | clearFlushComplete(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | NuPlayer::~NuPlayer() { |
| 178 | } |
| 179 | |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 180 | void NuPlayer::setUID(uid_t uid) { |
| 181 | mUIDValid = true; |
| 182 | mUID = uid; |
| 183 | } |
| 184 | |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 185 | void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { |
| 186 | mDriver = driver; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 189 | void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 190 | sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); |
| 191 | |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 192 | sp<AMessage> notify = new AMessage(kWhatSourceNotify, id()); |
| 193 | |
Andreas Huber | 240abcc | 2014-02-13 13:32:37 -0800 | [diff] [blame] | 194 | msg->setObject("source", new StreamingSource(notify, source)); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 195 | msg->post(); |
| 196 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 197 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 198 | static bool IsHTTPLiveURL(const char *url) { |
| 199 | if (!strncasecmp("http://", url, 7) |
Andreas Huber | 9975940 | 2013-04-01 14:28:31 -0700 | [diff] [blame] | 200 | || !strncasecmp("https://", url, 8) |
| 201 | || !strncasecmp("file://", url, 7)) { |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 202 | size_t len = strlen(url); |
| 203 | if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | if (strstr(url,"m3u8")) { |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return false; |
| 213 | } |
| 214 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 215 | void NuPlayer::setDataSourceAsync( |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 216 | const sp<IMediaHTTPService> &httpService, |
| 217 | const char *url, |
| 218 | const KeyedVector<String8, String8> *headers) { |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 219 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 220 | sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); |
Oscar Rydhé | 7a33b77 | 2012-02-20 10:15:48 +0100 | [diff] [blame] | 221 | size_t len = strlen(url); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 222 | |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 223 | sp<AMessage> notify = new AMessage(kWhatSourceNotify, id()); |
| 224 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 225 | sp<Source> source; |
| 226 | if (IsHTTPLiveURL(url)) { |
Andreas Huber | 81e6844 | 2014-02-05 11:52:33 -0800 | [diff] [blame] | 227 | source = new HTTPLiveSource(notify, httpService, url, headers); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 228 | } else if (!strncasecmp(url, "rtsp://", 7)) { |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 229 | source = new RTSPSource( |
| 230 | notify, httpService, url, headers, mUIDValid, mUID); |
Oscar Rydhé | 7a33b77 | 2012-02-20 10:15:48 +0100 | [diff] [blame] | 231 | } else if ((!strncasecmp(url, "http://", 7) |
| 232 | || !strncasecmp(url, "https://", 8)) |
| 233 | && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) |
| 234 | || strstr(url, ".sdp?"))) { |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 235 | source = new RTSPSource( |
| 236 | notify, httpService, url, headers, mUIDValid, mUID, true); |
Andreas Huber | 2bfdd42 | 2011-10-11 15:24:07 -0700 | [diff] [blame] | 237 | } else { |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 238 | sp<GenericSource> genericSource = |
| 239 | new GenericSource(notify, mUIDValid, mUID); |
| 240 | // Don't set FLAG_SECURE on mSourceFlags here for widevine. |
| 241 | // The correct flags will be updated in Source::kWhatFlagsChanged |
| 242 | // handler when GenericSource is prepared. |
Andreas Huber | 2bfdd42 | 2011-10-11 15:24:07 -0700 | [diff] [blame] | 243 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 244 | status_t err = genericSource->setDataSource(httpService, url, headers); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 245 | |
| 246 | if (err == OK) { |
| 247 | source = genericSource; |
| 248 | } else { |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 249 | ALOGE("Failed to set data source!"); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 252 | msg->setObject("source", source); |
| 253 | msg->post(); |
| 254 | } |
| 255 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 256 | void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) { |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 257 | sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); |
| 258 | |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 259 | sp<AMessage> notify = new AMessage(kWhatSourceNotify, id()); |
| 260 | |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 261 | sp<GenericSource> source = |
| 262 | new GenericSource(notify, mUIDValid, mUID); |
| 263 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 264 | status_t err = source->setDataSource(fd, offset, length); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 265 | |
| 266 | if (err != OK) { |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 267 | ALOGE("Failed to set data source!"); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 268 | source = NULL; |
| 269 | } |
| 270 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 271 | msg->setObject("source", source); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 272 | msg->post(); |
| 273 | } |
| 274 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 275 | void NuPlayer::prepareAsync() { |
| 276 | (new AMessage(kWhatPrepare, id()))->post(); |
| 277 | } |
| 278 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 279 | void NuPlayer::setVideoSurfaceTextureAsync( |
Andy McFadden | 8ba0102 | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 280 | const sp<IGraphicBufferProducer> &bufferProducer) { |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 281 | sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id()); |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 282 | |
Andy McFadden | 8ba0102 | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 283 | if (bufferProducer == NULL) { |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 284 | msg->setObject("native-window", NULL); |
| 285 | } else { |
| 286 | msg->setObject( |
| 287 | "native-window", |
| 288 | new NativeWindowWrapper( |
Wei Jia | 9c03a40 | 2014-08-26 15:24:43 -0700 | [diff] [blame] | 289 | new Surface(bufferProducer, true /* controlledByApp */))); |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 292 | msg->post(); |
| 293 | } |
| 294 | |
| 295 | void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { |
| 296 | sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id()); |
| 297 | msg->setObject("sink", sink); |
| 298 | msg->post(); |
| 299 | } |
| 300 | |
| 301 | void NuPlayer::start() { |
| 302 | (new AMessage(kWhatStart, id()))->post(); |
| 303 | } |
| 304 | |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 305 | void NuPlayer::pause() { |
Andreas Huber | b408222 | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 306 | (new AMessage(kWhatPause, id()))->post(); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void NuPlayer::resume() { |
Andreas Huber | b408222 | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 310 | (new AMessage(kWhatResume, id()))->post(); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 313 | void NuPlayer::resetAsync() { |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 314 | if (mSource != NULL) { |
| 315 | // During a reset, the data source might be unresponsive already, we need to |
| 316 | // disconnect explicitly so that reads exit promptly. |
| 317 | // We can't queue the disconnect request to the looper, as it might be |
| 318 | // queued behind a stuck read and never gets processed. |
| 319 | // Doing a disconnect outside the looper to allows the pending reads to exit |
| 320 | // (either successfully or with error). |
| 321 | mSource->disconnect(); |
| 322 | } |
| 323 | |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 324 | (new AMessage(kWhatReset, id()))->post(); |
| 325 | } |
| 326 | |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 327 | void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) { |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 328 | sp<AMessage> msg = new AMessage(kWhatSeek, id()); |
| 329 | msg->setInt64("seekTimeUs", seekTimeUs); |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 330 | msg->setInt32("needNotify", needNotify); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 331 | msg->post(); |
| 332 | } |
| 333 | |
Andreas Huber | 53df1a4 | 2010-12-22 10:03:04 -0800 | [diff] [blame] | 334 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 335 | void NuPlayer::writeTrackInfo( |
| 336 | Parcel* reply, const sp<AMessage> format) const { |
| 337 | int32_t trackType; |
| 338 | CHECK(format->findInt32("type", &trackType)); |
| 339 | |
| 340 | AString lang; |
| 341 | CHECK(format->findString("language", &lang)); |
| 342 | |
| 343 | reply->writeInt32(2); // write something non-zero |
| 344 | reply->writeInt32(trackType); |
| 345 | reply->writeString16(String16(lang.c_str())); |
| 346 | |
| 347 | if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
| 348 | AString mime; |
| 349 | CHECK(format->findString("mime", &mime)); |
| 350 | |
| 351 | int32_t isAuto, isDefault, isForced; |
| 352 | CHECK(format->findInt32("auto", &isAuto)); |
| 353 | CHECK(format->findInt32("default", &isDefault)); |
| 354 | CHECK(format->findInt32("forced", &isForced)); |
| 355 | |
| 356 | reply->writeString16(String16(mime.c_str())); |
| 357 | reply->writeInt32(isAuto); |
| 358 | reply->writeInt32(isDefault); |
| 359 | reply->writeInt32(isForced); |
| 360 | } |
| 361 | } |
| 362 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 363 | void NuPlayer::onMessageReceived(const sp<AMessage> &msg) { |
| 364 | switch (msg->what()) { |
| 365 | case kWhatSetDataSource: |
| 366 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 367 | ALOGV("kWhatSetDataSource"); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 368 | |
| 369 | CHECK(mSource == NULL); |
| 370 | |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 371 | status_t err = OK; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 372 | sp<RefBase> obj; |
| 373 | CHECK(msg->findObject("source", &obj)); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 374 | if (obj != NULL) { |
| 375 | mSource = static_cast<Source *>(obj.get()); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 376 | } else { |
| 377 | err = UNKNOWN_ERROR; |
| 378 | } |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 379 | |
| 380 | CHECK(mDriver != NULL); |
| 381 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 382 | if (driver != NULL) { |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 383 | driver->notifySetDataSourceCompleted(err); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 384 | } |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | case kWhatPrepare: |
| 389 | { |
| 390 | mSource->prepareAsync(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 394 | case kWhatGetTrackInfo: |
| 395 | { |
| 396 | uint32_t replyID; |
| 397 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 398 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 399 | Parcel* reply; |
| 400 | CHECK(msg->findPointer("reply", (void**)&reply)); |
| 401 | |
| 402 | size_t inbandTracks = 0; |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 403 | if (mSource != NULL) { |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 404 | inbandTracks = mSource->getTrackCount(); |
| 405 | } |
| 406 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 407 | size_t ccTracks = 0; |
| 408 | if (mCCDecoder != NULL) { |
| 409 | ccTracks = mCCDecoder->getTrackCount(); |
| 410 | } |
| 411 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 412 | // total track count |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 413 | reply->writeInt32(inbandTracks + ccTracks); |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 414 | |
| 415 | // write inband tracks |
| 416 | for (size_t i = 0; i < inbandTracks; ++i) { |
| 417 | writeTrackInfo(reply, mSource->getTrackInfo(i)); |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 420 | // write CC track |
| 421 | for (size_t i = 0; i < ccTracks; ++i) { |
| 422 | writeTrackInfo(reply, mCCDecoder->getTrackInfo(i)); |
| 423 | } |
| 424 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 425 | sp<AMessage> response = new AMessage; |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 426 | response->postReply(replyID); |
| 427 | break; |
| 428 | } |
| 429 | |
Robert Shih | 7c4f0d7 | 2014-07-09 18:53:31 -0700 | [diff] [blame] | 430 | case kWhatGetSelectedTrack: |
| 431 | { |
| 432 | status_t err = INVALID_OPERATION; |
| 433 | if (mSource != NULL) { |
| 434 | err = OK; |
| 435 | |
| 436 | int32_t type32; |
| 437 | CHECK(msg->findInt32("type", (int32_t*)&type32)); |
| 438 | media_track_type type = (media_track_type)type32; |
| 439 | ssize_t selectedTrack = mSource->getSelectedTrack(type); |
| 440 | |
| 441 | Parcel* reply; |
| 442 | CHECK(msg->findPointer("reply", (void**)&reply)); |
| 443 | reply->writeInt32(selectedTrack); |
| 444 | } |
| 445 | |
| 446 | sp<AMessage> response = new AMessage; |
| 447 | response->setInt32("err", err); |
| 448 | |
| 449 | uint32_t replyID; |
| 450 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 451 | response->postReply(replyID); |
| 452 | break; |
| 453 | } |
| 454 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 455 | case kWhatSelectTrack: |
| 456 | { |
| 457 | uint32_t replyID; |
| 458 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 459 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 460 | size_t trackIndex; |
| 461 | int32_t select; |
| 462 | CHECK(msg->findSize("trackIndex", &trackIndex)); |
| 463 | CHECK(msg->findInt32("select", &select)); |
| 464 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 465 | status_t err = INVALID_OPERATION; |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 466 | |
| 467 | size_t inbandTracks = 0; |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 468 | if (mSource != NULL) { |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 469 | inbandTracks = mSource->getTrackCount(); |
| 470 | } |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 471 | size_t ccTracks = 0; |
| 472 | if (mCCDecoder != NULL) { |
| 473 | ccTracks = mCCDecoder->getTrackCount(); |
| 474 | } |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 475 | |
| 476 | if (trackIndex < inbandTracks) { |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 477 | err = mSource->selectTrack(trackIndex, select); |
Robert Shih | d3b0bbb | 2014-07-23 15:00:25 -0700 | [diff] [blame] | 478 | |
| 479 | if (!select && err == OK) { |
| 480 | int32_t type; |
| 481 | sp<AMessage> info = mSource->getTrackInfo(trackIndex); |
| 482 | if (info != NULL |
| 483 | && info->findInt32("type", &type) |
| 484 | && type == MEDIA_TRACK_TYPE_TIMEDTEXT) { |
| 485 | ++mTimedTextGeneration; |
| 486 | } |
| 487 | } |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 488 | } else { |
| 489 | trackIndex -= inbandTracks; |
| 490 | |
| 491 | if (trackIndex < ccTracks) { |
| 492 | err = mCCDecoder->selectTrack(trackIndex, select); |
| 493 | } |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | sp<AMessage> response = new AMessage; |
| 497 | response->setInt32("err", err); |
| 498 | |
| 499 | response->postReply(replyID); |
| 500 | break; |
| 501 | } |
| 502 | |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 503 | case kWhatPollDuration: |
| 504 | { |
| 505 | int32_t generation; |
| 506 | CHECK(msg->findInt32("generation", &generation)); |
| 507 | |
| 508 | if (generation != mPollDurationGeneration) { |
| 509 | // stale |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | int64_t durationUs; |
| 514 | if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { |
| 515 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 516 | if (driver != NULL) { |
| 517 | driver->notifyDuration(durationUs); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | msg->post(1000000ll); // poll again in a second. |
| 522 | break; |
| 523 | } |
| 524 | |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 525 | case kWhatSetVideoNativeWindow: |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 526 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 527 | ALOGV("kWhatSetVideoNativeWindow"); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 528 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 529 | mDeferredActions.push_back( |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 530 | new ShutdownDecoderAction( |
| 531 | false /* audio */, true /* video */)); |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 532 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 533 | sp<RefBase> obj; |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 534 | CHECK(msg->findObject("native-window", &obj)); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 535 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 536 | mDeferredActions.push_back( |
| 537 | new SetSurfaceAction( |
| 538 | static_cast<NativeWindowWrapper *>(obj.get()))); |
James Dong | 0d268a3 | 2012-08-31 12:18:27 -0700 | [diff] [blame] | 539 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 540 | if (obj != NULL) { |
Wei Jia | 36f3898 | 2014-09-25 15:27:04 -0700 | [diff] [blame] | 541 | if (mStarted && mSource->getFormat(false /* audio */) != NULL) { |
Andy Hung | 7353585 | 2014-09-05 11:42:58 -0700 | [diff] [blame] | 542 | // Issue a seek to refresh the video screen only if started otherwise |
| 543 | // the extractor may not yet be started and will assert. |
| 544 | // If the video decoder is not set (perhaps audio only in this case) |
| 545 | // do not perform a seek as it is not needed. |
Ronghua Wu | a73d9e0 | 2014-10-08 15:13:29 -0700 | [diff] [blame] | 546 | int64_t currentPositionUs = 0; |
| 547 | if (getCurrentPosition(¤tPositionUs) == OK) { |
| 548 | mDeferredActions.push_back( |
| 549 | new SeekAction(currentPositionUs, false /* needNotify */)); |
| 550 | } |
Andy Hung | 7353585 | 2014-09-05 11:42:58 -0700 | [diff] [blame] | 551 | } |
Wei Jia | ac428aa | 2014-09-02 19:01:34 -0700 | [diff] [blame] | 552 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 553 | // If there is a new surface texture, instantiate decoders |
| 554 | // again if possible. |
| 555 | mDeferredActions.push_back( |
| 556 | new SimpleAction(&NuPlayer::performScanSources)); |
| 557 | } |
| 558 | |
| 559 | processDeferredActions(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | |
| 563 | case kWhatSetAudioSink: |
| 564 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 565 | ALOGV("kWhatSetAudioSink"); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 566 | |
| 567 | sp<RefBase> obj; |
| 568 | CHECK(msg->findObject("sink", &obj)); |
| 569 | |
| 570 | mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get()); |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | case kWhatStart: |
| 575 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 576 | ALOGV("kWhatStart"); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 577 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 578 | mVideoIsAVC = false; |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 579 | mOffloadAudio = false; |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 580 | mAudioEOS = false; |
| 581 | mVideoEOS = false; |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 582 | mSkipRenderingAudioUntilMediaTimeUs = -1; |
| 583 | mSkipRenderingVideoUntilMediaTimeUs = -1; |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 584 | mNumFramesTotal = 0; |
| 585 | mNumFramesDropped = 0; |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 586 | mStarted = true; |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 587 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 588 | /* instantiate decoders now for secure playback */ |
| 589 | if (mSourceFlags & Source::FLAG_SECURE) { |
| 590 | if (mNativeWindow != NULL) { |
| 591 | instantiateDecoder(false, &mVideoDecoder); |
| 592 | } |
| 593 | |
| 594 | if (mAudioSink != NULL) { |
| 595 | instantiateDecoder(true, &mAudioDecoder); |
| 596 | } |
| 597 | } |
| 598 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 599 | mSource->start(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 600 | |
Andreas Huber | d5e5623 | 2013-03-12 11:01:43 -0700 | [diff] [blame] | 601 | uint32_t flags = 0; |
| 602 | |
| 603 | if (mSource->isRealTime()) { |
| 604 | flags |= Renderer::FLAG_REAL_TIME; |
| 605 | } |
| 606 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 607 | sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); |
| 608 | audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; |
| 609 | if (mAudioSink != NULL) { |
| 610 | streamType = mAudioSink->getAudioStreamType(); |
| 611 | } |
| 612 | |
| 613 | sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); |
| 614 | |
| 615 | mOffloadAudio = |
| 616 | canOffloadStream(audioMeta, (videoFormat != NULL), |
| 617 | true /* is_streaming */, streamType); |
| 618 | if (mOffloadAudio) { |
| 619 | flags |= Renderer::FLAG_OFFLOAD_AUDIO; |
| 620 | } |
| 621 | |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 622 | sp<AMessage> notify = new AMessage(kWhatRendererNotify, id()); |
| 623 | ++mRendererGeneration; |
| 624 | notify->setInt32("generation", mRendererGeneration); |
| 625 | mRenderer = new Renderer(mAudioSink, notify, flags); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 626 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 627 | mRendererLooper = new ALooper; |
| 628 | mRendererLooper->setName("NuPlayerRenderer"); |
| 629 | mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); |
| 630 | mRendererLooper->registerHandler(mRenderer); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 631 | |
Lajos Molnar | c851b5d | 2014-09-18 14:14:29 -0700 | [diff] [blame] | 632 | sp<MetaData> meta = getFileMeta(); |
| 633 | int32_t rate; |
| 634 | if (meta != NULL |
| 635 | && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) { |
| 636 | mRenderer->setVideoFrameRate(rate); |
| 637 | } |
| 638 | |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 639 | postScanSources(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
| 643 | case kWhatScanSources: |
| 644 | { |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 645 | int32_t generation; |
| 646 | CHECK(msg->findInt32("generation", &generation)); |
| 647 | if (generation != mScanSourcesGeneration) { |
| 648 | // Drop obsolete msg. |
| 649 | break; |
| 650 | } |
| 651 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 652 | mScanSourcesPending = false; |
| 653 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 654 | ALOGV("scanning sources haveAudio=%d, haveVideo=%d", |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 655 | mAudioDecoder != NULL, mVideoDecoder != NULL); |
| 656 | |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 657 | bool mHadAnySourcesBefore = |
| 658 | (mAudioDecoder != NULL) || (mVideoDecoder != NULL); |
| 659 | |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 660 | // initialize video before audio because successful initialization of |
| 661 | // video may change deep buffer mode of audio. |
Haynes Mathew George | 5d246ef | 2012-07-09 10:36:57 -0700 | [diff] [blame] | 662 | if (mNativeWindow != NULL) { |
| 663 | instantiateDecoder(false, &mVideoDecoder); |
| 664 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 665 | |
| 666 | if (mAudioSink != NULL) { |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 667 | if (mOffloadAudio) { |
| 668 | // open audio sink early under offload mode. |
| 669 | sp<AMessage> format = mSource->getFormat(true /*audio*/); |
| 670 | openAudioSink(format, true /*offloadOnly*/); |
| 671 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 672 | instantiateDecoder(true, &mAudioDecoder); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 675 | if (!mHadAnySourcesBefore |
| 676 | && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { |
| 677 | // This is the first time we've found anything playable. |
| 678 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 679 | if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) { |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 680 | schedulePollDuration(); |
| 681 | } |
| 682 | } |
| 683 | |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 684 | status_t err; |
| 685 | if ((err = mSource->feedMoreTSData()) != OK) { |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 686 | if (mAudioDecoder == NULL && mVideoDecoder == NULL) { |
| 687 | // We're not currently decoding anything (no audio or |
| 688 | // video tracks found) and we just ran out of input data. |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 689 | |
| 690 | if (err == ERROR_END_OF_STREAM) { |
| 691 | notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); |
| 692 | } else { |
| 693 | notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); |
| 694 | } |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 695 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 696 | break; |
| 697 | } |
| 698 | |
Andreas Huber | fbe9d81 | 2012-08-31 14:05:27 -0700 | [diff] [blame] | 699 | if ((mAudioDecoder == NULL && mAudioSink != NULL) |
| 700 | || (mVideoDecoder == NULL && mNativeWindow != NULL)) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 701 | msg->post(100000ll); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 702 | mScanSourcesPending = true; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 703 | } |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | case kWhatVideoNotify: |
| 708 | case kWhatAudioNotify: |
| 709 | { |
| 710 | bool audio = msg->what() == kWhatAudioNotify; |
| 711 | |
Wei Jia | 88703c3 | 2014-08-06 11:24:07 -0700 | [diff] [blame] | 712 | int32_t currentDecoderGeneration = |
| 713 | (audio? mAudioDecoderGeneration : mVideoDecoderGeneration); |
| 714 | int32_t requesterGeneration = currentDecoderGeneration - 1; |
| 715 | CHECK(msg->findInt32("generation", &requesterGeneration)); |
| 716 | |
| 717 | if (requesterGeneration != currentDecoderGeneration) { |
| 718 | ALOGV("got message from old %s decoder, generation(%d:%d)", |
| 719 | audio ? "audio" : "video", requesterGeneration, |
| 720 | currentDecoderGeneration); |
| 721 | sp<AMessage> reply; |
| 722 | if (!(msg->findMessage("reply", &reply))) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | reply->setInt32("err", INFO_DISCONTINUITY); |
| 727 | reply->post(); |
| 728 | return; |
| 729 | } |
| 730 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 731 | int32_t what; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 732 | CHECK(msg->findInt32("what", &what)); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 733 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 734 | if (what == Decoder::kWhatFillThisBuffer) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 735 | status_t err = feedDecoderInputData( |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 736 | audio, msg); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 737 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 738 | if (err == -EWOULDBLOCK) { |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 739 | if (mSource->feedMoreTSData() == OK) { |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 740 | msg->post(10 * 1000ll); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 741 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 742 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 743 | } else if (what == Decoder::kWhatEOS) { |
Andreas Huber | dc9bacd | 2011-09-26 10:53:29 -0700 | [diff] [blame] | 744 | int32_t err; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 745 | CHECK(msg->findInt32("err", &err)); |
Andreas Huber | dc9bacd | 2011-09-26 10:53:29 -0700 | [diff] [blame] | 746 | |
| 747 | if (err == ERROR_END_OF_STREAM) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 748 | ALOGV("got %s decoder EOS", audio ? "audio" : "video"); |
Andreas Huber | dc9bacd | 2011-09-26 10:53:29 -0700 | [diff] [blame] | 749 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 750 | ALOGV("got %s decoder EOS w/ error %d", |
Andreas Huber | dc9bacd | 2011-09-26 10:53:29 -0700 | [diff] [blame] | 751 | audio ? "audio" : "video", |
| 752 | err); |
| 753 | } |
| 754 | |
| 755 | mRenderer->queueEOS(audio, err); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 756 | } else if (what == Decoder::kWhatFlushCompleted) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 757 | ALOGV("decoder %s flush completed", audio ? "audio" : "video"); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 758 | |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 759 | handleFlushComplete(audio, true /* isDecoder */); |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 760 | finishFlushIfPossible(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 761 | } else if (what == Decoder::kWhatOutputFormatChanged) { |
| 762 | sp<AMessage> format; |
| 763 | CHECK(msg->findMessage("format", &format)); |
| 764 | |
Andreas Huber | 31e2508 | 2011-01-10 10:38:31 -0800 | [diff] [blame] | 765 | if (audio) { |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 766 | openAudioSink(format, false /*offloadOnly*/); |
Andreas Huber | 31e2508 | 2011-01-10 10:38:31 -0800 | [diff] [blame] | 767 | } else { |
| 768 | // video |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 769 | sp<AMessage> inputFormat = |
| 770 | mSource->getFormat(false /* audio */); |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 771 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 772 | updateVideoSize(inputFormat, format); |
Andreas Huber | 31e2508 | 2011-01-10 10:38:31 -0800 | [diff] [blame] | 773 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 774 | } else if (what == Decoder::kWhatShutdownCompleted) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 775 | ALOGV("%s shutdown completed", audio ? "audio" : "video"); |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 776 | if (audio) { |
| 777 | mAudioDecoder.clear(); |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 778 | ++mAudioDecoderGeneration; |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 779 | |
| 780 | CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); |
| 781 | mFlushingAudio = SHUT_DOWN; |
| 782 | } else { |
| 783 | mVideoDecoder.clear(); |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 784 | ++mVideoDecoderGeneration; |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 785 | |
| 786 | CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); |
| 787 | mFlushingVideo = SHUT_DOWN; |
| 788 | } |
| 789 | |
| 790 | finishFlushIfPossible(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 791 | } else if (what == Decoder::kWhatError) { |
Chong Zhang | f4c0a94 | 2014-08-11 15:14:10 -0700 | [diff] [blame] | 792 | status_t err; |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 793 | if (!msg->findInt32("err", &err) || err == OK) { |
Chong Zhang | f4c0a94 | 2014-08-11 15:14:10 -0700 | [diff] [blame] | 794 | err = UNKNOWN_ERROR; |
| 795 | } |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 796 | |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 797 | // Decoder errors can be due to Source (e.g. from streaming), |
| 798 | // or from decoding corrupted bitstreams, or from other decoder |
| 799 | // MediaCodec operations (e.g. from an ongoing reset or seek). |
| 800 | // |
| 801 | // We try to gracefully shut down the affected decoder if possible, |
| 802 | // rather than trying to force the shutdown with something |
| 803 | // similar to performReset(). This method can lead to a hang |
| 804 | // if MediaCodec functions block after an error, but they should |
| 805 | // typically return INVALID_OPERATION instead of blocking. |
| 806 | |
| 807 | FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo; |
| 808 | ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down", |
| 809 | err, audio ? "audio" : "video", *flushing); |
| 810 | |
| 811 | switch (*flushing) { |
| 812 | case NONE: |
| 813 | mDeferredActions.push_back( |
| 814 | new ShutdownDecoderAction(audio, !audio /* video */)); |
| 815 | processDeferredActions(); |
| 816 | break; |
| 817 | case FLUSHING_DECODER: |
| 818 | *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush. |
| 819 | break; // Wait for flush to complete. |
| 820 | case FLUSHING_DECODER_SHUTDOWN: |
| 821 | break; // Wait for flush to complete. |
| 822 | case SHUTTING_DOWN_DECODER: |
| 823 | break; // Wait for shutdown to complete. |
| 824 | case FLUSHED: |
| 825 | // Widevine source reads must stop before releasing the video decoder. |
| 826 | if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { |
| 827 | mSource->stop(); |
| 828 | } |
| 829 | getDecoder(audio)->initiateShutdown(); // In the middle of a seek. |
| 830 | *flushing = SHUTTING_DOWN_DECODER; // Shut down. |
| 831 | break; |
| 832 | case SHUT_DOWN: |
| 833 | finishFlushIfPossible(); // Should not occur. |
| 834 | break; // Finish anyways. |
Marco Nelissen | 9e2b791 | 2014-08-18 16:13:03 -0700 | [diff] [blame] | 835 | } |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 836 | notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 837 | } else if (what == Decoder::kWhatDrainThisBuffer) { |
| 838 | renderBuffer(audio, msg); |
| 839 | } else { |
| 840 | ALOGV("Unhandled decoder notification %d '%c%c%c%c'.", |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 841 | what, |
| 842 | what >> 24, |
| 843 | (what >> 16) & 0xff, |
| 844 | (what >> 8) & 0xff, |
| 845 | what & 0xff); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | case kWhatRendererNotify: |
| 852 | { |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 853 | int32_t requesterGeneration = mRendererGeneration - 1; |
| 854 | CHECK(msg->findInt32("generation", &requesterGeneration)); |
| 855 | if (requesterGeneration != mRendererGeneration) { |
| 856 | ALOGV("got message from old renderer, generation(%d:%d)", |
| 857 | requesterGeneration, mRendererGeneration); |
| 858 | return; |
| 859 | } |
| 860 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 861 | int32_t what; |
| 862 | CHECK(msg->findInt32("what", &what)); |
| 863 | |
| 864 | if (what == Renderer::kWhatEOS) { |
| 865 | int32_t audio; |
| 866 | CHECK(msg->findInt32("audio", &audio)); |
| 867 | |
Andreas Huber | c92fd24 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 868 | int32_t finalResult; |
| 869 | CHECK(msg->findInt32("finalResult", &finalResult)); |
| 870 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 871 | if (audio) { |
| 872 | mAudioEOS = true; |
| 873 | } else { |
| 874 | mVideoEOS = true; |
| 875 | } |
| 876 | |
Andreas Huber | c92fd24 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 877 | if (finalResult == ERROR_END_OF_STREAM) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 878 | ALOGV("reached %s EOS", audio ? "audio" : "video"); |
Andreas Huber | c92fd24 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 879 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 880 | ALOGE("%s track encountered an error (%d)", |
Andreas Huber | c92fd24 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 881 | audio ? "audio" : "video", finalResult); |
| 882 | |
| 883 | notifyListener( |
| 884 | MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult); |
| 885 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 886 | |
| 887 | if ((mAudioEOS || mAudioDecoder == NULL) |
| 888 | && (mVideoEOS || mVideoDecoder == NULL)) { |
| 889 | notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); |
| 890 | } |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 891 | } else if (what == Renderer::kWhatFlushComplete) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 892 | int32_t audio; |
| 893 | CHECK(msg->findInt32("audio", &audio)); |
| 894 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 895 | ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 896 | handleFlushComplete(audio, false /* isDecoder */); |
| 897 | finishFlushIfPossible(); |
James Dong | f57b4ea | 2012-07-20 13:38:36 -0700 | [diff] [blame] | 898 | } else if (what == Renderer::kWhatVideoRenderingStart) { |
| 899 | notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0); |
Lajos Molnar | cbaffcf | 2013-08-14 18:30:38 -0700 | [diff] [blame] | 900 | } else if (what == Renderer::kWhatMediaRenderingStart) { |
| 901 | ALOGV("media rendering started"); |
| 902 | notifyListener(MEDIA_STARTED, 0, 0); |
Wei Jia | 3a2956d | 2014-07-22 16:01:33 -0700 | [diff] [blame] | 903 | } else if (what == Renderer::kWhatAudioOffloadTearDown) { |
| 904 | ALOGV("Tear down audio offload, fall back to s/w path"); |
| 905 | int64_t positionUs; |
| 906 | CHECK(msg->findInt64("positionUs", &positionUs)); |
Ronghua Wu | 0852917 | 2014-10-02 16:55:52 -0700 | [diff] [blame] | 907 | int32_t reason; |
| 908 | CHECK(msg->findInt32("reason", &reason)); |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 909 | closeAudioSink(); |
Wei Jia | 3a2956d | 2014-07-22 16:01:33 -0700 | [diff] [blame] | 910 | mAudioDecoder.clear(); |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 911 | ++mAudioDecoderGeneration; |
Wei Jia | 3a2956d | 2014-07-22 16:01:33 -0700 | [diff] [blame] | 912 | mRenderer->flush(true /* audio */); |
| 913 | if (mVideoDecoder != NULL) { |
| 914 | mRenderer->flush(false /* audio */); |
| 915 | } |
| 916 | mRenderer->signalDisableOffloadAudio(); |
| 917 | mOffloadAudio = false; |
| 918 | |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 919 | performSeek(positionUs, false /* needNotify */); |
Ronghua Wu | 0852917 | 2014-10-02 16:55:52 -0700 | [diff] [blame] | 920 | if (reason == Renderer::kDueToError) { |
| 921 | instantiateDecoder(true /* audio */, &mAudioDecoder); |
| 922 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 923 | } |
| 924 | break; |
| 925 | } |
| 926 | |
| 927 | case kWhatMoreDataQueued: |
| 928 | { |
| 929 | break; |
| 930 | } |
| 931 | |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 932 | case kWhatReset: |
| 933 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 934 | ALOGV("kWhatReset"); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 935 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 936 | mDeferredActions.push_back( |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 937 | new ShutdownDecoderAction( |
| 938 | true /* audio */, true /* video */)); |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 939 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 940 | mDeferredActions.push_back( |
| 941 | new SimpleAction(&NuPlayer::performReset)); |
Andreas Huber | b58ce9f | 2011-11-28 16:27:35 -0800 | [diff] [blame] | 942 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 943 | processDeferredActions(); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 947 | case kWhatSeek: |
| 948 | { |
| 949 | int64_t seekTimeUs; |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 950 | int32_t needNotify; |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 951 | CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 952 | CHECK(msg->findInt32("needNotify", &needNotify)); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 953 | |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 954 | ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d", |
| 955 | seekTimeUs, needNotify); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 956 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 957 | mDeferredActions.push_back( |
| 958 | new SimpleAction(&NuPlayer::performDecoderFlush)); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 959 | |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 960 | mDeferredActions.push_back( |
| 961 | new SeekAction(seekTimeUs, needNotify)); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 962 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 963 | processDeferredActions(); |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 964 | break; |
| 965 | } |
| 966 | |
Andreas Huber | b408222 | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 967 | case kWhatPause: |
| 968 | { |
Wei Jia | 7e9f7f7 | 2014-09-23 10:55:35 -0700 | [diff] [blame] | 969 | if (mSource != NULL) { |
| 970 | mSource->pause(); |
| 971 | } else { |
| 972 | ALOGW("pause called when source is gone or not set"); |
| 973 | } |
| 974 | if (mRenderer != NULL) { |
| 975 | mRenderer->pause(); |
| 976 | } else { |
| 977 | ALOGW("pause called when renderer is gone or not set"); |
| 978 | } |
Andreas Huber | b408222 | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 979 | break; |
| 980 | } |
| 981 | |
| 982 | case kWhatResume: |
| 983 | { |
Wei Jia | 7e9f7f7 | 2014-09-23 10:55:35 -0700 | [diff] [blame] | 984 | if (mSource != NULL) { |
| 985 | mSource->resume(); |
| 986 | } else { |
| 987 | ALOGW("resume called when source is gone or not set"); |
| 988 | } |
Ronghua Wu | d7988b1 | 2014-10-03 15:19:10 -0700 | [diff] [blame] | 989 | // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if |
| 990 | // needed. |
| 991 | if (audioDecoderStillNeeded() && mAudioDecoder == NULL) { |
Ronghua Wu | 0852917 | 2014-10-02 16:55:52 -0700 | [diff] [blame] | 992 | instantiateDecoder(true /* audio */, &mAudioDecoder); |
| 993 | } |
Wei Jia | 7e9f7f7 | 2014-09-23 10:55:35 -0700 | [diff] [blame] | 994 | if (mRenderer != NULL) { |
| 995 | mRenderer->resume(); |
| 996 | } else { |
| 997 | ALOGW("resume called when renderer is gone or not set"); |
| 998 | } |
Andreas Huber | b408222 | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 999 | break; |
| 1000 | } |
| 1001 | |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 1002 | case kWhatSourceNotify: |
| 1003 | { |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1004 | onSourceNotify(msg); |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 1005 | break; |
| 1006 | } |
| 1007 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1008 | case kWhatClosedCaptionNotify: |
| 1009 | { |
| 1010 | onClosedCaptionNotify(msg); |
| 1011 | break; |
| 1012 | } |
| 1013 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1014 | default: |
| 1015 | TRESPASS(); |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
Ronghua Wu | d7988b1 | 2014-10-03 15:19:10 -0700 | [diff] [blame] | 1020 | bool NuPlayer::audioDecoderStillNeeded() { |
| 1021 | // Audio decoder is no longer needed if it's in shut/shutting down status. |
| 1022 | return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER)); |
| 1023 | } |
| 1024 | |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 1025 | void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) { |
| 1026 | // We wait for both the decoder flush and the renderer flush to complete |
| 1027 | // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state. |
| 1028 | |
| 1029 | mFlushComplete[audio][isDecoder] = true; |
| 1030 | if (!mFlushComplete[audio][!isDecoder]) { |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo; |
| 1035 | switch (*state) { |
| 1036 | case FLUSHING_DECODER: |
| 1037 | { |
| 1038 | *state = FLUSHED; |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | case FLUSHING_DECODER_SHUTDOWN: |
| 1043 | { |
| 1044 | *state = SHUTTING_DOWN_DECODER; |
| 1045 | |
| 1046 | ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video"); |
| 1047 | if (!audio) { |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 1048 | // Widevine source reads must stop before releasing the video decoder. |
| 1049 | if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { |
| 1050 | mSource->stop(); |
| 1051 | } |
| 1052 | } |
| 1053 | getDecoder(audio)->initiateShutdown(); |
| 1054 | break; |
| 1055 | } |
| 1056 | |
| 1057 | default: |
| 1058 | // decoder flush completes only occur in a flushing state. |
| 1059 | LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state); |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1064 | void NuPlayer::finishFlushIfPossible() { |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1065 | if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED |
| 1066 | && mFlushingAudio != SHUT_DOWN) { |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1070 | if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED |
| 1071 | && mFlushingVideo != SHUT_DOWN) { |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1075 | ALOGV("both audio and video are flushed now."); |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1076 | |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1077 | mPendingAudioAccessUnit.clear(); |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1078 | mAggregateBuffer.clear(); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1079 | |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1080 | if (mTimeDiscontinuityPending) { |
| 1081 | mRenderer->signalTimeDiscontinuity(); |
| 1082 | mTimeDiscontinuityPending = false; |
| 1083 | } |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1084 | |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1085 | if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) { |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1086 | mAudioDecoder->signalResume(); |
| 1087 | } |
| 1088 | |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1089 | if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) { |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1090 | mVideoDecoder->signalResume(); |
| 1091 | } |
| 1092 | |
| 1093 | mFlushingAudio = NONE; |
| 1094 | mFlushingVideo = NONE; |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 1095 | |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 1096 | clearFlushComplete(); |
| 1097 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1098 | processDeferredActions(); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | void NuPlayer::postScanSources() { |
| 1102 | if (mScanSourcesPending) { |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | sp<AMessage> msg = new AMessage(kWhatScanSources, id()); |
| 1107 | msg->setInt32("generation", mScanSourcesGeneration); |
| 1108 | msg->post(); |
| 1109 | |
| 1110 | mScanSourcesPending = true; |
| 1111 | } |
| 1112 | |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1113 | void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) { |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1114 | uint32_t flags; |
| 1115 | int64_t durationUs; |
Chong Zhang | 3b9eb1f | 2014-10-15 17:05:08 -0700 | [diff] [blame^] | 1116 | bool hasVideo = (mVideoDecoder != NULL); |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1117 | // FIXME: we should handle the case where the video decoder |
| 1118 | // is created after we receive the format change indication. |
| 1119 | // Current code will just make that we select deep buffer |
| 1120 | // with video which should not be a problem as it should |
| 1121 | // not prevent from keeping A/V sync. |
Chong Zhang | 3b9eb1f | 2014-10-15 17:05:08 -0700 | [diff] [blame^] | 1122 | if (hasVideo && |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1123 | mSource->getDuration(&durationUs) == OK && |
| 1124 | durationUs |
| 1125 | > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) { |
| 1126 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 1127 | } else { |
| 1128 | flags = AUDIO_OUTPUT_FLAG_NONE; |
| 1129 | } |
| 1130 | |
Chong Zhang | 3b9eb1f | 2014-10-15 17:05:08 -0700 | [diff] [blame^] | 1131 | mOffloadAudio = mRenderer->openAudioSink( |
| 1132 | format, offloadOnly, hasVideo, flags); |
| 1133 | |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1134 | if (mOffloadAudio) { |
Chong Zhang | 3b9eb1f | 2014-10-15 17:05:08 -0700 | [diff] [blame^] | 1135 | sp<MetaData> audioMeta = |
| 1136 | mSource->getFormatMeta(true /* audio */); |
| 1137 | sendMetaDataToHal(mAudioSink, audioMeta); |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | void NuPlayer::closeAudioSink() { |
Chong Zhang | 3b9eb1f | 2014-10-15 17:05:08 -0700 | [diff] [blame^] | 1142 | mRenderer->closeAudioSink(); |
Andy Hung | 282a7e3 | 2014-08-14 15:56:34 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 1145 | status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1146 | if (*decoder != NULL) { |
| 1147 | return OK; |
| 1148 | } |
| 1149 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1150 | sp<AMessage> format = mSource->getFormat(audio); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1151 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1152 | if (format == NULL) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1153 | return -EWOULDBLOCK; |
| 1154 | } |
| 1155 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1156 | if (!audio) { |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1157 | AString mime; |
| 1158 | CHECK(format->findString("mime", &mime)); |
| 1159 | mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str()); |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1160 | |
| 1161 | sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id()); |
| 1162 | mCCDecoder = new CCDecoder(ccNotify); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 1163 | |
| 1164 | if (mSourceFlags & Source::FLAG_SECURE) { |
| 1165 | format->setInt32("secure", true); |
| 1166 | } |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 1169 | if (audio) { |
Wei Jia | 88703c3 | 2014-08-06 11:24:07 -0700 | [diff] [blame] | 1170 | sp<AMessage> notify = new AMessage(kWhatAudioNotify, id()); |
| 1171 | ++mAudioDecoderGeneration; |
| 1172 | notify->setInt32("generation", mAudioDecoderGeneration); |
| 1173 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 1174 | if (mOffloadAudio) { |
| 1175 | *decoder = new DecoderPassThrough(notify); |
| 1176 | } else { |
| 1177 | *decoder = new Decoder(notify); |
| 1178 | } |
| 1179 | } else { |
Wei Jia | 88703c3 | 2014-08-06 11:24:07 -0700 | [diff] [blame] | 1180 | sp<AMessage> notify = new AMessage(kWhatVideoNotify, id()); |
| 1181 | ++mVideoDecoderGeneration; |
| 1182 | notify->setInt32("generation", mVideoDecoderGeneration); |
| 1183 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 1184 | *decoder = new Decoder(notify, mNativeWindow); |
| 1185 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 1186 | (*decoder)->init(); |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1187 | (*decoder)->configure(format); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1188 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 1189 | // allocate buffers to decrypt widevine source buffers |
| 1190 | if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { |
| 1191 | Vector<sp<ABuffer> > inputBufs; |
| 1192 | CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); |
| 1193 | |
| 1194 | Vector<MediaBuffer *> mediaBufs; |
| 1195 | for (size_t i = 0; i < inputBufs.size(); i++) { |
| 1196 | const sp<ABuffer> &buffer = inputBufs[i]; |
| 1197 | MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); |
| 1198 | mediaBufs.push(mbuf); |
| 1199 | } |
| 1200 | |
| 1201 | status_t err = mSource->setBuffers(audio, mediaBufs); |
| 1202 | if (err != OK) { |
| 1203 | for (size_t i = 0; i < mediaBufs.size(); ++i) { |
| 1204 | mediaBufs[i]->release(); |
| 1205 | } |
| 1206 | mediaBufs.clear(); |
| 1207 | ALOGE("Secure source didn't support secure mediaBufs."); |
| 1208 | return err; |
| 1209 | } |
| 1210 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1211 | return OK; |
| 1212 | } |
| 1213 | |
| 1214 | status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { |
| 1215 | sp<AMessage> reply; |
| 1216 | CHECK(msg->findMessage("reply", &reply)); |
| 1217 | |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1218 | if ((audio && mFlushingAudio != NONE) |
Wei Jia | f702d04 | 2014-09-09 12:08:47 -0700 | [diff] [blame] | 1219 | || (!audio && mFlushingVideo != NONE) |
| 1220 | || mSource == NULL) { |
Wei Jia | b189a5b | 2014-08-07 06:11:39 +0000 | [diff] [blame] | 1221 | reply->setInt32("err", INFO_DISCONTINUITY); |
| 1222 | reply->post(); |
| 1223 | return OK; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | sp<ABuffer> accessUnit; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1227 | |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1228 | // Aggregate smaller buffers into a larger buffer. |
| 1229 | // The goal is to reduce power consumption. |
Phil Burk | 33b51b0 | 2014-09-17 16:03:47 -0700 | [diff] [blame] | 1230 | // Note this will not work if the decoder requires one frame per buffer. |
| 1231 | bool doBufferAggregation = (audio && mOffloadAudio); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1232 | bool needMoreData = false; |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1233 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1234 | bool dropAccessUnit; |
| 1235 | do { |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1236 | status_t err; |
| 1237 | // Did we save an accessUnit earlier because of a discontinuity? |
| 1238 | if (audio && (mPendingAudioAccessUnit != NULL)) { |
| 1239 | accessUnit = mPendingAudioAccessUnit; |
| 1240 | mPendingAudioAccessUnit.clear(); |
| 1241 | err = mPendingAudioErr; |
| 1242 | ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit"); |
| 1243 | } else { |
| 1244 | err = mSource->dequeueAccessUnit(audio, &accessUnit); |
| 1245 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 1246 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1247 | if (err == -EWOULDBLOCK) { |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1248 | return err; |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1249 | } else if (err != OK) { |
| 1250 | if (err == INFO_DISCONTINUITY) { |
Phil Burk | 33b51b0 | 2014-09-17 16:03:47 -0700 | [diff] [blame] | 1251 | if (doBufferAggregation && (mAggregateBuffer != NULL)) { |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1252 | // We already have some data so save this for later. |
| 1253 | mPendingAudioErr = err; |
| 1254 | mPendingAudioAccessUnit = accessUnit; |
| 1255 | accessUnit.clear(); |
| 1256 | ALOGD("feedDecoderInputData() save discontinuity for later"); |
| 1257 | break; |
| 1258 | } |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1259 | int32_t type; |
| 1260 | CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); |
Andreas Huber | 53df1a4 | 2010-12-22 10:03:04 -0800 | [diff] [blame] | 1261 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1262 | bool formatChange = |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1263 | (audio && |
| 1264 | (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT)) |
| 1265 | || (!audio && |
| 1266 | (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT)); |
Andreas Huber | 53df1a4 | 2010-12-22 10:03:04 -0800 | [diff] [blame] | 1267 | |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1268 | bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0; |
| 1269 | |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1270 | ALOGI("%s discontinuity (formatChange=%d, time=%d)", |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1271 | audio ? "audio" : "video", formatChange, timeChange); |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1272 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1273 | if (audio) { |
| 1274 | mSkipRenderingAudioUntilMediaTimeUs = -1; |
| 1275 | } else { |
| 1276 | mSkipRenderingVideoUntilMediaTimeUs = -1; |
| 1277 | } |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1278 | |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1279 | if (timeChange) { |
| 1280 | sp<AMessage> extra; |
| 1281 | if (accessUnit->meta()->findMessage("extra", &extra) |
| 1282 | && extra != NULL) { |
| 1283 | int64_t resumeAtMediaTimeUs; |
| 1284 | if (extra->findInt64( |
| 1285 | "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1286 | ALOGI("suppressing rendering of %s until %lld us", |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1287 | audio ? "audio" : "video", resumeAtMediaTimeUs); |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1288 | |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1289 | if (audio) { |
| 1290 | mSkipRenderingAudioUntilMediaTimeUs = |
| 1291 | resumeAtMediaTimeUs; |
| 1292 | } else { |
| 1293 | mSkipRenderingVideoUntilMediaTimeUs = |
| 1294 | resumeAtMediaTimeUs; |
| 1295 | } |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1296 | } |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1297 | } |
| 1298 | } |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1299 | |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1300 | mTimeDiscontinuityPending = |
| 1301 | mTimeDiscontinuityPending || timeChange; |
| 1302 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1303 | bool seamlessFormatChange = false; |
| 1304 | sp<AMessage> newFormat = mSource->getFormat(audio); |
| 1305 | if (formatChange) { |
| 1306 | seamlessFormatChange = |
| 1307 | getDecoder(audio)->supportsSeamlessFormatChange(newFormat); |
| 1308 | // treat seamless format change separately |
| 1309 | formatChange = !seamlessFormatChange; |
| 1310 | } |
| 1311 | bool shutdownOrFlush = formatChange || timeChange; |
| 1312 | |
| 1313 | // We want to queue up scan-sources only once per discontinuity. |
| 1314 | // We control this by doing it only if neither audio nor video are |
| 1315 | // flushing or shutting down. (After handling 1st discontinuity, one |
| 1316 | // of the flushing states will not be NONE.) |
| 1317 | // No need to scan sources if this discontinuity does not result |
| 1318 | // in a flush or shutdown, as the flushing state will stay NONE. |
| 1319 | if (mFlushingAudio == NONE && mFlushingVideo == NONE && |
| 1320 | shutdownOrFlush) { |
Robert Shih | a298101 | 2014-07-30 17:41:24 -0700 | [diff] [blame] | 1321 | // And we'll resume scanning sources once we're done |
| 1322 | // flushing. |
| 1323 | mDeferredActions.push_front( |
| 1324 | new SimpleAction( |
| 1325 | &NuPlayer::performScanSources)); |
| 1326 | } |
| 1327 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1328 | if (formatChange /* not seamless */) { |
| 1329 | // must change decoder |
| 1330 | flushDecoder(audio, /* needShutdown = */ true); |
| 1331 | } else if (timeChange) { |
| 1332 | // need to flush |
| 1333 | flushDecoder(audio, /* needShutdown = */ false, newFormat); |
| 1334 | err = OK; |
| 1335 | } else if (seamlessFormatChange) { |
| 1336 | // reuse existing decoder and don't flush |
| 1337 | updateDecoderFormatWithoutFlush(audio, newFormat); |
| 1338 | err = OK; |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1339 | } else { |
| 1340 | // This stream is unaffected by the discontinuity |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1341 | return -EWOULDBLOCK; |
| 1342 | } |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1345 | reply->setInt32("err", err); |
| 1346 | reply->post(); |
| 1347 | return OK; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1350 | if (!audio) { |
| 1351 | ++mNumFramesTotal; |
| 1352 | } |
| 1353 | |
| 1354 | dropAccessUnit = false; |
| 1355 | if (!audio |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 1356 | && !(mSourceFlags & Source::FLAG_SECURE) |
Ronghua Wu | a73d9e0 | 2014-10-08 15:13:29 -0700 | [diff] [blame] | 1357 | && mRenderer->getVideoLateByUs() > 100000ll |
Andreas Huber | 3fe6215 | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 1358 | && mVideoIsAVC |
| 1359 | && !IsAVCReferenceFrame(accessUnit)) { |
| 1360 | dropAccessUnit = true; |
| 1361 | ++mNumFramesDropped; |
| 1362 | } |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1363 | |
| 1364 | size_t smallSize = accessUnit->size(); |
| 1365 | needMoreData = false; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1366 | if (doBufferAggregation && (mAggregateBuffer == NULL) |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1367 | // Don't bother if only room for a few small buffers. |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1368 | && (smallSize < (kAggregateBufferSizeBytes / 3))) { |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1369 | // Create a larger buffer for combining smaller buffers from the extractor. |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1370 | mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes); |
| 1371 | mAggregateBuffer->setRange(0, 0); // start empty |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Phil Burk | 33b51b0 | 2014-09-17 16:03:47 -0700 | [diff] [blame] | 1374 | if (doBufferAggregation && (mAggregateBuffer != NULL)) { |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1375 | int64_t timeUs; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1376 | int64_t dummy; |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1377 | bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs); |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1378 | bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1379 | // Will the smaller buffer fit? |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1380 | size_t bigSize = mAggregateBuffer->size(); |
| 1381 | size_t roomLeft = mAggregateBuffer->capacity() - bigSize; |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1382 | // Should we save this small buffer for the next big buffer? |
| 1383 | // If the first small buffer did not have a timestamp then save |
| 1384 | // any buffer that does have a timestamp until the next big buffer. |
| 1385 | if ((smallSize > roomLeft) |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1386 | || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) { |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1387 | mPendingAudioErr = err; |
| 1388 | mPendingAudioAccessUnit = accessUnit; |
| 1389 | accessUnit.clear(); |
| 1390 | } else { |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1391 | // Grab time from first small buffer if available. |
| 1392 | if ((bigSize == 0) && smallTimestampValid) { |
| 1393 | mAggregateBuffer->meta()->setInt64("timeUs", timeUs); |
| 1394 | } |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1395 | // Append small buffer to the bigger buffer. |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1396 | memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1397 | bigSize += smallSize; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1398 | mAggregateBuffer->setRange(0, bigSize); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1399 | |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1400 | // Keep looping until we run out of room in the mAggregateBuffer. |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1401 | needMoreData = true; |
| 1402 | |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1403 | ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu", |
| 1404 | smallSize, bigSize, mAggregateBuffer->capacity()); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | } while (dropAccessUnit || needMoreData); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1408 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1409 | // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1410 | |
| 1411 | #if 0 |
| 1412 | int64_t mediaTimeUs; |
| 1413 | CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1414 | ALOGV("feeding %s input buffer at media time %.2f secs", |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1415 | audio ? "audio" : "video", |
| 1416 | mediaTimeUs / 1E6); |
| 1417 | #endif |
| 1418 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1419 | if (!audio) { |
| 1420 | mCCDecoder->decode(accessUnit); |
| 1421 | } |
| 1422 | |
Phil Burk | 33b51b0 | 2014-09-17 16:03:47 -0700 | [diff] [blame] | 1423 | if (doBufferAggregation && (mAggregateBuffer != NULL)) { |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1424 | ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu", |
| 1425 | mAggregateBuffer->size()); |
| 1426 | reply->setBuffer("buffer", mAggregateBuffer); |
| 1427 | mAggregateBuffer.clear(); |
Phil Burk | 9f52649 | 2014-09-03 15:04:12 -0700 | [diff] [blame] | 1428 | } else { |
| 1429 | reply->setBuffer("buffer", accessUnit); |
| 1430 | } |
| 1431 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1432 | reply->post(); |
| 1433 | |
| 1434 | return OK; |
| 1435 | } |
| 1436 | |
| 1437 | void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1438 | // ALOGV("renderBuffer %s", audio ? "audio" : "video"); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1439 | |
| 1440 | sp<AMessage> reply; |
| 1441 | CHECK(msg->findMessage("reply", &reply)); |
| 1442 | |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1443 | if ((audio && mFlushingAudio != NONE) |
| 1444 | || (!audio && mFlushingVideo != NONE)) { |
Andreas Huber | 18ac540 | 2011-08-31 15:04:25 -0700 | [diff] [blame] | 1445 | // We're currently attempting to flush the decoder, in order |
| 1446 | // to complete this, the decoder wants all its buffers back, |
| 1447 | // so we don't want any output buffers it sent us (from before |
| 1448 | // we initiated the flush) to be stuck in the renderer's queue. |
| 1449 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1450 | ALOGV("we're still flushing the %s decoder, sending its output buffer" |
Andreas Huber | 18ac540 | 2011-08-31 15:04:25 -0700 | [diff] [blame] | 1451 | " right back.", audio ? "audio" : "video"); |
| 1452 | |
| 1453 | reply->post(); |
| 1454 | return; |
| 1455 | } |
| 1456 | |
Andreas Huber | 2d8bedd | 2012-02-21 14:38:23 -0800 | [diff] [blame] | 1457 | sp<ABuffer> buffer; |
| 1458 | CHECK(msg->findBuffer("buffer", &buffer)); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1459 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1460 | int64_t mediaTimeUs; |
| 1461 | CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs)); |
| 1462 | |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1463 | int64_t &skipUntilMediaTimeUs = |
| 1464 | audio |
| 1465 | ? mSkipRenderingAudioUntilMediaTimeUs |
| 1466 | : mSkipRenderingVideoUntilMediaTimeUs; |
| 1467 | |
| 1468 | if (skipUntilMediaTimeUs >= 0) { |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1469 | |
| 1470 | if (mediaTimeUs < skipUntilMediaTimeUs) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1471 | ALOGV("dropping %s buffer at time %lld as requested.", |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 1472 | audio ? "audio" : "video", |
| 1473 | mediaTimeUs); |
| 1474 | |
| 1475 | reply->post(); |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | skipUntilMediaTimeUs = -1; |
| 1480 | } |
| 1481 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1482 | if (!audio && mCCDecoder->isSelected()) { |
| 1483 | mCCDecoder->display(mediaTimeUs); |
| 1484 | } |
| 1485 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1486 | mRenderer->queueBuffer(audio, buffer, reply); |
| 1487 | } |
| 1488 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1489 | void NuPlayer::updateVideoSize( |
| 1490 | const sp<AMessage> &inputFormat, |
| 1491 | const sp<AMessage> &outputFormat) { |
| 1492 | if (inputFormat == NULL) { |
| 1493 | ALOGW("Unknown video size, reporting 0x0!"); |
| 1494 | notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); |
| 1495 | return; |
| 1496 | } |
| 1497 | |
| 1498 | int32_t displayWidth, displayHeight; |
| 1499 | int32_t cropLeft, cropTop, cropRight, cropBottom; |
| 1500 | |
| 1501 | if (outputFormat != NULL) { |
| 1502 | int32_t width, height; |
| 1503 | CHECK(outputFormat->findInt32("width", &width)); |
| 1504 | CHECK(outputFormat->findInt32("height", &height)); |
| 1505 | |
| 1506 | int32_t cropLeft, cropTop, cropRight, cropBottom; |
| 1507 | CHECK(outputFormat->findRect( |
| 1508 | "crop", |
| 1509 | &cropLeft, &cropTop, &cropRight, &cropBottom)); |
| 1510 | |
| 1511 | displayWidth = cropRight - cropLeft + 1; |
| 1512 | displayHeight = cropBottom - cropTop + 1; |
| 1513 | |
| 1514 | ALOGV("Video output format changed to %d x %d " |
| 1515 | "(crop: %d x %d @ (%d, %d))", |
| 1516 | width, height, |
| 1517 | displayWidth, |
| 1518 | displayHeight, |
| 1519 | cropLeft, cropTop); |
| 1520 | } else { |
| 1521 | CHECK(inputFormat->findInt32("width", &displayWidth)); |
| 1522 | CHECK(inputFormat->findInt32("height", &displayHeight)); |
| 1523 | |
| 1524 | ALOGV("Video input format %d x %d", displayWidth, displayHeight); |
| 1525 | } |
| 1526 | |
| 1527 | // Take into account sample aspect ratio if necessary: |
| 1528 | int32_t sarWidth, sarHeight; |
| 1529 | if (inputFormat->findInt32("sar-width", &sarWidth) |
| 1530 | && inputFormat->findInt32("sar-height", &sarHeight)) { |
| 1531 | ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); |
| 1532 | |
| 1533 | displayWidth = (displayWidth * sarWidth) / sarHeight; |
| 1534 | |
| 1535 | ALOGV("display dimensions %d x %d", displayWidth, displayHeight); |
| 1536 | } |
| 1537 | |
| 1538 | int32_t rotationDegrees; |
| 1539 | if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { |
| 1540 | rotationDegrees = 0; |
| 1541 | } |
| 1542 | |
| 1543 | if (rotationDegrees == 90 || rotationDegrees == 270) { |
| 1544 | int32_t tmp = displayWidth; |
| 1545 | displayWidth = displayHeight; |
| 1546 | displayHeight = tmp; |
| 1547 | } |
| 1548 | |
| 1549 | notifyListener( |
| 1550 | MEDIA_SET_VIDEO_SIZE, |
| 1551 | displayWidth, |
| 1552 | displayHeight); |
| 1553 | } |
| 1554 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1555 | void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 1556 | if (mDriver == NULL) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1557 | return; |
| 1558 | } |
| 1559 | |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 1560 | sp<NuPlayerDriver> driver = mDriver.promote(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1561 | |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 1562 | if (driver == NULL) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1566 | driver->notifyListener(msg, ext1, ext2, in); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1569 | void NuPlayer::flushDecoder( |
| 1570 | bool audio, bool needShutdown, const sp<AMessage> &newFormat) { |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 1571 | ALOGV("[%s] flushDecoder needShutdown=%d", |
| 1572 | audio ? "audio" : "video", needShutdown); |
| 1573 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1574 | const sp<Decoder> &decoder = getDecoder(audio); |
| 1575 | if (decoder == NULL) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1576 | ALOGI("flushDecoder %s without decoder present", |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1577 | audio ? "audio" : "video"); |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1578 | return; |
Andreas Huber | 6e3d311 | 2011-11-28 12:36:11 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1581 | // Make sure we don't continue to scan sources until we finish flushing. |
| 1582 | ++mScanSourcesGeneration; |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 1583 | mScanSourcesPending = false; |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1584 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1585 | decoder->signalFlush(newFormat); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1586 | mRenderer->flush(audio); |
| 1587 | |
| 1588 | FlushStatus newStatus = |
| 1589 | needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; |
| 1590 | |
Andy Hung | 8d121d4 | 2014-10-03 09:53:53 -0700 | [diff] [blame] | 1591 | mFlushComplete[audio][false /* isDecoder */] = false; |
| 1592 | mFlushComplete[audio][true /* isDecoder */] = false; |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1593 | if (audio) { |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1594 | ALOGE_IF(mFlushingAudio != NONE, |
| 1595 | "audio flushDecoder() is called in state %d", mFlushingAudio); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1596 | mFlushingAudio = newStatus; |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1597 | } else { |
Wei Jia | 53904f3 | 2014-07-29 10:22:53 -0700 | [diff] [blame] | 1598 | ALOGE_IF(mFlushingVideo != NONE, |
| 1599 | "video flushDecoder() is called in state %d", mFlushingVideo); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1600 | mFlushingVideo = newStatus; |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1601 | |
| 1602 | if (mCCDecoder != NULL) { |
| 1603 | mCCDecoder->flush(); |
| 1604 | } |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 1608 | void NuPlayer::updateDecoderFormatWithoutFlush( |
| 1609 | bool audio, const sp<AMessage> &format) { |
| 1610 | ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video"); |
| 1611 | |
| 1612 | const sp<Decoder> &decoder = getDecoder(audio); |
| 1613 | if (decoder == NULL) { |
| 1614 | ALOGI("updateDecoderFormatWithoutFlush %s without decoder present", |
| 1615 | audio ? "audio" : "video"); |
| 1616 | return; |
| 1617 | } |
| 1618 | |
| 1619 | decoder->signalUpdateFormat(format); |
| 1620 | } |
| 1621 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1622 | void NuPlayer::queueDecoderShutdown( |
| 1623 | bool audio, bool video, const sp<AMessage> &reply) { |
| 1624 | ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1625 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1626 | mDeferredActions.push_back( |
| 1627 | new ShutdownDecoderAction(audio, video)); |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1628 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1629 | mDeferredActions.push_back( |
| 1630 | new SimpleAction(&NuPlayer::performScanSources)); |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1631 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1632 | mDeferredActions.push_back(new PostMessageAction(reply)); |
| 1633 | |
| 1634 | processDeferredActions(); |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
James Dong | 0d268a3 | 2012-08-31 12:18:27 -0700 | [diff] [blame] | 1637 | status_t NuPlayer::setVideoScalingMode(int32_t mode) { |
| 1638 | mVideoScalingMode = mode; |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 1639 | if (mNativeWindow != NULL) { |
James Dong | 0d268a3 | 2012-08-31 12:18:27 -0700 | [diff] [blame] | 1640 | status_t ret = native_window_set_scaling_mode( |
| 1641 | mNativeWindow->getNativeWindow().get(), mVideoScalingMode); |
| 1642 | if (ret != OK) { |
| 1643 | ALOGE("Failed to set scaling mode (%d): %s", |
| 1644 | -ret, strerror(-ret)); |
| 1645 | return ret; |
| 1646 | } |
| 1647 | } |
| 1648 | return OK; |
| 1649 | } |
| 1650 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1651 | status_t NuPlayer::getTrackInfo(Parcel* reply) const { |
| 1652 | sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id()); |
| 1653 | msg->setPointer("reply", reply); |
| 1654 | |
| 1655 | sp<AMessage> response; |
| 1656 | status_t err = msg->postAndAwaitResponse(&response); |
| 1657 | return err; |
| 1658 | } |
| 1659 | |
Robert Shih | 7c4f0d7 | 2014-07-09 18:53:31 -0700 | [diff] [blame] | 1660 | status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { |
| 1661 | sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id()); |
| 1662 | msg->setPointer("reply", reply); |
| 1663 | msg->setInt32("type", type); |
| 1664 | |
| 1665 | sp<AMessage> response; |
| 1666 | status_t err = msg->postAndAwaitResponse(&response); |
| 1667 | if (err == OK && response != NULL) { |
| 1668 | CHECK(response->findInt32("err", &err)); |
| 1669 | } |
| 1670 | return err; |
| 1671 | } |
| 1672 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1673 | status_t NuPlayer::selectTrack(size_t trackIndex, bool select) { |
| 1674 | sp<AMessage> msg = new AMessage(kWhatSelectTrack, id()); |
| 1675 | msg->setSize("trackIndex", trackIndex); |
| 1676 | msg->setInt32("select", select); |
| 1677 | |
| 1678 | sp<AMessage> response; |
| 1679 | status_t err = msg->postAndAwaitResponse(&response); |
| 1680 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 1681 | if (err != OK) { |
| 1682 | return err; |
| 1683 | } |
| 1684 | |
| 1685 | if (!response->findInt32("err", &err)) { |
| 1686 | err = OK; |
| 1687 | } |
| 1688 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1689 | return err; |
| 1690 | } |
| 1691 | |
Ronghua Wu | a73d9e0 | 2014-10-08 15:13:29 -0700 | [diff] [blame] | 1692 | status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { |
| 1693 | sp<Renderer> renderer = mRenderer; |
| 1694 | if (renderer == NULL) { |
| 1695 | return NO_INIT; |
| 1696 | } |
| 1697 | |
| 1698 | return renderer->getCurrentPosition(mediaUs); |
| 1699 | } |
| 1700 | |
| 1701 | void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) { |
| 1702 | *numFramesTotal = mNumFramesTotal; |
| 1703 | *numFramesDropped = mNumFramesDropped; |
| 1704 | } |
| 1705 | |
Marco Nelissen | f0b72b5 | 2014-09-16 15:43:44 -0700 | [diff] [blame] | 1706 | sp<MetaData> NuPlayer::getFileMeta() { |
| 1707 | return mSource->getFileFormatMeta(); |
| 1708 | } |
| 1709 | |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 1710 | void NuPlayer::schedulePollDuration() { |
| 1711 | sp<AMessage> msg = new AMessage(kWhatPollDuration, id()); |
| 1712 | msg->setInt32("generation", mPollDurationGeneration); |
| 1713 | msg->post(); |
| 1714 | } |
| 1715 | |
| 1716 | void NuPlayer::cancelPollDuration() { |
| 1717 | ++mPollDurationGeneration; |
| 1718 | } |
| 1719 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1720 | void NuPlayer::processDeferredActions() { |
| 1721 | while (!mDeferredActions.empty()) { |
| 1722 | // We won't execute any deferred actions until we're no longer in |
| 1723 | // an intermediate state, i.e. one more more decoders are currently |
| 1724 | // flushing or shutting down. |
| 1725 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1726 | if (mFlushingAudio != NONE || mFlushingVideo != NONE) { |
| 1727 | // We're currently flushing, postpone the reset until that's |
| 1728 | // completed. |
| 1729 | |
| 1730 | ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", |
| 1731 | mFlushingAudio, mFlushingVideo); |
| 1732 | |
| 1733 | break; |
| 1734 | } |
| 1735 | |
| 1736 | sp<Action> action = *mDeferredActions.begin(); |
| 1737 | mDeferredActions.erase(mDeferredActions.begin()); |
| 1738 | |
| 1739 | action->execute(this); |
| 1740 | } |
| 1741 | } |
| 1742 | |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 1743 | void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) { |
| 1744 | ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)", |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1745 | seekTimeUs, |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 1746 | seekTimeUs / 1E6, |
| 1747 | needNotify); |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1748 | |
Andy Hung | adf34bf | 2014-09-03 18:22:22 -0700 | [diff] [blame] | 1749 | if (mSource == NULL) { |
| 1750 | // This happens when reset occurs right before the loop mode |
| 1751 | // asynchronously seeks to the start of the stream. |
| 1752 | LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, |
| 1753 | "mSource is NULL and decoders not NULL audio(%p) video(%p)", |
| 1754 | mAudioDecoder.get(), mVideoDecoder.get()); |
| 1755 | return; |
| 1756 | } |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1757 | mSource->seekTo(seekTimeUs); |
Robert Shih | d3b0bbb | 2014-07-23 15:00:25 -0700 | [diff] [blame] | 1758 | ++mTimedTextGeneration; |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1759 | |
| 1760 | if (mDriver != NULL) { |
| 1761 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 1762 | if (driver != NULL) { |
Wei Jia | e427abf | 2014-09-22 15:21:11 -0700 | [diff] [blame] | 1763 | if (needNotify) { |
| 1764 | driver->notifySeekComplete(); |
| 1765 | } |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | // everything's flushed, continue playback. |
| 1770 | } |
| 1771 | |
| 1772 | void NuPlayer::performDecoderFlush() { |
| 1773 | ALOGV("performDecoderFlush"); |
| 1774 | |
Andreas Huber | da9740e | 2013-04-16 10:54:03 -0700 | [diff] [blame] | 1775 | if (mAudioDecoder == NULL && mVideoDecoder == NULL) { |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1776 | return; |
| 1777 | } |
| 1778 | |
| 1779 | mTimeDiscontinuityPending = true; |
| 1780 | |
| 1781 | if (mAudioDecoder != NULL) { |
| 1782 | flushDecoder(true /* audio */, false /* needShutdown */); |
| 1783 | } |
| 1784 | |
| 1785 | if (mVideoDecoder != NULL) { |
| 1786 | flushDecoder(false /* audio */, false /* needShutdown */); |
| 1787 | } |
| 1788 | } |
| 1789 | |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 1790 | void NuPlayer::performDecoderShutdown(bool audio, bool video) { |
| 1791 | ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video); |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1792 | |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 1793 | if ((!audio || mAudioDecoder == NULL) |
| 1794 | && (!video || mVideoDecoder == NULL)) { |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1795 | return; |
| 1796 | } |
| 1797 | |
| 1798 | mTimeDiscontinuityPending = true; |
| 1799 | |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 1800 | if (audio && mAudioDecoder != NULL) { |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1801 | flushDecoder(true /* audio */, true /* needShutdown */); |
| 1802 | } |
| 1803 | |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 1804 | if (video && mVideoDecoder != NULL) { |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1805 | flushDecoder(false /* audio */, true /* needShutdown */); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | void NuPlayer::performReset() { |
| 1810 | ALOGV("performReset"); |
| 1811 | |
| 1812 | CHECK(mAudioDecoder == NULL); |
| 1813 | CHECK(mVideoDecoder == NULL); |
| 1814 | |
| 1815 | cancelPollDuration(); |
| 1816 | |
| 1817 | ++mScanSourcesGeneration; |
| 1818 | mScanSourcesPending = false; |
| 1819 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 1820 | if (mRendererLooper != NULL) { |
| 1821 | if (mRenderer != NULL) { |
| 1822 | mRendererLooper->unregisterHandler(mRenderer->id()); |
| 1823 | } |
| 1824 | mRendererLooper->stop(); |
| 1825 | mRendererLooper.clear(); |
| 1826 | } |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1827 | mRenderer.clear(); |
Wei Jia | 57568df | 2014-09-22 10:16:29 -0700 | [diff] [blame] | 1828 | ++mRendererGeneration; |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1829 | |
| 1830 | if (mSource != NULL) { |
| 1831 | mSource->stop(); |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 1832 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1833 | mSource.clear(); |
| 1834 | } |
| 1835 | |
| 1836 | if (mDriver != NULL) { |
| 1837 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 1838 | if (driver != NULL) { |
| 1839 | driver->notifyResetComplete(); |
| 1840 | } |
| 1841 | } |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 1842 | |
| 1843 | mStarted = false; |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | void NuPlayer::performScanSources() { |
| 1847 | ALOGV("performScanSources"); |
| 1848 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 1849 | if (!mStarted) { |
| 1850 | return; |
| 1851 | } |
| 1852 | |
Andreas Huber | a1f8ab0 | 2012-11-30 10:53:22 -0800 | [diff] [blame] | 1853 | if (mAudioDecoder == NULL || mVideoDecoder == NULL) { |
| 1854 | postScanSources(); |
| 1855 | } |
| 1856 | } |
| 1857 | |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 1858 | void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) { |
| 1859 | ALOGV("performSetSurface"); |
| 1860 | |
| 1861 | mNativeWindow = wrapper; |
| 1862 | |
| 1863 | // XXX - ignore error from setVideoScalingMode for now |
| 1864 | setVideoScalingMode(mVideoScalingMode); |
Chong Zhang | 13d6faa | 2014-08-22 15:35:28 -0700 | [diff] [blame] | 1865 | |
| 1866 | if (mDriver != NULL) { |
| 1867 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 1868 | if (driver != NULL) { |
| 1869 | driver->notifySetSurfaceComplete(); |
| 1870 | } |
| 1871 | } |
Andreas Huber | 57a339c | 2012-12-03 11:18:00 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1874 | void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { |
| 1875 | int32_t what; |
| 1876 | CHECK(msg->findInt32("what", &what)); |
| 1877 | |
| 1878 | switch (what) { |
| 1879 | case Source::kWhatPrepared: |
| 1880 | { |
Andreas Huber | b5f28d4 | 2013-04-25 15:11:19 -0700 | [diff] [blame] | 1881 | if (mSource == NULL) { |
| 1882 | // This is a stale notification from a source that was |
| 1883 | // asynchronously preparing when the client called reset(). |
| 1884 | // We handled the reset, the source is gone. |
| 1885 | break; |
| 1886 | } |
| 1887 | |
Andreas Huber | ec0c597 | 2013-02-05 14:47:13 -0800 | [diff] [blame] | 1888 | int32_t err; |
| 1889 | CHECK(msg->findInt32("err", &err)); |
| 1890 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1891 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 1892 | if (driver != NULL) { |
Marco Nelissen | dd114d1 | 2014-05-28 15:23:14 -0700 | [diff] [blame] | 1893 | // notify duration first, so that it's definitely set when |
| 1894 | // the app received the "prepare complete" callback. |
| 1895 | int64_t durationUs; |
| 1896 | if (mSource->getDuration(&durationUs) == OK) { |
| 1897 | driver->notifyDuration(durationUs); |
| 1898 | } |
Andreas Huber | ec0c597 | 2013-02-05 14:47:13 -0800 | [diff] [blame] | 1899 | driver->notifyPrepareCompleted(err); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1900 | } |
Andreas Huber | 9975940 | 2013-04-01 14:28:31 -0700 | [diff] [blame] | 1901 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1902 | break; |
| 1903 | } |
| 1904 | |
| 1905 | case Source::kWhatFlagsChanged: |
| 1906 | { |
| 1907 | uint32_t flags; |
| 1908 | CHECK(msg->findInt32("flags", (int32_t *)&flags)); |
| 1909 | |
Chong Zhang | 4b7069d | 2013-09-11 12:52:43 -0700 | [diff] [blame] | 1910 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 1911 | if (driver != NULL) { |
| 1912 | driver->notifyFlagsChanged(flags); |
| 1913 | } |
| 1914 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1915 | if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) |
| 1916 | && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { |
| 1917 | cancelPollDuration(); |
| 1918 | } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) |
| 1919 | && (flags & Source::FLAG_DYNAMIC_DURATION) |
| 1920 | && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { |
| 1921 | schedulePollDuration(); |
| 1922 | } |
| 1923 | |
| 1924 | mSourceFlags = flags; |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | case Source::kWhatVideoSizeChanged: |
| 1929 | { |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1930 | sp<AMessage> format; |
| 1931 | CHECK(msg->findMessage("format", &format)); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1932 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 1933 | updateVideoSize(format); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 1934 | break; |
| 1935 | } |
| 1936 | |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 1937 | case Source::kWhatBufferingUpdate: |
| 1938 | { |
| 1939 | int32_t percentage; |
| 1940 | CHECK(msg->findInt32("percentage", &percentage)); |
| 1941 | |
| 1942 | notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); |
| 1943 | break; |
| 1944 | } |
| 1945 | |
Roger Jönsson | b50e83e | 2013-01-21 16:26:41 +0100 | [diff] [blame] | 1946 | case Source::kWhatBufferingStart: |
| 1947 | { |
| 1948 | notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); |
| 1949 | break; |
| 1950 | } |
| 1951 | |
| 1952 | case Source::kWhatBufferingEnd: |
| 1953 | { |
| 1954 | notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); |
| 1955 | break; |
| 1956 | } |
| 1957 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1958 | case Source::kWhatSubtitleData: |
| 1959 | { |
| 1960 | sp<ABuffer> buffer; |
| 1961 | CHECK(msg->findBuffer("buffer", &buffer)); |
| 1962 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 1963 | sendSubtitleData(buffer, 0 /* baseIndex */); |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 1964 | break; |
| 1965 | } |
| 1966 | |
Robert Shih | d3b0bbb | 2014-07-23 15:00:25 -0700 | [diff] [blame] | 1967 | case Source::kWhatTimedTextData: |
| 1968 | { |
| 1969 | int32_t generation; |
| 1970 | if (msg->findInt32("generation", &generation) |
| 1971 | && generation != mTimedTextGeneration) { |
| 1972 | break; |
| 1973 | } |
| 1974 | |
| 1975 | sp<ABuffer> buffer; |
| 1976 | CHECK(msg->findBuffer("buffer", &buffer)); |
| 1977 | |
| 1978 | sp<NuPlayerDriver> driver = mDriver.promote(); |
| 1979 | if (driver == NULL) { |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | int posMs; |
| 1984 | int64_t timeUs, posUs; |
| 1985 | driver->getCurrentPosition(&posMs); |
| 1986 | posUs = posMs * 1000; |
| 1987 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
| 1988 | |
| 1989 | if (posUs < timeUs) { |
| 1990 | if (!msg->findInt32("generation", &generation)) { |
| 1991 | msg->setInt32("generation", mTimedTextGeneration); |
| 1992 | } |
| 1993 | msg->post(timeUs - posUs); |
| 1994 | } else { |
| 1995 | sendTimedTextData(buffer); |
| 1996 | } |
| 1997 | break; |
| 1998 | } |
| 1999 | |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 2000 | case Source::kWhatQueueDecoderShutdown: |
| 2001 | { |
| 2002 | int32_t audio, video; |
| 2003 | CHECK(msg->findInt32("audio", &audio)); |
| 2004 | CHECK(msg->findInt32("video", &video)); |
| 2005 | |
| 2006 | sp<AMessage> reply; |
| 2007 | CHECK(msg->findMessage("reply", &reply)); |
| 2008 | |
| 2009 | queueDecoderShutdown(audio, video, reply); |
| 2010 | break; |
| 2011 | } |
| 2012 | |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 2013 | case Source::kWhatDrmNoLicense: |
| 2014 | { |
| 2015 | notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); |
| 2016 | break; |
| 2017 | } |
| 2018 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 2019 | default: |
| 2020 | TRESPASS(); |
| 2021 | } |
| 2022 | } |
| 2023 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 2024 | void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { |
| 2025 | int32_t what; |
| 2026 | CHECK(msg->findInt32("what", &what)); |
| 2027 | |
| 2028 | switch (what) { |
| 2029 | case NuPlayer::CCDecoder::kWhatClosedCaptionData: |
| 2030 | { |
| 2031 | sp<ABuffer> buffer; |
| 2032 | CHECK(msg->findBuffer("buffer", &buffer)); |
| 2033 | |
| 2034 | size_t inbandTracks = 0; |
| 2035 | if (mSource != NULL) { |
| 2036 | inbandTracks = mSource->getTrackCount(); |
| 2037 | } |
| 2038 | |
| 2039 | sendSubtitleData(buffer, inbandTracks); |
| 2040 | break; |
| 2041 | } |
| 2042 | |
| 2043 | case NuPlayer::CCDecoder::kWhatTrackAdded: |
| 2044 | { |
| 2045 | notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); |
| 2046 | |
| 2047 | break; |
| 2048 | } |
| 2049 | |
| 2050 | default: |
| 2051 | TRESPASS(); |
| 2052 | } |
| 2053 | |
| 2054 | |
| 2055 | } |
| 2056 | |
Chong Zhang | 404fced | 2014-06-11 14:45:31 -0700 | [diff] [blame] | 2057 | void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { |
| 2058 | int32_t trackIndex; |
| 2059 | int64_t timeUs, durationUs; |
| 2060 | CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); |
| 2061 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
| 2062 | CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); |
| 2063 | |
| 2064 | Parcel in; |
| 2065 | in.writeInt32(trackIndex + baseIndex); |
| 2066 | in.writeInt64(timeUs); |
| 2067 | in.writeInt64(durationUs); |
| 2068 | in.writeInt32(buffer->size()); |
| 2069 | in.writeInt32(buffer->size()); |
| 2070 | in.write(buffer->data(), buffer->size()); |
| 2071 | |
| 2072 | notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); |
| 2073 | } |
Robert Shih | d3b0bbb | 2014-07-23 15:00:25 -0700 | [diff] [blame] | 2074 | |
| 2075 | void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { |
| 2076 | const void *data; |
| 2077 | size_t size = 0; |
| 2078 | int64_t timeUs; |
| 2079 | int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS; |
| 2080 | |
| 2081 | AString mime; |
| 2082 | CHECK(buffer->meta()->findString("mime", &mime)); |
| 2083 | CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); |
| 2084 | |
| 2085 | data = buffer->data(); |
| 2086 | size = buffer->size(); |
| 2087 | |
| 2088 | Parcel parcel; |
| 2089 | if (size > 0) { |
| 2090 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
| 2091 | flag |= TextDescriptions::IN_BAND_TEXT_3GPP; |
| 2092 | TextDescriptions::getParcelOfDescriptions( |
| 2093 | (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); |
| 2094 | } |
| 2095 | |
| 2096 | if ((parcel.dataSize() > 0)) { |
| 2097 | notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); |
| 2098 | } else { // send an empty timed text |
| 2099 | notifyListener(MEDIA_TIMED_TEXT, 0, 0); |
| 2100 | } |
| 2101 | } |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 2102 | //////////////////////////////////////////////////////////////////////////////// |
| 2103 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 2104 | sp<AMessage> NuPlayer::Source::getFormat(bool audio) { |
| 2105 | sp<MetaData> meta = getFormatMeta(audio); |
| 2106 | |
| 2107 | if (meta == NULL) { |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | |
| 2111 | sp<AMessage> msg = new AMessage; |
| 2112 | |
| 2113 | if(convertMetaDataToMessage(meta, &msg) == OK) { |
| 2114 | return msg; |
| 2115 | } |
| 2116 | return NULL; |
| 2117 | } |
| 2118 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 2119 | void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { |
| 2120 | sp<AMessage> notify = dupNotify(); |
| 2121 | notify->setInt32("what", kWhatFlagsChanged); |
| 2122 | notify->setInt32("flags", flags); |
| 2123 | notify->post(); |
| 2124 | } |
| 2125 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 2126 | void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 2127 | sp<AMessage> notify = dupNotify(); |
| 2128 | notify->setInt32("what", kWhatVideoSizeChanged); |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 2129 | notify->setMessage("format", format); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 2130 | notify->post(); |
| 2131 | } |
| 2132 | |
Andreas Huber | ec0c597 | 2013-02-05 14:47:13 -0800 | [diff] [blame] | 2133 | void NuPlayer::Source::notifyPrepared(status_t err) { |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 2134 | sp<AMessage> notify = dupNotify(); |
| 2135 | notify->setInt32("what", kWhatPrepared); |
Andreas Huber | ec0c597 | 2013-02-05 14:47:13 -0800 | [diff] [blame] | 2136 | notify->setInt32("err", err); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 2137 | notify->post(); |
| 2138 | } |
| 2139 | |
Andreas Huber | 84333e0 | 2014-02-07 15:36:10 -0800 | [diff] [blame] | 2140 | void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 2141 | TRESPASS(); |
| 2142 | } |
| 2143 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 2144 | } // namespace android |