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