blob: a6cdb13f8bff3484455c2bb6e371c9fa822ee866 [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
Ivan Lozano8cf3a072017-08-09 09:01:33 -070051using media::VolumeShaper;
52
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053MediaPlayer::MediaPlayer()
54{
Steve Block3856b092011-10-20 11:56:00 +010055 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056 mListener = NULL;
57 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070058 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070059 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080061 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080063 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 mCurrentState = MEDIA_PLAYER_IDLE;
65 mPrepareSync = false;
66 mPrepareStatus = NO_ERROR;
67 mLoop = false;
68 mLeftVolume = mRightVolume = 1.0;
69 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070070 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080071 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Marco Nelissend457c972014-02-11 08:47:07 -080072 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070073 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080074 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075}
76
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077MediaPlayer::~MediaPlayer()
78{
Steve Block3856b092011-10-20 11:56:00 +010079 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070080 if (mAudioAttributesParcel != NULL) {
81 delete mAudioAttributesParcel;
82 mAudioAttributesParcel = NULL;
83 }
Marco Nelissend457c972014-02-11 08:47:07 -080084 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 disconnect();
86 IPCThreadState::self()->flushCommands();
87}
88
89void MediaPlayer::disconnect()
90{
Steve Block3856b092011-10-20 11:56:00 +010091 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 sp<IMediaPlayer> p;
93 {
94 Mutex::Autolock _l(mLock);
95 p = mPlayer;
96 mPlayer.clear();
97 }
98
99 if (p != 0) {
100 p->disconnect();
101 }
102}
103
104// always call with lock held
105void MediaPlayer::clear_l()
106{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800108 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800110 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800112 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113}
114
115status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
116{
Steve Block3856b092011-10-20 11:56:00 +0100117 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 Mutex::Autolock _l(mLock);
119 mListener = listener;
120 return NO_ERROR;
121}
122
123
Dave Burked681bbb2011-08-30 14:39:17 +0100124status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125{
126 status_t err = UNKNOWN_ERROR;
127 sp<IMediaPlayer> p;
128 { // scope for the lock
129 Mutex::Autolock _l(mLock);
130
Marco Nelissen83ff1432010-03-10 10:53:16 -0800131 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
132 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000133 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 return INVALID_OPERATION;
135 }
136
137 clear_l();
138 p = mPlayer;
139 mPlayer = player;
140 if (player != 0) {
141 mCurrentState = MEDIA_PLAYER_INITIALIZED;
Wei Jiaed320862017-01-18 14:03:40 -0800142 player->getDefaultBufferingSettings(&mCurrentBufferingSettings);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 err = NO_ERROR;
144 } else {
Wei Jiaed320862017-01-18 14:03:40 -0800145 mCurrentBufferingSettings = BufferingSettings();
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000146 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 }
148 }
149
150 if (p != 0) {
151 p->disconnect();
152 }
153
154 return err;
155}
156
Andreas Huber2db84552010-01-28 11:19:57 -0800157status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800158 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800159 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160{
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162 status_t err = BAD_VALUE;
163 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700164 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800166 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800167 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800168 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100169 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100170 }
Dave Burke06620672011-09-06 20:39:47 +0100171 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 }
173 }
174 return err;
175}
176
177status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
178{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700179 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700181 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800183 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800184 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
185 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100186 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100187 }
Dave Burke06620672011-09-06 20:39:47 +0100188 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100189 }
190 return err;
191}
192
Chris Watkins99f31602015-03-20 13:06:33 -0700193status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
194{
195 ALOGV("setDataSource(IDataSource)");
196 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700197 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700198 if (service != 0) {
199 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
200 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
201 (NO_ERROR != player->setDataSource(source))) {
202 player.clear();
203 }
204 err = attachNewPlayer(player);
205 }
206 return err;
207}
208
Nicolas Catania1d187f12009-05-12 23:25:55 -0700209status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
210{
211 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800212 const bool hasBeenInitialized =
213 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
214 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
215 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700216 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700217 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700218 }
Wei Jia848ebc62016-03-23 14:06:46 -0700219 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700220 return INVALID_OPERATION;
221}
222
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700223status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
224{
Steve Blockb8a80522011-12-20 16:23:08 +0000225 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700226 Mutex::Autolock lock(mLock);
227 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700228 return NO_INIT;
229 }
230 return mPlayer->setMetadataFilter(filter);
231}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700232
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700233status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
234{
Steve Blockb8a80522011-12-20 16:23:08 +0000235 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700236 Mutex::Autolock lock(mLock);
237 if (mPlayer == NULL) {
238 return NO_INIT;
239 }
240 return mPlayer->getMetadata(update_only, apply_filter, metadata);
241}
242
Glenn Kasten11731182011-02-08 17:26:17 -0800243status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800244 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800245{
Steve Block3856b092011-10-20 11:56:00 +0100246 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800247 Mutex::Autolock _l(mLock);
248 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800249 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800250}
251
Wei Jiadc6f3402017-01-09 15:04:18 -0800252status_t MediaPlayer::getDefaultBufferingSettings(BufferingSettings* buffering /* nonnull */)
253{
254 ALOGV("getDefaultBufferingSettings");
255
256 Mutex::Autolock _l(mLock);
257 if (mPlayer == 0) {
258 return NO_INIT;
259 }
260 return mPlayer->getDefaultBufferingSettings(buffering);
261}
262
Wei Jiaed320862017-01-18 14:03:40 -0800263status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
264{
265 ALOGV("getBufferingSettings");
266
267 Mutex::Autolock _l(mLock);
268 if (mPlayer == 0) {
269 return NO_INIT;
270 }
271 *buffering = mCurrentBufferingSettings;
272 return NO_ERROR;
273}
274
Wei Jiadc6f3402017-01-09 15:04:18 -0800275status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering)
276{
277 ALOGV("setBufferingSettings");
278
279 Mutex::Autolock _l(mLock);
280 if (mPlayer == 0) {
281 return NO_INIT;
282 }
Wei Jiaed320862017-01-18 14:03:40 -0800283 status_t err = mPlayer->setBufferingSettings(buffering);
284 if (err == NO_ERROR) {
285 mCurrentBufferingSettings = buffering;
286 }
287 return err;
Wei Jiadc6f3402017-01-09 15:04:18 -0800288}
289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290// must call with lock held
291status_t MediaPlayer::prepareAsync_l()
292{
Glenn Kastenb187de12014-12-30 08:18:15 -0800293 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700294 if (mAudioAttributesParcel != NULL) {
295 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700296 } else {
297 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700298 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 mCurrentState = MEDIA_PLAYER_PREPARING;
300 return mPlayer->prepareAsync();
301 }
Wei Jia848ebc62016-03-23 14:06:46 -0700302 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 return INVALID_OPERATION;
304}
305
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700306// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
307// one defined in the Android framework and one provided by the implementation
308// that generated the error. The sync version of prepare returns only 1 error
309// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310status_t MediaPlayer::prepare()
311{
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700314 mLockThreadId = getThreadId();
315 if (mPrepareSync) {
316 mLockThreadId = 0;
317 return -EALREADY;
318 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 mPrepareSync = true;
320 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700321 if (ret != NO_ERROR) {
322 mLockThreadId = 0;
323 return ret;
324 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325
326 if (mPrepareSync) {
327 mSignal.wait(mLock); // wait for prepare done
328 mPrepareSync = false;
329 }
Steve Block3856b092011-10-20 11:56:00 +0100330 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700331 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 return mPrepareStatus;
333}
334
335status_t MediaPlayer::prepareAsync()
336{
Steve Block3856b092011-10-20 11:56:00 +0100337 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338 Mutex::Autolock _l(mLock);
339 return prepareAsync_l();
340}
341
342status_t MediaPlayer::start()
343{
Steve Block3856b092011-10-20 11:56:00 +0100344 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700345
346 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700348
349 mLockThreadId = getThreadId();
350
351 if (mCurrentState & MEDIA_PLAYER_STARTED) {
352 ret = NO_ERROR;
353 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
355 mPlayer->setLooping(mLoop);
356 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700357 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700359 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 if (ret != NO_ERROR) {
361 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
362 } else {
363 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100364 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 }
366 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700367 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700368 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700369 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700371
372 mLockThreadId = 0;
373
374 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375}
376
377status_t MediaPlayer::stop()
378{
Steve Block3856b092011-10-20 11:56:00 +0100379 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 Mutex::Autolock _l(mLock);
381 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
382 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
383 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
384 status_t ret = mPlayer->stop();
385 if (ret != NO_ERROR) {
386 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
387 } else {
388 mCurrentState = MEDIA_PLAYER_STOPPED;
389 }
390 return ret;
391 }
Wei Jia848ebc62016-03-23 14:06:46 -0700392 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 return INVALID_OPERATION;
394}
395
396status_t MediaPlayer::pause()
397{
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800400 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 return NO_ERROR;
402 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
403 status_t ret = mPlayer->pause();
404 if (ret != NO_ERROR) {
405 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
406 } else {
407 mCurrentState = MEDIA_PLAYER_PAUSED;
408 }
409 return ret;
410 }
Wei Jia848ebc62016-03-23 14:06:46 -0700411 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 return INVALID_OPERATION;
413}
414
415bool MediaPlayer::isPlaying()
416{
417 Mutex::Autolock _l(mLock);
418 if (mPlayer != 0) {
419 bool temp = false;
420 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100421 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000423 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700425 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
426 ALOGE("internal/external state mismatch corrected");
427 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 }
429 return temp;
430 }
Steve Block3856b092011-10-20 11:56:00 +0100431 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 return false;
433}
434
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700435status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800436{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700437 ALOGV("setPlaybackSettings: %f %f %d %d",
438 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
439 // Negative speed and pitch does not make sense. Further validation will
440 // be done by the respective mediaplayers.
441 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800442 return BAD_VALUE;
443 }
444 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700445 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
446 return INVALID_OPERATION;
447 }
Wei Jia5be109f2016-06-10 16:15:07 -0700448
449 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
450 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
451 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
452 mPlayer->setLooping(mLoop);
453 mPlayer->setVolume(mLeftVolume, mRightVolume);
454 mPlayer->setAuxEffectSendLevel(mSendLevel);
455 }
456
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700457 status_t err = mPlayer->setPlaybackSettings(rate);
458 if (err == OK) {
459 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
460 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700461 } else if (rate.mSpeed != 0.f
462 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
463 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700464 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800465 }
Wei Jia98160162015-02-04 17:01:11 -0800466 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700467 return err;
468}
469
470status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
471{
472 Mutex::Autolock _l(mLock);
473 if (mPlayer == 0) return INVALID_OPERATION;
474 return mPlayer->getPlaybackSettings(rate);
475}
476
477status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
478{
479 ALOGV("setSyncSettings: %u %u %f %f",
480 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
481 Mutex::Autolock _l(mLock);
482 if (mPlayer == 0) return INVALID_OPERATION;
483 return mPlayer->setSyncSettings(sync, videoFpsHint);
484}
485
486status_t MediaPlayer::getSyncSettings(
487 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
488{
489 Mutex::Autolock _l(mLock);
490 if (mPlayer == 0) return INVALID_OPERATION;
491 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800492}
493
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494status_t MediaPlayer::getVideoWidth(int *w)
495{
Steve Block3856b092011-10-20 11:56:00 +0100496 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497 Mutex::Autolock _l(mLock);
498 if (mPlayer == 0) return INVALID_OPERATION;
499 *w = mVideoWidth;
500 return NO_ERROR;
501}
502
503status_t MediaPlayer::getVideoHeight(int *h)
504{
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 Mutex::Autolock _l(mLock);
507 if (mPlayer == 0) return INVALID_OPERATION;
508 *h = mVideoHeight;
509 return NO_ERROR;
510}
511
512status_t MediaPlayer::getCurrentPosition(int *msec)
513{
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 Mutex::Autolock _l(mLock);
516 if (mPlayer != 0) {
517 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100518 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 *msec = mCurrentPosition;
520 return NO_ERROR;
521 }
522 return mPlayer->getCurrentPosition(msec);
523 }
524 return INVALID_OPERATION;
525}
526
527status_t MediaPlayer::getDuration_l(int *msec)
528{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800529 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800530 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
531 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800533 int durationMs;
534 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700535
536 if (ret != OK) {
537 // Do not enter error state just because no duration was available.
538 durationMs = -1;
539 ret = OK;
540 }
541
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800542 if (msec) {
543 *msec = durationMs;
544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 return ret;
546 }
Wei Jia848ebc62016-03-23 14:06:46 -0700547 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
548 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 return INVALID_OPERATION;
550}
551
552status_t MediaPlayer::getDuration(int *msec)
553{
554 Mutex::Autolock _l(mLock);
555 return getDuration_l(msec);
556}
557
Wei Jiac5de0912016-11-18 10:22:14 -0800558status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559{
Wei Jiac5de0912016-11-18 10:22:14 -0800560 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800561 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
562 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000564 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800567
568 int durationMs;
569 status_t err = mPlayer->getDuration(&durationMs);
570
571 if (err != OK) {
572 ALOGW("Stream has no duration and is therefore not seekable.");
573 return err;
574 }
575
576 if (msec > durationMs) {
577 ALOGW("Attempt to seek to past end of file: request = %d, "
578 "durationMs = %d",
579 msec,
580 durationMs);
581
582 msec = durationMs;
583 }
584
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 // cache duration
586 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800587 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800590 mSeekMode = mode;
591 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592 }
593 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800594 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595 return NO_ERROR;
596 }
597 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800598 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
599 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 return INVALID_OPERATION;
601}
602
Wei Jiac5de0912016-11-18 10:22:14 -0800603status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700605 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800607 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700608 mLockThreadId = 0;
609
610 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611}
612
Wei Jia52c28512017-09-13 18:17:51 -0700613status_t MediaPlayer::notifyAt(int64_t mediaTimeUs)
614{
615 Mutex::Autolock _l(mLock);
616 if (mPlayer != 0) {
617 return mPlayer->notifyAt(mediaTimeUs);
618 }
619 return INVALID_OPERATION;
620}
621
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700622status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624 mLoop = false;
625 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
626 mPrepareSync = false;
627 if (mPlayer != 0) {
628 status_t ret = mPlayer->reset();
629 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000630 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
632 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700633 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 mCurrentState = MEDIA_PLAYER_IDLE;
635 }
James Donga1680bc2010-11-18 12:23:58 -0800636 // setDataSource has to be called again to create a
637 // new mediaplayer.
638 mPlayer = 0;
Wei Jiaed320862017-01-18 14:03:40 -0800639 mCurrentBufferingSettings = BufferingSettings();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640 return ret;
641 }
642 clear_l();
643 return NO_ERROR;
644}
645
John Grossmanc795b642012-02-22 15:38:35 -0800646status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
647 Mutex::Autolock _l(mLock);
648
649 if (player == NULL) {
650 return UNKNOWN_ERROR;
651 }
652
653 if (mRetransmitEndpointValid) {
654 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
655 }
656
657 return OK;
658}
659
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700660status_t MediaPlayer::reset()
661{
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("reset");
Wonsik Kim5f648972017-07-21 15:42:59 -0700663 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700664 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700665 status_t result = reset_l();
666 mLockThreadId = 0;
667
668 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700669}
670
Glenn Kastenfff6d712012-01-12 16:38:12 -0800671status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672{
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674 Mutex::Autolock _l(mLock);
675 if (mStreamType == type) return NO_ERROR;
676 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
677 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
678 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000679 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680 return INVALID_OPERATION;
681 }
682 // cache
683 mStreamType = type;
684 return OK;
685}
686
John Spurlockde9453f2014-03-19 13:05:45 -0400687status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
688{
689 ALOGV("getAudioStreamType");
690 Mutex::Autolock _l(mLock);
691 *type = mStreamType;
692 return OK;
693}
694
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695status_t MediaPlayer::setLooping(int loop)
696{
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 Mutex::Autolock _l(mLock);
699 mLoop = (loop != 0);
700 if (mPlayer != 0) {
701 return mPlayer->setLooping(loop);
702 }
703 return OK;
704}
705
706bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 Mutex::Autolock _l(mLock);
709 if (mPlayer != 0) {
710 return mLoop;
711 }
Steve Block3856b092011-10-20 11:56:00 +0100712 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713 return false;
714}
715
716status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
717{
Steve Block3856b092011-10-20 11:56:00 +0100718 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 Mutex::Autolock _l(mLock);
720 mLeftVolume = leftVolume;
721 mRightVolume = rightVolume;
722 if (mPlayer != 0) {
723 return mPlayer->setVolume(leftVolume, rightVolume);
724 }
725 return OK;
726}
727
Glenn Kastend848eb42016-03-08 13:42:11 -0800728status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700729{
Steve Block3856b092011-10-20 11:56:00 +0100730 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700731 Mutex::Autolock _l(mLock);
732 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000733 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700734 return INVALID_OPERATION;
735 }
736 if (sessionId < 0) {
737 return BAD_VALUE;
738 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700739 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800740 AudioSystem::acquireAudioSessionId(sessionId, -1);
741 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700742 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700743 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700744 return NO_ERROR;
745}
746
Glenn Kastend848eb42016-03-08 13:42:11 -0800747audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700748{
749 Mutex::Autolock _l(mLock);
750 return mAudioSessionId;
751}
752
Eric Laurent2beeb502010-07-16 07:43:46 -0700753status_t MediaPlayer::setAuxEffectSendLevel(float level)
754{
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700756 Mutex::Autolock _l(mLock);
757 mSendLevel = level;
758 if (mPlayer != 0) {
759 return mPlayer->setAuxEffectSendLevel(level);
760 }
761 return OK;
762}
763
764status_t MediaPlayer::attachAuxEffect(int effectId)
765{
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700767 Mutex::Autolock _l(mLock);
768 if (mPlayer == 0 ||
769 (mCurrentState & MEDIA_PLAYER_IDLE) ||
770 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700771 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700772 return INVALID_OPERATION;
773 }
774
775 return mPlayer->attachAuxEffect(effectId);
776}
777
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700778// always call with lock held
779status_t MediaPlayer::checkStateForKeySet_l(int key)
780{
781 switch(key) {
782 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
783 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
784 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
785 // Can't change the audio attributes after prepare
786 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
787 return INVALID_OPERATION;
788 }
789 break;
790 default:
791 // parameter doesn't require player state check
792 break;
793 }
794 return OK;
795}
796
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700797status_t MediaPlayer::setParameter(int key, const Parcel& request)
798{
Steve Block3856b092011-10-20 11:56:00 +0100799 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700800 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700801 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700802 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700803 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700804 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700805 switch (key) {
806 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700807 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700808 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
809 mAudioAttributesParcel = new Parcel();
810 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700811 status = OK;
812 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700813 default:
Eric Laurent43562692015-07-15 16:49:07 -0700814 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
815 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700816 }
Eric Laurent43562692015-07-15 16:49:07 -0700817
818 if (mPlayer != NULL) {
819 status = mPlayer->setParameter(key, request);
820 }
821 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700822}
823
824status_t MediaPlayer::getParameter(int key, Parcel *reply)
825{
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700827 Mutex::Autolock _l(mLock);
828 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800829 status_t status = mPlayer->getParameter(key, reply);
830 if (status != OK) {
831 ALOGD("getParameter returns %d", status);
832 }
833 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700834 }
Steve Block3856b092011-10-20 11:56:00 +0100835 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700836 return INVALID_OPERATION;
837}
838
John Grossmanc795b642012-02-22 15:38:35 -0800839status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
840 uint16_t port) {
841 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
842 addrString ? addrString : "(null)", port);
843
844 Mutex::Autolock _l(mLock);
845 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
846 return INVALID_OPERATION;
847
848 if (NULL == addrString) {
849 mRetransmitEndpointValid = false;
850 return OK;
851 }
852
853 struct in_addr saddr;
854 if(!inet_aton(addrString, &saddr)) {
855 return BAD_VALUE;
856 }
857
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800858 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800859 mRetransmitEndpoint.sin_family = AF_INET;
860 mRetransmitEndpoint.sin_addr = saddr;
861 mRetransmitEndpoint.sin_port = htons(port);
862 mRetransmitEndpointValid = true;
863
864 return OK;
865}
866
Gloria Wangb483c472011-04-11 17:23:27 -0700867void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800868{
Steve Block3856b092011-10-20 11:56:00 +0100869 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700871 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872
873 // TODO: In the future, we might be on the same thread if the app is
874 // running in the same process as the media server. In that case,
875 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700876 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700877 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700878 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700879 // FIXME: Remember, this is a hack, it's not even a hack that is applied
880 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700881 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700882 mLock.lock();
883 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700884 }
Jason Sams1af452f2009-03-24 18:45:22 -0700885
Eric Laurent3b268442010-08-03 07:49:49 -0700886 // Allows calls from JNI in idle state to notify errors
887 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100888 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700889 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800890 return;
891 }
892
893 switch (msg) {
894 case MEDIA_NOP: // interface test message
895 break;
896 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800897 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 mCurrentState = MEDIA_PLAYER_PREPARED;
899 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100900 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800901 mPrepareSync = false;
902 mPrepareStatus = NO_ERROR;
903 mSignal.signal();
904 }
905 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800906 case MEDIA_DRM_INFO:
907 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
908 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700911 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000912 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700913 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 if (!mLoop) {
915 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
916 }
917 break;
918 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700919 // Always log errors.
920 // ext1: Media framework error code.
921 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000922 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
924 if (mPrepareSync)
925 {
Steve Block3856b092011-10-20 11:56:00 +0100926 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800927 mPrepareSync = false;
928 mPrepareStatus = ext1;
929 mSignal.signal();
930 send = false;
931 }
932 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700933 case MEDIA_INFO:
934 // ext1: Media framework error code.
935 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800936 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000937 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800938 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700939 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800940 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100941 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800942 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
943 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800945 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
946 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 }
948 else {
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800951 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800952 }
953 break;
954 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 break;
957 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100958 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800959 mVideoWidth = ext1;
960 mVideoHeight = ext2;
961 break;
Wei Jia52c28512017-09-13 18:17:51 -0700962 case MEDIA_NOTIFY_TIME:
963 ALOGV("Received notify time message");
964 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700965 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700967 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700968 case MEDIA_SUBTITLE_DATA:
969 ALOGV("Received subtitle data message");
970 break;
Robert Shih08528432015-04-08 09:06:54 -0700971 case MEDIA_META_DATA:
972 ALOGV("Received timed metadata message");
973 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 default:
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 break;
977 }
978
979 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700980 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981
982 // this prevents re-entrant calls into client code
983 if ((listener != 0) && send) {
984 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700986 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 }
989}
990
James Dongdd172fc2010-01-15 18:13:58 -0800991void MediaPlayer::died()
992{
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800994 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
995}
996
Marco Nelissen6b74d672012-02-28 16:07:44 -0800997status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700998 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800999 if (mPlayer == NULL) {
1000 return NO_INIT;
1001 }
Marco Nelissenb13820f2013-08-05 12:22:43 -07001002
1003 if (next != NULL && !(next->mCurrentState &
1004 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
1005 ALOGE("next player is not prepared");
1006 return INVALID_OPERATION;
1007 }
1008
Marco Nelissen6b74d672012-02-28 16:07:44 -08001009 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
1010}
1011
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001012VolumeShaper::Status MediaPlayer::applyVolumeShaper(
1013 const sp<VolumeShaper::Configuration>& configuration,
1014 const sp<VolumeShaper::Operation>& operation)
1015{
1016 Mutex::Autolock _l(mLock);
1017 if (mPlayer == nullptr) {
1018 return VolumeShaper::Status(NO_INIT);
1019 }
1020 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
1021 return status;
1022}
1023
1024sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
1025{
1026 Mutex::Autolock _l(mLock);
1027 if (mPlayer == nullptr) {
1028 return nullptr;
1029 }
1030 return mPlayer->getVolumeShaperState(id);
1031}
1032
Hassan Shojaniacefac142017-02-06 21:02:02 -08001033// Modular DRM
1034status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001035{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001036 // TODO change to ALOGV
1037 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1038 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001039 Mutex::Autolock _l(mLock);
1040 if (mPlayer == NULL) {
1041 return NO_INIT;
1042 }
1043
Hassan Shojania838be392017-05-23 14:14:24 -07001044 // Only allowed it in player's preparing/prepared state.
1045 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1046 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1047 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1048 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1049 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001050 return INVALID_OPERATION;
1051 }
1052
Hassan Shojaniacefac142017-02-06 21:02:02 -08001053 if (drmSessionId.isEmpty()) {
1054 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1055 return INVALID_OPERATION;
1056 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001057
Hassan Shojaniacefac142017-02-06 21:02:02 -08001058 // Passing down to mediaserver mainly for creating the crypto
1059 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1060 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1061
1062 // TODO change to ALOGV
1063 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1064
1065 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001066}
1067
1068status_t MediaPlayer::releaseDrm()
1069{
1070 Mutex::Autolock _l(mLock);
1071 if (mPlayer == NULL) {
1072 return NO_INIT;
1073 }
1074
Hassan Shojaniacefac142017-02-06 21:02:02 -08001075 // Not allowing releaseDrm in an active/resumable state
1076 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1077 MEDIA_PLAYER_PAUSED |
1078 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1079 MEDIA_PLAYER_STATE_ERROR)) {
1080 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001081 return INVALID_OPERATION;
1082 }
1083
Hassan Shojaniacefac142017-02-06 21:02:02 -08001084 status_t status = mPlayer->releaseDrm();
1085 // TODO change to ALOGV
1086 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1087 if (status != OK) {
1088 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1089 // Overriding to OK so the client proceed with its own cleanup
1090 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1091 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001092 }
1093
Hassan Shojaniacefac142017-02-06 21:02:02 -08001094 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001095}
1096
jiabin156c6872017-10-06 09:47:15 -07001097status_t MediaPlayer::setOutputDevice(audio_port_handle_t deviceId)
1098{
1099 Mutex::Autolock _l(mLock);
1100 if (mPlayer == NULL) {
1101 ALOGV("setOutputDevice: player not init");
1102 return NO_INIT;
1103 }
1104 return mPlayer->setOutputDevice(deviceId);
1105}
1106
1107audio_port_handle_t MediaPlayer::getRoutedDeviceId()
1108{
1109 Mutex::Autolock _l(mLock);
1110 if (mPlayer == NULL) {
1111 ALOGV("getRoutedDeviceId: player not init");
1112 return AUDIO_PORT_HANDLE_NONE;
1113 }
1114 audio_port_handle_t deviceId;
1115 status_t status = mPlayer->getRoutedDeviceId(&deviceId);
1116 if (status != NO_ERROR) {
1117 return AUDIO_PORT_HANDLE_NONE;
1118 }
1119 return deviceId;
1120}
1121
1122status_t MediaPlayer::enableAudioDeviceCallback(bool enabled)
1123{
1124 Mutex::Autolock _l(mLock);
1125 if (mPlayer == NULL) {
1126 ALOGV("addAudioDeviceCallback: player not init");
1127 return NO_INIT;
1128 }
1129 return mPlayer->enableAudioDeviceCallback(enabled);
1130}
1131
Glenn Kasten40bc9062015-03-20 09:09:33 -07001132} // namespace android