blob: b43f75f31e90a7270a201d59b6584ab3fb5cbef4 [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
30#include <media/mediaplayer.h>
31#include <media/AudioTrack.h>
32
Mathias Agopian3cf61352010-02-09 17:46:37 -080033#include <surfaceflinger/Surface.h>
34
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Andreas Huber2db84552010-01-28 11:19:57 -080037#include <utils/KeyedVector.h>
38#include <utils/String8.h>
39
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040namespace android {
41
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042MediaPlayer::MediaPlayer()
43{
44 LOGV("constructor");
45 mListener = NULL;
46 mCookie = NULL;
47 mDuration = -1;
48 mStreamType = AudioSystem::MUSIC;
49 mCurrentPosition = -1;
50 mSeekPosition = -1;
51 mCurrentState = MEDIA_PLAYER_IDLE;
52 mPrepareSync = false;
53 mPrepareStatus = NO_ERROR;
54 mLoop = false;
55 mLeftVolume = mRightVolume = 1.0;
56 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070057 mLockThreadId = 0;
Eric Laurenta514bdb2010-06-21 09:27:30 -070058 mAudioSessionId = AudioSystem::newAudioSessionId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080059}
60
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061MediaPlayer::~MediaPlayer()
62{
63 LOGV("destructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 disconnect();
65 IPCThreadState::self()->flushCommands();
66}
67
68void MediaPlayer::disconnect()
69{
70 LOGV("disconnect");
71 sp<IMediaPlayer> p;
72 {
73 Mutex::Autolock _l(mLock);
74 p = mPlayer;
75 mPlayer.clear();
76 }
77
78 if (p != 0) {
79 p->disconnect();
80 }
81}
82
83// always call with lock held
84void MediaPlayer::clear_l()
85{
86 mDuration = -1;
87 mCurrentPosition = -1;
88 mSeekPosition = -1;
89 mVideoWidth = mVideoHeight = 0;
90}
91
92status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
93{
94 LOGV("setListener");
95 Mutex::Autolock _l(mLock);
96 mListener = listener;
97 return NO_ERROR;
98}
99
100
101status_t MediaPlayer::setDataSource(const sp<IMediaPlayer>& player)
102{
103 status_t err = UNKNOWN_ERROR;
104 sp<IMediaPlayer> p;
105 { // scope for the lock
106 Mutex::Autolock _l(mLock);
107
Marco Nelissen83ff1432010-03-10 10:53:16 -0800108 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
109 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110 LOGE("setDataSource called in state %d", mCurrentState);
111 return INVALID_OPERATION;
112 }
113
114 clear_l();
115 p = mPlayer;
116 mPlayer = player;
117 if (player != 0) {
118 mCurrentState = MEDIA_PLAYER_INITIALIZED;
119 err = NO_ERROR;
120 } else {
121 LOGE("Unable to to create media player");
122 }
123 }
124
125 if (p != 0) {
126 p->disconnect();
127 }
128
129 return err;
130}
131
Andreas Huber2db84552010-01-28 11:19:57 -0800132status_t MediaPlayer::setDataSource(
133 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134{
135 LOGV("setDataSource(%s)", url);
136 status_t err = BAD_VALUE;
137 if (url != NULL) {
138 const sp<IMediaPlayerService>& service(getMediaPlayerService());
139 if (service != 0) {
Andreas Huber2db84552010-01-28 11:19:57 -0800140 sp<IMediaPlayer> player(
Eric Laurenta514bdb2010-06-21 09:27:30 -0700141 service->create(getpid(), this, url, headers, mAudioSessionId));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 err = setDataSource(player);
143 }
144 }
145 return err;
146}
147
148status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
149{
150 LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
151 status_t err = UNKNOWN_ERROR;
152 const sp<IMediaPlayerService>& service(getMediaPlayerService());
153 if (service != 0) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700154 sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length, mAudioSessionId));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 err = setDataSource(player);
156 }
157 return err;
158}
159
Nicolas Catania1d187f12009-05-12 23:25:55 -0700160status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
161{
162 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800163 const bool hasBeenInitialized =
164 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
165 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
166 if ((mPlayer != NULL) && hasBeenInitialized) {
Nicolas Catania1d187f12009-05-12 23:25:55 -0700167 LOGV("invoke %d", request.dataSize());
168 return mPlayer->invoke(request, reply);
169 }
170 LOGE("invoke failed: wrong state %X", mCurrentState);
171 return INVALID_OPERATION;
172}
173
Andreas Huber4e92c7e2010-02-12 12:35:58 -0800174status_t MediaPlayer::suspend() {
175 Mutex::Autolock _l(mLock);
176 return mPlayer->suspend();
177}
178
179status_t MediaPlayer::resume() {
180 Mutex::Autolock _l(mLock);
181 return mPlayer->resume();
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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface)
205{
206 LOGV("setVideoSurface");
207 Mutex::Autolock _l(mLock);
208 if (mPlayer == 0) return NO_INIT;
Marco Nelissenaa371732009-07-10 09:34:59 -0700209 if (surface != NULL)
210 return mPlayer->setVideoSurface(surface->getISurface());
211 else
212 return mPlayer->setVideoSurface(NULL);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213}
214
215// must call with lock held
216status_t MediaPlayer::prepareAsync_l()
217{
218 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
219 mPlayer->setAudioStreamType(mStreamType);
220 mCurrentState = MEDIA_PLAYER_PREPARING;
221 return mPlayer->prepareAsync();
222 }
223 LOGE("prepareAsync called in state %d", mCurrentState);
224 return INVALID_OPERATION;
225}
226
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700227// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
228// one defined in the Android framework and one provided by the implementation
229// that generated the error. The sync version of prepare returns only 1 error
230// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231status_t MediaPlayer::prepare()
232{
233 LOGV("prepare");
234 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700235 mLockThreadId = getThreadId();
236 if (mPrepareSync) {
237 mLockThreadId = 0;
238 return -EALREADY;
239 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240 mPrepareSync = true;
241 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700242 if (ret != NO_ERROR) {
243 mLockThreadId = 0;
244 return ret;
245 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246
247 if (mPrepareSync) {
248 mSignal.wait(mLock); // wait for prepare done
249 mPrepareSync = false;
250 }
251 LOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700252 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 return mPrepareStatus;
254}
255
256status_t MediaPlayer::prepareAsync()
257{
258 LOGV("prepareAsync");
259 Mutex::Autolock _l(mLock);
260 return prepareAsync_l();
261}
262
263status_t MediaPlayer::start()
264{
265 LOGV("start");
266 Mutex::Autolock _l(mLock);
267 if (mCurrentState & MEDIA_PLAYER_STARTED)
268 return NO_ERROR;
269 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
270 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
271 mPlayer->setLooping(mLoop);
272 mPlayer->setVolume(mLeftVolume, mRightVolume);
273 mCurrentState = MEDIA_PLAYER_STARTED;
274 status_t ret = mPlayer->start();
275 if (ret != NO_ERROR) {
276 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
277 } else {
278 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
279 LOGV("playback completed immediately following start()");
280 }
281 }
282 return ret;
283 }
284 LOGE("start called in state %d", mCurrentState);
285 return INVALID_OPERATION;
286}
287
288status_t MediaPlayer::stop()
289{
290 LOGV("stop");
291 Mutex::Autolock _l(mLock);
292 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
293 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
294 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
295 status_t ret = mPlayer->stop();
296 if (ret != NO_ERROR) {
297 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
298 } else {
299 mCurrentState = MEDIA_PLAYER_STOPPED;
300 }
301 return ret;
302 }
303 LOGE("stop called in state %d", mCurrentState);
304 return INVALID_OPERATION;
305}
306
307status_t MediaPlayer::pause()
308{
309 LOGV("pause");
310 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800311 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 return NO_ERROR;
313 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
314 status_t ret = mPlayer->pause();
315 if (ret != NO_ERROR) {
316 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
317 } else {
318 mCurrentState = MEDIA_PLAYER_PAUSED;
319 }
320 return ret;
321 }
322 LOGE("pause called in state %d", mCurrentState);
323 return INVALID_OPERATION;
324}
325
326bool MediaPlayer::isPlaying()
327{
328 Mutex::Autolock _l(mLock);
329 if (mPlayer != 0) {
330 bool temp = false;
331 mPlayer->isPlaying(&temp);
332 LOGV("isPlaying: %d", temp);
333 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
334 LOGE("internal/external state mismatch corrected");
335 mCurrentState = MEDIA_PLAYER_PAUSED;
336 }
337 return temp;
338 }
339 LOGV("isPlaying: no active player");
340 return false;
341}
342
343status_t MediaPlayer::getVideoWidth(int *w)
344{
345 LOGV("getVideoWidth");
346 Mutex::Autolock _l(mLock);
347 if (mPlayer == 0) return INVALID_OPERATION;
348 *w = mVideoWidth;
349 return NO_ERROR;
350}
351
352status_t MediaPlayer::getVideoHeight(int *h)
353{
354 LOGV("getVideoHeight");
355 Mutex::Autolock _l(mLock);
356 if (mPlayer == 0) return INVALID_OPERATION;
357 *h = mVideoHeight;
358 return NO_ERROR;
359}
360
361status_t MediaPlayer::getCurrentPosition(int *msec)
362{
363 LOGV("getCurrentPosition");
364 Mutex::Autolock _l(mLock);
365 if (mPlayer != 0) {
366 if (mCurrentPosition >= 0) {
367 LOGV("Using cached seek position: %d", mCurrentPosition);
368 *msec = mCurrentPosition;
369 return NO_ERROR;
370 }
371 return mPlayer->getCurrentPosition(msec);
372 }
373 return INVALID_OPERATION;
374}
375
376status_t MediaPlayer::getDuration_l(int *msec)
377{
378 LOGV("getDuration");
379 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
380 if (mPlayer != 0 && isValidState) {
381 status_t ret = NO_ERROR;
382 if (mDuration <= 0)
383 ret = mPlayer->getDuration(&mDuration);
384 if (msec)
385 *msec = mDuration;
386 return ret;
387 }
388 LOGE("Attempt to call getDuration without a valid mediaplayer");
389 return INVALID_OPERATION;
390}
391
392status_t MediaPlayer::getDuration(int *msec)
393{
394 Mutex::Autolock _l(mLock);
395 return getDuration_l(msec);
396}
397
398status_t MediaPlayer::seekTo_l(int msec)
399{
400 LOGV("seekTo %d", msec);
401 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
402 if ( msec < 0 ) {
403 LOGW("Attempt to seek to invalid position: %d", msec);
404 msec = 0;
405 } else if ((mDuration > 0) && (msec > mDuration)) {
406 LOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
407 msec = mDuration;
408 }
409 // cache duration
410 mCurrentPosition = msec;
411 if (mSeekPosition < 0) {
412 getDuration_l(NULL);
413 mSeekPosition = msec;
414 return mPlayer->seekTo(msec);
415 }
416 else {
417 LOGV("Seek in progress - queue up seekTo[%d]", msec);
418 return NO_ERROR;
419 }
420 }
421 LOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
422 return INVALID_OPERATION;
423}
424
425status_t MediaPlayer::seekTo(int msec)
426{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700427 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700429 status_t result = seekTo_l(msec);
430 mLockThreadId = 0;
431
432 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433}
434
435status_t MediaPlayer::reset()
436{
437 LOGV("reset");
438 Mutex::Autolock _l(mLock);
439 mLoop = false;
440 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
441 mPrepareSync = false;
442 if (mPlayer != 0) {
443 status_t ret = mPlayer->reset();
444 if (ret != NO_ERROR) {
445 LOGE("reset() failed with return code (%d)", ret);
446 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
447 } else {
448 mCurrentState = MEDIA_PLAYER_IDLE;
449 }
450 return ret;
451 }
452 clear_l();
453 return NO_ERROR;
454}
455
456status_t MediaPlayer::setAudioStreamType(int type)
457{
458 LOGV("MediaPlayer::setAudioStreamType");
459 Mutex::Autolock _l(mLock);
460 if (mStreamType == type) return NO_ERROR;
461 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
462 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
463 // Can't change the stream type after prepare
464 LOGE("setAudioStream called in state %d", mCurrentState);
465 return INVALID_OPERATION;
466 }
467 // cache
468 mStreamType = type;
469 return OK;
470}
471
472status_t MediaPlayer::setLooping(int loop)
473{
474 LOGV("MediaPlayer::setLooping");
475 Mutex::Autolock _l(mLock);
476 mLoop = (loop != 0);
477 if (mPlayer != 0) {
478 return mPlayer->setLooping(loop);
479 }
480 return OK;
481}
482
483bool MediaPlayer::isLooping() {
484 LOGV("isLooping");
485 Mutex::Autolock _l(mLock);
486 if (mPlayer != 0) {
487 return mLoop;
488 }
489 LOGV("isLooping: no active player");
490 return false;
491}
492
493status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
494{
495 LOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
496 Mutex::Autolock _l(mLock);
497 mLeftVolume = leftVolume;
498 mRightVolume = rightVolume;
499 if (mPlayer != 0) {
500 return mPlayer->setVolume(leftVolume, rightVolume);
501 }
502 return OK;
503}
504
Eric Laurenta514bdb2010-06-21 09:27:30 -0700505status_t MediaPlayer::setAudioSessionId(int sessionId)
506{
507 LOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
508 Mutex::Autolock _l(mLock);
509 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
510 LOGE("setAudioSessionId called in state %d", mCurrentState);
511 return INVALID_OPERATION;
512 }
513 if (sessionId < 0) {
514 return BAD_VALUE;
515 }
516 mAudioSessionId = sessionId;
517 return NO_ERROR;
518}
519
520int MediaPlayer::getAudioSessionId()
521{
522 Mutex::Autolock _l(mLock);
523 return mAudioSessionId;
524}
525
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526void MediaPlayer::notify(int msg, int ext1, int ext2)
527{
528 LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
529 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700530 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531
532 // TODO: In the future, we might be on the same thread if the app is
533 // running in the same process as the media server. In that case,
534 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700535 //
Jason Sams1af452f2009-03-24 18:45:22 -0700536 // The threadId hack below works around this for the care of prepare
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700537 // and seekTo within the same process.
538 // FIXME: Remember, this is a hack, it's not even a hack that is applied
539 // consistently for all use-cases, this needs to be revisited.
Jason Sams1af452f2009-03-24 18:45:22 -0700540 if (mLockThreadId != getThreadId()) {
541 mLock.lock();
542 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700543 }
Jason Sams1af452f2009-03-24 18:45:22 -0700544
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 if (mPlayer == 0) {
546 LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700547 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 return;
549 }
550
551 switch (msg) {
552 case MEDIA_NOP: // interface test message
553 break;
554 case MEDIA_PREPARED:
555 LOGV("prepared");
556 mCurrentState = MEDIA_PLAYER_PREPARED;
557 if (mPrepareSync) {
558 LOGV("signal application thread");
559 mPrepareSync = false;
560 mPrepareStatus = NO_ERROR;
561 mSignal.signal();
562 }
563 break;
564 case MEDIA_PLAYBACK_COMPLETE:
565 LOGV("playback complete");
566 if (!mLoop) {
567 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
568 }
569 break;
570 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700571 // Always log errors.
572 // ext1: Media framework error code.
573 // ext2: Implementation dependant error code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 LOGE("error (%d, %d)", ext1, ext2);
575 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
576 if (mPrepareSync)
577 {
578 LOGV("signal application thread");
579 mPrepareSync = false;
580 mPrepareStatus = ext1;
581 mSignal.signal();
582 send = false;
583 }
584 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700585 case MEDIA_INFO:
586 // ext1: Media framework error code.
587 // ext2: Implementation dependant error code.
588 LOGW("info/warning (%d, %d)", ext1, ext2);
589 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590 case MEDIA_SEEK_COMPLETE:
591 LOGV("Received seek complete");
592 if (mSeekPosition != mCurrentPosition) {
593 LOGV("Executing queued seekTo(%d)", mSeekPosition);
594 mSeekPosition = -1;
595 seekTo_l(mCurrentPosition);
596 }
597 else {
598 LOGV("All seeks complete - return to regularly scheduled program");
599 mCurrentPosition = mSeekPosition = -1;
600 }
601 break;
602 case MEDIA_BUFFERING_UPDATE:
603 LOGV("buffering %d", ext1);
604 break;
605 case MEDIA_SET_VIDEO_SIZE:
606 LOGV("New video size %d x %d", ext1, ext2);
607 mVideoWidth = ext1;
608 mVideoHeight = ext2;
609 break;
610 default:
611 LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
612 break;
613 }
614
615 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700616 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617
618 // this prevents re-entrant calls into client code
619 if ((listener != 0) && send) {
620 Mutex::Autolock _l(mNotifyLock);
621 LOGV("callback application");
622 listener->notify(msg, ext1, ext2);
623 LOGV("back from callback");
624 }
625}
626
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
628{
629 LOGV("decode(%s)", url);
630 sp<IMemory> p;
631 const sp<IMediaPlayerService>& service = getMediaPlayerService();
632 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800633 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 } else {
635 LOGE("Unable to locate media service");
636 }
637 return p;
638
639}
640
James Dongdd172fc2010-01-15 18:13:58 -0800641void MediaPlayer::died()
642{
643 LOGV("died");
644 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
645}
646
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
648{
649 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
650 sp<IMemory> p;
651 const sp<IMediaPlayerService>& service = getMediaPlayerService();
652 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800653 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654 } else {
655 LOGE("Unable to locate media service");
656 }
657 return p;
658
659}
660
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661}; // namespace android