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 "NuPlayerDriver"
19 #include <inttypes.h>
20 #include <android-base/macros.h>
21 #include <utils/Log.h>
22 #include <cutils/properties.h>
23
24 #include "NuPlayerDriver.h"
25
26 #include "NuPlayer.h"
27 #include "NuPlayerSource.h"
28
29 #include <media/stagefright/foundation/ADebug.h>
30 #include <media/stagefright/foundation/ALooper.h>
31 #include <media/stagefright/foundation/AUtils.h>
32 #include <media/stagefright/foundation/ByteUtils.h>
33 #include <media/stagefright/MediaClock.h>
34 #include <media/stagefright/MetaData.h>
35 #include <media/stagefright/Utils.h>
36 #include <media/stagefright/FoundationUtils.h>
37
38 #include <media/IMediaAnalyticsService.h>
39
40 static const int kDumpLockRetries = 50;
41 static const int kDumpLockSleepUs = 20000;
42
43 namespace android {
44
45 // key for media statistics
46 static const char *kKeyPlayer = "nuplayer";
47 // attrs for media statistics
48 // NB: these are matched with public Java API constants defined
49 // in frameworks/base/media/java/android/media/MediaPlayer.java
50 // These must be kept synchronized with the constants there.
51 static const char *kPlayerVMime = "android.media.mediaplayer.video.mime";
52 static const char *kPlayerVCodec = "android.media.mediaplayer.video.codec";
53 static const char *kPlayerWidth = "android.media.mediaplayer.width";
54 static const char *kPlayerHeight = "android.media.mediaplayer.height";
55 static const char *kPlayerFrames = "android.media.mediaplayer.frames";
56 static const char *kPlayerFramesDropped = "android.media.mediaplayer.dropped";
57 static const char *kPlayerFrameRate = "android.media.mediaplayer.fps";
58 static const char *kPlayerAMime = "android.media.mediaplayer.audio.mime";
59 static const char *kPlayerACodec = "android.media.mediaplayer.audio.codec";
60 static const char *kPlayerDuration = "android.media.mediaplayer.durationMs";
61 static const char *kPlayerPlaying = "android.media.mediaplayer.playingMs";
62 static const char *kPlayerError = "android.media.mediaplayer.err";
63 static const char *kPlayerErrorCode = "android.media.mediaplayer.errcode";
64
65 // NB: These are not yet exposed as public Java API constants.
66 static const char *kPlayerErrorState = "android.media.mediaplayer.errstate";
67 static const char *kPlayerDataSourceType = "android.media.mediaplayer.dataSource";
68 //
69 static const char *kPlayerRebuffering = "android.media.mediaplayer.rebufferingMs";
70 static const char *kPlayerRebufferingCount = "android.media.mediaplayer.rebuffers";
71 static const char *kPlayerRebufferingAtExit = "android.media.mediaplayer.rebufferExit";
72
73
NuPlayerDriver(pid_t pid)74 NuPlayerDriver::NuPlayerDriver(pid_t pid)
75 : mState(STATE_IDLE),
76 mIsAsyncPrepare(false),
77 mAsyncResult(UNKNOWN_ERROR),
78 mSetSurfaceInProgress(false),
79 mDurationUs(-1),
80 mPositionUs(-1),
81 mSeekInProgress(false),
82 mPlayingTimeUs(0),
83 mRebufferingTimeUs(0),
84 mRebufferingEvents(0),
85 mRebufferingAtExit(false),
86 mLooper(new ALooper),
87 mMediaClock(new MediaClock),
88 mPlayer(new NuPlayer(pid, mMediaClock)),
89 mPlayerFlags(0),
90 mAnalyticsItem(NULL),
91 mClientUid(-1),
92 mAtEOS(false),
93 mLooping(false),
94 mAutoLoop(false) {
95 ALOGD("NuPlayerDriver(%p) created, clientPid(%d)", this, pid);
96 mLooper->setName("NuPlayerDriver Looper");
97
98 mMediaClock->init();
99
100 // set up an analytics record
101 mAnalyticsItem = MediaAnalyticsItem::create(kKeyPlayer);
102
103 mLooper->start(
104 false, /* runOnCallingThread */
105 true, /* canCallJava */
106 PRIORITY_AUDIO);
107
108 mLooper->registerHandler(mPlayer);
109
110 mPlayer->init(this);
111 }
112
~NuPlayerDriver()113 NuPlayerDriver::~NuPlayerDriver() {
114 ALOGV("~NuPlayerDriver(%p)", this);
115 mLooper->stop();
116
117 // finalize any pending metrics, usually a no-op.
118 updateMetrics("destructor");
119 logMetrics("destructor");
120
121 if (mAnalyticsItem != NULL) {
122 delete mAnalyticsItem;
123 mAnalyticsItem = NULL;
124 }
125 }
126
initCheck()127 status_t NuPlayerDriver::initCheck() {
128 return OK;
129 }
130
setUID(uid_t uid)131 status_t NuPlayerDriver::setUID(uid_t uid) {
132 mPlayer->setUID(uid);
133 mClientUid = uid;
134 if (mAnalyticsItem) {
135 mAnalyticsItem->setUid(mClientUid);
136 }
137
138 return OK;
139 }
140
setDataSource(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)141 status_t NuPlayerDriver::setDataSource(
142 const sp<IMediaHTTPService> &httpService,
143 const char *url,
144 const KeyedVector<String8, String8> *headers) {
145 ALOGV("setDataSource(%p) url(%s)", this, uriDebugString(url, false).c_str());
146 Mutex::Autolock autoLock(mLock);
147
148 if (mState != STATE_IDLE) {
149 return INVALID_OPERATION;
150 }
151
152 mState = STATE_SET_DATASOURCE_PENDING;
153
154 mPlayer->setDataSourceAsync(httpService, url, headers);
155
156 while (mState == STATE_SET_DATASOURCE_PENDING) {
157 mCondition.wait(mLock);
158 }
159
160 return mAsyncResult;
161 }
162
setDataSource(int fd,int64_t offset,int64_t length)163 status_t NuPlayerDriver::setDataSource(int fd, int64_t offset, int64_t length) {
164 ALOGV("setDataSource(%p) file(%d)", this, fd);
165 Mutex::Autolock autoLock(mLock);
166
167 if (mState != STATE_IDLE) {
168 return INVALID_OPERATION;
169 }
170
171 mState = STATE_SET_DATASOURCE_PENDING;
172
173 mPlayer->setDataSourceAsync(fd, offset, length);
174
175 while (mState == STATE_SET_DATASOURCE_PENDING) {
176 mCondition.wait(mLock);
177 }
178
179 return mAsyncResult;
180 }
181
setDataSource(const sp<IStreamSource> & source)182 status_t NuPlayerDriver::setDataSource(const sp<IStreamSource> &source) {
183 ALOGV("setDataSource(%p) stream source", this);
184 Mutex::Autolock autoLock(mLock);
185
186 if (mState != STATE_IDLE) {
187 return INVALID_OPERATION;
188 }
189
190 mState = STATE_SET_DATASOURCE_PENDING;
191
192 mPlayer->setDataSourceAsync(source);
193
194 while (mState == STATE_SET_DATASOURCE_PENDING) {
195 mCondition.wait(mLock);
196 }
197
198 return mAsyncResult;
199 }
200
setDataSource(const sp<DataSource> & source)201 status_t NuPlayerDriver::setDataSource(const sp<DataSource> &source) {
202 ALOGV("setDataSource(%p) callback source", this);
203 Mutex::Autolock autoLock(mLock);
204
205 if (mState != STATE_IDLE) {
206 return INVALID_OPERATION;
207 }
208
209 mState = STATE_SET_DATASOURCE_PENDING;
210
211 mPlayer->setDataSourceAsync(source);
212
213 while (mState == STATE_SET_DATASOURCE_PENDING) {
214 mCondition.wait(mLock);
215 }
216
217 return mAsyncResult;
218 }
219
setVideoSurfaceTexture(const sp<IGraphicBufferProducer> & bufferProducer)220 status_t NuPlayerDriver::setVideoSurfaceTexture(
221 const sp<IGraphicBufferProducer> &bufferProducer) {
222 ALOGV("setVideoSurfaceTexture(%p)", this);
223 Mutex::Autolock autoLock(mLock);
224
225 if (mSetSurfaceInProgress) {
226 return INVALID_OPERATION;
227 }
228
229 switch (mState) {
230 case STATE_SET_DATASOURCE_PENDING:
231 case STATE_RESET_IN_PROGRESS:
232 return INVALID_OPERATION;
233
234 default:
235 break;
236 }
237
238 mSetSurfaceInProgress = true;
239
240 mPlayer->setVideoSurfaceTextureAsync(bufferProducer);
241
242 while (mSetSurfaceInProgress) {
243 mCondition.wait(mLock);
244 }
245
246 return OK;
247 }
248
getBufferingSettings(BufferingSettings * buffering)249 status_t NuPlayerDriver::getBufferingSettings(BufferingSettings* buffering) {
250 ALOGV("getBufferingSettings(%p)", this);
251 {
252 Mutex::Autolock autoLock(mLock);
253 if (mState == STATE_IDLE) {
254 return INVALID_OPERATION;
255 }
256 }
257
258 return mPlayer->getBufferingSettings(buffering);
259 }
260
setBufferingSettings(const BufferingSettings & buffering)261 status_t NuPlayerDriver::setBufferingSettings(const BufferingSettings& buffering) {
262 ALOGV("setBufferingSettings(%p)", this);
263 {
264 Mutex::Autolock autoLock(mLock);
265 if (mState == STATE_IDLE) {
266 return INVALID_OPERATION;
267 }
268 }
269
270 return mPlayer->setBufferingSettings(buffering);
271 }
272
prepare()273 status_t NuPlayerDriver::prepare() {
274 ALOGV("prepare(%p)", this);
275 Mutex::Autolock autoLock(mLock);
276 return prepare_l();
277 }
278
prepare_l()279 status_t NuPlayerDriver::prepare_l() {
280 switch (mState) {
281 case STATE_UNPREPARED:
282 mState = STATE_PREPARING;
283
284 // Make sure we're not posting any notifications, success or
285 // failure information is only communicated through our result
286 // code.
287 mIsAsyncPrepare = false;
288 mPlayer->prepareAsync();
289 while (mState == STATE_PREPARING) {
290 mCondition.wait(mLock);
291 }
292 return (mState == STATE_PREPARED) ? OK : UNKNOWN_ERROR;
293 case STATE_STOPPED:
294 // this is really just paused. handle as seek to start
295 mAtEOS = false;
296 mState = STATE_STOPPED_AND_PREPARING;
297 mIsAsyncPrepare = false;
298 mPlayer->seekToAsync(0, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */,
299 true /* needNotify */);
300 while (mState == STATE_STOPPED_AND_PREPARING) {
301 mCondition.wait(mLock);
302 }
303 return (mState == STATE_STOPPED_AND_PREPARED) ? OK : UNKNOWN_ERROR;
304 default:
305 return INVALID_OPERATION;
306 };
307 }
308
prepareAsync()309 status_t NuPlayerDriver::prepareAsync() {
310 ALOGV("prepareAsync(%p)", this);
311 Mutex::Autolock autoLock(mLock);
312
313 switch (mState) {
314 case STATE_UNPREPARED:
315 mState = STATE_PREPARING;
316 mIsAsyncPrepare = true;
317 mPlayer->prepareAsync();
318 return OK;
319 case STATE_STOPPED:
320 // this is really just paused. handle as seek to start
321 mAtEOS = false;
322 mState = STATE_STOPPED_AND_PREPARING;
323 mIsAsyncPrepare = true;
324 mPlayer->seekToAsync(0, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */,
325 true /* needNotify */);
326 return OK;
327 default:
328 return INVALID_OPERATION;
329 };
330 }
331
start()332 status_t NuPlayerDriver::start() {
333 ALOGV("start(%p), state is %d, eos is %d", this, mState, mAtEOS);
334 Mutex::Autolock autoLock(mLock);
335 return start_l();
336 }
337
start_l()338 status_t NuPlayerDriver::start_l() {
339 switch (mState) {
340 case STATE_UNPREPARED:
341 {
342 status_t err = prepare_l();
343
344 if (err != OK) {
345 return err;
346 }
347
348 CHECK_EQ(mState, STATE_PREPARED);
349
350 FALLTHROUGH_INTENDED;
351 }
352
353 case STATE_PAUSED:
354 case STATE_STOPPED_AND_PREPARED:
355 case STATE_PREPARED:
356 {
357 mPlayer->start();
358
359 FALLTHROUGH_INTENDED;
360 }
361
362 case STATE_RUNNING:
363 {
364 if (mAtEOS) {
365 mPlayer->seekToAsync(0);
366 mAtEOS = false;
367 mPositionUs = -1;
368 }
369 break;
370 }
371
372 default:
373 return INVALID_OPERATION;
374 }
375
376 mState = STATE_RUNNING;
377
378 return OK;
379 }
380
stop()381 status_t NuPlayerDriver::stop() {
382 ALOGD("stop(%p)", this);
383 Mutex::Autolock autoLock(mLock);
384
385 switch (mState) {
386 case STATE_RUNNING:
387 mPlayer->pause();
388 FALLTHROUGH_INTENDED;
389
390 case STATE_PAUSED:
391 mState = STATE_STOPPED;
392 notifyListener_l(MEDIA_STOPPED);
393 break;
394
395 case STATE_PREPARED:
396 case STATE_STOPPED:
397 case STATE_STOPPED_AND_PREPARING:
398 case STATE_STOPPED_AND_PREPARED:
399 mState = STATE_STOPPED;
400 break;
401
402 default:
403 return INVALID_OPERATION;
404 }
405
406 return OK;
407 }
408
pause()409 status_t NuPlayerDriver::pause() {
410 ALOGD("pause(%p)", this);
411 // The NuPlayerRenderer may get flushed if pause for long enough, e.g. the pause timeout tear
412 // down for audio offload mode. If that happens, the NuPlayerRenderer will no longer know the
413 // current position. So similar to seekTo, update |mPositionUs| to the pause position by calling
414 // getCurrentPosition here.
415 int unused;
416 getCurrentPosition(&unused);
417
418 Mutex::Autolock autoLock(mLock);
419
420 switch (mState) {
421 case STATE_PAUSED:
422 case STATE_PREPARED:
423 return OK;
424
425 case STATE_RUNNING:
426 mState = STATE_PAUSED;
427 notifyListener_l(MEDIA_PAUSED);
428 mPlayer->pause();
429 break;
430
431 default:
432 return INVALID_OPERATION;
433 }
434
435 return OK;
436 }
437
isPlaying()438 bool NuPlayerDriver::isPlaying() {
439 return mState == STATE_RUNNING && !mAtEOS;
440 }
441
setPlaybackSettings(const AudioPlaybackRate & rate)442 status_t NuPlayerDriver::setPlaybackSettings(const AudioPlaybackRate &rate) {
443 status_t err = mPlayer->setPlaybackSettings(rate);
444 if (err == OK) {
445 // try to update position
446 int unused;
447 getCurrentPosition(&unused);
448 Mutex::Autolock autoLock(mLock);
449 if (rate.mSpeed == 0.f && mState == STATE_RUNNING) {
450 mState = STATE_PAUSED;
451 notifyListener_l(MEDIA_PAUSED);
452 } else if (rate.mSpeed != 0.f
453 && (mState == STATE_PAUSED
454 || mState == STATE_STOPPED_AND_PREPARED
455 || mState == STATE_PREPARED)) {
456 err = start_l();
457 }
458 }
459 return err;
460 }
461
getPlaybackSettings(AudioPlaybackRate * rate)462 status_t NuPlayerDriver::getPlaybackSettings(AudioPlaybackRate *rate) {
463 return mPlayer->getPlaybackSettings(rate);
464 }
465
setSyncSettings(const AVSyncSettings & sync,float videoFpsHint)466 status_t NuPlayerDriver::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
467 return mPlayer->setSyncSettings(sync, videoFpsHint);
468 }
469
getSyncSettings(AVSyncSettings * sync,float * videoFps)470 status_t NuPlayerDriver::getSyncSettings(AVSyncSettings *sync, float *videoFps) {
471 return mPlayer->getSyncSettings(sync, videoFps);
472 }
473
seekTo(int msec,MediaPlayerSeekMode mode)474 status_t NuPlayerDriver::seekTo(int msec, MediaPlayerSeekMode mode) {
475 ALOGV("seekTo(%p) (%d ms, %d) at state %d", this, msec, mode, mState);
476 Mutex::Autolock autoLock(mLock);
477
478 int64_t seekTimeUs = msec * 1000LL;
479
480 switch (mState) {
481 case STATE_PREPARED:
482 case STATE_STOPPED_AND_PREPARED:
483 case STATE_PAUSED:
484 case STATE_RUNNING:
485 {
486 mAtEOS = false;
487 mSeekInProgress = true;
488 // seeks can take a while, so we essentially paused
489 notifyListener_l(MEDIA_PAUSED);
490 mPlayer->seekToAsync(seekTimeUs, mode, true /* needNotify */);
491 break;
492 }
493
494 default:
495 return INVALID_OPERATION;
496 }
497
498 mPositionUs = seekTimeUs;
499 return OK;
500 }
501
getCurrentPosition(int * msec)502 status_t NuPlayerDriver::getCurrentPosition(int *msec) {
503 int64_t tempUs = 0;
504 {
505 Mutex::Autolock autoLock(mLock);
506 if (mSeekInProgress || (mState == STATE_PAUSED && !mAtEOS)) {
507 tempUs = (mPositionUs <= 0) ? 0 : mPositionUs;
508 *msec = (int)divRound(tempUs, (int64_t)(1000));
509 return OK;
510 }
511 }
512
513 status_t ret = mPlayer->getCurrentPosition(&tempUs);
514
515 Mutex::Autolock autoLock(mLock);
516 // We need to check mSeekInProgress here because mPlayer->seekToAsync is an async call, which
517 // means getCurrentPosition can be called before seek is completed. Iow, renderer may return a
518 // position value that's different the seek to position.
519 if (ret != OK) {
520 tempUs = (mPositionUs <= 0) ? 0 : mPositionUs;
521 } else {
522 mPositionUs = tempUs;
523 }
524 *msec = (int)divRound(tempUs, (int64_t)(1000));
525 return OK;
526 }
527
getDuration(int * msec)528 status_t NuPlayerDriver::getDuration(int *msec) {
529 Mutex::Autolock autoLock(mLock);
530
531 if (mDurationUs < 0) {
532 return UNKNOWN_ERROR;
533 }
534
535 *msec = (mDurationUs + 500LL) / 1000;
536
537 return OK;
538 }
539
updateMetrics(const char * where)540 void NuPlayerDriver::updateMetrics(const char *where) {
541
542 if (where == NULL) {
543 where = "unknown";
544 }
545 ALOGV("updateMetrics(%p) from %s at state %d", this, where, mState);
546
547 // gather the final stats for this record
548 Vector<sp<AMessage>> trackStats;
549 mPlayer->getStats(&trackStats);
550
551 if (trackStats.size() > 0) {
552 for (size_t i = 0; i < trackStats.size(); ++i) {
553 const sp<AMessage> &stats = trackStats.itemAt(i);
554
555 AString mime;
556 stats->findString("mime", &mime);
557
558 AString name;
559 stats->findString("component-name", &name);
560
561 if (mime.startsWith("video/")) {
562 int32_t width, height;
563 mAnalyticsItem->setCString(kPlayerVMime, mime.c_str());
564 if (!name.empty()) {
565 mAnalyticsItem->setCString(kPlayerVCodec, name.c_str());
566 }
567
568 if (stats->findInt32("width", &width)
569 && stats->findInt32("height", &height)) {
570 mAnalyticsItem->setInt32(kPlayerWidth, width);
571 mAnalyticsItem->setInt32(kPlayerHeight, height);
572 }
573
574 int64_t numFramesTotal = 0;
575 int64_t numFramesDropped = 0;
576 stats->findInt64("frames-total", &numFramesTotal);
577 stats->findInt64("frames-dropped-output", &numFramesDropped);
578
579 mAnalyticsItem->setInt64(kPlayerFrames, numFramesTotal);
580 mAnalyticsItem->setInt64(kPlayerFramesDropped, numFramesDropped);
581
582 float frameRate = 0;
583 if (stats->findFloat("frame-rate-total", &frameRate)) {
584 mAnalyticsItem->setDouble(kPlayerFrameRate, (double) frameRate);
585 }
586
587 } else if (mime.startsWith("audio/")) {
588 mAnalyticsItem->setCString(kPlayerAMime, mime.c_str());
589 if (!name.empty()) {
590 mAnalyticsItem->setCString(kPlayerACodec, name.c_str());
591 }
592 }
593 }
594 }
595
596 // always provide duration and playing time, even if they have 0/unknown values.
597
598 // getDuration() uses mLock for mutex -- careful where we use it.
599 int duration_ms = -1;
600 getDuration(&duration_ms);
601 mAnalyticsItem->setInt64(kPlayerDuration, duration_ms);
602
603 mPlayer->updateInternalTimers();
604
605 mAnalyticsItem->setInt64(kPlayerPlaying, (mPlayingTimeUs+500)/1000 );
606
607 if (mRebufferingEvents != 0) {
608 mAnalyticsItem->setInt64(kPlayerRebuffering, (mRebufferingTimeUs+500)/1000 );
609 mAnalyticsItem->setInt32(kPlayerRebufferingCount, mRebufferingEvents);
610 mAnalyticsItem->setInt32(kPlayerRebufferingAtExit, mRebufferingAtExit);
611
612 }
613
614 mAnalyticsItem->setCString(kPlayerDataSourceType, mPlayer->getDataSourceType());
615 }
616
617
logMetrics(const char * where)618 void NuPlayerDriver::logMetrics(const char *where) {
619 if (where == NULL) {
620 where = "unknown";
621 }
622 ALOGV("logMetrics(%p) from %s at state %d", this, where, mState);
623
624 if (mAnalyticsItem == NULL || mAnalyticsItem->isEnabled() == false) {
625 return;
626 }
627
628 // log only non-empty records
629 // we always updateMetrics() before we get here
630 // and that always injects 3 fields (duration, playing time, and
631 // datasource) into the record.
632 // So the canonical "empty" record has 3 elements in it.
633 if (mAnalyticsItem->count() > 3) {
634
635 mAnalyticsItem->selfrecord();
636
637 // re-init in case we prepare() and start() again.
638 delete mAnalyticsItem ;
639 mAnalyticsItem = MediaAnalyticsItem::create(kKeyPlayer);
640 if (mAnalyticsItem) {
641 mAnalyticsItem->setUid(mClientUid);
642 }
643 } else {
644 ALOGV("nothing to record (only %d fields)", mAnalyticsItem->count());
645 }
646 }
647
reset()648 status_t NuPlayerDriver::reset() {
649 ALOGD("reset(%p) at state %d", this, mState);
650
651 updateMetrics("reset");
652 logMetrics("reset");
653
654 Mutex::Autolock autoLock(mLock);
655
656 switch (mState) {
657 case STATE_IDLE:
658 return OK;
659
660 case STATE_SET_DATASOURCE_PENDING:
661 case STATE_RESET_IN_PROGRESS:
662 return INVALID_OPERATION;
663
664 case STATE_PREPARING:
665 {
666 CHECK(mIsAsyncPrepare);
667
668 notifyListener_l(MEDIA_PREPARED);
669 break;
670 }
671
672 default:
673 break;
674 }
675
676 if (mState != STATE_STOPPED) {
677 notifyListener_l(MEDIA_STOPPED);
678 }
679
680 if (property_get_bool("persist.debug.sf.stats", false)) {
681 Vector<String16> args;
682 dump(-1, args);
683 }
684
685 mState = STATE_RESET_IN_PROGRESS;
686 mPlayer->resetAsync();
687
688 while (mState == STATE_RESET_IN_PROGRESS) {
689 mCondition.wait(mLock);
690 }
691
692 mDurationUs = -1;
693 mPositionUs = -1;
694 mLooping = false;
695 mPlayingTimeUs = 0;
696 mRebufferingTimeUs = 0;
697 mRebufferingEvents = 0;
698 mRebufferingAtExit = false;
699
700 return OK;
701 }
702
notifyAt(int64_t mediaTimeUs)703 status_t NuPlayerDriver::notifyAt(int64_t mediaTimeUs) {
704 ALOGV("notifyAt(%p), time:%lld", this, (long long)mediaTimeUs);
705 return mPlayer->notifyAt(mediaTimeUs);
706 }
707
setLooping(int loop)708 status_t NuPlayerDriver::setLooping(int loop) {
709 mLooping = loop != 0;
710 return OK;
711 }
712
playerType()713 player_type NuPlayerDriver::playerType() {
714 return NU_PLAYER;
715 }
716
invoke(const Parcel & request,Parcel * reply)717 status_t NuPlayerDriver::invoke(const Parcel &request, Parcel *reply) {
718 if (reply == NULL) {
719 ALOGE("reply is a NULL pointer");
720 return BAD_VALUE;
721 }
722
723 int32_t methodId;
724 status_t ret = request.readInt32(&methodId);
725 if (ret != OK) {
726 ALOGE("Failed to retrieve the requested method to invoke");
727 return ret;
728 }
729
730 switch (methodId) {
731 case INVOKE_ID_SET_VIDEO_SCALING_MODE:
732 {
733 int mode = request.readInt32();
734 return mPlayer->setVideoScalingMode(mode);
735 }
736
737 case INVOKE_ID_GET_TRACK_INFO:
738 {
739 return mPlayer->getTrackInfo(reply);
740 }
741
742 case INVOKE_ID_SELECT_TRACK:
743 {
744 int trackIndex = request.readInt32();
745 int msec = 0;
746 // getCurrentPosition should always return OK
747 getCurrentPosition(&msec);
748 return mPlayer->selectTrack(trackIndex, true /* select */, msec * 1000LL);
749 }
750
751 case INVOKE_ID_UNSELECT_TRACK:
752 {
753 int trackIndex = request.readInt32();
754 return mPlayer->selectTrack(trackIndex, false /* select */, 0xdeadbeef /* not used */);
755 }
756
757 case INVOKE_ID_GET_SELECTED_TRACK:
758 {
759 int32_t type = request.readInt32();
760 return mPlayer->getSelectedTrack(type, reply);
761 }
762
763 default:
764 {
765 return INVALID_OPERATION;
766 }
767 }
768 }
769
setAudioSink(const sp<AudioSink> & audioSink)770 void NuPlayerDriver::setAudioSink(const sp<AudioSink> &audioSink) {
771 mPlayer->setAudioSink(audioSink);
772 mAudioSink = audioSink;
773 }
774
setParameter(int,const Parcel &)775 status_t NuPlayerDriver::setParameter(
776 int /* key */, const Parcel & /* request */) {
777 return INVALID_OPERATION;
778 }
779
getParameter(int key,Parcel * reply)780 status_t NuPlayerDriver::getParameter(int key, Parcel *reply) {
781
782 if (key == FOURCC('m','t','r','X') && mAnalyticsItem != NULL) {
783 // mtrX -- a play on 'metrics' (not matrix)
784 // gather current info all together, parcel it, and send it back
785 updateMetrics("api");
786 mAnalyticsItem->writeToParcel(reply);
787 return OK;
788 }
789
790 return INVALID_OPERATION;
791 }
792
getMetadata(const media::Metadata::Filter &,Parcel * records)793 status_t NuPlayerDriver::getMetadata(
794 const media::Metadata::Filter& /* ids */, Parcel *records) {
795 Mutex::Autolock autoLock(mLock);
796
797 using media::Metadata;
798
799 Metadata meta(records);
800
801 meta.appendBool(
802 Metadata::kPauseAvailable,
803 mPlayerFlags & NuPlayer::Source::FLAG_CAN_PAUSE);
804
805 meta.appendBool(
806 Metadata::kSeekBackwardAvailable,
807 mPlayerFlags & NuPlayer::Source::FLAG_CAN_SEEK_BACKWARD);
808
809 meta.appendBool(
810 Metadata::kSeekForwardAvailable,
811 mPlayerFlags & NuPlayer::Source::FLAG_CAN_SEEK_FORWARD);
812
813 meta.appendBool(
814 Metadata::kSeekAvailable,
815 mPlayerFlags & NuPlayer::Source::FLAG_CAN_SEEK);
816
817 return OK;
818 }
819
notifyResetComplete()820 void NuPlayerDriver::notifyResetComplete() {
821 ALOGD("notifyResetComplete(%p)", this);
822 Mutex::Autolock autoLock(mLock);
823
824 CHECK_EQ(mState, STATE_RESET_IN_PROGRESS);
825 mState = STATE_IDLE;
826 mCondition.broadcast();
827 }
828
notifySetSurfaceComplete()829 void NuPlayerDriver::notifySetSurfaceComplete() {
830 ALOGV("notifySetSurfaceComplete(%p)", this);
831 Mutex::Autolock autoLock(mLock);
832
833 CHECK(mSetSurfaceInProgress);
834 mSetSurfaceInProgress = false;
835
836 mCondition.broadcast();
837 }
838
notifyDuration(int64_t durationUs)839 void NuPlayerDriver::notifyDuration(int64_t durationUs) {
840 Mutex::Autolock autoLock(mLock);
841 mDurationUs = durationUs;
842 }
843
notifyMorePlayingTimeUs(int64_t playingUs)844 void NuPlayerDriver::notifyMorePlayingTimeUs(int64_t playingUs) {
845 Mutex::Autolock autoLock(mLock);
846 mPlayingTimeUs += playingUs;
847 }
848
notifyMoreRebufferingTimeUs(int64_t rebufferingUs)849 void NuPlayerDriver::notifyMoreRebufferingTimeUs(int64_t rebufferingUs) {
850 Mutex::Autolock autoLock(mLock);
851 mRebufferingTimeUs += rebufferingUs;
852 mRebufferingEvents++;
853 }
854
notifyRebufferingWhenExit(bool status)855 void NuPlayerDriver::notifyRebufferingWhenExit(bool status) {
856 Mutex::Autolock autoLock(mLock);
857 mRebufferingAtExit = status;
858 }
859
notifySeekComplete()860 void NuPlayerDriver::notifySeekComplete() {
861 ALOGV("notifySeekComplete(%p)", this);
862 Mutex::Autolock autoLock(mLock);
863 mSeekInProgress = false;
864 notifySeekComplete_l();
865 }
866
notifySeekComplete_l()867 void NuPlayerDriver::notifySeekComplete_l() {
868 bool wasSeeking = true;
869 if (mState == STATE_STOPPED_AND_PREPARING) {
870 wasSeeking = false;
871 mState = STATE_STOPPED_AND_PREPARED;
872 mCondition.broadcast();
873 if (!mIsAsyncPrepare) {
874 // if we are preparing synchronously, no need to notify listener
875 return;
876 }
877 } else if (mState == STATE_STOPPED) {
878 // no need to notify listener
879 return;
880 }
881 notifyListener_l(wasSeeking ? MEDIA_SEEK_COMPLETE : MEDIA_PREPARED);
882 }
883
dump(int fd,const Vector<String16> &) const884 status_t NuPlayerDriver::dump(
885 int fd, const Vector<String16> & /* args */) const {
886
887 Vector<sp<AMessage> > trackStats;
888 mPlayer->getStats(&trackStats);
889
890 AString logString(" NuPlayer\n");
891 char buf[256] = {0};
892
893 bool locked = false;
894 for (int i = 0; i < kDumpLockRetries; ++i) {
895 if (mLock.tryLock() == NO_ERROR) {
896 locked = true;
897 break;
898 }
899 usleep(kDumpLockSleepUs);
900 }
901
902 if (locked) {
903 snprintf(buf, sizeof(buf), " state(%d), atEOS(%d), looping(%d), autoLoop(%d)\n",
904 mState, mAtEOS, mLooping, mAutoLoop);
905 mLock.unlock();
906 } else {
907 snprintf(buf, sizeof(buf), " NPD(%p) lock is taken\n", this);
908 }
909 logString.append(buf);
910
911 for (size_t i = 0; i < trackStats.size(); ++i) {
912 const sp<AMessage> &stats = trackStats.itemAt(i);
913
914 AString mime;
915 if (stats->findString("mime", &mime)) {
916 snprintf(buf, sizeof(buf), " mime(%s)\n", mime.c_str());
917 logString.append(buf);
918 }
919
920 AString name;
921 if (stats->findString("component-name", &name)) {
922 snprintf(buf, sizeof(buf), " decoder(%s)\n", name.c_str());
923 logString.append(buf);
924 }
925
926 if (mime.startsWith("video/")) {
927 int32_t width, height;
928 if (stats->findInt32("width", &width)
929 && stats->findInt32("height", &height)) {
930 snprintf(buf, sizeof(buf), " resolution(%d x %d)\n", width, height);
931 logString.append(buf);
932 }
933
934 int64_t numFramesTotal = 0;
935 int64_t numFramesDropped = 0;
936
937 stats->findInt64("frames-total", &numFramesTotal);
938 stats->findInt64("frames-dropped-output", &numFramesDropped);
939 snprintf(buf, sizeof(buf), " numFramesTotal(%lld), numFramesDropped(%lld), "
940 "percentageDropped(%.2f%%)\n",
941 (long long)numFramesTotal,
942 (long long)numFramesDropped,
943 numFramesTotal == 0
944 ? 0.0 : (double)(numFramesDropped * 100) / numFramesTotal);
945 logString.append(buf);
946 }
947 }
948
949 ALOGI("%s", logString.c_str());
950
951 if (fd >= 0) {
952 FILE *out = fdopen(dup(fd), "w");
953 fprintf(out, "%s", logString.c_str());
954 fclose(out);
955 out = NULL;
956 }
957
958 return OK;
959 }
960
notifyListener(int msg,int ext1,int ext2,const Parcel * in)961 void NuPlayerDriver::notifyListener(
962 int msg, int ext1, int ext2, const Parcel *in) {
963 Mutex::Autolock autoLock(mLock);
964 notifyListener_l(msg, ext1, ext2, in);
965 }
966
notifyListener_l(int msg,int ext1,int ext2,const Parcel * in)967 void NuPlayerDriver::notifyListener_l(
968 int msg, int ext1, int ext2, const Parcel *in) {
969 ALOGV("notifyListener_l(%p), (%d, %d, %d, %d), loop setting(%d, %d)",
970 this, msg, ext1, ext2, (in == NULL ? -1 : (int)in->dataSize()), mAutoLoop, mLooping);
971 switch (msg) {
972 case MEDIA_PLAYBACK_COMPLETE:
973 {
974 if (mState != STATE_RESET_IN_PROGRESS) {
975 if (mAutoLoop) {
976 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
977 if (mAudioSink != NULL) {
978 streamType = mAudioSink->getAudioStreamType();
979 }
980 if (streamType == AUDIO_STREAM_NOTIFICATION) {
981 ALOGW("disabling auto-loop for notification");
982 mAutoLoop = false;
983 }
984 }
985 if (mLooping || mAutoLoop) {
986 mPlayer->seekToAsync(0);
987 if (mAudioSink != NULL) {
988 // The renderer has stopped the sink at the end in order to play out
989 // the last little bit of audio. If we're looping, we need to restart it.
990 mAudioSink->start();
991 }
992 // don't send completion event when looping
993 return;
994 }
995 if (property_get_bool("persist.debug.sf.stats", false)) {
996 Vector<String16> args;
997 dump(-1, args);
998 }
999 mPlayer->pause();
1000 mState = STATE_PAUSED;
1001 }
1002 FALLTHROUGH_INTENDED;
1003 }
1004
1005 case MEDIA_ERROR:
1006 {
1007 // when we have an error, add it to the analytics for this playback.
1008 // ext1 is our primary 'error type' value. Only add ext2 when non-zero.
1009 // [test against msg is due to fall through from previous switch value]
1010 if (msg == MEDIA_ERROR && mAnalyticsItem != NULL) {
1011 mAnalyticsItem->setInt32(kPlayerError, ext1);
1012 if (ext2 != 0) {
1013 mAnalyticsItem->setInt32(kPlayerErrorCode, ext2);
1014 }
1015 mAnalyticsItem->setCString(kPlayerErrorState, stateString(mState).c_str());
1016 }
1017 mAtEOS = true;
1018 break;
1019 }
1020
1021 default:
1022 break;
1023 }
1024
1025 mLock.unlock();
1026 sendEvent(msg, ext1, ext2, in);
1027 mLock.lock();
1028 }
1029
notifySetDataSourceCompleted(status_t err)1030 void NuPlayerDriver::notifySetDataSourceCompleted(status_t err) {
1031 Mutex::Autolock autoLock(mLock);
1032
1033 CHECK_EQ(mState, STATE_SET_DATASOURCE_PENDING);
1034
1035 mAsyncResult = err;
1036 mState = (err == OK) ? STATE_UNPREPARED : STATE_IDLE;
1037 mCondition.broadcast();
1038 }
1039
notifyPrepareCompleted(status_t err)1040 void NuPlayerDriver::notifyPrepareCompleted(status_t err) {
1041 ALOGV("notifyPrepareCompleted %d", err);
1042
1043 Mutex::Autolock autoLock(mLock);
1044
1045 if (mState != STATE_PREPARING) {
1046 // We were preparing asynchronously when the client called
1047 // reset(), we sent a premature "prepared" notification and
1048 // then initiated the reset. This notification is stale.
1049 CHECK(mState == STATE_RESET_IN_PROGRESS || mState == STATE_IDLE);
1050 return;
1051 }
1052
1053 CHECK_EQ(mState, STATE_PREPARING);
1054
1055 mAsyncResult = err;
1056
1057 if (err == OK) {
1058 // update state before notifying client, so that if client calls back into NuPlayerDriver
1059 // in response, NuPlayerDriver has the right state
1060 mState = STATE_PREPARED;
1061 if (mIsAsyncPrepare) {
1062 notifyListener_l(MEDIA_PREPARED);
1063 }
1064 } else {
1065 mState = STATE_UNPREPARED;
1066 if (mIsAsyncPrepare) {
1067 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1068 }
1069 }
1070
1071 sp<MetaData> meta = mPlayer->getFileMeta();
1072 int32_t loop;
1073 if (meta != NULL
1074 && meta->findInt32(kKeyAutoLoop, &loop) && loop != 0) {
1075 mAutoLoop = true;
1076 }
1077
1078 mCondition.broadcast();
1079 }
1080
notifyFlagsChanged(uint32_t flags)1081 void NuPlayerDriver::notifyFlagsChanged(uint32_t flags) {
1082 Mutex::Autolock autoLock(mLock);
1083
1084 mPlayerFlags = flags;
1085 }
1086
1087 // Modular DRM
prepareDrm(const uint8_t uuid[16],const Vector<uint8_t> & drmSessionId)1088 status_t NuPlayerDriver::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId)
1089 {
1090 ALOGV("prepareDrm(%p) state: %d", this, mState);
1091
1092 // leaving the state verification for mediaplayer.cpp
1093 status_t ret = mPlayer->prepareDrm(uuid, drmSessionId);
1094
1095 ALOGV("prepareDrm ret: %d", ret);
1096
1097 return ret;
1098 }
1099
releaseDrm()1100 status_t NuPlayerDriver::releaseDrm()
1101 {
1102 ALOGV("releaseDrm(%p) state: %d", this, mState);
1103
1104 // leaving the state verification for mediaplayer.cpp
1105 status_t ret = mPlayer->releaseDrm();
1106
1107 ALOGV("releaseDrm ret: %d", ret);
1108
1109 return ret;
1110 }
1111
stateString(State state)1112 std::string NuPlayerDriver::stateString(State state) {
1113 const char *rval = NULL;
1114 char rawbuffer[16]; // allows "%d"
1115
1116 switch (state) {
1117 case STATE_IDLE: rval = "IDLE"; break;
1118 case STATE_SET_DATASOURCE_PENDING: rval = "SET_DATASOURCE_PENDING"; break;
1119 case STATE_UNPREPARED: rval = "UNPREPARED"; break;
1120 case STATE_PREPARING: rval = "PREPARING"; break;
1121 case STATE_PREPARED: rval = "PREPARED"; break;
1122 case STATE_RUNNING: rval = "RUNNING"; break;
1123 case STATE_PAUSED: rval = "PAUSED"; break;
1124 case STATE_RESET_IN_PROGRESS: rval = "RESET_IN_PROGRESS"; break;
1125 case STATE_STOPPED: rval = "STOPPED"; break;
1126 case STATE_STOPPED_AND_PREPARING: rval = "STOPPED_AND_PREPARING"; break;
1127 case STATE_STOPPED_AND_PREPARED: rval = "STOPPED_AND_PREPARED"; break;
1128 default:
1129 // yes, this buffer is shared and vulnerable to races
1130 snprintf(rawbuffer, sizeof(rawbuffer), "%d", state);
1131 rval = rawbuffer;
1132 break;
1133 }
1134
1135 return rval;
1136 }
1137
1138 } // namespace android
1139