Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 16 | //#define LOG_NDEBUG 0 |
| 17 | #define LOG_TAG "DataSource" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 18 | |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 19 | #include "include/CallbackDataSource.h" |
Marco Nelissen | 5699712 | 2012-08-28 15:09:49 -0700 | [diff] [blame] | 20 | #include "include/HTTPBase.h" |
Marco Nelissen | 5699712 | 2012-08-28 15:09:49 -0700 | [diff] [blame] | 21 | #include "include/NuCachedSource2.h" |
Andreas Huber | 093437c | 2010-05-20 14:56:53 -0700 | [diff] [blame] | 22 | |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 23 | #include <media/IDataSource.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 24 | #include <media/IMediaHTTPConnection.h> |
| 25 | #include <media/IMediaHTTPService.h> |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 26 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 5a1c352 | 2010-08-25 11:09:41 -0700 | [diff] [blame] | 27 | #include <media/stagefright/foundation/AMessage.h> |
Dongwon Kang | 6076128 | 2017-10-09 11:16:48 -0700 | [diff] [blame] | 28 | #include <media/stagefright/foundation/ByteUtils.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 29 | #include <media/stagefright/DataSource.h> |
Andreas Huber | 4bbfff2 | 2014-02-10 14:40:45 -0800 | [diff] [blame] | 30 | #include <media/stagefright/DataURISource.h> |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 31 | #include <media/stagefright/FileSource.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 32 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 33 | #include <media/stagefright/MediaHTTP.h> |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 34 | #include <media/stagefright/RemoteDataSource.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 35 | #include <utils/String8.h> |
| 36 | |
Gloria Wang | a890829 | 2010-10-29 14:50:17 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
| 38 | |
Marco Nelissen | 3001a8a | 2015-12-18 14:17:18 -0800 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
| 40 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 43 | bool DataSource::getUInt16(off64_t offset, uint16_t *x) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 44 | *x = 0; |
| 45 | |
| 46 | uint8_t byte[2]; |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 47 | if (readAt(offset, byte, 2) != 2) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | |
| 51 | *x = (byte[0] << 8) | byte[1]; |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
Marco Nelissen | ec77122 | 2013-04-08 14:30:57 -0700 | [diff] [blame] | 56 | bool DataSource::getUInt24(off64_t offset, uint32_t *x) { |
| 57 | *x = 0; |
| 58 | |
| 59 | uint8_t byte[3]; |
| 60 | if (readAt(offset, byte, 3) != 3) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | *x = (byte[0] << 16) | (byte[1] << 8) | byte[2]; |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
Marco Nelissen | 05f625c | 2013-02-13 09:27:28 -0800 | [diff] [blame] | 69 | bool DataSource::getUInt32(off64_t offset, uint32_t *x) { |
| 70 | *x = 0; |
| 71 | |
| 72 | uint32_t tmp; |
| 73 | if (readAt(offset, &tmp, 4) != 4) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | *x = ntohl(tmp); |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool DataSource::getUInt64(off64_t offset, uint64_t *x) { |
| 83 | *x = 0; |
| 84 | |
| 85 | uint64_t tmp; |
| 86 | if (readAt(offset, &tmp, 8) != 8) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | *x = ntoh64(tmp); |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 95 | bool DataSource::getUInt16Var(off64_t offset, uint16_t *x, size_t size) { |
| 96 | if (size == 2) { |
| 97 | return getUInt16(offset, x); |
| 98 | } |
| 99 | if (size == 1) { |
| 100 | uint8_t tmp; |
| 101 | if (readAt(offset, &tmp, 1) == 1) { |
| 102 | *x = tmp; |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | bool DataSource::getUInt32Var(off64_t offset, uint32_t *x, size_t size) { |
| 110 | if (size == 4) { |
| 111 | return getUInt32(offset, x); |
| 112 | } |
| 113 | if (size == 2) { |
| 114 | uint16_t tmp; |
| 115 | if (getUInt16(offset, &tmp)) { |
| 116 | *x = tmp; |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | bool DataSource::getUInt64Var(off64_t offset, uint64_t *x, size_t size) { |
| 124 | if (size == 8) { |
| 125 | return getUInt64(offset, x); |
| 126 | } |
| 127 | if (size == 4) { |
| 128 | uint32_t tmp; |
| 129 | if (getUInt32(offset, &tmp)) { |
| 130 | *x = tmp; |
| 131 | return true; |
| 132 | } |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 137 | status_t DataSource::getSize(off64_t *size) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 138 | *size = 0; |
| 139 | |
| 140 | return ERROR_UNSUPPORTED; |
| 141 | } |
| 142 | |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 143 | sp<IDataSource> DataSource::getIDataSource() const { |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 147 | //////////////////////////////////////////////////////////////////////////////// |
| 148 | |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 149 | // static |
Andreas Huber | 5561ccf | 2010-01-27 16:49:05 -0800 | [diff] [blame] | 150 | sp<DataSource> DataSource::CreateFromURI( |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 151 | const sp<IMediaHTTPService> &httpService, |
| 152 | const char *uri, |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 153 | const KeyedVector<String8, String8> *headers, |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 154 | String8 *contentType, |
| 155 | HTTPBase *httpSource) { |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 156 | if (contentType != NULL) { |
| 157 | *contentType = ""; |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 160 | sp<DataSource> source; |
| 161 | if (!strncasecmp("file://", uri, 7)) { |
| 162 | source = new FileSource(uri + 7); |
Jeff Tinker | 29b7dcf | 2016-10-24 10:28:30 -0700 | [diff] [blame] | 163 | } else if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) { |
Chong Zhang | 217bde8 | 2014-09-22 12:25:36 -0700 | [diff] [blame] | 164 | if (httpService == NULL) { |
| 165 | ALOGE("Invalid http service!"); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 169 | if (httpSource == NULL) { |
| 170 | sp<IMediaHTTPConnection> conn = httpService->makeHTTPConnection(); |
| 171 | if (conn == NULL) { |
| 172 | ALOGE("Failed to make http connection from http service!"); |
| 173 | return NULL; |
| 174 | } |
| 175 | httpSource = new MediaHTTP(conn); |
Chong Zhang | 217bde8 | 2014-09-22 12:25:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Chong Zhang | 7b3cd1f | 2014-08-13 15:26:49 -0700 | [diff] [blame] | 178 | String8 cacheConfig; |
Caroline Tice | 65c954e | 2017-02-08 11:33:04 -0800 | [diff] [blame] | 179 | bool disconnectAtHighwatermark = false; |
Chong Zhang | 7b3cd1f | 2014-08-13 15:26:49 -0700 | [diff] [blame] | 180 | KeyedVector<String8, String8> nonCacheSpecificHeaders; |
| 181 | if (headers != NULL) { |
| 182 | nonCacheSpecificHeaders = *headers; |
| 183 | NuCachedSource2::RemoveCacheSpecificHeaders( |
| 184 | &nonCacheSpecificHeaders, |
| 185 | &cacheConfig, |
| 186 | &disconnectAtHighwatermark); |
| 187 | } |
| 188 | |
| 189 | if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) { |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 190 | ALOGE("Failed to connect http source!"); |
Andreas Huber | e94bd14 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Andreas Huber | 918c765 | 2012-04-13 11:16:48 -0700 | [diff] [blame] | 193 | |
Jeff Tinker | 29b7dcf | 2016-10-24 10:28:30 -0700 | [diff] [blame] | 194 | if (contentType != NULL) { |
| 195 | *contentType = httpSource->getMIMEType(); |
Andreas Huber | 1608735 | 2012-04-13 14:54:36 -0700 | [diff] [blame] | 196 | } |
Jeff Tinker | 29b7dcf | 2016-10-24 10:28:30 -0700 | [diff] [blame] | 197 | |
| 198 | source = NuCachedSource2::Create( |
| 199 | httpSource, |
| 200 | cacheConfig.isEmpty() ? NULL : cacheConfig.string(), |
| 201 | disconnectAtHighwatermark); |
Andreas Huber | 4bbfff2 | 2014-02-10 14:40:45 -0800 | [diff] [blame] | 202 | } else if (!strncasecmp("data:", uri, 5)) { |
| 203 | source = DataURISource::Create(uri); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 204 | } else { |
| 205 | // Assume it's a filename. |
| 206 | source = new FileSource(uri); |
| 207 | } |
| 208 | |
| 209 | if (source == NULL || source->initCheck() != OK) { |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | return source; |
| 214 | } |
| 215 | |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 216 | sp<DataSource> DataSource::CreateFromFd(int fd, int64_t offset, int64_t length) { |
| 217 | sp<FileSource> source = new FileSource(fd, offset, length); |
| 218 | return source->initCheck() != OK ? nullptr : source; |
| 219 | } |
| 220 | |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 221 | sp<DataSource> DataSource::CreateMediaHTTP(const sp<IMediaHTTPService> &httpService) { |
| 222 | if (httpService == NULL) { |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | sp<IMediaHTTPConnection> conn = httpService->makeHTTPConnection(); |
| 227 | if (conn == NULL) { |
| 228 | return NULL; |
| 229 | } else { |
| 230 | return new MediaHTTP(conn); |
| 231 | } |
| 232 | } |
| 233 | |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 234 | sp<DataSource> DataSource::CreateFromIDataSource(const sp<IDataSource> &source) { |
Chris Watkins | da7e453 | 2015-04-07 10:01:15 -0700 | [diff] [blame] | 235 | return new TinyCacheSource(new CallbackDataSource(source)); |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Andreas Huber | 6511c97 | 2011-03-30 11:15:27 -0700 | [diff] [blame] | 238 | String8 DataSource::getMIMEType() const { |
| 239 | return String8("application/octet-stream"); |
| 240 | } |
| 241 | |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 242 | sp<IDataSource> DataSource::asIDataSource() { |
| 243 | return RemoteDataSource::wrap(sp<DataSource>(this)); |
| 244 | } |
| 245 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 246 | } // namespace android |