blob: 9a276aecdbb01204c48e95ab82dafc8df03ed70c [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>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080035#include <media/AudioSystem.h>
Chris Watkins99f31602015-03-20 13:06:33 -070036#include <media/IDataSource.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Andreas Huber2db84552010-01-28 11:19:57 -080040#include <utils/KeyedVector.h>
41#include <utils/String8.h>
42
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070044#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046namespace android {
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048MediaPlayer::MediaPlayer()
49{
Steve Block3856b092011-10-20 11:56:00 +010050 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051 mListener = NULL;
52 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070053 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070054 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055 mCurrentPosition = -1;
56 mSeekPosition = -1;
57 mCurrentState = MEDIA_PLAYER_IDLE;
58 mPrepareSync = false;
59 mPrepareStatus = NO_ERROR;
60 mLoop = false;
61 mLeftVolume = mRightVolume = 1.0;
62 mVideoWidth = mVideoHeight = 0;
Wei Jia98160162015-02-04 17:01:11 -080063 mPlaybackRate = 1.0;
Jason Sams1af452f2009-03-24 18:45:22 -070064 mLockThreadId = 0;
Eric Laurentde3f8392014-07-27 18:38:22 -070065 mAudioSessionId = AudioSystem::newAudioUniqueId();
Marco Nelissend457c972014-02-11 08:47:07 -080066 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070067 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080068 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069}
70
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071MediaPlayer::~MediaPlayer()
72{
Steve Block3856b092011-10-20 11:56:00 +010073 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070074 if (mAudioAttributesParcel != NULL) {
75 delete mAudioAttributesParcel;
76 mAudioAttributesParcel = NULL;
77 }
Marco Nelissend457c972014-02-11 08:47:07 -080078 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080079 disconnect();
80 IPCThreadState::self()->flushCommands();
81}
82
83void MediaPlayer::disconnect()
84{
Steve Block3856b092011-10-20 11:56:00 +010085 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086 sp<IMediaPlayer> p;
87 {
88 Mutex::Autolock _l(mLock);
89 p = mPlayer;
90 mPlayer.clear();
91 }
92
93 if (p != 0) {
94 p->disconnect();
95 }
96}
97
98// always call with lock held
99void MediaPlayer::clear_l()
100{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101 mCurrentPosition = -1;
102 mSeekPosition = -1;
103 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800104 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105}
106
107status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
108{
Steve Block3856b092011-10-20 11:56:00 +0100109 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110 Mutex::Autolock _l(mLock);
111 mListener = listener;
112 return NO_ERROR;
113}
114
115
Dave Burked681bbb2011-08-30 14:39:17 +0100116status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
118 status_t err = UNKNOWN_ERROR;
119 sp<IMediaPlayer> p;
120 { // scope for the lock
121 Mutex::Autolock _l(mLock);
122
Marco Nelissen83ff1432010-03-10 10:53:16 -0800123 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
124 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000125 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800126 return INVALID_OPERATION;
127 }
128
129 clear_l();
130 p = mPlayer;
131 mPlayer = player;
132 if (player != 0) {
133 mCurrentState = MEDIA_PLAYER_INITIALIZED;
134 err = NO_ERROR;
135 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000136 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137 }
138 }
139
140 if (p != 0) {
141 p->disconnect();
142 }
143
144 return err;
145}
146
Andreas Huber2db84552010-01-28 11:19:57 -0800147status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800148 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800149 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150{
Steve Block3856b092011-10-20 11:56:00 +0100151 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 status_t err = BAD_VALUE;
153 if (url != NULL) {
154 const sp<IMediaPlayerService>& service(getMediaPlayerService());
155 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800156 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800157 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800158 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100159 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100160 }
Dave Burke06620672011-09-06 20:39:47 +0100161 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162 }
163 }
164 return err;
165}
166
167status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
168{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700169 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 status_t err = UNKNOWN_ERROR;
171 const sp<IMediaPlayerService>& service(getMediaPlayerService());
172 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800173 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800174 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
175 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100176 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100177 }
Dave Burke06620672011-09-06 20:39:47 +0100178 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100179 }
180 return err;
181}
182
183status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
184{
Steve Block3856b092011-10-20 11:56:00 +0100185 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100186 status_t err = UNKNOWN_ERROR;
187 const sp<IMediaPlayerService>& service(getMediaPlayerService());
188 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800189 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800190 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
191 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100192 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100193 }
Dave Burke06620672011-09-06 20:39:47 +0100194 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 }
196 return err;
197}
198
Chris Watkins99f31602015-03-20 13:06:33 -0700199status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
200{
201 ALOGV("setDataSource(IDataSource)");
202 status_t err = UNKNOWN_ERROR;
203 const sp<IMediaPlayerService>& service(getMediaPlayerService());
204 if (service != 0) {
205 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
206 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
207 (NO_ERROR != player->setDataSource(source))) {
208 player.clear();
209 }
210 err = attachNewPlayer(player);
211 }
212 return err;
213}
214
Nicolas Catania1d187f12009-05-12 23:25:55 -0700215status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
216{
217 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800218 const bool hasBeenInitialized =
219 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
220 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
221 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700222 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700223 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700224 }
Steve Block29357bc2012-01-06 19:20:56 +0000225 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700226 return INVALID_OPERATION;
227}
228
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700229status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
230{
Steve Blockb8a80522011-12-20 16:23:08 +0000231 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700232 Mutex::Autolock lock(mLock);
233 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700234 return NO_INIT;
235 }
236 return mPlayer->setMetadataFilter(filter);
237}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700238
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700239status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
240{
Steve Blockb8a80522011-12-20 16:23:08 +0000241 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700242 Mutex::Autolock lock(mLock);
243 if (mPlayer == NULL) {
244 return NO_INIT;
245 }
246 return mPlayer->getMetadata(update_only, apply_filter, metadata);
247}
248
Glenn Kasten11731182011-02-08 17:26:17 -0800249status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800250 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800251{
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800253 Mutex::Autolock _l(mLock);
254 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800255 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800256}
257
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258// must call with lock held
259status_t MediaPlayer::prepareAsync_l()
260{
Glenn Kastenb187de12014-12-30 08:18:15 -0800261 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700263 if (mAudioAttributesParcel != NULL) {
264 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
265 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 mCurrentState = MEDIA_PLAYER_PREPARING;
267 return mPlayer->prepareAsync();
268 }
Steve Block29357bc2012-01-06 19:20:56 +0000269 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 return INVALID_OPERATION;
271}
272
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700273// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
274// one defined in the Android framework and one provided by the implementation
275// that generated the error. The sync version of prepare returns only 1 error
276// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277status_t MediaPlayer::prepare()
278{
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700281 mLockThreadId = getThreadId();
282 if (mPrepareSync) {
283 mLockThreadId = 0;
284 return -EALREADY;
285 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286 mPrepareSync = true;
287 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700288 if (ret != NO_ERROR) {
289 mLockThreadId = 0;
290 return ret;
291 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292
293 if (mPrepareSync) {
294 mSignal.wait(mLock); // wait for prepare done
295 mPrepareSync = false;
296 }
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700298 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 return mPrepareStatus;
300}
301
302status_t MediaPlayer::prepareAsync()
303{
Steve Block3856b092011-10-20 11:56:00 +0100304 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 Mutex::Autolock _l(mLock);
306 return prepareAsync_l();
307}
308
309status_t MediaPlayer::start()
310{
Steve Block3856b092011-10-20 11:56:00 +0100311 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700312
313 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700315
316 mLockThreadId = getThreadId();
317
318 if (mCurrentState & MEDIA_PLAYER_STARTED) {
319 ret = NO_ERROR;
320 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
322 mPlayer->setLooping(mLoop);
323 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700324 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700326 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 if (ret != NO_ERROR) {
328 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
329 } else {
330 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100331 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 }
333 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700334 } else {
335 ALOGE("start called in state %d", mCurrentState);
336 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700338
339 mLockThreadId = 0;
340
341 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342}
343
344status_t MediaPlayer::stop()
345{
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 Mutex::Autolock _l(mLock);
348 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
349 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
350 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
351 status_t ret = mPlayer->stop();
352 if (ret != NO_ERROR) {
353 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
354 } else {
355 mCurrentState = MEDIA_PLAYER_STOPPED;
356 }
357 return ret;
358 }
Steve Block29357bc2012-01-06 19:20:56 +0000359 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 return INVALID_OPERATION;
361}
362
363status_t MediaPlayer::pause()
364{
Steve Block3856b092011-10-20 11:56:00 +0100365 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800367 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 return NO_ERROR;
369 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
370 status_t ret = mPlayer->pause();
371 if (ret != NO_ERROR) {
372 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
373 } else {
374 mCurrentState = MEDIA_PLAYER_PAUSED;
375 }
376 return ret;
377 }
Steve Block29357bc2012-01-06 19:20:56 +0000378 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379 return INVALID_OPERATION;
380}
381
382bool MediaPlayer::isPlaying()
383{
384 Mutex::Autolock _l(mLock);
385 if (mPlayer != 0) {
386 bool temp = false;
387 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000390 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 mCurrentState = MEDIA_PLAYER_PAUSED;
392 }
393 return temp;
394 }
Steve Block3856b092011-10-20 11:56:00 +0100395 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 return false;
397}
398
Wei Jia98160162015-02-04 17:01:11 -0800399status_t MediaPlayer::setPlaybackRate(float rate)
400{
401 ALOGV("setPlaybackRate: %f", rate);
402 if (rate <= 0.0) {
403 return BAD_VALUE;
404 }
405 Mutex::Autolock _l(mLock);
406 if (mPlayer != 0) {
407 if (mPlaybackRate == rate) {
408 return NO_ERROR;
409 }
410 mPlaybackRate = rate;
411 return mPlayer->setPlaybackRate(rate);
412 }
413 ALOGV("setPlaybackRate: no active player");
414 return INVALID_OPERATION;
415}
416
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417status_t MediaPlayer::getVideoWidth(int *w)
418{
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420 Mutex::Autolock _l(mLock);
421 if (mPlayer == 0) return INVALID_OPERATION;
422 *w = mVideoWidth;
423 return NO_ERROR;
424}
425
426status_t MediaPlayer::getVideoHeight(int *h)
427{
Steve Block3856b092011-10-20 11:56:00 +0100428 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 Mutex::Autolock _l(mLock);
430 if (mPlayer == 0) return INVALID_OPERATION;
431 *h = mVideoHeight;
432 return NO_ERROR;
433}
434
435status_t MediaPlayer::getCurrentPosition(int *msec)
436{
Steve Block3856b092011-10-20 11:56:00 +0100437 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 Mutex::Autolock _l(mLock);
439 if (mPlayer != 0) {
440 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100441 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 *msec = mCurrentPosition;
443 return NO_ERROR;
444 }
445 return mPlayer->getCurrentPosition(msec);
446 }
447 return INVALID_OPERATION;
448}
449
450status_t MediaPlayer::getDuration_l(int *msec)
451{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800452 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800453 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
454 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800456 int durationMs;
457 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700458
459 if (ret != OK) {
460 // Do not enter error state just because no duration was available.
461 durationMs = -1;
462 ret = OK;
463 }
464
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800465 if (msec) {
466 *msec = durationMs;
467 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 return ret;
469 }
Steve Block29357bc2012-01-06 19:20:56 +0000470 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 return INVALID_OPERATION;
472}
473
474status_t MediaPlayer::getDuration(int *msec)
475{
476 Mutex::Autolock _l(mLock);
477 return getDuration_l(msec);
478}
479
480status_t MediaPlayer::seekTo_l(int msec)
481{
Steve Block3856b092011-10-20 11:56:00 +0100482 ALOGV("seekTo %d", msec);
Glenn Kastenb187de12014-12-30 08:18:15 -0800483 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
484 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000486 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800489
490 int durationMs;
491 status_t err = mPlayer->getDuration(&durationMs);
492
493 if (err != OK) {
494 ALOGW("Stream has no duration and is therefore not seekable.");
495 return err;
496 }
497
498 if (msec > durationMs) {
499 ALOGW("Attempt to seek to past end of file: request = %d, "
500 "durationMs = %d",
501 msec,
502 durationMs);
503
504 msec = durationMs;
505 }
506
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507 // cache duration
508 mCurrentPosition = msec;
509 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mSeekPosition = msec;
511 return mPlayer->seekTo(msec);
512 }
513 else {
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 return NO_ERROR;
516 }
517 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800518 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
519 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 return INVALID_OPERATION;
521}
522
523status_t MediaPlayer::seekTo(int msec)
524{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700525 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700527 status_t result = seekTo_l(msec);
528 mLockThreadId = 0;
529
530 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531}
532
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700533status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535 mLoop = false;
536 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
537 mPrepareSync = false;
538 if (mPlayer != 0) {
539 status_t ret = mPlayer->reset();
540 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000541 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
543 } else {
544 mCurrentState = MEDIA_PLAYER_IDLE;
545 }
James Donga1680bc2010-11-18 12:23:58 -0800546 // setDataSource has to be called again to create a
547 // new mediaplayer.
548 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 return ret;
550 }
551 clear_l();
552 return NO_ERROR;
553}
554
John Grossmanc795b642012-02-22 15:38:35 -0800555status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
556 Mutex::Autolock _l(mLock);
557
558 if (player == NULL) {
559 return UNKNOWN_ERROR;
560 }
561
562 if (mRetransmitEndpointValid) {
563 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
564 }
565
566 return OK;
567}
568
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700569status_t MediaPlayer::reset()
570{
Steve Block3856b092011-10-20 11:56:00 +0100571 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700572 Mutex::Autolock _l(mLock);
573 return reset_l();
574}
575
Glenn Kastenfff6d712012-01-12 16:38:12 -0800576status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577{
Steve Block3856b092011-10-20 11:56:00 +0100578 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 Mutex::Autolock _l(mLock);
580 if (mStreamType == type) return NO_ERROR;
581 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
582 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
583 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000584 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 return INVALID_OPERATION;
586 }
587 // cache
588 mStreamType = type;
589 return OK;
590}
591
John Spurlockde9453f2014-03-19 13:05:45 -0400592status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
593{
594 ALOGV("getAudioStreamType");
595 Mutex::Autolock _l(mLock);
596 *type = mStreamType;
597 return OK;
598}
599
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600status_t MediaPlayer::setLooping(int loop)
601{
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 Mutex::Autolock _l(mLock);
604 mLoop = (loop != 0);
605 if (mPlayer != 0) {
606 return mPlayer->setLooping(loop);
607 }
608 return OK;
609}
610
611bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100612 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 Mutex::Autolock _l(mLock);
614 if (mPlayer != 0) {
615 return mLoop;
616 }
Steve Block3856b092011-10-20 11:56:00 +0100617 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 return false;
619}
620
621status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
622{
Steve Block3856b092011-10-20 11:56:00 +0100623 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624 Mutex::Autolock _l(mLock);
625 mLeftVolume = leftVolume;
626 mRightVolume = rightVolume;
627 if (mPlayer != 0) {
628 return mPlayer->setVolume(leftVolume, rightVolume);
629 }
630 return OK;
631}
632
Eric Laurenta514bdb2010-06-21 09:27:30 -0700633status_t MediaPlayer::setAudioSessionId(int sessionId)
634{
Steve Block3856b092011-10-20 11:56:00 +0100635 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700636 Mutex::Autolock _l(mLock);
637 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000638 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700639 return INVALID_OPERATION;
640 }
641 if (sessionId < 0) {
642 return BAD_VALUE;
643 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700644 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800645 AudioSystem::acquireAudioSessionId(sessionId, -1);
646 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700647 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700648 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700649 return NO_ERROR;
650}
651
652int MediaPlayer::getAudioSessionId()
653{
654 Mutex::Autolock _l(mLock);
655 return mAudioSessionId;
656}
657
Eric Laurent2beeb502010-07-16 07:43:46 -0700658status_t MediaPlayer::setAuxEffectSendLevel(float level)
659{
Steve Block3856b092011-10-20 11:56:00 +0100660 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700661 Mutex::Autolock _l(mLock);
662 mSendLevel = level;
663 if (mPlayer != 0) {
664 return mPlayer->setAuxEffectSendLevel(level);
665 }
666 return OK;
667}
668
669status_t MediaPlayer::attachAuxEffect(int effectId)
670{
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700672 Mutex::Autolock _l(mLock);
673 if (mPlayer == 0 ||
674 (mCurrentState & MEDIA_PLAYER_IDLE) ||
675 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000676 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700677 return INVALID_OPERATION;
678 }
679
680 return mPlayer->attachAuxEffect(effectId);
681}
682
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700683// always call with lock held
684status_t MediaPlayer::checkStateForKeySet_l(int key)
685{
686 switch(key) {
687 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
688 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
689 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
690 // Can't change the audio attributes after prepare
691 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
692 return INVALID_OPERATION;
693 }
694 break;
695 default:
696 // parameter doesn't require player state check
697 break;
698 }
699 return OK;
700}
701
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700702status_t MediaPlayer::setParameter(int key, const Parcel& request)
703{
Steve Block3856b092011-10-20 11:56:00 +0100704 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700705 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700706 if (checkStateForKeySet_l(key) != OK) {
707 return INVALID_OPERATION;
708 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700709 if (mPlayer != NULL) {
710 return mPlayer->setParameter(key, request);
711 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700712 switch (key) {
713 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
714 // no player, save the marshalled audio attributes
715 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
716 mAudioAttributesParcel = new Parcel();
717 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
718 return OK;
719 default:
720 ALOGV("setParameter: no active player");
721 return INVALID_OPERATION;
722 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700723}
724
725status_t MediaPlayer::getParameter(int key, Parcel *reply)
726{
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700728 Mutex::Autolock _l(mLock);
729 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700730 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700731 }
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700733 return INVALID_OPERATION;
734}
735
John Grossmanc795b642012-02-22 15:38:35 -0800736status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
737 uint16_t port) {
738 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
739 addrString ? addrString : "(null)", port);
740
741 Mutex::Autolock _l(mLock);
742 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
743 return INVALID_OPERATION;
744
745 if (NULL == addrString) {
746 mRetransmitEndpointValid = false;
747 return OK;
748 }
749
750 struct in_addr saddr;
751 if(!inet_aton(addrString, &saddr)) {
752 return BAD_VALUE;
753 }
754
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800755 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800756 mRetransmitEndpoint.sin_family = AF_INET;
757 mRetransmitEndpoint.sin_addr = saddr;
758 mRetransmitEndpoint.sin_port = htons(port);
759 mRetransmitEndpointValid = true;
760
761 return OK;
762}
763
Gloria Wangb483c472011-04-11 17:23:27 -0700764void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765{
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700768 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769
770 // TODO: In the future, we might be on the same thread if the app is
771 // running in the same process as the media server. In that case,
772 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700773 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700774 // The threadId hack below works around this for the care of prepare,
775 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700776 // FIXME: Remember, this is a hack, it's not even a hack that is applied
777 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700778 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700779 mLock.lock();
780 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700781 }
Jason Sams1af452f2009-03-24 18:45:22 -0700782
Eric Laurent3b268442010-08-03 07:49:49 -0700783 // Allows calls from JNI in idle state to notify errors
784 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700786 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800787 return;
788 }
789
790 switch (msg) {
791 case MEDIA_NOP: // interface test message
792 break;
793 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800795 mCurrentState = MEDIA_PLAYER_PREPARED;
796 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100797 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800798 mPrepareSync = false;
799 mPrepareStatus = NO_ERROR;
800 mSignal.signal();
801 }
802 break;
803 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100804 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700805 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000806 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700807 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800808 if (!mLoop) {
809 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
810 }
811 break;
812 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700813 // Always log errors.
814 // ext1: Media framework error code.
815 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000816 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800817 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
818 if (mPrepareSync)
819 {
Steve Block3856b092011-10-20 11:56:00 +0100820 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800821 mPrepareSync = false;
822 mPrepareStatus = ext1;
823 mSignal.signal();
824 send = false;
825 }
826 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700827 case MEDIA_INFO:
828 // ext1: Media framework error code.
829 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800830 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000831 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800832 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700833 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800834 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100835 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800836 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800838 mSeekPosition = -1;
839 seekTo_l(mCurrentPosition);
840 }
841 else {
Steve Block3856b092011-10-20 11:56:00 +0100842 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800843 mCurrentPosition = mSeekPosition = -1;
844 }
845 break;
846 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800848 break;
849 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100850 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800851 mVideoWidth = ext1;
852 mVideoHeight = ext2;
853 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700854 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100855 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700856 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700857 case MEDIA_SUBTITLE_DATA:
858 ALOGV("Received subtitle data message");
859 break;
Robert Shih08528432015-04-08 09:06:54 -0700860 case MEDIA_META_DATA:
861 ALOGV("Received timed metadata message");
862 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863 default:
Steve Block3856b092011-10-20 11:56:00 +0100864 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800865 break;
866 }
867
868 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700869 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870
871 // this prevents re-entrant calls into client code
872 if ((listener != 0) && send) {
873 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100874 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700875 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100876 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800877 }
878}
879
James Dongdd172fc2010-01-15 18:13:58 -0800880void MediaPlayer::died()
881{
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800883 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
884}
885
Marco Nelissen6b74d672012-02-28 16:07:44 -0800886status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
887 if (mPlayer == NULL) {
888 return NO_INIT;
889 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700890
891 if (next != NULL && !(next->mCurrentState &
892 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
893 ALOGE("next player is not prepared");
894 return INVALID_OPERATION;
895 }
896
Marco Nelissen6b74d672012-02-28 16:07:44 -0800897 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
898}
899
Glenn Kasten40bc9062015-03-20 09:09:33 -0700900} // namespace android