blob: 30c50063ff79eae309669adc204baedae1dbb3ee [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
Hassan Shojaniacefac142017-02-06 21:02:02 -080019#define LOG_TAG "MediaPlayerNative"
Marco Nelissendab79b32019-11-18 08:25:47 -080020#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070023#include <inttypes.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
Marco Nelissendab79b32019-11-18 08:25:47 -080029#include <android/IDataSource.h>
Mathias Agopian75624082009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <media/mediaplayer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070032#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080033#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070034#include <media/AVSyncSettings.h>
Andreas Huber2db84552010-01-28 11:19:57 -080035#include <utils/KeyedVector.h>
36#include <utils/String8.h>
Dima Zavin64760242011-05-11 14:15:23 -070037#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070038#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070039
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040namespace android {
41
Ivan Lozano8cf3a072017-08-09 09:01:33 -070042using media::VolumeShaper;
43
Colin Crossb8a9dbb2020-08-27 04:12:26 +000044MediaPlayer::MediaPlayer(const std::string opPackageName) : mOpPackageName(opPackageName)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045{
Steve Block3856b092011-10-20 11:56:00 +010046 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047 mListener = NULL;
48 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070049 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070050 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080052 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080054 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055 mCurrentState = MEDIA_PLAYER_IDLE;
56 mPrepareSync = false;
57 mPrepareStatus = NO_ERROR;
58 mLoop = false;
59 mLeftVolume = mRightVolume = 1.0;
60 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070061 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080062 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Andy Hung8b0bfd92019-12-23 13:11:11 -080063 AudioSystem::acquireAudioSessionId(mAudioSessionId, (pid_t)-1, (uid_t)-1); // always in client.
Eric Laurent8c563ed2010-10-07 18:23:03 -070064 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080065 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066}
67
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068MediaPlayer::~MediaPlayer()
69{
Steve Block3856b092011-10-20 11:56:00 +010070 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070071 if (mAudioAttributesParcel != NULL) {
72 delete mAudioAttributesParcel;
73 mAudioAttributesParcel = NULL;
74 }
Andy Hung8b0bfd92019-12-23 13:11:11 -080075 AudioSystem::releaseAudioSessionId(mAudioSessionId, (pid_t)-1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 disconnect();
77 IPCThreadState::self()->flushCommands();
78}
79
80void MediaPlayer::disconnect()
81{
Steve Block3856b092011-10-20 11:56:00 +010082 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 sp<IMediaPlayer> p;
84 {
85 Mutex::Autolock _l(mLock);
86 p = mPlayer;
87 mPlayer.clear();
88 }
89
90 if (p != 0) {
91 p->disconnect();
92 }
93}
94
95// always call with lock held
96void MediaPlayer::clear_l()
97{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080099 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800101 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800103 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104}
105
106status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
107{
Steve Block3856b092011-10-20 11:56:00 +0100108 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 Mutex::Autolock _l(mLock);
110 mListener = listener;
111 return NO_ERROR;
112}
113
114
Dave Burked681bbb2011-08-30 14:39:17 +0100115status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116{
117 status_t err = UNKNOWN_ERROR;
118 sp<IMediaPlayer> p;
119 { // scope for the lock
120 Mutex::Autolock _l(mLock);
121
Marco Nelissen83ff1432010-03-10 10:53:16 -0800122 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
123 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 return INVALID_OPERATION;
126 }
127
128 clear_l();
129 p = mPlayer;
130 mPlayer = player;
131 if (player != 0) {
132 mCurrentState = MEDIA_PLAYER_INITIALIZED;
133 err = NO_ERROR;
134 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000135 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 }
137 }
138
139 if (p != 0) {
140 p->disconnect();
141 }
142
143 return err;
144}
145
Andreas Huber2db84552010-01-28 11:19:57 -0800146status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800147 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800148 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149{
Steve Block3856b092011-10-20 11:56:00 +0100150 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 status_t err = BAD_VALUE;
152 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700153 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 if (service != 0) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000155 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mOpPackageName));
John Grossmanc795b642012-02-22 15:38:35 -0800156 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800157 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100158 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100159 }
Dave Burke06620672011-09-06 20:39:47 +0100160 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162 }
163 return err;
164}
165
166status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
167{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700168 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700170 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 if (service != 0) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000172 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mOpPackageName));
John Grossmanc795b642012-02-22 15:38:35 -0800173 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
174 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100175 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100176 }
Dave Burke06620672011-09-06 20:39:47 +0100177 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100178 }
179 return err;
180}
181
Chris Watkins99f31602015-03-20 13:06:33 -0700182status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
183{
184 ALOGV("setDataSource(IDataSource)");
185 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700186 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700187 if (service != 0) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000188 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mOpPackageName));
Chris Watkins99f31602015-03-20 13:06:33 -0700189 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
190 (NO_ERROR != player->setDataSource(source))) {
191 player.clear();
192 }
193 err = attachNewPlayer(player);
194 }
195 return err;
196}
197
Byeongjo Park28225ab2019-01-24 20:31:19 +0900198status_t MediaPlayer::setDataSource(const String8& rtpParams)
199{
200 ALOGV("setDataSource(rtpParams)");
201 status_t err = UNKNOWN_ERROR;
202 const sp<IMediaPlayerService> service(getMediaPlayerService());
203 if (service != 0) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000204 sp<IMediaPlayer> player(service->create(this, mAudioSessionId, mOpPackageName));
Byeongjo Park28225ab2019-01-24 20:31:19 +0900205 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
206 (NO_ERROR != player->setDataSource(rtpParams))) {
207 player.clear();
208 }
209 err = attachNewPlayer(player);
210 }
211 return err;
212}
213
Nicolas Catania1d187f12009-05-12 23:25:55 -0700214status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
215{
216 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800217 const bool hasBeenInitialized =
218 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
219 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
220 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700221 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700222 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700223 }
Wei Jia848ebc62016-03-23 14:06:46 -0700224 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700225 return INVALID_OPERATION;
226}
227
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700228status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
229{
Steve Blockb8a80522011-12-20 16:23:08 +0000230 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700231 Mutex::Autolock lock(mLock);
232 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700233 return NO_INIT;
234 }
235 return mPlayer->setMetadataFilter(filter);
236}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700237
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700238status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
239{
Steve Blockb8a80522011-12-20 16:23:08 +0000240 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700241 Mutex::Autolock lock(mLock);
242 if (mPlayer == NULL) {
243 return NO_INIT;
244 }
245 return mPlayer->getMetadata(update_only, apply_filter, metadata);
246}
247
Glenn Kasten11731182011-02-08 17:26:17 -0800248status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800249 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800250{
Steve Block3856b092011-10-20 11:56:00 +0100251 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800252 Mutex::Autolock _l(mLock);
253 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800254 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800255}
256
Wei Jiaed320862017-01-18 14:03:40 -0800257status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
258{
259 ALOGV("getBufferingSettings");
260
261 Mutex::Autolock _l(mLock);
262 if (mPlayer == 0) {
263 return NO_INIT;
264 }
Wei Jia9bb38032017-03-23 18:00:38 -0700265 return mPlayer->getBufferingSettings(buffering);
Wei Jiaed320862017-01-18 14:03:40 -0800266}
267
Wei Jiadc6f3402017-01-09 15:04:18 -0800268status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering)
269{
270 ALOGV("setBufferingSettings");
271
272 Mutex::Autolock _l(mLock);
273 if (mPlayer == 0) {
274 return NO_INIT;
275 }
Wei Jia9bb38032017-03-23 18:00:38 -0700276 return mPlayer->setBufferingSettings(buffering);
Wei Jiadc6f3402017-01-09 15:04:18 -0800277}
278
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279// must call with lock held
280status_t MediaPlayer::prepareAsync_l()
281{
Glenn Kastenb187de12014-12-30 08:18:15 -0800282 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700283 if (mAudioAttributesParcel != NULL) {
284 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700285 } else {
286 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700287 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 mCurrentState = MEDIA_PLAYER_PREPARING;
289 return mPlayer->prepareAsync();
290 }
Wei Jia848ebc62016-03-23 14:06:46 -0700291 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 return INVALID_OPERATION;
293}
294
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700295// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
296// one defined in the Android framework and one provided by the implementation
297// that generated the error. The sync version of prepare returns only 1 error
298// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299status_t MediaPlayer::prepare()
300{
Steve Block3856b092011-10-20 11:56:00 +0100301 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700303 mLockThreadId = getThreadId();
304 if (mPrepareSync) {
305 mLockThreadId = 0;
306 return -EALREADY;
307 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 mPrepareSync = true;
309 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700310 if (ret != NO_ERROR) {
311 mLockThreadId = 0;
312 return ret;
313 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314
315 if (mPrepareSync) {
316 mSignal.wait(mLock); // wait for prepare done
317 mPrepareSync = false;
318 }
Steve Block3856b092011-10-20 11:56:00 +0100319 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700320 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321 return mPrepareStatus;
322}
323
324status_t MediaPlayer::prepareAsync()
325{
Steve Block3856b092011-10-20 11:56:00 +0100326 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 Mutex::Autolock _l(mLock);
328 return prepareAsync_l();
329}
330
331status_t MediaPlayer::start()
332{
Steve Block3856b092011-10-20 11:56:00 +0100333 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700334
335 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700337
338 mLockThreadId = getThreadId();
339
340 if (mCurrentState & MEDIA_PLAYER_STARTED) {
341 ret = NO_ERROR;
342 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
344 mPlayer->setLooping(mLoop);
345 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700346 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700348 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349 if (ret != NO_ERROR) {
350 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
351 } else {
352 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100353 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 }
355 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700356 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700357 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700358 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700360
361 mLockThreadId = 0;
362
363 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364}
365
366status_t MediaPlayer::stop()
367{
Steve Block3856b092011-10-20 11:56:00 +0100368 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369 Mutex::Autolock _l(mLock);
370 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
371 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
372 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
373 status_t ret = mPlayer->stop();
374 if (ret != NO_ERROR) {
375 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
376 } else {
377 mCurrentState = MEDIA_PLAYER_STOPPED;
378 }
379 return ret;
380 }
Wei Jia848ebc62016-03-23 14:06:46 -0700381 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 return INVALID_OPERATION;
383}
384
385status_t MediaPlayer::pause()
386{
Steve Block3856b092011-10-20 11:56:00 +0100387 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800389 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 return NO_ERROR;
391 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
392 status_t ret = mPlayer->pause();
393 if (ret != NO_ERROR) {
394 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
395 } else {
396 mCurrentState = MEDIA_PLAYER_PAUSED;
397 }
398 return ret;
399 }
Wei Jia848ebc62016-03-23 14:06:46 -0700400 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 return INVALID_OPERATION;
402}
403
404bool MediaPlayer::isPlaying()
405{
406 Mutex::Autolock _l(mLock);
407 if (mPlayer != 0) {
408 bool temp = false;
409 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100410 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000412 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700414 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
415 ALOGE("internal/external state mismatch corrected");
416 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 }
418 return temp;
419 }
Steve Block3856b092011-10-20 11:56:00 +0100420 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 return false;
422}
423
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700424status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800425{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700426 ALOGV("setPlaybackSettings: %f %f %d %d",
427 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
428 // Negative speed and pitch does not make sense. Further validation will
429 // be done by the respective mediaplayers.
430 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800431 return BAD_VALUE;
432 }
433 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700434 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
435 return INVALID_OPERATION;
436 }
Wei Jia5be109f2016-06-10 16:15:07 -0700437
438 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
439 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
440 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
441 mPlayer->setLooping(mLoop);
442 mPlayer->setVolume(mLeftVolume, mRightVolume);
443 mPlayer->setAuxEffectSendLevel(mSendLevel);
444 }
445
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700446 status_t err = mPlayer->setPlaybackSettings(rate);
447 if (err == OK) {
448 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
449 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700450 } else if (rate.mSpeed != 0.f
451 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
452 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700453 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800454 }
Wei Jia98160162015-02-04 17:01:11 -0800455 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700456 return err;
457}
458
459status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
460{
461 Mutex::Autolock _l(mLock);
462 if (mPlayer == 0) return INVALID_OPERATION;
463 return mPlayer->getPlaybackSettings(rate);
464}
465
466status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
467{
468 ALOGV("setSyncSettings: %u %u %f %f",
469 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
470 Mutex::Autolock _l(mLock);
471 if (mPlayer == 0) return INVALID_OPERATION;
472 return mPlayer->setSyncSettings(sync, videoFpsHint);
473}
474
475status_t MediaPlayer::getSyncSettings(
476 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
477{
478 Mutex::Autolock _l(mLock);
479 if (mPlayer == 0) return INVALID_OPERATION;
480 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800481}
482
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483status_t MediaPlayer::getVideoWidth(int *w)
484{
Steve Block3856b092011-10-20 11:56:00 +0100485 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 Mutex::Autolock _l(mLock);
487 if (mPlayer == 0) return INVALID_OPERATION;
488 *w = mVideoWidth;
489 return NO_ERROR;
490}
491
492status_t MediaPlayer::getVideoHeight(int *h)
493{
Steve Block3856b092011-10-20 11:56:00 +0100494 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495 Mutex::Autolock _l(mLock);
496 if (mPlayer == 0) return INVALID_OPERATION;
497 *h = mVideoHeight;
498 return NO_ERROR;
499}
500
501status_t MediaPlayer::getCurrentPosition(int *msec)
502{
Steve Block3856b092011-10-20 11:56:00 +0100503 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 Mutex::Autolock _l(mLock);
505 if (mPlayer != 0) {
506 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100507 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508 *msec = mCurrentPosition;
509 return NO_ERROR;
510 }
511 return mPlayer->getCurrentPosition(msec);
512 }
513 return INVALID_OPERATION;
514}
515
516status_t MediaPlayer::getDuration_l(int *msec)
517{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800518 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800519 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
520 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800522 int durationMs;
523 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700524
525 if (ret != OK) {
526 // Do not enter error state just because no duration was available.
527 durationMs = -1;
528 ret = OK;
529 }
530
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800531 if (msec) {
532 *msec = durationMs;
533 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534 return ret;
535 }
Wei Jia848ebc62016-03-23 14:06:46 -0700536 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
537 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 return INVALID_OPERATION;
539}
540
541status_t MediaPlayer::getDuration(int *msec)
542{
543 Mutex::Autolock _l(mLock);
544 return getDuration_l(msec);
545}
546
Wei Jiac5de0912016-11-18 10:22:14 -0800547status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
Wei Jiac5de0912016-11-18 10:22:14 -0800549 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800550 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
551 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000553 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800556
557 int durationMs;
558 status_t err = mPlayer->getDuration(&durationMs);
559
560 if (err != OK) {
561 ALOGW("Stream has no duration and is therefore not seekable.");
562 return err;
563 }
564
565 if (msec > durationMs) {
566 ALOGW("Attempt to seek to past end of file: request = %d, "
567 "durationMs = %d",
568 msec,
569 durationMs);
570
571 msec = durationMs;
572 }
573
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 // cache duration
575 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800576 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800579 mSeekMode = mode;
580 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 }
582 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800583 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 return NO_ERROR;
585 }
586 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800587 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
588 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 return INVALID_OPERATION;
590}
591
Wei Jiac5de0912016-11-18 10:22:14 -0800592status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700594 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800596 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700597 mLockThreadId = 0;
598
599 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600}
601
Wei Jia52c28512017-09-13 18:17:51 -0700602status_t MediaPlayer::notifyAt(int64_t mediaTimeUs)
603{
604 Mutex::Autolock _l(mLock);
605 if (mPlayer != 0) {
606 return mPlayer->notifyAt(mediaTimeUs);
607 }
608 return INVALID_OPERATION;
609}
610
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700611status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 mLoop = false;
614 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
615 mPrepareSync = false;
616 if (mPlayer != 0) {
617 status_t ret = mPlayer->reset();
618 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000619 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
621 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700622 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 mCurrentState = MEDIA_PLAYER_IDLE;
624 }
James Donga1680bc2010-11-18 12:23:58 -0800625 // setDataSource has to be called again to create a
626 // new mediaplayer.
627 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628 return ret;
629 }
630 clear_l();
631 return NO_ERROR;
632}
633
John Grossmanc795b642012-02-22 15:38:35 -0800634status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
635 Mutex::Autolock _l(mLock);
636
637 if (player == NULL) {
638 return UNKNOWN_ERROR;
639 }
640
641 if (mRetransmitEndpointValid) {
642 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
643 }
644
645 return OK;
646}
647
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700648status_t MediaPlayer::reset()
649{
Steve Block3856b092011-10-20 11:56:00 +0100650 ALOGV("reset");
Wonsik Kim5f648972017-07-21 15:42:59 -0700651 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700652 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700653 status_t result = reset_l();
654 mLockThreadId = 0;
655
656 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700657}
658
Glenn Kastenfff6d712012-01-12 16:38:12 -0800659status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660{
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 Mutex::Autolock _l(mLock);
663 if (mStreamType == type) return NO_ERROR;
664 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
665 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
666 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000667 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668 return INVALID_OPERATION;
669 }
670 // cache
671 mStreamType = type;
672 return OK;
673}
674
John Spurlockde9453f2014-03-19 13:05:45 -0400675status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
676{
677 ALOGV("getAudioStreamType");
678 Mutex::Autolock _l(mLock);
679 *type = mStreamType;
680 return OK;
681}
682
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683status_t MediaPlayer::setLooping(int loop)
684{
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686 Mutex::Autolock _l(mLock);
687 mLoop = (loop != 0);
688 if (mPlayer != 0) {
689 return mPlayer->setLooping(loop);
690 }
691 return OK;
692}
693
694bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696 Mutex::Autolock _l(mLock);
697 if (mPlayer != 0) {
698 return mLoop;
699 }
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701 return false;
702}
703
704status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
705{
Steve Block3856b092011-10-20 11:56:00 +0100706 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 Mutex::Autolock _l(mLock);
708 mLeftVolume = leftVolume;
709 mRightVolume = rightVolume;
710 if (mPlayer != 0) {
711 return mPlayer->setVolume(leftVolume, rightVolume);
712 }
713 return OK;
714}
715
Glenn Kastend848eb42016-03-08 13:42:11 -0800716status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700717{
Steve Block3856b092011-10-20 11:56:00 +0100718 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700719 Mutex::Autolock _l(mLock);
720 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000721 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700722 return INVALID_OPERATION;
723 }
724 if (sessionId < 0) {
725 return BAD_VALUE;
726 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700727 if (sessionId != mAudioSessionId) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800728 AudioSystem::acquireAudioSessionId(sessionId, (pid_t)-1, (uid_t)-1);
729 AudioSystem::releaseAudioSessionId(mAudioSessionId, (pid_t)-1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700730 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700731 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700732 return NO_ERROR;
733}
734
Glenn Kastend848eb42016-03-08 13:42:11 -0800735audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700736{
737 Mutex::Autolock _l(mLock);
738 return mAudioSessionId;
739}
740
Eric Laurent2beeb502010-07-16 07:43:46 -0700741status_t MediaPlayer::setAuxEffectSendLevel(float level)
742{
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700744 Mutex::Autolock _l(mLock);
745 mSendLevel = level;
746 if (mPlayer != 0) {
747 return mPlayer->setAuxEffectSendLevel(level);
748 }
749 return OK;
750}
751
752status_t MediaPlayer::attachAuxEffect(int effectId)
753{
Steve Block3856b092011-10-20 11:56:00 +0100754 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700755 Mutex::Autolock _l(mLock);
756 if (mPlayer == 0 ||
757 (mCurrentState & MEDIA_PLAYER_IDLE) ||
758 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700759 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700760 return INVALID_OPERATION;
761 }
762
763 return mPlayer->attachAuxEffect(effectId);
764}
765
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700766// always call with lock held
767status_t MediaPlayer::checkStateForKeySet_l(int key)
768{
769 switch(key) {
770 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
771 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
772 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
773 // Can't change the audio attributes after prepare
774 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
775 return INVALID_OPERATION;
776 }
777 break;
778 default:
779 // parameter doesn't require player state check
780 break;
781 }
782 return OK;
783}
784
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700785status_t MediaPlayer::setParameter(int key, const Parcel& request)
786{
Steve Block3856b092011-10-20 11:56:00 +0100787 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700788 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700789 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700790 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700791 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700792 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700793 switch (key) {
794 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700795 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700796 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
797 mAudioAttributesParcel = new Parcel();
798 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700799 status = OK;
800 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700801 default:
Eric Laurent43562692015-07-15 16:49:07 -0700802 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
803 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700804 }
Eric Laurent43562692015-07-15 16:49:07 -0700805
806 if (mPlayer != NULL) {
807 status = mPlayer->setParameter(key, request);
808 }
809 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700810}
811
812status_t MediaPlayer::getParameter(int key, Parcel *reply)
813{
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700815 Mutex::Autolock _l(mLock);
816 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800817 status_t status = mPlayer->getParameter(key, reply);
818 if (status != OK) {
819 ALOGD("getParameter returns %d", status);
820 }
821 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700822 }
Steve Block3856b092011-10-20 11:56:00 +0100823 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700824 return INVALID_OPERATION;
825}
826
John Grossmanc795b642012-02-22 15:38:35 -0800827status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
828 uint16_t port) {
829 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
830 addrString ? addrString : "(null)", port);
831
832 Mutex::Autolock _l(mLock);
833 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
834 return INVALID_OPERATION;
835
836 if (NULL == addrString) {
837 mRetransmitEndpointValid = false;
838 return OK;
839 }
840
841 struct in_addr saddr;
842 if(!inet_aton(addrString, &saddr)) {
843 return BAD_VALUE;
844 }
845
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800846 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800847 mRetransmitEndpoint.sin_family = AF_INET;
848 mRetransmitEndpoint.sin_addr = saddr;
849 mRetransmitEndpoint.sin_port = htons(port);
850 mRetransmitEndpointValid = true;
851
852 return OK;
853}
854
Gloria Wangb483c472011-04-11 17:23:27 -0700855void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856{
Steve Block3856b092011-10-20 11:56:00 +0100857 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800858 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700859 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800860
861 // TODO: In the future, we might be on the same thread if the app is
862 // running in the same process as the media server. In that case,
863 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700864 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700865 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700866 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700867 // FIXME: Remember, this is a hack, it's not even a hack that is applied
868 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700869 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700870 mLock.lock();
871 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700872 }
Jason Sams1af452f2009-03-24 18:45:22 -0700873
Eric Laurent3b268442010-08-03 07:49:49 -0700874 // Allows calls from JNI in idle state to notify errors
875 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100876 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700877 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800878 return;
879 }
880
881 switch (msg) {
882 case MEDIA_NOP: // interface test message
883 break;
884 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800885 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 mCurrentState = MEDIA_PLAYER_PREPARED;
887 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100888 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800889 mPrepareSync = false;
890 mPrepareStatus = NO_ERROR;
891 mSignal.signal();
892 }
893 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800894 case MEDIA_DRM_INFO:
895 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
896 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800897 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100898 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700899 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000900 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700901 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800902 if (!mLoop) {
903 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
904 }
905 break;
906 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700907 // Always log errors.
908 // ext1: Media framework error code.
909 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000910 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
912 if (mPrepareSync)
913 {
Steve Block3856b092011-10-20 11:56:00 +0100914 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800915 mPrepareSync = false;
916 mPrepareStatus = ext1;
917 mSignal.signal();
918 send = false;
919 }
920 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700921 case MEDIA_INFO:
922 // ext1: Media framework error code.
923 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800924 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000925 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800926 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700927 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100929 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800930 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
931 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800933 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
934 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935 }
936 else {
Steve Block3856b092011-10-20 11:56:00 +0100937 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800939 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800940 }
941 break;
942 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 break;
945 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 mVideoWidth = ext1;
948 mVideoHeight = ext2;
949 break;
Wei Jia52c28512017-09-13 18:17:51 -0700950 case MEDIA_NOTIFY_TIME:
951 ALOGV("Received notify time message");
952 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700953 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700955 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700956 case MEDIA_SUBTITLE_DATA:
957 ALOGV("Received subtitle data message");
958 break;
Robert Shih08528432015-04-08 09:06:54 -0700959 case MEDIA_META_DATA:
960 ALOGV("Received timed metadata message");
961 break;
Kim Sungyeond3c6b322018-03-02 14:41:19 +0900962 case MEDIA_IMS_RX_NOTICE:
963 ALOGV("Received IMS Rx notice message");
964 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800965 default:
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 break;
968 }
969
970 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700971 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972
973 // this prevents re-entrant calls into client code
974 if ((listener != 0) && send) {
975 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100976 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700977 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100978 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 }
980}
981
James Dongdd172fc2010-01-15 18:13:58 -0800982void MediaPlayer::died()
983{
Steve Block3856b092011-10-20 11:56:00 +0100984 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800985 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
986}
987
Marco Nelissen6b74d672012-02-28 16:07:44 -0800988status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700989 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800990 if (mPlayer == NULL) {
991 return NO_INIT;
992 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700993
994 if (next != NULL && !(next->mCurrentState &
995 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
996 ALOGE("next player is not prepared");
997 return INVALID_OPERATION;
998 }
999
Marco Nelissen6b74d672012-02-28 16:07:44 -08001000 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
1001}
1002
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001003VolumeShaper::Status MediaPlayer::applyVolumeShaper(
1004 const sp<VolumeShaper::Configuration>& configuration,
1005 const sp<VolumeShaper::Operation>& operation)
1006{
1007 Mutex::Autolock _l(mLock);
1008 if (mPlayer == nullptr) {
1009 return VolumeShaper::Status(NO_INIT);
1010 }
1011 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
1012 return status;
1013}
1014
1015sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
1016{
1017 Mutex::Autolock _l(mLock);
1018 if (mPlayer == nullptr) {
1019 return nullptr;
1020 }
1021 return mPlayer->getVolumeShaperState(id);
1022}
1023
Hassan Shojaniacefac142017-02-06 21:02:02 -08001024// Modular DRM
1025status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001026{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001027 // TODO change to ALOGV
1028 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1029 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001030 Mutex::Autolock _l(mLock);
1031 if (mPlayer == NULL) {
1032 return NO_INIT;
1033 }
1034
Hassan Shojania838be392017-05-23 14:14:24 -07001035 // Only allowed it in player's preparing/prepared state.
1036 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1037 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1038 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1039 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1040 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001041 return INVALID_OPERATION;
1042 }
1043
Hassan Shojaniacefac142017-02-06 21:02:02 -08001044 if (drmSessionId.isEmpty()) {
1045 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1046 return INVALID_OPERATION;
1047 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001048
Hassan Shojaniacefac142017-02-06 21:02:02 -08001049 // Passing down to mediaserver mainly for creating the crypto
1050 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1051 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1052
1053 // TODO change to ALOGV
1054 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1055
1056 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001057}
1058
1059status_t MediaPlayer::releaseDrm()
1060{
1061 Mutex::Autolock _l(mLock);
1062 if (mPlayer == NULL) {
1063 return NO_INIT;
1064 }
1065
Hassan Shojaniacefac142017-02-06 21:02:02 -08001066 // Not allowing releaseDrm in an active/resumable state
1067 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1068 MEDIA_PLAYER_PAUSED |
1069 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1070 MEDIA_PLAYER_STATE_ERROR)) {
1071 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001072 return INVALID_OPERATION;
1073 }
1074
Hassan Shojaniacefac142017-02-06 21:02:02 -08001075 status_t status = mPlayer->releaseDrm();
1076 // TODO change to ALOGV
1077 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1078 if (status != OK) {
1079 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1080 // Overriding to OK so the client proceed with its own cleanup
1081 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1082 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001083 }
1084
Hassan Shojaniacefac142017-02-06 21:02:02 -08001085 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001086}
1087
jiabin156c6872017-10-06 09:47:15 -07001088status_t MediaPlayer::setOutputDevice(audio_port_handle_t deviceId)
1089{
1090 Mutex::Autolock _l(mLock);
1091 if (mPlayer == NULL) {
1092 ALOGV("setOutputDevice: player not init");
1093 return NO_INIT;
1094 }
1095 return mPlayer->setOutputDevice(deviceId);
1096}
1097
1098audio_port_handle_t MediaPlayer::getRoutedDeviceId()
1099{
1100 Mutex::Autolock _l(mLock);
1101 if (mPlayer == NULL) {
1102 ALOGV("getRoutedDeviceId: player not init");
1103 return AUDIO_PORT_HANDLE_NONE;
1104 }
1105 audio_port_handle_t deviceId;
1106 status_t status = mPlayer->getRoutedDeviceId(&deviceId);
1107 if (status != NO_ERROR) {
1108 return AUDIO_PORT_HANDLE_NONE;
1109 }
1110 return deviceId;
1111}
1112
1113status_t MediaPlayer::enableAudioDeviceCallback(bool enabled)
1114{
1115 Mutex::Autolock _l(mLock);
1116 if (mPlayer == NULL) {
1117 ALOGV("addAudioDeviceCallback: player not init");
1118 return NO_INIT;
1119 }
1120 return mPlayer->enableAudioDeviceCallback(enabled);
1121}
1122
Glenn Kasten40bc9062015-03-20 09:09:33 -07001123} // namespace android