blob: 5c34d4aaadee405597757d24564e0318d8f9b5fb [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2**
3** Copyright 2017, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "MediaPlayer2Native"
20
21#include <fcntl.h>
22#include <inttypes.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <utils/Log.h>
28
29#include <binder/IServiceManager.h>
30#include <binder/IPCThreadState.h>
31
Wei Jia53692fa2017-12-11 10:33:46 -080032#include <media/mediaplayer2.h>
33#include <media/AudioResamplerPublic.h>
34#include <media/AudioSystem.h>
35#include <media/AVSyncSettings.h>
Wei Jiac5c79da2017-12-21 18:03:05 -080036#include <media/DataSource.h>
Wei Jia53692fa2017-12-11 10:33:46 -080037#include <media/MediaAnalyticsItem.h>
Wei Jia28288fb2017-12-15 13:45:29 -080038#include <media/NdkWrapper.h>
Wei Jia53692fa2017-12-11 10:33:46 -080039
40#include <binder/MemoryBase.h>
41
42#include <utils/KeyedVector.h>
43#include <utils/String8.h>
44
45#include <system/audio.h>
46#include <system/window.h>
47
48#include "MediaPlayer2Manager.h"
49
50namespace android {
51
52using media::VolumeShaper;
53
54MediaPlayer2::MediaPlayer2()
55{
56 ALOGV("constructor");
57 mListener = NULL;
58 mCookie = NULL;
59 mStreamType = AUDIO_STREAM_MUSIC;
60 mAudioAttributesParcel = NULL;
61 mCurrentPosition = -1;
62 mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
63 mSeekPosition = -1;
64 mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
65 mCurrentState = MEDIA_PLAYER2_IDLE;
66 mPrepareSync = false;
67 mPrepareStatus = NO_ERROR;
68 mLoop = false;
69 mLeftVolume = mRightVolume = 1.0;
70 mVideoWidth = mVideoHeight = 0;
71 mLockThreadId = 0;
72 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
73 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
74 mSendLevel = 0;
75 mRetransmitEndpointValid = false;
76}
77
78MediaPlayer2::~MediaPlayer2()
79{
80 ALOGV("destructor");
81 if (mAudioAttributesParcel != NULL) {
82 delete mAudioAttributesParcel;
83 mAudioAttributesParcel = NULL;
84 }
85 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
86 disconnect();
87 IPCThreadState::self()->flushCommands();
88}
89
90void MediaPlayer2::disconnect()
91{
92 ALOGV("disconnect");
93 sp<MediaPlayer2Engine> p;
94 {
95 Mutex::Autolock _l(mLock);
96 p = mPlayer;
97 mPlayer.clear();
98 }
99
100 if (p != 0) {
101 p->disconnect();
102 }
103}
104
105// always call with lock held
106void MediaPlayer2::clear_l()
107{
108 mCurrentPosition = -1;
109 mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
110 mSeekPosition = -1;
111 mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
112 mVideoWidth = mVideoHeight = 0;
113 mRetransmitEndpointValid = false;
114}
115
116status_t MediaPlayer2::setListener(const sp<MediaPlayer2Listener>& listener)
117{
118 ALOGV("setListener");
119 Mutex::Autolock _l(mLock);
120 mListener = listener;
121 return NO_ERROR;
122}
123
124
125status_t MediaPlayer2::attachNewPlayer(const sp<MediaPlayer2Engine>& player)
126{
127 status_t err = UNKNOWN_ERROR;
128 sp<MediaPlayer2Engine> p;
129 { // scope for the lock
130 Mutex::Autolock _l(mLock);
131
132 if ( !( (mCurrentState & MEDIA_PLAYER2_IDLE) ||
133 (mCurrentState == MEDIA_PLAYER2_STATE_ERROR ) ) ) {
134 ALOGE("attachNewPlayer called in state %d", mCurrentState);
135 return INVALID_OPERATION;
136 }
137
138 clear_l();
139 p = mPlayer;
140 mPlayer = player;
141 if (player != 0) {
142 mCurrentState = MEDIA_PLAYER2_INITIALIZED;
143 err = NO_ERROR;
144 } else {
145 ALOGE("Unable to create media player");
146 }
147 }
148
149 if (p != 0) {
150 p->disconnect();
151 }
152
153 return err;
154}
155
156status_t MediaPlayer2::setDataSource(
157 const sp<MediaHTTPService> &httpService,
158 const char *url, const KeyedVector<String8, String8> *headers)
159{
160 ALOGV("setDataSource(%s)", url);
161 status_t err = BAD_VALUE;
162 if (url != NULL) {
163 sp<MediaPlayer2Engine> player(MediaPlayer2Manager::get().create(this, mAudioSessionId));
164 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
165 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
166 player.clear();
167 }
168 err = attachNewPlayer(player);
169 }
170 return err;
171}
172
173status_t MediaPlayer2::setDataSource(int fd, int64_t offset, int64_t length)
174{
175 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
176 status_t err = UNKNOWN_ERROR;
177 sp<MediaPlayer2Engine> player(MediaPlayer2Manager::get().create(this, mAudioSessionId));
178 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
179 (NO_ERROR != player->setDataSource(fd, offset, length))) {
180 player.clear();
181 }
182 err = attachNewPlayer(player);
183 return err;
184}
185
Wei Jiac5c79da2017-12-21 18:03:05 -0800186status_t MediaPlayer2::setDataSource(const sp<DataSource> &source)
Wei Jia53692fa2017-12-11 10:33:46 -0800187{
Wei Jiac5c79da2017-12-21 18:03:05 -0800188 ALOGV("setDataSource(DataSource)");
Wei Jia53692fa2017-12-11 10:33:46 -0800189 status_t err = UNKNOWN_ERROR;
190 sp<MediaPlayer2Engine> player(MediaPlayer2Manager::get().create(this, mAudioSessionId));
191 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
192 (NO_ERROR != player->setDataSource(source))) {
193 player.clear();
194 }
195 err = attachNewPlayer(player);
196 return err;
197}
198
199status_t MediaPlayer2::invoke(const Parcel& request, Parcel *reply)
200{
201 Mutex::Autolock _l(mLock);
202 const bool hasBeenInitialized =
203 (mCurrentState != MEDIA_PLAYER2_STATE_ERROR) &&
204 ((mCurrentState & MEDIA_PLAYER2_IDLE) != MEDIA_PLAYER2_IDLE);
205 if ((mPlayer != NULL) && hasBeenInitialized) {
206 ALOGV("invoke %zu", request.dataSize());
207 return mPlayer->invoke(request, reply);
208 }
209 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
210 return INVALID_OPERATION;
211}
212
213status_t MediaPlayer2::setMetadataFilter(const Parcel& filter)
214{
215 ALOGD("setMetadataFilter");
216 Mutex::Autolock lock(mLock);
217 if (mPlayer == NULL) {
218 return NO_INIT;
219 }
220 return mPlayer->setMetadataFilter(filter);
221}
222
223status_t MediaPlayer2::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
224{
225 ALOGD("getMetadata");
226 Mutex::Autolock lock(mLock);
227 if (mPlayer == NULL) {
228 return NO_INIT;
229 }
230 return mPlayer->getMetadata(update_only, apply_filter, metadata);
231}
232
Wei Jia28288fb2017-12-15 13:45:29 -0800233status_t MediaPlayer2::setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww)
Wei Jia53692fa2017-12-11 10:33:46 -0800234{
235 ALOGV("setVideoSurfaceTexture");
236 Mutex::Autolock _l(mLock);
237 if (mPlayer == 0) return NO_INIT;
Wei Jia28288fb2017-12-15 13:45:29 -0800238 return mPlayer->setVideoSurfaceTexture(nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800239}
240
241status_t MediaPlayer2::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
242{
243 ALOGV("getBufferingSettings");
244
245 Mutex::Autolock _l(mLock);
246 if (mPlayer == 0) {
247 return NO_INIT;
248 }
249 return mPlayer->getBufferingSettings(buffering);
250}
251
252status_t MediaPlayer2::setBufferingSettings(const BufferingSettings& buffering)
253{
254 ALOGV("setBufferingSettings");
255
256 Mutex::Autolock _l(mLock);
257 if (mPlayer == 0) {
258 return NO_INIT;
259 }
260 return mPlayer->setBufferingSettings(buffering);
261}
262
263// must call with lock held
264status_t MediaPlayer2::prepareAsync_l()
265{
266 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER2_INITIALIZED | MEDIA_PLAYER2_STOPPED) ) ) {
267 if (mAudioAttributesParcel != NULL) {
268 mPlayer->setParameter(MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
269 } else {
270 mPlayer->setAudioStreamType(mStreamType);
271 }
272 mCurrentState = MEDIA_PLAYER2_PREPARING;
273 return mPlayer->prepareAsync();
274 }
275 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
276 return INVALID_OPERATION;
277}
278
279// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
280// one defined in the Android framework and one provided by the implementation
281// that generated the error. The sync version of prepare returns only 1 error
282// code.
283status_t MediaPlayer2::prepare()
284{
285 ALOGV("prepare");
286 Mutex::Autolock _l(mLock);
287 mLockThreadId = getThreadId();
288 if (mPrepareSync) {
289 mLockThreadId = 0;
290 return -EALREADY;
291 }
292 mPrepareSync = true;
293 status_t ret = prepareAsync_l();
294 if (ret != NO_ERROR) {
295 mLockThreadId = 0;
296 return ret;
297 }
298
299 if (mPrepareSync) {
300 mSignal.wait(mLock); // wait for prepare done
301 mPrepareSync = false;
302 }
303 ALOGV("prepare complete - status=%d", mPrepareStatus);
304 mLockThreadId = 0;
305 return mPrepareStatus;
306}
307
308status_t MediaPlayer2::prepareAsync()
309{
310 ALOGV("prepareAsync");
311 Mutex::Autolock _l(mLock);
312 return prepareAsync_l();
313}
314
315status_t MediaPlayer2::start()
316{
317 ALOGV("start");
318
319 status_t ret = NO_ERROR;
320 Mutex::Autolock _l(mLock);
321
322 mLockThreadId = getThreadId();
323
324 if (mCurrentState & MEDIA_PLAYER2_STARTED) {
325 ret = NO_ERROR;
326 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER2_PREPARED |
327 MEDIA_PLAYER2_PLAYBACK_COMPLETE | MEDIA_PLAYER2_PAUSED ) ) ) {
328 mPlayer->setLooping(mLoop);
329 mPlayer->setVolume(mLeftVolume, mRightVolume);
330 mPlayer->setAuxEffectSendLevel(mSendLevel);
331 mCurrentState = MEDIA_PLAYER2_STARTED;
332 ret = mPlayer->start();
333 if (ret != NO_ERROR) {
334 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
335 } else {
336 if (mCurrentState == MEDIA_PLAYER2_PLAYBACK_COMPLETE) {
337 ALOGV("playback completed immediately following start()");
338 }
339 }
340 } else {
341 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
342 ret = INVALID_OPERATION;
343 }
344
345 mLockThreadId = 0;
346
347 return ret;
348}
349
350status_t MediaPlayer2::stop()
351{
352 ALOGV("stop");
353 Mutex::Autolock _l(mLock);
354 if (mCurrentState & MEDIA_PLAYER2_STOPPED) return NO_ERROR;
355 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED |
356 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE ) ) ) {
357 status_t ret = mPlayer->stop();
358 if (ret != NO_ERROR) {
359 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
360 } else {
361 mCurrentState = MEDIA_PLAYER2_STOPPED;
362 }
363 return ret;
364 }
365 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
366 return INVALID_OPERATION;
367}
368
369status_t MediaPlayer2::pause()
370{
371 ALOGV("pause");
372 Mutex::Autolock _l(mLock);
373 if (mCurrentState & (MEDIA_PLAYER2_PAUSED|MEDIA_PLAYER2_PLAYBACK_COMPLETE))
374 return NO_ERROR;
375 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER2_STARTED)) {
376 status_t ret = mPlayer->pause();
377 if (ret != NO_ERROR) {
378 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
379 } else {
380 mCurrentState = MEDIA_PLAYER2_PAUSED;
381 }
382 return ret;
383 }
384 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
385 return INVALID_OPERATION;
386}
387
388bool MediaPlayer2::isPlaying()
389{
390 Mutex::Autolock _l(mLock);
391 if (mPlayer != 0) {
392 bool temp = false;
393 mPlayer->isPlaying(&temp);
394 ALOGV("isPlaying: %d", temp);
395 if ((mCurrentState & MEDIA_PLAYER2_STARTED) && ! temp) {
396 ALOGE("internal/external state mismatch corrected");
397 mCurrentState = MEDIA_PLAYER2_PAUSED;
398 } else if ((mCurrentState & MEDIA_PLAYER2_PAUSED) && temp) {
399 ALOGE("internal/external state mismatch corrected");
400 mCurrentState = MEDIA_PLAYER2_STARTED;
401 }
402 return temp;
403 }
404 ALOGV("isPlaying: no active player");
405 return false;
406}
407
408status_t MediaPlayer2::setPlaybackSettings(const AudioPlaybackRate& rate)
409{
410 ALOGV("setPlaybackSettings: %f %f %d %d",
411 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
412 // Negative speed and pitch does not make sense. Further validation will
413 // be done by the respective mediaplayers.
414 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
415 return BAD_VALUE;
416 }
417 Mutex::Autolock _l(mLock);
418 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER2_STOPPED)) {
419 return INVALID_OPERATION;
420 }
421
422 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER2_STARTED)
423 && (mCurrentState & (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_PAUSED
424 | MEDIA_PLAYER2_PLAYBACK_COMPLETE))) {
425 mPlayer->setLooping(mLoop);
426 mPlayer->setVolume(mLeftVolume, mRightVolume);
427 mPlayer->setAuxEffectSendLevel(mSendLevel);
428 }
429
430 status_t err = mPlayer->setPlaybackSettings(rate);
431 if (err == OK) {
432 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER2_STARTED) {
433 mCurrentState = MEDIA_PLAYER2_PAUSED;
434 } else if (rate.mSpeed != 0.f
435 && (mCurrentState & (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_PAUSED
436 | MEDIA_PLAYER2_PLAYBACK_COMPLETE))) {
437 mCurrentState = MEDIA_PLAYER2_STARTED;
438 }
439 }
440 return err;
441}
442
443status_t MediaPlayer2::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
444{
445 Mutex::Autolock _l(mLock);
446 if (mPlayer == 0) return INVALID_OPERATION;
447 return mPlayer->getPlaybackSettings(rate);
448}
449
450status_t MediaPlayer2::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
451{
452 ALOGV("setSyncSettings: %u %u %f %f",
453 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
454 Mutex::Autolock _l(mLock);
455 if (mPlayer == 0) return INVALID_OPERATION;
456 return mPlayer->setSyncSettings(sync, videoFpsHint);
457}
458
459status_t MediaPlayer2::getSyncSettings(
460 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
461{
462 Mutex::Autolock _l(mLock);
463 if (mPlayer == 0) return INVALID_OPERATION;
464 return mPlayer->getSyncSettings(sync, videoFps);
465}
466
467status_t MediaPlayer2::getVideoWidth(int *w)
468{
469 ALOGV("getVideoWidth");
470 Mutex::Autolock _l(mLock);
471 if (mPlayer == 0) return INVALID_OPERATION;
472 *w = mVideoWidth;
473 return NO_ERROR;
474}
475
476status_t MediaPlayer2::getVideoHeight(int *h)
477{
478 ALOGV("getVideoHeight");
479 Mutex::Autolock _l(mLock);
480 if (mPlayer == 0) return INVALID_OPERATION;
481 *h = mVideoHeight;
482 return NO_ERROR;
483}
484
485status_t MediaPlayer2::getCurrentPosition(int *msec)
486{
487 ALOGV("getCurrentPosition");
488 Mutex::Autolock _l(mLock);
489 if (mPlayer != 0) {
490 if (mCurrentPosition >= 0) {
491 ALOGV("Using cached seek position: %d", mCurrentPosition);
492 *msec = mCurrentPosition;
493 return NO_ERROR;
494 }
495 return mPlayer->getCurrentPosition(msec);
496 }
497 return INVALID_OPERATION;
498}
499
500status_t MediaPlayer2::getDuration_l(int *msec)
501{
502 ALOGV("getDuration_l");
503 bool isValidState = (mCurrentState & (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED |
504 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_STOPPED | MEDIA_PLAYER2_PLAYBACK_COMPLETE));
505 if (mPlayer != 0 && isValidState) {
506 int durationMs;
507 status_t ret = mPlayer->getDuration(&durationMs);
508
509 if (ret != OK) {
510 // Do not enter error state just because no duration was available.
511 durationMs = -1;
512 ret = OK;
513 }
514
515 if (msec) {
516 *msec = durationMs;
517 }
518 return ret;
519 }
520 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
521 mPlayer.get(), mCurrentState);
522 return INVALID_OPERATION;
523}
524
525status_t MediaPlayer2::getDuration(int *msec)
526{
527 Mutex::Autolock _l(mLock);
528 return getDuration_l(msec);
529}
530
531status_t MediaPlayer2::seekTo_l(int msec, MediaPlayer2SeekMode mode)
532{
533 ALOGV("seekTo (%d, %d)", msec, mode);
534 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED |
535 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE) ) ) {
536 if ( msec < 0 ) {
537 ALOGW("Attempt to seek to invalid position: %d", msec);
538 msec = 0;
539 }
540
541 int durationMs;
542 status_t err = mPlayer->getDuration(&durationMs);
543
544 if (err != OK) {
545 ALOGW("Stream has no duration and is therefore not seekable.");
546 return err;
547 }
548
549 if (msec > durationMs) {
550 ALOGW("Attempt to seek to past end of file: request = %d, "
551 "durationMs = %d",
552 msec,
553 durationMs);
554
555 msec = durationMs;
556 }
557
558 // cache duration
559 mCurrentPosition = msec;
560 mCurrentSeekMode = mode;
561 if (mSeekPosition < 0) {
562 mSeekPosition = msec;
563 mSeekMode = mode;
564 return mPlayer->seekTo(msec, mode);
565 }
566 else {
567 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
568 return NO_ERROR;
569 }
570 }
571 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
572 mCurrentState);
573 return INVALID_OPERATION;
574}
575
576status_t MediaPlayer2::seekTo(int msec, MediaPlayer2SeekMode mode)
577{
578 mLockThreadId = getThreadId();
579 Mutex::Autolock _l(mLock);
580 status_t result = seekTo_l(msec, mode);
581 mLockThreadId = 0;
582
583 return result;
584}
585
586status_t MediaPlayer2::notifyAt(int64_t mediaTimeUs)
587{
588 Mutex::Autolock _l(mLock);
589 if (mPlayer != 0) {
590 return mPlayer->notifyAt(mediaTimeUs);
591 }
592 return INVALID_OPERATION;
593}
594
595status_t MediaPlayer2::reset_l()
596{
597 mLoop = false;
598 if (mCurrentState == MEDIA_PLAYER2_IDLE) return NO_ERROR;
599 mPrepareSync = false;
600 if (mPlayer != 0) {
601 status_t ret = mPlayer->reset();
602 if (ret != NO_ERROR) {
603 ALOGE("reset() failed with return code (%d)", ret);
604 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
605 } else {
606 mPlayer->disconnect();
607 mCurrentState = MEDIA_PLAYER2_IDLE;
608 }
609 // setDataSource has to be called again to create a
610 // new mediaplayer.
611 mPlayer = 0;
612 return ret;
613 }
614 clear_l();
615 return NO_ERROR;
616}
617
618status_t MediaPlayer2::doSetRetransmitEndpoint(const sp<MediaPlayer2Engine>& player) {
619 Mutex::Autolock _l(mLock);
620
621 if (player == NULL) {
622 return UNKNOWN_ERROR;
623 }
624
625 if (mRetransmitEndpointValid) {
626 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
627 }
628
629 return OK;
630}
631
632status_t MediaPlayer2::reset()
633{
634 ALOGV("reset");
635 mLockThreadId = getThreadId();
636 Mutex::Autolock _l(mLock);
637 status_t result = reset_l();
638 mLockThreadId = 0;
639
640 return result;
641}
642
643status_t MediaPlayer2::setAudioStreamType(audio_stream_type_t type)
644{
645 ALOGV("MediaPlayer2::setAudioStreamType");
646 Mutex::Autolock _l(mLock);
647 if (mStreamType == type) return NO_ERROR;
648 if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED |
649 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE ) ) {
650 // Can't change the stream type after prepare
651 ALOGE("setAudioStream called in state %d", mCurrentState);
652 return INVALID_OPERATION;
653 }
654 // cache
655 mStreamType = type;
656 return OK;
657}
658
659status_t MediaPlayer2::getAudioStreamType(audio_stream_type_t *type)
660{
661 ALOGV("getAudioStreamType");
662 Mutex::Autolock _l(mLock);
663 *type = mStreamType;
664 return OK;
665}
666
667status_t MediaPlayer2::setLooping(int loop)
668{
669 ALOGV("MediaPlayer2::setLooping");
670 Mutex::Autolock _l(mLock);
671 mLoop = (loop != 0);
672 if (mPlayer != 0) {
673 return mPlayer->setLooping(loop);
674 }
675 return OK;
676}
677
678bool MediaPlayer2::isLooping() {
679 ALOGV("isLooping");
680 Mutex::Autolock _l(mLock);
681 if (mPlayer != 0) {
682 return mLoop;
683 }
684 ALOGV("isLooping: no active player");
685 return false;
686}
687
688status_t MediaPlayer2::setVolume(float leftVolume, float rightVolume)
689{
690 ALOGV("MediaPlayer2::setVolume(%f, %f)", leftVolume, rightVolume);
691 Mutex::Autolock _l(mLock);
692 mLeftVolume = leftVolume;
693 mRightVolume = rightVolume;
694 if (mPlayer != 0) {
695 return mPlayer->setVolume(leftVolume, rightVolume);
696 }
697 return OK;
698}
699
700status_t MediaPlayer2::setAudioSessionId(audio_session_t sessionId)
701{
702 ALOGV("MediaPlayer2::setAudioSessionId(%d)", sessionId);
703 Mutex::Autolock _l(mLock);
704 if (!(mCurrentState & MEDIA_PLAYER2_IDLE)) {
705 ALOGE("setAudioSessionId called in state %d", mCurrentState);
706 return INVALID_OPERATION;
707 }
708 if (sessionId < 0) {
709 return BAD_VALUE;
710 }
711 if (sessionId != mAudioSessionId) {
712 AudioSystem::acquireAudioSessionId(sessionId, -1);
713 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
714 mAudioSessionId = sessionId;
715 }
716 return NO_ERROR;
717}
718
719audio_session_t MediaPlayer2::getAudioSessionId()
720{
721 Mutex::Autolock _l(mLock);
722 return mAudioSessionId;
723}
724
725status_t MediaPlayer2::setAuxEffectSendLevel(float level)
726{
727 ALOGV("MediaPlayer2::setAuxEffectSendLevel(%f)", level);
728 Mutex::Autolock _l(mLock);
729 mSendLevel = level;
730 if (mPlayer != 0) {
731 return mPlayer->setAuxEffectSendLevel(level);
732 }
733 return OK;
734}
735
736status_t MediaPlayer2::attachAuxEffect(int effectId)
737{
738 ALOGV("MediaPlayer2::attachAuxEffect(%d)", effectId);
739 Mutex::Autolock _l(mLock);
740 if (mPlayer == 0 ||
741 (mCurrentState & MEDIA_PLAYER2_IDLE) ||
742 (mCurrentState == MEDIA_PLAYER2_STATE_ERROR )) {
743 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
744 return INVALID_OPERATION;
745 }
746
747 return mPlayer->attachAuxEffect(effectId);
748}
749
750// always call with lock held
751status_t MediaPlayer2::checkStateForKeySet_l(int key)
752{
753 switch(key) {
754 case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES:
755 if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED |
756 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE) ) {
757 // Can't change the audio attributes after prepare
758 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
759 return INVALID_OPERATION;
760 }
761 break;
762 default:
763 // parameter doesn't require player state check
764 break;
765 }
766 return OK;
767}
768
769status_t MediaPlayer2::setParameter(int key, const Parcel& request)
770{
771 ALOGV("MediaPlayer2::setParameter(%d)", key);
772 status_t status = INVALID_OPERATION;
773 Mutex::Autolock _l(mLock);
774 if (checkStateForKeySet_l(key) != OK) {
775 return status;
776 }
777 switch (key) {
778 case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES:
779 // save the marshalled audio attributes
780 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
781 mAudioAttributesParcel = new Parcel();
782 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
783 status = OK;
784 break;
785 default:
786 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
787 break;
788 }
789
790 if (mPlayer != NULL) {
791 status = mPlayer->setParameter(key, request);
792 }
793 return status;
794}
795
796status_t MediaPlayer2::getParameter(int key, Parcel *reply)
797{
798 ALOGV("MediaPlayer2::getParameter(%d)", key);
799 Mutex::Autolock _l(mLock);
800 if (mPlayer != NULL) {
801 status_t status = mPlayer->getParameter(key, reply);
802 if (status != OK) {
803 ALOGD("getParameter returns %d", status);
804 }
805 return status;
806 }
807 ALOGV("getParameter: no active player");
808 return INVALID_OPERATION;
809}
810
811status_t MediaPlayer2::setRetransmitEndpoint(const char* addrString,
812 uint16_t port) {
813 ALOGV("MediaPlayer2::setRetransmitEndpoint(%s:%hu)",
814 addrString ? addrString : "(null)", port);
815
816 Mutex::Autolock _l(mLock);
817 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER2_IDLE))
818 return INVALID_OPERATION;
819
820 if (NULL == addrString) {
821 mRetransmitEndpointValid = false;
822 return OK;
823 }
824
825 struct in_addr saddr;
826 if(!inet_aton(addrString, &saddr)) {
827 return BAD_VALUE;
828 }
829
830 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
831 mRetransmitEndpoint.sin_family = AF_INET;
832 mRetransmitEndpoint.sin_addr = saddr;
833 mRetransmitEndpoint.sin_port = htons(port);
834 mRetransmitEndpointValid = true;
835
836 return OK;
837}
838
839void MediaPlayer2::notify(int msg, int ext1, int ext2, const Parcel *obj)
840{
841 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
842 bool send = true;
843 bool locked = false;
844
845 // TODO: In the future, we might be on the same thread if the app is
846 // running in the same process as the media server. In that case,
847 // this will deadlock.
848 //
849 // The threadId hack below works around this for the care of prepare,
850 // seekTo, start, and reset within the same process.
851 // FIXME: Remember, this is a hack, it's not even a hack that is applied
852 // consistently for all use-cases, this needs to be revisited.
853 if (mLockThreadId != getThreadId()) {
854 mLock.lock();
855 locked = true;
856 }
857
858 // Allows calls from JNI in idle state to notify errors
859 if (!(msg == MEDIA2_ERROR && mCurrentState == MEDIA_PLAYER2_IDLE) && mPlayer == 0) {
860 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
861 if (locked) mLock.unlock(); // release the lock when done.
862 return;
863 }
864
865 switch (msg) {
866 case MEDIA2_NOP: // interface test message
867 break;
868 case MEDIA2_PREPARED:
869 ALOGV("MediaPlayer2::notify() prepared");
870 mCurrentState = MEDIA_PLAYER2_PREPARED;
871 if (mPrepareSync) {
872 ALOGV("signal application thread");
873 mPrepareSync = false;
874 mPrepareStatus = NO_ERROR;
875 mSignal.signal();
876 }
877 break;
878 case MEDIA2_DRM_INFO:
879 ALOGV("MediaPlayer2::notify() MEDIA2_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
880 break;
881 case MEDIA2_PLAYBACK_COMPLETE:
882 ALOGV("playback complete");
883 if (mCurrentState == MEDIA_PLAYER2_IDLE) {
884 ALOGE("playback complete in idle state");
885 }
886 if (!mLoop) {
887 mCurrentState = MEDIA_PLAYER2_PLAYBACK_COMPLETE;
888 }
889 break;
890 case MEDIA2_ERROR:
891 // Always log errors.
892 // ext1: Media framework error code.
893 // ext2: Implementation dependant error code.
894 ALOGE("error (%d, %d)", ext1, ext2);
895 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
896 if (mPrepareSync)
897 {
898 ALOGV("signal application thread");
899 mPrepareSync = false;
900 mPrepareStatus = ext1;
901 mSignal.signal();
902 send = false;
903 }
904 break;
905 case MEDIA2_INFO:
906 // ext1: Media framework error code.
907 // ext2: Implementation dependant error code.
908 if (ext1 != MEDIA2_INFO_VIDEO_TRACK_LAGGING) {
909 ALOGW("info/warning (%d, %d)", ext1, ext2);
910 }
911 break;
912 case MEDIA2_SEEK_COMPLETE:
913 ALOGV("Received seek complete");
914 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
915 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
916 mSeekPosition = -1;
917 mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
918 seekTo_l(mCurrentPosition, mCurrentSeekMode);
919 }
920 else {
921 ALOGV("All seeks complete - return to regularly scheduled program");
922 mCurrentPosition = mSeekPosition = -1;
923 mCurrentSeekMode = mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
924 }
925 break;
926 case MEDIA2_BUFFERING_UPDATE:
927 ALOGV("buffering %d", ext1);
928 break;
929 case MEDIA2_SET_VIDEO_SIZE:
930 ALOGV("New video size %d x %d", ext1, ext2);
931 mVideoWidth = ext1;
932 mVideoHeight = ext2;
933 break;
934 case MEDIA2_NOTIFY_TIME:
935 ALOGV("Received notify time message");
936 break;
937 case MEDIA2_TIMED_TEXT:
938 ALOGV("Received timed text message");
939 break;
940 case MEDIA2_SUBTITLE_DATA:
941 ALOGV("Received subtitle data message");
942 break;
943 case MEDIA2_META_DATA:
944 ALOGV("Received timed metadata message");
945 break;
946 default:
947 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
948 break;
949 }
950
951 sp<MediaPlayer2Listener> listener = mListener;
952 if (locked) mLock.unlock();
953
954 // this prevents re-entrant calls into client code
955 if ((listener != 0) && send) {
956 Mutex::Autolock _l(mNotifyLock);
957 ALOGV("callback application");
958 listener->notify(msg, ext1, ext2, obj);
959 ALOGV("back from callback");
960 }
961}
962
963status_t MediaPlayer2::setNextMediaPlayer(const sp<MediaPlayer2>& next) {
964 Mutex::Autolock _l(mLock);
965 if (mPlayer == NULL) {
966 return NO_INIT;
967 }
968
969 if (next != NULL && !(next->mCurrentState &
970 (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE))) {
971 ALOGE("next player is not prepared");
972 return INVALID_OPERATION;
973 }
974
975 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
976}
977
978VolumeShaper::Status MediaPlayer2::applyVolumeShaper(
979 const sp<VolumeShaper::Configuration>& configuration,
980 const sp<VolumeShaper::Operation>& operation)
981{
982 Mutex::Autolock _l(mLock);
983 if (mPlayer == nullptr) {
984 return VolumeShaper::Status(NO_INIT);
985 }
986 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
987 return status;
988}
989
990sp<VolumeShaper::State> MediaPlayer2::getVolumeShaperState(int id)
991{
992 Mutex::Autolock _l(mLock);
993 if (mPlayer == nullptr) {
994 return nullptr;
995 }
996 return mPlayer->getVolumeShaperState(id);
997}
998
999// Modular DRM
1000status_t MediaPlayer2::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
1001{
1002 // TODO change to ALOGV
1003 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1004 drmSessionId.array(), drmSessionId.size());
1005 Mutex::Autolock _l(mLock);
1006 if (mPlayer == NULL) {
1007 return NO_INIT;
1008 }
1009
1010 // Only allowed it in player's preparing/prepared state.
1011 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1012 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1013 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1014 if (!(mCurrentState & (MEDIA_PLAYER2_PREPARING | MEDIA_PLAYER2_PREPARED))) {
1015 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
1016 return INVALID_OPERATION;
1017 }
1018
1019 if (drmSessionId.isEmpty()) {
1020 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1021 return INVALID_OPERATION;
1022 }
1023
1024 // Passing down to mediaserver mainly for creating the crypto
1025 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1026 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1027
1028 // TODO change to ALOGV
1029 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1030
1031 return status;
1032}
1033
1034status_t MediaPlayer2::releaseDrm()
1035{
1036 Mutex::Autolock _l(mLock);
1037 if (mPlayer == NULL) {
1038 return NO_INIT;
1039 }
1040
1041 // Not allowing releaseDrm in an active/resumable state
1042 if (mCurrentState & (MEDIA_PLAYER2_STARTED |
1043 MEDIA_PLAYER2_PAUSED |
1044 MEDIA_PLAYER2_PLAYBACK_COMPLETE |
1045 MEDIA_PLAYER2_STATE_ERROR)) {
1046 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
1047 return INVALID_OPERATION;
1048 }
1049
1050 status_t status = mPlayer->releaseDrm();
1051 // TODO change to ALOGV
1052 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1053 if (status != OK) {
1054 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1055 // Overriding to OK so the client proceed with its own cleanup
1056 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1057 status = OK;
1058 }
1059
1060 return status;
1061}
1062
1063status_t MediaPlayer2::setOutputDevice(audio_port_handle_t deviceId)
1064{
1065 Mutex::Autolock _l(mLock);
1066 if (mPlayer == NULL) {
1067 ALOGV("setOutputDevice: player not init");
1068 return NO_INIT;
1069 }
1070 return mPlayer->setOutputDevice(deviceId);
1071}
1072
1073audio_port_handle_t MediaPlayer2::getRoutedDeviceId()
1074{
1075 Mutex::Autolock _l(mLock);
1076 if (mPlayer == NULL) {
1077 ALOGV("getRoutedDeviceId: player not init");
1078 return AUDIO_PORT_HANDLE_NONE;
1079 }
1080 audio_port_handle_t deviceId;
1081 status_t status = mPlayer->getRoutedDeviceId(&deviceId);
1082 if (status != NO_ERROR) {
1083 return AUDIO_PORT_HANDLE_NONE;
1084 }
1085 return deviceId;
1086}
1087
1088status_t MediaPlayer2::enableAudioDeviceCallback(bool enabled)
1089{
1090 Mutex::Autolock _l(mLock);
1091 if (mPlayer == NULL) {
1092 ALOGV("addAudioDeviceCallback: player not init");
1093 return NO_INIT;
1094 }
1095 return mPlayer->enableAudioDeviceCallback(enabled);
1096}
1097
1098} // namespace android