blob: bb6a08cc1feeaec1ce768ce46bc86f17efebc2b0 [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());
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070068 }
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 Kang79b0c242019-10-13 08:17:34 -070081 if (mediaHTTP->connect(uri, &nonCacheSpecificHeaders) != OK) {
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070082 ALOGE("Failed to connect http source!");
83 return NULL;
84 }
85
86 if (contentType != NULL) {
Dongwon Kang79b0c242019-10-13 08:17:34 -070087 *contentType = mediaHTTP->getMIMEType();
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070088 }
89
90 source = NuCachedSource2::Create(
Dongwon Kang79b0c242019-10-13 08:17:34 -070091 mediaHTTP,
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070092 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 Kang79b0c242019-10-13 08:17:34 -070098 source = CreateFileSource(uri);
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070099 }
100
101 if (source == NULL || source->initCheck() != OK) {
102 return NULL;
103 }
104
105 return source;
106}
107
108sp<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 Jia73feb8c2017-12-05 14:07:27 -0800113sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) {
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700114 if (httpService == NULL) {
115 return NULL;
116 }
117
Wei Jia73feb8c2017-12-05 14:07:27 -0800118 sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700119 if (conn == NULL) {
Dongwon Kang79b0c242019-10-13 08:17:34 -0700120 ALOGE("Failed to make http connection from http service!");
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700121 return NULL;
122 } else {
123 return new MediaHTTP(conn);
124 }
125}
126
Dongwon Kang79b0c242019-10-13 08:17:34 -0700127sp<DataSource> DataSourceFactory::CreateFileSource(const char *uri) {
128 return new FileSource(uri);
129}
130
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700131} // namespace android