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