Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 1 | /* |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 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 | |
Marco Nelissen | fa8be7d | 2019-09-23 12:15:57 -0700 | [diff] [blame] | 21 | #include <datasource/MediaHTTP.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 22 | |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 23 | #include <media/stagefright/foundation/ADebug.h> |
| 24 | #include <media/stagefright/foundation/ALooper.h> |
Marco Nelissen | fa8be7d | 2019-09-23 12:15:57 -0700 | [diff] [blame] | 25 | #include <media/stagefright/FoundationUtils.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 26 | |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 27 | #include <media/MediaHTTPConnection.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 31 | MediaHTTP::MediaHTTP(const sp<MediaHTTPConnection> &conn) |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 32 | : mInitCheck((conn != NULL) ? OK : NO_INIT), |
| 33 | mHTTPConnection(conn), |
| 34 | mCachedSizeValid(false), |
| 35 | mCachedSize(0ll) { |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | MediaHTTP::~MediaHTTP() { |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 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; |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 49 | KeyedVector<String8, String8> extHeaders; |
| 50 | if (headers != NULL) { |
| 51 | extHeaders = *headers; |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 54 | if (extHeaders.indexOfKey(String8("User-Agent")) < 0) { |
| 55 | extHeaders.add(String8("User-Agent"), String8(MakeUserAgent().c_str())); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 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()); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 74 | return success ? OK : UNKNOWN_ERROR; |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 77 | void MediaHTTP::close() { |
| 78 | disconnect(); |
| 79 | } |
| 80 | |
| 81 | void MediaHTTP::disconnect() { |
| 82 | mName = String8("MediaHTTP(<disconnected>)"); |
| 83 | if (mInitCheck != OK) { |
| 84 | return; |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 85 | } |
Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 86 | |
| 87 | mHTTPConnection->disconnect(); |
| 88 | } |
| 89 | |
| 90 | status_t MediaHTTP::initCheck() const { |
| 91 | return mInitCheck; |
| 92 | } |
| 93 | |
| 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 | |
| 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 | |
| 148 | uint32_t MediaHTTP::flags() { |
| 149 | return kWantsPrefetching | kIsHTTPBasedSource; |
| 150 | } |
| 151 | |
| 152 | status_t MediaHTTP::reconnectAtOffset(off64_t offset) { |
| 153 | return connect(mLastURI.c_str(), &mLastHeaders, offset); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | String8 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 | |
| 169 | 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; |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } // namespace android |