blob: 1fadc94570b6e53b2147ac980a87c6af48509685 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2006, 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
Hassan Shojaniacefac142017-02-06 21:02:02 -080019#define LOG_TAG "MediaPlayerNative"
Marco Nelissendab79b32019-11-18 08:25:47 -080020#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070023#include <inttypes.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
Marco Nelissendab79b32019-11-18 08:25:47 -080029#include <android/IDataSource.h>
Mathias Agopian75624082009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/mediaplayer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070032#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080033#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070034#include <media/AVSyncSettings.h>
Andreas Huber2db84552010-01-28 11:19:57 -080035#include <utils/KeyedVector.h>
36#include <utils/String8.h>
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070038#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070039
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040namespace android {
41
Ivan Lozano8cf3a072017-08-09 09:01:33 -070042using media::VolumeShaper;
43
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044MediaPlayer::MediaPlayer()
45{
Steve Block3856b092011-10-20 11:56:00 +010046 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047 mListener = NULL;
48 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070049 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070050 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080052 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080054 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055 mCurrentState = MEDIA_PLAYER_IDLE;
56 mPrepareSync = false;
57 mPrepareStatus = NO_ERROR;
58 mLoop = false;
59 mLeftVolume = mRightVolume = 1.0;
60 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070061 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080062 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Andy Hung8b0bfd92019-12-23 13:11:11 -080063 AudioSystem::acquireAudioSessionId(mAudioSessionId, (pid_t)-1, (uid_t)-1); // always in client.
Eric Laurent8c563ed2010-10-07 18:23:03 -070064 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080065 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066}
67
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068MediaPlayer::~MediaPlayer()
69{
Steve Block3856b092011-10-20 11:56:00 +010070 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070071 if (mAudioAttributesParcel != NULL) {
72 delete mAudioAttributesParcel;
73 mAudioAttributesParcel = NULL;
74 }
Andy Hung8b0bfd92019-12-23 13:11:11 -080075 AudioSystem::releaseAudioSessionId(mAudioSessionId, (pid_t)-1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 disconnect();
77 IPCThreadState::self()->flushCommands();
78}
79
80void MediaPlayer::disconnect()
81{
Steve Block3856b092011-10-20 11:56:00 +010082 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 sp<IMediaPlayer> p;
84 {
85 Mutex::Autolock _l(mLock);
86 p = mPlayer;
87 mPlayer.clear();
88 }
89
90 if (p != 0) {
91 p->disconnect();
92 }
93}
94
95// always call with lock held
96void MediaPlayer::clear_l()
97{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080099 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800101 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800103 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104}
105
106status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
107{
Steve Block3856b092011-10-20 11:56:00 +0100108 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 Mutex::Autolock _l(mLock);
110 mListener = listener;
111 return NO_ERROR;
112}
113
114
Dave Burked681bbb2011-08-30 14:39:17 +0100115status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116{
117 status_t err = UNKNOWN_ERROR;
118 sp<IMediaPlayer> p;
119 { // scope for the lock
120 Mutex::Autolock _l(mLock);
121
Marco Nelissen83ff1432010-03-10 10:53:16 -0800122 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
123 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 return INVALID_OPERATION;
126 }
127
128 clear_l();
129 p = mPlayer;
130 mPlayer = player;
131 if (player != 0) {
132 mCurrentState = MEDIA_PLAYER_INITIALIZED;
133 err = NO_ERROR;
134 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000135 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 }
137 }
138
139 if (p != 0) {
140 p->disconnect();
141 }
142
143 return err;
144}
145
Andreas Huber2db84552010-01-28 11:19:57 -0800146status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800147 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800148 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149{
Steve Block3856b092011-10-20 11:56:00 +0100150 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 status_t err = BAD_VALUE;
152 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700153 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800155 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800156 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800157 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100158 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100159 }
Dave Burke06620672011-09-06 20:39:47 +0100160 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162 }
163 return err;
164}
165
166status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
167{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700168 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700170 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800172 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800173 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
174 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100175 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100176 }
Dave Burke06620672011-09-06 20:39:47 +0100177 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100178 }
179 return err;
180}
181
Chris Watkins99f31602015-03-20 13:06:33 -0700182status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
183{
184 ALOGV("setDataSource(IDataSource)");
185 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700186 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700187 if (service != 0) {
188 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
189 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
190 (NO_ERROR != player->setDataSource(source))) {
191 player.clear();
192 }
193 err = attachNewPlayer(player);
194 }
195 return err;
196}
197
Nicolas Catania1d187f12009-05-12 23:25:55 -0700198status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
199{
200 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800201 const bool hasBeenInitialized =
202 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
203 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
204 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700205 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700206 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700207 }
Wei Jia848ebc62016-03-23 14:06:46 -0700208 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700209 return INVALID_OPERATION;
210}
211
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700212status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
213{
Steve Blockb8a80522011-12-20 16:23:08 +0000214 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700215 Mutex::Autolock lock(mLock);
216 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700217 return NO_INIT;
218 }
219 return mPlayer->setMetadataFilter(filter);
220}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700221
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700222status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
223{
Steve Blockb8a80522011-12-20 16:23:08 +0000224 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700225 Mutex::Autolock lock(mLock);
226 if (mPlayer == NULL) {
227 return NO_INIT;
228 }
229 return mPlayer->getMetadata(update_only, apply_filter, metadata);
230}
231
Glenn Kasten11731182011-02-08 17:26:17 -0800232status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800233 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800234{
Steve Block3856b092011-10-20 11:56:00 +0100235 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800236 Mutex::Autolock _l(mLock);
237 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800238 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800239}
240
Wei Jiaed320862017-01-18 14:03:40 -0800241status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
242{
243 ALOGV("getBufferingSettings");
244
245 Mutex::Autolock _l(mLock);
246 if (mPlayer == 0) {
247 return NO_INIT;
248 }
Wei Jia9bb38032017-03-23 18:00:38 -0700249 return mPlayer->getBufferingSettings(buffering);
Wei Jiaed320862017-01-18 14:03:40 -0800250}
251
Wei Jiadc6f3402017-01-09 15:04:18 -0800252status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering)
253{
254 ALOGV("setBufferingSettings");
255
256 Mutex::Autolock _l(mLock);
257 if (mPlayer == 0) {
258 return NO_INIT;
259 }
Wei Jia9bb38032017-03-23 18:00:38 -0700260 return mPlayer->setBufferingSettings(buffering);
Wei Jiadc6f3402017-01-09 15:04:18 -0800261}
262
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263// must call with lock held
264status_t MediaPlayer::prepareAsync_l()
265{
Glenn Kastenb187de12014-12-30 08:18:15 -0800266 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700267 if (mAudioAttributesParcel != NULL) {
268 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700269 } else {
270 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700271 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 mCurrentState = MEDIA_PLAYER_PREPARING;
273 return mPlayer->prepareAsync();
274 }
Wei Jia848ebc62016-03-23 14:06:46 -0700275 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 return INVALID_OPERATION;
277}
278
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700279// 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.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283status_t MediaPlayer::prepare()
284{
Steve Block3856b092011-10-20 11:56:00 +0100285 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700287 mLockThreadId = getThreadId();
288 if (mPrepareSync) {
289 mLockThreadId = 0;
290 return -EALREADY;
291 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 mPrepareSync = true;
293 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700294 if (ret != NO_ERROR) {
295 mLockThreadId = 0;
296 return ret;
297 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298
299 if (mPrepareSync) {
300 mSignal.wait(mLock); // wait for prepare done
301 mPrepareSync = false;
302 }
Steve Block3856b092011-10-20 11:56:00 +0100303 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700304 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 return mPrepareStatus;
306}
307
308status_t MediaPlayer::prepareAsync()
309{
Steve Block3856b092011-10-20 11:56:00 +0100310 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 Mutex::Autolock _l(mLock);
312 return prepareAsync_l();
313}
314
315status_t MediaPlayer::start()
316{
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700318
319 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700321
322 mLockThreadId = getThreadId();
323
324 if (mCurrentState & MEDIA_PLAYER_STARTED) {
325 ret = NO_ERROR;
326 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
328 mPlayer->setLooping(mLoop);
329 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700330 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700332 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 if (ret != NO_ERROR) {
334 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
335 } else {
336 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100337 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338 }
339 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700340 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700341 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700342 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700344
345 mLockThreadId = 0;
346
347 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348}
349
350status_t MediaPlayer::stop()
351{
Steve Block3856b092011-10-20 11:56:00 +0100352 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353 Mutex::Autolock _l(mLock);
354 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
355 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
356 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
357 status_t ret = mPlayer->stop();
358 if (ret != NO_ERROR) {
359 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
360 } else {
361 mCurrentState = MEDIA_PLAYER_STOPPED;
362 }
363 return ret;
364 }
Wei Jia848ebc62016-03-23 14:06:46 -0700365 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 return INVALID_OPERATION;
367}
368
369status_t MediaPlayer::pause()
370{
Steve Block3856b092011-10-20 11:56:00 +0100371 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800373 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 return NO_ERROR;
375 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
376 status_t ret = mPlayer->pause();
377 if (ret != NO_ERROR) {
378 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
379 } else {
380 mCurrentState = MEDIA_PLAYER_PAUSED;
381 }
382 return ret;
383 }
Wei Jia848ebc62016-03-23 14:06:46 -0700384 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 return INVALID_OPERATION;
386}
387
388bool MediaPlayer::isPlaying()
389{
390 Mutex::Autolock _l(mLock);
391 if (mPlayer != 0) {
392 bool temp = false;
393 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100394 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000396 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700398 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
399 ALOGE("internal/external state mismatch corrected");
400 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 }
402 return temp;
403 }
Steve Block3856b092011-10-20 11:56:00 +0100404 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405 return false;
406}
407
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700408status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800409{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700410 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) {
Wei Jia98160162015-02-04 17:01:11 -0800415 return BAD_VALUE;
416 }
417 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700418 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
419 return INVALID_OPERATION;
420 }
Wei Jia5be109f2016-06-10 16:15:07 -0700421
422 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
423 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
424 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
425 mPlayer->setLooping(mLoop);
426 mPlayer->setVolume(mLeftVolume, mRightVolume);
427 mPlayer->setAuxEffectSendLevel(mSendLevel);
428 }
429
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700430 status_t err = mPlayer->setPlaybackSettings(rate);
431 if (err == OK) {
432 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
433 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700434 } else if (rate.mSpeed != 0.f
435 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
436 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700437 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800438 }
Wei Jia98160162015-02-04 17:01:11 -0800439 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700440 return err;
441}
442
443status_t MediaPlayer::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 MediaPlayer::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 MediaPlayer::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);
Wei Jia98160162015-02-04 17:01:11 -0800465}
466
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467status_t MediaPlayer::getVideoWidth(int *w)
468{
Steve Block3856b092011-10-20 11:56:00 +0100469 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 Mutex::Autolock _l(mLock);
471 if (mPlayer == 0) return INVALID_OPERATION;
472 *w = mVideoWidth;
473 return NO_ERROR;
474}
475
476status_t MediaPlayer::getVideoHeight(int *h)
477{
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 Mutex::Autolock _l(mLock);
480 if (mPlayer == 0) return INVALID_OPERATION;
481 *h = mVideoHeight;
482 return NO_ERROR;
483}
484
485status_t MediaPlayer::getCurrentPosition(int *msec)
486{
Steve Block3856b092011-10-20 11:56:00 +0100487 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 Mutex::Autolock _l(mLock);
489 if (mPlayer != 0) {
490 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 *msec = mCurrentPosition;
493 return NO_ERROR;
494 }
495 return mPlayer->getCurrentPosition(msec);
496 }
497 return INVALID_OPERATION;
498}
499
500status_t MediaPlayer::getDuration_l(int *msec)
501{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800502 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800503 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
504 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800506 int durationMs;
507 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700508
509 if (ret != OK) {
510 // Do not enter error state just because no duration was available.
511 durationMs = -1;
512 ret = OK;
513 }
514
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800515 if (msec) {
516 *msec = durationMs;
517 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 return ret;
519 }
Wei Jia848ebc62016-03-23 14:06:46 -0700520 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
521 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 return INVALID_OPERATION;
523}
524
525status_t MediaPlayer::getDuration(int *msec)
526{
527 Mutex::Autolock _l(mLock);
528 return getDuration_l(msec);
529}
530
Wei Jiac5de0912016-11-18 10:22:14 -0800531status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532{
Wei Jiac5de0912016-11-18 10:22:14 -0800533 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800534 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
535 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000537 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800540
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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558 // cache duration
559 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800560 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800563 mSeekMode = mode;
564 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 }
566 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800567 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 return NO_ERROR;
569 }
570 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800571 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
572 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 return INVALID_OPERATION;
574}
575
Wei Jiac5de0912016-11-18 10:22:14 -0800576status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700578 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800580 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700581 mLockThreadId = 0;
582
583 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584}
585
Wei Jia52c28512017-09-13 18:17:51 -0700586status_t MediaPlayer::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
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700595status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 mLoop = false;
598 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
599 mPrepareSync = false;
600 if (mPlayer != 0) {
601 status_t ret = mPlayer->reset();
602 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000603 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
605 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700606 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 mCurrentState = MEDIA_PLAYER_IDLE;
608 }
James Donga1680bc2010-11-18 12:23:58 -0800609 // setDataSource has to be called again to create a
610 // new mediaplayer.
611 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612 return ret;
613 }
614 clear_l();
615 return NO_ERROR;
616}
617
John Grossmanc795b642012-02-22 15:38:35 -0800618status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& 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
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700632status_t MediaPlayer::reset()
633{
Steve Block3856b092011-10-20 11:56:00 +0100634 ALOGV("reset");
Wonsik Kim5f648972017-07-21 15:42:59 -0700635 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700636 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700637 status_t result = reset_l();
638 mLockThreadId = 0;
639
640 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700641}
642
Glenn Kastenfff6d712012-01-12 16:38:12 -0800643status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644{
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646 Mutex::Autolock _l(mLock);
647 if (mStreamType == type) return NO_ERROR;
648 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
649 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
650 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000651 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return INVALID_OPERATION;
653 }
654 // cache
655 mStreamType = type;
656 return OK;
657}
658
John Spurlockde9453f2014-03-19 13:05:45 -0400659status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
660{
661 ALOGV("getAudioStreamType");
662 Mutex::Autolock _l(mLock);
663 *type = mStreamType;
664 return OK;
665}
666
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667status_t MediaPlayer::setLooping(int loop)
668{
Steve Block3856b092011-10-20 11:56:00 +0100669 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 Mutex::Autolock _l(mLock);
671 mLoop = (loop != 0);
672 if (mPlayer != 0) {
673 return mPlayer->setLooping(loop);
674 }
675 return OK;
676}
677
678bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680 Mutex::Autolock _l(mLock);
681 if (mPlayer != 0) {
682 return mLoop;
683 }
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685 return false;
686}
687
688status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
689{
Steve Block3856b092011-10-20 11:56:00 +0100690 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691 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
Glenn Kastend848eb42016-03-08 13:42:11 -0800700status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700701{
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700703 Mutex::Autolock _l(mLock);
704 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000705 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700706 return INVALID_OPERATION;
707 }
708 if (sessionId < 0) {
709 return BAD_VALUE;
710 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700711 if (sessionId != mAudioSessionId) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800712 AudioSystem::acquireAudioSessionId(sessionId, (pid_t)-1, (uid_t)-1);
713 AudioSystem::releaseAudioSessionId(mAudioSessionId, (pid_t)-1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700714 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700715 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700716 return NO_ERROR;
717}
718
Glenn Kastend848eb42016-03-08 13:42:11 -0800719audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700720{
721 Mutex::Autolock _l(mLock);
722 return mAudioSessionId;
723}
724
Eric Laurent2beeb502010-07-16 07:43:46 -0700725status_t MediaPlayer::setAuxEffectSendLevel(float level)
726{
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700728 Mutex::Autolock _l(mLock);
729 mSendLevel = level;
730 if (mPlayer != 0) {
731 return mPlayer->setAuxEffectSendLevel(level);
732 }
733 return OK;
734}
735
736status_t MediaPlayer::attachAuxEffect(int effectId)
737{
Steve Block3856b092011-10-20 11:56:00 +0100738 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700739 Mutex::Autolock _l(mLock);
740 if (mPlayer == 0 ||
741 (mCurrentState & MEDIA_PLAYER_IDLE) ||
742 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700743 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700744 return INVALID_OPERATION;
745 }
746
747 return mPlayer->attachAuxEffect(effectId);
748}
749
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700750// always call with lock held
751status_t MediaPlayer::checkStateForKeySet_l(int key)
752{
753 switch(key) {
754 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
755 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
756 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_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
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700769status_t MediaPlayer::setParameter(int key, const Parcel& request)
770{
Steve Block3856b092011-10-20 11:56:00 +0100771 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700772 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700773 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700774 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700775 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700776 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700777 switch (key) {
778 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700779 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700780 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
781 mAudioAttributesParcel = new Parcel();
782 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700783 status = OK;
784 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700785 default:
Eric Laurent43562692015-07-15 16:49:07 -0700786 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
787 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700788 }
Eric Laurent43562692015-07-15 16:49:07 -0700789
790 if (mPlayer != NULL) {
791 status = mPlayer->setParameter(key, request);
792 }
793 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700794}
795
796status_t MediaPlayer::getParameter(int key, Parcel *reply)
797{
Steve Block3856b092011-10-20 11:56:00 +0100798 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700799 Mutex::Autolock _l(mLock);
800 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800801 status_t status = mPlayer->getParameter(key, reply);
802 if (status != OK) {
803 ALOGD("getParameter returns %d", status);
804 }
805 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700806 }
Steve Block3856b092011-10-20 11:56:00 +0100807 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700808 return INVALID_OPERATION;
809}
810
John Grossmanc795b642012-02-22 15:38:35 -0800811status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
812 uint16_t port) {
813 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
814 addrString ? addrString : "(null)", port);
815
816 Mutex::Autolock _l(mLock);
817 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_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
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800830 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800831 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
Gloria Wangb483c472011-04-11 17:23:27 -0700839void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800840{
Steve Block3856b092011-10-20 11:56:00 +0100841 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800842 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700843 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844
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.
Nicolas Catania66095182009-06-11 16:33:49 -0700848 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700849 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700850 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700851 // 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.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700853 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700854 mLock.lock();
855 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700856 }
Jason Sams1af452f2009-03-24 18:45:22 -0700857
Eric Laurent3b268442010-08-03 07:49:49 -0700858 // Allows calls from JNI in idle state to notify errors
859 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700861 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800862 return;
863 }
864
865 switch (msg) {
866 case MEDIA_NOP: // interface test message
867 break;
868 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800869 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870 mCurrentState = MEDIA_PLAYER_PREPARED;
871 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800873 mPrepareSync = false;
874 mPrepareStatus = NO_ERROR;
875 mSignal.signal();
876 }
877 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800878 case MEDIA_DRM_INFO:
879 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
880 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700883 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000884 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700885 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 if (!mLoop) {
887 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
888 }
889 break;
890 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700891 // Always log errors.
892 // ext1: Media framework error code.
893 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000894 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800895 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
896 if (mPrepareSync)
897 {
Steve Block3856b092011-10-20 11:56:00 +0100898 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899 mPrepareSync = false;
900 mPrepareStatus = ext1;
901 mSignal.signal();
902 send = false;
903 }
904 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700905 case MEDIA_INFO:
906 // ext1: Media framework error code.
907 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800908 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000909 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800910 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700911 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100913 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800914 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
915 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800916 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800917 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
918 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800919 }
920 else {
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800923 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924 }
925 break;
926 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928 break;
929 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 mVideoWidth = ext1;
932 mVideoHeight = ext2;
933 break;
Wei Jia52c28512017-09-13 18:17:51 -0700934 case MEDIA_NOTIFY_TIME:
935 ALOGV("Received notify time message");
936 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700937 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100938 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700939 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700940 case MEDIA_SUBTITLE_DATA:
941 ALOGV("Received subtitle data message");
942 break;
Robert Shih08528432015-04-08 09:06:54 -0700943 case MEDIA_META_DATA:
944 ALOGV("Received timed metadata message");
945 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 default:
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948 break;
949 }
950
951 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700952 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953
954 // this prevents re-entrant calls into client code
955 if ((listener != 0) && send) {
956 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700958 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100959 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800960 }
961}
962
James Dongdd172fc2010-01-15 18:13:58 -0800963void MediaPlayer::died()
964{
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800966 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
967}
968
Marco Nelissen6b74d672012-02-28 16:07:44 -0800969status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700970 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800971 if (mPlayer == NULL) {
972 return NO_INIT;
973 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700974
975 if (next != NULL && !(next->mCurrentState &
976 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
977 ALOGE("next player is not prepared");
978 return INVALID_OPERATION;
979 }
980
Marco Nelissen6b74d672012-02-28 16:07:44 -0800981 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
982}
983
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800984VolumeShaper::Status MediaPlayer::applyVolumeShaper(
985 const sp<VolumeShaper::Configuration>& configuration,
986 const sp<VolumeShaper::Operation>& operation)
987{
988 Mutex::Autolock _l(mLock);
989 if (mPlayer == nullptr) {
990 return VolumeShaper::Status(NO_INIT);
991 }
992 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
993 return status;
994}
995
996sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
997{
998 Mutex::Autolock _l(mLock);
999 if (mPlayer == nullptr) {
1000 return nullptr;
1001 }
1002 return mPlayer->getVolumeShaperState(id);
1003}
1004
Hassan Shojaniacefac142017-02-06 21:02:02 -08001005// Modular DRM
1006status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001007{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001008 // TODO change to ALOGV
1009 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1010 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001011 Mutex::Autolock _l(mLock);
1012 if (mPlayer == NULL) {
1013 return NO_INIT;
1014 }
1015
Hassan Shojania838be392017-05-23 14:14:24 -07001016 // Only allowed it in player's preparing/prepared state.
1017 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1018 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1019 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1020 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1021 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001022 return INVALID_OPERATION;
1023 }
1024
Hassan Shojaniacefac142017-02-06 21:02:02 -08001025 if (drmSessionId.isEmpty()) {
1026 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1027 return INVALID_OPERATION;
1028 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001029
Hassan Shojaniacefac142017-02-06 21:02:02 -08001030 // Passing down to mediaserver mainly for creating the crypto
1031 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1032 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1033
1034 // TODO change to ALOGV
1035 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1036
1037 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001038}
1039
1040status_t MediaPlayer::releaseDrm()
1041{
1042 Mutex::Autolock _l(mLock);
1043 if (mPlayer == NULL) {
1044 return NO_INIT;
1045 }
1046
Hassan Shojaniacefac142017-02-06 21:02:02 -08001047 // Not allowing releaseDrm in an active/resumable state
1048 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1049 MEDIA_PLAYER_PAUSED |
1050 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1051 MEDIA_PLAYER_STATE_ERROR)) {
1052 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001053 return INVALID_OPERATION;
1054 }
1055
Hassan Shojaniacefac142017-02-06 21:02:02 -08001056 status_t status = mPlayer->releaseDrm();
1057 // TODO change to ALOGV
1058 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1059 if (status != OK) {
1060 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1061 // Overriding to OK so the client proceed with its own cleanup
1062 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1063 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001064 }
1065
Hassan Shojaniacefac142017-02-06 21:02:02 -08001066 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001067}
1068
jiabin156c6872017-10-06 09:47:15 -07001069status_t MediaPlayer::setOutputDevice(audio_port_handle_t deviceId)
1070{
1071 Mutex::Autolock _l(mLock);
1072 if (mPlayer == NULL) {
1073 ALOGV("setOutputDevice: player not init");
1074 return NO_INIT;
1075 }
1076 return mPlayer->setOutputDevice(deviceId);
1077}
1078
1079audio_port_handle_t MediaPlayer::getRoutedDeviceId()
1080{
1081 Mutex::Autolock _l(mLock);
1082 if (mPlayer == NULL) {
1083 ALOGV("getRoutedDeviceId: player not init");
1084 return AUDIO_PORT_HANDLE_NONE;
1085 }
1086 audio_port_handle_t deviceId;
1087 status_t status = mPlayer->getRoutedDeviceId(&deviceId);
1088 if (status != NO_ERROR) {
1089 return AUDIO_PORT_HANDLE_NONE;
1090 }
1091 return deviceId;
1092}
1093
1094status_t MediaPlayer::enableAudioDeviceCallback(bool enabled)
1095{
1096 Mutex::Autolock _l(mLock);
1097 if (mPlayer == NULL) {
1098 ALOGV("addAudioDeviceCallback: player not init");
1099 return NO_INIT;
1100 }
1101 return mPlayer->enableAudioDeviceCallback(enabled);
1102}
1103
Glenn Kasten40bc9062015-03-20 09:09:33 -07001104} // namespace android