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