1 /*
2 * Copyright (C) 2009 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 #include "include/AMRExtractor.h"
20
21 #include "include/AACExtractor.h"
22 #include "include/CallbackDataSource.h"
23 #include "include/DRMExtractor.h"
24 #include "include/FLACExtractor.h"
25 #include "include/HTTPBase.h"
26 #include "include/MidiExtractor.h"
27 #include "include/MP3Extractor.h"
28 #include "include/MPEG2PSExtractor.h"
29 #include "include/MPEG2TSExtractor.h"
30 #include "include/MPEG4Extractor.h"
31 #include "include/NuCachedSource2.h"
32 #include "include/OggExtractor.h"
33 #include "include/WAVExtractor.h"
34 #include "include/WVMExtractor.h"
35
36 #include "matroska/MatroskaExtractor.h"
37
38 #include <media/IMediaHTTPConnection.h>
39 #include <media/IMediaHTTPService.h>
40 #include <media/stagefright/foundation/ADebug.h>
41 #include <media/stagefright/foundation/AMessage.h>
42 #include <media/stagefright/DataSource.h>
43 #include <media/stagefright/DataURISource.h>
44 #include <media/stagefright/FileSource.h>
45 #include <media/stagefright/MediaErrors.h>
46 #include <media/stagefright/MediaHTTP.h>
47 #include <utils/String8.h>
48
49 #include <cutils/properties.h>
50
51 namespace android {
52
getUInt16(off64_t offset,uint16_t * x)53 bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
54 *x = 0;
55
56 uint8_t byte[2];
57 if (readAt(offset, byte, 2) != 2) {
58 return false;
59 }
60
61 *x = (byte[0] << 8) | byte[1];
62
63 return true;
64 }
65
getUInt24(off64_t offset,uint32_t * x)66 bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
67 *x = 0;
68
69 uint8_t byte[3];
70 if (readAt(offset, byte, 3) != 3) {
71 return false;
72 }
73
74 *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
75
76 return true;
77 }
78
getUInt32(off64_t offset,uint32_t * x)79 bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
80 *x = 0;
81
82 uint32_t tmp;
83 if (readAt(offset, &tmp, 4) != 4) {
84 return false;
85 }
86
87 *x = ntohl(tmp);
88
89 return true;
90 }
91
getUInt64(off64_t offset,uint64_t * x)92 bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
93 *x = 0;
94
95 uint64_t tmp;
96 if (readAt(offset, &tmp, 8) != 8) {
97 return false;
98 }
99
100 *x = ntoh64(tmp);
101
102 return true;
103 }
104
getSize(off64_t * size)105 status_t DataSource::getSize(off64_t *size) {
106 *size = 0;
107
108 return ERROR_UNSUPPORTED;
109 }
110
111 ////////////////////////////////////////////////////////////////////////////////
112
113 Mutex DataSource::gSnifferMutex;
114 List<DataSource::SnifferFunc> DataSource::gSniffers;
115 bool DataSource::gSniffersRegistered = false;
116
sniff(String8 * mimeType,float * confidence,sp<AMessage> * meta)117 bool DataSource::sniff(
118 String8 *mimeType, float *confidence, sp<AMessage> *meta) {
119 *mimeType = "";
120 *confidence = 0.0f;
121 meta->clear();
122
123 {
124 Mutex::Autolock autoLock(gSnifferMutex);
125 if (!gSniffersRegistered) {
126 return false;
127 }
128 }
129
130 for (List<SnifferFunc>::iterator it = gSniffers.begin();
131 it != gSniffers.end(); ++it) {
132 String8 newMimeType;
133 float newConfidence;
134 sp<AMessage> newMeta;
135 if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
136 if (newConfidence > *confidence) {
137 *mimeType = newMimeType;
138 *confidence = newConfidence;
139 *meta = newMeta;
140 }
141 }
142 }
143
144 return *confidence > 0.0;
145 }
146
147 // static
RegisterSniffer_l(SnifferFunc func)148 void DataSource::RegisterSniffer_l(SnifferFunc func) {
149 for (List<SnifferFunc>::iterator it = gSniffers.begin();
150 it != gSniffers.end(); ++it) {
151 if (*it == func) {
152 return;
153 }
154 }
155
156 gSniffers.push_back(func);
157 }
158
159 // static
RegisterDefaultSniffers()160 void DataSource::RegisterDefaultSniffers() {
161 Mutex::Autolock autoLock(gSnifferMutex);
162 if (gSniffersRegistered) {
163 return;
164 }
165
166 RegisterSniffer_l(SniffMPEG4);
167 RegisterSniffer_l(SniffMatroska);
168 RegisterSniffer_l(SniffOgg);
169 RegisterSniffer_l(SniffWAV);
170 RegisterSniffer_l(SniffFLAC);
171 RegisterSniffer_l(SniffAMR);
172 RegisterSniffer_l(SniffMPEG2TS);
173 RegisterSniffer_l(SniffMP3);
174 RegisterSniffer_l(SniffAAC);
175 RegisterSniffer_l(SniffMPEG2PS);
176 RegisterSniffer_l(SniffWVM);
177 RegisterSniffer_l(SniffMidi);
178
179 char value[PROPERTY_VALUE_MAX];
180 if (property_get("drm.service.enabled", value, NULL)
181 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
182 RegisterSniffer_l(SniffDRM);
183 }
184 gSniffersRegistered = true;
185 }
186
187 // static
CreateFromURI(const sp<IMediaHTTPService> & httpService,const char * uri,const KeyedVector<String8,String8> * headers,String8 * contentType,HTTPBase * httpSource)188 sp<DataSource> DataSource::CreateFromURI(
189 const sp<IMediaHTTPService> &httpService,
190 const char *uri,
191 const KeyedVector<String8, String8> *headers,
192 String8 *contentType,
193 HTTPBase *httpSource) {
194 if (contentType != NULL) {
195 *contentType = "";
196 }
197
198 bool isWidevine = !strncasecmp("widevine://", uri, 11);
199
200 sp<DataSource> source;
201 if (!strncasecmp("file://", uri, 7)) {
202 source = new FileSource(uri + 7);
203 } else if (!strncasecmp("http://", uri, 7)
204 || !strncasecmp("https://", uri, 8)
205 || isWidevine) {
206 if (httpService == NULL) {
207 ALOGE("Invalid http service!");
208 return NULL;
209 }
210
211 if (httpSource == NULL) {
212 sp<IMediaHTTPConnection> conn = httpService->makeHTTPConnection();
213 if (conn == NULL) {
214 ALOGE("Failed to make http connection from http service!");
215 return NULL;
216 }
217 httpSource = new MediaHTTP(conn);
218 }
219
220 String8 tmp;
221 if (isWidevine) {
222 tmp = String8("http://");
223 tmp.append(uri + 11);
224
225 uri = tmp.string();
226 }
227
228 String8 cacheConfig;
229 bool disconnectAtHighwatermark;
230 KeyedVector<String8, String8> nonCacheSpecificHeaders;
231 if (headers != NULL) {
232 nonCacheSpecificHeaders = *headers;
233 NuCachedSource2::RemoveCacheSpecificHeaders(
234 &nonCacheSpecificHeaders,
235 &cacheConfig,
236 &disconnectAtHighwatermark);
237 }
238
239 if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) {
240 ALOGE("Failed to connect http source!");
241 return NULL;
242 }
243
244 if (!isWidevine) {
245 if (contentType != NULL) {
246 *contentType = httpSource->getMIMEType();
247 }
248
249 source = new NuCachedSource2(
250 httpSource,
251 cacheConfig.isEmpty() ? NULL : cacheConfig.string(),
252 disconnectAtHighwatermark);
253 } else {
254 // We do not want that prefetching, caching, datasource wrapper
255 // in the widevine:// case.
256 source = httpSource;
257 }
258 } else if (!strncasecmp("data:", uri, 5)) {
259 source = DataURISource::Create(uri);
260 } else {
261 // Assume it's a filename.
262 source = new FileSource(uri);
263 }
264
265 if (source == NULL || source->initCheck() != OK) {
266 return NULL;
267 }
268
269 return source;
270 }
271
CreateMediaHTTP(const sp<IMediaHTTPService> & httpService)272 sp<DataSource> DataSource::CreateMediaHTTP(const sp<IMediaHTTPService> &httpService) {
273 if (httpService == NULL) {
274 return NULL;
275 }
276
277 sp<IMediaHTTPConnection> conn = httpService->makeHTTPConnection();
278 if (conn == NULL) {
279 return NULL;
280 } else {
281 return new MediaHTTP(conn);
282 }
283 }
284
CreateFromIDataSource(const sp<IDataSource> & source)285 sp<DataSource> DataSource::CreateFromIDataSource(const sp<IDataSource> &source) {
286 return new TinyCacheSource(new CallbackDataSource(source));
287 }
288
getMIMEType() const289 String8 DataSource::getMIMEType() const {
290 return String8("application/octet-stream");
291 }
292
293 } // namespace android
294