blob: 58c1ce8991478c89ce3cdf92e7b78a3a047a7d2f [file] [log] [blame]
Andreas Huber1b86fe02014-01-29 11:13:26 -08001/*
Dongwon Kang9c6f7902019-10-14 11:16:39 -07002 * Copyright (C) 2018 The Android Open Source Project
Andreas Huber1b86fe02014-01-29 11:13:26 -08003 *
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
Marco Nelissenfa8be7d2019-09-23 12:15:57 -070021#include <datasource/MediaHTTP.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080022
Andreas Huber1b86fe02014-01-29 11:13:26 -080023#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/ALooper.h>
Marco Nelissenfa8be7d2019-09-23 12:15:57 -070025#include <media/stagefright/FoundationUtils.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080026
Wei Jia73feb8c2017-12-05 14:07:27 -080027#include <media/MediaHTTPConnection.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080028
29namespace android {
30
Wei Jia73feb8c2017-12-05 14:07:27 -080031MediaHTTP::MediaHTTP(const sp<MediaHTTPConnection> &conn)
Dongwon Kang9c6f7902019-10-14 11:16:39 -070032 : mInitCheck((conn != NULL) ? OK : NO_INIT),
33 mHTTPConnection(conn),
34 mCachedSizeValid(false),
35 mCachedSize(0ll) {
Andreas Huber1b86fe02014-01-29 11:13:26 -080036}
37
38MediaHTTP::~MediaHTTP() {
Andreas Huber1b86fe02014-01-29 11:13:26 -080039}
40
Dongwon Kang9c6f7902019-10-14 11:16:39 -070041status_t MediaHTTP::connect(
42 const char *uri,
43 const KeyedVector<String8, String8> *headers,
44 off64_t /* offset */) {
45 if (mInitCheck != OK) {
46 return mInitCheck;
Andreas Huber1b86fe02014-01-29 11:13:26 -080047 }
48
Dongwon Kang9c6f7902019-10-14 11:16:39 -070049 KeyedVector<String8, String8> extHeaders;
50 if (headers != NULL) {
51 extHeaders = *headers;
Andreas Huber1b86fe02014-01-29 11:13:26 -080052 }
53
Dongwon Kang9c6f7902019-10-14 11:16:39 -070054 if (extHeaders.indexOfKey(String8("User-Agent")) < 0) {
55 extHeaders.add(String8("User-Agent"), String8(MakeUserAgent().c_str()));
Andreas Huber1b86fe02014-01-29 11:13:26 -080056 }
57
Dongwon Kang9c6f7902019-10-14 11:16:39 -070058 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("MediaHTTP(%s)", sanitized.c_str());
Andreas Huber1b86fe02014-01-29 11:13:26 -080072 }
73
Dongwon Kang9c6f7902019-10-14 11:16:39 -070074 return success ? OK : UNKNOWN_ERROR;
Andreas Huber1b86fe02014-01-29 11:13:26 -080075}
76
Dongwon Kang9c6f7902019-10-14 11:16:39 -070077void MediaHTTP::close() {
78 disconnect();
79}
80
81void MediaHTTP::disconnect() {
82 mName = String8("MediaHTTP(<disconnected>)");
83 if (mInitCheck != OK) {
84 return;
Andreas Huber1b86fe02014-01-29 11:13:26 -080085 }
Dongwon Kang9c6f7902019-10-14 11:16:39 -070086
87 mHTTPConnection->disconnect();
88}
89
90status_t MediaHTTP::initCheck() const {
91 return mInitCheck;
92}
93
94ssize_t MediaHTTP::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 MediaHTTP::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 MediaHTTP::flags() {
149 return kWantsPrefetching | kIsHTTPBasedSource;
150}
151
152status_t MediaHTTP::reconnectAtOffset(off64_t offset) {
153 return connect(mLastURI.c_str(), &mLastHeaders, offset);
154}
155
156
157String8 MediaHTTP::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 MediaHTTP::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;
Andreas Huber1b86fe02014-01-29 11:13:26 -0800182}
183
184} // namespace android