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 
19 
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>
26 #include <media/MediaHTTPConnection.h>
27 #include <media/MediaHTTPService.h>
28 #include <utils/String8.h>
29 
30 namespace android {
31 
32 // static
33 sp<DataSourceFactory> DataSourceFactory::sInstance;
34 // static
35 Mutex DataSourceFactory::sInstanceLock;
36 
37 // static
getInstance()38 sp<DataSourceFactory> DataSourceFactory::getInstance() {
39     Mutex::Autolock l(sInstanceLock);
40     if (!sInstance) {
41         sInstance = new DataSourceFactory();
42     }
43     return sInstance;
44 }
45 
CreateFromURI(const sp<MediaHTTPService> & httpService,const char * uri,const KeyedVector<String8,String8> * headers,String8 * contentType,HTTPBase * httpSource)46 sp<DataSource> DataSourceFactory::CreateFromURI(
47         const sp<MediaHTTPService> &httpService,
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)) {
58         source = CreateFileSource(uri + 7);
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 
65         sp<HTTPBase> mediaHTTP = httpSource;
66         if (mediaHTTP == NULL) {
67             mediaHTTP = static_cast<HTTPBase *>(CreateMediaHTTP(httpService).get());
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 
81         if (mediaHTTP->connect(uri, &nonCacheSpecificHeaders) != OK) {
82             ALOGE("Failed to connect http source!");
83             return NULL;
84         }
85 
86         if (contentType != NULL) {
87             *contentType = mediaHTTP->getMIMEType();
88         }
89 
90         source = NuCachedSource2::Create(
91                 mediaHTTP,
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.
98         source = CreateFileSource(uri);
99     }
100 
101     if (source == NULL || source->initCheck() != OK) {
102         return NULL;
103     }
104 
105     return source;
106 }
107 
CreateFromFd(int fd,int64_t offset,int64_t length)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 
CreateMediaHTTP(const sp<MediaHTTPService> & httpService)113 sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) {
114     if (httpService == NULL) {
115         return NULL;
116     }
117 
118     sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
119     if (conn == NULL) {
120         ALOGE("Failed to make http connection from http service!");
121         return NULL;
122     } else {
123         return new MediaHTTP(conn);
124     }
125 }
126 
CreateFileSource(const char * uri)127 sp<DataSource> DataSourceFactory::CreateFileSource(const char *uri) {
128     return new FileSource(uri);
129 }
130 
131 }  // namespace android
132