• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "include/AMRExtractor.h"
18 
19 #if CHROMIUM_AVAILABLE
20 #include "include/chromium_http_stub.h"
21 #endif
22 
23 #include "include/AACExtractor.h"
24 #include "include/DRMExtractor.h"
25 #include "include/FLACExtractor.h"
26 #include "include/HTTPBase.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/stagefright/foundation/AMessage.h>
39 #include <media/stagefright/DataSource.h>
40 #include <media/stagefright/FileSource.h>
41 #include <media/stagefright/MediaErrors.h>
42 #include <utils/String8.h>
43 
44 #include <cutils/properties.h>
45 
46 namespace android {
47 
getUInt16(off64_t offset,uint16_t * x)48 bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
49     *x = 0;
50 
51     uint8_t byte[2];
52     if (readAt(offset, byte, 2) != 2) {
53         return false;
54     }
55 
56     *x = (byte[0] << 8) | byte[1];
57 
58     return true;
59 }
60 
getUInt24(off64_t offset,uint32_t * x)61 bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
62     *x = 0;
63 
64     uint8_t byte[3];
65     if (readAt(offset, byte, 3) != 3) {
66         return false;
67     }
68 
69     *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
70 
71     return true;
72 }
73 
getUInt32(off64_t offset,uint32_t * x)74 bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
75     *x = 0;
76 
77     uint32_t tmp;
78     if (readAt(offset, &tmp, 4) != 4) {
79         return false;
80     }
81 
82     *x = ntohl(tmp);
83 
84     return true;
85 }
86 
getUInt64(off64_t offset,uint64_t * x)87 bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
88     *x = 0;
89 
90     uint64_t tmp;
91     if (readAt(offset, &tmp, 8) != 8) {
92         return false;
93     }
94 
95     *x = ntoh64(tmp);
96 
97     return true;
98 }
99 
getSize(off64_t * size)100 status_t DataSource::getSize(off64_t *size) {
101     *size = 0;
102 
103     return ERROR_UNSUPPORTED;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 
108 Mutex DataSource::gSnifferMutex;
109 List<DataSource::SnifferFunc> DataSource::gSniffers;
110 
sniff(String8 * mimeType,float * confidence,sp<AMessage> * meta)111 bool DataSource::sniff(
112         String8 *mimeType, float *confidence, sp<AMessage> *meta) {
113     *mimeType = "";
114     *confidence = 0.0f;
115     meta->clear();
116 
117     Mutex::Autolock autoLock(gSnifferMutex);
118     for (List<SnifferFunc>::iterator it = gSniffers.begin();
119          it != gSniffers.end(); ++it) {
120         String8 newMimeType;
121         float newConfidence;
122         sp<AMessage> newMeta;
123         if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
124             if (newConfidence > *confidence) {
125                 *mimeType = newMimeType;
126                 *confidence = newConfidence;
127                 *meta = newMeta;
128             }
129         }
130     }
131 
132     return *confidence > 0.0;
133 }
134 
135 // static
RegisterSniffer(SnifferFunc func)136 void DataSource::RegisterSniffer(SnifferFunc func) {
137     Mutex::Autolock autoLock(gSnifferMutex);
138 
139     for (List<SnifferFunc>::iterator it = gSniffers.begin();
140          it != gSniffers.end(); ++it) {
141         if (*it == func) {
142             return;
143         }
144     }
145 
146     gSniffers.push_back(func);
147 }
148 
149 // static
RegisterDefaultSniffers()150 void DataSource::RegisterDefaultSniffers() {
151     RegisterSniffer(SniffMPEG4);
152     RegisterSniffer(SniffMatroska);
153     RegisterSniffer(SniffOgg);
154     RegisterSniffer(SniffWAV);
155     RegisterSniffer(SniffFLAC);
156     RegisterSniffer(SniffAMR);
157     RegisterSniffer(SniffMPEG2TS);
158     RegisterSniffer(SniffMP3);
159     RegisterSniffer(SniffAAC);
160     RegisterSniffer(SniffMPEG2PS);
161     RegisterSniffer(SniffWVM);
162 
163     char value[PROPERTY_VALUE_MAX];
164     if (property_get("drm.service.enabled", value, NULL)
165             && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
166         RegisterSniffer(SniffDRM);
167     }
168 }
169 
170 // static
CreateFromURI(const char * uri,const KeyedVector<String8,String8> * headers)171 sp<DataSource> DataSource::CreateFromURI(
172         const char *uri, const KeyedVector<String8, String8> *headers) {
173     bool isWidevine = !strncasecmp("widevine://", uri, 11);
174 
175     sp<DataSource> source;
176     if (!strncasecmp("file://", uri, 7)) {
177         source = new FileSource(uri + 7);
178     } else if (!strncasecmp("http://", uri, 7)
179             || !strncasecmp("https://", uri, 8)
180             || isWidevine) {
181         sp<HTTPBase> httpSource = HTTPBase::Create();
182 
183         String8 tmp;
184         if (isWidevine) {
185             tmp = String8("http://");
186             tmp.append(uri + 11);
187 
188             uri = tmp.string();
189         }
190 
191         if (httpSource->connect(uri, headers) != OK) {
192             return NULL;
193         }
194 
195         if (!isWidevine) {
196             String8 cacheConfig;
197             bool disconnectAtHighwatermark;
198             if (headers != NULL) {
199                 KeyedVector<String8, String8> copy = *headers;
200                 NuCachedSource2::RemoveCacheSpecificHeaders(
201                         &copy, &cacheConfig, &disconnectAtHighwatermark);
202             }
203 
204             source = new NuCachedSource2(
205                     httpSource,
206                     cacheConfig.isEmpty() ? NULL : cacheConfig.string());
207         } else {
208             // We do not want that prefetching, caching, datasource wrapper
209             // in the widevine:// case.
210             source = httpSource;
211         }
212 
213 # if CHROMIUM_AVAILABLE
214     } else if (!strncasecmp("data:", uri, 5)) {
215         source = createDataUriSource(uri);
216 #endif
217     } else {
218         // Assume it's a filename.
219         source = new FileSource(uri);
220     }
221 
222     if (source == NULL || source->initCheck() != OK) {
223         return NULL;
224     }
225 
226     return source;
227 }
228 
getMIMEType() const229 String8 DataSource::getMIMEType() const {
230     return String8("application/octet-stream");
231 }
232 
233 }  // namespace android
234