mediaplayer.cpp revision df712ea86e6350f7005a02ab0e1c60c28a343ed0
1/*
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
27#include <binder/IServiceManager.h>
28#include <binder/IPCThreadState.h>
29
30#include <gui/SurfaceTextureClient.h>
31
32#include <media/mediaplayer.h>
33#include <media/AudioSystem.h>
34
35#include <binder/MemoryBase.h>
36
37#include <utils/KeyedVector.h>
38#include <utils/String8.h>
39
40#include <system/audio.h>
41#include <system/window.h>
42
43namespace android {
44
45MediaPlayer::MediaPlayer()
46{
47    ALOGV("constructor");
48    mListener = NULL;
49    mCookie = NULL;
50    mDuration = -1;
51    mStreamType = AUDIO_STREAM_MUSIC;
52    mCurrentPosition = -1;
53    mSeekPosition = -1;
54    mCurrentState = MEDIA_PLAYER_IDLE;
55    mPrepareSync = false;
56    mPrepareStatus = NO_ERROR;
57    mLoop = false;
58    mLeftVolume = mRightVolume = 1.0;
59    mVideoWidth = mVideoHeight = 0;
60    mLockThreadId = 0;
61    mAudioSessionId = AudioSystem::newAudioSessionId();
62    AudioSystem::acquireAudioSessionId(mAudioSessionId);
63    mSendLevel = 0;
64}
65
66MediaPlayer::~MediaPlayer()
67{
68    ALOGV("destructor");
69    AudioSystem::releaseAudioSessionId(mAudioSessionId);
70    disconnect();
71    IPCThreadState::self()->flushCommands();
72}
73
74void MediaPlayer::disconnect()
75{
76    ALOGV("disconnect");
77    sp<IMediaPlayer> p;
78    {
79        Mutex::Autolock _l(mLock);
80        p = mPlayer;
81        mPlayer.clear();
82    }
83
84    if (p != 0) {
85        p->disconnect();
86    }
87}
88
89// always call with lock held
90void MediaPlayer::clear_l()
91{
92    mDuration = -1;
93    mCurrentPosition = -1;
94    mSeekPosition = -1;
95    mVideoWidth = mVideoHeight = 0;
96}
97
98status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
99{
100    ALOGV("setListener");
101    Mutex::Autolock _l(mLock);
102    mListener = listener;
103    return NO_ERROR;
104}
105
106
107status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
108{
109    status_t err = UNKNOWN_ERROR;
110    sp<IMediaPlayer> p;
111    { // scope for the lock
112        Mutex::Autolock _l(mLock);
113
114        if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
115                (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
116            ALOGE("attachNewPlayer called in state %d", mCurrentState);
117            return INVALID_OPERATION;
118        }
119
120        clear_l();
121        p = mPlayer;
122        mPlayer = player;
123        if (player != 0) {
124            mCurrentState = MEDIA_PLAYER_INITIALIZED;
125            err = NO_ERROR;
126        } else {
127            ALOGE("Unable to to create media player");
128        }
129    }
130
131    if (p != 0) {
132        p->disconnect();
133    }
134
135    return err;
136}
137
138status_t MediaPlayer::setDataSource(
139        const char *url, const KeyedVector<String8, String8> *headers)
140{
141    ALOGV("setDataSource(%s)", url);
142    status_t err = BAD_VALUE;
143    if (url != NULL) {
144        const sp<IMediaPlayerService>& service(getMediaPlayerService());
145        if (service != 0) {
146            sp<IMediaPlayer> player(service->create(getpid(), this, mAudioSessionId));
147            if (NO_ERROR != player->setDataSource(url, headers)) {
148                player.clear();
149            }
150            err = attachNewPlayer(player);
151        }
152    }
153    return err;
154}
155
156status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
157{
158    ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
159    status_t err = UNKNOWN_ERROR;
160    const sp<IMediaPlayerService>& service(getMediaPlayerService());
161    if (service != 0) {
162        sp<IMediaPlayer> player(service->create(getpid(), this, mAudioSessionId));
163        if (NO_ERROR != player->setDataSource(fd, offset, length)) {
164            player.clear();
165        }
166        err = attachNewPlayer(player);
167    }
168    return err;
169}
170
171status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
172{
173    ALOGV("setDataSource");
174    status_t err = UNKNOWN_ERROR;
175    const sp<IMediaPlayerService>& service(getMediaPlayerService());
176    if (service != 0) {
177        sp<IMediaPlayer> player(service->create(getpid(), this, mAudioSessionId));
178        if (NO_ERROR != player->setDataSource(source)) {
179            player.clear();
180        }
181        err = attachNewPlayer(player);
182    }
183    return err;
184}
185
186status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
187{
188    Mutex::Autolock _l(mLock);
189    const bool hasBeenInitialized =
190            (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
191            ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
192    if ((mPlayer != NULL) && hasBeenInitialized) {
193         ALOGV("invoke %d", request.dataSize());
194         return  mPlayer->invoke(request, reply);
195    }
196    ALOGE("invoke failed: wrong state %X", mCurrentState);
197    return INVALID_OPERATION;
198}
199
200status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
201{
202    ALOGD("setMetadataFilter");
203    Mutex::Autolock lock(mLock);
204    if (mPlayer == NULL) {
205        return NO_INIT;
206    }
207    return mPlayer->setMetadataFilter(filter);
208}
209
210status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
211{
212    ALOGD("getMetadata");
213    Mutex::Autolock lock(mLock);
214    if (mPlayer == NULL) {
215        return NO_INIT;
216    }
217    return mPlayer->getMetadata(update_only, apply_filter, metadata);
218}
219
220status_t MediaPlayer::setVideoSurfaceTexture(
221        const sp<ISurfaceTexture>& surfaceTexture)
222{
223    ALOGV("setVideoSurfaceTexture");
224    Mutex::Autolock _l(mLock);
225    if (mPlayer == 0) return NO_INIT;
226    return mPlayer->setVideoSurfaceTexture(surfaceTexture);
227}
228
229// must call with lock held
230status_t MediaPlayer::prepareAsync_l()
231{
232    if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
233        mPlayer->setAudioStreamType(mStreamType);
234        mCurrentState = MEDIA_PLAYER_PREPARING;
235        return mPlayer->prepareAsync();
236    }
237    ALOGE("prepareAsync called in state %d", mCurrentState);
238    return INVALID_OPERATION;
239}
240
241// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
242// one defined in the Android framework and one provided by the implementation
243// that generated the error. The sync version of prepare returns only 1 error
244// code.
245status_t MediaPlayer::prepare()
246{
247    ALOGV("prepare");
248    Mutex::Autolock _l(mLock);
249    mLockThreadId = getThreadId();
250    if (mPrepareSync) {
251        mLockThreadId = 0;
252        return -EALREADY;
253    }
254    mPrepareSync = true;
255    status_t ret = prepareAsync_l();
256    if (ret != NO_ERROR) {
257        mLockThreadId = 0;
258        return ret;
259    }
260
261    if (mPrepareSync) {
262        mSignal.wait(mLock);  // wait for prepare done
263        mPrepareSync = false;
264    }
265    ALOGV("prepare complete - status=%d", mPrepareStatus);
266    mLockThreadId = 0;
267    return mPrepareStatus;
268}
269
270status_t MediaPlayer::prepareAsync()
271{
272    ALOGV("prepareAsync");
273    Mutex::Autolock _l(mLock);
274    return prepareAsync_l();
275}
276
277status_t MediaPlayer::start()
278{
279    ALOGV("start");
280    Mutex::Autolock _l(mLock);
281    if (mCurrentState & MEDIA_PLAYER_STARTED)
282        return NO_ERROR;
283    if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
284                    MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
285        mPlayer->setLooping(mLoop);
286        mPlayer->setVolume(mLeftVolume, mRightVolume);
287        mPlayer->setAuxEffectSendLevel(mSendLevel);
288        mCurrentState = MEDIA_PLAYER_STARTED;
289        status_t ret = mPlayer->start();
290        if (ret != NO_ERROR) {
291            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
292        } else {
293            if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
294                ALOGV("playback completed immediately following start()");
295            }
296        }
297        return ret;
298    }
299    ALOGE("start called in state %d", mCurrentState);
300    return INVALID_OPERATION;
301}
302
303status_t MediaPlayer::stop()
304{
305    ALOGV("stop");
306    Mutex::Autolock _l(mLock);
307    if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
308    if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
309                    MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
310        status_t ret = mPlayer->stop();
311        if (ret != NO_ERROR) {
312            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
313        } else {
314            mCurrentState = MEDIA_PLAYER_STOPPED;
315        }
316        return ret;
317    }
318    ALOGE("stop called in state %d", mCurrentState);
319    return INVALID_OPERATION;
320}
321
322status_t MediaPlayer::pause()
323{
324    ALOGV("pause");
325    Mutex::Autolock _l(mLock);
326    if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
327        return NO_ERROR;
328    if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
329        status_t ret = mPlayer->pause();
330        if (ret != NO_ERROR) {
331            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
332        } else {
333            mCurrentState = MEDIA_PLAYER_PAUSED;
334        }
335        return ret;
336    }
337    ALOGE("pause called in state %d", mCurrentState);
338    return INVALID_OPERATION;
339}
340
341bool MediaPlayer::isPlaying()
342{
343    Mutex::Autolock _l(mLock);
344    if (mPlayer != 0) {
345        bool temp = false;
346        mPlayer->isPlaying(&temp);
347        ALOGV("isPlaying: %d", temp);
348        if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
349            ALOGE("internal/external state mismatch corrected");
350            mCurrentState = MEDIA_PLAYER_PAUSED;
351        }
352        return temp;
353    }
354    ALOGV("isPlaying: no active player");
355    return false;
356}
357
358status_t MediaPlayer::getVideoWidth(int *w)
359{
360    ALOGV("getVideoWidth");
361    Mutex::Autolock _l(mLock);
362    if (mPlayer == 0) return INVALID_OPERATION;
363    *w = mVideoWidth;
364    return NO_ERROR;
365}
366
367status_t MediaPlayer::getVideoHeight(int *h)
368{
369    ALOGV("getVideoHeight");
370    Mutex::Autolock _l(mLock);
371    if (mPlayer == 0) return INVALID_OPERATION;
372    *h = mVideoHeight;
373    return NO_ERROR;
374}
375
376status_t MediaPlayer::getCurrentPosition(int *msec)
377{
378    ALOGV("getCurrentPosition");
379    Mutex::Autolock _l(mLock);
380    if (mPlayer != 0) {
381        if (mCurrentPosition >= 0) {
382            ALOGV("Using cached seek position: %d", mCurrentPosition);
383            *msec = mCurrentPosition;
384            return NO_ERROR;
385        }
386        return mPlayer->getCurrentPosition(msec);
387    }
388    return INVALID_OPERATION;
389}
390
391status_t MediaPlayer::getDuration_l(int *msec)
392{
393    ALOGV("getDuration");
394    bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
395    if (mPlayer != 0 && isValidState) {
396        status_t ret = NO_ERROR;
397        if (mDuration <= 0)
398            ret = mPlayer->getDuration(&mDuration);
399        if (msec)
400            *msec = mDuration;
401        return ret;
402    }
403    ALOGE("Attempt to call getDuration without a valid mediaplayer");
404    return INVALID_OPERATION;
405}
406
407status_t MediaPlayer::getDuration(int *msec)
408{
409    Mutex::Autolock _l(mLock);
410    return getDuration_l(msec);
411}
412
413status_t MediaPlayer::seekTo_l(int msec)
414{
415    ALOGV("seekTo %d", msec);
416    if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED |  MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
417        if ( msec < 0 ) {
418            ALOGW("Attempt to seek to invalid position: %d", msec);
419            msec = 0;
420        } else if ((mDuration > 0) && (msec > mDuration)) {
421            ALOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration);
422            msec = mDuration;
423        }
424        // cache duration
425        mCurrentPosition = msec;
426        if (mSeekPosition < 0) {
427            getDuration_l(NULL);
428            mSeekPosition = msec;
429            return mPlayer->seekTo(msec);
430        }
431        else {
432            ALOGV("Seek in progress - queue up seekTo[%d]", msec);
433            return NO_ERROR;
434        }
435    }
436    ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
437    return INVALID_OPERATION;
438}
439
440status_t MediaPlayer::seekTo(int msec)
441{
442    mLockThreadId = getThreadId();
443    Mutex::Autolock _l(mLock);
444    status_t result = seekTo_l(msec);
445    mLockThreadId = 0;
446
447    return result;
448}
449
450status_t MediaPlayer::reset_l()
451{
452    mLoop = false;
453    if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
454    mPrepareSync = false;
455    if (mPlayer != 0) {
456        status_t ret = mPlayer->reset();
457        if (ret != NO_ERROR) {
458            ALOGE("reset() failed with return code (%d)", ret);
459            mCurrentState = MEDIA_PLAYER_STATE_ERROR;
460        } else {
461            mCurrentState = MEDIA_PLAYER_IDLE;
462        }
463        // setDataSource has to be called again to create a
464        // new mediaplayer.
465        mPlayer = 0;
466        return ret;
467    }
468    clear_l();
469    return NO_ERROR;
470}
471
472status_t MediaPlayer::reset()
473{
474    ALOGV("reset");
475    Mutex::Autolock _l(mLock);
476    return reset_l();
477}
478
479status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
480{
481    ALOGV("MediaPlayer::setAudioStreamType");
482    Mutex::Autolock _l(mLock);
483    if (mStreamType == type) return NO_ERROR;
484    if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
485                MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
486        // Can't change the stream type after prepare
487        ALOGE("setAudioStream called in state %d", mCurrentState);
488        return INVALID_OPERATION;
489    }
490    // cache
491    mStreamType = type;
492    return OK;
493}
494
495status_t MediaPlayer::setLooping(int loop)
496{
497    ALOGV("MediaPlayer::setLooping");
498    Mutex::Autolock _l(mLock);
499    mLoop = (loop != 0);
500    if (mPlayer != 0) {
501        return mPlayer->setLooping(loop);
502    }
503    return OK;
504}
505
506bool MediaPlayer::isLooping() {
507    ALOGV("isLooping");
508    Mutex::Autolock _l(mLock);
509    if (mPlayer != 0) {
510        return mLoop;
511    }
512    ALOGV("isLooping: no active player");
513    return false;
514}
515
516status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
517{
518    ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
519    Mutex::Autolock _l(mLock);
520    mLeftVolume = leftVolume;
521    mRightVolume = rightVolume;
522    if (mPlayer != 0) {
523        return mPlayer->setVolume(leftVolume, rightVolume);
524    }
525    return OK;
526}
527
528status_t MediaPlayer::setAudioSessionId(int sessionId)
529{
530    ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
531    Mutex::Autolock _l(mLock);
532    if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
533        ALOGE("setAudioSessionId called in state %d", mCurrentState);
534        return INVALID_OPERATION;
535    }
536    if (sessionId < 0) {
537        return BAD_VALUE;
538    }
539    if (sessionId != mAudioSessionId) {
540      AudioSystem::releaseAudioSessionId(mAudioSessionId);
541      AudioSystem::acquireAudioSessionId(sessionId);
542      mAudioSessionId = sessionId;
543    }
544    return NO_ERROR;
545}
546
547int MediaPlayer::getAudioSessionId()
548{
549    Mutex::Autolock _l(mLock);
550    return mAudioSessionId;
551}
552
553status_t MediaPlayer::setAuxEffectSendLevel(float level)
554{
555    ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
556    Mutex::Autolock _l(mLock);
557    mSendLevel = level;
558    if (mPlayer != 0) {
559        return mPlayer->setAuxEffectSendLevel(level);
560    }
561    return OK;
562}
563
564status_t MediaPlayer::attachAuxEffect(int effectId)
565{
566    ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
567    Mutex::Autolock _l(mLock);
568    if (mPlayer == 0 ||
569        (mCurrentState & MEDIA_PLAYER_IDLE) ||
570        (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
571        ALOGE("attachAuxEffect called in state %d", mCurrentState);
572        return INVALID_OPERATION;
573    }
574
575    return mPlayer->attachAuxEffect(effectId);
576}
577
578status_t MediaPlayer::setParameter(int key, const Parcel& request)
579{
580    ALOGV("MediaPlayer::setParameter(%d)", key);
581    Mutex::Autolock _l(mLock);
582    if (mPlayer != NULL) {
583        return  mPlayer->setParameter(key, request);
584    }
585    ALOGV("setParameter: no active player");
586    return INVALID_OPERATION;
587}
588
589status_t MediaPlayer::getParameter(int key, Parcel *reply)
590{
591    ALOGV("MediaPlayer::getParameter(%d)", key);
592    Mutex::Autolock _l(mLock);
593    if (mPlayer != NULL) {
594         return  mPlayer->getParameter(key, reply);
595    }
596    ALOGV("getParameter: no active player");
597    return INVALID_OPERATION;
598}
599
600void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
601{
602    ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
603    bool send = true;
604    bool locked = false;
605
606    // TODO: In the future, we might be on the same thread if the app is
607    // running in the same process as the media server. In that case,
608    // this will deadlock.
609    //
610    // The threadId hack below works around this for the care of prepare
611    // and seekTo within the same process.
612    // FIXME: Remember, this is a hack, it's not even a hack that is applied
613    // consistently for all use-cases, this needs to be revisited.
614     if (mLockThreadId != getThreadId()) {
615        mLock.lock();
616        locked = true;
617    }
618
619    // Allows calls from JNI in idle state to notify errors
620    if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
621        ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
622        if (locked) mLock.unlock();   // release the lock when done.
623        return;
624    }
625
626    switch (msg) {
627    case MEDIA_NOP: // interface test message
628        break;
629    case MEDIA_PREPARED:
630        ALOGV("prepared");
631        mCurrentState = MEDIA_PLAYER_PREPARED;
632        if (mPrepareSync) {
633            ALOGV("signal application thread");
634            mPrepareSync = false;
635            mPrepareStatus = NO_ERROR;
636            mSignal.signal();
637        }
638        break;
639    case MEDIA_PLAYBACK_COMPLETE:
640        ALOGV("playback complete");
641        if (mCurrentState == MEDIA_PLAYER_IDLE) {
642            ALOGE("playback complete in idle state");
643        }
644        if (!mLoop) {
645            mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
646        }
647        break;
648    case MEDIA_ERROR:
649        // Always log errors.
650        // ext1: Media framework error code.
651        // ext2: Implementation dependant error code.
652        ALOGE("error (%d, %d)", ext1, ext2);
653        mCurrentState = MEDIA_PLAYER_STATE_ERROR;
654        if (mPrepareSync)
655        {
656            ALOGV("signal application thread");
657            mPrepareSync = false;
658            mPrepareStatus = ext1;
659            mSignal.signal();
660            send = false;
661        }
662        break;
663    case MEDIA_INFO:
664        // ext1: Media framework error code.
665        // ext2: Implementation dependant error code.
666        if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
667            ALOGW("info/warning (%d, %d)", ext1, ext2);
668        }
669        break;
670    case MEDIA_SEEK_COMPLETE:
671        ALOGV("Received seek complete");
672        if (mSeekPosition != mCurrentPosition) {
673            ALOGV("Executing queued seekTo(%d)", mSeekPosition);
674            mSeekPosition = -1;
675            seekTo_l(mCurrentPosition);
676        }
677        else {
678            ALOGV("All seeks complete - return to regularly scheduled program");
679            mCurrentPosition = mSeekPosition = -1;
680        }
681        break;
682    case MEDIA_BUFFERING_UPDATE:
683        ALOGV("buffering %d", ext1);
684        break;
685    case MEDIA_SET_VIDEO_SIZE:
686        ALOGV("New video size %d x %d", ext1, ext2);
687        mVideoWidth = ext1;
688        mVideoHeight = ext2;
689        break;
690    case MEDIA_TIMED_TEXT:
691        ALOGV("Received timed text message");
692        break;
693    default:
694        ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
695        break;
696    }
697
698    sp<MediaPlayerListener> listener = mListener;
699    if (locked) mLock.unlock();
700
701    // this prevents re-entrant calls into client code
702    if ((listener != 0) && send) {
703        Mutex::Autolock _l(mNotifyLock);
704        ALOGV("callback application");
705        listener->notify(msg, ext1, ext2, obj);
706        ALOGV("back from callback");
707    }
708}
709
710/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
711{
712    ALOGV("decode(%s)", url);
713    sp<IMemory> p;
714    const sp<IMediaPlayerService>& service = getMediaPlayerService();
715    if (service != 0) {
716        p = service->decode(url, pSampleRate, pNumChannels, pFormat);
717    } else {
718        ALOGE("Unable to locate media service");
719    }
720    return p;
721
722}
723
724void MediaPlayer::died()
725{
726    ALOGV("died");
727    notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
728}
729
730/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
731{
732    ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
733    sp<IMemory> p;
734    const sp<IMediaPlayerService>& service = getMediaPlayerService();
735    if (service != 0) {
736        p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
737    } else {
738        ALOGE("Unable to locate media service");
739    }
740    return p;
741
742}
743
744}; // namespace android
745