Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | //#define LOG_NDEBUG 0 |
| 17 | #define LOG_TAG "DataSource" |
| 18 | |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 19 | |
Marco Nelissen | fa8be7d | 2019-09-23 12:15:57 -0700 | [diff] [blame] | 20 | #include <datasource/DataSourceFactory.h> |
| 21 | #include <datasource/DataURISource.h> |
| 22 | #include <datasource/HTTPBase.h> |
| 23 | #include <datasource/FileSource.h> |
| 24 | #include <datasource/MediaHTTP.h> |
| 25 | #include <datasource/NuCachedSource2.h> |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 26 | #include <media/MediaHTTPConnection.h> |
| 27 | #include <media/MediaHTTPService.h> |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 28 | #include <utils/String8.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // static |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 33 | sp<DataSourceFactory> DataSourceFactory::sInstance; |
| 34 | // static |
| 35 | Mutex DataSourceFactory::sInstanceLock; |
| 36 | |
| 37 | // static |
| 38 | sp<DataSourceFactory> DataSourceFactory::getInstance() { |
| 39 | Mutex::Autolock l(sInstanceLock); |
| 40 | if (!sInstance) { |
| 41 | sInstance = new DataSourceFactory(); |
| 42 | } |
| 43 | return sInstance; |
| 44 | } |
| 45 | |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 46 | sp<DataSource> DataSourceFactory::CreateFromURI( |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 47 | const sp<MediaHTTPService> &httpService, |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 48 | const char *uri, |
| 49 | const KeyedVector<String8, String8> *headers, |
| 50 | String8 *contentType, |
| 51 | HTTPBase *httpSource) { |
| 52 | if (contentType != NULL) { |
| 53 | *contentType = ""; |
| 54 | } |
| 55 | |
| 56 | sp<DataSource> source; |
| 57 | if (!strncasecmp("file://", uri, 7)) { |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 58 | source = CreateFileSource(uri + 7); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 59 | } else if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) { |
| 60 | if (httpService == NULL) { |
| 61 | ALOGE("Invalid http service!"); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 65 | sp<HTTPBase> mediaHTTP = httpSource; |
| 66 | if (mediaHTTP == NULL) { |
| 67 | mediaHTTP = static_cast<HTTPBase *>(CreateMediaHTTP(httpService).get()); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | String8 cacheConfig; |
| 71 | bool disconnectAtHighwatermark = false; |
| 72 | KeyedVector<String8, String8> nonCacheSpecificHeaders; |
| 73 | if (headers != NULL) { |
| 74 | nonCacheSpecificHeaders = *headers; |
| 75 | NuCachedSource2::RemoveCacheSpecificHeaders( |
| 76 | &nonCacheSpecificHeaders, |
| 77 | &cacheConfig, |
| 78 | &disconnectAtHighwatermark); |
| 79 | } |
| 80 | |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 81 | if (mediaHTTP->connect(uri, &nonCacheSpecificHeaders) != OK) { |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 82 | ALOGE("Failed to connect http source!"); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | if (contentType != NULL) { |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 87 | *contentType = mediaHTTP->getMIMEType(); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | source = NuCachedSource2::Create( |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 91 | mediaHTTP, |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 92 | cacheConfig.isEmpty() ? NULL : cacheConfig.string(), |
| 93 | disconnectAtHighwatermark); |
| 94 | } else if (!strncasecmp("data:", uri, 5)) { |
| 95 | source = DataURISource::Create(uri); |
| 96 | } else { |
| 97 | // Assume it's a filename. |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 98 | source = CreateFileSource(uri); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (source == NULL || source->initCheck() != OK) { |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | return source; |
| 106 | } |
| 107 | |
| 108 | sp<DataSource> DataSourceFactory::CreateFromFd(int fd, int64_t offset, int64_t length) { |
| 109 | sp<FileSource> source = new FileSource(fd, offset, length); |
| 110 | return source->initCheck() != OK ? nullptr : source; |
| 111 | } |
| 112 | |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 113 | sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) { |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 114 | if (httpService == NULL) { |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 118 | sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection(); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 119 | if (conn == NULL) { |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 120 | ALOGE("Failed to make http connection from http service!"); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 121 | return NULL; |
| 122 | } else { |
| 123 | return new MediaHTTP(conn); |
| 124 | } |
| 125 | } |
| 126 | |
Dongwon Kang | 79b0c24 | 2019-10-13 08:17:34 -0700 | [diff] [blame] | 127 | sp<DataSource> DataSourceFactory::CreateFileSource(const char *uri) { |
| 128 | return new FileSource(uri); |
| 129 | } |
| 130 | |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 131 | } // namespace android |