blob: efe947adc8b1b38a06f62f8b6e14d8168f3db09b [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");
Wonsik Kim5f648972017-07-21 15:42:59 -0700652 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700653 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700654 status_t result = reset_l();
655 mLockThreadId = 0;
656
657 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700658}
659
Glenn Kastenfff6d712012-01-12 16:38:12 -0800660status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661{
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663 Mutex::Autolock _l(mLock);
664 if (mStreamType == type) return NO_ERROR;
665 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
666 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
667 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000668 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669 return INVALID_OPERATION;
670 }
671 // cache
672 mStreamType = type;
673 return OK;
674}
675
John Spurlockde9453f2014-03-19 13:05:45 -0400676status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
677{
678 ALOGV("getAudioStreamType");
679 Mutex::Autolock _l(mLock);
680 *type = mStreamType;
681 return OK;
682}
683
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684status_t MediaPlayer::setLooping(int loop)
685{
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687 Mutex::Autolock _l(mLock);
688 mLoop = (loop != 0);
689 if (mPlayer != 0) {
690 return mPlayer->setLooping(loop);
691 }
692 return OK;
693}
694
695bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 Mutex::Autolock _l(mLock);
698 if (mPlayer != 0) {
699 return mLoop;
700 }
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 return false;
703}
704
705status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
706{
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 Mutex::Autolock _l(mLock);
709 mLeftVolume = leftVolume;
710 mRightVolume = rightVolume;
711 if (mPlayer != 0) {
712 return mPlayer->setVolume(leftVolume, rightVolume);
713 }
714 return OK;
715}
716
Glenn Kastend848eb42016-03-08 13:42:11 -0800717status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700718{
Steve Block3856b092011-10-20 11:56:00 +0100719 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700720 Mutex::Autolock _l(mLock);
721 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000722 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700723 return INVALID_OPERATION;
724 }
725 if (sessionId < 0) {
726 return BAD_VALUE;
727 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700728 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800729 AudioSystem::acquireAudioSessionId(sessionId, -1);
730 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700731 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700732 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700733 return NO_ERROR;
734}
735
Glenn Kastend848eb42016-03-08 13:42:11 -0800736audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700737{
738 Mutex::Autolock _l(mLock);
739 return mAudioSessionId;
740}
741
Eric Laurent2beeb502010-07-16 07:43:46 -0700742status_t MediaPlayer::setAuxEffectSendLevel(float level)
743{
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700745 Mutex::Autolock _l(mLock);
746 mSendLevel = level;
747 if (mPlayer != 0) {
748 return mPlayer->setAuxEffectSendLevel(level);
749 }
750 return OK;
751}
752
753status_t MediaPlayer::attachAuxEffect(int effectId)
754{
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700756 Mutex::Autolock _l(mLock);
757 if (mPlayer == 0 ||
758 (mCurrentState & MEDIA_PLAYER_IDLE) ||
759 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700760 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700761 return INVALID_OPERATION;
762 }
763
764 return mPlayer->attachAuxEffect(effectId);
765}
766
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700767// always call with lock held
768status_t MediaPlayer::checkStateForKeySet_l(int key)
769{
770 switch(key) {
771 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
772 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
773 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
774 // Can't change the audio attributes after prepare
775 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
776 return INVALID_OPERATION;
777 }
778 break;
779 default:
780 // parameter doesn't require player state check
781 break;
782 }
783 return OK;
784}
785
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700786status_t MediaPlayer::setParameter(int key, const Parcel& request)
787{
Steve Block3856b092011-10-20 11:56:00 +0100788 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700789 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700790 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700791 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700792 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700793 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700794 switch (key) {
795 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700796 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700797 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
798 mAudioAttributesParcel = new Parcel();
799 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700800 status = OK;
801 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700802 default:
Eric Laurent43562692015-07-15 16:49:07 -0700803 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
804 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700805 }
Eric Laurent43562692015-07-15 16:49:07 -0700806
807 if (mPlayer != NULL) {
808 status = mPlayer->setParameter(key, request);
809 }
810 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700811}
812
813status_t MediaPlayer::getParameter(int key, Parcel *reply)
814{
Steve Block3856b092011-10-20 11:56:00 +0100815 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700816 Mutex::Autolock _l(mLock);
817 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800818 status_t status = mPlayer->getParameter(key, reply);
819 if (status != OK) {
820 ALOGD("getParameter returns %d", status);
821 }
822 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700823 }
Steve Block3856b092011-10-20 11:56:00 +0100824 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700825 return INVALID_OPERATION;
826}
827
John Grossmanc795b642012-02-22 15:38:35 -0800828status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
829 uint16_t port) {
830 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
831 addrString ? addrString : "(null)", port);
832
833 Mutex::Autolock _l(mLock);
834 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
835 return INVALID_OPERATION;
836
837 if (NULL == addrString) {
838 mRetransmitEndpointValid = false;
839 return OK;
840 }
841
842 struct in_addr saddr;
843 if(!inet_aton(addrString, &saddr)) {
844 return BAD_VALUE;
845 }
846
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800847 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800848 mRetransmitEndpoint.sin_family = AF_INET;
849 mRetransmitEndpoint.sin_addr = saddr;
850 mRetransmitEndpoint.sin_port = htons(port);
851 mRetransmitEndpointValid = true;
852
853 return OK;
854}
855
Gloria Wangb483c472011-04-11 17:23:27 -0700856void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800857{
Steve Block3856b092011-10-20 11:56:00 +0100858 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800859 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700860 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861
862 // TODO: In the future, we might be on the same thread if the app is
863 // running in the same process as the media server. In that case,
864 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700865 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700866 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700867 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700868 // FIXME: Remember, this is a hack, it's not even a hack that is applied
869 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700870 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700871 mLock.lock();
872 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700873 }
Jason Sams1af452f2009-03-24 18:45:22 -0700874
Eric Laurent3b268442010-08-03 07:49:49 -0700875 // Allows calls from JNI in idle state to notify errors
876 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700878 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800879 return;
880 }
881
882 switch (msg) {
883 case MEDIA_NOP: // interface test message
884 break;
885 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800886 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800887 mCurrentState = MEDIA_PLAYER_PREPARED;
888 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100889 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800890 mPrepareSync = false;
891 mPrepareStatus = NO_ERROR;
892 mSignal.signal();
893 }
894 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800895 case MEDIA_DRM_INFO:
896 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
897 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700900 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000901 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700902 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 if (!mLoop) {
904 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
905 }
906 break;
907 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700908 // Always log errors.
909 // ext1: Media framework error code.
910 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000911 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
913 if (mPrepareSync)
914 {
Steve Block3856b092011-10-20 11:56:00 +0100915 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800916 mPrepareSync = false;
917 mPrepareStatus = ext1;
918 mSignal.signal();
919 send = false;
920 }
921 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700922 case MEDIA_INFO:
923 // ext1: Media framework error code.
924 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800925 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000926 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800927 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700928 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800931 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
932 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800933 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800934 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
935 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936 }
937 else {
Steve Block3856b092011-10-20 11:56:00 +0100938 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800939 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800940 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 }
942 break;
943 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 break;
946 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948 mVideoWidth = ext1;
949 mVideoHeight = ext2;
950 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700951 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700953 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700954 case MEDIA_SUBTITLE_DATA:
955 ALOGV("Received subtitle data message");
956 break;
Robert Shih08528432015-04-08 09:06:54 -0700957 case MEDIA_META_DATA:
958 ALOGV("Received timed metadata message");
959 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800960 default:
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 break;
963 }
964
965 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700966 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967
968 // this prevents re-entrant calls into client code
969 if ((listener != 0) && send) {
970 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700972 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100973 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 }
975}
976
James Dongdd172fc2010-01-15 18:13:58 -0800977void MediaPlayer::died()
978{
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800980 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
981}
982
Marco Nelissen6b74d672012-02-28 16:07:44 -0800983status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700984 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800985 if (mPlayer == NULL) {
986 return NO_INIT;
987 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700988
989 if (next != NULL && !(next->mCurrentState &
990 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
991 ALOGE("next player is not prepared");
992 return INVALID_OPERATION;
993 }
994
Marco Nelissen6b74d672012-02-28 16:07:44 -0800995 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
996}
997
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800998VolumeShaper::Status MediaPlayer::applyVolumeShaper(
999 const sp<VolumeShaper::Configuration>& configuration,
1000 const sp<VolumeShaper::Operation>& operation)
1001{
1002 Mutex::Autolock _l(mLock);
1003 if (mPlayer == nullptr) {
1004 return VolumeShaper::Status(NO_INIT);
1005 }
1006 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
1007 return status;
1008}
1009
1010sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
1011{
1012 Mutex::Autolock _l(mLock);
1013 if (mPlayer == nullptr) {
1014 return nullptr;
1015 }
1016 return mPlayer->getVolumeShaperState(id);
1017}
1018
Hassan Shojaniacefac142017-02-06 21:02:02 -08001019// Modular DRM
1020status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001021{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001022 // TODO change to ALOGV
1023 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1024 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001025 Mutex::Autolock _l(mLock);
1026 if (mPlayer == NULL) {
1027 return NO_INIT;
1028 }
1029
Hassan Shojania838be392017-05-23 14:14:24 -07001030 // Only allowed it in player's preparing/prepared state.
1031 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1032 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1033 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1034 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1035 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001036 return INVALID_OPERATION;
1037 }
1038
Hassan Shojaniacefac142017-02-06 21:02:02 -08001039 if (drmSessionId.isEmpty()) {
1040 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1041 return INVALID_OPERATION;
1042 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001043
Hassan Shojaniacefac142017-02-06 21:02:02 -08001044 // Passing down to mediaserver mainly for creating the crypto
1045 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1046 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1047
1048 // TODO change to ALOGV
1049 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1050
1051 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001052}
1053
1054status_t MediaPlayer::releaseDrm()
1055{
1056 Mutex::Autolock _l(mLock);
1057 if (mPlayer == NULL) {
1058 return NO_INIT;
1059 }
1060
Hassan Shojaniacefac142017-02-06 21:02:02 -08001061 // Not allowing releaseDrm in an active/resumable state
1062 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1063 MEDIA_PLAYER_PAUSED |
1064 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1065 MEDIA_PLAYER_STATE_ERROR)) {
1066 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001067 return INVALID_OPERATION;
1068 }
1069
Hassan Shojaniacefac142017-02-06 21:02:02 -08001070 status_t status = mPlayer->releaseDrm();
1071 // TODO change to ALOGV
1072 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1073 if (status != OK) {
1074 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1075 // Overriding to OK so the client proceed with its own cleanup
1076 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1077 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001078 }
1079
Hassan Shojaniacefac142017-02-06 21:02:02 -08001080 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001081}
1082
Glenn Kasten40bc9062015-03-20 09:09:33 -07001083} // namespace android