blob: f91e3ea853ab91b2c63cc57761ecf59d877b6ec9 [file] [log] [blame]
Dongwon Kangd91dc5a2017-10-10 00:07:09 -07001/*
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 Kangd91dc5a2017-10-10 00:07:09 -070019
Marco Nelissenfa8be7d2019-09-23 12:15:57 -070020#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 Jia73feb8c2017-12-05 14:07:27 -080026#include <media/MediaHTTPConnection.h>
27#include <media/MediaHTTPService.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070028#include <utils/String8.h>
29
30namespace android {
31
32// static
Dongwon Kang79b0c242019-10-13 08:17:34 -070033sp<DataSourceFactory> DataSourceFactory::sInstance;
34// static
35Mutex DataSourceFactory::sInstanceLock;
36
37// static
38sp<DataSourceFactory> DataSourceFactory::getInstance() {
39 Mutex::Autolock l(sInstanceLock);
40 if (!sInstance) {
41 sInstance = new DataSourceFactory();
42 }
43 return sInstance;
44}
45
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070046sp<DataSource> DataSourceFactory::CreateFromURI(
Wei Jia73feb8c2017-12-05 14:07:27 -080047 const sp<MediaHTTPService> &httpService,
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070048 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 Kang79b0c242019-10-13 08:17:34 -070058 source = CreateFileSource(uri + 7);
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070059 } 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 Kang79b0c242019-10-13 08:17:34 -070065 sp<HTTPBase> mediaHTTP = httpSource;
66 if (mediaHTTP == NULL) {
67 mediaHTTP = static_cast<HTTPBase *>(CreateMediaHTTP(httpService).get());
Yuxiu Zhang980bdf22021-04-25 14:31:23 +080068 if (mediaHTTP == NULL) {
69 return NULL;
70 }
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070071 }
72
73 String8 cacheConfig;
74 bool disconnectAtHighwatermark = false;
75 KeyedVector<String8, String8> nonCacheSpecificHeaders;
76 if (headers != NULL) {
77 nonCacheSpecificHeaders = *headers;
78 NuCachedSource2::RemoveCacheSpecificHeaders(
79 &nonCacheSpecificHeaders,
80 &cacheConfig,
81 &disconnectAtHighwatermark);
82 }
83
Dongwon Kang79b0c242019-10-13 08:17:34 -070084 if (mediaHTTP->connect(uri, &nonCacheSpecificHeaders) != OK) {
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070085 ALOGE("Failed to connect http source!");
86 return NULL;
87 }
88
89 if (contentType != NULL) {
Dongwon Kang79b0c242019-10-13 08:17:34 -070090 *contentType = mediaHTTP->getMIMEType();
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070091 }
92
93 source = NuCachedSource2::Create(
Dongwon Kang79b0c242019-10-13 08:17:34 -070094 mediaHTTP,
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070095 cacheConfig.isEmpty() ? NULL : cacheConfig.string(),
96 disconnectAtHighwatermark);
97 } else if (!strncasecmp("data:", uri, 5)) {
98 source = DataURISource::Create(uri);
99 } else {
100 // Assume it's a filename.
Dongwon Kang79b0c242019-10-13 08:17:34 -0700101 source = CreateFileSource(uri);
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700102 }
103
104 if (source == NULL || source->initCheck() != OK) {
105 return NULL;
106 }
107
108 return source;
109}
110
111sp<DataSource> DataSourceFactory::CreateFromFd(int fd, int64_t offset, int64_t length) {
112 sp<FileSource> source = new FileSource(fd, offset, length);
113 return source->initCheck() != OK ? nullptr : source;
114}
115
Wei Jia73feb8c2017-12-05 14:07:27 -0800116sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) {
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700117 if (httpService == NULL) {
118 return NULL;
119 }
120
Wei Jia73feb8c2017-12-05 14:07:27 -0800121 sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700122 if (conn == NULL) {
Dongwon Kang79b0c242019-10-13 08:17:34 -0700123 ALOGE("Failed to make http connection from http service!");
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700124 return NULL;
125 } else {
126 return new MediaHTTP(conn);
127 }
128}
129
Dongwon Kang79b0c242019-10-13 08:17:34 -0700130sp<DataSource> DataSourceFactory::CreateFileSource(const char *uri) {
131 return new FileSource(uri);
132}
133
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700134} // namespace android