AwesomePlayer.cpp revision f337772630b0a1b48d7828647d1079ebdc22919d
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#undef DEBUG_HDCP
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AwesomePlayer"
21#include <utils/Log.h>
22
23#include <dlfcn.h>
24
25#include "include/ARTSPController.h"
26#include "include/AwesomePlayer.h"
27#include "include/DRMExtractor.h"
28#include "include/SoftwareRenderer.h"
29#include "include/NuCachedSource2.h"
30#include "include/ThrottledSource.h"
31#include "include/MPEG2TSExtractor.h"
32#include "include/WVMExtractor.h"
33
34#include "timedtext/TimedTextPlayer.h"
35
36#include <binder/IPCThreadState.h>
37#include <binder/IServiceManager.h>
38#include <media/IMediaPlayerService.h>
39#include <media/stagefright/foundation/hexdump.h>
40#include <media/stagefright/foundation/ADebug.h>
41#include <media/stagefright/AudioPlayer.h>
42#include <media/stagefright/DataSource.h>
43#include <media/stagefright/FileSource.h>
44#include <media/stagefright/MediaBuffer.h>
45#include <media/stagefright/MediaDefs.h>
46#include <media/stagefright/MediaExtractor.h>
47#include <media/stagefright/MediaSource.h>
48#include <media/stagefright/MetaData.h>
49#include <media/stagefright/OMXCodec.h>
50
51#include <surfaceflinger/Surface.h>
52#include <gui/ISurfaceTexture.h>
53#include <gui/SurfaceTextureClient.h>
54#include <surfaceflinger/ISurfaceComposer.h>
55
56#include <media/stagefright/foundation/ALooper.h>
57#include <media/stagefright/foundation/AMessage.h>
58
59#include <cutils/properties.h>
60
61#define USE_SURFACE_ALLOC 1
62#define FRAME_DROP_FREQ 0
63
64namespace android {
65
66static int64_t kLowWaterMarkUs = 2000000ll;  // 2secs
67static int64_t kHighWaterMarkUs = 5000000ll;  // 5secs
68static int64_t kHighWaterMarkRTSPUs = 4000000ll;  // 4secs
69static const size_t kLowWaterMarkBytes = 40000;
70static const size_t kHighWaterMarkBytes = 200000;
71
72struct AwesomeEvent : public TimedEventQueue::Event {
73    AwesomeEvent(
74            AwesomePlayer *player,
75            void (AwesomePlayer::*method)())
76        : mPlayer(player),
77          mMethod(method) {
78    }
79
80protected:
81    virtual ~AwesomeEvent() {}
82
83    virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) {
84        (mPlayer->*mMethod)();
85    }
86
87private:
88    AwesomePlayer *mPlayer;
89    void (AwesomePlayer::*mMethod)();
90
91    AwesomeEvent(const AwesomeEvent &);
92    AwesomeEvent &operator=(const AwesomeEvent &);
93};
94
95struct AwesomeLocalRenderer : public AwesomeRenderer {
96    AwesomeLocalRenderer(
97            const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta)
98        : mTarget(new SoftwareRenderer(nativeWindow, meta)) {
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
116private:
117    SoftwareRenderer *mTarget;
118
119    AwesomeLocalRenderer(const AwesomeLocalRenderer &);
120    AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);;
121};
122
123struct AwesomeNativeWindowRenderer : public AwesomeRenderer {
124    AwesomeNativeWindowRenderer(
125            const sp<ANativeWindow> &nativeWindow,
126            int32_t rotationDegrees)
127        : mNativeWindow(nativeWindow) {
128        applyRotation(rotationDegrees);
129    }
130
131    virtual void render(MediaBuffer *buffer) {
132        int64_t timeUs;
133        CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
134        native_window_set_buffers_timestamp(mNativeWindow.get(), timeUs * 1000);
135        status_t err = mNativeWindow->queueBuffer(
136                mNativeWindow.get(), buffer->graphicBuffer().get());
137        if (err != 0) {
138            LOGE("queueBuffer failed with error %s (%d)", strerror(-err),
139                    -err);
140            return;
141        }
142
143        sp<MetaData> metaData = buffer->meta_data();
144        metaData->setInt32(kKeyRendered, 1);
145    }
146
147protected:
148    virtual ~AwesomeNativeWindowRenderer() {}
149
150private:
151    sp<ANativeWindow> mNativeWindow;
152
153    void applyRotation(int32_t rotationDegrees) {
154        uint32_t transform;
155        switch (rotationDegrees) {
156            case 0: transform = 0; break;
157            case 90: transform = HAL_TRANSFORM_ROT_90; break;
158            case 180: transform = HAL_TRANSFORM_ROT_180; break;
159            case 270: transform = HAL_TRANSFORM_ROT_270; break;
160            default: transform = 0; break;
161        }
162
163        if (transform) {
164            CHECK_EQ(0, native_window_set_buffers_transform(
165                        mNativeWindow.get(), transform));
166        }
167    }
168
169    AwesomeNativeWindowRenderer(const AwesomeNativeWindowRenderer &);
170    AwesomeNativeWindowRenderer &operator=(
171            const AwesomeNativeWindowRenderer &);
172};
173
174// To collect the decoder usage
175void addBatteryData(uint32_t params) {
176    sp<IBinder> binder =
177        defaultServiceManager()->getService(String16("media.player"));
178    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
179    CHECK(service.get() != NULL);
180
181    service->addBatteryData(params);
182}
183
184////////////////////////////////////////////////////////////////////////////////
185AwesomePlayer::AwesomePlayer()
186    : mQueueStarted(false),
187      mUIDValid(false),
188      mTimeSource(NULL),
189      mVideoRendererIsPreview(false),
190      mAudioPlayer(NULL),
191      mDisplayWidth(0),
192      mDisplayHeight(0),
193      mFlags(0),
194      mExtractorFlags(0),
195      mVideoBuffer(NULL),
196      mDecryptHandle(NULL),
197      mLastVideoTimeUs(-1),
198      mTextPlayer(NULL) {
199    CHECK_EQ(mClient.connect(), (status_t)OK);
200
201    DataSource::RegisterDefaultSniffers();
202
203    mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent);
204    mVideoEventPending = false;
205    mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone);
206    mStreamDoneEventPending = false;
207    mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate);
208    mBufferingEventPending = false;
209    mVideoLagEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoLagUpdate);
210    mVideoEventPending = false;
211
212    mCheckAudioStatusEvent = new AwesomeEvent(
213            this, &AwesomePlayer::onCheckAudioStatus);
214
215    mAudioStatusEventPending = false;
216
217    reset();
218}
219
220AwesomePlayer::~AwesomePlayer() {
221    if (mQueueStarted) {
222        mQueue.stop();
223    }
224
225    reset();
226
227    mClient.disconnect();
228}
229
230void AwesomePlayer::cancelPlayerEvents(bool keepBufferingGoing) {
231    mQueue.cancelEvent(mVideoEvent->eventID());
232    mVideoEventPending = false;
233    mQueue.cancelEvent(mStreamDoneEvent->eventID());
234    mStreamDoneEventPending = false;
235    mQueue.cancelEvent(mCheckAudioStatusEvent->eventID());
236    mAudioStatusEventPending = false;
237    mQueue.cancelEvent(mVideoLagEvent->eventID());
238    mVideoLagEventPending = false;
239
240    if (!keepBufferingGoing) {
241        mQueue.cancelEvent(mBufferingEvent->eventID());
242        mBufferingEventPending = false;
243    }
244}
245
246void AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) {
247    Mutex::Autolock autoLock(mLock);
248    mListener = listener;
249}
250
251void AwesomePlayer::setUID(uid_t uid) {
252    LOGV("AwesomePlayer running on behalf of uid %d", uid);
253
254    mUID = uid;
255    mUIDValid = true;
256}
257
258status_t AwesomePlayer::setDataSource(
259        const char *uri, const KeyedVector<String8, String8> *headers) {
260    Mutex::Autolock autoLock(mLock);
261    return setDataSource_l(uri, headers);
262}
263
264status_t AwesomePlayer::setDataSource_l(
265        const char *uri, const KeyedVector<String8, String8> *headers) {
266    reset_l();
267
268    mUri = uri;
269
270    if (headers) {
271        mUriHeaders = *headers;
272
273        ssize_t index = mUriHeaders.indexOfKey(String8("x-hide-urls-from-log"));
274        if (index >= 0) {
275            // Browser is in "incognito" mode, suppress logging URLs.
276
277            // This isn't something that should be passed to the server.
278            mUriHeaders.removeItemsAt(index);
279
280            modifyFlags(INCOGNITO, SET);
281        }
282    }
283
284    if (!(mFlags & INCOGNITO)) {
285        LOGI("setDataSource_l('%s')", mUri.string());
286    } else {
287        LOGI("setDataSource_l(URL suppressed)");
288    }
289
290    // The actual work will be done during preparation in the call to
291    // ::finishSetDataSource_l to avoid blocking the calling thread in
292    // setDataSource for any significant time.
293
294    {
295        Mutex::Autolock autoLock(mStatsLock);
296        mStats.mFd = -1;
297        mStats.mURI = mUri;
298    }
299
300    return OK;
301}
302
303status_t AwesomePlayer::setDataSource(
304        int fd, int64_t offset, int64_t length) {
305    Mutex::Autolock autoLock(mLock);
306
307    reset_l();
308
309    sp<DataSource> dataSource = new FileSource(fd, offset, length);
310
311    status_t err = dataSource->initCheck();
312
313    if (err != OK) {
314        return err;
315    }
316
317    mFileSource = dataSource;
318
319    {
320        Mutex::Autolock autoLock(mStatsLock);
321        mStats.mFd = fd;
322        mStats.mURI = String8();
323    }
324
325    return setDataSource_l(dataSource);
326}
327
328status_t AwesomePlayer::setDataSource(const sp<IStreamSource> &source) {
329    return INVALID_OPERATION;
330}
331
332status_t AwesomePlayer::setDataSource_l(
333        const sp<DataSource> &dataSource) {
334    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
335
336    if (extractor == NULL) {
337        return UNKNOWN_ERROR;
338    }
339
340    dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
341    if (mDecryptHandle != NULL) {
342        CHECK(mDrmManagerClient);
343        if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
344            notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
345        }
346    }
347
348    return setDataSource_l(extractor);
349}
350
351status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {
352    // Attempt to approximate overall stream bitrate by summing all
353    // tracks' individual bitrates, if not all of them advertise bitrate,
354    // we have to fail.
355
356    int64_t totalBitRate = 0;
357
358    for (size_t i = 0; i < extractor->countTracks(); ++i) {
359        sp<MetaData> meta = extractor->getTrackMetaData(i);
360
361        int32_t bitrate;
362        if (!meta->findInt32(kKeyBitRate, &bitrate)) {
363            const char *mime;
364            CHECK(meta->findCString(kKeyMIMEType, &mime));
365            LOGV("track of type '%s' does not publish bitrate", mime);
366
367            totalBitRate = -1;
368            break;
369        }
370
371        totalBitRate += bitrate;
372    }
373
374    mBitrate = totalBitRate;
375
376    LOGV("mBitrate = %lld bits/sec", mBitrate);
377
378    {
379        Mutex::Autolock autoLock(mStatsLock);
380        mStats.mBitrate = mBitrate;
381        mStats.mTracks.clear();
382        mStats.mAudioTrackIndex = -1;
383        mStats.mVideoTrackIndex = -1;
384    }
385
386    bool haveAudio = false;
387    bool haveVideo = false;
388    for (size_t i = 0; i < extractor->countTracks(); ++i) {
389        sp<MetaData> meta = extractor->getTrackMetaData(i);
390
391        const char *mime;
392        CHECK(meta->findCString(kKeyMIMEType, &mime));
393
394        if (!haveVideo && !strncasecmp(mime, "video/", 6)) {
395            setVideoSource(extractor->getTrack(i));
396            haveVideo = true;
397
398            // Set the presentation/display size
399            int32_t displayWidth, displayHeight;
400            bool success = meta->findInt32(kKeyDisplayWidth, &displayWidth);
401            if (success) {
402                success = meta->findInt32(kKeyDisplayHeight, &displayHeight);
403            }
404            if (success) {
405                mDisplayWidth = displayWidth;
406                mDisplayHeight = displayHeight;
407            }
408
409            {
410                Mutex::Autolock autoLock(mStatsLock);
411                mStats.mVideoTrackIndex = mStats.mTracks.size();
412                mStats.mTracks.push();
413                TrackStat *stat =
414                    &mStats.mTracks.editItemAt(mStats.mVideoTrackIndex);
415                stat->mMIME = mime;
416            }
417        } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {
418            setAudioSource(extractor->getTrack(i));
419            haveAudio = true;
420
421            {
422                Mutex::Autolock autoLock(mStatsLock);
423                mStats.mAudioTrackIndex = mStats.mTracks.size();
424                mStats.mTracks.push();
425                TrackStat *stat =
426                    &mStats.mTracks.editItemAt(mStats.mAudioTrackIndex);
427                stat->mMIME = mime;
428            }
429
430            if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
431                // Only do this for vorbis audio, none of the other audio
432                // formats even support this ringtone specific hack and
433                // retrieving the metadata on some extractors may turn out
434                // to be very expensive.
435                sp<MetaData> fileMeta = extractor->getMetaData();
436                int32_t loop;
437                if (fileMeta != NULL
438                        && fileMeta->findInt32(kKeyAutoLoop, &loop) && loop != 0) {
439                    modifyFlags(AUTO_LOOPING, SET);
440                }
441            }
442        } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
443            addTextSource(extractor->getTrack(i));
444        }
445    }
446
447    if (!haveAudio && !haveVideo) {
448        return UNKNOWN_ERROR;
449    }
450
451    mExtractorFlags = extractor->flags();
452
453    return OK;
454}
455
456void AwesomePlayer::reset() {
457    Mutex::Autolock autoLock(mLock);
458    reset_l();
459}
460
461void AwesomePlayer::reset_l() {
462    mDisplayWidth = 0;
463    mDisplayHeight = 0;
464
465    if (mDecryptHandle != NULL) {
466            mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
467                    Playback::STOP, 0);
468            mDecryptHandle = NULL;
469            mDrmManagerClient = NULL;
470    }
471
472    if (mFlags & PLAYING) {
473        uint32_t params = IMediaPlayerService::kBatteryDataTrackDecoder;
474        if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) {
475            params |= IMediaPlayerService::kBatteryDataTrackAudio;
476        }
477        if (mVideoSource != NULL) {
478            params |= IMediaPlayerService::kBatteryDataTrackVideo;
479        }
480        addBatteryData(params);
481    }
482
483    if (mFlags & PREPARING) {
484        modifyFlags(PREPARE_CANCELLED, SET);
485        if (mConnectingDataSource != NULL) {
486            LOGI("interrupting the connection process");
487            mConnectingDataSource->disconnect();
488        } else if (mConnectingRTSPController != NULL) {
489            LOGI("interrupting the connection process");
490            mConnectingRTSPController->disconnect();
491        }
492
493        if (mFlags & PREPARING_CONNECTED) {
494            // We are basically done preparing, we're just buffering
495            // enough data to start playback, we can safely interrupt that.
496            finishAsyncPrepare_l();
497        }
498    }
499
500    while (mFlags & PREPARING) {
501        mPreparedCondition.wait(mLock);
502    }
503
504    cancelPlayerEvents();
505
506    mWVMExtractor.clear();
507    mCachedSource.clear();
508    mAudioTrack.clear();
509    mVideoTrack.clear();
510
511    // Shutdown audio first, so that the respone to the reset request
512    // appears to happen instantaneously as far as the user is concerned
513    // If we did this later, audio would continue playing while we
514    // shutdown the video-related resources and the player appear to
515    // not be as responsive to a reset request.
516    if ((mAudioPlayer == NULL || !(mFlags & AUDIOPLAYER_STARTED))
517            && mAudioSource != NULL) {
518        // If we had an audio player, it would have effectively
519        // taken possession of the audio source and stopped it when
520        // _it_ is stopped. Otherwise this is still our responsibility.
521        mAudioSource->stop();
522    }
523    mAudioSource.clear();
524
525    mTimeSource = NULL;
526
527    delete mAudioPlayer;
528    mAudioPlayer = NULL;
529
530    if (mTextPlayer != NULL) {
531        delete mTextPlayer;
532        mTextPlayer = NULL;
533    }
534
535    mVideoRenderer.clear();
536
537    if (mRTSPController != NULL) {
538        mRTSPController->disconnect();
539        mRTSPController.clear();
540    }
541
542    if (mVideoSource != NULL) {
543        shutdownVideoDecoder_l();
544    }
545
546    mDurationUs = -1;
547    modifyFlags(0, ASSIGN);
548    mExtractorFlags = 0;
549    mTimeSourceDeltaUs = 0;
550    mVideoTimeUs = 0;
551
552    mSeeking = NO_SEEK;
553    mSeekNotificationSent = true;
554    mSeekTimeUs = 0;
555
556    mUri.setTo("");
557    mUriHeaders.clear();
558
559    mFileSource.clear();
560
561    mBitrate = -1;
562    mLastVideoTimeUs = -1;
563
564    {
565        Mutex::Autolock autoLock(mStatsLock);
566        mStats.mFd = -1;
567        mStats.mURI = String8();
568        mStats.mBitrate = -1;
569        mStats.mAudioTrackIndex = -1;
570        mStats.mVideoTrackIndex = -1;
571        mStats.mNumVideoFramesDecoded = 0;
572        mStats.mNumVideoFramesDropped = 0;
573        mStats.mVideoWidth = -1;
574        mStats.mVideoHeight = -1;
575        mStats.mFlags = 0;
576        mStats.mTracks.clear();
577    }
578
579    mWatchForAudioSeekComplete = false;
580    mWatchForAudioEOS = false;
581}
582
583void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) {
584    if (mListener != NULL) {
585        sp<MediaPlayerBase> listener = mListener.promote();
586
587        if (listener != NULL) {
588            listener->sendEvent(msg, ext1, ext2);
589        }
590    }
591}
592
593bool AwesomePlayer::getBitrate(int64_t *bitrate) {
594    off64_t size;
595    if (mDurationUs >= 0 && mCachedSource != NULL
596            && mCachedSource->getSize(&size) == OK) {
597        *bitrate = size * 8000000ll / mDurationUs;  // in bits/sec
598        return true;
599    }
600
601    if (mBitrate >= 0) {
602        *bitrate = mBitrate;
603        return true;
604    }
605
606    *bitrate = 0;
607
608    return false;
609}
610
611// Returns true iff cached duration is available/applicable.
612bool AwesomePlayer::getCachedDuration_l(int64_t *durationUs, bool *eos) {
613    int64_t bitrate;
614
615    if (mRTSPController != NULL) {
616        *durationUs = mRTSPController->getQueueDurationUs(eos);
617        return true;
618    } else if (mCachedSource != NULL && getBitrate(&bitrate)) {
619        status_t finalStatus;
620        size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus);
621        *durationUs = cachedDataRemaining * 8000000ll / bitrate;
622        *eos = (finalStatus != OK);
623        return true;
624    } else if (mWVMExtractor != NULL) {
625        status_t finalStatus;
626        *durationUs = mWVMExtractor->getCachedDurationUs(&finalStatus);
627        *eos = (finalStatus != OK);
628        return true;
629    }
630
631    return false;
632}
633
634void AwesomePlayer::ensureCacheIsFetching_l() {
635    if (mCachedSource != NULL) {
636        mCachedSource->resumeFetchingIfNecessary();
637    }
638}
639
640void AwesomePlayer::onVideoLagUpdate() {
641    Mutex::Autolock autoLock(mLock);
642    if (!mVideoLagEventPending) {
643        return;
644    }
645    mVideoLagEventPending = false;
646
647    int64_t audioTimeUs = mAudioPlayer->getMediaTimeUs();
648    int64_t videoLateByUs = audioTimeUs - mVideoTimeUs;
649
650    if (!(mFlags & VIDEO_AT_EOS) && videoLateByUs > 300000ll) {
651        LOGV("video late by %lld ms.", videoLateByUs / 1000ll);
652
653        notifyListener_l(
654                MEDIA_INFO,
655                MEDIA_INFO_VIDEO_TRACK_LAGGING,
656                videoLateByUs / 1000ll);
657    }
658
659    postVideoLagEvent_l();
660}
661
662void AwesomePlayer::onBufferingUpdate() {
663    Mutex::Autolock autoLock(mLock);
664    if (!mBufferingEventPending) {
665        return;
666    }
667    mBufferingEventPending = false;
668
669    if (mCachedSource != NULL) {
670        status_t finalStatus;
671        size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus);
672        bool eos = (finalStatus != OK);
673
674        if (eos) {
675            if (finalStatus == ERROR_END_OF_STREAM) {
676                notifyListener_l(MEDIA_BUFFERING_UPDATE, 100);
677            }
678            if (mFlags & PREPARING) {
679                LOGV("cache has reached EOS, prepare is done.");
680                finishAsyncPrepare_l();
681            }
682        } else {
683            int64_t bitrate;
684            if (getBitrate(&bitrate)) {
685                size_t cachedSize = mCachedSource->cachedSize();
686                int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate;
687
688                int percentage = 100.0 * (double)cachedDurationUs / mDurationUs;
689                if (percentage > 100) {
690                    percentage = 100;
691                }
692
693                notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage);
694            } else {
695                // We don't know the bitrate of the stream, use absolute size
696                // limits to maintain the cache.
697
698                if ((mFlags & PLAYING) && !eos
699                        && (cachedDataRemaining < kLowWaterMarkBytes)) {
700                    LOGI("cache is running low (< %d) , pausing.",
701                         kLowWaterMarkBytes);
702                    modifyFlags(CACHE_UNDERRUN, SET);
703                    pause_l();
704                    ensureCacheIsFetching_l();
705                    sendCacheStats();
706                    notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START);
707                } else if (eos || cachedDataRemaining > kHighWaterMarkBytes) {
708                    if (mFlags & CACHE_UNDERRUN) {
709                        LOGI("cache has filled up (> %d), resuming.",
710                             kHighWaterMarkBytes);
711                        modifyFlags(CACHE_UNDERRUN, CLEAR);
712                        play_l();
713                        notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END);
714                    } else if (mFlags & PREPARING) {
715                        LOGV("cache has filled up (> %d), prepare is done",
716                             kHighWaterMarkBytes);
717                        finishAsyncPrepare_l();
718                    }
719                }
720            }
721        }
722    } else if (mWVMExtractor != NULL) {
723        status_t finalStatus;
724
725        int64_t cachedDurationUs
726            = mWVMExtractor->getCachedDurationUs(&finalStatus);
727
728        bool eos = (finalStatus != OK);
729
730        if (eos) {
731            if (finalStatus == ERROR_END_OF_STREAM) {
732                notifyListener_l(MEDIA_BUFFERING_UPDATE, 100);
733            }
734            if (mFlags & PREPARING) {
735                LOGV("cache has reached EOS, prepare is done.");
736                finishAsyncPrepare_l();
737            }
738        } else {
739            int percentage = 100.0 * (double)cachedDurationUs / mDurationUs;
740            if (percentage > 100) {
741                percentage = 100;
742            }
743
744            notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage);
745        }
746    }
747
748    int64_t cachedDurationUs;
749    bool eos;
750    if (getCachedDuration_l(&cachedDurationUs, &eos)) {
751        LOGV("cachedDurationUs = %.2f secs, eos=%d",
752             cachedDurationUs / 1E6, eos);
753
754        int64_t highWaterMarkUs =
755            (mRTSPController != NULL) ? kHighWaterMarkRTSPUs : kHighWaterMarkUs;
756
757        if ((mFlags & PLAYING) && !eos
758                && (cachedDurationUs < kLowWaterMarkUs)) {
759            LOGI("cache is running low (%.2f secs) , pausing.",
760                 cachedDurationUs / 1E6);
761            modifyFlags(CACHE_UNDERRUN, SET);
762            pause_l();
763            ensureCacheIsFetching_l();
764            sendCacheStats();
765            notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START);
766        } else if (eos || cachedDurationUs > highWaterMarkUs) {
767            if (mFlags & CACHE_UNDERRUN) {
768                LOGI("cache has filled up (%.2f secs), resuming.",
769                     cachedDurationUs / 1E6);
770                modifyFlags(CACHE_UNDERRUN, CLEAR);
771                play_l();
772                notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END);
773            } else if (mFlags & PREPARING) {
774                LOGV("cache has filled up (%.2f secs), prepare is done",
775                     cachedDurationUs / 1E6);
776                finishAsyncPrepare_l();
777            }
778        }
779    }
780
781    postBufferingEvent_l();
782}
783
784void AwesomePlayer::sendCacheStats() {
785    sp<MediaPlayerBase> listener = mListener.promote();
786    if (listener != NULL && mCachedSource != NULL) {
787        int32_t kbps = 0;
788        status_t err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
789        if (err == OK) {
790            listener->sendEvent(
791                MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
792        }
793    }
794}
795
796void AwesomePlayer::onStreamDone() {
797    // Posted whenever any stream finishes playing.
798
799    Mutex::Autolock autoLock(mLock);
800    if (!mStreamDoneEventPending) {
801        return;
802    }
803    mStreamDoneEventPending = false;
804
805    if (mStreamDoneStatus != ERROR_END_OF_STREAM) {
806        LOGV("MEDIA_ERROR %d", mStreamDoneStatus);
807
808        notifyListener_l(
809                MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus);
810
811        pause_l(true /* at eos */);
812
813        modifyFlags(AT_EOS, SET);
814        return;
815    }
816
817    const bool allDone =
818        (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS))
819            && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS));
820
821    if (!allDone) {
822        return;
823    }
824
825    if ((mFlags & LOOPING)
826            || ((mFlags & AUTO_LOOPING)
827                && (mAudioSink == NULL || mAudioSink->realtime()))) {
828        // Don't AUTO_LOOP if we're being recorded, since that cannot be
829        // turned off and recording would go on indefinitely.
830
831        seekTo_l(0);
832
833        if (mVideoSource != NULL) {
834            postVideoEvent_l();
835        }
836    } else {
837        LOGV("MEDIA_PLAYBACK_COMPLETE");
838        notifyListener_l(MEDIA_PLAYBACK_COMPLETE);
839
840        pause_l(true /* at eos */);
841
842        modifyFlags(AT_EOS, SET);
843    }
844}
845
846status_t AwesomePlayer::play() {
847    Mutex::Autolock autoLock(mLock);
848
849    modifyFlags(CACHE_UNDERRUN, CLEAR);
850
851    return play_l();
852}
853
854status_t AwesomePlayer::play_l() {
855    modifyFlags(SEEK_PREVIEW, CLEAR);
856
857    if (mFlags & PLAYING) {
858        return OK;
859    }
860
861    if (!(mFlags & PREPARED)) {
862        status_t err = prepare_l();
863
864        if (err != OK) {
865            return err;
866        }
867    }
868
869    modifyFlags(PLAYING, SET);
870    modifyFlags(FIRST_FRAME, SET);
871
872    if (mDecryptHandle != NULL) {
873        int64_t position;
874        getPosition(&position);
875        mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
876                Playback::START, position / 1000);
877    }
878
879    if (mAudioSource != NULL) {
880        if (mAudioPlayer == NULL) {
881            if (mAudioSink != NULL) {
882                mAudioPlayer = new AudioPlayer(mAudioSink, this);
883                mAudioPlayer->setSource(mAudioSource);
884
885                mTimeSource = mAudioPlayer;
886
887                // If there was a seek request before we ever started,
888                // honor the request now.
889                // Make sure to do this before starting the audio player
890                // to avoid a race condition.
891                seekAudioIfNecessary_l();
892            }
893        }
894
895        CHECK(!(mFlags & AUDIO_RUNNING));
896
897        if (mVideoSource == NULL) {
898            // We don't want to post an error notification at this point,
899            // the error returned from MediaPlayer::start() will suffice.
900
901            status_t err = startAudioPlayer_l(
902                    false /* sendErrorNotification */);
903
904            if (err != OK) {
905                delete mAudioPlayer;
906                mAudioPlayer = NULL;
907
908                modifyFlags((PLAYING | FIRST_FRAME), CLEAR);
909
910                if (mDecryptHandle != NULL) {
911                    mDrmManagerClient->setPlaybackStatus(
912                            mDecryptHandle, Playback::STOP, 0);
913                }
914
915                return err;
916            }
917        }
918    }
919
920    if (mTimeSource == NULL && mAudioPlayer == NULL) {
921        mTimeSource = &mSystemTimeSource;
922    }
923
924    if (mVideoSource != NULL) {
925        // Kick off video playback
926        postVideoEvent_l();
927
928        if (mAudioSource != NULL && mVideoSource != NULL) {
929            postVideoLagEvent_l();
930        }
931    }
932
933    if (mFlags & AT_EOS) {
934        // Legacy behaviour, if a stream finishes playing and then
935        // is started again, we play from the start...
936        seekTo_l(0);
937    }
938
939    uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted
940        | IMediaPlayerService::kBatteryDataTrackDecoder;
941    if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) {
942        params |= IMediaPlayerService::kBatteryDataTrackAudio;
943    }
944    if (mVideoSource != NULL) {
945        params |= IMediaPlayerService::kBatteryDataTrackVideo;
946    }
947    addBatteryData(params);
948
949    return OK;
950}
951
952status_t AwesomePlayer::startAudioPlayer_l(bool sendErrorNotification) {
953    CHECK(!(mFlags & AUDIO_RUNNING));
954
955    if (mAudioSource == NULL || mAudioPlayer == NULL) {
956        return OK;
957    }
958
959    if (!(mFlags & AUDIOPLAYER_STARTED)) {
960        modifyFlags(AUDIOPLAYER_STARTED, SET);
961
962        bool wasSeeking = mAudioPlayer->isSeeking();
963
964        // We've already started the MediaSource in order to enable
965        // the prefetcher to read its data.
966        status_t err = mAudioPlayer->start(
967                true /* sourceAlreadyStarted */);
968
969        if (err != OK) {
970            if (sendErrorNotification) {
971                notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
972            }
973
974            return err;
975        }
976
977        if (wasSeeking) {
978            CHECK(!mAudioPlayer->isSeeking());
979
980            // We will have finished the seek while starting the audio player.
981            postAudioSeekComplete();
982        }
983    } else {
984        mAudioPlayer->resume();
985    }
986
987    modifyFlags(AUDIO_RUNNING, SET);
988
989    mWatchForAudioEOS = true;
990
991    return OK;
992}
993
994void AwesomePlayer::notifyVideoSize_l() {
995    sp<MetaData> meta = mVideoSource->getFormat();
996
997    int32_t cropLeft, cropTop, cropRight, cropBottom;
998    if (!meta->findRect(
999                kKeyCropRect, &cropLeft, &cropTop, &cropRight, &cropBottom)) {
1000        int32_t width, height;
1001        CHECK(meta->findInt32(kKeyWidth, &width));
1002        CHECK(meta->findInt32(kKeyHeight, &height));
1003
1004        cropLeft = cropTop = 0;
1005        cropRight = width - 1;
1006        cropBottom = height - 1;
1007
1008        LOGV("got dimensions only %d x %d", width, height);
1009    } else {
1010        LOGV("got crop rect %d, %d, %d, %d",
1011             cropLeft, cropTop, cropRight, cropBottom);
1012    }
1013
1014    int32_t displayWidth;
1015    if (meta->findInt32(kKeyDisplayWidth, &displayWidth)) {
1016        LOGV("Display width changed (%d=>%d)", mDisplayWidth, displayWidth);
1017        mDisplayWidth = displayWidth;
1018    }
1019    int32_t displayHeight;
1020    if (meta->findInt32(kKeyDisplayHeight, &displayHeight)) {
1021        LOGV("Display height changed (%d=>%d)", mDisplayHeight, displayHeight);
1022        mDisplayHeight = displayHeight;
1023    }
1024
1025    int32_t usableWidth = cropRight - cropLeft + 1;
1026    int32_t usableHeight = cropBottom - cropTop + 1;
1027    if (mDisplayWidth != 0) {
1028        usableWidth = mDisplayWidth;
1029    }
1030    if (mDisplayHeight != 0) {
1031        usableHeight = mDisplayHeight;
1032    }
1033
1034    {
1035        Mutex::Autolock autoLock(mStatsLock);
1036        mStats.mVideoWidth = usableWidth;
1037        mStats.mVideoHeight = usableHeight;
1038    }
1039
1040    int32_t rotationDegrees;
1041    if (!mVideoTrack->getFormat()->findInt32(
1042                kKeyRotation, &rotationDegrees)) {
1043        rotationDegrees = 0;
1044    }
1045
1046    if (rotationDegrees == 90 || rotationDegrees == 270) {
1047        notifyListener_l(
1048                MEDIA_SET_VIDEO_SIZE, usableHeight, usableWidth);
1049    } else {
1050        notifyListener_l(
1051                MEDIA_SET_VIDEO_SIZE, usableWidth, usableHeight);
1052    }
1053}
1054
1055void AwesomePlayer::initRenderer_l() {
1056    if (mNativeWindow == NULL) {
1057        return;
1058    }
1059
1060    sp<MetaData> meta = mVideoSource->getFormat();
1061
1062    int32_t format;
1063    const char *component;
1064    int32_t decodedWidth, decodedHeight;
1065    CHECK(meta->findInt32(kKeyColorFormat, &format));
1066    CHECK(meta->findCString(kKeyDecoderComponent, &component));
1067    CHECK(meta->findInt32(kKeyWidth, &decodedWidth));
1068    CHECK(meta->findInt32(kKeyHeight, &decodedHeight));
1069
1070    int32_t rotationDegrees;
1071    if (!mVideoTrack->getFormat()->findInt32(
1072                kKeyRotation, &rotationDegrees)) {
1073        rotationDegrees = 0;
1074    }
1075
1076    mVideoRenderer.clear();
1077
1078    // Must ensure that mVideoRenderer's destructor is actually executed
1079    // before creating a new one.
1080    IPCThreadState::self()->flushCommands();
1081
1082    if (USE_SURFACE_ALLOC
1083            && !strncmp(component, "OMX.", 4)
1084            && strncmp(component, "OMX.google.", 11)
1085            && strcmp(component, "OMX.Nvidia.mpeg2v.decode")) {
1086        // Hardware decoders avoid the CPU color conversion by decoding
1087        // directly to ANativeBuffers, so we must use a renderer that
1088        // just pushes those buffers to the ANativeWindow.
1089        mVideoRenderer =
1090            new AwesomeNativeWindowRenderer(mNativeWindow, rotationDegrees);
1091    } else {
1092        // Other decoders are instantiated locally and as a consequence
1093        // allocate their buffers in local address space.  This renderer
1094        // then performs a color conversion and copy to get the data
1095        // into the ANativeBuffer.
1096        mVideoRenderer = new AwesomeLocalRenderer(mNativeWindow, meta);
1097    }
1098}
1099
1100status_t AwesomePlayer::pause() {
1101    Mutex::Autolock autoLock(mLock);
1102
1103    modifyFlags(CACHE_UNDERRUN, CLEAR);
1104
1105    return pause_l();
1106}
1107
1108status_t AwesomePlayer::pause_l(bool at_eos) {
1109    if (!(mFlags & PLAYING)) {
1110        return OK;
1111    }
1112
1113    cancelPlayerEvents(true /* keepBufferingGoing */);
1114
1115    if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) {
1116        if (at_eos) {
1117            // If we played the audio stream to completion we
1118            // want to make sure that all samples remaining in the audio
1119            // track's queue are played out.
1120            mAudioPlayer->pause(true /* playPendingSamples */);
1121        } else {
1122            mAudioPlayer->pause();
1123        }
1124
1125        modifyFlags(AUDIO_RUNNING, CLEAR);
1126    }
1127
1128    if (mFlags & TEXTPLAYER_STARTED) {
1129        mTextPlayer->pause();
1130        modifyFlags(TEXT_RUNNING, CLEAR);
1131    }
1132
1133    modifyFlags(PLAYING, CLEAR);
1134
1135    if (mDecryptHandle != NULL) {
1136        mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
1137                Playback::PAUSE, 0);
1138    }
1139
1140    uint32_t params = IMediaPlayerService::kBatteryDataTrackDecoder;
1141    if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) {
1142        params |= IMediaPlayerService::kBatteryDataTrackAudio;
1143    }
1144    if (mVideoSource != NULL) {
1145        params |= IMediaPlayerService::kBatteryDataTrackVideo;
1146    }
1147
1148    addBatteryData(params);
1149
1150    return OK;
1151}
1152
1153bool AwesomePlayer::isPlaying() const {
1154    return (mFlags & PLAYING) || (mFlags & CACHE_UNDERRUN);
1155}
1156
1157status_t AwesomePlayer::setSurface(const sp<Surface> &surface) {
1158    Mutex::Autolock autoLock(mLock);
1159
1160    mSurface = surface;
1161    return setNativeWindow_l(surface);
1162}
1163
1164status_t AwesomePlayer::setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
1165    Mutex::Autolock autoLock(mLock);
1166
1167    mSurface.clear();
1168
1169    status_t err;
1170    if (surfaceTexture != NULL) {
1171        err = setNativeWindow_l(new SurfaceTextureClient(surfaceTexture));
1172    } else {
1173        err = setNativeWindow_l(NULL);
1174    }
1175
1176    return err;
1177}
1178
1179void AwesomePlayer::shutdownVideoDecoder_l() {
1180    if (mVideoBuffer) {
1181        mVideoBuffer->release();
1182        mVideoBuffer = NULL;
1183    }
1184
1185    mVideoSource->stop();
1186
1187    // The following hack is necessary to ensure that the OMX
1188    // component is completely released by the time we may try
1189    // to instantiate it again.
1190    wp<MediaSource> tmp = mVideoSource;
1191    mVideoSource.clear();
1192    while (tmp.promote() != NULL) {
1193        usleep(1000);
1194    }
1195    IPCThreadState::self()->flushCommands();
1196    LOGV("video decoder shutdown completed");
1197}
1198
1199status_t AwesomePlayer::setNativeWindow_l(const sp<ANativeWindow> &native) {
1200    mNativeWindow = native;
1201
1202    if (mVideoSource == NULL) {
1203        return OK;
1204    }
1205
1206    LOGV("attempting to reconfigure to use new surface");
1207
1208    bool wasPlaying = (mFlags & PLAYING) != 0;
1209
1210    pause_l();
1211    mVideoRenderer.clear();
1212
1213    shutdownVideoDecoder_l();
1214
1215    status_t err = initVideoDecoder();
1216
1217    if (err != OK) {
1218        LOGE("failed to reinstantiate video decoder after surface change.");
1219        return err;
1220    }
1221
1222    if (mLastVideoTimeUs >= 0) {
1223        mSeeking = SEEK;
1224        mSeekTimeUs = mLastVideoTimeUs;
1225        modifyFlags((AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS), CLEAR);
1226    }
1227
1228    if (wasPlaying) {
1229        play_l();
1230    }
1231
1232    return OK;
1233}
1234
1235void AwesomePlayer::setAudioSink(
1236        const sp<MediaPlayerBase::AudioSink> &audioSink) {
1237    Mutex::Autolock autoLock(mLock);
1238
1239    mAudioSink = audioSink;
1240}
1241
1242status_t AwesomePlayer::setLooping(bool shouldLoop) {
1243    Mutex::Autolock autoLock(mLock);
1244
1245    modifyFlags(LOOPING, CLEAR);
1246
1247    if (shouldLoop) {
1248        modifyFlags(LOOPING, SET);
1249    }
1250
1251    return OK;
1252}
1253
1254status_t AwesomePlayer::getDuration(int64_t *durationUs) {
1255    Mutex::Autolock autoLock(mMiscStateLock);
1256
1257    if (mDurationUs < 0) {
1258        return UNKNOWN_ERROR;
1259    }
1260
1261    *durationUs = mDurationUs;
1262
1263    return OK;
1264}
1265
1266status_t AwesomePlayer::getPosition(int64_t *positionUs) {
1267    if (mRTSPController != NULL) {
1268        *positionUs = mRTSPController->getNormalPlayTimeUs();
1269    }
1270    else if (mSeeking != NO_SEEK) {
1271        *positionUs = mSeekTimeUs;
1272    } else if (mVideoSource != NULL
1273            && (mAudioPlayer == NULL || !(mFlags & VIDEO_AT_EOS))) {
1274        Mutex::Autolock autoLock(mMiscStateLock);
1275        *positionUs = mVideoTimeUs;
1276    } else if (mAudioPlayer != NULL) {
1277        *positionUs = mAudioPlayer->getMediaTimeUs();
1278    } else {
1279        *positionUs = 0;
1280    }
1281
1282    return OK;
1283}
1284
1285status_t AwesomePlayer::seekTo(int64_t timeUs) {
1286    if (mExtractorFlags & MediaExtractor::CAN_SEEK) {
1287        Mutex::Autolock autoLock(mLock);
1288        return seekTo_l(timeUs);
1289    }
1290
1291    return OK;
1292}
1293
1294status_t AwesomePlayer::setTimedTextTrackIndex(int32_t index) {
1295    if (mTextPlayer != NULL) {
1296        if (index >= 0) { // to turn on a text track
1297            status_t err = mTextPlayer->setTimedTextTrackIndex(index);
1298            if (err != OK) {
1299                return err;
1300            }
1301
1302            modifyFlags(TEXT_RUNNING, SET);
1303            modifyFlags(TEXTPLAYER_STARTED, SET);
1304            return OK;
1305        } else { // to turn off the text track display
1306            if (mFlags  & TEXT_RUNNING) {
1307                modifyFlags(TEXT_RUNNING, CLEAR);
1308            }
1309            if (mFlags  & TEXTPLAYER_STARTED) {
1310                modifyFlags(TEXTPLAYER_STARTED, CLEAR);
1311            }
1312
1313            return mTextPlayer->setTimedTextTrackIndex(index);
1314        }
1315    } else {
1316        return INVALID_OPERATION;
1317    }
1318}
1319
1320// static
1321void AwesomePlayer::OnRTSPSeekDoneWrapper(void *cookie) {
1322    static_cast<AwesomePlayer *>(cookie)->onRTSPSeekDone();
1323}
1324
1325void AwesomePlayer::onRTSPSeekDone() {
1326    if (!mSeekNotificationSent) {
1327        notifyListener_l(MEDIA_SEEK_COMPLETE);
1328        mSeekNotificationSent = true;
1329    }
1330}
1331
1332status_t AwesomePlayer::seekTo_l(int64_t timeUs) {
1333    if (mRTSPController != NULL) {
1334        mSeekNotificationSent = false;
1335        mRTSPController->seekAsync(timeUs, OnRTSPSeekDoneWrapper, this);
1336        return OK;
1337    }
1338
1339    if (mFlags & CACHE_UNDERRUN) {
1340        modifyFlags(CACHE_UNDERRUN, CLEAR);
1341        play_l();
1342    }
1343
1344    if ((mFlags & PLAYING) && mVideoSource != NULL && (mFlags & VIDEO_AT_EOS)) {
1345        // Video playback completed before, there's no pending
1346        // video event right now. In order for this new seek
1347        // to be honored, we need to post one.
1348
1349        postVideoEvent_l();
1350    }
1351
1352    mSeeking = SEEK;
1353    mSeekNotificationSent = false;
1354    mSeekTimeUs = timeUs;
1355    modifyFlags((AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS), CLEAR);
1356
1357    seekAudioIfNecessary_l();
1358
1359    if (mFlags & TEXTPLAYER_STARTED) {
1360        mTextPlayer->seekTo(mSeekTimeUs);
1361    }
1362
1363    if (!(mFlags & PLAYING)) {
1364        LOGV("seeking while paused, sending SEEK_COMPLETE notification"
1365             " immediately.");
1366
1367        notifyListener_l(MEDIA_SEEK_COMPLETE);
1368        mSeekNotificationSent = true;
1369
1370        if ((mFlags & PREPARED) && mVideoSource != NULL) {
1371            modifyFlags(SEEK_PREVIEW, SET);
1372            postVideoEvent_l();
1373        }
1374    }
1375
1376    return OK;
1377}
1378
1379void AwesomePlayer::seekAudioIfNecessary_l() {
1380    if (mSeeking != NO_SEEK && mVideoSource == NULL && mAudioPlayer != NULL) {
1381        mAudioPlayer->seekTo(mSeekTimeUs);
1382
1383        mWatchForAudioSeekComplete = true;
1384        mWatchForAudioEOS = true;
1385
1386        if (mDecryptHandle != NULL) {
1387            mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
1388                    Playback::PAUSE, 0);
1389            mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
1390                    Playback::START, mSeekTimeUs / 1000);
1391        }
1392    }
1393}
1394
1395void AwesomePlayer::setAudioSource(sp<MediaSource> source) {
1396    CHECK(source != NULL);
1397
1398    mAudioTrack = source;
1399}
1400
1401void AwesomePlayer::addTextSource(sp<MediaSource> source) {
1402    Mutex::Autolock autoLock(mTimedTextLock);
1403    CHECK(source != NULL);
1404
1405    if (mTextPlayer == NULL) {
1406        mTextPlayer = new TimedTextPlayer(this, mListener, &mQueue);
1407    }
1408
1409    mTextPlayer->addTextSource(source);
1410}
1411
1412status_t AwesomePlayer::initAudioDecoder() {
1413    sp<MetaData> meta = mAudioTrack->getFormat();
1414
1415    const char *mime;
1416    CHECK(meta->findCString(kKeyMIMEType, &mime));
1417
1418    if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
1419        mAudioSource = mAudioTrack;
1420    } else {
1421        mAudioSource = OMXCodec::Create(
1422                mClient.interface(), mAudioTrack->getFormat(),
1423                false, // createEncoder
1424                mAudioTrack);
1425    }
1426
1427    if (mAudioSource != NULL) {
1428        int64_t durationUs;
1429        if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
1430            Mutex::Autolock autoLock(mMiscStateLock);
1431            if (mDurationUs < 0 || durationUs > mDurationUs) {
1432                mDurationUs = durationUs;
1433            }
1434        }
1435
1436        status_t err = mAudioSource->start();
1437
1438        if (err != OK) {
1439            mAudioSource.clear();
1440            return err;
1441        }
1442    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) {
1443        // For legacy reasons we're simply going to ignore the absence
1444        // of an audio decoder for QCELP instead of aborting playback
1445        // altogether.
1446        return OK;
1447    }
1448
1449    if (mAudioSource != NULL) {
1450        Mutex::Autolock autoLock(mStatsLock);
1451        TrackStat *stat = &mStats.mTracks.editItemAt(mStats.mAudioTrackIndex);
1452
1453        const char *component;
1454        if (!mAudioSource->getFormat()
1455                ->findCString(kKeyDecoderComponent, &component)) {
1456            component = "none";
1457        }
1458
1459        stat->mDecoderName = component;
1460    }
1461
1462    return mAudioSource != NULL ? OK : UNKNOWN_ERROR;
1463}
1464
1465void AwesomePlayer::setVideoSource(sp<MediaSource> source) {
1466    CHECK(source != NULL);
1467
1468    mVideoTrack = source;
1469}
1470
1471status_t AwesomePlayer::initVideoDecoder(uint32_t flags) {
1472
1473    // Either the application or the DRM system can independently say
1474    // that there must be a hardware-protected path to an external video sink.
1475    // For now we always require a hardware-protected path to external video sink
1476    // if content is DRMed, but eventually this could be optional per DRM agent.
1477    // When the application wants protection, then
1478    //   (USE_SURFACE_ALLOC && (mSurface != 0) &&
1479    //   (mSurface->getFlags() & ISurfaceComposer::eProtectedByApp))
1480    // will be true, but that part is already handled by SurfaceFlinger.
1481
1482#ifdef DEBUG_HDCP
1483    // For debugging, we allow a system property to control the protected usage.
1484    // In case of uninitialized or unexpected property, we default to "DRM only".
1485    bool setProtectionBit = false;
1486    char value[PROPERTY_VALUE_MAX];
1487    if (property_get("persist.sys.hdcp_checking", value, NULL)) {
1488        if (!strcmp(value, "never")) {
1489            // nop
1490        } else if (!strcmp(value, "always")) {
1491            setProtectionBit = true;
1492        } else if (!strcmp(value, "drm-only")) {
1493            if (mDecryptHandle != NULL) {
1494                setProtectionBit = true;
1495            }
1496        // property value is empty, or unexpected value
1497        } else {
1498            if (mDecryptHandle != NULL) {
1499                setProtectionBit = true;
1500            }
1501        }
1502    // can' read property value
1503    } else {
1504        if (mDecryptHandle != NULL) {
1505            setProtectionBit = true;
1506        }
1507    }
1508    // note that usage bit is already cleared, so no need to clear it in the "else" case
1509    if (setProtectionBit) {
1510        flags |= OMXCodec::kEnableGrallocUsageProtected;
1511    }
1512#else
1513    if (mDecryptHandle != NULL) {
1514        flags |= OMXCodec::kEnableGrallocUsageProtected;
1515    }
1516#endif
1517    LOGV("initVideoDecoder flags=0x%x", flags);
1518    mVideoSource = OMXCodec::Create(
1519            mClient.interface(), mVideoTrack->getFormat(),
1520            false, // createEncoder
1521            mVideoTrack,
1522            NULL, flags, USE_SURFACE_ALLOC ? mNativeWindow : NULL);
1523
1524    if (mVideoSource != NULL) {
1525        int64_t durationUs;
1526        if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) {
1527            Mutex::Autolock autoLock(mMiscStateLock);
1528            if (mDurationUs < 0 || durationUs > mDurationUs) {
1529                mDurationUs = durationUs;
1530            }
1531        }
1532
1533        status_t err = mVideoSource->start();
1534
1535        if (err != OK) {
1536            mVideoSource.clear();
1537            return err;
1538        }
1539    }
1540
1541    if (mVideoSource != NULL) {
1542        const char *componentName;
1543        CHECK(mVideoSource->getFormat()
1544                ->findCString(kKeyDecoderComponent, &componentName));
1545
1546        {
1547            Mutex::Autolock autoLock(mStatsLock);
1548            TrackStat *stat = &mStats.mTracks.editItemAt(mStats.mVideoTrackIndex);
1549
1550            stat->mDecoderName = componentName;
1551        }
1552
1553        static const char *kPrefix = "OMX.Nvidia.";
1554        static const char *kSuffix = ".decode";
1555        static const size_t kSuffixLength = strlen(kSuffix);
1556
1557        size_t componentNameLength = strlen(componentName);
1558
1559        if (!strncmp(componentName, kPrefix, strlen(kPrefix))
1560                && componentNameLength >= kSuffixLength
1561                && !strcmp(&componentName[
1562                    componentNameLength - kSuffixLength], kSuffix)) {
1563            modifyFlags(SLOW_DECODER_HACK, SET);
1564        }
1565    }
1566
1567    return mVideoSource != NULL ? OK : UNKNOWN_ERROR;
1568}
1569
1570void AwesomePlayer::finishSeekIfNecessary(int64_t videoTimeUs) {
1571    if (mSeeking == SEEK_VIDEO_ONLY) {
1572        mSeeking = NO_SEEK;
1573        return;
1574    }
1575
1576    if (mSeeking == NO_SEEK || (mFlags & SEEK_PREVIEW)) {
1577        return;
1578    }
1579
1580    if (mAudioPlayer != NULL) {
1581        LOGV("seeking audio to %lld us (%.2f secs).", videoTimeUs, videoTimeUs / 1E6);
1582
1583        // If we don't have a video time, seek audio to the originally
1584        // requested seek time instead.
1585
1586        mAudioPlayer->seekTo(videoTimeUs < 0 ? mSeekTimeUs : videoTimeUs);
1587        mWatchForAudioSeekComplete = true;
1588        mWatchForAudioEOS = true;
1589    } else if (!mSeekNotificationSent) {
1590        // If we're playing video only, report seek complete now,
1591        // otherwise audio player will notify us later.
1592        notifyListener_l(MEDIA_SEEK_COMPLETE);
1593        mSeekNotificationSent = true;
1594    }
1595
1596    modifyFlags(FIRST_FRAME, SET);
1597    mSeeking = NO_SEEK;
1598
1599    if (mDecryptHandle != NULL) {
1600        mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
1601                Playback::PAUSE, 0);
1602        mDrmManagerClient->setPlaybackStatus(mDecryptHandle,
1603                Playback::START, videoTimeUs / 1000);
1604    }
1605}
1606
1607void AwesomePlayer::onVideoEvent() {
1608    Mutex::Autolock autoLock(mLock);
1609    if (!mVideoEventPending) {
1610        // The event has been cancelled in reset_l() but had already
1611        // been scheduled for execution at that time.
1612        return;
1613    }
1614    mVideoEventPending = false;
1615
1616    if (mSeeking != NO_SEEK) {
1617        if (mVideoBuffer) {
1618            mVideoBuffer->release();
1619            mVideoBuffer = NULL;
1620        }
1621
1622        if (mSeeking == SEEK && isStreamingHTTP() && mAudioSource != NULL
1623                && !(mFlags & SEEK_PREVIEW)) {
1624            // We're going to seek the video source first, followed by
1625            // the audio source.
1626            // In order to avoid jumps in the DataSource offset caused by
1627            // the audio codec prefetching data from the old locations
1628            // while the video codec is already reading data from the new
1629            // locations, we'll "pause" the audio source, causing it to
1630            // stop reading input data until a subsequent seek.
1631
1632            if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) {
1633                mAudioPlayer->pause();
1634
1635                modifyFlags(AUDIO_RUNNING, CLEAR);
1636            }
1637            mAudioSource->pause();
1638        }
1639    }
1640
1641    if (!mVideoBuffer) {
1642        MediaSource::ReadOptions options;
1643        if (mSeeking != NO_SEEK) {
1644            LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6);
1645
1646            options.setSeekTo(
1647                    mSeekTimeUs,
1648                    mSeeking == SEEK_VIDEO_ONLY
1649                        ? MediaSource::ReadOptions::SEEK_NEXT_SYNC
1650                        : MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
1651        }
1652        for (;;) {
1653            status_t err = mVideoSource->read(&mVideoBuffer, &options);
1654            options.clearSeekTo();
1655
1656            if (err != OK) {
1657                CHECK(mVideoBuffer == NULL);
1658
1659                if (err == INFO_FORMAT_CHANGED) {
1660                    LOGV("VideoSource signalled format change.");
1661
1662                    notifyVideoSize_l();
1663
1664                    if (mVideoRenderer != NULL) {
1665                        mVideoRendererIsPreview = false;
1666                        initRenderer_l();
1667                    }
1668                    continue;
1669                }
1670
1671                // So video playback is complete, but we may still have
1672                // a seek request pending that needs to be applied
1673                // to the audio track.
1674                if (mSeeking != NO_SEEK) {
1675                    LOGV("video stream ended while seeking!");
1676                }
1677                finishSeekIfNecessary(-1);
1678
1679                if (mAudioPlayer != NULL
1680                        && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) {
1681                    startAudioPlayer_l();
1682                }
1683
1684                modifyFlags(VIDEO_AT_EOS, SET);
1685                postStreamDoneEvent_l(err);
1686                return;
1687            }
1688
1689            if (mVideoBuffer->range_length() == 0) {
1690                // Some decoders, notably the PV AVC software decoder
1691                // return spurious empty buffers that we just want to ignore.
1692
1693                mVideoBuffer->release();
1694                mVideoBuffer = NULL;
1695                continue;
1696            }
1697
1698            break;
1699        }
1700
1701        {
1702            Mutex::Autolock autoLock(mStatsLock);
1703            ++mStats.mNumVideoFramesDecoded;
1704        }
1705    }
1706
1707    int64_t timeUs;
1708    CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs));
1709
1710    mLastVideoTimeUs = timeUs;
1711
1712    if (mSeeking == SEEK_VIDEO_ONLY) {
1713        if (mSeekTimeUs > timeUs) {
1714            LOGI("XXX mSeekTimeUs = %lld us, timeUs = %lld us",
1715                 mSeekTimeUs, timeUs);
1716        }
1717    }
1718
1719    {
1720        Mutex::Autolock autoLock(mMiscStateLock);
1721        mVideoTimeUs = timeUs;
1722    }
1723
1724    SeekType wasSeeking = mSeeking;
1725    finishSeekIfNecessary(timeUs);
1726
1727    if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) {
1728        status_t err = startAudioPlayer_l();
1729        if (err != OK) {
1730            LOGE("Starting the audio player failed w/ err %d", err);
1731            return;
1732        }
1733    }
1734
1735    if ((mFlags & TEXTPLAYER_STARTED) && !(mFlags & (TEXT_RUNNING | SEEK_PREVIEW))) {
1736        mTextPlayer->resume();
1737        modifyFlags(TEXT_RUNNING, SET);
1738    }
1739
1740    TimeSource *ts =
1741        ((mFlags & AUDIO_AT_EOS) || !(mFlags & AUDIOPLAYER_STARTED))
1742            ? &mSystemTimeSource : mTimeSource;
1743
1744    if (mFlags & FIRST_FRAME) {
1745        modifyFlags(FIRST_FRAME, CLEAR);
1746        mSinceLastDropped = 0;
1747        mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs;
1748    }
1749
1750    int64_t realTimeUs, mediaTimeUs;
1751    if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL
1752        && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) {
1753        mTimeSourceDeltaUs = realTimeUs - mediaTimeUs;
1754    }
1755
1756    if (wasSeeking == SEEK_VIDEO_ONLY) {
1757        int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
1758
1759        int64_t latenessUs = nowUs - timeUs;
1760
1761        if (latenessUs > 0) {
1762            LOGI("after SEEK_VIDEO_ONLY we're late by %.2f secs", latenessUs / 1E6);
1763        }
1764    }
1765
1766    if (wasSeeking == NO_SEEK) {
1767        // Let's display the first frame after seeking right away.
1768
1769        int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs;
1770
1771        int64_t latenessUs = nowUs - timeUs;
1772
1773        if (latenessUs > 500000ll
1774                && mRTSPController == NULL
1775                && mAudioPlayer != NULL
1776                && mAudioPlayer->getMediaTimeMapping(
1777                    &realTimeUs, &mediaTimeUs)) {
1778            LOGI("we're much too late (%.2f secs), video skipping ahead",
1779                 latenessUs / 1E6);
1780
1781            mVideoBuffer->release();
1782            mVideoBuffer = NULL;
1783
1784            mSeeking = SEEK_VIDEO_ONLY;
1785            mSeekTimeUs = mediaTimeUs;
1786
1787            postVideoEvent_l();
1788            return;
1789        }
1790
1791        if (latenessUs > 40000) {
1792            // We're more than 40ms late.
1793            LOGV("we're late by %lld us (%.2f secs)",
1794                 latenessUs, latenessUs / 1E6);
1795
1796            if (!(mFlags & SLOW_DECODER_HACK)
1797                    || mSinceLastDropped > FRAME_DROP_FREQ)
1798            {
1799                LOGV("we're late by %lld us (%.2f secs) dropping "
1800                     "one after %d frames",
1801                     latenessUs, latenessUs / 1E6, mSinceLastDropped);
1802
1803                mSinceLastDropped = 0;
1804                mVideoBuffer->release();
1805                mVideoBuffer = NULL;
1806
1807                {
1808                    Mutex::Autolock autoLock(mStatsLock);
1809                    ++mStats.mNumVideoFramesDropped;
1810                }
1811
1812                postVideoEvent_l();
1813                return;
1814            }
1815        }
1816
1817        if (latenessUs < -10000) {
1818            // We're more than 10ms early.
1819
1820            postVideoEvent_l(10000);
1821            return;
1822        }
1823    }
1824
1825    if ((mNativeWindow != NULL)
1826            && (mVideoRendererIsPreview || mVideoRenderer == NULL)) {
1827        mVideoRendererIsPreview = false;
1828
1829        initRenderer_l();
1830    }
1831
1832    if (mVideoRenderer != NULL) {
1833        mSinceLastDropped++;
1834        mVideoRenderer->render(mVideoBuffer);
1835    }
1836
1837    mVideoBuffer->release();
1838    mVideoBuffer = NULL;
1839
1840    if (wasSeeking != NO_SEEK && (mFlags & SEEK_PREVIEW)) {
1841        modifyFlags(SEEK_PREVIEW, CLEAR);
1842        return;
1843    }
1844
1845    postVideoEvent_l();
1846}
1847
1848void AwesomePlayer::postVideoEvent_l(int64_t delayUs) {
1849    if (mVideoEventPending) {
1850        return;
1851    }
1852
1853    mVideoEventPending = true;
1854    mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs);
1855}
1856
1857void AwesomePlayer::postStreamDoneEvent_l(status_t status) {
1858    if (mStreamDoneEventPending) {
1859        return;
1860    }
1861    mStreamDoneEventPending = true;
1862
1863    mStreamDoneStatus = status;
1864    mQueue.postEvent(mStreamDoneEvent);
1865}
1866
1867void AwesomePlayer::postBufferingEvent_l() {
1868    if (mBufferingEventPending) {
1869        return;
1870    }
1871    mBufferingEventPending = true;
1872    mQueue.postEventWithDelay(mBufferingEvent, 1000000ll);
1873}
1874
1875void AwesomePlayer::postVideoLagEvent_l() {
1876    if (mVideoLagEventPending) {
1877        return;
1878    }
1879    mVideoLagEventPending = true;
1880    mQueue.postEventWithDelay(mVideoLagEvent, 1000000ll);
1881}
1882
1883void AwesomePlayer::postCheckAudioStatusEvent(int64_t delayUs) {
1884    Mutex::Autolock autoLock(mAudioLock);
1885    if (mAudioStatusEventPending) {
1886        return;
1887    }
1888    mAudioStatusEventPending = true;
1889    mQueue.postEventWithDelay(mCheckAudioStatusEvent, delayUs);
1890}
1891
1892void AwesomePlayer::onCheckAudioStatus() {
1893    {
1894        Mutex::Autolock autoLock(mAudioLock);
1895        if (!mAudioStatusEventPending) {
1896            // Event was dispatched and while we were blocking on the mutex,
1897            // has already been cancelled.
1898            return;
1899        }
1900
1901        mAudioStatusEventPending = false;
1902    }
1903
1904    Mutex::Autolock autoLock(mLock);
1905
1906    if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) {
1907        mWatchForAudioSeekComplete = false;
1908
1909        if (!mSeekNotificationSent) {
1910            notifyListener_l(MEDIA_SEEK_COMPLETE);
1911            mSeekNotificationSent = true;
1912        }
1913
1914        mSeeking = NO_SEEK;
1915    }
1916
1917    status_t finalStatus;
1918    if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) {
1919        mWatchForAudioEOS = false;
1920        modifyFlags(AUDIO_AT_EOS, SET);
1921        modifyFlags(FIRST_FRAME, SET);
1922        postStreamDoneEvent_l(finalStatus);
1923    }
1924}
1925
1926status_t AwesomePlayer::prepare() {
1927    Mutex::Autolock autoLock(mLock);
1928    return prepare_l();
1929}
1930
1931status_t AwesomePlayer::prepare_l() {
1932    if (mFlags & PREPARED) {
1933        return OK;
1934    }
1935
1936    if (mFlags & PREPARING) {
1937        return UNKNOWN_ERROR;
1938    }
1939
1940    mIsAsyncPrepare = false;
1941    status_t err = prepareAsync_l();
1942
1943    if (err != OK) {
1944        return err;
1945    }
1946
1947    while (mFlags & PREPARING) {
1948        mPreparedCondition.wait(mLock);
1949    }
1950
1951    return mPrepareResult;
1952}
1953
1954status_t AwesomePlayer::prepareAsync() {
1955    Mutex::Autolock autoLock(mLock);
1956
1957    if (mFlags & PREPARING) {
1958        return UNKNOWN_ERROR;  // async prepare already pending
1959    }
1960
1961    mIsAsyncPrepare = true;
1962    return prepareAsync_l();
1963}
1964
1965status_t AwesomePlayer::prepareAsync_l() {
1966    if (mFlags & PREPARING) {
1967        return UNKNOWN_ERROR;  // async prepare already pending
1968    }
1969
1970    if (!mQueueStarted) {
1971        mQueue.start();
1972        mQueueStarted = true;
1973    }
1974
1975    modifyFlags(PREPARING, SET);
1976    mAsyncPrepareEvent = new AwesomeEvent(
1977            this, &AwesomePlayer::onPrepareAsyncEvent);
1978
1979    mQueue.postEvent(mAsyncPrepareEvent);
1980
1981    return OK;
1982}
1983
1984status_t AwesomePlayer::finishSetDataSource_l() {
1985    sp<DataSource> dataSource;
1986
1987    bool isWidevineStreaming = false;
1988    if (!strncasecmp("widevine://", mUri.string(), 11)) {
1989        isWidevineStreaming = true;
1990
1991        String8 newURI = String8("http://");
1992        newURI.append(mUri.string() + 11);
1993
1994        mUri = newURI;
1995    }
1996
1997    if (!strncasecmp("http://", mUri.string(), 7)
1998            || !strncasecmp("https://", mUri.string(), 8)
1999            || isWidevineStreaming) {
2000        mConnectingDataSource = HTTPBase::Create(
2001                (mFlags & INCOGNITO)
2002                    ? HTTPBase::kFlagIncognito
2003                    : 0);
2004
2005        if (mUIDValid) {
2006            mConnectingDataSource->setUID(mUID);
2007        }
2008
2009        String8 cacheConfig;
2010        bool disconnectAtHighwatermark;
2011        NuCachedSource2::RemoveCacheSpecificHeaders(
2012                &mUriHeaders, &cacheConfig, &disconnectAtHighwatermark);
2013
2014        mLock.unlock();
2015        status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders);
2016        mLock.lock();
2017
2018        if (err != OK) {
2019            mConnectingDataSource.clear();
2020
2021            LOGI("mConnectingDataSource->connect() returned %d", err);
2022            return err;
2023        }
2024
2025        if (!isWidevineStreaming) {
2026            // The widevine extractor does its own caching.
2027
2028#if 0
2029            mCachedSource = new NuCachedSource2(
2030                    new ThrottledSource(
2031                        mConnectingDataSource, 50 * 1024 /* bytes/sec */));
2032#else
2033            mCachedSource = new NuCachedSource2(
2034                    mConnectingDataSource,
2035                    cacheConfig.isEmpty() ? NULL : cacheConfig.string(),
2036                    disconnectAtHighwatermark);
2037#endif
2038
2039            dataSource = mCachedSource;
2040        } else {
2041            dataSource = mConnectingDataSource;
2042        }
2043
2044        mConnectingDataSource.clear();
2045
2046
2047        String8 contentType = dataSource->getMIMEType();
2048
2049        if (strncasecmp(contentType.string(), "audio/", 6)) {
2050            // We're not doing this for streams that appear to be audio-only
2051            // streams to ensure that even low bandwidth streams start
2052            // playing back fairly instantly.
2053
2054            // We're going to prefill the cache before trying to instantiate
2055            // the extractor below, as the latter is an operation that otherwise
2056            // could block on the datasource for a significant amount of time.
2057            // During that time we'd be unable to abort the preparation phase
2058            // without this prefill.
2059            if (mCachedSource != NULL) {
2060                // We're going to prefill the cache before trying to instantiate
2061                // the extractor below, as the latter is an operation that otherwise
2062                // could block on the datasource for a significant amount of time.
2063                // During that time we'd be unable to abort the preparation phase
2064                // without this prefill.
2065
2066                mLock.unlock();
2067
2068                for (;;) {
2069                    status_t finalStatus;
2070                    size_t cachedDataRemaining =
2071                        mCachedSource->approxDataRemaining(&finalStatus);
2072
2073                    if (finalStatus != OK || cachedDataRemaining >= kHighWaterMarkBytes
2074                            || (mFlags & PREPARE_CANCELLED)) {
2075                        break;
2076                    }
2077
2078                    usleep(200000);
2079                }
2080
2081                mLock.lock();
2082            }
2083
2084            if (mFlags & PREPARE_CANCELLED) {
2085                LOGI("Prepare cancelled while waiting for initial cache fill.");
2086                return UNKNOWN_ERROR;
2087            }
2088        }
2089    } else if (!strncasecmp("rtsp://", mUri.string(), 7)) {
2090        if (mLooper == NULL) {
2091            mLooper = new ALooper;
2092            mLooper->setName("rtsp");
2093            mLooper->start();
2094        }
2095        mRTSPController = new ARTSPController(mLooper);
2096        mConnectingRTSPController = mRTSPController;
2097
2098        if (mUIDValid) {
2099            mConnectingRTSPController->setUID(mUID);
2100        }
2101
2102        mLock.unlock();
2103        status_t err = mRTSPController->connect(mUri.string());
2104        mLock.lock();
2105
2106        mConnectingRTSPController.clear();
2107
2108        LOGI("ARTSPController::connect returned %d", err);
2109
2110        if (err != OK) {
2111            mRTSPController.clear();
2112            return err;
2113        }
2114
2115        sp<MediaExtractor> extractor = mRTSPController.get();
2116        return setDataSource_l(extractor);
2117    } else {
2118        dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders);
2119    }
2120
2121    if (dataSource == NULL) {
2122        return UNKNOWN_ERROR;
2123    }
2124
2125    sp<MediaExtractor> extractor;
2126
2127    if (isWidevineStreaming) {
2128        String8 mimeType;
2129        float confidence;
2130        sp<AMessage> dummy;
2131        bool success = SniffDRM(dataSource, &mimeType, &confidence, &dummy);
2132
2133        if (!success
2134                || strcasecmp(
2135                    mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) {
2136            return ERROR_UNSUPPORTED;
2137        }
2138
2139        mWVMExtractor = new WVMExtractor(dataSource);
2140        mWVMExtractor->setAdaptiveStreamingMode(true);
2141        extractor = mWVMExtractor;
2142    } else {
2143        extractor = MediaExtractor::Create(dataSource);
2144
2145        if (extractor == NULL) {
2146            return UNKNOWN_ERROR;
2147        }
2148    }
2149
2150    dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
2151
2152    if (mDecryptHandle != NULL) {
2153        CHECK(mDrmManagerClient);
2154        if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
2155            notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2156        }
2157    }
2158
2159    status_t err = setDataSource_l(extractor);
2160
2161    if (err != OK) {
2162        mWVMExtractor.clear();
2163
2164        return err;
2165    }
2166
2167    return OK;
2168}
2169
2170void AwesomePlayer::abortPrepare(status_t err) {
2171    CHECK(err != OK);
2172
2173    if (mIsAsyncPrepare) {
2174        notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
2175    }
2176
2177    mPrepareResult = err;
2178    modifyFlags((PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED), CLEAR);
2179    mAsyncPrepareEvent = NULL;
2180    mPreparedCondition.broadcast();
2181}
2182
2183// static
2184bool AwesomePlayer::ContinuePreparation(void *cookie) {
2185    AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie);
2186
2187    return (me->mFlags & PREPARE_CANCELLED) == 0;
2188}
2189
2190void AwesomePlayer::onPrepareAsyncEvent() {
2191    Mutex::Autolock autoLock(mLock);
2192
2193    if (mFlags & PREPARE_CANCELLED) {
2194        LOGI("prepare was cancelled before doing anything");
2195        abortPrepare(UNKNOWN_ERROR);
2196        return;
2197    }
2198
2199    if (mUri.size() > 0) {
2200        status_t err = finishSetDataSource_l();
2201
2202        if (err != OK) {
2203            abortPrepare(err);
2204            return;
2205        }
2206    }
2207
2208    if (mVideoTrack != NULL && mVideoSource == NULL) {
2209        status_t err = initVideoDecoder();
2210
2211        if (err != OK) {
2212            abortPrepare(err);
2213            return;
2214        }
2215    }
2216
2217    if (mAudioTrack != NULL && mAudioSource == NULL) {
2218        status_t err = initAudioDecoder();
2219
2220        if (err != OK) {
2221            abortPrepare(err);
2222            return;
2223        }
2224    }
2225
2226    modifyFlags(PREPARING_CONNECTED, SET);
2227
2228    if (isStreamingHTTP() || mRTSPController != NULL) {
2229        postBufferingEvent_l();
2230    } else {
2231        finishAsyncPrepare_l();
2232    }
2233}
2234
2235void AwesomePlayer::finishAsyncPrepare_l() {
2236    if (mIsAsyncPrepare) {
2237        if (mVideoSource == NULL) {
2238            notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0);
2239        } else {
2240            notifyVideoSize_l();
2241        }
2242
2243        notifyListener_l(MEDIA_PREPARED);
2244    }
2245
2246    mPrepareResult = OK;
2247    modifyFlags((PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED), CLEAR);
2248    modifyFlags(PREPARED, SET);
2249    mAsyncPrepareEvent = NULL;
2250    mPreparedCondition.broadcast();
2251}
2252
2253uint32_t AwesomePlayer::flags() const {
2254    return mExtractorFlags;
2255}
2256
2257void AwesomePlayer::postAudioEOS(int64_t delayUs) {
2258    postCheckAudioStatusEvent(delayUs);
2259}
2260
2261void AwesomePlayer::postAudioSeekComplete() {
2262    postCheckAudioStatusEvent(0);
2263}
2264
2265status_t AwesomePlayer::setParameter(int key, const Parcel &request) {
2266    switch (key) {
2267        case KEY_PARAMETER_TIMED_TEXT_TRACK_INDEX:
2268        {
2269            Mutex::Autolock autoLock(mTimedTextLock);
2270            return setTimedTextTrackIndex(request.readInt32());
2271        }
2272        case KEY_PARAMETER_TIMED_TEXT_ADD_OUT_OF_BAND_SOURCE:
2273        {
2274            Mutex::Autolock autoLock(mTimedTextLock);
2275            if (mTextPlayer == NULL) {
2276                mTextPlayer = new TimedTextPlayer(this, mListener, &mQueue);
2277            }
2278
2279            return mTextPlayer->setParameter(key, request);
2280        }
2281        case KEY_PARAMETER_CACHE_STAT_COLLECT_FREQ_MS:
2282        {
2283            return setCacheStatCollectFreq(request);
2284        }
2285        default:
2286        {
2287            return ERROR_UNSUPPORTED;
2288        }
2289    }
2290}
2291
2292status_t AwesomePlayer::setCacheStatCollectFreq(const Parcel &request) {
2293    if (mCachedSource != NULL) {
2294        int32_t freqMs = request.readInt32();
2295        LOGD("Request to keep cache stats in the past %d ms",
2296            freqMs);
2297        return mCachedSource->setCacheStatCollectFreq(freqMs);
2298    }
2299    return ERROR_UNSUPPORTED;
2300}
2301
2302status_t AwesomePlayer::getParameter(int key, Parcel *reply) {
2303    switch (key) {
2304    case KEY_PARAMETER_AUDIO_CHANNEL_COUNT:
2305        {
2306            int32_t channelCount;
2307            if (mAudioTrack == 0 ||
2308                    !mAudioTrack->getFormat()->findInt32(kKeyChannelCount, &channelCount)) {
2309                channelCount = 0;
2310            }
2311            reply->writeInt32(channelCount);
2312        }
2313        return OK;
2314    default:
2315        {
2316            return ERROR_UNSUPPORTED;
2317        }
2318    }
2319}
2320
2321bool AwesomePlayer::isStreamingHTTP() const {
2322    return mCachedSource != NULL || mWVMExtractor != NULL;
2323}
2324
2325status_t AwesomePlayer::dump(int fd, const Vector<String16> &args) const {
2326    Mutex::Autolock autoLock(mStatsLock);
2327
2328    FILE *out = fdopen(dup(fd), "w");
2329
2330    fprintf(out, " AwesomePlayer\n");
2331    if (mStats.mFd < 0) {
2332        fprintf(out, "  URI(%s)", mStats.mURI.string());
2333    } else {
2334        fprintf(out, "  fd(%d)", mStats.mFd);
2335    }
2336
2337    fprintf(out, ", flags(0x%08x)", mStats.mFlags);
2338
2339    if (mStats.mBitrate >= 0) {
2340        fprintf(out, ", bitrate(%lld bps)", mStats.mBitrate);
2341    }
2342
2343    fprintf(out, "\n");
2344
2345    for (size_t i = 0; i < mStats.mTracks.size(); ++i) {
2346        const TrackStat &stat = mStats.mTracks.itemAt(i);
2347
2348        fprintf(out, "  Track %d\n", i + 1);
2349        fprintf(out, "   MIME(%s)", stat.mMIME.string());
2350
2351        if (!stat.mDecoderName.isEmpty()) {
2352            fprintf(out, ", decoder(%s)", stat.mDecoderName.string());
2353        }
2354
2355        fprintf(out, "\n");
2356
2357        if ((ssize_t)i == mStats.mVideoTrackIndex) {
2358            fprintf(out,
2359                    "   videoDimensions(%d x %d), "
2360                    "numVideoFramesDecoded(%lld), "
2361                    "numVideoFramesDropped(%lld)\n",
2362                    mStats.mVideoWidth,
2363                    mStats.mVideoHeight,
2364                    mStats.mNumVideoFramesDecoded,
2365                    mStats.mNumVideoFramesDropped);
2366        }
2367    }
2368
2369    fclose(out);
2370    out = NULL;
2371
2372    return OK;
2373}
2374
2375void AwesomePlayer::modifyFlags(unsigned value, FlagMode mode) {
2376    switch (mode) {
2377        case SET:
2378            mFlags |= value;
2379            break;
2380        case CLEAR:
2381            mFlags &= ~value;
2382            break;
2383        case ASSIGN:
2384            mFlags = value;
2385            break;
2386        default:
2387            TRESPASS();
2388    }
2389
2390    {
2391        Mutex::Autolock autoLock(mStatsLock);
2392        mStats.mFlags = mFlags;
2393    }
2394}
2395
2396}  // namespace android
2397