blob: b976721cd120580518bb0a13c0e4f601f854c774 [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"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080020
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070022#include <inttypes.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
Mathias Agopian75624082009-05-19 19:08:10 -070029#include <binder/IServiceManager.h>
30#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080032#include <gui/Surface.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034#include <media/mediaplayer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070035#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080036#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070037#include <media/AVSyncSettings.h>
Chris Watkins99f31602015-03-20 13:06:33 -070038#include <media/IDataSource.h>
Ray Essickdb122142017-01-25 17:51:55 -080039#include <media/MediaAnalyticsItem.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040
Mathias Agopian75624082009-05-19 19:08:10 -070041#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042
Andreas Huber2db84552010-01-28 11:19:57 -080043#include <utils/KeyedVector.h>
44#include <utils/String8.h>
45
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070047#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070048
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049namespace android {
50
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051MediaPlayer::MediaPlayer()
52{
Steve Block3856b092011-10-20 11:56:00 +010053 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054 mListener = NULL;
55 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070056 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070057 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080058 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080059 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080061 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062 mCurrentState = MEDIA_PLAYER_IDLE;
63 mPrepareSync = false;
64 mPrepareStatus = NO_ERROR;
65 mLoop = false;
66 mLeftVolume = mRightVolume = 1.0;
67 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070068 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080069 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Marco Nelissend457c972014-02-11 08:47:07 -080070 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070071 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080072 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073}
74
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075MediaPlayer::~MediaPlayer()
76{
Steve Block3856b092011-10-20 11:56:00 +010077 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070078 if (mAudioAttributesParcel != NULL) {
79 delete mAudioAttributesParcel;
80 mAudioAttributesParcel = NULL;
81 }
Marco Nelissend457c972014-02-11 08:47:07 -080082 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 disconnect();
84 IPCThreadState::self()->flushCommands();
85}
86
87void MediaPlayer::disconnect()
88{
Steve Block3856b092011-10-20 11:56:00 +010089 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090 sp<IMediaPlayer> p;
91 {
92 Mutex::Autolock _l(mLock);
93 p = mPlayer;
94 mPlayer.clear();
95 }
96
97 if (p != 0) {
98 p->disconnect();
99 }
100}
101
102// always call with lock held
103void MediaPlayer::clear_l()
104{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800106 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800108 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800110 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111}
112
113status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
114{
Steve Block3856b092011-10-20 11:56:00 +0100115 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116 Mutex::Autolock _l(mLock);
117 mListener = listener;
118 return NO_ERROR;
119}
120
121
Dave Burked681bbb2011-08-30 14:39:17 +0100122status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123{
124 status_t err = UNKNOWN_ERROR;
125 sp<IMediaPlayer> p;
126 { // scope for the lock
127 Mutex::Autolock _l(mLock);
128
Marco Nelissen83ff1432010-03-10 10:53:16 -0800129 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
130 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000131 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 return INVALID_OPERATION;
133 }
134
135 clear_l();
136 p = mPlayer;
137 mPlayer = player;
138 if (player != 0) {
139 mCurrentState = MEDIA_PLAYER_INITIALIZED;
Wei Jiaed320862017-01-18 14:03:40 -0800140 player->getDefaultBufferingSettings(&mCurrentBufferingSettings);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 err = NO_ERROR;
142 } else {
Wei Jiaed320862017-01-18 14:03:40 -0800143 mCurrentBufferingSettings = BufferingSettings();
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000144 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 }
146 }
147
148 if (p != 0) {
149 p->disconnect();
150 }
151
152 return err;
153}
154
Andreas Huber2db84552010-01-28 11:19:57 -0800155status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800156 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800157 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158{
Steve Block3856b092011-10-20 11:56:00 +0100159 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 status_t err = BAD_VALUE;
161 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700162 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800164 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800165 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800166 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100167 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100168 }
Dave Burke06620672011-09-06 20:39:47 +0100169 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 }
171 }
172 return err;
173}
174
175status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
176{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700177 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700179 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800181 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800182 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
183 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100184 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100185 }
Dave Burke06620672011-09-06 20:39:47 +0100186 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100187 }
188 return err;
189}
190
Chris Watkins99f31602015-03-20 13:06:33 -0700191status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
192{
193 ALOGV("setDataSource(IDataSource)");
194 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700195 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700196 if (service != 0) {
197 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
198 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
199 (NO_ERROR != player->setDataSource(source))) {
200 player.clear();
201 }
202 err = attachNewPlayer(player);
203 }
204 return err;
205}
206
Nicolas Catania1d187f12009-05-12 23:25:55 -0700207status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
208{
209 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800210 const bool hasBeenInitialized =
211 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
212 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
213 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700214 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700215 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700216 }
Wei Jia848ebc62016-03-23 14:06:46 -0700217 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700218 return INVALID_OPERATION;
219}
220
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700221status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
222{
Steve Blockb8a80522011-12-20 16:23:08 +0000223 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700224 Mutex::Autolock lock(mLock);
225 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700226 return NO_INIT;
227 }
228 return mPlayer->setMetadataFilter(filter);
229}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700230
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700231status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
232{
Steve Blockb8a80522011-12-20 16:23:08 +0000233 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700234 Mutex::Autolock lock(mLock);
235 if (mPlayer == NULL) {
236 return NO_INIT;
237 }
238 return mPlayer->getMetadata(update_only, apply_filter, metadata);
239}
240
Glenn Kasten11731182011-02-08 17:26:17 -0800241status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800242 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800243{
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800245 Mutex::Autolock _l(mLock);
246 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800247 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800248}
249
Wei Jiadc6f3402017-01-09 15:04:18 -0800250status_t MediaPlayer::getDefaultBufferingSettings(BufferingSettings* buffering /* nonnull */)
251{
252 ALOGV("getDefaultBufferingSettings");
253
254 Mutex::Autolock _l(mLock);
255 if (mPlayer == 0) {
256 return NO_INIT;
257 }
258 return mPlayer->getDefaultBufferingSettings(buffering);
259}
260
Wei Jiaed320862017-01-18 14:03:40 -0800261status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
262{
263 ALOGV("getBufferingSettings");
264
265 Mutex::Autolock _l(mLock);
266 if (mPlayer == 0) {
267 return NO_INIT;
268 }
269 *buffering = mCurrentBufferingSettings;
270 return NO_ERROR;
271}
272
Wei Jiadc6f3402017-01-09 15:04:18 -0800273status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering)
274{
275 ALOGV("setBufferingSettings");
276
277 Mutex::Autolock _l(mLock);
278 if (mPlayer == 0) {
279 return NO_INIT;
280 }
Wei Jiaed320862017-01-18 14:03:40 -0800281 status_t err = mPlayer->setBufferingSettings(buffering);
282 if (err == NO_ERROR) {
283 mCurrentBufferingSettings = buffering;
284 }
285 return err;
Wei Jiadc6f3402017-01-09 15:04:18 -0800286}
287
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288// must call with lock held
289status_t MediaPlayer::prepareAsync_l()
290{
Glenn Kastenb187de12014-12-30 08:18:15 -0800291 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700292 if (mAudioAttributesParcel != NULL) {
293 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700294 } else {
295 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700296 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297 mCurrentState = MEDIA_PLAYER_PREPARING;
298 return mPlayer->prepareAsync();
299 }
Wei Jia848ebc62016-03-23 14:06:46 -0700300 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 return INVALID_OPERATION;
302}
303
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700304// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
305// one defined in the Android framework and one provided by the implementation
306// that generated the error. The sync version of prepare returns only 1 error
307// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308status_t MediaPlayer::prepare()
309{
Steve Block3856b092011-10-20 11:56:00 +0100310 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700312 mLockThreadId = getThreadId();
313 if (mPrepareSync) {
314 mLockThreadId = 0;
315 return -EALREADY;
316 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 mPrepareSync = true;
318 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700319 if (ret != NO_ERROR) {
320 mLockThreadId = 0;
321 return ret;
322 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323
324 if (mPrepareSync) {
325 mSignal.wait(mLock); // wait for prepare done
326 mPrepareSync = false;
327 }
Steve Block3856b092011-10-20 11:56:00 +0100328 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700329 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 return mPrepareStatus;
331}
332
333status_t MediaPlayer::prepareAsync()
334{
Steve Block3856b092011-10-20 11:56:00 +0100335 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 Mutex::Autolock _l(mLock);
337 return prepareAsync_l();
338}
339
340status_t MediaPlayer::start()
341{
Steve Block3856b092011-10-20 11:56:00 +0100342 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700343
344 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700346
347 mLockThreadId = getThreadId();
348
349 if (mCurrentState & MEDIA_PLAYER_STARTED) {
350 ret = NO_ERROR;
351 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
353 mPlayer->setLooping(mLoop);
354 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700355 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700357 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 if (ret != NO_ERROR) {
359 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
360 } else {
361 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100362 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 }
364 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700365 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700366 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700367 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700369
370 mLockThreadId = 0;
371
372 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373}
374
375status_t MediaPlayer::stop()
376{
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 Mutex::Autolock _l(mLock);
379 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
380 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
381 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
382 status_t ret = mPlayer->stop();
383 if (ret != NO_ERROR) {
384 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
385 } else {
386 mCurrentState = MEDIA_PLAYER_STOPPED;
387 }
388 return ret;
389 }
Wei Jia848ebc62016-03-23 14:06:46 -0700390 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 return INVALID_OPERATION;
392}
393
394status_t MediaPlayer::pause()
395{
Steve Block3856b092011-10-20 11:56:00 +0100396 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800398 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 return NO_ERROR;
400 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
401 status_t ret = mPlayer->pause();
402 if (ret != NO_ERROR) {
403 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
404 } else {
405 mCurrentState = MEDIA_PLAYER_PAUSED;
406 }
407 return ret;
408 }
Wei Jia848ebc62016-03-23 14:06:46 -0700409 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 return INVALID_OPERATION;
411}
412
413bool MediaPlayer::isPlaying()
414{
415 Mutex::Autolock _l(mLock);
416 if (mPlayer != 0) {
417 bool temp = false;
418 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000421 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700423 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
424 ALOGE("internal/external state mismatch corrected");
425 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 }
427 return temp;
428 }
Steve Block3856b092011-10-20 11:56:00 +0100429 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 return false;
431}
432
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700433status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800434{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700435 ALOGV("setPlaybackSettings: %f %f %d %d",
436 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
437 // Negative speed and pitch does not make sense. Further validation will
438 // be done by the respective mediaplayers.
439 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800440 return BAD_VALUE;
441 }
442 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700443 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
444 return INVALID_OPERATION;
445 }
Wei Jia5be109f2016-06-10 16:15:07 -0700446
447 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
448 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
449 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
450 mPlayer->setLooping(mLoop);
451 mPlayer->setVolume(mLeftVolume, mRightVolume);
452 mPlayer->setAuxEffectSendLevel(mSendLevel);
453 }
454
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700455 status_t err = mPlayer->setPlaybackSettings(rate);
456 if (err == OK) {
457 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
458 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700459 } else if (rate.mSpeed != 0.f
460 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
461 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700462 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800463 }
Wei Jia98160162015-02-04 17:01:11 -0800464 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700465 return err;
466}
467
468status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
469{
470 Mutex::Autolock _l(mLock);
471 if (mPlayer == 0) return INVALID_OPERATION;
472 return mPlayer->getPlaybackSettings(rate);
473}
474
475status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
476{
477 ALOGV("setSyncSettings: %u %u %f %f",
478 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
479 Mutex::Autolock _l(mLock);
480 if (mPlayer == 0) return INVALID_OPERATION;
481 return mPlayer->setSyncSettings(sync, videoFpsHint);
482}
483
484status_t MediaPlayer::getSyncSettings(
485 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
486{
487 Mutex::Autolock _l(mLock);
488 if (mPlayer == 0) return INVALID_OPERATION;
489 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800490}
491
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492status_t MediaPlayer::getVideoWidth(int *w)
493{
Steve Block3856b092011-10-20 11:56:00 +0100494 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495 Mutex::Autolock _l(mLock);
496 if (mPlayer == 0) return INVALID_OPERATION;
497 *w = mVideoWidth;
498 return NO_ERROR;
499}
500
501status_t MediaPlayer::getVideoHeight(int *h)
502{
Steve Block3856b092011-10-20 11:56:00 +0100503 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 Mutex::Autolock _l(mLock);
505 if (mPlayer == 0) return INVALID_OPERATION;
506 *h = mVideoHeight;
507 return NO_ERROR;
508}
509
510status_t MediaPlayer::getCurrentPosition(int *msec)
511{
Steve Block3856b092011-10-20 11:56:00 +0100512 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 Mutex::Autolock _l(mLock);
514 if (mPlayer != 0) {
515 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100516 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517 *msec = mCurrentPosition;
518 return NO_ERROR;
519 }
520 return mPlayer->getCurrentPosition(msec);
521 }
522 return INVALID_OPERATION;
523}
524
525status_t MediaPlayer::getDuration_l(int *msec)
526{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800527 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800528 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
529 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800531 int durationMs;
532 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700533
534 if (ret != OK) {
535 // Do not enter error state just because no duration was available.
536 durationMs = -1;
537 ret = OK;
538 }
539
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800540 if (msec) {
541 *msec = durationMs;
542 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543 return ret;
544 }
Wei Jia848ebc62016-03-23 14:06:46 -0700545 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
546 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 return INVALID_OPERATION;
548}
549
550status_t MediaPlayer::getDuration(int *msec)
551{
552 Mutex::Autolock _l(mLock);
553 return getDuration_l(msec);
554}
555
Wei Jiac5de0912016-11-18 10:22:14 -0800556status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557{
Wei Jiac5de0912016-11-18 10:22:14 -0800558 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800559 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
560 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000562 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800565
566 int durationMs;
567 status_t err = mPlayer->getDuration(&durationMs);
568
569 if (err != OK) {
570 ALOGW("Stream has no duration and is therefore not seekable.");
571 return err;
572 }
573
574 if (msec > durationMs) {
575 ALOGW("Attempt to seek to past end of file: request = %d, "
576 "durationMs = %d",
577 msec,
578 durationMs);
579
580 msec = durationMs;
581 }
582
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583 // cache duration
584 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800585 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800588 mSeekMode = mode;
589 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590 }
591 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800592 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593 return NO_ERROR;
594 }
595 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800596 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
597 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 return INVALID_OPERATION;
599}
600
Wei Jiac5de0912016-11-18 10:22:14 -0800601status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700603 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800605 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700606 mLockThreadId = 0;
607
608 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609}
610
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700611status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 mLoop = false;
614 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
615 mPrepareSync = false;
616 if (mPlayer != 0) {
617 status_t ret = mPlayer->reset();
618 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000619 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
621 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700622 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 mCurrentState = MEDIA_PLAYER_IDLE;
624 }
James Donga1680bc2010-11-18 12:23:58 -0800625 // setDataSource has to be called again to create a
626 // new mediaplayer.
627 mPlayer = 0;
Wei Jiaed320862017-01-18 14:03:40 -0800628 mCurrentBufferingSettings = BufferingSettings();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629 return ret;
630 }
631 clear_l();
632 return NO_ERROR;
633}
634
John Grossmanc795b642012-02-22 15:38:35 -0800635status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
636 Mutex::Autolock _l(mLock);
637
638 if (player == NULL) {
639 return UNKNOWN_ERROR;
640 }
641
642 if (mRetransmitEndpointValid) {
643 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
644 }
645
646 return OK;
647}
648
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700649status_t MediaPlayer::reset()
650{
Steve Block3856b092011-10-20 11:56:00 +0100651 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700652 Mutex::Autolock _l(mLock);
653 return reset_l();
654}
655
Glenn Kastenfff6d712012-01-12 16:38:12 -0800656status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657{
Steve Block3856b092011-10-20 11:56:00 +0100658 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659 Mutex::Autolock _l(mLock);
660 if (mStreamType == type) return NO_ERROR;
661 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
662 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
663 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000664 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 return INVALID_OPERATION;
666 }
667 // cache
668 mStreamType = type;
669 return OK;
670}
671
John Spurlockde9453f2014-03-19 13:05:45 -0400672status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
673{
674 ALOGV("getAudioStreamType");
675 Mutex::Autolock _l(mLock);
676 *type = mStreamType;
677 return OK;
678}
679
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680status_t MediaPlayer::setLooping(int loop)
681{
Steve Block3856b092011-10-20 11:56:00 +0100682 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683 Mutex::Autolock _l(mLock);
684 mLoop = (loop != 0);
685 if (mPlayer != 0) {
686 return mPlayer->setLooping(loop);
687 }
688 return OK;
689}
690
691bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 Mutex::Autolock _l(mLock);
694 if (mPlayer != 0) {
695 return mLoop;
696 }
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 return false;
699}
700
701status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
702{
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 Mutex::Autolock _l(mLock);
705 mLeftVolume = leftVolume;
706 mRightVolume = rightVolume;
707 if (mPlayer != 0) {
708 return mPlayer->setVolume(leftVolume, rightVolume);
709 }
710 return OK;
711}
712
Glenn Kastend848eb42016-03-08 13:42:11 -0800713status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700714{
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700716 Mutex::Autolock _l(mLock);
717 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000718 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700719 return INVALID_OPERATION;
720 }
721 if (sessionId < 0) {
722 return BAD_VALUE;
723 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700724 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800725 AudioSystem::acquireAudioSessionId(sessionId, -1);
726 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700727 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700728 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700729 return NO_ERROR;
730}
731
Glenn Kastend848eb42016-03-08 13:42:11 -0800732audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700733{
734 Mutex::Autolock _l(mLock);
735 return mAudioSessionId;
736}
737
Eric Laurent2beeb502010-07-16 07:43:46 -0700738status_t MediaPlayer::setAuxEffectSendLevel(float level)
739{
Steve Block3856b092011-10-20 11:56:00 +0100740 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700741 Mutex::Autolock _l(mLock);
742 mSendLevel = level;
743 if (mPlayer != 0) {
744 return mPlayer->setAuxEffectSendLevel(level);
745 }
746 return OK;
747}
748
749status_t MediaPlayer::attachAuxEffect(int effectId)
750{
Steve Block3856b092011-10-20 11:56:00 +0100751 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700752 Mutex::Autolock _l(mLock);
753 if (mPlayer == 0 ||
754 (mCurrentState & MEDIA_PLAYER_IDLE) ||
755 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700756 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700757 return INVALID_OPERATION;
758 }
759
760 return mPlayer->attachAuxEffect(effectId);
761}
762
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700763// always call with lock held
764status_t MediaPlayer::checkStateForKeySet_l(int key)
765{
766 switch(key) {
767 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
768 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
769 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
770 // Can't change the audio attributes after prepare
771 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
772 return INVALID_OPERATION;
773 }
774 break;
775 default:
776 // parameter doesn't require player state check
777 break;
778 }
779 return OK;
780}
781
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700782status_t MediaPlayer::setParameter(int key, const Parcel& request)
783{
Steve Block3856b092011-10-20 11:56:00 +0100784 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700785 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700786 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700787 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700788 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700789 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700790 switch (key) {
791 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700792 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700793 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
794 mAudioAttributesParcel = new Parcel();
795 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700796 status = OK;
797 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700798 default:
Eric Laurent43562692015-07-15 16:49:07 -0700799 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
800 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700801 }
Eric Laurent43562692015-07-15 16:49:07 -0700802
803 if (mPlayer != NULL) {
804 status = mPlayer->setParameter(key, request);
805 }
806 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700807}
808
809status_t MediaPlayer::getParameter(int key, Parcel *reply)
810{
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700812 Mutex::Autolock _l(mLock);
813 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800814 status_t status = mPlayer->getParameter(key, reply);
815 if (status != OK) {
816 ALOGD("getParameter returns %d", status);
817 }
818 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700819 }
Steve Block3856b092011-10-20 11:56:00 +0100820 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700821 return INVALID_OPERATION;
822}
823
John Grossmanc795b642012-02-22 15:38:35 -0800824status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
825 uint16_t port) {
826 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
827 addrString ? addrString : "(null)", port);
828
829 Mutex::Autolock _l(mLock);
830 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
831 return INVALID_OPERATION;
832
833 if (NULL == addrString) {
834 mRetransmitEndpointValid = false;
835 return OK;
836 }
837
838 struct in_addr saddr;
839 if(!inet_aton(addrString, &saddr)) {
840 return BAD_VALUE;
841 }
842
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800843 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800844 mRetransmitEndpoint.sin_family = AF_INET;
845 mRetransmitEndpoint.sin_addr = saddr;
846 mRetransmitEndpoint.sin_port = htons(port);
847 mRetransmitEndpointValid = true;
848
849 return OK;
850}
851
Gloria Wangb483c472011-04-11 17:23:27 -0700852void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853{
Steve Block3856b092011-10-20 11:56:00 +0100854 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700856 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800857
858 // TODO: In the future, we might be on the same thread if the app is
859 // running in the same process as the media server. In that case,
860 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700861 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700862 // The threadId hack below works around this for the care of prepare,
863 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700864 // FIXME: Remember, this is a hack, it's not even a hack that is applied
865 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700866 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700867 mLock.lock();
868 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700869 }
Jason Sams1af452f2009-03-24 18:45:22 -0700870
Eric Laurent3b268442010-08-03 07:49:49 -0700871 // Allows calls from JNI in idle state to notify errors
872 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700874 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800875 return;
876 }
877
878 switch (msg) {
879 case MEDIA_NOP: // interface test message
880 break;
881 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800882 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800883 mCurrentState = MEDIA_PLAYER_PREPARED;
884 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 mPrepareSync = false;
887 mPrepareStatus = NO_ERROR;
888 mSignal.signal();
889 }
890 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800891 case MEDIA_DRM_INFO:
892 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
893 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800894 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700896 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000897 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700898 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899 if (!mLoop) {
900 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
901 }
902 break;
903 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700904 // Always log errors.
905 // ext1: Media framework error code.
906 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000907 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
909 if (mPrepareSync)
910 {
Steve Block3856b092011-10-20 11:56:00 +0100911 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912 mPrepareSync = false;
913 mPrepareStatus = ext1;
914 mSignal.signal();
915 send = false;
916 }
917 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700918 case MEDIA_INFO:
919 // ext1: Media framework error code.
920 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800921 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000922 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800923 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700924 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100926 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800927 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
928 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800930 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
931 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 }
933 else {
Steve Block3856b092011-10-20 11:56:00 +0100934 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800936 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937 }
938 break;
939 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 break;
942 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 mVideoWidth = ext1;
945 mVideoHeight = ext2;
946 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700947 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700949 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700950 case MEDIA_SUBTITLE_DATA:
951 ALOGV("Received subtitle data message");
952 break;
Robert Shih08528432015-04-08 09:06:54 -0700953 case MEDIA_META_DATA:
954 ALOGV("Received timed metadata message");
955 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 default:
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 break;
959 }
960
961 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700962 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800963
964 // this prevents re-entrant calls into client code
965 if ((listener != 0) && send) {
966 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100967 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700968 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100969 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 }
971}
972
James Dongdd172fc2010-01-15 18:13:58 -0800973void MediaPlayer::died()
974{
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800976 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
977}
978
Marco Nelissen6b74d672012-02-28 16:07:44 -0800979status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700980 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800981 if (mPlayer == NULL) {
982 return NO_INIT;
983 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700984
985 if (next != NULL && !(next->mCurrentState &
986 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
987 ALOGE("next player is not prepared");
988 return INVALID_OPERATION;
989 }
990
Marco Nelissen6b74d672012-02-28 16:07:44 -0800991 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
992}
993
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800994VolumeShaper::Status MediaPlayer::applyVolumeShaper(
995 const sp<VolumeShaper::Configuration>& configuration,
996 const sp<VolumeShaper::Operation>& operation)
997{
998 Mutex::Autolock _l(mLock);
999 if (mPlayer == nullptr) {
1000 return VolumeShaper::Status(NO_INIT);
1001 }
1002 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
1003 return status;
1004}
1005
1006sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
1007{
1008 Mutex::Autolock _l(mLock);
1009 if (mPlayer == nullptr) {
1010 return nullptr;
1011 }
1012 return mPlayer->getVolumeShaperState(id);
1013}
1014
Hassan Shojaniacefac142017-02-06 21:02:02 -08001015// Modular DRM
1016status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001017{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001018 // TODO change to ALOGV
1019 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1020 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001021 Mutex::Autolock _l(mLock);
1022 if (mPlayer == NULL) {
1023 return NO_INIT;
1024 }
1025
Hassan Shojania838be392017-05-23 14:14:24 -07001026 // Only allowed it in player's preparing/prepared state.
1027 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1028 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1029 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1030 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1031 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001032 return INVALID_OPERATION;
1033 }
1034
Hassan Shojaniacefac142017-02-06 21:02:02 -08001035 if (drmSessionId.isEmpty()) {
1036 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1037 return INVALID_OPERATION;
1038 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001039
Hassan Shojaniacefac142017-02-06 21:02:02 -08001040 // Passing down to mediaserver mainly for creating the crypto
1041 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1042 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1043
1044 // TODO change to ALOGV
1045 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1046
1047 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001048}
1049
1050status_t MediaPlayer::releaseDrm()
1051{
1052 Mutex::Autolock _l(mLock);
1053 if (mPlayer == NULL) {
1054 return NO_INIT;
1055 }
1056
Hassan Shojaniacefac142017-02-06 21:02:02 -08001057 // Not allowing releaseDrm in an active/resumable state
1058 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1059 MEDIA_PLAYER_PAUSED |
1060 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1061 MEDIA_PLAYER_STATE_ERROR)) {
1062 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001063 return INVALID_OPERATION;
1064 }
1065
Hassan Shojaniacefac142017-02-06 21:02:02 -08001066 status_t status = mPlayer->releaseDrm();
1067 // TODO change to ALOGV
1068 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1069 if (status != OK) {
1070 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1071 // Overriding to OK so the client proceed with its own cleanup
1072 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1073 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001074 }
1075
Hassan Shojaniacefac142017-02-06 21:02:02 -08001076 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001077}
1078
Glenn Kasten40bc9062015-03-20 09:09:33 -07001079} // namespace android