AwesomePlayer.cpp revision 1321fdd94d354431b930735e9f38f32ecd189a2d
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 (mVideoSource != NULL) {
672        Mutex::Autolock autoLock(mMiscStateLock);
673        *positionUs = mVideoTimeUs;
674    } else if (mAudioPlayer != NULL) {
675        *positionUs = mAudioPlayer->getMediaTimeUs();
676    } else {
677        *positionUs = 0;
678    }
679
680    return OK;
681}
682
683status_t AwesomePlayer::seekTo(int64_t timeUs) {
684    Mutex::Autolock autoLock(mLock);
685    return seekTo_l(timeUs);
686}
687
688status_t AwesomePlayer::seekTo_l(int64_t timeUs) {
689    mSeeking = true;
690    mSeekNotificationSent = false;
691    mSeekTimeUs = timeUs;
692    mFlags &= ~AT_EOS;
693
694    seekAudioIfNecessary_l();
695
696    if (!(mFlags & PLAYING)) {
697        LOGV("seeking while paused, sending SEEK_COMPLETE notification"
698             " immediately.");
699
700        notifyListener_l(MEDIA_SEEK_COMPLETE);
701        mSeekNotificationSent = true;
702    }
703
704    return OK;
705}
706
707void AwesomePlayer::seekAudioIfNecessary_l() {
708    if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) {
709        mAudioPlayer->seekTo(mSeekTimeUs);
710
711        mWatchForAudioSeekComplete = true;
712        mWatchForAudioEOS = true;
713        mSeeking = false;
714        mSeekNotificationSent = false;
715    }
716}
717
718status_t AwesomePlayer::getVideoDimensions(
719        int32_t *width, int32_t *height) const {
720    Mutex::Autolock autoLock(mLock);
721
722    if (mVideoWidth < 0 || mVideoHeight < 0) {
723        return UNKNOWN_ERROR;
724    }
725
726    *width = mVideoWidth;
727    *height = mVideoHeight;
728
729    return OK;
730}
731
732void AwesomePlayer::setAudioSource(sp<MediaSource> source) {
733    CHECK(source != NULL);
734
735    if (mPrefetcher != NULL) {
736        source = mPrefetcher->addSource(source);
737    }
738
739    mAudioTrack = source;
740}
741
742status_t AwesomePlayer::initAudioDecoder() {
743    sp<MetaData> meta = mAudioTrack->getFormat();
744
745    const char *mime;
746    CHECK(meta->findCString(kKeyMIMEType, &mime));
747
748    if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
749        mAudioSource = mAudioTrack;
750    } else {
751        mAudioSource = OMXCodec::Create(
752                mClient.interface(), mAudioTrack->getFormat(),
753                false, // createEncoder
754                mAudioTrack);
755    }
756
757    if (mAudioSource != NULL) {
758        int64_t durationUs;
759        if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
760            Mutex::Autolock autoLock(mMiscStateLock);
761            if (mDurationUs < 0 || durationUs > mDurationUs) {
762                mDurationUs = durationUs;
763            }
764        }
765    }
766
767    mAudioSource->start();
768
769    return mAudioSource != NULL ? OK : UNKNOWN_ERROR;
770}
771
772void AwesomePlayer::setVideoSource(sp<MediaSource> source) {
773    CHECK(source != NULL);
774
775    if (mPrefetcher != NULL) {
776        source = mPrefetcher->addSource(source);
777    }
778
779    mVideoTrack = source;
780}
781
782status_t AwesomePlayer::initVideoDecoder() {
783    mVideoSource = OMXCodec::Create(
784            mClient.interface(), mVideoTrack->getFormat(),
785            false, // createEncoder
786            mVideoTrack);
787
788    if (mVideoSource != NULL) {
789        int64_t durationUs;
790        if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
791            Mutex::Autolock autoLock(mMiscStateLock);
792            if (mDurationUs < 0 || durationUs > mDurationUs) {
793                mDurationUs = durationUs;
794            }
795        }
796
797        CHECK(mVideoTrack->getFormat()->findInt32(kKeyWidth, &mVideoWidth));
798        CHECK(mVideoTrack->getFormat()->findInt32(kKeyHeight, &mVideoHeight));
799
800        mVideoSource->start();
801    }
802
803    return mVideoSource != NULL ? OK : UNKNOWN_ERROR;
804}
805
806void AwesomePlayer::onVideoEvent() {
807    Mutex::Autolock autoLock(mLock);
808    if (!mVideoEventPending) {
809        // The event has been cancelled in reset_l() but had already
810        // been scheduled for execution at that time.
811        return;
812    }
813    mVideoEventPending = false;
814
815    if (mSeeking) {
816        if (mLastVideoBuffer) {
817            mLastVideoBuffer->release();
818            mLastVideoBuffer = NULL;
819        }
820
821        if (mVideoBuffer) {
822            mVideoBuffer->release();
823            mVideoBuffer = NULL;
824        }
825    }
826
827    if (!mVideoBuffer) {
828        MediaSource::ReadOptions options;
829        if (mSeeking) {
830            LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6);
831
832            options.setSeekTo(mSeekTimeUs);
833        }
834        for (;;) {
835            status_t err = mVideoSource->read(&mVideoBuffer, &options);
836            options.clearSeekTo();
837
838            if (err != OK) {
839                CHECK_EQ(mVideoBuffer, NULL);
840
841                if (err == INFO_FORMAT_CHANGED) {
842                    LOGV("VideoSource signalled format change.");
843
844                    if (mVideoRenderer != NULL) {
845                        mVideoRendererIsPreview = false;
846                        initRenderer_l();
847                    }
848                    continue;
849                }
850
851                postStreamDoneEvent_l(err);
852                return;
853            }
854
855            if (mVideoBuffer->range_length() == 0) {
856                // Some decoders, notably the PV AVC software decoder
857                // return spurious empty buffers that we just want to ignore.
858
859                mVideoBuffer->release();
860                mVideoBuffer = NULL;
861                continue;
862            }
863
864            break;
865        }
866    }
867
868    int64_t timeUs;
869    CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
870
871    {
872        Mutex::Autolock autoLock(mMiscStateLock);
873        mVideoTimeUs = timeUs;
874    }
875
876    if (mSeeking) {
877        if (mAudioPlayer != NULL) {
878            LOGV("seeking audio to %lld us (%.2f secs).", timeUs, timeUs / 1E6);
879
880            mAudioPlayer->seekTo(timeUs);
881            mWatchForAudioSeekComplete = true;
882            mWatchForAudioEOS = true;
883        } else if (!mSeekNotificationSent) {
884            // If we're playing video only, report seek complete now,
885            // otherwise audio player will notify us later.
886            notifyListener_l(MEDIA_SEEK_COMPLETE);
887        }
888
889        mFlags |= FIRST_FRAME;
890        mSeeking = false;
891        mSeekNotificationSent = false;
892    }
893
894    if (mFlags & FIRST_FRAME) {
895        mFlags &= ~FIRST_FRAME;
896
897        mTimeSourceDeltaUs = mTimeSource->getRealTimeUs() - timeUs;
898    }
899
900    int64_t realTimeUs, mediaTimeUs;
901    if (mAudioPlayer != NULL
902        && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) {
903        mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
904    }
905
906    int64_t nowUs = mTimeSource->getRealTimeUs() - mTimeSourceDeltaUs;
907
908    int64_t latenessUs = nowUs - timeUs;
909
910    if (latenessUs > 40000) {
911        // We're more than 40ms late.
912        LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6);
913
914        mVideoBuffer->release();
915        mVideoBuffer = NULL;
916
917        postVideoEvent_l();
918        return;
919    }
920
921    if (latenessUs < -10000) {
922        // We're more than 10ms early.
923
924        postVideoEvent_l(10000);
925        return;
926    }
927
928    if (mVideoRendererIsPreview || mVideoRenderer == NULL) {
929        mVideoRendererIsPreview = false;
930
931        initRenderer_l();
932    }
933
934    if (mVideoRenderer != NULL) {
935        mVideoRenderer->render(mVideoBuffer);
936    }
937
938    if (mLastVideoBuffer) {
939        mLastVideoBuffer->release();
940        mLastVideoBuffer = NULL;
941    }
942    mLastVideoBuffer = mVideoBuffer;
943    mVideoBuffer = NULL;
944
945    postVideoEvent_l();
946}
947
948void AwesomePlayer::postVideoEvent_l(int64_t delayUs) {
949    if (mVideoEventPending) {
950        return;
951    }
952
953    mVideoEventPending = true;
954    mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs);
955}
956
957void AwesomePlayer::postStreamDoneEvent_l(status_t status) {
958    if (mStreamDoneEventPending) {
959        return;
960    }
961    mStreamDoneEventPending = true;
962
963    mStreamDoneStatus = status;
964    mQueue.postEvent(mStreamDoneEvent);
965}
966
967void AwesomePlayer::postBufferingEvent_l() {
968    if (mPrefetcher == NULL) {
969        return;
970    }
971
972    if (mBufferingEventPending) {
973        return;
974    }
975    mBufferingEventPending = true;
976    mQueue.postEventWithDelay(mBufferingEvent, 1000000ll);
977}
978
979void AwesomePlayer::postCheckAudioStatusEvent_l() {
980    if (mAudioStatusEventPending) {
981        return;
982    }
983    mAudioStatusEventPending = true;
984    mQueue.postEventWithDelay(mCheckAudioStatusEvent, 100000ll);
985}
986
987void AwesomePlayer::onCheckAudioStatus() {
988    Mutex::Autolock autoLock(mLock);
989    if (!mAudioStatusEventPending) {
990        // Event was dispatched and while we were blocking on the mutex,
991        // has already been cancelled.
992        return;
993    }
994
995    mAudioStatusEventPending = false;
996
997    if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) {
998        mWatchForAudioSeekComplete = false;
999
1000        if (!mSeekNotificationSent) {
1001            notifyListener_l(MEDIA_SEEK_COMPLETE);
1002            mSeekNotificationSent = true;
1003        }
1004    }
1005
1006    status_t finalStatus;
1007    if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) {
1008        mWatchForAudioEOS = false;
1009        postStreamDoneEvent_l(finalStatus);
1010    }
1011
1012    postCheckAudioStatusEvent_l();
1013}
1014
1015status_t AwesomePlayer::prepare() {
1016    Mutex::Autolock autoLock(mLock);
1017    return prepare_l();
1018}
1019
1020status_t AwesomePlayer::prepare_l() {
1021    if (mFlags & PREPARED) {
1022        return OK;
1023    }
1024
1025    if (mFlags & PREPARING) {
1026        return UNKNOWN_ERROR;
1027    }
1028
1029    mIsAsyncPrepare = false;
1030    status_t err = prepareAsync_l();
1031
1032    if (err != OK) {
1033        return err;
1034    }
1035
1036    while (mFlags & PREPARING) {
1037        mPreparedCondition.wait(mLock);
1038    }
1039
1040    return mPrepareResult;
1041}
1042
1043status_t AwesomePlayer::prepareAsync() {
1044    Mutex::Autolock autoLock(mLock);
1045
1046    if (mFlags & PREPARING) {
1047        return UNKNOWN_ERROR;  // async prepare already pending
1048    }
1049
1050    mIsAsyncPrepare = true;
1051    return prepareAsync_l();
1052}
1053
1054status_t AwesomePlayer::prepareAsync_l() {
1055    if (mFlags & PREPARING) {
1056        return UNKNOWN_ERROR;  // async prepare already pending
1057    }
1058
1059    if (!mQueueStarted) {
1060        mQueue.start();
1061        mQueueStarted = true;
1062    }
1063
1064    mFlags |= PREPARING;
1065    mAsyncPrepareEvent = new AwesomeEvent(
1066            this, &AwesomePlayer::onPrepareAsyncEvent);
1067
1068    mQueue.postEvent(mAsyncPrepareEvent);
1069
1070    return OK;
1071}
1072
1073status_t AwesomePlayer::finishSetDataSource_l() {
1074    sp<DataSource> dataSource;
1075
1076    if (!strncasecmp("http://", mUri.string(), 7)) {
1077        mConnectingDataSource = new HTTPDataSource(mUri, &mUriHeaders);
1078
1079        mLock.unlock();
1080        status_t err = mConnectingDataSource->connect();
1081        mLock.lock();
1082
1083        if (err != OK) {
1084            mConnectingDataSource.clear();
1085
1086            LOGI("mConnectingDataSource->connect() returned %d", err);
1087            return err;
1088        }
1089
1090        dataSource = new CachingDataSource(
1091                mConnectingDataSource, 32 * 1024, 20);
1092
1093        mConnectingDataSource.clear();
1094    } else {
1095        dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders);
1096    }
1097
1098    if (dataSource == NULL) {
1099        return UNKNOWN_ERROR;
1100    }
1101
1102    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
1103
1104    if (extractor == NULL) {
1105        return UNKNOWN_ERROR;
1106    }
1107
1108    if (dataSource->flags() & DataSource::kWantsPrefetching) {
1109        mPrefetcher = new Prefetcher;
1110    }
1111
1112    return setDataSource_l(extractor);
1113}
1114
1115void AwesomePlayer::abortPrepare(status_t err) {
1116    CHECK(err != OK);
1117
1118    if (mIsAsyncPrepare) {
1119        notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1120    }
1121
1122    mPrepareResult = err;
1123    mFlags &= ~(PREPARING|PREPARE_CANCELLED);
1124    mAsyncPrepareEvent = NULL;
1125    mPreparedCondition.broadcast();
1126}
1127
1128// static
1129bool AwesomePlayer::ContinuePreparation(void *cookie) {
1130    AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie);
1131
1132    return (me->mFlags & PREPARE_CANCELLED) == 0;
1133}
1134
1135void AwesomePlayer::onPrepareAsyncEvent() {
1136    sp<Prefetcher> prefetcher;
1137
1138    {
1139        Mutex::Autolock autoLock(mLock);
1140
1141        if (mFlags & PREPARE_CANCELLED) {
1142            LOGI("prepare was cancelled before doing anything");
1143            abortPrepare(UNKNOWN_ERROR);
1144            return;
1145        }
1146
1147        if (mUri.size() > 0) {
1148            status_t err = finishSetDataSource_l();
1149
1150            if (err != OK) {
1151                abortPrepare(err);
1152                return;
1153            }
1154        }
1155
1156        if (mVideoTrack != NULL && mVideoSource == NULL) {
1157            status_t err = initVideoDecoder();
1158
1159            if (err != OK) {
1160                abortPrepare(err);
1161                return;
1162            }
1163        }
1164
1165        if (mAudioTrack != NULL && mAudioSource == NULL) {
1166            status_t err = initAudioDecoder();
1167
1168            if (err != OK) {
1169                abortPrepare(err);
1170                return;
1171            }
1172        }
1173
1174        prefetcher = mPrefetcher;
1175    }
1176
1177    if (prefetcher != NULL) {
1178        {
1179            Mutex::Autolock autoLock(mLock);
1180            if (mFlags & PREPARE_CANCELLED) {
1181                LOGI("prepare was cancelled before preparing the prefetcher");
1182
1183                prefetcher.clear();
1184                abortPrepare(UNKNOWN_ERROR);
1185                return;
1186            }
1187        }
1188
1189        LOGI("calling prefetcher->prepare()");
1190        status_t result =
1191            prefetcher->prepare(&AwesomePlayer::ContinuePreparation, this);
1192
1193        prefetcher.clear();
1194
1195        if (result == OK) {
1196            LOGI("prefetcher is done preparing");
1197        } else {
1198            Mutex::Autolock autoLock(mLock);
1199
1200            CHECK_EQ(result, -EINTR);
1201
1202            LOGI("prefetcher->prepare() was cancelled early.");
1203            abortPrepare(UNKNOWN_ERROR);
1204            return;
1205        }
1206    }
1207
1208    Mutex::Autolock autoLock(mLock);
1209
1210    if (mIsAsyncPrepare) {
1211        if (mVideoWidth < 0 || mVideoHeight < 0) {
1212            notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0);
1213        } else {
1214            notifyListener_l(MEDIA_SET_VIDEO_SIZE, mVideoWidth, mVideoHeight);
1215        }
1216
1217        notifyListener_l(MEDIA_PREPARED);
1218    }
1219
1220    mPrepareResult = OK;
1221    mFlags &= ~(PREPARING|PREPARE_CANCELLED);
1222    mFlags |= PREPARED;
1223    mAsyncPrepareEvent = NULL;
1224    mPreparedCondition.broadcast();
1225}
1226
1227status_t AwesomePlayer::suspend() {
1228    LOGV("suspend");
1229    Mutex::Autolock autoLock(mLock);
1230
1231    if (mSuspensionState != NULL) {
1232        return INVALID_OPERATION;
1233    }
1234
1235    if (mFlags & PREPARING) {
1236        mFlags |= PREPARE_CANCELLED;
1237        if (mConnectingDataSource != NULL) {
1238            LOGI("interrupting the connection process");
1239            mConnectingDataSource->disconnect();
1240        }
1241    }
1242
1243    while (mFlags & PREPARING) {
1244        mPreparedCondition.wait(mLock);
1245    }
1246
1247    SuspensionState *state = new SuspensionState;
1248    state->mUri = mUri;
1249    state->mUriHeaders = mUriHeaders;
1250    state->mFileSource = mFileSource;
1251
1252    state->mFlags = mFlags & (PLAYING | LOOPING | AT_EOS);
1253    getPosition(&state->mPositionUs);
1254
1255    if (mLastVideoBuffer) {
1256        size_t size = mLastVideoBuffer->range_length();
1257        if (size) {
1258            state->mLastVideoFrameSize = size;
1259            state->mLastVideoFrame = malloc(size);
1260            memcpy(state->mLastVideoFrame,
1261                   (const uint8_t *)mLastVideoBuffer->data()
1262                        + mLastVideoBuffer->range_offset(),
1263                   size);
1264
1265            state->mVideoWidth = mVideoWidth;
1266            state->mVideoHeight = mVideoHeight;
1267
1268            sp<MetaData> meta = mVideoSource->getFormat();
1269            CHECK(meta->findInt32(kKeyColorFormat, &state->mColorFormat));
1270            CHECK(meta->findInt32(kKeyWidth, &state->mDecodedWidth));
1271            CHECK(meta->findInt32(kKeyHeight, &state->mDecodedHeight));
1272        }
1273    }
1274
1275    reset_l();
1276
1277    mSuspensionState = state;
1278
1279    return OK;
1280}
1281
1282status_t AwesomePlayer::resume() {
1283    LOGV("resume");
1284    Mutex::Autolock autoLock(mLock);
1285
1286    if (mSuspensionState == NULL) {
1287        return INVALID_OPERATION;
1288    }
1289
1290    SuspensionState *state = mSuspensionState;
1291    mSuspensionState = NULL;
1292
1293    status_t err;
1294    if (state->mFileSource != NULL) {
1295        err = setDataSource_l(state->mFileSource);
1296
1297        if (err == OK) {
1298            mFileSource = state->mFileSource;
1299        }
1300    } else {
1301        err = setDataSource_l(state->mUri, &state->mUriHeaders);
1302    }
1303
1304    if (err != OK) {
1305        delete state;
1306        state = NULL;
1307
1308        return err;
1309    }
1310
1311    seekTo_l(state->mPositionUs);
1312
1313    mFlags = state->mFlags & (LOOPING | AT_EOS);
1314
1315    if (state->mLastVideoFrame && mISurface != NULL) {
1316        mVideoRenderer =
1317            new AwesomeLocalRenderer(
1318                    true,  // previewOnly
1319                    "",
1320                    (OMX_COLOR_FORMATTYPE)state->mColorFormat,
1321                    mISurface,
1322                    state->mVideoWidth,
1323                    state->mVideoHeight,
1324                    state->mDecodedWidth,
1325                    state->mDecodedHeight);
1326
1327        mVideoRendererIsPreview = true;
1328
1329        ((AwesomeLocalRenderer *)mVideoRenderer.get())->render(
1330                state->mLastVideoFrame, state->mLastVideoFrameSize);
1331    }
1332
1333    if (state->mFlags & PLAYING) {
1334        play_l();
1335    }
1336
1337    delete state;
1338    state = NULL;
1339
1340    return OK;
1341}
1342
1343}  // namespace android
1344
1345