blob: 0ee0249a677391f44d4224be899411a7ab552c59 [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
30#include <media/mediaplayer.h>
31#include <media/AudioTrack.h>
32
Mathias Agopian000479f2010-02-09 17:46:37 -080033#include <surfaceflinger/Surface.h>
34
Mathias Agopian07952722009-05-19 19:08:10 -070035#include <binder/MemoryBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Andreas Huber25643002010-01-28 11:19:57 -080037#include <utils/KeyedVector.h>
38#include <utils/String8.h>
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040namespace android {
41
The Android Open Source Project9066cfe2009-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 Samsebb020a2009-03-24 18:45:22 -070057 mLockThreadId = 0;
Eric Laurent619346f2010-06-21 09:27:30 -070058 mAudioSessionId = AudioSystem::newAudioSessionId();
Eric Laurentb3bdf3f2010-10-07 18:23:03 -070059 mSendLevel = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060}
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062MediaPlayer::~MediaPlayer()
63{
64 LOGV("destructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 disconnect();
66 IPCThreadState::self()->flushCommands();
67}
68
69void MediaPlayer::disconnect()
70{
71 LOGV("disconnect");
72 sp<IMediaPlayer> p;
73 {
74 Mutex::Autolock _l(mLock);
75 p = mPlayer;
76 mPlayer.clear();
77 }
78
79 if (p != 0) {
80 p->disconnect();
81 }
82}
83
84// always call with lock held
85void MediaPlayer::clear_l()
86{
87 mDuration = -1;
88 mCurrentPosition = -1;
89 mSeekPosition = -1;
90 mVideoWidth = mVideoHeight = 0;
91}
92
93status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
94{
95 LOGV("setListener");
96 Mutex::Autolock _l(mLock);
97 mListener = listener;
98 return NO_ERROR;
99}
100
101
102status_t MediaPlayer::setDataSource(const sp<IMediaPlayer>& player)
103{
104 status_t err = UNKNOWN_ERROR;
105 sp<IMediaPlayer> p;
106 { // scope for the lock
107 Mutex::Autolock _l(mLock);
108
Marco Nelissene44b41b2010-03-10 10:53:16 -0800109 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
110 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 LOGE("setDataSource called in state %d", mCurrentState);
112 return INVALID_OPERATION;
113 }
114
115 clear_l();
116 p = mPlayer;
117 mPlayer = player;
118 if (player != 0) {
119 mCurrentState = MEDIA_PLAYER_INITIALIZED;
120 err = NO_ERROR;
121 } else {
122 LOGE("Unable to to create media player");
123 }
124 }
125
126 if (p != 0) {
127 p->disconnect();
128 }
129
130 return err;
131}
132
Andreas Huber25643002010-01-28 11:19:57 -0800133status_t MediaPlayer::setDataSource(
134 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135{
136 LOGV("setDataSource(%s)", url);
137 status_t err = BAD_VALUE;
138 if (url != NULL) {
139 const sp<IMediaPlayerService>& service(getMediaPlayerService());
140 if (service != 0) {
Andreas Huber25643002010-01-28 11:19:57 -0800141 sp<IMediaPlayer> player(
Eric Laurent619346f2010-06-21 09:27:30 -0700142 service->create(getpid(), this, url, headers, mAudioSessionId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 err = setDataSource(player);
144 }
145 }
146 return err;
147}
148
149status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
150{
151 LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
152 status_t err = UNKNOWN_ERROR;
153 const sp<IMediaPlayerService>& service(getMediaPlayerService());
154 if (service != 0) {
Eric Laurent619346f2010-06-21 09:27:30 -0700155 sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length, mAudioSessionId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 err = setDataSource(player);
157 }
158 return err;
159}
160
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700161status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
162{
163 Mutex::Autolock _l(mLock);
Nicolas Catania7bd3d7d2010-03-10 10:41:04 -0800164 const bool hasBeenInitialized =
165 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
166 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
167 if ((mPlayer != NULL) && hasBeenInitialized) {
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700168 LOGV("invoke %d", request.dataSize());
169 return mPlayer->invoke(request, reply);
170 }
171 LOGE("invoke failed: wrong state %X", mCurrentState);
172 return INVALID_OPERATION;
173}
174
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700175status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
176{
177 LOGD("setMetadataFilter");
Nicolas Catania5d55c712009-07-09 09:21:33 -0700178 Mutex::Autolock lock(mLock);
179 if (mPlayer == NULL) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700180 return NO_INIT;
181 }
182 return mPlayer->setMetadataFilter(filter);
183}
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700184
Nicolas Catania5d55c712009-07-09 09:21:33 -0700185status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
186{
187 LOGD("getMetadata");
188 Mutex::Autolock lock(mLock);
189 if (mPlayer == NULL) {
190 return NO_INIT;
191 }
192 return mPlayer->getMetadata(update_only, apply_filter, metadata);
193}
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface)
196{
197 LOGV("setVideoSurface");
198 Mutex::Autolock _l(mLock);
199 if (mPlayer == 0) return NO_INIT;
Andreas Hubere3c01832010-08-16 08:49:37 -0700200
Andreas Hubere3c01832010-08-16 08:49:37 -0700201 return mPlayer->setVideoSurface(surface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202}
203
Glenn Kastencc562a32011-02-08 17:26:17 -0800204status_t MediaPlayer::setVideoSurfaceTexture(
205 const sp<ISurfaceTexture>& surfaceTexture)
206{
207 LOGV("setVideoSurfaceTexture");
208 Mutex::Autolock _l(mLock);
209 if (mPlayer == 0) return NO_INIT;
210
211 return mPlayer->setVideoSurfaceTexture(surfaceTexture);
212}
213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214// must call with lock held
215status_t MediaPlayer::prepareAsync_l()
216{
217 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
218 mPlayer->setAudioStreamType(mStreamType);
219 mCurrentState = MEDIA_PLAYER_PREPARING;
220 return mPlayer->prepareAsync();
221 }
222 LOGE("prepareAsync called in state %d", mCurrentState);
223 return INVALID_OPERATION;
224}
225
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700226// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
227// one defined in the Android framework and one provided by the implementation
228// that generated the error. The sync version of prepare returns only 1 error
229// code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230status_t MediaPlayer::prepare()
231{
232 LOGV("prepare");
233 Mutex::Autolock _l(mLock);
Jason Samsebb020a2009-03-24 18:45:22 -0700234 mLockThreadId = getThreadId();
235 if (mPrepareSync) {
236 mLockThreadId = 0;
237 return -EALREADY;
238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 mPrepareSync = true;
240 status_t ret = prepareAsync_l();
Jason Samsebb020a2009-03-24 18:45:22 -0700241 if (ret != NO_ERROR) {
242 mLockThreadId = 0;
243 return ret;
244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 if (mPrepareSync) {
247 mSignal.wait(mLock); // wait for prepare done
248 mPrepareSync = false;
249 }
250 LOGV("prepare complete - status=%d", mPrepareStatus);
Jason Samsebb020a2009-03-24 18:45:22 -0700251 mLockThreadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 return mPrepareStatus;
253}
254
255status_t MediaPlayer::prepareAsync()
256{
257 LOGV("prepareAsync");
258 Mutex::Autolock _l(mLock);
259 return prepareAsync_l();
260}
261
262status_t MediaPlayer::start()
263{
264 LOGV("start");
265 Mutex::Autolock _l(mLock);
266 if (mCurrentState & MEDIA_PLAYER_STARTED)
267 return NO_ERROR;
268 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
269 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
270 mPlayer->setLooping(mLoop);
271 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -0700272 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 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 Nelissen465faa92010-02-26 13:16:23 -0800311 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project9066cfe2009-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 Hubereffd8d52009-03-24 20:48:51 -0700427 mLockThreadId = getThreadId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 Mutex::Autolock _l(mLock);
Andreas Hubereffd8d52009-03-24 20:48:51 -0700429 status_t result = seekTo_l(msec);
430 mLockThreadId = 0;
431
432 return result;
The Android Open Source Project9066cfe2009-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 }
James Dongc5bba6c2010-11-18 12:23:58 -0800450 // setDataSource has to be called again to create a
451 // new mediaplayer.
452 mPlayer = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 return ret;
454 }
455 clear_l();
456 return NO_ERROR;
457}
458
459status_t MediaPlayer::setAudioStreamType(int type)
460{
461 LOGV("MediaPlayer::setAudioStreamType");
462 Mutex::Autolock _l(mLock);
463 if (mStreamType == type) return NO_ERROR;
464 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
465 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
466 // Can't change the stream type after prepare
467 LOGE("setAudioStream called in state %d", mCurrentState);
468 return INVALID_OPERATION;
469 }
470 // cache
471 mStreamType = type;
472 return OK;
473}
474
475status_t MediaPlayer::setLooping(int loop)
476{
477 LOGV("MediaPlayer::setLooping");
478 Mutex::Autolock _l(mLock);
479 mLoop = (loop != 0);
480 if (mPlayer != 0) {
481 return mPlayer->setLooping(loop);
482 }
483 return OK;
484}
485
486bool MediaPlayer::isLooping() {
487 LOGV("isLooping");
488 Mutex::Autolock _l(mLock);
489 if (mPlayer != 0) {
490 return mLoop;
491 }
492 LOGV("isLooping: no active player");
493 return false;
494}
495
496status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
497{
498 LOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
499 Mutex::Autolock _l(mLock);
500 mLeftVolume = leftVolume;
501 mRightVolume = rightVolume;
502 if (mPlayer != 0) {
503 return mPlayer->setVolume(leftVolume, rightVolume);
504 }
505 return OK;
506}
507
Eric Laurent619346f2010-06-21 09:27:30 -0700508status_t MediaPlayer::setAudioSessionId(int sessionId)
509{
510 LOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
511 Mutex::Autolock _l(mLock);
512 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
513 LOGE("setAudioSessionId called in state %d", mCurrentState);
514 return INVALID_OPERATION;
515 }
516 if (sessionId < 0) {
517 return BAD_VALUE;
518 }
519 mAudioSessionId = sessionId;
520 return NO_ERROR;
521}
522
523int MediaPlayer::getAudioSessionId()
524{
525 Mutex::Autolock _l(mLock);
526 return mAudioSessionId;
527}
528
Eric Laurent7070b362010-07-16 07:43:46 -0700529status_t MediaPlayer::setAuxEffectSendLevel(float level)
530{
531 LOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
532 Mutex::Autolock _l(mLock);
533 mSendLevel = level;
534 if (mPlayer != 0) {
535 return mPlayer->setAuxEffectSendLevel(level);
536 }
537 return OK;
538}
539
540status_t MediaPlayer::attachAuxEffect(int effectId)
541{
542 LOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
543 Mutex::Autolock _l(mLock);
544 if (mPlayer == 0 ||
545 (mCurrentState & MEDIA_PLAYER_IDLE) ||
546 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
547 LOGE("attachAuxEffect called in state %d", mCurrentState);
548 return INVALID_OPERATION;
549 }
550
551 return mPlayer->attachAuxEffect(effectId);
552}
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554void MediaPlayer::notify(int msg, int ext1, int ext2)
555{
556 LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
557 bool send = true;
Jason Samsebb020a2009-03-24 18:45:22 -0700558 bool locked = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
560 // TODO: In the future, we might be on the same thread if the app is
561 // running in the same process as the media server. In that case,
562 // this will deadlock.
Nicolas Catania32f82772009-06-11 16:33:49 -0700563 //
Jason Samsebb020a2009-03-24 18:45:22 -0700564 // The threadId hack below works around this for the care of prepare
Andreas Hubereffd8d52009-03-24 20:48:51 -0700565 // and seekTo within the same process.
566 // FIXME: Remember, this is a hack, it's not even a hack that is applied
567 // consistently for all use-cases, this needs to be revisited.
Jason Samsebb020a2009-03-24 18:45:22 -0700568 if (mLockThreadId != getThreadId()) {
569 mLock.lock();
570 locked = true;
Nicolas Catania32f82772009-06-11 16:33:49 -0700571 }
Jason Samsebb020a2009-03-24 18:45:22 -0700572
Eric Laurent1d53b2a2010-08-03 07:49:49 -0700573 // Allows calls from JNI in idle state to notify errors
574 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Samsebb020a2009-03-24 18:45:22 -0700576 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 return;
578 }
579
580 switch (msg) {
581 case MEDIA_NOP: // interface test message
582 break;
583 case MEDIA_PREPARED:
584 LOGV("prepared");
585 mCurrentState = MEDIA_PLAYER_PREPARED;
586 if (mPrepareSync) {
587 LOGV("signal application thread");
588 mPrepareSync = false;
589 mPrepareStatus = NO_ERROR;
590 mSignal.signal();
591 }
592 break;
593 case MEDIA_PLAYBACK_COMPLETE:
594 LOGV("playback complete");
Marco Nelissen3c473ea2010-09-17 15:04:01 -0700595 if (mCurrentState == MEDIA_PLAYER_IDLE) {
596 LOGE("playback complete in idle state");
597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 if (!mLoop) {
599 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
600 }
601 break;
602 case MEDIA_ERROR:
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700603 // Always log errors.
604 // ext1: Media framework error code.
605 // ext2: Implementation dependant error code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 LOGE("error (%d, %d)", ext1, ext2);
607 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
608 if (mPrepareSync)
609 {
610 LOGV("signal application thread");
611 mPrepareSync = false;
612 mPrepareStatus = ext1;
613 mSignal.signal();
614 send = false;
615 }
616 break;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700617 case MEDIA_INFO:
618 // ext1: Media framework error code.
619 // ext2: Implementation dependant error code.
Andreas Huber52c78322011-01-11 15:05:28 -0800620 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
621 LOGW("info/warning (%d, %d)", ext1, ext2);
622 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700623 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 case MEDIA_SEEK_COMPLETE:
625 LOGV("Received seek complete");
626 if (mSeekPosition != mCurrentPosition) {
627 LOGV("Executing queued seekTo(%d)", mSeekPosition);
628 mSeekPosition = -1;
629 seekTo_l(mCurrentPosition);
630 }
631 else {
632 LOGV("All seeks complete - return to regularly scheduled program");
633 mCurrentPosition = mSeekPosition = -1;
634 }
635 break;
636 case MEDIA_BUFFERING_UPDATE:
637 LOGV("buffering %d", ext1);
638 break;
639 case MEDIA_SET_VIDEO_SIZE:
640 LOGV("New video size %d x %d", ext1, ext2);
641 mVideoWidth = ext1;
642 mVideoHeight = ext2;
643 break;
644 default:
645 LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
646 break;
647 }
648
649 sp<MediaPlayerListener> listener = mListener;
Jason Samsebb020a2009-03-24 18:45:22 -0700650 if (locked) mLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
652 // this prevents re-entrant calls into client code
653 if ((listener != 0) && send) {
654 Mutex::Autolock _l(mNotifyLock);
655 LOGV("callback application");
656 listener->notify(msg, ext1, ext2);
657 LOGV("back from callback");
658 }
659}
660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
662{
663 LOGV("decode(%s)", url);
664 sp<IMemory> p;
665 const sp<IMediaPlayerService>& service = getMediaPlayerService();
666 if (service != 0) {
James Dong34bbc222010-01-15 18:13:58 -0800667 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 } else {
669 LOGE("Unable to locate media service");
670 }
671 return p;
672
673}
674
James Dong34bbc222010-01-15 18:13:58 -0800675void MediaPlayer::died()
676{
677 LOGV("died");
678 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
679}
680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
682{
683 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
684 sp<IMemory> p;
685 const sp<IMediaPlayerService>& service = getMediaPlayerService();
686 if (service != 0) {
James Dong34bbc222010-01-15 18:13:58 -0800687 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 } else {
689 LOGE("Unable to locate media service");
690 }
691 return p;
692
693}
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695}; // namespace android