blob: 7249c841266852c103c370d28cb21579e7a4fb88 [file] [log] [blame]
Dongwon Kang1e1bcaa2018-08-01 12:44:46 -07001/*
2 * Copyright (C) 2018 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 "ClearMediaHTTP"
19#include <utils/Log.h>
20
Marco Nelissen42057ce2019-09-23 12:15:57 -070021#include <datasource/ClearMediaHTTP.h>
Dongwon Kang1e1bcaa2018-08-01 12:44:46 -070022
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/ALooper.h>
Marco Nelissen42057ce2019-09-23 12:15:57 -070025#include <media/stagefright/FoundationUtils.h>
Dongwon Kang1e1bcaa2018-08-01 12:44:46 -070026
27#include <media/MediaHTTPConnection.h>
28
29namespace android {
30
31ClearMediaHTTP::ClearMediaHTTP(const sp<MediaHTTPConnection> &conn)
32 : mInitCheck((conn != NULL) ? OK : NO_INIT),
33 mHTTPConnection(conn),
34 mCachedSizeValid(false),
35 mCachedSize(0ll) {
36}
37
38ClearMediaHTTP::~ClearMediaHTTP() {
39}
40
41status_t ClearMediaHTTP::connect(
42 const char *uri,
43 const KeyedVector<String8, String8> *headers,
44 off64_t /* offset */) {
45 if (mInitCheck != OK) {
46 return mInitCheck;
47 }
48
49 KeyedVector<String8, String8> extHeaders;
50 if (headers != NULL) {
51 extHeaders = *headers;
52 }
53
54 if (extHeaders.indexOfKey(String8("User-Agent")) < 0) {
55 extHeaders.add(String8("User-Agent"), String8(MakeUserAgent().c_str()));
56 }
57
58 mLastURI = uri;
59 // reconnect() calls with uri == old mLastURI.c_str(), which gets zapped
60 // as part of the above assignment. Ensure no accidental later use.
61 uri = NULL;
62
63 bool success = mHTTPConnection->connect(mLastURI.c_str(), &extHeaders);
64
65 mLastHeaders = extHeaders;
66
67 mCachedSizeValid = false;
68
69 if (success) {
70 AString sanitized = uriDebugString(mLastURI);
71 mName = String8::format("ClearMediaHTTP(%s)", sanitized.c_str());
72 }
73
74 return success ? OK : UNKNOWN_ERROR;
75}
76
Robert Shih03244032018-10-02 14:36:32 -070077void ClearMediaHTTP::close() {
78 disconnect();
79}
80
Dongwon Kang1e1bcaa2018-08-01 12:44:46 -070081void ClearMediaHTTP::disconnect() {
82 mName = String8("ClearMediaHTTP(<disconnected>)");
83 if (mInitCheck != OK) {
84 return;
85 }
86
87 mHTTPConnection->disconnect();
88}
89
90status_t ClearMediaHTTP::initCheck() const {
91 return mInitCheck;
92}
93
94ssize_t ClearMediaHTTP::readAt(off64_t offset, void *data, size_t size) {
95 if (mInitCheck != OK) {
96 return mInitCheck;
97 }
98
99 int64_t startTimeUs = ALooper::GetNowUs();
100
101 size_t numBytesRead = 0;
102 while (numBytesRead < size) {
103 size_t copy = size - numBytesRead;
104
105 if (copy > 64 * 1024) {
106 // limit the buffer sizes transferred across binder boundaries
107 // to avoid spurious transaction failures.
108 copy = 64 * 1024;
109 }
110
111 ssize_t n = mHTTPConnection->readAt(
112 offset + numBytesRead, (uint8_t *)data + numBytesRead, copy);
113
114 if (n < 0) {
115 return n;
116 } else if (n == 0) {
117 break;
118 }
119
120 numBytesRead += n;
121 }
122
123 int64_t delayUs = ALooper::GetNowUs() - startTimeUs;
124
125 addBandwidthMeasurement(numBytesRead, delayUs);
126
127 return numBytesRead;
128}
129
130status_t ClearMediaHTTP::getSize(off64_t *size) {
131 if (mInitCheck != OK) {
132 return mInitCheck;
133 }
134
135 // Caching the returned size so that it stays valid even after a
136 // disconnect. NuCachedSource2 relies on this.
137
138 if (!mCachedSizeValid) {
139 mCachedSize = mHTTPConnection->getSize();
140 mCachedSizeValid = true;
141 }
142
143 *size = mCachedSize;
144
145 return *size < 0 ? *size : static_cast<status_t>(OK);
146}
147
148uint32_t ClearMediaHTTP::flags() {
149 return kWantsPrefetching | kIsHTTPBasedSource;
150}
151
152status_t ClearMediaHTTP::reconnectAtOffset(off64_t offset) {
153 return connect(mLastURI.c_str(), &mLastHeaders, offset);
154}
155
156
157String8 ClearMediaHTTP::getUri() {
158 if (mInitCheck != OK) {
159 return String8::empty();
160 }
161
162 String8 uri;
163 if (OK == mHTTPConnection->getUri(&uri)) {
164 return uri;
165 }
166 return String8(mLastURI.c_str());
167}
168
169String8 ClearMediaHTTP::getMIMEType() const {
170 if (mInitCheck != OK) {
171 return String8("application/octet-stream");
172 }
173
174 String8 mimeType;
175 status_t err = mHTTPConnection->getMIMEType(&mimeType);
176
177 if (err != OK) {
178 return String8("application/octet-stream");
179 }
180
181 return mimeType;
182}
183
184} // namespace android