blob: 695c4a82560643f07ac849ead04c6b5deb046340 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* mediaplayer.cpp
2**
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"
20#include <utils/Log.h>
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26
Mathias Agopian07952722009-05-19 19:08:10 -070027#include <binder/IServiceManager.h>
28#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Jamie Gennisc85ca5d2011-07-13 12:59:34 -070030#include <gui/SurfaceTextureClient.h>
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <media/mediaplayer.h>
33#include <media/AudioTrack.h>
34
Mathias Agopian000479f2010-02-09 17:46:37 -080035#include <surfaceflinger/Surface.h>
36
Mathias Agopian07952722009-05-19 19:08:10 -070037#include <binder/MemoryBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Andreas Huber25643002010-01-28 11:19:57 -080039#include <utils/KeyedVector.h>
40#include <utils/String8.h>
41
Dima Zavin34bb4192011-05-11 14:15:23 -070042#include <system/audio.h>
Jamie Gennisc85ca5d2011-07-13 12:59:34 -070043#include <system/window.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045namespace android {
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047MediaPlayer::MediaPlayer()
48{
Steve Block71f2cf12011-10-20 11:56:00 +010049 ALOGV("constructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 mListener = NULL;
51 mCookie = NULL;
52 mDuration = -1;
Dima Zavin24fc2fb2011-04-19 22:30:36 -070053 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-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 Samsebb020a2009-03-24 18:45:22 -070062 mLockThreadId = 0;
Eric Laurent619346f2010-06-21 09:27:30 -070063 mAudioSessionId = AudioSystem::newAudioSessionId();
Marco Nelissenc74b93f2011-08-02 13:33:41 -070064 AudioSystem::acquireAudioSessionId(mAudioSessionId);
Eric Laurentb3bdf3f2010-10-07 18:23:03 -070065 mSendLevel = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066}
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068MediaPlayer::~MediaPlayer()
69{
Steve Block71f2cf12011-10-20 11:56:00 +010070 ALOGV("destructor");
Marco Nelissenc74b93f2011-08-02 13:33:41 -070071 AudioSystem::releaseAudioSessionId(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 disconnect();
73 IPCThreadState::self()->flushCommands();
74}
75
76void MediaPlayer::disconnect()
77{
Steve Block71f2cf12011-10-20 11:56:00 +010078 ALOGV("disconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 sp<IMediaPlayer> p;
80 {
81 Mutex::Autolock _l(mLock);
82 p = mPlayer;
83 mPlayer.clear();
84 }
85
86 if (p != 0) {
87 p->disconnect();
88 }
89}
90
91// always call with lock held
92void MediaPlayer::clear_l()
93{
94 mDuration = -1;
95 mCurrentPosition = -1;
96 mSeekPosition = -1;
97 mVideoWidth = mVideoHeight = 0;
98}
99
100status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
101{
Steve Block71f2cf12011-10-20 11:56:00 +0100102 ALOGV("setListener");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 Mutex::Autolock _l(mLock);
104 mListener = listener;
105 return NO_ERROR;
106}
107
108
Dave Burkefc301b02011-08-30 14:39:17 +0100109status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110{
111 status_t err = UNKNOWN_ERROR;
112 sp<IMediaPlayer> p;
113 { // scope for the lock
114 Mutex::Autolock _l(mLock);
115
Marco Nelissene44b41b2010-03-10 10:53:16 -0800116 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
117 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Dave Burkefc301b02011-08-30 14:39:17 +0100118 LOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 return INVALID_OPERATION;
120 }
121
122 clear_l();
123 p = mPlayer;
124 mPlayer = player;
125 if (player != 0) {
126 mCurrentState = MEDIA_PLAYER_INITIALIZED;
127 err = NO_ERROR;
128 } else {
129 LOGE("Unable to to create media player");
130 }
131 }
132
133 if (p != 0) {
134 p->disconnect();
135 }
136
137 return err;
138}
139
Andreas Huber25643002010-01-28 11:19:57 -0800140status_t MediaPlayer::setDataSource(
141 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142{
Steve Block71f2cf12011-10-20 11:56:00 +0100143 ALOGV("setDataSource(%s)", url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 status_t err = BAD_VALUE;
145 if (url != NULL) {
146 const sp<IMediaPlayerService>& service(getMediaPlayerService());
147 if (service != 0) {
Dave Burkefc301b02011-08-30 14:39:17 +0100148 sp<IMediaPlayer> player(service->create(getpid(), this, mAudioSessionId));
Dave Burkea28279b2011-09-06 20:39:47 +0100149 if (NO_ERROR != player->setDataSource(url, headers)) {
150 player.clear();
Dave Burkefc301b02011-08-30 14:39:17 +0100151 }
Dave Burkea28279b2011-09-06 20:39:47 +0100152 err = attachNewPlayer(player);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
154 }
155 return err;
156}
157
158status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
159{
Steve Block71f2cf12011-10-20 11:56:00 +0100160 ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 status_t err = UNKNOWN_ERROR;
162 const sp<IMediaPlayerService>& service(getMediaPlayerService());
163 if (service != 0) {
Dave Burkefc301b02011-08-30 14:39:17 +0100164 sp<IMediaPlayer> player(service->create(getpid(), this, mAudioSessionId));
Dave Burkea28279b2011-09-06 20:39:47 +0100165 if (NO_ERROR != player->setDataSource(fd, offset, length)) {
166 player.clear();
Dave Burkefc301b02011-08-30 14:39:17 +0100167 }
Dave Burkea28279b2011-09-06 20:39:47 +0100168 err = attachNewPlayer(player);
Dave Burkefc301b02011-08-30 14:39:17 +0100169 }
170 return err;
171}
172
173status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
174{
Steve Block71f2cf12011-10-20 11:56:00 +0100175 ALOGV("setDataSource");
Dave Burkefc301b02011-08-30 14:39:17 +0100176 status_t err = UNKNOWN_ERROR;
177 const sp<IMediaPlayerService>& service(getMediaPlayerService());
178 if (service != 0) {
179 sp<IMediaPlayer> player(service->create(getpid(), this, mAudioSessionId));
Dave Burkea28279b2011-09-06 20:39:47 +0100180 if (NO_ERROR != player->setDataSource(source)) {
181 player.clear();
Dave Burkefc301b02011-08-30 14:39:17 +0100182 }
Dave Burkea28279b2011-09-06 20:39:47 +0100183 err = attachNewPlayer(player);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
185 return err;
186}
187
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700188status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
189{
190 Mutex::Autolock _l(mLock);
Nicolas Catania7bd3d7d2010-03-10 10:41:04 -0800191 const bool hasBeenInitialized =
192 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
193 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
194 if ((mPlayer != NULL) && hasBeenInitialized) {
Steve Block71f2cf12011-10-20 11:56:00 +0100195 ALOGV("invoke %d", request.dataSize());
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700196 return mPlayer->invoke(request, reply);
197 }
198 LOGE("invoke failed: wrong state %X", mCurrentState);
199 return INVALID_OPERATION;
200}
201
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700202status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
203{
Steve Block5baa3a62011-12-20 16:23:08 +0000204 ALOGD("setMetadataFilter");
Nicolas Catania5d55c712009-07-09 09:21:33 -0700205 Mutex::Autolock lock(mLock);
206 if (mPlayer == NULL) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700207 return NO_INIT;
208 }
209 return mPlayer->setMetadataFilter(filter);
210}
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700211
Nicolas Catania5d55c712009-07-09 09:21:33 -0700212status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
213{
Steve Block5baa3a62011-12-20 16:23:08 +0000214 ALOGD("getMetadata");
Nicolas Catania5d55c712009-07-09 09:21:33 -0700215 Mutex::Autolock lock(mLock);
216 if (mPlayer == NULL) {
217 return NO_INIT;
218 }
219 return mPlayer->getMetadata(update_only, apply_filter, metadata);
220}
221
Glenn Kastencc562a32011-02-08 17:26:17 -0800222status_t MediaPlayer::setVideoSurfaceTexture(
223 const sp<ISurfaceTexture>& surfaceTexture)
224{
Steve Block71f2cf12011-10-20 11:56:00 +0100225 ALOGV("setVideoSurfaceTexture");
Glenn Kastencc562a32011-02-08 17:26:17 -0800226 Mutex::Autolock _l(mLock);
227 if (mPlayer == 0) return NO_INIT;
Jamie Gennis2fa0ac22011-10-26 18:36:31 -0700228 return mPlayer->setVideoSurfaceTexture(surfaceTexture);
Glenn Kastencc562a32011-02-08 17:26:17 -0800229}
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231// must call with lock held
232status_t MediaPlayer::prepareAsync_l()
233{
234 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
235 mPlayer->setAudioStreamType(mStreamType);
236 mCurrentState = MEDIA_PLAYER_PREPARING;
237 return mPlayer->prepareAsync();
238 }
239 LOGE("prepareAsync called in state %d", mCurrentState);
240 return INVALID_OPERATION;
241}
242
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700243// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
244// one defined in the Android framework and one provided by the implementation
245// that generated the error. The sync version of prepare returns only 1 error
246// code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247status_t MediaPlayer::prepare()
248{
Steve Block71f2cf12011-10-20 11:56:00 +0100249 ALOGV("prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 Mutex::Autolock _l(mLock);
Jason Samsebb020a2009-03-24 18:45:22 -0700251 mLockThreadId = getThreadId();
252 if (mPrepareSync) {
253 mLockThreadId = 0;
254 return -EALREADY;
255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 mPrepareSync = true;
257 status_t ret = prepareAsync_l();
Jason Samsebb020a2009-03-24 18:45:22 -0700258 if (ret != NO_ERROR) {
259 mLockThreadId = 0;
260 return ret;
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 if (mPrepareSync) {
264 mSignal.wait(mLock); // wait for prepare done
265 mPrepareSync = false;
266 }
Steve Block71f2cf12011-10-20 11:56:00 +0100267 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Samsebb020a2009-03-24 18:45:22 -0700268 mLockThreadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 return mPrepareStatus;
270}
271
272status_t MediaPlayer::prepareAsync()
273{
Steve Block71f2cf12011-10-20 11:56:00 +0100274 ALOGV("prepareAsync");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 Mutex::Autolock _l(mLock);
276 return prepareAsync_l();
277}
278
279status_t MediaPlayer::start()
280{
Steve Block71f2cf12011-10-20 11:56:00 +0100281 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 Mutex::Autolock _l(mLock);
283 if (mCurrentState & MEDIA_PLAYER_STARTED)
284 return NO_ERROR;
285 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
286 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
287 mPlayer->setLooping(mLoop);
288 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -0700289 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 mCurrentState = MEDIA_PLAYER_STARTED;
291 status_t ret = mPlayer->start();
292 if (ret != NO_ERROR) {
293 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
294 } else {
295 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block71f2cf12011-10-20 11:56:00 +0100296 ALOGV("playback completed immediately following start()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298 }
299 return ret;
300 }
301 LOGE("start called in state %d", mCurrentState);
302 return INVALID_OPERATION;
303}
304
305status_t MediaPlayer::stop()
306{
Steve Block71f2cf12011-10-20 11:56:00 +0100307 ALOGV("stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 Mutex::Autolock _l(mLock);
309 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
310 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
311 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
312 status_t ret = mPlayer->stop();
313 if (ret != NO_ERROR) {
314 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
315 } else {
316 mCurrentState = MEDIA_PLAYER_STOPPED;
317 }
318 return ret;
319 }
320 LOGE("stop called in state %d", mCurrentState);
321 return INVALID_OPERATION;
322}
323
324status_t MediaPlayer::pause()
325{
Steve Block71f2cf12011-10-20 11:56:00 +0100326 ALOGV("pause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 Mutex::Autolock _l(mLock);
Marco Nelissen465faa92010-02-26 13:16:23 -0800328 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 return NO_ERROR;
330 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
331 status_t ret = mPlayer->pause();
332 if (ret != NO_ERROR) {
333 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
334 } else {
335 mCurrentState = MEDIA_PLAYER_PAUSED;
336 }
337 return ret;
338 }
339 LOGE("pause called in state %d", mCurrentState);
340 return INVALID_OPERATION;
341}
342
343bool MediaPlayer::isPlaying()
344{
345 Mutex::Autolock _l(mLock);
346 if (mPlayer != 0) {
347 bool temp = false;
348 mPlayer->isPlaying(&temp);
Steve Block71f2cf12011-10-20 11:56:00 +0100349 ALOGV("isPlaying: %d", temp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
351 LOGE("internal/external state mismatch corrected");
352 mCurrentState = MEDIA_PLAYER_PAUSED;
353 }
354 return temp;
355 }
Steve Block71f2cf12011-10-20 11:56:00 +0100356 ALOGV("isPlaying: no active player");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 return false;
358}
359
360status_t MediaPlayer::getVideoWidth(int *w)
361{
Steve Block71f2cf12011-10-20 11:56:00 +0100362 ALOGV("getVideoWidth");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 Mutex::Autolock _l(mLock);
364 if (mPlayer == 0) return INVALID_OPERATION;
365 *w = mVideoWidth;
366 return NO_ERROR;
367}
368
369status_t MediaPlayer::getVideoHeight(int *h)
370{
Steve Block71f2cf12011-10-20 11:56:00 +0100371 ALOGV("getVideoHeight");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 Mutex::Autolock _l(mLock);
373 if (mPlayer == 0) return INVALID_OPERATION;
374 *h = mVideoHeight;
375 return NO_ERROR;
376}
377
378status_t MediaPlayer::getCurrentPosition(int *msec)
379{
Steve Block71f2cf12011-10-20 11:56:00 +0100380 ALOGV("getCurrentPosition");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 Mutex::Autolock _l(mLock);
382 if (mPlayer != 0) {
383 if (mCurrentPosition >= 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100384 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 *msec = mCurrentPosition;
386 return NO_ERROR;
387 }
388 return mPlayer->getCurrentPosition(msec);
389 }
390 return INVALID_OPERATION;
391}
392
393status_t MediaPlayer::getDuration_l(int *msec)
394{
Steve Block71f2cf12011-10-20 11:56:00 +0100395 ALOGV("getDuration");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
397 if (mPlayer != 0 && isValidState) {
398 status_t ret = NO_ERROR;
399 if (mDuration <= 0)
400 ret = mPlayer->getDuration(&mDuration);
401 if (msec)
402 *msec = mDuration;
403 return ret;
404 }
405 LOGE("Attempt to call getDuration without a valid mediaplayer");
406 return INVALID_OPERATION;
407}
408
409status_t MediaPlayer::getDuration(int *msec)
410{
411 Mutex::Autolock _l(mLock);
412 return getDuration_l(msec);
413}
414
415status_t MediaPlayer::seekTo_l(int msec)
416{
Steve Block71f2cf12011-10-20 11:56:00 +0100417 ALOGV("seekTo %d", msec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
419 if ( msec < 0 ) {
420 LOGW("Attempt to seek to invalid position: %d", msec);
421 msec = 0;
422 } else if ((mDuration > 0) && (msec > mDuration)) {
423 LOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
424 msec = mDuration;
425 }
426 // cache duration
427 mCurrentPosition = msec;
428 if (mSeekPosition < 0) {
429 getDuration_l(NULL);
430 mSeekPosition = msec;
431 return mPlayer->seekTo(msec);
432 }
433 else {
Steve Block71f2cf12011-10-20 11:56:00 +0100434 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 return NO_ERROR;
436 }
437 }
438 LOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
439 return INVALID_OPERATION;
440}
441
442status_t MediaPlayer::seekTo(int msec)
443{
Andreas Hubereffd8d52009-03-24 20:48:51 -0700444 mLockThreadId = getThreadId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 Mutex::Autolock _l(mLock);
Andreas Hubereffd8d52009-03-24 20:48:51 -0700446 status_t result = seekTo_l(msec);
447 mLockThreadId = 0;
448
449 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450}
451
Jamie Gennisc85ca5d2011-07-13 12:59:34 -0700452status_t MediaPlayer::reset_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 mLoop = false;
455 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
456 mPrepareSync = false;
457 if (mPlayer != 0) {
458 status_t ret = mPlayer->reset();
459 if (ret != NO_ERROR) {
460 LOGE("reset() failed with return code (%d)", ret);
461 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
462 } else {
463 mCurrentState = MEDIA_PLAYER_IDLE;
464 }
James Dongc5bba6c2010-11-18 12:23:58 -0800465 // setDataSource has to be called again to create a
466 // new mediaplayer.
467 mPlayer = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 return ret;
469 }
470 clear_l();
471 return NO_ERROR;
472}
473
Jamie Gennisc85ca5d2011-07-13 12:59:34 -0700474status_t MediaPlayer::reset()
475{
Steve Block71f2cf12011-10-20 11:56:00 +0100476 ALOGV("reset");
Jamie Gennisc85ca5d2011-07-13 12:59:34 -0700477 Mutex::Autolock _l(mLock);
478 return reset_l();
479}
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481status_t MediaPlayer::setAudioStreamType(int type)
482{
Steve Block71f2cf12011-10-20 11:56:00 +0100483 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 Mutex::Autolock _l(mLock);
485 if (mStreamType == type) return NO_ERROR;
486 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
487 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
488 // Can't change the stream type after prepare
489 LOGE("setAudioStream called in state %d", mCurrentState);
490 return INVALID_OPERATION;
491 }
492 // cache
493 mStreamType = type;
494 return OK;
495}
496
497status_t MediaPlayer::setLooping(int loop)
498{
Steve Block71f2cf12011-10-20 11:56:00 +0100499 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 Mutex::Autolock _l(mLock);
501 mLoop = (loop != 0);
502 if (mPlayer != 0) {
503 return mPlayer->setLooping(loop);
504 }
505 return OK;
506}
507
508bool MediaPlayer::isLooping() {
Steve Block71f2cf12011-10-20 11:56:00 +0100509 ALOGV("isLooping");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 Mutex::Autolock _l(mLock);
511 if (mPlayer != 0) {
512 return mLoop;
513 }
Steve Block71f2cf12011-10-20 11:56:00 +0100514 ALOGV("isLooping: no active player");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 return false;
516}
517
518status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
519{
Steve Block71f2cf12011-10-20 11:56:00 +0100520 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 Mutex::Autolock _l(mLock);
522 mLeftVolume = leftVolume;
523 mRightVolume = rightVolume;
524 if (mPlayer != 0) {
525 return mPlayer->setVolume(leftVolume, rightVolume);
526 }
527 return OK;
528}
529
Eric Laurent619346f2010-06-21 09:27:30 -0700530status_t MediaPlayer::setAudioSessionId(int sessionId)
531{
Steve Block71f2cf12011-10-20 11:56:00 +0100532 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurent619346f2010-06-21 09:27:30 -0700533 Mutex::Autolock _l(mLock);
534 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
535 LOGE("setAudioSessionId called in state %d", mCurrentState);
536 return INVALID_OPERATION;
537 }
538 if (sessionId < 0) {
539 return BAD_VALUE;
540 }
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700541 if (sessionId != mAudioSessionId) {
542 AudioSystem::releaseAudioSessionId(mAudioSessionId);
543 AudioSystem::acquireAudioSessionId(sessionId);
544 mAudioSessionId = sessionId;
545 }
Eric Laurent619346f2010-06-21 09:27:30 -0700546 return NO_ERROR;
547}
548
549int MediaPlayer::getAudioSessionId()
550{
551 Mutex::Autolock _l(mLock);
552 return mAudioSessionId;
553}
554
Eric Laurent7070b362010-07-16 07:43:46 -0700555status_t MediaPlayer::setAuxEffectSendLevel(float level)
556{
Steve Block71f2cf12011-10-20 11:56:00 +0100557 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent7070b362010-07-16 07:43:46 -0700558 Mutex::Autolock _l(mLock);
559 mSendLevel = level;
560 if (mPlayer != 0) {
561 return mPlayer->setAuxEffectSendLevel(level);
562 }
563 return OK;
564}
565
566status_t MediaPlayer::attachAuxEffect(int effectId)
567{
Steve Block71f2cf12011-10-20 11:56:00 +0100568 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700569 Mutex::Autolock _l(mLock);
570 if (mPlayer == 0 ||
571 (mCurrentState & MEDIA_PLAYER_IDLE) ||
572 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
573 LOGE("attachAuxEffect called in state %d", mCurrentState);
574 return INVALID_OPERATION;
575 }
576
577 return mPlayer->attachAuxEffect(effectId);
578}
579
Gloria Wangd01ec6e2011-04-25 17:28:22 -0700580status_t MediaPlayer::setParameter(int key, const Parcel& request)
581{
Steve Block71f2cf12011-10-20 11:56:00 +0100582 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wangd01ec6e2011-04-25 17:28:22 -0700583 Mutex::Autolock _l(mLock);
584 if (mPlayer != NULL) {
585 return mPlayer->setParameter(key, request);
586 }
Steve Block71f2cf12011-10-20 11:56:00 +0100587 ALOGV("setParameter: no active player");
Gloria Wangd01ec6e2011-04-25 17:28:22 -0700588 return INVALID_OPERATION;
589}
590
591status_t MediaPlayer::getParameter(int key, Parcel *reply)
592{
Steve Block71f2cf12011-10-20 11:56:00 +0100593 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wangd01ec6e2011-04-25 17:28:22 -0700594 Mutex::Autolock _l(mLock);
595 if (mPlayer != NULL) {
596 return mPlayer->getParameter(key, reply);
597 }
Steve Block71f2cf12011-10-20 11:56:00 +0100598 ALOGV("getParameter: no active player");
Gloria Wangd01ec6e2011-04-25 17:28:22 -0700599 return INVALID_OPERATION;
600}
601
Gloria Wang162ee492011-04-11 17:23:27 -0700602void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603{
Steve Block71f2cf12011-10-20 11:56:00 +0100604 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 bool send = true;
Jason Samsebb020a2009-03-24 18:45:22 -0700606 bool locked = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
608 // TODO: In the future, we might be on the same thread if the app is
609 // running in the same process as the media server. In that case,
610 // this will deadlock.
Nicolas Catania32f82772009-06-11 16:33:49 -0700611 //
Jason Samsebb020a2009-03-24 18:45:22 -0700612 // The threadId hack below works around this for the care of prepare
Andreas Hubereffd8d52009-03-24 20:48:51 -0700613 // and seekTo within the same process.
614 // FIXME: Remember, this is a hack, it's not even a hack that is applied
615 // consistently for all use-cases, this needs to be revisited.
Jason Samsebb020a2009-03-24 18:45:22 -0700616 if (mLockThreadId != getThreadId()) {
617 mLock.lock();
618 locked = true;
Nicolas Catania32f82772009-06-11 16:33:49 -0700619 }
Jason Samsebb020a2009-03-24 18:45:22 -0700620
Eric Laurent1d53b2a2010-08-03 07:49:49 -0700621 // Allows calls from JNI in idle state to notify errors
622 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100623 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Samsebb020a2009-03-24 18:45:22 -0700624 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 return;
626 }
627
628 switch (msg) {
629 case MEDIA_NOP: // interface test message
630 break;
631 case MEDIA_PREPARED:
Steve Block71f2cf12011-10-20 11:56:00 +0100632 ALOGV("prepared");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 mCurrentState = MEDIA_PLAYER_PREPARED;
634 if (mPrepareSync) {
Steve Block71f2cf12011-10-20 11:56:00 +0100635 ALOGV("signal application thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 mPrepareSync = false;
637 mPrepareStatus = NO_ERROR;
638 mSignal.signal();
639 }
640 break;
641 case MEDIA_PLAYBACK_COMPLETE:
Steve Block71f2cf12011-10-20 11:56:00 +0100642 ALOGV("playback complete");
Marco Nelissen3c473ea2010-09-17 15:04:01 -0700643 if (mCurrentState == MEDIA_PLAYER_IDLE) {
644 LOGE("playback complete in idle state");
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 if (!mLoop) {
647 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
648 }
649 break;
650 case MEDIA_ERROR:
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700651 // Always log errors.
652 // ext1: Media framework error code.
653 // ext2: Implementation dependant error code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 LOGE("error (%d, %d)", ext1, ext2);
655 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
656 if (mPrepareSync)
657 {
Steve Block71f2cf12011-10-20 11:56:00 +0100658 ALOGV("signal application thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 mPrepareSync = false;
660 mPrepareStatus = ext1;
661 mSignal.signal();
662 send = false;
663 }
664 break;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700665 case MEDIA_INFO:
666 // ext1: Media framework error code.
667 // ext2: Implementation dependant error code.
Andreas Huber52c78322011-01-11 15:05:28 -0800668 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
669 LOGW("info/warning (%d, %d)", ext1, ext2);
670 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700671 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 case MEDIA_SEEK_COMPLETE:
Steve Block71f2cf12011-10-20 11:56:00 +0100673 ALOGV("Received seek complete");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 if (mSeekPosition != mCurrentPosition) {
Steve Block71f2cf12011-10-20 11:56:00 +0100675 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 mSeekPosition = -1;
677 seekTo_l(mCurrentPosition);
678 }
679 else {
Steve Block71f2cf12011-10-20 11:56:00 +0100680 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 mCurrentPosition = mSeekPosition = -1;
682 }
683 break;
684 case MEDIA_BUFFERING_UPDATE:
Steve Block71f2cf12011-10-20 11:56:00 +0100685 ALOGV("buffering %d", ext1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 break;
687 case MEDIA_SET_VIDEO_SIZE:
Steve Block71f2cf12011-10-20 11:56:00 +0100688 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 mVideoWidth = ext1;
690 mVideoHeight = ext2;
691 break;
Gloria Wang162ee492011-04-11 17:23:27 -0700692 case MEDIA_TIMED_TEXT:
Steve Block71f2cf12011-10-20 11:56:00 +0100693 ALOGV("Received timed text message");
Gloria Wang162ee492011-04-11 17:23:27 -0700694 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 default:
Steve Block71f2cf12011-10-20 11:56:00 +0100696 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 break;
698 }
699
700 sp<MediaPlayerListener> listener = mListener;
Jason Samsebb020a2009-03-24 18:45:22 -0700701 if (locked) mLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702
703 // this prevents re-entrant calls into client code
704 if ((listener != 0) && send) {
705 Mutex::Autolock _l(mNotifyLock);
Steve Block71f2cf12011-10-20 11:56:00 +0100706 ALOGV("callback application");
Gloria Wang162ee492011-04-11 17:23:27 -0700707 listener->notify(msg, ext1, ext2, obj);
Steve Block71f2cf12011-10-20 11:56:00 +0100708 ALOGV("back from callback");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710}
711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
713{
Steve Block71f2cf12011-10-20 11:56:00 +0100714 ALOGV("decode(%s)", url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 sp<IMemory> p;
716 const sp<IMediaPlayerService>& service = getMediaPlayerService();
717 if (service != 0) {
James Dong34bbc222010-01-15 18:13:58 -0800718 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 } else {
720 LOGE("Unable to locate media service");
721 }
722 return p;
723
724}
725
James Dong34bbc222010-01-15 18:13:58 -0800726void MediaPlayer::died()
727{
Steve Block71f2cf12011-10-20 11:56:00 +0100728 ALOGV("died");
James Dong34bbc222010-01-15 18:13:58 -0800729 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
730}
731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
733{
Steve Block71f2cf12011-10-20 11:56:00 +0100734 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 sp<IMemory> p;
736 const sp<IMediaPlayerService>& service = getMediaPlayerService();
737 if (service != 0) {
James Dong34bbc222010-01-15 18:13:58 -0800738 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 } else {
740 LOGE("Unable to locate media service");
741 }
742 return p;
743
744}
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746}; // namespace android