1 /*
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 "MediaHTTP"
19 #include <utils/Log.h>
20
21 #include <datasource/MediaHTTP.h>
22
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/foundation/ALooper.h>
25 #include <media/stagefright/FoundationUtils.h>
26
27 #include <media/MediaHTTPConnection.h>
28
29 namespace android {
30
MediaHTTP(const sp<MediaHTTPConnection> & conn)31 MediaHTTP::MediaHTTP(const sp<MediaHTTPConnection> &conn)
32 : mInitCheck((conn != NULL) ? OK : NO_INIT),
33 mHTTPConnection(conn),
34 mCachedSizeValid(false),
35 mCachedSize(0ll) {
36 }
37
~MediaHTTP()38 MediaHTTP::~MediaHTTP() {
39 }
40
connect(const char * uri,const KeyedVector<String8,String8> * headers,off64_t)41 status_t MediaHTTP::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("MediaHTTP(%s)", sanitized.c_str());
72 }
73
74 return success ? OK : UNKNOWN_ERROR;
75 }
76
close()77 void MediaHTTP::close() {
78 disconnect();
79 }
80
disconnect()81 void MediaHTTP::disconnect() {
82 mName = String8("MediaHTTP(<disconnected>)");
83 if (mInitCheck != OK) {
84 return;
85 }
86
87 mHTTPConnection->disconnect();
88 }
89
initCheck() const90 status_t MediaHTTP::initCheck() const {
91 return mInitCheck;
92 }
93
readAt(off64_t offset,void * data,size_t size)94 ssize_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
getSize(off64_t * size)130 status_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
flags()148 uint32_t MediaHTTP::flags() {
149 return kWantsPrefetching | kIsHTTPBasedSource;
150 }
151
reconnectAtOffset(off64_t offset)152 status_t MediaHTTP::reconnectAtOffset(off64_t offset) {
153 return connect(mLastURI.c_str(), &mLastHeaders, offset);
154 }
155
156
getUri()157 String8 MediaHTTP::getUri() {
158 if (mInitCheck != OK) {
159 return String8();
160 }
161
162 String8 uri;
163 if (OK == mHTTPConnection->getUri(&uri)) {
164 return uri;
165 }
166 return String8(mLastURI.c_str());
167 }
168
getMIMEType() const169 String8 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;
182 }
183
184 } // namespace android
185