blob: 67a66a289f1cf6674bd088488a126a2d4ab95ffa [file] [log] [blame]
The Android Open Source Project89fa4ad2009-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 Agopian75624082009-05-19 19:08:10 -070027#include <binder/IServiceManager.h>
28#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Jamie Gennis61c7ef52011-07-13 12:59:34 -070030#include <gui/SurfaceTextureClient.h>
31
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <media/mediaplayer.h>
33#include <media/AudioTrack.h>
34
Mathias Agopian3cf61352010-02-09 17:46:37 -080035#include <surfaceflinger/Surface.h>
36
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{
49 LOGV("constructor");
50 mListener = NULL;
51 mCookie = NULL;
52 mDuration = -1;
Dima Zavinfce7a472011-04-19 22:30:36 -070053 mStreamType = AUDIO_STREAM_MUSIC;
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 Laurenta514bdb2010-06-21 09:27:30 -070063 mAudioSessionId = AudioSystem::newAudioSessionId();
Marco Nelissen3a34bef2011-08-02 13:33:41 -070064 AudioSystem::acquireAudioSessionId(mAudioSessionId);
Eric Laurent8c563ed2010-10-07 18:23:03 -070065 mSendLevel = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066}
67
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068MediaPlayer::~MediaPlayer()
69{
70 LOGV("destructor");
Marco Nelissen3a34bef2011-08-02 13:33:41 -070071 AudioSystem::releaseAudioSessionId(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072 disconnect();
73 IPCThreadState::self()->flushCommands();
74}
75
76void MediaPlayer::disconnect()
77{
78 LOGV("disconnect");
79 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 }
Jamie Gennisbea47bc2011-07-19 14:50:43 -070089
90 disconnectNativeWindow();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091}
92
93// always call with lock held
94void MediaPlayer::clear_l()
95{
96 mDuration = -1;
97 mCurrentPosition = -1;
98 mSeekPosition = -1;
99 mVideoWidth = mVideoHeight = 0;
100}
101
102status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
103{
104 LOGV("setListener");
105 Mutex::Autolock _l(mLock);
106 mListener = listener;
107 return NO_ERROR;
108}
109
110
111status_t MediaPlayer::setDataSource(const sp<IMediaPlayer>& player)
112{
113 status_t err = UNKNOWN_ERROR;
114 sp<IMediaPlayer> p;
115 { // scope for the lock
116 Mutex::Autolock _l(mLock);
117
Marco Nelissen83ff1432010-03-10 10:53:16 -0800118 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
119 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800120 LOGE("setDataSource called in state %d", mCurrentState);
121 return INVALID_OPERATION;
122 }
123
124 clear_l();
125 p = mPlayer;
126 mPlayer = player;
127 if (player != 0) {
128 mCurrentState = MEDIA_PLAYER_INITIALIZED;
129 err = NO_ERROR;
130 } else {
131 LOGE("Unable to to create media player");
132 }
133 }
134
135 if (p != 0) {
136 p->disconnect();
137 }
138
139 return err;
140}
141
Andreas Huber2db84552010-01-28 11:19:57 -0800142status_t MediaPlayer::setDataSource(
143 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144{
145 LOGV("setDataSource(%s)", url);
146 status_t err = BAD_VALUE;
147 if (url != NULL) {
148 const sp<IMediaPlayerService>& service(getMediaPlayerService());
149 if (service != 0) {
Andreas Huber2db84552010-01-28 11:19:57 -0800150 sp<IMediaPlayer> player(
Eric Laurenta514bdb2010-06-21 09:27:30 -0700151 service->create(getpid(), this, url, headers, mAudioSessionId));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 err = setDataSource(player);
153 }
154 }
155 return err;
156}
157
158status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
159{
160 LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
161 status_t err = UNKNOWN_ERROR;
162 const sp<IMediaPlayerService>& service(getMediaPlayerService());
163 if (service != 0) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700164 sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length, mAudioSessionId));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 err = setDataSource(player);
166 }
167 return err;
168}
169
Nicolas Catania1d187f12009-05-12 23:25:55 -0700170status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
171{
172 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800173 const bool hasBeenInitialized =
174 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
175 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
176 if ((mPlayer != NULL) && hasBeenInitialized) {
Nicolas Catania1d187f12009-05-12 23:25:55 -0700177 LOGV("invoke %d", request.dataSize());
178 return mPlayer->invoke(request, reply);
179 }
180 LOGE("invoke failed: wrong state %X", mCurrentState);
181 return INVALID_OPERATION;
182}
183
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700184status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
185{
186 LOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700187 Mutex::Autolock lock(mLock);
188 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700189 return NO_INIT;
190 }
191 return mPlayer->setMetadataFilter(filter);
192}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700193
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700194status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
195{
196 LOGD("getMetadata");
197 Mutex::Autolock lock(mLock);
198 if (mPlayer == NULL) {
199 return NO_INIT;
200 }
201 return mPlayer->getMetadata(update_only, apply_filter, metadata);
202}
203
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700204void MediaPlayer::disconnectNativeWindow() {
205 if (mConnectedWindow != NULL) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700206 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700207 NATIVE_WINDOW_API_MEDIA);
208
209 if (err != OK) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700210 LOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700211 strerror(-err), err);
212 }
213 }
214 mConnectedWindow.clear();
215}
216
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface)
218{
219 LOGV("setVideoSurface");
220 Mutex::Autolock _l(mLock);
221 if (mPlayer == 0) return NO_INIT;
Andreas Huber5daeb122010-08-16 08:49:37 -0700222
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700223 sp<IBinder> binder(surface == NULL ? NULL : surface->asBinder());
224 if (mConnectedWindowBinder == binder) {
225 return OK;
226 }
227
228 if (surface != NULL) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700229 status_t err = native_window_api_connect(surface.get(),
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700230 NATIVE_WINDOW_API_MEDIA);
231
232 if (err != OK) {
James Dongfada58a2011-07-21 17:32:55 -0700233 LOGE("setVideoSurface failed: %d", err);
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700234 // Note that we must do the reset before disconnecting from the ANW.
235 // Otherwise queue/dequeue calls could be made on the disconnected
236 // ANW, which may result in errors.
237 reset_l();
238
239 disconnectNativeWindow();
240
241 return err;
242 }
243 }
244
245 // Note that we must set the player's new surface before disconnecting the
246 // old one. Otherwise queue/dequeue calls could be made on the disconnected
247 // ANW, which may result in errors.
248 status_t err = mPlayer->setVideoSurface(surface);
249
250 disconnectNativeWindow();
251
252 mConnectedWindow = surface;
253
254 if (err == OK) {
255 mConnectedWindowBinder = binder;
256 } else {
257 disconnectNativeWindow();
258 }
259
260 return err;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261}
262
Glenn Kasten11731182011-02-08 17:26:17 -0800263status_t MediaPlayer::setVideoSurfaceTexture(
264 const sp<ISurfaceTexture>& surfaceTexture)
265{
266 LOGV("setVideoSurfaceTexture");
267 Mutex::Autolock _l(mLock);
268 if (mPlayer == 0) return NO_INIT;
269
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700270 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
271 surfaceTexture->asBinder());
272 if (mConnectedWindowBinder == binder) {
273 return OK;
274 }
275
276 sp<ANativeWindow> anw;
277 if (surfaceTexture != NULL) {
278 anw = new SurfaceTextureClient(surfaceTexture);
Mathias Agopianc3da3432011-07-29 17:55:48 -0700279 status_t err = native_window_api_connect(anw.get(),
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700280 NATIVE_WINDOW_API_MEDIA);
281
282 if (err != OK) {
James Dongfada58a2011-07-21 17:32:55 -0700283 LOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700284 // Note that we must do the reset before disconnecting from the ANW.
285 // Otherwise queue/dequeue calls could be made on the disconnected
286 // ANW, which may result in errors.
287 reset_l();
288
289 disconnectNativeWindow();
290
291 return err;
292 }
293 }
294
295 // Note that we must set the player's new SurfaceTexture before
296 // disconnecting the old one. Otherwise queue/dequeue calls could be made
297 // on the disconnected ANW, which may result in errors.
298 status_t err = mPlayer->setVideoSurfaceTexture(surfaceTexture);
299
300 disconnectNativeWindow();
301
302 mConnectedWindow = anw;
303
304 if (err == OK) {
305 mConnectedWindowBinder = binder;
306 } else {
307 disconnectNativeWindow();
308 }
309
310 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800311}
312
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313// must call with lock held
314status_t MediaPlayer::prepareAsync_l()
315{
316 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
317 mPlayer->setAudioStreamType(mStreamType);
318 mCurrentState = MEDIA_PLAYER_PREPARING;
319 return mPlayer->prepareAsync();
320 }
321 LOGE("prepareAsync called in state %d", mCurrentState);
322 return INVALID_OPERATION;
323}
324
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700325// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
326// one defined in the Android framework and one provided by the implementation
327// that generated the error. The sync version of prepare returns only 1 error
328// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329status_t MediaPlayer::prepare()
330{
331 LOGV("prepare");
332 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700333 mLockThreadId = getThreadId();
334 if (mPrepareSync) {
335 mLockThreadId = 0;
336 return -EALREADY;
337 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338 mPrepareSync = true;
339 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700340 if (ret != NO_ERROR) {
341 mLockThreadId = 0;
342 return ret;
343 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344
345 if (mPrepareSync) {
346 mSignal.wait(mLock); // wait for prepare done
347 mPrepareSync = false;
348 }
349 LOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700350 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 return mPrepareStatus;
352}
353
354status_t MediaPlayer::prepareAsync()
355{
356 LOGV("prepareAsync");
357 Mutex::Autolock _l(mLock);
358 return prepareAsync_l();
359}
360
361status_t MediaPlayer::start()
362{
363 LOGV("start");
364 Mutex::Autolock _l(mLock);
365 if (mCurrentState & MEDIA_PLAYER_STARTED)
366 return NO_ERROR;
367 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
368 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
369 mPlayer->setLooping(mLoop);
370 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700371 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 mCurrentState = MEDIA_PLAYER_STARTED;
373 status_t ret = mPlayer->start();
374 if (ret != NO_ERROR) {
375 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
376 } else {
377 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
378 LOGV("playback completed immediately following start()");
379 }
380 }
381 return ret;
382 }
383 LOGE("start called in state %d", mCurrentState);
384 return INVALID_OPERATION;
385}
386
387status_t MediaPlayer::stop()
388{
389 LOGV("stop");
390 Mutex::Autolock _l(mLock);
391 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
392 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
393 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
394 status_t ret = mPlayer->stop();
395 if (ret != NO_ERROR) {
396 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
397 } else {
398 mCurrentState = MEDIA_PLAYER_STOPPED;
399 }
400 return ret;
401 }
402 LOGE("stop called in state %d", mCurrentState);
403 return INVALID_OPERATION;
404}
405
406status_t MediaPlayer::pause()
407{
408 LOGV("pause");
409 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800410 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 return NO_ERROR;
412 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
413 status_t ret = mPlayer->pause();
414 if (ret != NO_ERROR) {
415 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
416 } else {
417 mCurrentState = MEDIA_PLAYER_PAUSED;
418 }
419 return ret;
420 }
421 LOGE("pause called in state %d", mCurrentState);
422 return INVALID_OPERATION;
423}
424
425bool MediaPlayer::isPlaying()
426{
427 Mutex::Autolock _l(mLock);
428 if (mPlayer != 0) {
429 bool temp = false;
430 mPlayer->isPlaying(&temp);
431 LOGV("isPlaying: %d", temp);
432 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
433 LOGE("internal/external state mismatch corrected");
434 mCurrentState = MEDIA_PLAYER_PAUSED;
435 }
436 return temp;
437 }
438 LOGV("isPlaying: no active player");
439 return false;
440}
441
442status_t MediaPlayer::getVideoWidth(int *w)
443{
444 LOGV("getVideoWidth");
445 Mutex::Autolock _l(mLock);
446 if (mPlayer == 0) return INVALID_OPERATION;
447 *w = mVideoWidth;
448 return NO_ERROR;
449}
450
451status_t MediaPlayer::getVideoHeight(int *h)
452{
453 LOGV("getVideoHeight");
454 Mutex::Autolock _l(mLock);
455 if (mPlayer == 0) return INVALID_OPERATION;
456 *h = mVideoHeight;
457 return NO_ERROR;
458}
459
460status_t MediaPlayer::getCurrentPosition(int *msec)
461{
462 LOGV("getCurrentPosition");
463 Mutex::Autolock _l(mLock);
464 if (mPlayer != 0) {
465 if (mCurrentPosition >= 0) {
466 LOGV("Using cached seek position: %d", mCurrentPosition);
467 *msec = mCurrentPosition;
468 return NO_ERROR;
469 }
470 return mPlayer->getCurrentPosition(msec);
471 }
472 return INVALID_OPERATION;
473}
474
475status_t MediaPlayer::getDuration_l(int *msec)
476{
477 LOGV("getDuration");
478 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
479 if (mPlayer != 0 && isValidState) {
480 status_t ret = NO_ERROR;
481 if (mDuration <= 0)
482 ret = mPlayer->getDuration(&mDuration);
483 if (msec)
484 *msec = mDuration;
485 return ret;
486 }
487 LOGE("Attempt to call getDuration without a valid mediaplayer");
488 return INVALID_OPERATION;
489}
490
491status_t MediaPlayer::getDuration(int *msec)
492{
493 Mutex::Autolock _l(mLock);
494 return getDuration_l(msec);
495}
496
497status_t MediaPlayer::seekTo_l(int msec)
498{
499 LOGV("seekTo %d", msec);
500 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
501 if ( msec < 0 ) {
502 LOGW("Attempt to seek to invalid position: %d", msec);
503 msec = 0;
504 } else if ((mDuration > 0) && (msec > mDuration)) {
505 LOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
506 msec = mDuration;
507 }
508 // cache duration
509 mCurrentPosition = msec;
510 if (mSeekPosition < 0) {
511 getDuration_l(NULL);
512 mSeekPosition = msec;
513 return mPlayer->seekTo(msec);
514 }
515 else {
516 LOGV("Seek in progress - queue up seekTo[%d]", msec);
517 return NO_ERROR;
518 }
519 }
520 LOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
521 return INVALID_OPERATION;
522}
523
524status_t MediaPlayer::seekTo(int msec)
525{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700526 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700528 status_t result = seekTo_l(msec);
529 mLockThreadId = 0;
530
531 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532}
533
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700534status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536 mLoop = false;
537 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
538 mPrepareSync = false;
539 if (mPlayer != 0) {
540 status_t ret = mPlayer->reset();
541 if (ret != NO_ERROR) {
542 LOGE("reset() failed with return code (%d)", ret);
543 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
544 } else {
545 mCurrentState = MEDIA_PLAYER_IDLE;
546 }
James Donga1680bc2010-11-18 12:23:58 -0800547 // setDataSource has to be called again to create a
548 // new mediaplayer.
549 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 return ret;
551 }
552 clear_l();
553 return NO_ERROR;
554}
555
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700556status_t MediaPlayer::reset()
557{
558 LOGV("reset");
559 Mutex::Autolock _l(mLock);
560 return reset_l();
561}
562
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563status_t MediaPlayer::setAudioStreamType(int type)
564{
565 LOGV("MediaPlayer::setAudioStreamType");
566 Mutex::Autolock _l(mLock);
567 if (mStreamType == type) return NO_ERROR;
568 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
569 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
570 // Can't change the stream type after prepare
571 LOGE("setAudioStream called in state %d", mCurrentState);
572 return INVALID_OPERATION;
573 }
574 // cache
575 mStreamType = type;
576 return OK;
577}
578
579status_t MediaPlayer::setLooping(int loop)
580{
581 LOGV("MediaPlayer::setLooping");
582 Mutex::Autolock _l(mLock);
583 mLoop = (loop != 0);
584 if (mPlayer != 0) {
585 return mPlayer->setLooping(loop);
586 }
587 return OK;
588}
589
590bool MediaPlayer::isLooping() {
591 LOGV("isLooping");
592 Mutex::Autolock _l(mLock);
593 if (mPlayer != 0) {
594 return mLoop;
595 }
596 LOGV("isLooping: no active player");
597 return false;
598}
599
600status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
601{
602 LOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
603 Mutex::Autolock _l(mLock);
604 mLeftVolume = leftVolume;
605 mRightVolume = rightVolume;
606 if (mPlayer != 0) {
607 return mPlayer->setVolume(leftVolume, rightVolume);
608 }
609 return OK;
610}
611
Eric Laurenta514bdb2010-06-21 09:27:30 -0700612status_t MediaPlayer::setAudioSessionId(int sessionId)
613{
614 LOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
615 Mutex::Autolock _l(mLock);
616 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
617 LOGE("setAudioSessionId called in state %d", mCurrentState);
618 return INVALID_OPERATION;
619 }
620 if (sessionId < 0) {
621 return BAD_VALUE;
622 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700623 if (sessionId != mAudioSessionId) {
624 AudioSystem::releaseAudioSessionId(mAudioSessionId);
625 AudioSystem::acquireAudioSessionId(sessionId);
626 mAudioSessionId = sessionId;
627 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700628 return NO_ERROR;
629}
630
631int MediaPlayer::getAudioSessionId()
632{
633 Mutex::Autolock _l(mLock);
634 return mAudioSessionId;
635}
636
Eric Laurent2beeb502010-07-16 07:43:46 -0700637status_t MediaPlayer::setAuxEffectSendLevel(float level)
638{
639 LOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
640 Mutex::Autolock _l(mLock);
641 mSendLevel = level;
642 if (mPlayer != 0) {
643 return mPlayer->setAuxEffectSendLevel(level);
644 }
645 return OK;
646}
647
648status_t MediaPlayer::attachAuxEffect(int effectId)
649{
650 LOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
651 Mutex::Autolock _l(mLock);
652 if (mPlayer == 0 ||
653 (mCurrentState & MEDIA_PLAYER_IDLE) ||
654 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
655 LOGE("attachAuxEffect called in state %d", mCurrentState);
656 return INVALID_OPERATION;
657 }
658
659 return mPlayer->attachAuxEffect(effectId);
660}
661
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700662status_t MediaPlayer::setParameter(int key, const Parcel& request)
663{
664 LOGV("MediaPlayer::setParameter(%d)", key);
665 Mutex::Autolock _l(mLock);
666 if (mPlayer != NULL) {
667 return mPlayer->setParameter(key, request);
668 }
669 LOGV("setParameter: no active player");
670 return INVALID_OPERATION;
671}
672
673status_t MediaPlayer::getParameter(int key, Parcel *reply)
674{
675 LOGV("MediaPlayer::getParameter(%d)", key);
676 Mutex::Autolock _l(mLock);
677 if (mPlayer != NULL) {
678 return mPlayer->getParameter(key, reply);
679 }
680 LOGV("getParameter: no active player");
681 return INVALID_OPERATION;
682}
683
Gloria Wangb483c472011-04-11 17:23:27 -0700684void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685{
686 LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
687 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700688 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689
690 // TODO: In the future, we might be on the same thread if the app is
691 // running in the same process as the media server. In that case,
692 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700693 //
Jason Sams1af452f2009-03-24 18:45:22 -0700694 // The threadId hack below works around this for the care of prepare
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700695 // and seekTo within the same process.
696 // FIXME: Remember, this is a hack, it's not even a hack that is applied
697 // consistently for all use-cases, this needs to be revisited.
Jason Sams1af452f2009-03-24 18:45:22 -0700698 if (mLockThreadId != getThreadId()) {
699 mLock.lock();
700 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700701 }
Jason Sams1af452f2009-03-24 18:45:22 -0700702
Eric Laurent3b268442010-08-03 07:49:49 -0700703 // Allows calls from JNI in idle state to notify errors
704 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705 LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700706 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 return;
708 }
709
710 switch (msg) {
711 case MEDIA_NOP: // interface test message
712 break;
713 case MEDIA_PREPARED:
714 LOGV("prepared");
715 mCurrentState = MEDIA_PLAYER_PREPARED;
716 if (mPrepareSync) {
717 LOGV("signal application thread");
718 mPrepareSync = false;
719 mPrepareStatus = NO_ERROR;
720 mSignal.signal();
721 }
722 break;
723 case MEDIA_PLAYBACK_COMPLETE:
724 LOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700725 if (mCurrentState == MEDIA_PLAYER_IDLE) {
726 LOGE("playback complete in idle state");
727 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 if (!mLoop) {
729 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
730 }
731 break;
732 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700733 // Always log errors.
734 // ext1: Media framework error code.
735 // ext2: Implementation dependant error code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800736 LOGE("error (%d, %d)", ext1, ext2);
737 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
738 if (mPrepareSync)
739 {
740 LOGV("signal application thread");
741 mPrepareSync = false;
742 mPrepareStatus = ext1;
743 mSignal.signal();
744 send = false;
745 }
746 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700747 case MEDIA_INFO:
748 // ext1: Media framework error code.
749 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800750 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
751 LOGW("info/warning (%d, %d)", ext1, ext2);
752 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700753 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754 case MEDIA_SEEK_COMPLETE:
755 LOGV("Received seek complete");
756 if (mSeekPosition != mCurrentPosition) {
757 LOGV("Executing queued seekTo(%d)", mSeekPosition);
758 mSeekPosition = -1;
759 seekTo_l(mCurrentPosition);
760 }
761 else {
762 LOGV("All seeks complete - return to regularly scheduled program");
763 mCurrentPosition = mSeekPosition = -1;
764 }
765 break;
766 case MEDIA_BUFFERING_UPDATE:
767 LOGV("buffering %d", ext1);
768 break;
769 case MEDIA_SET_VIDEO_SIZE:
770 LOGV("New video size %d x %d", ext1, ext2);
771 mVideoWidth = ext1;
772 mVideoHeight = ext2;
773 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700774 case MEDIA_TIMED_TEXT:
775 LOGV("Received timed text message");
776 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777 default:
778 LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
779 break;
780 }
781
782 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700783 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800784
785 // this prevents re-entrant calls into client code
786 if ((listener != 0) && send) {
787 Mutex::Autolock _l(mNotifyLock);
788 LOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700789 listener->notify(msg, ext1, ext2, obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790 LOGV("back from callback");
791 }
792}
793
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800794/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
795{
796 LOGV("decode(%s)", url);
797 sp<IMemory> p;
798 const sp<IMediaPlayerService>& service = getMediaPlayerService();
799 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800800 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800801 } else {
802 LOGE("Unable to locate media service");
803 }
804 return p;
805
806}
807
James Dongdd172fc2010-01-15 18:13:58 -0800808void MediaPlayer::died()
809{
810 LOGV("died");
811 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
812}
813
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800814/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
815{
816 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
817 sp<IMemory> p;
818 const sp<IMediaPlayerService>& service = getMediaPlayerService();
819 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800820 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800821 } else {
822 LOGE("Unable to locate media service");
823 }
824 return p;
825
826}
827
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800828}; // namespace android