• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             if (mediaHTTP == NULL) {
69                 return NULL;
70             }
71         }
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 
84         if (mediaHTTP->connect(uri, &nonCacheSpecificHeaders) != OK) {
85             ALOGE("Failed to connect http source!");
86             return NULL;
87         }
88 
89         if (contentType != NULL) {
90             *contentType = mediaHTTP->getMIMEType();
91         }
92 
93         source = NuCachedSource2::Create(
94                 mediaHTTP,
95                 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.
101         source = CreateFileSource(uri);
102     }
103 
104     if (source == NULL || source->initCheck() != OK) {
105         return NULL;
106     }
107 
108     return source;
109 }
110 
CreateFromFd(int fd,int64_t offset,int64_t length)111 sp<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 
CreateMediaHTTP(const sp<MediaHTTPService> & httpService)116 sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) {
117     if (httpService == NULL) {
118         return NULL;
119     }
120 
121     sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
122     if (conn == NULL) {
123         ALOGE("Failed to make http connection from http service!");
124         return NULL;
125     } else {
126         return new MediaHTTP(conn);
127     }
128 }
129 
CreateFileSource(const char * uri)130 sp<DataSource> DataSourceFactory::CreateFileSource(const char *uri) {
131     return new FileSource(uri);
132 }
133 
134 }  // namespace android
135