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