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