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