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