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