blob: 6bba1f10f80f3cf61e80a83b81b3f3755603f4ad [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
19#define LOG_TAG "MediaPlayer"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080020
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070022#include <inttypes.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
Mathias Agopian75624082009-05-19 19:08:10 -070029#include <binder/IServiceManager.h>
30#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080032#include <gui/Surface.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034#include <media/mediaplayer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070035#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080036#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070037#include <media/AVSyncSettings.h>
Chris Watkins99f31602015-03-20 13:06:33 -070038#include <media/IDataSource.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Mathias Agopian75624082009-05-19 19:08:10 -070040#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041
Andreas Huber2db84552010-01-28 11:19:57 -080042#include <utils/KeyedVector.h>
43#include <utils/String8.h>
44
Dima Zavin64760242011-05-11 14:15:23 -070045#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070046#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070047
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
49
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050MediaPlayer::MediaPlayer()
51{
Steve Block3856b092011-10-20 11:56:00 +010052 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 mListener = NULL;
54 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070055 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070056 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080058 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080059 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080060 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061 mCurrentState = MEDIA_PLAYER_IDLE;
62 mPrepareSync = false;
63 mPrepareStatus = NO_ERROR;
64 mLoop = false;
65 mLeftVolume = mRightVolume = 1.0;
66 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070067 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080068 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Marco Nelissend457c972014-02-11 08:47:07 -080069 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070070 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080071 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072}
73
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080074MediaPlayer::~MediaPlayer()
75{
Steve Block3856b092011-10-20 11:56:00 +010076 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070077 if (mAudioAttributesParcel != NULL) {
78 delete mAudioAttributesParcel;
79 mAudioAttributesParcel = NULL;
80 }
Marco Nelissend457c972014-02-11 08:47:07 -080081 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080082 disconnect();
83 IPCThreadState::self()->flushCommands();
84}
85
86void MediaPlayer::disconnect()
87{
Steve Block3856b092011-10-20 11:56:00 +010088 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089 sp<IMediaPlayer> p;
90 {
91 Mutex::Autolock _l(mLock);
92 p = mPlayer;
93 mPlayer.clear();
94 }
95
96 if (p != 0) {
97 p->disconnect();
98 }
99}
100
101// always call with lock held
102void MediaPlayer::clear_l()
103{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800105 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800107 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800109 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110}
111
112status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
113{
Steve Block3856b092011-10-20 11:56:00 +0100114 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115 Mutex::Autolock _l(mLock);
116 mListener = listener;
117 return NO_ERROR;
118}
119
120
Dave Burked681bbb2011-08-30 14:39:17 +0100121status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800122{
123 status_t err = UNKNOWN_ERROR;
124 sp<IMediaPlayer> p;
125 { // scope for the lock
126 Mutex::Autolock _l(mLock);
127
Marco Nelissen83ff1432010-03-10 10:53:16 -0800128 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
129 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000130 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131 return INVALID_OPERATION;
132 }
133
134 clear_l();
135 p = mPlayer;
136 mPlayer = player;
137 if (player != 0) {
138 mCurrentState = MEDIA_PLAYER_INITIALIZED;
139 err = NO_ERROR;
140 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000141 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 }
143 }
144
145 if (p != 0) {
146 p->disconnect();
147 }
148
149 return err;
150}
151
Andreas Huber2db84552010-01-28 11:19:57 -0800152status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800153 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800154 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155{
Steve Block3856b092011-10-20 11:56:00 +0100156 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 status_t err = BAD_VALUE;
158 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700159 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800161 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800162 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800163 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100164 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100165 }
Dave Burke06620672011-09-06 20:39:47 +0100166 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 }
168 }
169 return err;
170}
171
172status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
173{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700174 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700176 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800178 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800179 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
180 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100181 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100182 }
Dave Burke06620672011-09-06 20:39:47 +0100183 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100184 }
185 return err;
186}
187
Chris Watkins99f31602015-03-20 13:06:33 -0700188status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
189{
190 ALOGV("setDataSource(IDataSource)");
191 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700192 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700193 if (service != 0) {
194 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
195 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
196 (NO_ERROR != player->setDataSource(source))) {
197 player.clear();
198 }
199 err = attachNewPlayer(player);
200 }
201 return err;
202}
203
Nicolas Catania1d187f12009-05-12 23:25:55 -0700204status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
205{
206 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800207 const bool hasBeenInitialized =
208 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
209 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
210 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700211 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700212 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700213 }
Wei Jia848ebc62016-03-23 14:06:46 -0700214 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700215 return INVALID_OPERATION;
216}
217
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700218status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
219{
Steve Blockb8a80522011-12-20 16:23:08 +0000220 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700221 Mutex::Autolock lock(mLock);
222 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700223 return NO_INIT;
224 }
225 return mPlayer->setMetadataFilter(filter);
226}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700227
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700228status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
229{
Steve Blockb8a80522011-12-20 16:23:08 +0000230 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700231 Mutex::Autolock lock(mLock);
232 if (mPlayer == NULL) {
233 return NO_INIT;
234 }
235 return mPlayer->getMetadata(update_only, apply_filter, metadata);
236}
237
Glenn Kasten11731182011-02-08 17:26:17 -0800238status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800239 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800240{
Steve Block3856b092011-10-20 11:56:00 +0100241 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800242 Mutex::Autolock _l(mLock);
243 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800244 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800245}
246
Wei Jiadc6f3402017-01-09 15:04:18 -0800247status_t MediaPlayer::getDefaultBufferingSettings(BufferingSettings* buffering /* nonnull */)
248{
249 ALOGV("getDefaultBufferingSettings");
250
251 Mutex::Autolock _l(mLock);
252 if (mPlayer == 0) {
253 return NO_INIT;
254 }
255 return mPlayer->getDefaultBufferingSettings(buffering);
256}
257
258status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering)
259{
260 ALOGV("setBufferingSettings");
261
262 Mutex::Autolock _l(mLock);
263 if (mPlayer == 0) {
264 return NO_INIT;
265 }
266 return mPlayer->setBufferingSettings(buffering);
267}
268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269// must call with lock held
270status_t MediaPlayer::prepareAsync_l()
271{
Glenn Kastenb187de12014-12-30 08:18:15 -0800272 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700273 if (mAudioAttributesParcel != NULL) {
274 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700275 } else {
276 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700277 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800278 mCurrentState = MEDIA_PLAYER_PREPARING;
279 return mPlayer->prepareAsync();
280 }
Wei Jia848ebc62016-03-23 14:06:46 -0700281 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 return INVALID_OPERATION;
283}
284
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700285// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
286// one defined in the Android framework and one provided by the implementation
287// that generated the error. The sync version of prepare returns only 1 error
288// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289status_t MediaPlayer::prepare()
290{
Steve Block3856b092011-10-20 11:56:00 +0100291 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700293 mLockThreadId = getThreadId();
294 if (mPrepareSync) {
295 mLockThreadId = 0;
296 return -EALREADY;
297 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 mPrepareSync = true;
299 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700300 if (ret != NO_ERROR) {
301 mLockThreadId = 0;
302 return ret;
303 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304
305 if (mPrepareSync) {
306 mSignal.wait(mLock); // wait for prepare done
307 mPrepareSync = false;
308 }
Steve Block3856b092011-10-20 11:56:00 +0100309 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700310 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 return mPrepareStatus;
312}
313
314status_t MediaPlayer::prepareAsync()
315{
Steve Block3856b092011-10-20 11:56:00 +0100316 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 Mutex::Autolock _l(mLock);
318 return prepareAsync_l();
319}
320
321status_t MediaPlayer::start()
322{
Steve Block3856b092011-10-20 11:56:00 +0100323 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700324
325 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700327
328 mLockThreadId = getThreadId();
329
330 if (mCurrentState & MEDIA_PLAYER_STARTED) {
331 ret = NO_ERROR;
332 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
334 mPlayer->setLooping(mLoop);
335 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700336 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700338 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339 if (ret != NO_ERROR) {
340 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
341 } else {
342 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100343 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 }
345 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700346 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700347 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700348 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700350
351 mLockThreadId = 0;
352
353 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354}
355
356status_t MediaPlayer::stop()
357{
Steve Block3856b092011-10-20 11:56:00 +0100358 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 Mutex::Autolock _l(mLock);
360 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
361 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
362 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
363 status_t ret = mPlayer->stop();
364 if (ret != NO_ERROR) {
365 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
366 } else {
367 mCurrentState = MEDIA_PLAYER_STOPPED;
368 }
369 return ret;
370 }
Wei Jia848ebc62016-03-23 14:06:46 -0700371 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 return INVALID_OPERATION;
373}
374
375status_t MediaPlayer::pause()
376{
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800379 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 return NO_ERROR;
381 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
382 status_t ret = mPlayer->pause();
383 if (ret != NO_ERROR) {
384 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
385 } else {
386 mCurrentState = MEDIA_PLAYER_PAUSED;
387 }
388 return ret;
389 }
Wei Jia848ebc62016-03-23 14:06:46 -0700390 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 return INVALID_OPERATION;
392}
393
394bool MediaPlayer::isPlaying()
395{
396 Mutex::Autolock _l(mLock);
397 if (mPlayer != 0) {
398 bool temp = false;
399 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100400 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000402 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700404 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
405 ALOGE("internal/external state mismatch corrected");
406 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 }
408 return temp;
409 }
Steve Block3856b092011-10-20 11:56:00 +0100410 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 return false;
412}
413
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700414status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800415{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700416 ALOGV("setPlaybackSettings: %f %f %d %d",
417 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
418 // Negative speed and pitch does not make sense. Further validation will
419 // be done by the respective mediaplayers.
420 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800421 return BAD_VALUE;
422 }
423 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700424 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
425 return INVALID_OPERATION;
426 }
Wei Jia5be109f2016-06-10 16:15:07 -0700427
428 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
429 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
430 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
431 mPlayer->setLooping(mLoop);
432 mPlayer->setVolume(mLeftVolume, mRightVolume);
433 mPlayer->setAuxEffectSendLevel(mSendLevel);
434 }
435
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700436 status_t err = mPlayer->setPlaybackSettings(rate);
437 if (err == OK) {
438 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
439 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700440 } else if (rate.mSpeed != 0.f
441 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
442 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700443 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800444 }
Wei Jia98160162015-02-04 17:01:11 -0800445 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700446 return err;
447}
448
449status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
450{
451 Mutex::Autolock _l(mLock);
452 if (mPlayer == 0) return INVALID_OPERATION;
453 return mPlayer->getPlaybackSettings(rate);
454}
455
456status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
457{
458 ALOGV("setSyncSettings: %u %u %f %f",
459 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
460 Mutex::Autolock _l(mLock);
461 if (mPlayer == 0) return INVALID_OPERATION;
462 return mPlayer->setSyncSettings(sync, videoFpsHint);
463}
464
465status_t MediaPlayer::getSyncSettings(
466 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
467{
468 Mutex::Autolock _l(mLock);
469 if (mPlayer == 0) return INVALID_OPERATION;
470 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800471}
472
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473status_t MediaPlayer::getVideoWidth(int *w)
474{
Steve Block3856b092011-10-20 11:56:00 +0100475 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 Mutex::Autolock _l(mLock);
477 if (mPlayer == 0) return INVALID_OPERATION;
478 *w = mVideoWidth;
479 return NO_ERROR;
480}
481
482status_t MediaPlayer::getVideoHeight(int *h)
483{
Steve Block3856b092011-10-20 11:56:00 +0100484 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 Mutex::Autolock _l(mLock);
486 if (mPlayer == 0) return INVALID_OPERATION;
487 *h = mVideoHeight;
488 return NO_ERROR;
489}
490
491status_t MediaPlayer::getCurrentPosition(int *msec)
492{
Steve Block3856b092011-10-20 11:56:00 +0100493 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 Mutex::Autolock _l(mLock);
495 if (mPlayer != 0) {
496 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100497 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 *msec = mCurrentPosition;
499 return NO_ERROR;
500 }
501 return mPlayer->getCurrentPosition(msec);
502 }
503 return INVALID_OPERATION;
504}
505
506status_t MediaPlayer::getDuration_l(int *msec)
507{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800508 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800509 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
510 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800512 int durationMs;
513 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700514
515 if (ret != OK) {
516 // Do not enter error state just because no duration was available.
517 durationMs = -1;
518 ret = OK;
519 }
520
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800521 if (msec) {
522 *msec = durationMs;
523 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524 return ret;
525 }
Wei Jia848ebc62016-03-23 14:06:46 -0700526 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
527 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 return INVALID_OPERATION;
529}
530
531status_t MediaPlayer::getDuration(int *msec)
532{
533 Mutex::Autolock _l(mLock);
534 return getDuration_l(msec);
535}
536
Wei Jiac5de0912016-11-18 10:22:14 -0800537status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538{
Wei Jiac5de0912016-11-18 10:22:14 -0800539 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800540 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
541 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000543 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800546
547 int durationMs;
548 status_t err = mPlayer->getDuration(&durationMs);
549
550 if (err != OK) {
551 ALOGW("Stream has no duration and is therefore not seekable.");
552 return err;
553 }
554
555 if (msec > durationMs) {
556 ALOGW("Attempt to seek to past end of file: request = %d, "
557 "durationMs = %d",
558 msec,
559 durationMs);
560
561 msec = durationMs;
562 }
563
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 // cache duration
565 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800566 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800569 mSeekMode = mode;
570 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 }
572 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800573 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 return NO_ERROR;
575 }
576 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800577 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
578 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 return INVALID_OPERATION;
580}
581
Wei Jiac5de0912016-11-18 10:22:14 -0800582status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700584 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800586 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700587 mLockThreadId = 0;
588
589 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590}
591
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700592status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800594 mLoop = false;
595 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
596 mPrepareSync = false;
597 if (mPlayer != 0) {
598 status_t ret = mPlayer->reset();
599 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000600 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
602 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700603 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 mCurrentState = MEDIA_PLAYER_IDLE;
605 }
James Donga1680bc2010-11-18 12:23:58 -0800606 // setDataSource has to be called again to create a
607 // new mediaplayer.
608 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609 return ret;
610 }
611 clear_l();
612 return NO_ERROR;
613}
614
John Grossmanc795b642012-02-22 15:38:35 -0800615status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
616 Mutex::Autolock _l(mLock);
617
618 if (player == NULL) {
619 return UNKNOWN_ERROR;
620 }
621
622 if (mRetransmitEndpointValid) {
623 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
624 }
625
626 return OK;
627}
628
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700629status_t MediaPlayer::reset()
630{
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700632 Mutex::Autolock _l(mLock);
633 return reset_l();
634}
635
Glenn Kastenfff6d712012-01-12 16:38:12 -0800636status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637{
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639 Mutex::Autolock _l(mLock);
640 if (mStreamType == type) return NO_ERROR;
641 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
642 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
643 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000644 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 return INVALID_OPERATION;
646 }
647 // cache
648 mStreamType = type;
649 return OK;
650}
651
John Spurlockde9453f2014-03-19 13:05:45 -0400652status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
653{
654 ALOGV("getAudioStreamType");
655 Mutex::Autolock _l(mLock);
656 *type = mStreamType;
657 return OK;
658}
659
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660status_t MediaPlayer::setLooping(int loop)
661{
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663 Mutex::Autolock _l(mLock);
664 mLoop = (loop != 0);
665 if (mPlayer != 0) {
666 return mPlayer->setLooping(loop);
667 }
668 return OK;
669}
670
671bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100672 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673 Mutex::Autolock _l(mLock);
674 if (mPlayer != 0) {
675 return mLoop;
676 }
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 return false;
679}
680
681status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
682{
Steve Block3856b092011-10-20 11:56:00 +0100683 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684 Mutex::Autolock _l(mLock);
685 mLeftVolume = leftVolume;
686 mRightVolume = rightVolume;
687 if (mPlayer != 0) {
688 return mPlayer->setVolume(leftVolume, rightVolume);
689 }
690 return OK;
691}
692
Glenn Kastend848eb42016-03-08 13:42:11 -0800693status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700694{
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700696 Mutex::Autolock _l(mLock);
697 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000698 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700699 return INVALID_OPERATION;
700 }
701 if (sessionId < 0) {
702 return BAD_VALUE;
703 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700704 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800705 AudioSystem::acquireAudioSessionId(sessionId, -1);
706 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700707 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700708 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700709 return NO_ERROR;
710}
711
Glenn Kastend848eb42016-03-08 13:42:11 -0800712audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700713{
714 Mutex::Autolock _l(mLock);
715 return mAudioSessionId;
716}
717
Eric Laurent2beeb502010-07-16 07:43:46 -0700718status_t MediaPlayer::setAuxEffectSendLevel(float level)
719{
Steve Block3856b092011-10-20 11:56:00 +0100720 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700721 Mutex::Autolock _l(mLock);
722 mSendLevel = level;
723 if (mPlayer != 0) {
724 return mPlayer->setAuxEffectSendLevel(level);
725 }
726 return OK;
727}
728
729status_t MediaPlayer::attachAuxEffect(int effectId)
730{
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700732 Mutex::Autolock _l(mLock);
733 if (mPlayer == 0 ||
734 (mCurrentState & MEDIA_PLAYER_IDLE) ||
735 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700736 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700737 return INVALID_OPERATION;
738 }
739
740 return mPlayer->attachAuxEffect(effectId);
741}
742
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700743// always call with lock held
744status_t MediaPlayer::checkStateForKeySet_l(int key)
745{
746 switch(key) {
747 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
748 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
749 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
750 // Can't change the audio attributes after prepare
751 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
752 return INVALID_OPERATION;
753 }
754 break;
755 default:
756 // parameter doesn't require player state check
757 break;
758 }
759 return OK;
760}
761
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700762status_t MediaPlayer::setParameter(int key, const Parcel& request)
763{
Steve Block3856b092011-10-20 11:56:00 +0100764 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700765 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700766 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700767 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700768 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700769 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700770 switch (key) {
771 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700772 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700773 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
774 mAudioAttributesParcel = new Parcel();
775 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700776 status = OK;
777 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700778 default:
Eric Laurent43562692015-07-15 16:49:07 -0700779 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
780 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700781 }
Eric Laurent43562692015-07-15 16:49:07 -0700782
783 if (mPlayer != NULL) {
784 status = mPlayer->setParameter(key, request);
785 }
786 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700787}
788
789status_t MediaPlayer::getParameter(int key, Parcel *reply)
790{
Steve Block3856b092011-10-20 11:56:00 +0100791 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700792 Mutex::Autolock _l(mLock);
793 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700794 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700795 }
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700797 return INVALID_OPERATION;
798}
799
John Grossmanc795b642012-02-22 15:38:35 -0800800status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
801 uint16_t port) {
802 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
803 addrString ? addrString : "(null)", port);
804
805 Mutex::Autolock _l(mLock);
806 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
807 return INVALID_OPERATION;
808
809 if (NULL == addrString) {
810 mRetransmitEndpointValid = false;
811 return OK;
812 }
813
814 struct in_addr saddr;
815 if(!inet_aton(addrString, &saddr)) {
816 return BAD_VALUE;
817 }
818
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800819 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800820 mRetransmitEndpoint.sin_family = AF_INET;
821 mRetransmitEndpoint.sin_addr = saddr;
822 mRetransmitEndpoint.sin_port = htons(port);
823 mRetransmitEndpointValid = true;
824
825 return OK;
826}
827
Gloria Wangb483c472011-04-11 17:23:27 -0700828void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800829{
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700832 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800833
834 // TODO: In the future, we might be on the same thread if the app is
835 // running in the same process as the media server. In that case,
836 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700837 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700838 // The threadId hack below works around this for the care of prepare,
839 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700840 // FIXME: Remember, this is a hack, it's not even a hack that is applied
841 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700842 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700843 mLock.lock();
844 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700845 }
Jason Sams1af452f2009-03-24 18:45:22 -0700846
Eric Laurent3b268442010-08-03 07:49:49 -0700847 // Allows calls from JNI in idle state to notify errors
848 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100849 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700850 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800851 return;
852 }
853
854 switch (msg) {
855 case MEDIA_NOP: // interface test message
856 break;
857 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100858 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800859 mCurrentState = MEDIA_PLAYER_PREPARED;
860 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800862 mPrepareSync = false;
863 mPrepareStatus = NO_ERROR;
864 mSignal.signal();
865 }
866 break;
867 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700869 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000870 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700871 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 if (!mLoop) {
873 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
874 }
875 break;
876 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700877 // Always log errors.
878 // ext1: Media framework error code.
879 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000880 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
882 if (mPrepareSync)
883 {
Steve Block3856b092011-10-20 11:56:00 +0100884 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800885 mPrepareSync = false;
886 mPrepareStatus = ext1;
887 mSignal.signal();
888 send = false;
889 }
890 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700891 case MEDIA_INFO:
892 // ext1: Media framework error code.
893 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800894 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000895 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800896 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700897 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800900 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
901 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800902 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800903 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
904 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800905 }
906 else {
Steve Block3856b092011-10-20 11:56:00 +0100907 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800909 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800910 }
911 break;
912 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100913 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 break;
915 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100916 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800917 mVideoWidth = ext1;
918 mVideoHeight = ext2;
919 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700920 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700922 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700923 case MEDIA_SUBTITLE_DATA:
924 ALOGV("Received subtitle data message");
925 break;
Robert Shih08528432015-04-08 09:06:54 -0700926 case MEDIA_META_DATA:
927 ALOGV("Received timed metadata message");
928 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 default:
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 break;
932 }
933
934 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700935 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936
937 // this prevents re-entrant calls into client code
938 if ((listener != 0) && send) {
939 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700941 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100942 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943 }
944}
945
James Dongdd172fc2010-01-15 18:13:58 -0800946void MediaPlayer::died()
947{
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800949 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
950}
951
Marco Nelissen6b74d672012-02-28 16:07:44 -0800952status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700953 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800954 if (mPlayer == NULL) {
955 return NO_INIT;
956 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700957
958 if (next != NULL && !(next->mCurrentState &
959 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
960 ALOGE("next player is not prepared");
961 return INVALID_OPERATION;
962 }
963
Marco Nelissen6b74d672012-02-28 16:07:44 -0800964 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
965}
966
Glenn Kasten40bc9062015-03-20 09:09:33 -0700967} // namespace android