blob: 432ecda49d3a10c8f928621c25189f4e68655388 [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>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080038
Andreas Huber2db84552010-01-28 11:19:57 -080039#include <utils/KeyedVector.h>
40#include <utils/String8.h>
41
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070043#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070044
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045namespace android {
46
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047MediaPlayer::MediaPlayer()
48{
Steve Block3856b092011-10-20 11:56:00 +010049 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050 mListener = NULL;
51 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070052 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070053 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054 mCurrentPosition = -1;
55 mSeekPosition = -1;
56 mCurrentState = MEDIA_PLAYER_IDLE;
57 mPrepareSync = false;
58 mPrepareStatus = NO_ERROR;
59 mLoop = false;
60 mLeftVolume = mRightVolume = 1.0;
61 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070062 mLockThreadId = 0;
Eric Laurentde3f8392014-07-27 18:38:22 -070063 mAudioSessionId = AudioSystem::newAudioUniqueId();
Marco Nelissend457c972014-02-11 08:47:07 -080064 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070065 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080066 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067}
68
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069MediaPlayer::~MediaPlayer()
70{
Steve Block3856b092011-10-20 11:56:00 +010071 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070072 if (mAudioAttributesParcel != NULL) {
73 delete mAudioAttributesParcel;
74 mAudioAttributesParcel = NULL;
75 }
Marco Nelissend457c972014-02-11 08:47:07 -080076 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 disconnect();
78 IPCThreadState::self()->flushCommands();
79}
80
81void MediaPlayer::disconnect()
82{
Steve Block3856b092011-10-20 11:56:00 +010083 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084 sp<IMediaPlayer> p;
85 {
86 Mutex::Autolock _l(mLock);
87 p = mPlayer;
88 mPlayer.clear();
89 }
90
91 if (p != 0) {
92 p->disconnect();
93 }
94}
95
96// always call with lock held
97void MediaPlayer::clear_l()
98{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 mCurrentPosition = -1;
100 mSeekPosition = -1;
101 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800102 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103}
104
105status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
106{
Steve Block3856b092011-10-20 11:56:00 +0100107 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 Mutex::Autolock _l(mLock);
109 mListener = listener;
110 return NO_ERROR;
111}
112
113
Dave Burked681bbb2011-08-30 14:39:17 +0100114status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115{
116 status_t err = UNKNOWN_ERROR;
117 sp<IMediaPlayer> p;
118 { // scope for the lock
119 Mutex::Autolock _l(mLock);
120
Marco Nelissen83ff1432010-03-10 10:53:16 -0800121 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
122 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000123 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 return INVALID_OPERATION;
125 }
126
127 clear_l();
128 p = mPlayer;
129 mPlayer = player;
130 if (player != 0) {
131 mCurrentState = MEDIA_PLAYER_INITIALIZED;
132 err = NO_ERROR;
133 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000134 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 }
136 }
137
138 if (p != 0) {
139 p->disconnect();
140 }
141
142 return err;
143}
144
Andreas Huber2db84552010-01-28 11:19:57 -0800145status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800146 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800147 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148{
Steve Block3856b092011-10-20 11:56:00 +0100149 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 status_t err = BAD_VALUE;
151 if (url != NULL) {
152 const sp<IMediaPlayerService>& service(getMediaPlayerService());
153 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800154 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800155 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800156 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100157 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100158 }
Dave Burke06620672011-09-06 20:39:47 +0100159 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 }
161 }
162 return err;
163}
164
165status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
166{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700167 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 status_t err = UNKNOWN_ERROR;
169 const sp<IMediaPlayerService>& service(getMediaPlayerService());
170 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800171 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800172 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
173 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100174 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100175 }
Dave Burke06620672011-09-06 20:39:47 +0100176 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100177 }
178 return err;
179}
180
181status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
182{
Steve Block3856b092011-10-20 11:56:00 +0100183 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100184 status_t err = UNKNOWN_ERROR;
185 const sp<IMediaPlayerService>& service(getMediaPlayerService());
186 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800187 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800188 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
189 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100190 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100191 }
Dave Burke06620672011-09-06 20:39:47 +0100192 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 }
194 return err;
195}
196
Nicolas Catania1d187f12009-05-12 23:25:55 -0700197status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
198{
199 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800200 const bool hasBeenInitialized =
201 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
202 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
203 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700204 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700205 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700206 }
Steve Block29357bc2012-01-06 19:20:56 +0000207 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700208 return INVALID_OPERATION;
209}
210
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700211status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
212{
Steve Blockb8a80522011-12-20 16:23:08 +0000213 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700214 Mutex::Autolock lock(mLock);
215 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700216 return NO_INIT;
217 }
218 return mPlayer->setMetadataFilter(filter);
219}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700220
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700221status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
222{
Steve Blockb8a80522011-12-20 16:23:08 +0000223 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700224 Mutex::Autolock lock(mLock);
225 if (mPlayer == NULL) {
226 return NO_INIT;
227 }
228 return mPlayer->getMetadata(update_only, apply_filter, metadata);
229}
230
Glenn Kasten11731182011-02-08 17:26:17 -0800231status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800232 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800233{
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800235 Mutex::Autolock _l(mLock);
236 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800237 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800238}
239
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240// must call with lock held
241status_t MediaPlayer::prepareAsync_l()
242{
Glenn Kastenb187de12014-12-30 08:18:15 -0800243 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700245 if (mAudioAttributesParcel != NULL) {
246 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
247 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 mCurrentState = MEDIA_PLAYER_PREPARING;
249 return mPlayer->prepareAsync();
250 }
Steve Block29357bc2012-01-06 19:20:56 +0000251 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 return INVALID_OPERATION;
253}
254
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700255// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
256// one defined in the Android framework and one provided by the implementation
257// that generated the error. The sync version of prepare returns only 1 error
258// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259status_t MediaPlayer::prepare()
260{
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700263 mLockThreadId = getThreadId();
264 if (mPrepareSync) {
265 mLockThreadId = 0;
266 return -EALREADY;
267 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 mPrepareSync = true;
269 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700270 if (ret != NO_ERROR) {
271 mLockThreadId = 0;
272 return ret;
273 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274
275 if (mPrepareSync) {
276 mSignal.wait(mLock); // wait for prepare done
277 mPrepareSync = false;
278 }
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700280 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 return mPrepareStatus;
282}
283
284status_t MediaPlayer::prepareAsync()
285{
Steve Block3856b092011-10-20 11:56:00 +0100286 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 Mutex::Autolock _l(mLock);
288 return prepareAsync_l();
289}
290
291status_t MediaPlayer::start()
292{
Steve Block3856b092011-10-20 11:56:00 +0100293 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700294
295 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700297
298 mLockThreadId = getThreadId();
299
300 if (mCurrentState & MEDIA_PLAYER_STARTED) {
301 ret = NO_ERROR;
302 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
304 mPlayer->setLooping(mLoop);
305 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700306 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700308 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 if (ret != NO_ERROR) {
310 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
311 } else {
312 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 }
315 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700316 } else {
317 ALOGE("start called in state %d", mCurrentState);
318 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700320
321 mLockThreadId = 0;
322
323 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324}
325
326status_t MediaPlayer::stop()
327{
Steve Block3856b092011-10-20 11:56:00 +0100328 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 Mutex::Autolock _l(mLock);
330 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
331 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
332 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
333 status_t ret = mPlayer->stop();
334 if (ret != NO_ERROR) {
335 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
336 } else {
337 mCurrentState = MEDIA_PLAYER_STOPPED;
338 }
339 return ret;
340 }
Steve Block29357bc2012-01-06 19:20:56 +0000341 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 return INVALID_OPERATION;
343}
344
345status_t MediaPlayer::pause()
346{
Steve Block3856b092011-10-20 11:56:00 +0100347 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800349 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350 return NO_ERROR;
351 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
352 status_t ret = mPlayer->pause();
353 if (ret != NO_ERROR) {
354 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
355 } else {
356 mCurrentState = MEDIA_PLAYER_PAUSED;
357 }
358 return ret;
359 }
Steve Block29357bc2012-01-06 19:20:56 +0000360 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 return INVALID_OPERATION;
362}
363
364bool MediaPlayer::isPlaying()
365{
366 Mutex::Autolock _l(mLock);
367 if (mPlayer != 0) {
368 bool temp = false;
369 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100370 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000372 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 mCurrentState = MEDIA_PLAYER_PAUSED;
374 }
375 return temp;
376 }
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 return false;
379}
380
381status_t MediaPlayer::getVideoWidth(int *w)
382{
Steve Block3856b092011-10-20 11:56:00 +0100383 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 Mutex::Autolock _l(mLock);
385 if (mPlayer == 0) return INVALID_OPERATION;
386 *w = mVideoWidth;
387 return NO_ERROR;
388}
389
390status_t MediaPlayer::getVideoHeight(int *h)
391{
Steve Block3856b092011-10-20 11:56:00 +0100392 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 Mutex::Autolock _l(mLock);
394 if (mPlayer == 0) return INVALID_OPERATION;
395 *h = mVideoHeight;
396 return NO_ERROR;
397}
398
399status_t MediaPlayer::getCurrentPosition(int *msec)
400{
Steve Block3856b092011-10-20 11:56:00 +0100401 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 Mutex::Autolock _l(mLock);
403 if (mPlayer != 0) {
404 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100405 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 *msec = mCurrentPosition;
407 return NO_ERROR;
408 }
409 return mPlayer->getCurrentPosition(msec);
410 }
411 return INVALID_OPERATION;
412}
413
414status_t MediaPlayer::getDuration_l(int *msec)
415{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800416 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800417 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
418 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800420 int durationMs;
421 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700422
423 if (ret != OK) {
424 // Do not enter error state just because no duration was available.
425 durationMs = -1;
426 ret = OK;
427 }
428
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800429 if (msec) {
430 *msec = durationMs;
431 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 return ret;
433 }
Steve Block29357bc2012-01-06 19:20:56 +0000434 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 return INVALID_OPERATION;
436}
437
438status_t MediaPlayer::getDuration(int *msec)
439{
440 Mutex::Autolock _l(mLock);
441 return getDuration_l(msec);
442}
443
444status_t MediaPlayer::seekTo_l(int msec)
445{
Steve Block3856b092011-10-20 11:56:00 +0100446 ALOGV("seekTo %d", msec);
Glenn Kastenb187de12014-12-30 08:18:15 -0800447 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
448 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000450 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800453
454 int durationMs;
455 status_t err = mPlayer->getDuration(&durationMs);
456
457 if (err != OK) {
458 ALOGW("Stream has no duration and is therefore not seekable.");
459 return err;
460 }
461
462 if (msec > durationMs) {
463 ALOGW("Attempt to seek to past end of file: request = %d, "
464 "durationMs = %d",
465 msec,
466 durationMs);
467
468 msec = durationMs;
469 }
470
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 // cache duration
472 mCurrentPosition = msec;
473 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474 mSeekPosition = msec;
475 return mPlayer->seekTo(msec);
476 }
477 else {
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 return NO_ERROR;
480 }
481 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800482 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
483 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 return INVALID_OPERATION;
485}
486
487status_t MediaPlayer::seekTo(int msec)
488{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700489 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700491 status_t result = seekTo_l(msec);
492 mLockThreadId = 0;
493
494 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495}
496
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700497status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499 mLoop = false;
500 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
501 mPrepareSync = false;
502 if (mPlayer != 0) {
503 status_t ret = mPlayer->reset();
504 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000505 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
507 } else {
508 mCurrentState = MEDIA_PLAYER_IDLE;
509 }
James Donga1680bc2010-11-18 12:23:58 -0800510 // setDataSource has to be called again to create a
511 // new mediaplayer.
512 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 return ret;
514 }
515 clear_l();
516 return NO_ERROR;
517}
518
John Grossmanc795b642012-02-22 15:38:35 -0800519status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
520 Mutex::Autolock _l(mLock);
521
522 if (player == NULL) {
523 return UNKNOWN_ERROR;
524 }
525
526 if (mRetransmitEndpointValid) {
527 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
528 }
529
530 return OK;
531}
532
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700533status_t MediaPlayer::reset()
534{
Steve Block3856b092011-10-20 11:56:00 +0100535 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700536 Mutex::Autolock _l(mLock);
537 return reset_l();
538}
539
Glenn Kastenfff6d712012-01-12 16:38:12 -0800540status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541{
Steve Block3856b092011-10-20 11:56:00 +0100542 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543 Mutex::Autolock _l(mLock);
544 if (mStreamType == type) return NO_ERROR;
545 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
546 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
547 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000548 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 return INVALID_OPERATION;
550 }
551 // cache
552 mStreamType = type;
553 return OK;
554}
555
John Spurlockde9453f2014-03-19 13:05:45 -0400556status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
557{
558 ALOGV("getAudioStreamType");
559 Mutex::Autolock _l(mLock);
560 *type = mStreamType;
561 return OK;
562}
563
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564status_t MediaPlayer::setLooping(int loop)
565{
Steve Block3856b092011-10-20 11:56:00 +0100566 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567 Mutex::Autolock _l(mLock);
568 mLoop = (loop != 0);
569 if (mPlayer != 0) {
570 return mPlayer->setLooping(loop);
571 }
572 return OK;
573}
574
575bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 Mutex::Autolock _l(mLock);
578 if (mPlayer != 0) {
579 return mLoop;
580 }
Steve Block3856b092011-10-20 11:56:00 +0100581 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 return false;
583}
584
585status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
586{
Steve Block3856b092011-10-20 11:56:00 +0100587 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 Mutex::Autolock _l(mLock);
589 mLeftVolume = leftVolume;
590 mRightVolume = rightVolume;
591 if (mPlayer != 0) {
592 return mPlayer->setVolume(leftVolume, rightVolume);
593 }
594 return OK;
595}
596
Eric Laurenta514bdb2010-06-21 09:27:30 -0700597status_t MediaPlayer::setAudioSessionId(int sessionId)
598{
Steve Block3856b092011-10-20 11:56:00 +0100599 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700600 Mutex::Autolock _l(mLock);
601 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000602 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700603 return INVALID_OPERATION;
604 }
605 if (sessionId < 0) {
606 return BAD_VALUE;
607 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700608 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800609 AudioSystem::acquireAudioSessionId(sessionId, -1);
610 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700611 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700612 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700613 return NO_ERROR;
614}
615
616int MediaPlayer::getAudioSessionId()
617{
618 Mutex::Autolock _l(mLock);
619 return mAudioSessionId;
620}
621
Eric Laurent2beeb502010-07-16 07:43:46 -0700622status_t MediaPlayer::setAuxEffectSendLevel(float level)
623{
Steve Block3856b092011-10-20 11:56:00 +0100624 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700625 Mutex::Autolock _l(mLock);
626 mSendLevel = level;
627 if (mPlayer != 0) {
628 return mPlayer->setAuxEffectSendLevel(level);
629 }
630 return OK;
631}
632
633status_t MediaPlayer::attachAuxEffect(int effectId)
634{
Steve Block3856b092011-10-20 11:56:00 +0100635 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700636 Mutex::Autolock _l(mLock);
637 if (mPlayer == 0 ||
638 (mCurrentState & MEDIA_PLAYER_IDLE) ||
639 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000640 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700641 return INVALID_OPERATION;
642 }
643
644 return mPlayer->attachAuxEffect(effectId);
645}
646
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700647// always call with lock held
648status_t MediaPlayer::checkStateForKeySet_l(int key)
649{
650 switch(key) {
651 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
652 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
653 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
654 // Can't change the audio attributes after prepare
655 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
656 return INVALID_OPERATION;
657 }
658 break;
659 default:
660 // parameter doesn't require player state check
661 break;
662 }
663 return OK;
664}
665
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700666status_t MediaPlayer::setParameter(int key, const Parcel& request)
667{
Steve Block3856b092011-10-20 11:56:00 +0100668 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700669 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700670 if (checkStateForKeySet_l(key) != OK) {
671 return INVALID_OPERATION;
672 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700673 if (mPlayer != NULL) {
674 return mPlayer->setParameter(key, request);
675 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700676 switch (key) {
677 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
678 // no player, save the marshalled audio attributes
679 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
680 mAudioAttributesParcel = new Parcel();
681 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
682 return OK;
683 default:
684 ALOGV("setParameter: no active player");
685 return INVALID_OPERATION;
686 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700687}
688
689status_t MediaPlayer::getParameter(int key, Parcel *reply)
690{
Steve Block3856b092011-10-20 11:56:00 +0100691 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700692 Mutex::Autolock _l(mLock);
693 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700694 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700695 }
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700697 return INVALID_OPERATION;
698}
699
John Grossmanc795b642012-02-22 15:38:35 -0800700status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
701 uint16_t port) {
702 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
703 addrString ? addrString : "(null)", port);
704
705 Mutex::Autolock _l(mLock);
706 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
707 return INVALID_OPERATION;
708
709 if (NULL == addrString) {
710 mRetransmitEndpointValid = false;
711 return OK;
712 }
713
714 struct in_addr saddr;
715 if(!inet_aton(addrString, &saddr)) {
716 return BAD_VALUE;
717 }
718
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800719 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800720 mRetransmitEndpoint.sin_family = AF_INET;
721 mRetransmitEndpoint.sin_addr = saddr;
722 mRetransmitEndpoint.sin_port = htons(port);
723 mRetransmitEndpointValid = true;
724
725 return OK;
726}
727
Gloria Wangb483c472011-04-11 17:23:27 -0700728void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729{
Steve Block3856b092011-10-20 11:56:00 +0100730 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800731 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700732 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733
734 // TODO: In the future, we might be on the same thread if the app is
735 // running in the same process as the media server. In that case,
736 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700737 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700738 // The threadId hack below works around this for the care of prepare,
739 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700740 // FIXME: Remember, this is a hack, it's not even a hack that is applied
741 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700742 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700743 mLock.lock();
744 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700745 }
Jason Sams1af452f2009-03-24 18:45:22 -0700746
Eric Laurent3b268442010-08-03 07:49:49 -0700747 // Allows calls from JNI in idle state to notify errors
748 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700750 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751 return;
752 }
753
754 switch (msg) {
755 case MEDIA_NOP: // interface test message
756 break;
757 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100758 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759 mCurrentState = MEDIA_PLAYER_PREPARED;
760 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762 mPrepareSync = false;
763 mPrepareStatus = NO_ERROR;
764 mSignal.signal();
765 }
766 break;
767 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700769 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000770 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700771 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772 if (!mLoop) {
773 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
774 }
775 break;
776 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700777 // Always log errors.
778 // ext1: Media framework error code.
779 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000780 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800781 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
782 if (mPrepareSync)
783 {
Steve Block3856b092011-10-20 11:56:00 +0100784 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800785 mPrepareSync = false;
786 mPrepareStatus = ext1;
787 mSignal.signal();
788 send = false;
789 }
790 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700791 case MEDIA_INFO:
792 // ext1: Media framework error code.
793 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800794 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000795 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800796 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700797 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800798 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100799 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800800 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100801 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800802 mSeekPosition = -1;
803 seekTo_l(mCurrentPosition);
804 }
805 else {
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800807 mCurrentPosition = mSeekPosition = -1;
808 }
809 break;
810 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 break;
813 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800815 mVideoWidth = ext1;
816 mVideoHeight = ext2;
817 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700818 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100819 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700820 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700821 case MEDIA_SUBTITLE_DATA:
822 ALOGV("Received subtitle data message");
823 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800824 default:
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800826 break;
827 }
828
829 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700830 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831
832 // this prevents re-entrant calls into client code
833 if ((listener != 0) && send) {
834 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100835 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700836 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800838 }
839}
840
James Dongdd172fc2010-01-15 18:13:58 -0800841void MediaPlayer::died()
842{
Steve Block3856b092011-10-20 11:56:00 +0100843 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800844 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
845}
846
Marco Nelissen6b74d672012-02-28 16:07:44 -0800847status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
848 if (mPlayer == NULL) {
849 return NO_INIT;
850 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700851
852 if (next != NULL && !(next->mCurrentState &
853 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
854 ALOGE("next player is not prepared");
855 return INVALID_OPERATION;
856 }
857
Marco Nelissen6b74d672012-02-28 16:07:44 -0800858 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
859}
860
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861}; // namespace android