[go: nahoru, domu]

1/*
2 * Copyright (C) 2013 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//#define LOG_NDEBUG 0
18#define LOG_TAG "MediaHTTP"
19#include <utils/Log.h>
20
21#include <media/stagefright/MediaHTTP.h>
22
23#include <binder/IServiceManager.h>
24#include <media/stagefright/foundation/ADebug.h>
25#include <media/stagefright/foundation/ALooper.h>
26#include <media/stagefright/Utils.h>
27
28#include <media/IMediaHTTPConnection.h>
29
30namespace android {
31
32MediaHTTP::MediaHTTP(const sp<IMediaHTTPConnection> &conn)
33    : mInitCheck((conn != NULL) ? OK : NO_INIT),
34      mHTTPConnection(conn),
35      mCachedSizeValid(false),
36      mCachedSize(0ll),
37      mDrmManagerClient(NULL) {
38}
39
40MediaHTTP::~MediaHTTP() {
41    clearDRMState_l();
42}
43
44status_t MediaHTTP::connect(
45        const char *uri,
46        const KeyedVector<String8, String8> *headers,
47        off64_t /* offset */) {
48    if (mInitCheck != OK) {
49        return mInitCheck;
50    }
51
52    KeyedVector<String8, String8> extHeaders;
53    if (headers != NULL) {
54        extHeaders = *headers;
55    }
56
57    if (extHeaders.indexOfKey(String8("User-Agent")) < 0) {
58        extHeaders.add(String8("User-Agent"), String8(MakeUserAgent().c_str()));
59    }
60
61    bool success = mHTTPConnection->connect(uri, &extHeaders);
62
63    mLastHeaders = extHeaders;
64    mLastURI = uri;
65
66    mCachedSizeValid = false;
67
68    if (success) {
69        AString sanitized = uriDebugString(uri);
70        mName = String8::format("MediaHTTP(%s)", sanitized.c_str());
71    }
72
73    return success ? OK : UNKNOWN_ERROR;
74}
75
76void MediaHTTP::disconnect() {
77    mName = String8("MediaHTTP(<disconnected>)");
78    if (mInitCheck != OK) {
79        return;
80    }
81
82    mHTTPConnection->disconnect();
83}
84
85status_t MediaHTTP::initCheck() const {
86    return mInitCheck;
87}
88
89ssize_t MediaHTTP::readAt(off64_t offset, void *data, size_t size) {
90    if (mInitCheck != OK) {
91        return mInitCheck;
92    }
93
94    int64_t startTimeUs = ALooper::GetNowUs();
95
96    size_t numBytesRead = 0;
97    while (numBytesRead < size) {
98        size_t copy = size - numBytesRead;
99
100        if (copy > 64 * 1024) {
101            // limit the buffer sizes transferred across binder boundaries
102            // to avoid spurious transaction failures.
103            copy = 64 * 1024;
104        }
105
106        ssize_t n = mHTTPConnection->readAt(
107                offset + numBytesRead, (uint8_t *)data + numBytesRead, copy);
108
109        if (n < 0) {
110            return n;
111        } else if (n == 0) {
112            break;
113        }
114
115        numBytesRead += n;
116    }
117
118    int64_t delayUs = ALooper::GetNowUs() - startTimeUs;
119
120    addBandwidthMeasurement(numBytesRead, delayUs);
121
122    return numBytesRead;
123}
124
125status_t MediaHTTP::getSize(off64_t *size) {
126    if (mInitCheck != OK) {
127        return mInitCheck;
128    }
129
130    // Caching the returned size so that it stays valid even after a
131    // disconnect. NuCachedSource2 relies on this.
132
133    if (!mCachedSizeValid) {
134        mCachedSize = mHTTPConnection->getSize();
135        mCachedSizeValid = true;
136    }
137
138    *size = mCachedSize;
139
140    return *size < 0 ? *size : static_cast<status_t>(OK);
141}
142
143uint32_t MediaHTTP::flags() {
144    return kWantsPrefetching | kIsHTTPBasedSource;
145}
146
147status_t MediaHTTP::reconnectAtOffset(off64_t offset) {
148    return connect(mLastURI.c_str(), &mLastHeaders, offset);
149}
150
151// DRM...
152
153sp<DecryptHandle> MediaHTTP::DrmInitialization(const char* mime) {
154    if (mDrmManagerClient == NULL) {
155        mDrmManagerClient = new DrmManagerClient();
156    }
157
158    if (mDrmManagerClient == NULL) {
159        return NULL;
160    }
161
162    if (mDecryptHandle == NULL) {
163        mDecryptHandle = mDrmManagerClient->openDecryptSession(
164                String8(mLastURI.c_str()), mime);
165    }
166
167    if (mDecryptHandle == NULL) {
168        delete mDrmManagerClient;
169        mDrmManagerClient = NULL;
170    }
171
172    return mDecryptHandle;
173}
174
175void MediaHTTP::getDrmInfo(
176        sp<DecryptHandle> &handle, DrmManagerClient **client) {
177    handle = mDecryptHandle;
178    *client = mDrmManagerClient;
179}
180
181String8 MediaHTTP::getUri() {
182    if (mInitCheck != OK) {
183        return String8::empty();
184    }
185
186    String8 uri;
187    if (OK == mHTTPConnection->getUri(&uri)) {
188        return uri;
189    }
190    return String8(mLastURI.c_str());
191}
192
193String8 MediaHTTP::getMIMEType() const {
194    if (mInitCheck != OK) {
195        return String8("application/octet-stream");
196    }
197
198    String8 mimeType;
199    status_t err = mHTTPConnection->getMIMEType(&mimeType);
200
201    if (err != OK) {
202        return String8("application/octet-stream");
203    }
204
205    return mimeType;
206}
207
208void MediaHTTP::clearDRMState_l() {
209    if (mDecryptHandle != NULL) {
210        // To release mDecryptHandle
211        CHECK(mDrmManagerClient);
212        mDrmManagerClient->closeDecryptSession(mDecryptHandle);
213        mDecryptHandle = NULL;
214    }
215}
216
217}  // namespace android
218