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