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 |
| 33 | sp<DataSource> DataSourceFactory::CreateFromURI( |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 34 | const sp<MediaHTTPService> &httpService, |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 35 | const char *uri, |
| 36 | const KeyedVector<String8, String8> *headers, |
| 37 | String8 *contentType, |
| 38 | HTTPBase *httpSource) { |
| 39 | if (contentType != NULL) { |
| 40 | *contentType = ""; |
| 41 | } |
| 42 | |
| 43 | sp<DataSource> source; |
| 44 | if (!strncasecmp("file://", uri, 7)) { |
| 45 | source = new FileSource(uri + 7); |
| 46 | } else if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) { |
| 47 | if (httpService == NULL) { |
| 48 | ALOGE("Invalid http service!"); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | if (httpSource == NULL) { |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 53 | sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection(); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 54 | if (conn == NULL) { |
| 55 | ALOGE("Failed to make http connection from http service!"); |
| 56 | return NULL; |
| 57 | } |
| 58 | httpSource = new MediaHTTP(conn); |
| 59 | } |
| 60 | |
| 61 | String8 cacheConfig; |
| 62 | bool disconnectAtHighwatermark = false; |
| 63 | KeyedVector<String8, String8> nonCacheSpecificHeaders; |
| 64 | if (headers != NULL) { |
| 65 | nonCacheSpecificHeaders = *headers; |
| 66 | NuCachedSource2::RemoveCacheSpecificHeaders( |
| 67 | &nonCacheSpecificHeaders, |
| 68 | &cacheConfig, |
| 69 | &disconnectAtHighwatermark); |
| 70 | } |
| 71 | |
| 72 | if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) { |
| 73 | ALOGE("Failed to connect http source!"); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | if (contentType != NULL) { |
| 78 | *contentType = httpSource->getMIMEType(); |
| 79 | } |
| 80 | |
| 81 | source = NuCachedSource2::Create( |
| 82 | httpSource, |
| 83 | cacheConfig.isEmpty() ? NULL : cacheConfig.string(), |
| 84 | disconnectAtHighwatermark); |
| 85 | } else if (!strncasecmp("data:", uri, 5)) { |
| 86 | source = DataURISource::Create(uri); |
| 87 | } else { |
| 88 | // Assume it's a filename. |
| 89 | source = new FileSource(uri); |
| 90 | } |
| 91 | |
| 92 | if (source == NULL || source->initCheck() != OK) { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | return source; |
| 97 | } |
| 98 | |
| 99 | sp<DataSource> DataSourceFactory::CreateFromFd(int fd, int64_t offset, int64_t length) { |
| 100 | sp<FileSource> source = new FileSource(fd, offset, length); |
| 101 | return source->initCheck() != OK ? nullptr : source; |
| 102 | } |
| 103 | |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 104 | sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) { |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 105 | if (httpService == NULL) { |
| 106 | return NULL; |
| 107 | } |
| 108 | |
Wei Jia | 73feb8c | 2017-12-05 14:07:27 -0800 | [diff] [blame] | 109 | sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection(); |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 110 | if (conn == NULL) { |
| 111 | return NULL; |
| 112 | } else { |
| 113 | return new MediaHTTP(conn); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } // namespace android |