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