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