AwesomePlayer.cpp revision dac4ee72bac87388a1495e098f39d73168c8078f
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "AwesomePlayer"
19#include <utils/Log.h>
20
21#include <dlfcn.h>
22
23#include "include/AwesomePlayer.h"
24#include "include/Prefetcher.h"
25#include "include/SoftwareRenderer.h"
26
27#include <binder/IPCThreadState.h>
28#include <media/stagefright/AudioPlayer.h>
29#include <media/stagefright/CachingDataSource.h>
30#include <media/stagefright/DataSource.h>
31#include <media/stagefright/FileSource.h>
32#include <media/stagefright/MediaBuffer.h>
33#include <media/stagefright/MediaDefs.h>
34#include <media/stagefright/MediaExtractor.h>
35#include <media/stagefright/MediaDebug.h>
36#include <media/stagefright/MediaSource.h>
37#include <media/stagefright/MetaData.h>
38#include <media/stagefright/OMXCodec.h>
39
40#include <surfaceflinger/ISurface.h>
41
42namespace android {
43
44struct AwesomeEvent : public TimedEventQueue::Event {
45    AwesomeEvent(
46            AwesomePlayer *player,
47            void (AwesomePlayer::*method)())
48        : mPlayer(player),
49          mMethod(method) {
50    }
51
52protected:
53    virtual ~AwesomeEvent() {}
54
55    virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) {
56        (mPlayer->*mMethod)();
57    }
58
59private:
60    AwesomePlayer *mPlayer;
61    void (AwesomePlayer::*mMethod)();
62
63    AwesomeEvent(const AwesomeEvent &);
64    AwesomeEvent &operator=(const AwesomeEvent &);
65};
66
67struct AwesomeRemoteRenderer : public AwesomeRenderer {
68    AwesomeRemoteRenderer(const sp<IOMXRenderer> &target)
69        : mTarget(target) {
70    }
71
72    virtual void render(MediaBuffer *buffer) {
73        void *id;
74        if (buffer->meta_data()->findPointer(kKeyBufferID, &id)) {
75            mTarget->render((IOMX::buffer_id)id);
76        }
77    }
78
79private:
80    sp<IOMXRenderer> mTarget;
81
82    AwesomeRemoteRenderer(const AwesomeRemoteRenderer &);
83    AwesomeRemoteRenderer &operator=(const AwesomeRemoteRenderer &);
84};
85
86struct AwesomeLocalRenderer : public AwesomeRenderer {
87    AwesomeLocalRenderer(
88            bool previewOnly,
89            const char *componentName,
90            OMX_COLOR_FORMATTYPE colorFormat,
91            const sp<ISurface> &surface,
92            size_t displayWidth, size_t displayHeight,
93            size_t decodedWidth, size_t decodedHeight)
94        : mTarget(NULL),
95          mLibHandle(NULL) {
96            init(previewOnly, componentName,
97                 colorFormat, surface, displayWidth,
98                 displayHeight, decodedWidth, decodedHeight);
99    }
100
101    virtual void render(MediaBuffer *buffer) {
102        render((const uint8_t *)buffer->data() + buffer->range_offset(),
103               buffer->range_length());
104    }
105
106    void render(const void *data, size_t size) {
107        mTarget->render(data, size, NULL);
108    }
109
110protected:
111    virtual ~AwesomeLocalRenderer() {
112        delete mTarget;
113        mTarget = NULL;
114
115        if (mLibHandle) {
116            dlclose(mLibHandle);
117            mLibHandle = NULL;
118        }
119    }
120
121private:
122    VideoRenderer *mTarget;
123    void *mLibHandle;
124
125    void init(
126            bool previewOnly,
127            const char *componentName,
128            OMX_COLOR_FORMATTYPE colorFormat,
129            const sp<ISurface> &surface,
130            size_t displayWidth, size_t displayHeight,
131            size_t decodedWidth, size_t decodedHeight);
132
133    AwesomeLocalRenderer(const AwesomeLocalRenderer &);
134    AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);;
135};
136
137void AwesomeLocalRenderer::init(
138        bool previewOnly,
139        const char *componentName,
140        OMX_COLOR_FORMATTYPE colorFormat,
141        const sp<ISurface> &surface,
142        size_t displayWidth, size_t displayHeight,
143        size_t decodedWidth, size_t decodedHeight) {
144    if (!previewOnly) {
145        // We will stick to the vanilla software-color-converting renderer
146        // for "previewOnly" mode, to avoid unneccessarily switching overlays
147        // more often than necessary.
148
149        mLibHandle = dlopen("libstagefrighthw.so", RTLD_NOW);
150
151        if (mLibHandle) {
152            typedef VideoRenderer *(*CreateRendererFunc)(
153                    const sp<ISurface> &surface,
154                    const char *componentName,
155                    OMX_COLOR_FORMATTYPE colorFormat,
156                    size_t displayWidth, size_t displayHeight,
157                    size_t decodedWidth, size_t decodedHeight);
158
159            CreateRendererFunc func =
160                (CreateRendererFunc)dlsym(
161                        mLibHandle,
162                        "_Z14createRendererRKN7android2spINS_8ISurfaceEEEPKc20"
163                        "OMX_COLOR_FORMATTYPEjjjj");
164
165            if (func) {
166                mTarget =
167                    (*func)(surface, componentName, colorFormat,
168                        displayWidth, displayHeight,
169                        decodedWidth, decodedHeight);
170            }
171        }
172    }
173
174    if (mTarget == NULL) {
175        mTarget = new SoftwareRenderer(
176                colorFormat, surface, displayWidth, displayHeight,
177                decodedWidth, decodedHeight);
178    }
179}
180
181AwesomePlayer::AwesomePlayer()
182    : mQueueStarted(false),
183      mTimeSource(NULL),
184      mVideoRendererIsPreview(false),
185      mAudioPlayer(NULL),
186      mFlags(0),
187      mLastVideoBuffer(NULL),
188      mVideoBuffer(NULL),
189      mSuspensionState(NULL) {
190    CHECK_EQ(mClient.connect(), OK);
191
192    DataSource::RegisterDefaultSniffers();
193
194    mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent);
195    mVideoEventPending = false;
196    mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone);
197    mStreamDoneEventPending = false;
198    mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate);
199    mBufferingEventPending = false;
200
201    mCheckAudioStatusEvent = new AwesomeEvent(
202            this, &AwesomePlayer::onCheckAudioStatus);
203
204    mAudioStatusEventPending = false;
205
206    reset();
207}
208
209AwesomePlayer::~AwesomePlayer() {
210    if (mQueueStarted) {
211        mQueue.stop();
212    }
213
214    reset();
215
216    mClient.disconnect();
217}
218
219void AwesomePlayer::cancelPlayerEvents(bool keepBufferingGoing) {
220    mQueue.cancelEvent(mVideoEvent->eventID());
221    mVideoEventPending = false;
222    mQueue.cancelEvent(mStreamDoneEvent->eventID());
223    mStreamDoneEventPending = false;
224    mQueue.cancelEvent(mCheckAudioStatusEvent->eventID());
225    mAudioStatusEventPending = false;
226
227    if (!keepBufferingGoing) {
228        mQueue.cancelEvent(mBufferingEvent->eventID());
229        mBufferingEventPending = false;
230    }
231}
232
233void AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) {
234    Mutex::Autolock autoLock(mLock);
235    mListener = listener;
236}
237
238status_t AwesomePlayer::setDataSource(
239        const char *uri, const KeyedVector<String8, String8> *headers) {
240    Mutex::Autolock autoLock(mLock);
241    return setDataSource_l(uri, headers);
242}
243
244status_t AwesomePlayer::setDataSource_l(
245        const char *uri, const KeyedVector<String8, String8> *headers) {
246    reset_l();
247
248    mUri = uri;
249
250    if (headers) {
251        mUriHeaders = *headers;
252    }
253
254    // The actual work will be done during preparation in the call to
255    // ::finishSetDataSource_l to avoid blocking the calling thread in
256    // setDataSource for any significant time.
257
258    return OK;
259}
260
261status_t AwesomePlayer::setDataSource(
262        int fd, int64_t offset, int64_t length) {
263    Mutex::Autolock autoLock(mLock);
264
265    reset_l();
266
267    sp<DataSource> dataSource = new FileSource(fd, offset, length);
268
269    status_t err = dataSource->initCheck();
270
271    if (err != OK) {
272        return err;
273    }
274
275    mFileSource = dataSource;
276
277    return setDataSource_l(dataSource);
278}
279
280status_t AwesomePlayer::setDataSource_l(
281        const sp<DataSource> &dataSource) {
282    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
283
284    if (extractor == NULL) {
285        return UNKNOWN_ERROR;
286    }
287
288    return setDataSource_l(extractor);
289}
290
291status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {
292    bool haveAudio = false;
293    bool haveVideo = false;
294    for (size_t i = 0; i < extractor->countTracks(); ++i) {
295        sp<MetaData> meta = extractor->getTrackMetaData(i);
296
297        const char *mime;
298        CHECK(meta->findCString(kKeyMIMEType, &mime));
299
300        if (!haveVideo && !strncasecmp(mime, "video/", 6)) {
301            setVideoSource(extractor->getTrack(i));
302            haveVideo = true;
303        } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {
304            setAudioSource(extractor->getTrack(i));
305            haveAudio = true;
306        }
307
308        if (haveAudio && haveVideo) {
309            break;
310        }
311    }
312
313    return !haveAudio && !haveVideo ? UNKNOWN_ERROR : OK;
314}
315
316void AwesomePlayer::reset() {
317    Mutex::Autolock autoLock(mLock);
318    reset_l();
319}
320
321void AwesomePlayer::reset_l() {
322    if (mFlags & PREPARING) {
323        mFlags |= PREPARE_CANCELLED;
324        if (mConnectingDataSource != NULL) {
325            LOGI("interrupting the connection process");
326            mConnectingDataSource->disconnect();
327        }
328    }
329
330    while (mFlags & PREPARING) {
331        mPreparedCondition.wait(mLock);
332    }
333
334    cancelPlayerEvents();
335
336    if (mPrefetcher != NULL) {
337        CHECK_EQ(mPrefetcher->getStrongCount(), 1);
338    }
339    mPrefetcher.clear();
340
341    mAudioTrack.clear();
342    mVideoTrack.clear();
343
344    // Shutdown audio first, so that the respone to the reset request
345    // appears to happen instantaneously as far as the user is concerned
346    // If we did this later, audio would continue playing while we
347    // shutdown the video-related resources and the player appear to
348    // not be as responsive to a reset request.
349    if (mAudioPlayer == NULL && mAudioSource != NULL) {
350        // If we had an audio player, it would have effectively
351        // taken possession of the audio source and stopped it when
352        // _it_ is stopped. Otherwise this is still our responsibility.
353        mAudioSource->stop();
354    }
355    mAudioSource.clear();
356
357    if (mTimeSource != mAudioPlayer) {
358        delete mTimeSource;
359    }
360    mTimeSource = NULL;
361
362    delete mAudioPlayer;
363    mAudioPlayer = NULL;
364
365    mVideoRenderer.clear();
366
367    if (mLastVideoBuffer) {
368        mLastVideoBuffer->release();
369        mLastVideoBuffer = NULL;
370    }
371
372    if (mVideoBuffer) {
373        mVideoBuffer->release();
374        mVideoBuffer = NULL;
375    }
376
377    if (mVideoSource != NULL) {
378        mVideoSource->stop();
379
380        // The following hack is necessary to ensure that the OMX
381        // component is completely released by the time we may try
382        // to instantiate it again.
383        wp<MediaSource> tmp = mVideoSource;
384        mVideoSource.clear();
385        while (tmp.promote() != NULL) {
386            usleep(1000);
387        }
388        IPCThreadState::self()->flushCommands();
389    }
390
391    mDurationUs = -1;
392    mFlags = 0;
393    mVideoWidth = mVideoHeight = -1;
394    mTimeSourceDeltaUs = 0;
395    mVideoTimeUs = 0;
396
397    mSeeking = false;
398    mSeekNotificationSent = false;
399    mSeekTimeUs = 0;
400
401    mUri.setTo("");
402    mUriHeaders.clear();
403
404    mFileSource.clear();
405
406    delete mSuspensionState;
407    mSuspensionState = NULL;
408}
409
410void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) {
411    if (mListener != NULL) {
412        sp<MediaPlayerBase> listener = mListener.promote();
413
414        if (listener != NULL) {
415            listener->sendEvent(msg, ext1, ext2);
416        }
417    }
418}
419
420void AwesomePlayer::onBufferingUpdate() {
421    Mutex::Autolock autoLock(mLock);
422    if (!mBufferingEventPending) {
423        return;
424    }
425    mBufferingEventPending = false;
426
427    int64_t durationUs;
428    {
429        Mutex::Autolock autoLock(mMiscStateLock);
430        durationUs = mDurationUs;
431    }
432
433    if (durationUs >= 0) {
434        int64_t cachedDurationUs = mPrefetcher->getCachedDurationUs();
435
436        LOGV("cache holds %.2f secs worth of data.", cachedDurationUs / 1E6);
437
438        int64_t positionUs;
439        getPosition(&positionUs);
440
441        cachedDurationUs += positionUs;
442
443        double percentage = (double)cachedDurationUs / durationUs;
444        notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage * 100.0);
445
446        postBufferingEvent_l();
447    }
448}
449
450void AwesomePlayer::onStreamDone() {
451    // Posted whenever any stream finishes playing.
452
453    Mutex::Autolock autoLock(mLock);
454    if (!mStreamDoneEventPending) {
455        return;
456    }
457    mStreamDoneEventPending = false;
458
459    if (mStreamDoneStatus == ERROR_END_OF_STREAM && (mFlags & LOOPING)) {
460        seekTo_l(0);
461
462        if (mVideoSource != NULL) {
463            postVideoEvent_l();
464        }
465    } else {
466        if (mStreamDoneStatus == ERROR_END_OF_STREAM) {
467            LOGV("MEDIA_PLAYBACK_COMPLETE");
468            notifyListener_l(MEDIA_PLAYBACK_COMPLETE);
469        } else {
470            LOGV("MEDIA_ERROR %d", mStreamDoneStatus);
471
472            notifyListener_l(
473                    MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus);
474        }
475
476        pause_l();
477
478        mFlags |= AT_EOS;
479    }
480}
481
482status_t AwesomePlayer::play() {
483    Mutex::Autolock autoLock(mLock);
484    return play_l();
485}
486
487status_t AwesomePlayer::play_l() {
488    if (mFlags & PLAYING) {
489        return OK;
490    }
491
492    if (!(mFlags & PREPARED)) {
493        status_t err = prepare_l();
494
495        if (err != OK) {
496            return err;
497        }
498    }
499
500    mFlags |= PLAYING;
501    mFlags |= FIRST_FRAME;
502
503    bool deferredAudioSeek = false;
504
505    if (mAudioSource != NULL) {
506        if (mAudioPlayer == NULL) {
507            if (mAudioSink != NULL) {
508                mAudioPlayer = new AudioPlayer(mAudioSink);
509                mAudioPlayer->setSource(mAudioSource);
510
511                // We've already started the MediaSource in order to enable
512                // the prefetcher to read its data.
513                status_t err = mAudioPlayer->start(
514                        true /* sourceAlreadyStarted */);
515
516                if (err != OK) {
517                    delete mAudioPlayer;
518                    mAudioPlayer = NULL;
519
520                    mFlags &= ~(PLAYING | FIRST_FRAME);
521
522                    return err;
523                }
524
525                delete mTimeSource;
526                mTimeSource = mAudioPlayer;
527
528                deferredAudioSeek = true;
529
530                mWatchForAudioSeekComplete = false;
531                mWatchForAudioEOS = true;
532            }
533        } else {
534            mAudioPlayer->resume();
535        }
536
537        postCheckAudioStatusEvent_l();
538    }
539
540    if (mTimeSource == NULL && mAudioPlayer == NULL) {
541        mTimeSource = new SystemTimeSource;
542    }
543
544    if (mVideoSource != NULL) {
545        // Kick off video playback
546        postVideoEvent_l();
547    }
548
549    if (deferredAudioSeek) {
550        // If there was a seek request while we were paused
551        // and we're just starting up again, honor the request now.
552        seekAudioIfNecessary_l();
553    }
554
555    postBufferingEvent_l();
556
557    if (mFlags & AT_EOS) {
558        // Legacy behaviour, if a stream finishes playing and then
559        // is started again, we play from the start...
560        seekTo_l(0);
561    }
562
563    return OK;
564}
565
566void AwesomePlayer::initRenderer_l() {
567    if (mISurface != NULL) {
568        sp<MetaData> meta = mVideoSource->getFormat();
569
570        int32_t format;
571        const char *component;
572        int32_t decodedWidth, decodedHeight;
573        CHECK(meta->findInt32(kKeyColorFormat, &format));
574        CHECK(meta->findCString(kKeyDecoderComponent, &component));
575        CHECK(meta->findInt32(kKeyWidth, &decodedWidth));
576        CHECK(meta->findInt32(kKeyHeight, &decodedHeight));
577
578        mVideoRenderer.clear();
579
580        // Must ensure that mVideoRenderer's destructor is actually executed
581        // before creating a new one.
582        IPCThreadState::self()->flushCommands();
583
584        if (!strncmp("OMX.", component, 4)) {
585            // Our OMX codecs allocate buffers on the media_server side
586            // therefore they require a remote IOMXRenderer that knows how
587            // to display them.
588            mVideoRenderer = new AwesomeRemoteRenderer(
589                mClient.interface()->createRenderer(
590                        mISurface, component,
591                        (OMX_COLOR_FORMATTYPE)format,
592                        decodedWidth, decodedHeight,
593                        mVideoWidth, mVideoHeight));
594        } else {
595            // Other decoders are instantiated locally and as a consequence
596            // allocate their buffers in local address space.
597            mVideoRenderer = new AwesomeLocalRenderer(
598                false,  // previewOnly
599                component,
600                (OMX_COLOR_FORMATTYPE)format,
601                mISurface,
602                mVideoWidth, mVideoHeight,
603                decodedWidth, decodedHeight);
604        }
605    }
606}
607
608status_t AwesomePlayer::pause() {
609    Mutex::Autolock autoLock(mLock);
610    return pause_l();
611}
612
613status_t AwesomePlayer::pause_l() {
614    if (!(mFlags & PLAYING)) {
615        return OK;
616    }
617
618    cancelPlayerEvents(true /* keepBufferingGoing */);
619
620    if (mAudioPlayer != NULL) {
621        mAudioPlayer->pause();
622    }
623
624    mFlags &= ~PLAYING;
625
626    return OK;
627}
628
629bool AwesomePlayer::isPlaying() const {
630    return mFlags & PLAYING;
631}
632
633void AwesomePlayer::setISurface(const sp<ISurface> &isurface) {
634    Mutex::Autolock autoLock(mLock);
635
636    mISurface = isurface;
637}
638
639void AwesomePlayer::setAudioSink(
640        const sp<MediaPlayerBase::AudioSink> &audioSink) {
641    Mutex::Autolock autoLock(mLock);
642
643    mAudioSink = audioSink;
644}
645
646status_t AwesomePlayer::setLooping(bool shouldLoop) {
647    Mutex::Autolock autoLock(mLock);
648
649    mFlags = mFlags & ~LOOPING;
650
651    if (shouldLoop) {
652        mFlags |= LOOPING;
653    }
654
655    return OK;
656}
657
658status_t AwesomePlayer::getDuration(int64_t *durationUs) {
659    Mutex::Autolock autoLock(mMiscStateLock);
660
661    if (mDurationUs < 0) {
662        return UNKNOWN_ERROR;
663    }
664
665    *durationUs = mDurationUs;
666
667    return OK;
668}
669
670status_t AwesomePlayer::getPosition(int64_t *positionUs) {
671    if (mSeeking) {
672        *positionUs = mSeekTimeUs;
673    } else if (mVideoSource != NULL) {
674        Mutex::Autolock autoLock(mMiscStateLock);
675        *positionUs = mVideoTimeUs;
676    } else if (mAudioPlayer != NULL) {
677        *positionUs = mAudioPlayer->getMediaTimeUs();
678    } else {
679        *positionUs = 0;
680    }
681
682    return OK;
683}
684
685status_t AwesomePlayer::seekTo(int64_t timeUs) {
686    Mutex::Autolock autoLock(mLock);
687    return seekTo_l(timeUs);
688}
689
690status_t AwesomePlayer::seekTo_l(int64_t timeUs) {
691    mSeeking = true;
692    mSeekNotificationSent = false;
693    mSeekTimeUs = timeUs;
694    mFlags &= ~AT_EOS;
695
696    seekAudioIfNecessary_l();
697
698    if (!(mFlags & PLAYING)) {
699        LOGV("seeking while paused, sending SEEK_COMPLETE notification"
700             " immediately.");
701
702        notifyListener_l(MEDIA_SEEK_COMPLETE);
703        mSeekNotificationSent = true;
704    }
705
706    return OK;
707}
708
709void AwesomePlayer::seekAudioIfNecessary_l() {
710    if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) {
711        mAudioPlayer->seekTo(mSeekTimeUs);
712
713        mWatchForAudioSeekComplete = true;
714        mWatchForAudioEOS = true;
715        mSeekNotificationSent = false;
716    }
717}
718
719status_t AwesomePlayer::getVideoDimensions(
720        int32_t *width, int32_t *height) const {
721    Mutex::Autolock autoLock(mLock);
722
723    if (mVideoWidth < 0 || mVideoHeight < 0) {
724        return UNKNOWN_ERROR;
725    }
726
727    *width = mVideoWidth;
728    *height = mVideoHeight;
729
730    return OK;
731}
732
733void AwesomePlayer::setAudioSource(sp<MediaSource> source) {
734    CHECK(source != NULL);
735
736    if (mPrefetcher != NULL) {
737        source = mPrefetcher->addSource(source);
738    }
739
740    mAudioTrack = source;
741}
742
743status_t AwesomePlayer::initAudioDecoder() {
744    sp<MetaData> meta = mAudioTrack->getFormat();
745
746    const char *mime;
747    CHECK(meta->findCString(kKeyMIMEType, &mime));
748
749    if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
750        mAudioSource = mAudioTrack;
751    } else {
752        mAudioSource = OMXCodec::Create(
753                mClient.interface(), mAudioTrack->getFormat(),
754                false, // createEncoder
755                mAudioTrack);
756    }
757
758    if (mAudioSource != NULL) {
759        int64_t durationUs;
760        if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
761            Mutex::Autolock autoLock(mMiscStateLock);
762            if (mDurationUs < 0 || durationUs > mDurationUs) {
763                mDurationUs = durationUs;
764            }
765        }
766    }
767
768    mAudioSource->start();
769
770    return mAudioSource != NULL ? OK : UNKNOWN_ERROR;
771}
772
773void AwesomePlayer::setVideoSource(sp<MediaSource> source) {
774    CHECK(source != NULL);
775
776    if (mPrefetcher != NULL) {
777        source = mPrefetcher->addSource(source);
778    }
779
780    mVideoTrack = source;
781}
782
783status_t AwesomePlayer::initVideoDecoder() {
784    mVideoSource = OMXCodec::Create(
785            mClient.interface(), mVideoTrack->getFormat(),
786            false, // createEncoder
787            mVideoTrack);
788
789    if (mVideoSource != NULL) {
790        int64_t durationUs;
791        if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
792            Mutex::Autolock autoLock(mMiscStateLock);
793            if (mDurationUs < 0 || durationUs > mDurationUs) {
794                mDurationUs = durationUs;
795            }
796        }
797
798        CHECK(mVideoTrack->getFormat()->findInt32(kKeyWidth, &mVideoWidth));
799        CHECK(mVideoTrack->getFormat()->findInt32(kKeyHeight, &mVideoHeight));
800
801        mVideoSource->start();
802    }
803
804    return mVideoSource != NULL ? OK : UNKNOWN_ERROR;
805}
806
807void AwesomePlayer::onVideoEvent() {
808    Mutex::Autolock autoLock(mLock);
809    if (!mVideoEventPending) {
810        // The event has been cancelled in reset_l() but had already
811        // been scheduled for execution at that time.
812        return;
813    }
814    mVideoEventPending = false;
815
816    if (mSeeking) {
817        if (mLastVideoBuffer) {
818            mLastVideoBuffer->release();
819            mLastVideoBuffer = NULL;
820        }
821
822        if (mVideoBuffer) {
823            mVideoBuffer->release();
824            mVideoBuffer = NULL;
825        }
826    }
827
828    if (!mVideoBuffer) {
829        MediaSource::ReadOptions options;
830        if (mSeeking) {
831            LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6);
832
833            options.setSeekTo(mSeekTimeUs);
834        }
835        for (;;) {
836            status_t err = mVideoSource->read(&mVideoBuffer, &options);
837            options.clearSeekTo();
838
839            if (err != OK) {
840                CHECK_EQ(mVideoBuffer, NULL);
841
842                if (err == INFO_FORMAT_CHANGED) {
843                    LOGV("VideoSource signalled format change.");
844
845                    if (mVideoRenderer != NULL) {
846                        mVideoRendererIsPreview = false;
847                        initRenderer_l();
848                    }
849                    continue;
850                }
851
852                postStreamDoneEvent_l(err);
853                return;
854            }
855
856            if (mVideoBuffer->range_length() == 0) {
857                // Some decoders, notably the PV AVC software decoder
858                // return spurious empty buffers that we just want to ignore.
859
860                mVideoBuffer->release();
861                mVideoBuffer = NULL;
862                continue;
863            }
864
865            break;
866        }
867    }
868
869    int64_t timeUs;
870    CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
871
872    {
873        Mutex::Autolock autoLock(mMiscStateLock);
874        mVideoTimeUs = timeUs;
875    }
876
877    if (mSeeking) {
878        if (mAudioPlayer != NULL) {
879            LOGV("seeking audio to %lld us (%.2f secs).", timeUs, timeUs / 1E6);
880
881            mAudioPlayer->seekTo(timeUs);
882            mWatchForAudioSeekComplete = true;
883            mWatchForAudioEOS = true;
884        } else if (!mSeekNotificationSent) {
885            // If we're playing video only, report seek complete now,
886            // otherwise audio player will notify us later.
887            notifyListener_l(MEDIA_SEEK_COMPLETE);
888        }
889
890        mFlags |= FIRST_FRAME;
891        mSeeking = false;
892        mSeekNotificationSent = false;
893    }
894
895    if (mFlags & FIRST_FRAME) {
896        mFlags &= ~FIRST_FRAME;
897
898        mTimeSourceDeltaUs = mTimeSource->getRealTimeUs() - timeUs;
899    }
900
901    int64_t realTimeUs, mediaTimeUs;
902    if (mAudioPlayer != NULL
903        && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) {
904        mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
905    }
906
907    int64_t nowUs = mTimeSource->getRealTimeUs() - mTimeSourceDeltaUs;
908
909    int64_t latenessUs = nowUs - timeUs;
910
911    if (latenessUs > 40000) {
912        // We're more than 40ms late.
913        LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6);
914
915        mVideoBuffer->release();
916        mVideoBuffer = NULL;
917
918        postVideoEvent_l();
919        return;
920    }
921
922    if (latenessUs < -10000) {
923        // We're more than 10ms early.
924
925        postVideoEvent_l(10000);
926        return;
927    }
928
929    if (mVideoRendererIsPreview || mVideoRenderer == NULL) {
930        mVideoRendererIsPreview = false;
931
932        initRenderer_l();
933    }
934
935    if (mVideoRenderer != NULL) {
936        mVideoRenderer->render(mVideoBuffer);
937    }
938
939    if (mLastVideoBuffer) {
940        mLastVideoBuffer->release();
941        mLastVideoBuffer = NULL;
942    }
943    mLastVideoBuffer = mVideoBuffer;
944    mVideoBuffer = NULL;
945
946    postVideoEvent_l();
947}
948
949void AwesomePlayer::postVideoEvent_l(int64_t delayUs) {
950    if (mVideoEventPending) {
951        return;
952    }
953
954    mVideoEventPending = true;
955    mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs);
956}
957
958void AwesomePlayer::postStreamDoneEvent_l(status_t status) {
959    if (mStreamDoneEventPending) {
960        return;
961    }
962    mStreamDoneEventPending = true;
963
964    mStreamDoneStatus = status;
965    mQueue.postEvent(mStreamDoneEvent);
966}
967
968void AwesomePlayer::postBufferingEvent_l() {
969    if (mPrefetcher == NULL) {
970        return;
971    }
972
973    if (mBufferingEventPending) {
974        return;
975    }
976    mBufferingEventPending = true;
977    mQueue.postEventWithDelay(mBufferingEvent, 1000000ll);
978}
979
980void AwesomePlayer::postCheckAudioStatusEvent_l() {
981    if (mAudioStatusEventPending) {
982        return;
983    }
984    mAudioStatusEventPending = true;
985    mQueue.postEventWithDelay(mCheckAudioStatusEvent, 100000ll);
986}
987
988void AwesomePlayer::onCheckAudioStatus() {
989    Mutex::Autolock autoLock(mLock);
990    if (!mAudioStatusEventPending) {
991        // Event was dispatched and while we were blocking on the mutex,
992        // has already been cancelled.
993        return;
994    }
995
996    mAudioStatusEventPending = false;
997
998    if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) {
999        mWatchForAudioSeekComplete = false;
1000
1001        if (!mSeekNotificationSent) {
1002            notifyListener_l(MEDIA_SEEK_COMPLETE);
1003            mSeekNotificationSent = true;
1004        }
1005
1006        mSeeking = false;
1007    }
1008
1009    status_t finalStatus;
1010    if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) {
1011        mWatchForAudioEOS = false;
1012        postStreamDoneEvent_l(finalStatus);
1013    }
1014
1015    postCheckAudioStatusEvent_l();
1016}
1017
1018status_t AwesomePlayer::prepare() {
1019    Mutex::Autolock autoLock(mLock);
1020    return prepare_l();
1021}
1022
1023status_t AwesomePlayer::prepare_l() {
1024    if (mFlags & PREPARED) {
1025        return OK;
1026    }
1027
1028    if (mFlags & PREPARING) {
1029        return UNKNOWN_ERROR;
1030    }
1031
1032    mIsAsyncPrepare = false;
1033    status_t err = prepareAsync_l();
1034
1035    if (err != OK) {
1036        return err;
1037    }
1038
1039    while (mFlags & PREPARING) {
1040        mPreparedCondition.wait(mLock);
1041    }
1042
1043    return mPrepareResult;
1044}
1045
1046status_t AwesomePlayer::prepareAsync() {
1047    Mutex::Autolock autoLock(mLock);
1048
1049    if (mFlags & PREPARING) {
1050        return UNKNOWN_ERROR;  // async prepare already pending
1051    }
1052
1053    mIsAsyncPrepare = true;
1054    return prepareAsync_l();
1055}
1056
1057status_t AwesomePlayer::prepareAsync_l() {
1058    if (mFlags & PREPARING) {
1059        return UNKNOWN_ERROR;  // async prepare already pending
1060    }
1061
1062    if (!mQueueStarted) {
1063        mQueue.start();
1064        mQueueStarted = true;
1065    }
1066
1067    mFlags |= PREPARING;
1068    mAsyncPrepareEvent = new AwesomeEvent(
1069            this, &AwesomePlayer::onPrepareAsyncEvent);
1070
1071    mQueue.postEvent(mAsyncPrepareEvent);
1072
1073    return OK;
1074}
1075
1076status_t AwesomePlayer::finishSetDataSource_l() {
1077    sp<DataSource> dataSource;
1078
1079    if (!strncasecmp("http://", mUri.string(), 7)) {
1080        mConnectingDataSource = new HTTPDataSource(mUri, &mUriHeaders);
1081
1082        mLock.unlock();
1083        status_t err = mConnectingDataSource->connect();
1084        mLock.lock();
1085
1086        if (err != OK) {
1087            mConnectingDataSource.clear();
1088
1089            LOGI("mConnectingDataSource->connect() returned %d", err);
1090            return err;
1091        }
1092
1093        dataSource = new CachingDataSource(
1094                mConnectingDataSource, 32 * 1024, 20);
1095
1096        mConnectingDataSource.clear();
1097    } else {
1098        dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders);
1099    }
1100
1101    if (dataSource == NULL) {
1102        return UNKNOWN_ERROR;
1103    }
1104
1105    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
1106
1107    if (extractor == NULL) {
1108        return UNKNOWN_ERROR;
1109    }
1110
1111    if (dataSource->flags() & DataSource::kWantsPrefetching) {
1112        mPrefetcher = new Prefetcher;
1113    }
1114
1115    return setDataSource_l(extractor);
1116}
1117
1118void AwesomePlayer::abortPrepare(status_t err) {
1119    CHECK(err != OK);
1120
1121    if (mIsAsyncPrepare) {
1122        notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1123    }
1124
1125    mPrepareResult = err;
1126    mFlags &= ~(PREPARING|PREPARE_CANCELLED);
1127    mAsyncPrepareEvent = NULL;
1128    mPreparedCondition.broadcast();
1129}
1130
1131// static
1132bool AwesomePlayer::ContinuePreparation(void *cookie) {
1133    AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie);
1134
1135    return (me->mFlags & PREPARE_CANCELLED) == 0;
1136}
1137
1138void AwesomePlayer::onPrepareAsyncEvent() {
1139    sp<Prefetcher> prefetcher;
1140
1141    {
1142        Mutex::Autolock autoLock(mLock);
1143
1144        if (mFlags & PREPARE_CANCELLED) {
1145            LOGI("prepare was cancelled before doing anything");
1146            abortPrepare(UNKNOWN_ERROR);
1147            return;
1148        }
1149
1150        if (mUri.size() > 0) {
1151            status_t err = finishSetDataSource_l();
1152
1153            if (err != OK) {
1154                abortPrepare(err);
1155                return;
1156            }
1157        }
1158
1159        if (mVideoTrack != NULL && mVideoSource == NULL) {
1160            status_t err = initVideoDecoder();
1161
1162            if (err != OK) {
1163                abortPrepare(err);
1164                return;
1165            }
1166        }
1167
1168        if (mAudioTrack != NULL && mAudioSource == NULL) {
1169            status_t err = initAudioDecoder();
1170
1171            if (err != OK) {
1172                abortPrepare(err);
1173                return;
1174            }
1175        }
1176
1177        prefetcher = mPrefetcher;
1178    }
1179
1180    if (prefetcher != NULL) {
1181        {
1182            Mutex::Autolock autoLock(mLock);
1183            if (mFlags & PREPARE_CANCELLED) {
1184                LOGI("prepare was cancelled before preparing the prefetcher");
1185
1186                prefetcher.clear();
1187                abortPrepare(UNKNOWN_ERROR);
1188                return;
1189            }
1190        }
1191
1192        LOGI("calling prefetcher->prepare()");
1193        status_t result =
1194            prefetcher->prepare(&AwesomePlayer::ContinuePreparation, this);
1195
1196        prefetcher.clear();
1197
1198        if (result == OK) {
1199            LOGI("prefetcher is done preparing");
1200        } else {
1201            Mutex::Autolock autoLock(mLock);
1202
1203            CHECK_EQ(result, -EINTR);
1204
1205            LOGI("prefetcher->prepare() was cancelled early.");
1206            abortPrepare(UNKNOWN_ERROR);
1207            return;
1208        }
1209    }
1210
1211    Mutex::Autolock autoLock(mLock);
1212
1213    if (mIsAsyncPrepare) {
1214        if (mVideoWidth < 0 || mVideoHeight < 0) {
1215            notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0);
1216        } else {
1217            notifyListener_l(MEDIA_SET_VIDEO_SIZE, mVideoWidth, mVideoHeight);
1218        }
1219
1220        notifyListener_l(MEDIA_PREPARED);
1221    }
1222
1223    mPrepareResult = OK;
1224    mFlags &= ~(PREPARING|PREPARE_CANCELLED);
1225    mFlags |= PREPARED;
1226    mAsyncPrepareEvent = NULL;
1227    mPreparedCondition.broadcast();
1228}
1229
1230status_t AwesomePlayer::suspend() {
1231    LOGV("suspend");
1232    Mutex::Autolock autoLock(mLock);
1233
1234    if (mSuspensionState != NULL) {
1235        return INVALID_OPERATION;
1236    }
1237
1238    if (mFlags & PREPARING) {
1239        mFlags |= PREPARE_CANCELLED;
1240        if (mConnectingDataSource != NULL) {
1241            LOGI("interrupting the connection process");
1242            mConnectingDataSource->disconnect();
1243        }
1244    }
1245
1246    while (mFlags & PREPARING) {
1247        mPreparedCondition.wait(mLock);
1248    }
1249
1250    SuspensionState *state = new SuspensionState;
1251    state->mUri = mUri;
1252    state->mUriHeaders = mUriHeaders;
1253    state->mFileSource = mFileSource;
1254
1255    state->mFlags = mFlags & (PLAYING | LOOPING | AT_EOS);
1256    getPosition(&state->mPositionUs);
1257
1258    if (mLastVideoBuffer) {
1259        size_t size = mLastVideoBuffer->range_length();
1260        if (size) {
1261            state->mLastVideoFrameSize = size;
1262            state->mLastVideoFrame = malloc(size);
1263            memcpy(state->mLastVideoFrame,
1264                   (const uint8_t *)mLastVideoBuffer->data()
1265                        + mLastVideoBuffer->range_offset(),
1266                   size);
1267
1268            state->mVideoWidth = mVideoWidth;
1269            state->mVideoHeight = mVideoHeight;
1270
1271            sp<MetaData> meta = mVideoSource->getFormat();
1272            CHECK(meta->findInt32(kKeyColorFormat, &state->mColorFormat));
1273            CHECK(meta->findInt32(kKeyWidth, &state->mDecodedWidth));
1274            CHECK(meta->findInt32(kKeyHeight, &state->mDecodedHeight));
1275        }
1276    }
1277
1278    reset_l();
1279
1280    mSuspensionState = state;
1281
1282    return OK;
1283}
1284
1285status_t AwesomePlayer::resume() {
1286    LOGV("resume");
1287    Mutex::Autolock autoLock(mLock);
1288
1289    if (mSuspensionState == NULL) {
1290        return INVALID_OPERATION;
1291    }
1292
1293    SuspensionState *state = mSuspensionState;
1294    mSuspensionState = NULL;
1295
1296    status_t err;
1297    if (state->mFileSource != NULL) {
1298        err = setDataSource_l(state->mFileSource);
1299
1300        if (err == OK) {
1301            mFileSource = state->mFileSource;
1302        }
1303    } else {
1304        err = setDataSource_l(state->mUri, &state->mUriHeaders);
1305    }
1306
1307    if (err != OK) {
1308        delete state;
1309        state = NULL;
1310
1311        return err;
1312    }
1313
1314    seekTo_l(state->mPositionUs);
1315
1316    mFlags = state->mFlags & (LOOPING | AT_EOS);
1317
1318    if (state->mLastVideoFrame && mISurface != NULL) {
1319        mVideoRenderer =
1320            new AwesomeLocalRenderer(
1321                    true,  // previewOnly
1322                    "",
1323                    (OMX_COLOR_FORMATTYPE)state->mColorFormat,
1324                    mISurface,
1325                    state->mVideoWidth,
1326                    state->mVideoHeight,
1327                    state->mDecodedWidth,
1328                    state->mDecodedHeight);
1329
1330        mVideoRendererIsPreview = true;
1331
1332        ((AwesomeLocalRenderer *)mVideoRenderer.get())->render(
1333                state->mLastVideoFrame, state->mLastVideoFrameSize);
1334    }
1335
1336    if (state->mFlags & PLAYING) {
1337        play_l();
1338    }
1339
1340    delete state;
1341    state = NULL;
1342
1343    return OK;
1344}
1345
1346}  // namespace android
1347
1348