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