blob: 9d3fce70b2d6d13cb9be9e7e9a8216a14c222ab3 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
Hassan Shojaniacefac142017-02-06 21:02:02 -080019#define LOG_TAG "MediaPlayerNative"
Marco Nelissendab79b32019-11-18 08:25:47 -080020#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070023#include <inttypes.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
Marco Nelissendab79b32019-11-18 08:25:47 -080029#include <android/IDataSource.h>
Mathias Agopian75624082009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/mediaplayer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070032#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080033#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070034#include <media/AVSyncSettings.h>
Andreas Huber2db84552010-01-28 11:19:57 -080035#include <utils/KeyedVector.h>
36#include <utils/String8.h>
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070038#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070039
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040namespace android {
41
Ivan Lozano8cf3a072017-08-09 09:01:33 -070042using media::VolumeShaper;
Svet Ganov33761132021-05-13 22:51:08 +000043using content::AttributionSourceState;
Ivan Lozano8cf3a072017-08-09 09:01:33 -070044
Jan Sebechlebsky1e541192023-01-12 10:16:56 +010045MediaPlayer::MediaPlayer(const AttributionSourceState& attributionSource,
46 const audio_session_t sessionId) : mAttributionSource(attributionSource)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047{
Steve Block3856b092011-10-20 11:56:00 +010048 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049 mListener = NULL;
50 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070051 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070052 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080054 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080056 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057 mCurrentState = MEDIA_PLAYER_IDLE;
58 mPrepareSync = false;
59 mPrepareStatus = NO_ERROR;
60 mLoop = false;
61 mLeftVolume = mRightVolume = 1.0;
62 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070063 mLockThreadId = 0;
Jan Sebechlebsky1e541192023-01-12 10:16:56 +010064 if (sessionId == AUDIO_SESSION_ALLOCATE) {
65 mAudioSessionId = static_cast<audio_session_t>(
66 AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION));
67 } else {
68 mAudioSessionId = sessionId;
69 }
Andy Hung8b0bfd92019-12-23 13:11:11 -080070 AudioSystem::acquireAudioSessionId(mAudioSessionId, (pid_t)-1, (uid_t)-1); // always in client.
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 }
Andy Hung8b0bfd92019-12-23 13:11:11 -080082 AudioSystem::releaseAudioSessionId(mAudioSessionId, (pid_t)-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;
140 err = NO_ERROR;
141 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000142 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 }
144 }
145
146 if (p != 0) {
147 p->disconnect();
148 }
149
150 return err;
151}
152
Andreas Huber2db84552010-01-28 11:19:57 -0800153status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800154 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800155 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156{
Steve Block3856b092011-10-20 11:56:00 +0100157 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 status_t err = BAD_VALUE;
159 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700160 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 if (service != 0) {
Svet Ganov33761132021-05-13 22:51:08 +0000162 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mAttributionSource));
John Grossmanc795b642012-02-22 15:38:35 -0800163 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800164 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100165 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100166 }
Dave Burke06620672011-09-06 20:39:47 +0100167 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 }
169 }
170 return err;
171}
172
173status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
174{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700175 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700177 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 if (service != 0) {
Svet Ganov33761132021-05-13 22:51:08 +0000179 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mAttributionSource));
John Grossmanc795b642012-02-22 15:38:35 -0800180 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
181 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100182 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100183 }
Dave Burke06620672011-09-06 20:39:47 +0100184 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100185 }
186 return err;
187}
188
Chris Watkins99f31602015-03-20 13:06:33 -0700189status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
190{
191 ALOGV("setDataSource(IDataSource)");
192 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700193 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700194 if (service != 0) {
Svet Ganov33761132021-05-13 22:51:08 +0000195 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mAttributionSource));
Chris Watkins99f31602015-03-20 13:06:33 -0700196 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
197 (NO_ERROR != player->setDataSource(source))) {
198 player.clear();
199 }
200 err = attachNewPlayer(player);
201 }
202 return err;
203}
204
Byeongjo Park28225ab2019-01-24 20:31:19 +0900205status_t MediaPlayer::setDataSource(const String8& rtpParams)
206{
207 ALOGV("setDataSource(rtpParams)");
208 status_t err = UNKNOWN_ERROR;
209 const sp<IMediaPlayerService> service(getMediaPlayerService());
210 if (service != 0) {
Svet Ganov33761132021-05-13 22:51:08 +0000211 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mAttributionSource));
Byeongjo Park28225ab2019-01-24 20:31:19 +0900212 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
213 (NO_ERROR != player->setDataSource(rtpParams))) {
214 player.clear();
215 }
216 err = attachNewPlayer(player);
217 }
218 return err;
219}
220
Nicolas Catania1d187f12009-05-12 23:25:55 -0700221status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
222{
223 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800224 const bool hasBeenInitialized =
225 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
226 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
227 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700228 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700229 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700230 }
Wei Jia848ebc62016-03-23 14:06:46 -0700231 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700232 return INVALID_OPERATION;
233}
234
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700235status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
236{
Steve Blockb8a80522011-12-20 16:23:08 +0000237 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700238 Mutex::Autolock lock(mLock);
239 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700240 return NO_INIT;
241 }
242 return mPlayer->setMetadataFilter(filter);
243}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700244
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700245status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
246{
Steve Blockb8a80522011-12-20 16:23:08 +0000247 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700248 Mutex::Autolock lock(mLock);
249 if (mPlayer == NULL) {
250 return NO_INIT;
251 }
252 return mPlayer->getMetadata(update_only, apply_filter, metadata);
253}
254
Glenn Kasten11731182011-02-08 17:26:17 -0800255status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800256 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800257{
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800259 Mutex::Autolock _l(mLock);
260 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800261 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800262}
263
Wei Jiaed320862017-01-18 14:03:40 -0800264status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
265{
266 ALOGV("getBufferingSettings");
267
268 Mutex::Autolock _l(mLock);
269 if (mPlayer == 0) {
270 return NO_INIT;
271 }
Wei Jia9bb38032017-03-23 18:00:38 -0700272 return mPlayer->getBufferingSettings(buffering);
Wei Jiaed320862017-01-18 14:03:40 -0800273}
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 Jia9bb38032017-03-23 18:00:38 -0700283 return mPlayer->setBufferingSettings(buffering);
Wei Jiadc6f3402017-01-09 15:04:18 -0800284}
285
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286// must call with lock held
287status_t MediaPlayer::prepareAsync_l()
288{
Glenn Kastenb187de12014-12-30 08:18:15 -0800289 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700290 if (mAudioAttributesParcel != NULL) {
291 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700292 } else {
293 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700294 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 mCurrentState = MEDIA_PLAYER_PREPARING;
296 return mPlayer->prepareAsync();
297 }
Wei Jia848ebc62016-03-23 14:06:46 -0700298 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 return INVALID_OPERATION;
300}
301
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700302// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
303// one defined in the Android framework and one provided by the implementation
304// that generated the error. The sync version of prepare returns only 1 error
305// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306status_t MediaPlayer::prepare()
307{
Steve Block3856b092011-10-20 11:56:00 +0100308 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700310 mLockThreadId = getThreadId();
311 if (mPrepareSync) {
312 mLockThreadId = 0;
313 return -EALREADY;
314 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 mPrepareSync = true;
316 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700317 if (ret != NO_ERROR) {
318 mLockThreadId = 0;
319 return ret;
320 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321
322 if (mPrepareSync) {
323 mSignal.wait(mLock); // wait for prepare done
324 mPrepareSync = false;
325 }
Steve Block3856b092011-10-20 11:56:00 +0100326 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700327 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 return mPrepareStatus;
329}
330
331status_t MediaPlayer::prepareAsync()
332{
Steve Block3856b092011-10-20 11:56:00 +0100333 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 Mutex::Autolock _l(mLock);
335 return prepareAsync_l();
336}
337
338status_t MediaPlayer::start()
339{
Steve Block3856b092011-10-20 11:56:00 +0100340 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700341
342 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700344
345 mLockThreadId = getThreadId();
346
347 if (mCurrentState & MEDIA_PLAYER_STARTED) {
348 ret = NO_ERROR;
349 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
351 mPlayer->setLooping(mLoop);
352 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700353 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700355 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 if (ret != NO_ERROR) {
357 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
358 } else {
359 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100360 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 }
362 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700363 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700364 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700365 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700367
368 mLockThreadId = 0;
369
370 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371}
372
373status_t MediaPlayer::stop()
374{
Steve Block3856b092011-10-20 11:56:00 +0100375 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 Mutex::Autolock _l(mLock);
377 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
378 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
379 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
380 status_t ret = mPlayer->stop();
381 if (ret != NO_ERROR) {
382 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
383 } else {
384 mCurrentState = MEDIA_PLAYER_STOPPED;
385 }
386 return ret;
387 }
Wei Jia848ebc62016-03-23 14:06:46 -0700388 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 return INVALID_OPERATION;
390}
391
392status_t MediaPlayer::pause()
393{
Steve Block3856b092011-10-20 11:56:00 +0100394 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800396 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 return NO_ERROR;
398 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
399 status_t ret = mPlayer->pause();
400 if (ret != NO_ERROR) {
401 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
402 } else {
403 mCurrentState = MEDIA_PLAYER_PAUSED;
404 }
405 return ret;
406 }
Wei Jia848ebc62016-03-23 14:06:46 -0700407 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 return INVALID_OPERATION;
409}
410
411bool MediaPlayer::isPlaying()
412{
413 Mutex::Autolock _l(mLock);
414 if (mPlayer != 0) {
415 bool temp = false;
416 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100417 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000419 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700421 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
422 ALOGE("internal/external state mismatch corrected");
423 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 }
425 return temp;
426 }
Steve Block3856b092011-10-20 11:56:00 +0100427 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 return false;
429}
430
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700431status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800432{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700433 ALOGV("setPlaybackSettings: %f %f %d %d",
434 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
435 // Negative speed and pitch does not make sense. Further validation will
436 // be done by the respective mediaplayers.
437 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800438 return BAD_VALUE;
439 }
440 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700441 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
442 return INVALID_OPERATION;
443 }
Wei Jia5be109f2016-06-10 16:15:07 -0700444
445 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
446 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
447 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
448 mPlayer->setLooping(mLoop);
449 mPlayer->setVolume(mLeftVolume, mRightVolume);
450 mPlayer->setAuxEffectSendLevel(mSendLevel);
451 }
452
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700453 status_t err = mPlayer->setPlaybackSettings(rate);
454 if (err == OK) {
455 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
456 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700457 } else if (rate.mSpeed != 0.f
458 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
459 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700460 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800461 }
Wei Jia98160162015-02-04 17:01:11 -0800462 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700463 return err;
464}
465
466status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
467{
468 Mutex::Autolock _l(mLock);
469 if (mPlayer == 0) return INVALID_OPERATION;
470 return mPlayer->getPlaybackSettings(rate);
471}
472
473status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
474{
475 ALOGV("setSyncSettings: %u %u %f %f",
476 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
477 Mutex::Autolock _l(mLock);
478 if (mPlayer == 0) return INVALID_OPERATION;
479 return mPlayer->setSyncSettings(sync, videoFpsHint);
480}
481
482status_t MediaPlayer::getSyncSettings(
483 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
484{
485 Mutex::Autolock _l(mLock);
486 if (mPlayer == 0) return INVALID_OPERATION;
487 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800488}
489
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490status_t MediaPlayer::getVideoWidth(int *w)
491{
Steve Block3856b092011-10-20 11:56:00 +0100492 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 Mutex::Autolock _l(mLock);
494 if (mPlayer == 0) return INVALID_OPERATION;
495 *w = mVideoWidth;
496 return NO_ERROR;
497}
498
499status_t MediaPlayer::getVideoHeight(int *h)
500{
Steve Block3856b092011-10-20 11:56:00 +0100501 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 Mutex::Autolock _l(mLock);
503 if (mPlayer == 0) return INVALID_OPERATION;
504 *h = mVideoHeight;
505 return NO_ERROR;
506}
507
508status_t MediaPlayer::getCurrentPosition(int *msec)
509{
Steve Block3856b092011-10-20 11:56:00 +0100510 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 Mutex::Autolock _l(mLock);
512 if (mPlayer != 0) {
513 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 *msec = mCurrentPosition;
516 return NO_ERROR;
517 }
518 return mPlayer->getCurrentPosition(msec);
519 }
520 return INVALID_OPERATION;
521}
522
523status_t MediaPlayer::getDuration_l(int *msec)
524{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800525 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800526 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
527 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800529 int durationMs;
530 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700531
532 if (ret != OK) {
533 // Do not enter error state just because no duration was available.
534 durationMs = -1;
535 ret = OK;
536 }
537
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800538 if (msec) {
539 *msec = durationMs;
540 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541 return ret;
542 }
Wei Jia848ebc62016-03-23 14:06:46 -0700543 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
544 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 return INVALID_OPERATION;
546}
547
548status_t MediaPlayer::getDuration(int *msec)
549{
550 Mutex::Autolock _l(mLock);
551 return getDuration_l(msec);
552}
553
Wei Jiac5de0912016-11-18 10:22:14 -0800554status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555{
Wei Jiac5de0912016-11-18 10:22:14 -0800556 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800557 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
558 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000560 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800563
564 int durationMs;
565 status_t err = mPlayer->getDuration(&durationMs);
566
567 if (err != OK) {
568 ALOGW("Stream has no duration and is therefore not seekable.");
569 return err;
570 }
571
572 if (msec > durationMs) {
573 ALOGW("Attempt to seek to past end of file: request = %d, "
574 "durationMs = %d",
575 msec,
576 durationMs);
577
578 msec = durationMs;
579 }
580
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 // cache duration
582 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800583 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800586 mSeekMode = mode;
587 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 }
589 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800590 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 return NO_ERROR;
592 }
593 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800594 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
595 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 return INVALID_OPERATION;
597}
598
Wei Jiac5de0912016-11-18 10:22:14 -0800599status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700601 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800603 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700604 mLockThreadId = 0;
605
606 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607}
608
Wei Jia52c28512017-09-13 18:17:51 -0700609status_t MediaPlayer::notifyAt(int64_t mediaTimeUs)
610{
611 Mutex::Autolock _l(mLock);
612 if (mPlayer != 0) {
613 return mPlayer->notifyAt(mediaTimeUs);
614 }
615 return INVALID_OPERATION;
616}
617
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700618status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 mLoop = false;
621 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
622 mPrepareSync = false;
623 if (mPlayer != 0) {
624 status_t ret = mPlayer->reset();
625 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000626 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
628 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700629 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630 mCurrentState = MEDIA_PLAYER_IDLE;
631 }
James Donga1680bc2010-11-18 12:23:58 -0800632 // setDataSource has to be called again to create a
633 // new mediaplayer.
634 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 return ret;
636 }
637 clear_l();
638 return NO_ERROR;
639}
640
John Grossmanc795b642012-02-22 15:38:35 -0800641status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
642 Mutex::Autolock _l(mLock);
643
644 if (player == NULL) {
645 return UNKNOWN_ERROR;
646 }
647
648 if (mRetransmitEndpointValid) {
649 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
650 }
651
652 return OK;
653}
654
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700655status_t MediaPlayer::reset()
656{
Steve Block3856b092011-10-20 11:56:00 +0100657 ALOGV("reset");
Wonsik Kim5f648972017-07-21 15:42:59 -0700658 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700659 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700660 status_t result = reset_l();
661 mLockThreadId = 0;
662
663 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700664}
665
Glenn Kastenfff6d712012-01-12 16:38:12 -0800666status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667{
Steve Block3856b092011-10-20 11:56:00 +0100668 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669 Mutex::Autolock _l(mLock);
670 if (mStreamType == type) return NO_ERROR;
671 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
672 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
673 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000674 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800675 return INVALID_OPERATION;
676 }
677 // cache
678 mStreamType = type;
679 return OK;
680}
681
John Spurlockde9453f2014-03-19 13:05:45 -0400682status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
683{
684 ALOGV("getAudioStreamType");
685 Mutex::Autolock _l(mLock);
686 *type = mStreamType;
687 return OK;
688}
689
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690status_t MediaPlayer::setLooping(int loop)
691{
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 Mutex::Autolock _l(mLock);
694 mLoop = (loop != 0);
695 if (mPlayer != 0) {
696 return mPlayer->setLooping(loop);
697 }
698 return OK;
699}
700
701bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703 Mutex::Autolock _l(mLock);
704 if (mPlayer != 0) {
705 return mLoop;
706 }
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 return false;
709}
710
711status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
712{
Steve Block3856b092011-10-20 11:56:00 +0100713 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 Mutex::Autolock _l(mLock);
715 mLeftVolume = leftVolume;
716 mRightVolume = rightVolume;
717 if (mPlayer != 0) {
718 return mPlayer->setVolume(leftVolume, rightVolume);
719 }
720 return OK;
721}
722
Glenn Kastend848eb42016-03-08 13:42:11 -0800723status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700724{
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700726 Mutex::Autolock _l(mLock);
727 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000728 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700729 return INVALID_OPERATION;
730 }
731 if (sessionId < 0) {
732 return BAD_VALUE;
733 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700734 if (sessionId != mAudioSessionId) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800735 AudioSystem::acquireAudioSessionId(sessionId, (pid_t)-1, (uid_t)-1);
736 AudioSystem::releaseAudioSessionId(mAudioSessionId, (pid_t)-1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700737 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700738 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700739 return NO_ERROR;
740}
741
Glenn Kastend848eb42016-03-08 13:42:11 -0800742audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700743{
744 Mutex::Autolock _l(mLock);
745 return mAudioSessionId;
746}
747
Eric Laurent2beeb502010-07-16 07:43:46 -0700748status_t MediaPlayer::setAuxEffectSendLevel(float level)
749{
Steve Block3856b092011-10-20 11:56:00 +0100750 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700751 Mutex::Autolock _l(mLock);
752 mSendLevel = level;
753 if (mPlayer != 0) {
754 return mPlayer->setAuxEffectSendLevel(level);
755 }
756 return OK;
757}
758
759status_t MediaPlayer::attachAuxEffect(int effectId)
760{
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700762 Mutex::Autolock _l(mLock);
763 if (mPlayer == 0 ||
764 (mCurrentState & MEDIA_PLAYER_IDLE) ||
765 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700766 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700767 return INVALID_OPERATION;
768 }
769
770 return mPlayer->attachAuxEffect(effectId);
771}
772
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700773// always call with lock held
774status_t MediaPlayer::checkStateForKeySet_l(int key)
775{
776 switch(key) {
777 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
778 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
779 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
780 // Can't change the audio attributes after prepare
781 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
782 return INVALID_OPERATION;
783 }
784 break;
785 default:
786 // parameter doesn't require player state check
787 break;
788 }
789 return OK;
790}
791
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700792status_t MediaPlayer::setParameter(int key, const Parcel& request)
793{
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700795 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700796 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700797 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700798 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700799 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700800 switch (key) {
801 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700802 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700803 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
804 mAudioAttributesParcel = new Parcel();
805 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700806 status = OK;
807 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700808 default:
Eric Laurent43562692015-07-15 16:49:07 -0700809 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
810 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700811 }
Eric Laurent43562692015-07-15 16:49:07 -0700812
813 if (mPlayer != NULL) {
814 status = mPlayer->setParameter(key, request);
815 }
816 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700817}
818
819status_t MediaPlayer::getParameter(int key, Parcel *reply)
820{
Steve Block3856b092011-10-20 11:56:00 +0100821 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700822 Mutex::Autolock _l(mLock);
823 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800824 status_t status = mPlayer->getParameter(key, reply);
825 if (status != OK) {
826 ALOGD("getParameter returns %d", status);
827 }
828 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700829 }
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700831 return INVALID_OPERATION;
832}
833
John Grossmanc795b642012-02-22 15:38:35 -0800834status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
835 uint16_t port) {
836 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
837 addrString ? addrString : "(null)", port);
838
839 Mutex::Autolock _l(mLock);
840 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
841 return INVALID_OPERATION;
842
843 if (NULL == addrString) {
844 mRetransmitEndpointValid = false;
845 return OK;
846 }
847
848 struct in_addr saddr;
849 if(!inet_aton(addrString, &saddr)) {
850 return BAD_VALUE;
851 }
852
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800853 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800854 mRetransmitEndpoint.sin_family = AF_INET;
855 mRetransmitEndpoint.sin_addr = saddr;
856 mRetransmitEndpoint.sin_port = htons(port);
857 mRetransmitEndpointValid = true;
858
859 return OK;
860}
861
Gloria Wangb483c472011-04-11 17:23:27 -0700862void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863{
Steve Block3856b092011-10-20 11:56:00 +0100864 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800865 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700866 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800867
868 // TODO: In the future, we might be on the same thread if the app is
869 // running in the same process as the media server. In that case,
870 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700871 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700872 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700873 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700874 // FIXME: Remember, this is a hack, it's not even a hack that is applied
875 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700876 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700877 mLock.lock();
878 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700879 }
Jason Sams1af452f2009-03-24 18:45:22 -0700880
Eric Laurent3b268442010-08-03 07:49:49 -0700881 // Allows calls from JNI in idle state to notify errors
882 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700884 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800885 return;
886 }
887
888 switch (msg) {
889 case MEDIA_NOP: // interface test message
890 break;
891 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800892 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800893 mCurrentState = MEDIA_PLAYER_PREPARED;
894 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800896 mPrepareSync = false;
897 mPrepareStatus = NO_ERROR;
898 mSignal.signal();
899 }
900 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800901 case MEDIA_DRM_INFO:
902 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
903 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800904 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700906 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000907 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700908 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 if (!mLoop) {
910 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
911 }
912 break;
913 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700914 // Always log errors.
915 // ext1: Media framework error code.
916 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000917 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
919 if (mPrepareSync)
920 {
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922 mPrepareSync = false;
923 mPrepareStatus = ext1;
924 mSignal.signal();
925 send = false;
926 }
927 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700928 case MEDIA_INFO:
929 // ext1: Media framework error code.
930 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800931 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000932 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800933 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700934 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800937 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
938 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800939 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800940 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
941 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942 }
943 else {
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800946 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 }
948 break;
949 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800951 break;
952 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800954 mVideoWidth = ext1;
955 mVideoHeight = ext2;
956 break;
Santiago Seifert7a6adc82021-11-04 14:48:57 +0000957 case MEDIA_STARTED:
958 ALOGV("Received media started message");
959 break;
Wei Jia52c28512017-09-13 18:17:51 -0700960 case MEDIA_NOTIFY_TIME:
961 ALOGV("Received notify time message");
962 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700963 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700965 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700966 case MEDIA_SUBTITLE_DATA:
967 ALOGV("Received subtitle data message");
968 break;
Robert Shih08528432015-04-08 09:06:54 -0700969 case MEDIA_META_DATA:
970 ALOGV("Received timed metadata message");
971 break;
Kim Sungyeond3c6b322018-03-02 14:41:19 +0900972 case MEDIA_IMS_RX_NOTICE:
973 ALOGV("Received IMS Rx notice message");
974 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975 default:
Steve Block3856b092011-10-20 11:56:00 +0100976 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977 break;
978 }
979
980 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700981 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982
983 // this prevents re-entrant calls into client code
984 if ((listener != 0) && send) {
985 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700987 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800989 }
990}
991
James Dongdd172fc2010-01-15 18:13:58 -0800992void MediaPlayer::died()
993{
Steve Block3856b092011-10-20 11:56:00 +0100994 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800995 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
996}
997
Marco Nelissen6b74d672012-02-28 16:07:44 -0800998status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700999 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001000 if (mPlayer == NULL) {
1001 return NO_INIT;
1002 }
Marco Nelissenb13820f2013-08-05 12:22:43 -07001003
1004 if (next != NULL && !(next->mCurrentState &
1005 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
1006 ALOGE("next player is not prepared");
1007 return INVALID_OPERATION;
1008 }
1009
Marco Nelissen6b74d672012-02-28 16:07:44 -08001010 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
1011}
1012
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001013VolumeShaper::Status MediaPlayer::applyVolumeShaper(
1014 const sp<VolumeShaper::Configuration>& configuration,
1015 const sp<VolumeShaper::Operation>& operation)
1016{
1017 Mutex::Autolock _l(mLock);
1018 if (mPlayer == nullptr) {
1019 return VolumeShaper::Status(NO_INIT);
1020 }
1021 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
1022 return status;
1023}
1024
1025sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
1026{
1027 Mutex::Autolock _l(mLock);
1028 if (mPlayer == nullptr) {
1029 return nullptr;
1030 }
1031 return mPlayer->getVolumeShaperState(id);
1032}
1033
Hassan Shojaniacefac142017-02-06 21:02:02 -08001034// Modular DRM
1035status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001036{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001037 // TODO change to ALOGV
1038 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1039 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001040 Mutex::Autolock _l(mLock);
1041 if (mPlayer == NULL) {
1042 return NO_INIT;
1043 }
1044
Hassan Shojania838be392017-05-23 14:14:24 -07001045 // Only allowed it in player's preparing/prepared state.
1046 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1047 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1048 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1049 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1050 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001051 return INVALID_OPERATION;
1052 }
1053
Hassan Shojaniacefac142017-02-06 21:02:02 -08001054 if (drmSessionId.isEmpty()) {
1055 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1056 return INVALID_OPERATION;
1057 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001058
Hassan Shojaniacefac142017-02-06 21:02:02 -08001059 // Passing down to mediaserver mainly for creating the crypto
1060 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1061 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1062
1063 // TODO change to ALOGV
1064 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1065
1066 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001067}
1068
1069status_t MediaPlayer::releaseDrm()
1070{
1071 Mutex::Autolock _l(mLock);
1072 if (mPlayer == NULL) {
1073 return NO_INIT;
1074 }
1075
Hassan Shojaniacefac142017-02-06 21:02:02 -08001076 // Not allowing releaseDrm in an active/resumable state
1077 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1078 MEDIA_PLAYER_PAUSED |
1079 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1080 MEDIA_PLAYER_STATE_ERROR)) {
1081 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001082 return INVALID_OPERATION;
1083 }
1084
Hassan Shojaniacefac142017-02-06 21:02:02 -08001085 status_t status = mPlayer->releaseDrm();
1086 // TODO change to ALOGV
1087 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1088 if (status != OK) {
1089 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1090 // Overriding to OK so the client proceed with its own cleanup
1091 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1092 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001093 }
1094
Hassan Shojaniacefac142017-02-06 21:02:02 -08001095 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001096}
1097
jiabin156c6872017-10-06 09:47:15 -07001098status_t MediaPlayer::setOutputDevice(audio_port_handle_t deviceId)
1099{
1100 Mutex::Autolock _l(mLock);
1101 if (mPlayer == NULL) {
1102 ALOGV("setOutputDevice: player not init");
1103 return NO_INIT;
1104 }
1105 return mPlayer->setOutputDevice(deviceId);
1106}
1107
Robert Wub7f8edc2024-11-04 19:54:38 +00001108status_t MediaPlayer::getRoutedDeviceIds(DeviceIdVector& deviceIds)
jiabin156c6872017-10-06 09:47:15 -07001109{
1110 Mutex::Autolock _l(mLock);
1111 if (mPlayer == NULL) {
Robert Wub7f8edc2024-11-04 19:54:38 +00001112 ALOGV("getRoutedDeviceIds: player not init");
1113 return NO_INIT;
jiabin156c6872017-10-06 09:47:15 -07001114 }
Robert Wub7f8edc2024-11-04 19:54:38 +00001115 return mPlayer->getRoutedDeviceIds(deviceIds);
jiabin156c6872017-10-06 09:47:15 -07001116}
1117
1118status_t MediaPlayer::enableAudioDeviceCallback(bool enabled)
1119{
1120 Mutex::Autolock _l(mLock);
1121 if (mPlayer == NULL) {
1122 ALOGV("addAudioDeviceCallback: player not init");
1123 return NO_INIT;
1124 }
1125 return mPlayer->enableAudioDeviceCallback(enabled);
1126}
1127
Glenn Kasten40bc9062015-03-20 09:09:33 -07001128} // namespace android