blob: 9daa80fd6d099b96857e1341d2bb2f3f949553f0 [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
Dima Zavin24fc2fb2011-04-19 22:30:36 -070040#include <hardware/audio.h>
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042namespace android {
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044MediaPlayer::MediaPlayer()
45{
46 LOGV("constructor");
47 mListener = NULL;
48 mCookie = NULL;
49 mDuration = -1;
Dima Zavin24fc2fb2011-04-19 22:30:36 -070050 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 mCurrentPosition = -1;
52 mSeekPosition = -1;
53 mCurrentState = MEDIA_PLAYER_IDLE;
54 mPrepareSync = false;
55 mPrepareStatus = NO_ERROR;
56 mLoop = false;
57 mLeftVolume = mRightVolume = 1.0;
58 mVideoWidth = mVideoHeight = 0;
Jason Samsebb020a2009-03-24 18:45:22 -070059 mLockThreadId = 0;
Eric Laurent619346f2010-06-21 09:27:30 -070060 mAudioSessionId = AudioSystem::newAudioSessionId();
Eric Laurentb3bdf3f2010-10-07 18:23:03 -070061 mSendLevel = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062}
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064MediaPlayer::~MediaPlayer()
65{
66 LOGV("destructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 disconnect();
68 IPCThreadState::self()->flushCommands();
69}
70
71void MediaPlayer::disconnect()
72{
73 LOGV("disconnect");
74 sp<IMediaPlayer> p;
75 {
76 Mutex::Autolock _l(mLock);
77 p = mPlayer;
78 mPlayer.clear();
79 }
80
81 if (p != 0) {
82 p->disconnect();
83 }
84}
85
86// always call with lock held
87void MediaPlayer::clear_l()
88{
89 mDuration = -1;
90 mCurrentPosition = -1;
91 mSeekPosition = -1;
92 mVideoWidth = mVideoHeight = 0;
93}
94
95status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
96{
97 LOGV("setListener");
98 Mutex::Autolock _l(mLock);
99 mListener = listener;
100 return NO_ERROR;
101}
102
103
104status_t MediaPlayer::setDataSource(const sp<IMediaPlayer>& player)
105{
106 status_t err = UNKNOWN_ERROR;
107 sp<IMediaPlayer> p;
108 { // scope for the lock
109 Mutex::Autolock _l(mLock);
110
Marco Nelissene44b41b2010-03-10 10:53:16 -0800111 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
112 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 LOGE("setDataSource called in state %d", mCurrentState);
114 return INVALID_OPERATION;
115 }
116
117 clear_l();
118 p = mPlayer;
119 mPlayer = player;
120 if (player != 0) {
121 mCurrentState = MEDIA_PLAYER_INITIALIZED;
122 err = NO_ERROR;
123 } else {
124 LOGE("Unable to to create media player");
125 }
126 }
127
128 if (p != 0) {
129 p->disconnect();
130 }
131
132 return err;
133}
134
Andreas Huber25643002010-01-28 11:19:57 -0800135status_t MediaPlayer::setDataSource(
136 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137{
138 LOGV("setDataSource(%s)", url);
139 status_t err = BAD_VALUE;
140 if (url != NULL) {
141 const sp<IMediaPlayerService>& service(getMediaPlayerService());
142 if (service != 0) {
Andreas Huber25643002010-01-28 11:19:57 -0800143 sp<IMediaPlayer> player(
Eric Laurent619346f2010-06-21 09:27:30 -0700144 service->create(getpid(), this, url, headers, mAudioSessionId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 err = setDataSource(player);
146 }
147 }
148 return err;
149}
150
151status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
152{
153 LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
154 status_t err = UNKNOWN_ERROR;
155 const sp<IMediaPlayerService>& service(getMediaPlayerService());
156 if (service != 0) {
Eric Laurent619346f2010-06-21 09:27:30 -0700157 sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length, mAudioSessionId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 err = setDataSource(player);
159 }
160 return err;
161}
162
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700163status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
164{
165 Mutex::Autolock _l(mLock);
Nicolas Catania7bd3d7d2010-03-10 10:41:04 -0800166 const bool hasBeenInitialized =
167 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
168 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
169 if ((mPlayer != NULL) && hasBeenInitialized) {
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700170 LOGV("invoke %d", request.dataSize());
171 return mPlayer->invoke(request, reply);
172 }
173 LOGE("invoke failed: wrong state %X", mCurrentState);
174 return INVALID_OPERATION;
175}
176
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700177status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
178{
179 LOGD("setMetadataFilter");
Nicolas Catania5d55c712009-07-09 09:21:33 -0700180 Mutex::Autolock lock(mLock);
181 if (mPlayer == NULL) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700182 return NO_INIT;
183 }
184 return mPlayer->setMetadataFilter(filter);
185}
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700186
Nicolas Catania5d55c712009-07-09 09:21:33 -0700187status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
188{
189 LOGD("getMetadata");
190 Mutex::Autolock lock(mLock);
191 if (mPlayer == NULL) {
192 return NO_INIT;
193 }
194 return mPlayer->getMetadata(update_only, apply_filter, metadata);
195}
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface)
198{
199 LOGV("setVideoSurface");
200 Mutex::Autolock _l(mLock);
201 if (mPlayer == 0) return NO_INIT;
Andreas Hubere3c01832010-08-16 08:49:37 -0700202
Andreas Hubere3c01832010-08-16 08:49:37 -0700203 return mPlayer->setVideoSurface(surface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204}
205
Glenn Kastencc562a32011-02-08 17:26:17 -0800206status_t MediaPlayer::setVideoSurfaceTexture(
207 const sp<ISurfaceTexture>& surfaceTexture)
208{
209 LOGV("setVideoSurfaceTexture");
210 Mutex::Autolock _l(mLock);
211 if (mPlayer == 0) return NO_INIT;
212
213 return mPlayer->setVideoSurfaceTexture(surfaceTexture);
214}
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216// must call with lock held
217status_t MediaPlayer::prepareAsync_l()
218{
219 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
220 mPlayer->setAudioStreamType(mStreamType);
221 mCurrentState = MEDIA_PLAYER_PREPARING;
222 return mPlayer->prepareAsync();
223 }
224 LOGE("prepareAsync called in state %d", mCurrentState);
225 return INVALID_OPERATION;
226}
227
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700228// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
229// one defined in the Android framework and one provided by the implementation
230// that generated the error. The sync version of prepare returns only 1 error
231// code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232status_t MediaPlayer::prepare()
233{
234 LOGV("prepare");
235 Mutex::Autolock _l(mLock);
Jason Samsebb020a2009-03-24 18:45:22 -0700236 mLockThreadId = getThreadId();
237 if (mPrepareSync) {
238 mLockThreadId = 0;
239 return -EALREADY;
240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 mPrepareSync = true;
242 status_t ret = prepareAsync_l();
Jason Samsebb020a2009-03-24 18:45:22 -0700243 if (ret != NO_ERROR) {
244 mLockThreadId = 0;
245 return ret;
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 if (mPrepareSync) {
249 mSignal.wait(mLock); // wait for prepare done
250 mPrepareSync = false;
251 }
252 LOGV("prepare complete - status=%d", mPrepareStatus);
Jason Samsebb020a2009-03-24 18:45:22 -0700253 mLockThreadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 return mPrepareStatus;
255}
256
257status_t MediaPlayer::prepareAsync()
258{
259 LOGV("prepareAsync");
260 Mutex::Autolock _l(mLock);
261 return prepareAsync_l();
262}
263
264status_t MediaPlayer::start()
265{
266 LOGV("start");
267 Mutex::Autolock _l(mLock);
268 if (mCurrentState & MEDIA_PLAYER_STARTED)
269 return NO_ERROR;
270 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
271 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
272 mPlayer->setLooping(mLoop);
273 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -0700274 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mCurrentState = MEDIA_PLAYER_STARTED;
276 status_t ret = mPlayer->start();
277 if (ret != NO_ERROR) {
278 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
279 } else {
280 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
281 LOGV("playback completed immediately following start()");
282 }
283 }
284 return ret;
285 }
286 LOGE("start called in state %d", mCurrentState);
287 return INVALID_OPERATION;
288}
289
290status_t MediaPlayer::stop()
291{
292 LOGV("stop");
293 Mutex::Autolock _l(mLock);
294 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
295 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
296 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
297 status_t ret = mPlayer->stop();
298 if (ret != NO_ERROR) {
299 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
300 } else {
301 mCurrentState = MEDIA_PLAYER_STOPPED;
302 }
303 return ret;
304 }
305 LOGE("stop called in state %d", mCurrentState);
306 return INVALID_OPERATION;
307}
308
309status_t MediaPlayer::pause()
310{
311 LOGV("pause");
312 Mutex::Autolock _l(mLock);
Marco Nelissen465faa92010-02-26 13:16:23 -0800313 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 return NO_ERROR;
315 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
316 status_t ret = mPlayer->pause();
317 if (ret != NO_ERROR) {
318 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
319 } else {
320 mCurrentState = MEDIA_PLAYER_PAUSED;
321 }
322 return ret;
323 }
324 LOGE("pause called in state %d", mCurrentState);
325 return INVALID_OPERATION;
326}
327
328bool MediaPlayer::isPlaying()
329{
330 Mutex::Autolock _l(mLock);
331 if (mPlayer != 0) {
332 bool temp = false;
333 mPlayer->isPlaying(&temp);
334 LOGV("isPlaying: %d", temp);
335 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
336 LOGE("internal/external state mismatch corrected");
337 mCurrentState = MEDIA_PLAYER_PAUSED;
338 }
339 return temp;
340 }
341 LOGV("isPlaying: no active player");
342 return false;
343}
344
345status_t MediaPlayer::getVideoWidth(int *w)
346{
347 LOGV("getVideoWidth");
348 Mutex::Autolock _l(mLock);
349 if (mPlayer == 0) return INVALID_OPERATION;
350 *w = mVideoWidth;
351 return NO_ERROR;
352}
353
354status_t MediaPlayer::getVideoHeight(int *h)
355{
356 LOGV("getVideoHeight");
357 Mutex::Autolock _l(mLock);
358 if (mPlayer == 0) return INVALID_OPERATION;
359 *h = mVideoHeight;
360 return NO_ERROR;
361}
362
363status_t MediaPlayer::getCurrentPosition(int *msec)
364{
365 LOGV("getCurrentPosition");
366 Mutex::Autolock _l(mLock);
367 if (mPlayer != 0) {
368 if (mCurrentPosition >= 0) {
369 LOGV("Using cached seek position: %d", mCurrentPosition);
370 *msec = mCurrentPosition;
371 return NO_ERROR;
372 }
373 return mPlayer->getCurrentPosition(msec);
374 }
375 return INVALID_OPERATION;
376}
377
378status_t MediaPlayer::getDuration_l(int *msec)
379{
380 LOGV("getDuration");
381 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
382 if (mPlayer != 0 && isValidState) {
383 status_t ret = NO_ERROR;
384 if (mDuration <= 0)
385 ret = mPlayer->getDuration(&mDuration);
386 if (msec)
387 *msec = mDuration;
388 return ret;
389 }
390 LOGE("Attempt to call getDuration without a valid mediaplayer");
391 return INVALID_OPERATION;
392}
393
394status_t MediaPlayer::getDuration(int *msec)
395{
396 Mutex::Autolock _l(mLock);
397 return getDuration_l(msec);
398}
399
400status_t MediaPlayer::seekTo_l(int msec)
401{
402 LOGV("seekTo %d", msec);
403 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
404 if ( msec < 0 ) {
405 LOGW("Attempt to seek to invalid position: %d", msec);
406 msec = 0;
407 } else if ((mDuration > 0) && (msec > mDuration)) {
408 LOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
409 msec = mDuration;
410 }
411 // cache duration
412 mCurrentPosition = msec;
413 if (mSeekPosition < 0) {
414 getDuration_l(NULL);
415 mSeekPosition = msec;
416 return mPlayer->seekTo(msec);
417 }
418 else {
419 LOGV("Seek in progress - queue up seekTo[%d]", msec);
420 return NO_ERROR;
421 }
422 }
423 LOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
424 return INVALID_OPERATION;
425}
426
427status_t MediaPlayer::seekTo(int msec)
428{
Andreas Hubereffd8d52009-03-24 20:48:51 -0700429 mLockThreadId = getThreadId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 Mutex::Autolock _l(mLock);
Andreas Hubereffd8d52009-03-24 20:48:51 -0700431 status_t result = seekTo_l(msec);
432 mLockThreadId = 0;
433
434 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435}
436
437status_t MediaPlayer::reset()
438{
439 LOGV("reset");
440 Mutex::Autolock _l(mLock);
441 mLoop = false;
442 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
443 mPrepareSync = false;
444 if (mPlayer != 0) {
445 status_t ret = mPlayer->reset();
446 if (ret != NO_ERROR) {
447 LOGE("reset() failed with return code (%d)", ret);
448 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
449 } else {
450 mCurrentState = MEDIA_PLAYER_IDLE;
451 }
James Dongc5bba6c2010-11-18 12:23:58 -0800452 // setDataSource has to be called again to create a
453 // new mediaplayer.
454 mPlayer = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 return ret;
456 }
457 clear_l();
458 return NO_ERROR;
459}
460
461status_t MediaPlayer::setAudioStreamType(int type)
462{
463 LOGV("MediaPlayer::setAudioStreamType");
464 Mutex::Autolock _l(mLock);
465 if (mStreamType == type) return NO_ERROR;
466 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
467 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
468 // Can't change the stream type after prepare
469 LOGE("setAudioStream called in state %d", mCurrentState);
470 return INVALID_OPERATION;
471 }
472 // cache
473 mStreamType = type;
474 return OK;
475}
476
477status_t MediaPlayer::setLooping(int loop)
478{
479 LOGV("MediaPlayer::setLooping");
480 Mutex::Autolock _l(mLock);
481 mLoop = (loop != 0);
482 if (mPlayer != 0) {
483 return mPlayer->setLooping(loop);
484 }
485 return OK;
486}
487
488bool MediaPlayer::isLooping() {
489 LOGV("isLooping");
490 Mutex::Autolock _l(mLock);
491 if (mPlayer != 0) {
492 return mLoop;
493 }
494 LOGV("isLooping: no active player");
495 return false;
496}
497
498status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
499{
500 LOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
501 Mutex::Autolock _l(mLock);
502 mLeftVolume = leftVolume;
503 mRightVolume = rightVolume;
504 if (mPlayer != 0) {
505 return mPlayer->setVolume(leftVolume, rightVolume);
506 }
507 return OK;
508}
509
Eric Laurent619346f2010-06-21 09:27:30 -0700510status_t MediaPlayer::setAudioSessionId(int sessionId)
511{
512 LOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
513 Mutex::Autolock _l(mLock);
514 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
515 LOGE("setAudioSessionId called in state %d", mCurrentState);
516 return INVALID_OPERATION;
517 }
518 if (sessionId < 0) {
519 return BAD_VALUE;
520 }
521 mAudioSessionId = sessionId;
522 return NO_ERROR;
523}
524
525int MediaPlayer::getAudioSessionId()
526{
527 Mutex::Autolock _l(mLock);
528 return mAudioSessionId;
529}
530
Eric Laurent7070b362010-07-16 07:43:46 -0700531status_t MediaPlayer::setAuxEffectSendLevel(float level)
532{
533 LOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
534 Mutex::Autolock _l(mLock);
535 mSendLevel = level;
536 if (mPlayer != 0) {
537 return mPlayer->setAuxEffectSendLevel(level);
538 }
539 return OK;
540}
541
542status_t MediaPlayer::attachAuxEffect(int effectId)
543{
544 LOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
545 Mutex::Autolock _l(mLock);
546 if (mPlayer == 0 ||
547 (mCurrentState & MEDIA_PLAYER_IDLE) ||
548 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
549 LOGE("attachAuxEffect called in state %d", mCurrentState);
550 return INVALID_OPERATION;
551 }
552
553 return mPlayer->attachAuxEffect(effectId);
554}
555
Gloria Wang162ee492011-04-11 17:23:27 -0700556void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557{
558 LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
559 bool send = true;
Jason Samsebb020a2009-03-24 18:45:22 -0700560 bool locked = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
562 // TODO: In the future, we might be on the same thread if the app is
563 // running in the same process as the media server. In that case,
564 // this will deadlock.
Nicolas Catania32f82772009-06-11 16:33:49 -0700565 //
Jason Samsebb020a2009-03-24 18:45:22 -0700566 // The threadId hack below works around this for the care of prepare
Andreas Hubereffd8d52009-03-24 20:48:51 -0700567 // and seekTo within the same process.
568 // FIXME: Remember, this is a hack, it's not even a hack that is applied
569 // consistently for all use-cases, this needs to be revisited.
Jason Samsebb020a2009-03-24 18:45:22 -0700570 if (mLockThreadId != getThreadId()) {
571 mLock.lock();
572 locked = true;
Nicolas Catania32f82772009-06-11 16:33:49 -0700573 }
Jason Samsebb020a2009-03-24 18:45:22 -0700574
Eric Laurent1d53b2a2010-08-03 07:49:49 -0700575 // Allows calls from JNI in idle state to notify errors
576 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Samsebb020a2009-03-24 18:45:22 -0700578 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 return;
580 }
581
582 switch (msg) {
583 case MEDIA_NOP: // interface test message
584 break;
585 case MEDIA_PREPARED:
586 LOGV("prepared");
587 mCurrentState = MEDIA_PLAYER_PREPARED;
588 if (mPrepareSync) {
589 LOGV("signal application thread");
590 mPrepareSync = false;
591 mPrepareStatus = NO_ERROR;
592 mSignal.signal();
593 }
594 break;
595 case MEDIA_PLAYBACK_COMPLETE:
596 LOGV("playback complete");
Marco Nelissen3c473ea2010-09-17 15:04:01 -0700597 if (mCurrentState == MEDIA_PLAYER_IDLE) {
598 LOGE("playback complete in idle state");
599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 if (!mLoop) {
601 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
602 }
603 break;
604 case MEDIA_ERROR:
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700605 // Always log errors.
606 // ext1: Media framework error code.
607 // ext2: Implementation dependant error code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 LOGE("error (%d, %d)", ext1, ext2);
609 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
610 if (mPrepareSync)
611 {
612 LOGV("signal application thread");
613 mPrepareSync = false;
614 mPrepareStatus = ext1;
615 mSignal.signal();
616 send = false;
617 }
618 break;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700619 case MEDIA_INFO:
620 // ext1: Media framework error code.
621 // ext2: Implementation dependant error code.
Andreas Huber52c78322011-01-11 15:05:28 -0800622 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
623 LOGW("info/warning (%d, %d)", ext1, ext2);
624 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700625 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 case MEDIA_SEEK_COMPLETE:
627 LOGV("Received seek complete");
628 if (mSeekPosition != mCurrentPosition) {
629 LOGV("Executing queued seekTo(%d)", mSeekPosition);
630 mSeekPosition = -1;
631 seekTo_l(mCurrentPosition);
632 }
633 else {
634 LOGV("All seeks complete - return to regularly scheduled program");
635 mCurrentPosition = mSeekPosition = -1;
636 }
637 break;
638 case MEDIA_BUFFERING_UPDATE:
639 LOGV("buffering %d", ext1);
640 break;
641 case MEDIA_SET_VIDEO_SIZE:
642 LOGV("New video size %d x %d", ext1, ext2);
643 mVideoWidth = ext1;
644 mVideoHeight = ext2;
645 break;
Gloria Wang162ee492011-04-11 17:23:27 -0700646 case MEDIA_TIMED_TEXT:
647 LOGV("Received timed text message");
648 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 default:
650 LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
651 break;
652 }
653
654 sp<MediaPlayerListener> listener = mListener;
Jason Samsebb020a2009-03-24 18:45:22 -0700655 if (locked) mLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656
657 // this prevents re-entrant calls into client code
658 if ((listener != 0) && send) {
659 Mutex::Autolock _l(mNotifyLock);
660 LOGV("callback application");
Gloria Wang162ee492011-04-11 17:23:27 -0700661 listener->notify(msg, ext1, ext2, obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 LOGV("back from callback");
663 }
664}
665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
667{
668 LOGV("decode(%s)", url);
669 sp<IMemory> p;
670 const sp<IMediaPlayerService>& service = getMediaPlayerService();
671 if (service != 0) {
James Dong34bbc222010-01-15 18:13:58 -0800672 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 } else {
674 LOGE("Unable to locate media service");
675 }
676 return p;
677
678}
679
James Dong34bbc222010-01-15 18:13:58 -0800680void MediaPlayer::died()
681{
682 LOGV("died");
683 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
684}
685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat)
687{
688 LOGV("decode(%d, %lld, %lld)", fd, offset, length);
689 sp<IMemory> p;
690 const sp<IMediaPlayerService>& service = getMediaPlayerService();
691 if (service != 0) {
James Dong34bbc222010-01-15 18:13:58 -0800692 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 } else {
694 LOGE("Unable to locate media service");
695 }
696 return p;
697
698}
699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700}; // namespace android