1bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber/*
2bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * Copyright (C) 2009 The Android Open Source Project
3bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber *
4bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * you may not use this file except in compliance with the License.
6bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * You may obtain a copy of the License at
7bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber *
8bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
9bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber *
10bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * Unless required by applicable law or agreed to in writing, software
11bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * See the License for the specific language governing permissions and
14bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber * limitations under the License.
15bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber */
16bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
176c00983982d167bdb348c437f0480a480f38696cGlenn Kasten#undef DEBUG_HDCP
18eabd34665adc284525fe7337ad5c56a8b92964efGlenn Kasten
19bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber//#define LOG_NDEBUG 0
20bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#define LOG_TAG "AwesomePlayer"
212163320110d2f971becbad82e15b2ab360a1ddb6Jamie Gennis#define ATRACE_TAG ATRACE_TAG_VIDEO
22a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn
23377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT#include <inttypes.h>
24a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn
25bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <utils/Log.h>
262163320110d2f971becbad82e15b2ab360a1ddb6Jamie Gennis#include <utils/Trace.h>
27bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
28988e3f0b2c74095deae580157c57935a98573052Andreas Huber#include <dlfcn.h>
29988e3f0b2c74095deae580157c57935a98573052Andreas Huber
30bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include "include/AwesomePlayer.h"
31681755fc0d0797506456f46a2a10555916d6be32Andreas Huber#include "include/DRMExtractor.h"
32733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber#include "include/SoftwareRenderer.h"
330a5baa9b411fe086013d2a5e9126ed63fbad046cAndreas Huber#include "include/NuCachedSource2.h"
340a5baa9b411fe086013d2a5e9126ed63fbad046cAndreas Huber#include "include/ThrottledSource.h"
35bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber#include "include/MPEG2TSExtractor.h"
36681755fc0d0797506456f46a2a10555916d6be32Andreas Huber#include "include/WVMExtractor.h"
37bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
384844ac54e8b5997c3b03872dbafe8ebed4787517Andreas Huber#include <binder/IPCThreadState.h>
397cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang#include <binder/IServiceManager.h>
401b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <media/IMediaHTTPConnection.h>
411b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <media/IMediaHTTPService.h>
427cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang#include <media/IMediaPlayerService.h>
43e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber#include <media/stagefright/foundation/hexdump.h>
44e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber#include <media/stagefright/foundation/ADebug.h>
453254b25e8b0f674ccc2226609e01dd86a600802eInsun Kang#include <media/stagefright/timedtext/TimedTextDriver.h>
46bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/AudioPlayer.h>
471b78c4b1484c7d4c12b9a87329dc8d4b6e8c0c37Lajos Molnar#include <media/stagefright/ClockEstimator.h>
48bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/DataSource.h>
49bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/FileSource.h>
50bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/MediaBuffer.h>
51717826ececd8d39596f62418677721d70776add1Andreas Huber#include <media/stagefright/MediaDefs.h>
52bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/MediaExtractor.h>
531b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <media/stagefright/MediaHTTP.h>
54bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/MediaSource.h>
55bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/MetaData.h>
56bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber#include <media/stagefright/OMXCodec.h>
5794ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald#include <media/stagefright/Utils.h>
58717826ececd8d39596f62418677721d70776add1Andreas Huber
598ba01021b573889802e67e029225a96f0dfa471aAndy McFadden#include <gui/IGraphicBufferProducer.h>
601a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian#include <gui/Surface.h>
613cf613507f1e2f7bd932d921a6e222e426fd3be4Mathias Agopian
6214acc736e336cbd6026df781d4f411e908831815Andreas Huber#include <media/stagefright/foundation/AMessage.h>
63e71d10e7ad55ccbcb0756c007caef1c959090384Andreas Huber
64eabd34665adc284525fe7337ad5c56a8b92964efGlenn Kasten#include <cutils/properties.h>
65eabd34665adc284525fe7337ad5c56a8b92964efGlenn Kasten
66e332a9181cf6a3155ed1a0fd2afc212ccb1f2753Andreas Huber#define USE_SURFACE_ALLOC 1
6714f95746466fb6abdbbf33ee26d92ebf3fbef98fJames Dong#define FRAME_DROP_FREQ 0
68e332a9181cf6a3155ed1a0fd2afc212ccb1f2753Andreas Huber
69bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Hubernamespace android {
70bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
718650e1960905097f6f1d6aa462ccb1c93c656834Andreas Huberstatic int64_t kLowWaterMarkUs = 2000000ll;  // 2secs
7283ed9d41b5aea53a5f3f0ae2fa14e101c079a12aGloria Wangstatic int64_t kHighWaterMarkUs = 5000000ll;  // 5secs
73ac05c317cd818701535c5d72ce90da98c4bae75bAndreas Huberstatic const size_t kLowWaterMarkBytes = 40000;
74ac05c317cd818701535c5d72ce90da98c4bae75bAndreas Huberstatic const size_t kHighWaterMarkBytes = 200000;
758650e1960905097f6f1d6aa462ccb1c93c656834Andreas Huber
7694ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald// maximum time in paused state when offloading audio decompression. When elapsed, the AudioPlayer
7794ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald// is destroyed to allow the audio DSP to power down.
78a5d316fd802cfc92954527f27e6f32206a896113Eric Laurentstatic int64_t kOffloadPauseMaxUs = 10000000ll;
7994ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald
8094ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald
81bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huberstruct AwesomeEvent : public TimedEventQueue::Event {
82c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    AwesomeEvent(
83c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber            AwesomePlayer *player,
84c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber            void (AwesomePlayer::*method)())
85bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        : mPlayer(player),
86c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber          mMethod(method) {
87bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    }
88bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
89bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huberprotected:
90bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    virtual ~AwesomeEvent() {}
91bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
9284333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber    virtual void fire(TimedEventQueue * /* queue */, int64_t /* now_us */) {
93c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber        (mPlayer->*mMethod)();
94bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    }
95bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
96bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huberprivate:
97bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    AwesomePlayer *mPlayer;
98c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    void (AwesomePlayer::*mMethod)();
99bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
100bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    AwesomeEvent(const AwesomeEvent &);
101bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    AwesomeEvent &operator=(const AwesomeEvent &);
102bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber};
103bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
104733b7729ea462fae9c6899456444e28fef1c757cAndreas Huberstruct AwesomeLocalRenderer : public AwesomeRenderer {
105733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    AwesomeLocalRenderer(
106475da7ef08c05a482ced8d318832cf5aa1141affRonghua Wu            const sp<ANativeWindow> &nativeWindow, const sp<AMessage> &format)
107475da7ef08c05a482ced8d318832cf5aa1141affRonghua Wu        : mFormat(format),
108475da7ef08c05a482ced8d318832cf5aa1141affRonghua Wu          mTarget(new SoftwareRenderer(nativeWindow)) {
109733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    }
110733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber
111733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    virtual void render(MediaBuffer *buffer) {
112908ea6603c64e29e40277a74c9fb0e776c097830Lajos Molnar        int64_t timeUs;
113908ea6603c64e29e40277a74c9fb0e776c097830Lajos Molnar        CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
114908ea6603c64e29e40277a74c9fb0e776c097830Lajos Molnar
115fa090f541bb84a546af2ec834e91a032ff10e66eAndreas Huber        render((const uint8_t *)buffer->data() + buffer->range_offset(),
116908ea6603c64e29e40277a74c9fb0e776c097830Lajos Molnar               buffer->range_length(), timeUs * 1000);
117fa090f541bb84a546af2ec834e91a032ff10e66eAndreas Huber    }
118fa090f541bb84a546af2ec834e91a032ff10e66eAndreas Huber
119908ea6603c64e29e40277a74c9fb0e776c097830Lajos Molnar    void render(const void *data, size_t size, int64_t timestampNs) {
120475da7ef08c05a482ced8d318832cf5aa1141affRonghua Wu        mTarget->render(data, size, timestampNs, NULL, mFormat);
121733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    }
122733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber
123733b7729ea462fae9c6899456444e28fef1c757cAndreas Huberprotected:
124733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    virtual ~AwesomeLocalRenderer() {
125733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber        delete mTarget;
126733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber        mTarget = NULL;
127733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    }
128733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber
129733b7729ea462fae9c6899456444e28fef1c757cAndreas Huberprivate:
130475da7ef08c05a482ced8d318832cf5aa1141affRonghua Wu    sp<AMessage> mFormat;
1310a5ca668c6f7d45706e9aec4a1dfec0aacc6d233Andreas Huber    SoftwareRenderer *mTarget;
132988e3f0b2c74095deae580157c57935a98573052Andreas Huber
133733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    AwesomeLocalRenderer(const AwesomeLocalRenderer &);
134733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber    AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);;
135733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber};
136733b7729ea462fae9c6899456444e28fef1c757cAndreas Huber
1376a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennisstruct AwesomeNativeWindowRenderer : public AwesomeRenderer {
138ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber    AwesomeNativeWindowRenderer(
139ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            const sp<ANativeWindow> &nativeWindow,
140ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            int32_t rotationDegrees)
1416a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis        : mNativeWindow(nativeWindow) {
142ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber        applyRotation(rotationDegrees);
1436a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    }
1446a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis
1456a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    virtual void render(MediaBuffer *buffer) {
1462163320110d2f971becbad82e15b2ab360a1ddb6Jamie Gennis        ATRACE_CALL();
147593e2773f616a926af45e74359e21a898c89875fGlenn Kasten        int64_t timeUs;
148593e2773f616a926af45e74359e21a898c89875fGlenn Kasten        CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
149593e2773f616a926af45e74359e21a898c89875fGlenn Kasten        native_window_set_buffers_timestamp(mNativeWindow.get(), timeUs * 1000);
1506a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis        status_t err = mNativeWindow->queueBuffer(
1511e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis                mNativeWindow.get(), buffer->graphicBuffer().get(), -1);
1526a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis        if (err != 0) {
15329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block            ALOGE("queueBuffer failed with error %s (%d)", strerror(-err),
1546a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis                    -err);
1556a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis            return;
1566a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis        }
1576a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis
1586a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis        sp<MetaData> metaData = buffer->meta_data();
1596a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis        metaData->setInt32(kKeyRendered, 1);
1606a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    }
1616a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis
1626a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennisprotected:
1636a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    virtual ~AwesomeNativeWindowRenderer() {}
1646a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis
1656a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennisprivate:
1666a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    sp<ANativeWindow> mNativeWindow;
1676a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis
168ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber    void applyRotation(int32_t rotationDegrees) {
169ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber        uint32_t transform;
170ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber        switch (rotationDegrees) {
171ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            case 0: transform = 0; break;
172ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            case 90: transform = HAL_TRANSFORM_ROT_90; break;
173ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            case 180: transform = HAL_TRANSFORM_ROT_180; break;
174ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            case 270: transform = HAL_TRANSFORM_ROT_270; break;
175ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            default: transform = 0; break;
176ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber        }
177ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber
178ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber        if (transform) {
179ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber            CHECK_EQ(0, native_window_set_buffers_transform(
180ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber                        mNativeWindow.get(), transform));
181ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber        }
182ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber    }
183ba529e486dde15fe75ea1b48570f9d60a4cf3c48Andreas Huber
1846a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    AwesomeNativeWindowRenderer(const AwesomeNativeWindowRenderer &);
1856a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis    AwesomeNativeWindowRenderer &operator=(
1866a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis            const AwesomeNativeWindowRenderer &);
1876a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis};
1886a9da9fc558263548ebfbae2cbf177eb7454a41bJamie Gennis
1897cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang// To collect the decoder usage
1907cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wangvoid addBatteryData(uint32_t params) {
1917cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    sp<IBinder> binder =
1927cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        defaultServiceManager()->getService(String16("media.player"));
1937cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
1947cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    CHECK(service.get() != NULL);
1957cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang
1967cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    service->addBatteryData(params);
1977cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang}
198e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber
1997cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang////////////////////////////////////////////////////////////////////////////////
200bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas HuberAwesomePlayer::AwesomePlayer()
2012e8ffaf95bd35358ecd14ddf00cddaf3abefe0a9Andreas Huber    : mQueueStarted(false),
2029b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber      mUIDValid(false),
2032e8ffaf95bd35358ecd14ddf00cddaf3abefe0a9Andreas Huber      mTimeSource(NULL),
204c374dae535b83d9b499dd9fe8f9dabdb8b0ac9fdJames Dong      mVideoRenderingStarted(false),
205fa090f541bb84a546af2ec834e91a032ff10e66eAndreas Huber      mVideoRendererIsPreview(false),
206cbaffcffee6418d678806e63097c19fe26d48fe0Lajos Molnar      mMediaRenderingStartGeneration(0),
207cbaffcffee6418d678806e63097c19fe26d48fe0Lajos Molnar      mStartGeneration(0),
208bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber      mAudioPlayer(NULL),
209b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong      mDisplayWidth(0),
210b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong      mDisplayHeight(0),
2114f6eed0d1c7972a983c075bdcf03089569e13fe1James Dong      mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
212bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber      mFlags(0),
213acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber      mExtractorFlags(0),
2147b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber      mVideoBuffer(NULL),
215150694cda0842d38a9358fe8d1fcb4fb4a76599eAndreas Huber      mDecryptHandle(NULL),
2167a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang      mLastVideoTimeUs(-1),
21794ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald      mTextDriver(NULL),
21894ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald      mOffloadAudio(false),
21994ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald      mAudioTearDown(false) {
220e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber    CHECK_EQ(mClient.connect(), (status_t)OK);
221bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
222bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    DataSource::RegisterDefaultSniffers();
223bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
224c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent);
225bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mVideoEventPending = false;
226c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone);
227bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mStreamDoneEventPending = false;
228c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate);
22966b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber    mBufferingEventPending = false;
230145e68fc778275963189b02a1adcbe27cce4d769Andreas Huber    mVideoLagEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoLagUpdate);
231f90b123a3a67316284ba4b48a4fb0c5a36158545Sungsoo Lim    mVideoLagEventPending = false;
232c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber
233c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber    mCheckAudioStatusEvent = new AwesomeEvent(
234c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber            this, &AwesomePlayer::onCheckAudioStatus);
235c23f12af0394aa2f6651968a3c8840f1af317aa1Andreas Huber
2361862a33b246249630b654182afb5914da3480d4cAndreas Huber    mAudioStatusEventPending = false;
237bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
23894ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald    mAudioTearDownEvent = new AwesomeEvent(this,
23994ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald                              &AwesomePlayer::onAudioTearDownEvent);
24094ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald    mAudioTearDownEventPending = false;
24194ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald
2421b78c4b1484c7d4c12b9a87329dc8d4b6e8c0c37Lajos Molnar    mClockEstimator = new WindowedLinearFitEstimator();
2431b78c4b1484c7d4c12b9a87329dc8d4b6e8c0c37Lajos Molnar
244bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    reset();
245bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
246bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
247bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas HuberAwesomePlayer::~AwesomePlayer() {
2482e8ffaf95bd35358ecd14ddf00cddaf3abefe0a9Andreas Huber    if (mQueueStarted) {
2492e8ffaf95bd35358ecd14ddf00cddaf3abefe0a9Andreas Huber        mQueue.stop();
2502e8ffaf95bd35358ecd14ddf00cddaf3abefe0a9Andreas Huber    }
251bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
252bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    reset();
253bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
254bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mClient.disconnect();
255bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
256bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
257c34233e673c9791e137456d0c427d58db184b690Andreas Hubervoid AwesomePlayer::cancelPlayerEvents(bool keepNotifications) {
258bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mQueue.cancelEvent(mVideoEvent->eventID());
259bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mVideoEventPending = false;
260145e68fc778275963189b02a1adcbe27cce4d769Andreas Huber    mQueue.cancelEvent(mVideoLagEvent->eventID());
261145e68fc778275963189b02a1adcbe27cce4d769Andreas Huber    mVideoLagEventPending = false;
26266b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber
26394ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald    if (mOffloadAudio) {
26494ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald        mQueue.cancelEvent(mAudioTearDownEvent->eventID());
26594ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald        mAudioTearDownEventPending = false;
26694ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald    }
26794ea60f975c3eb7ce6d2a4430538a42a5fc3babdRichard Fitzgerald
268c34233e673c9791e137456d0c427d58db184b690Andreas Huber    if (!keepNotifications) {
269c34233e673c9791e137456d0c427d58db184b690Andreas Huber        mQueue.cancelEvent(mStreamDoneEvent->eventID());
270c34233e673c9791e137456d0c427d58db184b690Andreas Huber        mStreamDoneEventPending = false;
271c34233e673c9791e137456d0c427d58db184b690Andreas Huber        mQueue.cancelEvent(mCheckAudioStatusEvent->eventID());
272c34233e673c9791e137456d0c427d58db184b690Andreas Huber        mAudioStatusEventPending = false;
273c34233e673c9791e137456d0c427d58db184b690Andreas Huber
27466b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber        mQueue.cancelEvent(mBufferingEvent->eventID());
27566b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber        mBufferingEventPending = false;
27642c5ae81036d4002da3fe1e3b1016131ba737e74Marco Nelissen        mAudioTearDown = false;
27766b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber    }
278bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
279bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
2800726045216f576e97672ebeefc265d39c4ebaaa5Andreas Hubervoid AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) {
281bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    Mutex::Autolock autoLock(mLock);
282bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    mListener = listener;
283bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
284bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
2859b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Hubervoid AwesomePlayer::setUID(uid_t uid) {
2863856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AwesomePlayer running on behalf of uid %d", uid);
2879b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
2889b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    mUID = uid;
2899b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber    mUIDValid = true;
2909b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber}
2919b80c2bdb205bc143104f54d0743b6eedd67b14eAndreas Huber
2925561ccf4a8db88a2e44eac1b3ed13b4ff53a7f20Andreas Huberstatus_t AwesomePlayer::setDataSource(
2931b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        const sp<IMediaHTTPService> &httpService,
2941b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        const char *uri,
2951b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        const KeyedVector<String8, String8> *headers) {
296bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    Mutex::Autolock autoLock(mLock);
2971b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    return setDataSource_l(httpService, uri, headers);
2987b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber}
299bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
3007b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huberstatus_t AwesomePlayer::setDataSource_l(
3011b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        const sp<IMediaHTTPService> &httpService,
3021b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        const char *uri,
3031b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        const KeyedVector<String8, String8> *headers) {
304bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    reset_l();
305bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
3061b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    mHTTPService = httpService;
307bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber    mUri = uri;
30866b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber
309bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber    if (headers) {
310bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber        mUriHeaders = *headers;
3117314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber
3127314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber        ssize_t index = mUriHeaders.indexOfKey(String8("x-hide-urls-from-log"));
3137314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber        if (index >= 0) {
3147314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber            // Browser is in "incognito" mode, suppress logging URLs.
3157314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber
3167314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber            // This isn't something that should be passed to the server.
3177314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber            mUriHeaders.removeItemsAt(index);
3187314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber
319a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            modifyFlags(INCOGNITO, SET);
3207314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber        }
3217314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber    }
3227314fa17093d514199fedcb55ac41136a1b31cb3Andreas Huber
323a1df816c0677185534babba6ffc29970b048e52eLajos Molnar    ALOGI("setDataSource_l(%s)", uriDebugString(mUri, mFlags & INCOGNITO).c_str());
32466b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber
325bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber    // The actual work will be done during preparation in the call to
326bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber    // ::finishSetDataSource_l to avoid blocking the calling thread in
327bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber    // setDataSource for any significant time.
32866b0a35c9a4136a84c3f73b1b25fcb5220138b95Andreas Huber
329a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    {
330a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        Mutex::Autolock autoLock(mStatsLock);
331a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mFd = -1;
332a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mURI = mUri;
333a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    }
334a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber
335bd22c52280179b29eefac91ef7e7bbbc33d25904Andreas Huber    return OK;
336bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
337bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
338bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huberstatus_t AwesomePlayer::setDataSource(
339bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        int fd, int64_t offset, int64_t length) {
340bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    Mutex::Autolock autoLock(mLock);
341bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
342bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    reset_l();
343bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
3447b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber    sp<DataSource> dataSource = new FileSource(fd, offset, length);
345bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
3467b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber    status_t err = dataSource->initCheck();
347bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
348bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    if (err != OK) {
349bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        return err;
350bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    }
351bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
3527b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber    mFileSource = dataSource;
3537b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber
354a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    {
355a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        Mutex::Autolock autoLock(mStatsLock);
356a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mFd = fd;
357a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mURI = String8();
358a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    }
359a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber
3607b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber    return setDataSource_l(dataSource);
3617b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber}
3627b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber
363e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huberstatus_t AwesomePlayer::setDataSource(const sp<IStreamSource> &source) {
36485704836f33a199d7e442a23db82abbd5620d35dAndreas Huber    return INVALID_OPERATION;
365e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber}
366e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber
3677b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huberstatus_t AwesomePlayer::setDataSource_l(
3687b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber        const sp<DataSource> &dataSource) {
3697b3396acc702451be9bafb19d7fe26d55d43a316Andreas Huber    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
370bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
371bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    if (extractor == NULL) {
372bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        return UNKNOWN_ERROR;
373bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    }
374bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
3759d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong    if (extractor->getDrmFlag()) {
3769d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong        checkDrmStatus(dataSource);
3779d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong    }
3789d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong
3799d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong    return setDataSource_l(extractor);
3809d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong}
3819d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong
3829d2f386dd2885eaffa11fd494ae258bb09fe6397James Dongvoid AwesomePlayer::checkDrmStatus(const sp<DataSource>& dataSource) {
383785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong    dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
384785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong    if (mDecryptHandle != NULL) {
385785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong        CHECK(mDrmManagerClient);
386785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong        if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
387785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong            notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
3888f64134f749e4f7861a08a3063450fc714c4651dGloria Wang        }
389dcd25efb46c41c8d24a0a9cf61fb57f84149709eGloria Wang    }
390bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
391bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
392bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huberstatus_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {
3937fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    // Attempt to approximate overall stream bitrate by summing all
3947fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    // tracks' individual bitrates, if not all of them advertise bitrate,
3957fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    // we have to fail.
3967fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
3977fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    int64_t totalBitRate = 0;
3987fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
399eec46abb88dcc50621fd2d3f17a6b8d24fd07a19James Dong    mExtractor = extractor;
4007fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    for (size_t i = 0; i < extractor->countTracks(); ++i) {
4017fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber        sp<MetaData> meta = extractor->getTrackMetaData(i);
4027fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
4037fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber        int32_t bitrate;
4047fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber        if (!meta->findInt32(kKeyBitRate, &bitrate)) {
405a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            const char *mime;
406a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            CHECK(meta->findCString(kKeyMIMEType, &mime));
4073856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("track of type '%s' does not publish bitrate", mime);
408a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber
4097fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber            totalBitRate = -1;
4107fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber            break;
4117fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber        }
4127fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
4137fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber        totalBitRate += bitrate;
4147fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    }
415c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen    sp<MetaData> fileMeta = mExtractor->getMetaData();
416c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen    if (fileMeta != NULL) {
417c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen        int64_t duration;
418c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen        if (fileMeta->findInt64(kKeyDuration, &duration)) {
419c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen            mDurationUs = duration;
420c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen        }
421c1f4b2b73fe9432cb75ce1dd368475a2867e70edMarco Nelissen    }
4227fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
4237fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber    mBitrate = totalBitRate;
4247fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
4253856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("mBitrate = %lld bits/sec", mBitrate);
4267fbdb0903dfbf70b314a74e64e28fb880cdb9247Andreas Huber
427a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    {
428a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        Mutex::Autolock autoLock(mStatsLock);
429a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mBitrate = mBitrate;
430a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mTracks.clear();
431a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mAudioTrackIndex = -1;
432a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber        mStats.mVideoTrackIndex = -1;
433a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber    }
434a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber
435bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    bool haveAudio = false;
436bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    bool haveVideo = false;
437bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    for (size_t i = 0; i < extractor->countTracks(); ++i) {
438bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        sp<MetaData> meta = extractor->getTrackMetaData(i);
439bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
44032bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber        const char *_mime;
44132bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber        CHECK(meta->findCString(kKeyMIMEType, &_mime));
442bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
44332bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber        String8 mime = String8(_mime);
44432bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber
44532bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber        if (!haveVideo && !strncasecmp(mime.string(), "video/", 6)) {
44688d8a83de12592635248aa1a3bd6b9ea46108501Andreas Huber            setVideoSource(extractor->getTrack(i));
44788d8a83de12592635248aa1a3bd6b9ea46108501Andreas Huber            haveVideo = true;
448b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong
449b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            // Set the presentation/display size
450b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            int32_t displayWidth, displayHeight;
451b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            bool success = meta->findInt32(kKeyDisplayWidth, &displayWidth);
452b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            if (success) {
453b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong                success = meta->findInt32(kKeyDisplayHeight, &displayHeight);
454b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            }
455b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            if (success) {
456b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong                mDisplayWidth = displayWidth;
457b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong                mDisplayHeight = displayHeight;
458b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong            }
459b45c01c2b8588ff431b511151666a55a39f0a6aeJames Dong
460a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            {
461a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                Mutex::Autolock autoLock(mStatsLock);
462a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                mStats.mVideoTrackIndex = mStats.mTracks.size();
463a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                mStats.mTracks.push();
464a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                TrackStat *stat =
465a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                    &mStats.mTracks.editItemAt(mStats.mVideoTrackIndex);
46632bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber                stat->mMIME = mime.string();
467a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            }
46832bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber        } else if (!haveAudio && !strncasecmp(mime.string(), "audio/", 6)) {
46988d8a83de12592635248aa1a3bd6b9ea46108501Andreas Huber            setAudioSource(extractor->getTrack(i));
47088d8a83de12592635248aa1a3bd6b9ea46108501Andreas Huber            haveAudio = true;
471ab7a2e544643edcb2e09ed5f204580afd763edc1James Dong            mActiveAudioTrackIndex = i;
4728ae49d87b98d57d6758b0c51b95e28a6581a79f1Andreas Huber
473a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            {
474a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                Mutex::Autolock autoLock(mStatsLock);
475a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                mStats.mAudioTrackIndex = mStats.mTracks.size();
476a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                mStats.mTracks.push();
477a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                TrackStat *stat =
478a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                    &mStats.mTracks.editItemAt(mStats.mAudioTrackIndex);
47932bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber                stat->mMIME = mime.string();
480a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber            }
481a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber
48232bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber            if (!strcasecmp(mime.string(), MEDIA_MIMETYPE_AUDIO_VORBIS)) {
4836954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                // Only do this for vorbis audio, none of the other audio
4846954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                // formats even support this ringtone specific hack and
4856954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                // retrieving the metadata on some extractors may turn out
4866954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                // to be very expensive.
4876954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                sp<MetaData> fileMeta = extractor->getMetaData();
4886954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                int32_t loop;
4896954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                if (fileMeta != NULL
4906954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                        && fileMeta->findInt32(kKeyAutoLoop, &loop) && loop != 0) {
491a0b1d4b161599c2bb2a47119e50c51e75bbe980eAndreas Huber                    modifyFlags(AUTO_LOOPING, SET);
4926954535ba64374e4e941cd83019a7af53edfb098Andreas Huber                }
4938ae49d87b98d57d6758b0c51b95e28a6581a79f1Andreas Huber            }
49432bdfd5acb76a02e82e0059c8bd892bc1f73a7e3Andreas Huber        } else if (!strcasecmp(mime.string(), MEDIA_MIMETYPE_TEXT_3GPP)) {
4956a3969cc3bcbfc84dbcc27aa726eb96bc03a830bEdwin Wong            addTextSource_l(i, extractor->getTrack(i));
496bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber        }
497bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    }
498bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
499acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    if (!haveAudio && !haveVideo) {
5008a74c9b8e2627560ae5a92d4261f0de4464490adEdwin Wong        if (mWVMExtractor != NULL) {
5018a74c9b8e2627560ae5a92d4261f0de4464490adEdwin Wong            return mWVMExtractor->getError();
5028a74c9b8e2627560ae5a92d4261f0de4464490adEdwin Wong        } else {
5038a74c9b8e2627560ae5a92d4261f0de4464490adEdwin Wong            return UNKNOWN_ERROR;
5048a74c9b8e2627560ae5a92d4261f0de4464490adEdwin Wong        }
505acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    }
506acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
507acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    mExtractorFlags = extractor->flags();
508acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber
509acdd9d0f59c3d8bdc3de80664a609527ec3d21e2Andreas Huber    return OK;
510bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
511bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
512bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Hubervoid AwesomePlayer::reset() {
513bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    Mutex::Autolock autoLock(mLock);
514bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber    reset_l();
515bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber}
516bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Huber
517bfa6b2d7a1be1832ac40ed90aece1834f720b5c6Andreas Hubervoid AwesomePlayer::reset_l() {
518c374dae535b83d9b499dd9fe8f9dabdb8b0ac9fdJames Dong    mVideoRenderingStarted = false;
519ab7a2e544643edcb2e09ed5f204580afd763edc1James Dong