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