DataSource.cpp revision 95b980dcc3aa2af687da2ab4e4ad2286a8c2040b
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/DRMExtractor.h"
23#include "include/FLACExtractor.h"
24#include "include/HTTPBase.h"
25#include "include/MP3Extractor.h"
26#include "include/MPEG2PSExtractor.h"
27#include "include/MPEG2TSExtractor.h"
28#include "include/MPEG4Extractor.h"
29#include "include/NuCachedSource2.h"
30#include "include/OggExtractor.h"
31#include "include/WAVExtractor.h"
32#include "include/WVMExtractor.h"
33
34#include "matroska/MatroskaExtractor.h"
35
36#include <media/IMediaHTTPConnection.h>
37#include <media/IMediaHTTPService.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/DataSource.h>
41#include <media/stagefright/DataURISource.h>
42#include <media/stagefright/FileSource.h>
43#include <media/stagefright/MediaErrors.h>
44#include <media/stagefright/MediaHTTP.h>
45#include <utils/String8.h>
46
47#include <cutils/properties.h>
48
49namespace android {
50
51bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
52    *x = 0;
53
54    uint8_t byte[2];
55    if (readAt(offset, byte, 2) != 2) {
56        return false;
57    }
58
59    *x = (byte[0] << 8) | byte[1];
60
61    return true;
62}
63
64bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
65    *x = 0;
66
67    uint8_t byte[3];
68    if (readAt(offset, byte, 3) != 3) {
69        return false;
70    }
71
72    *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
73
74    return true;
75}
76
77bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
78    *x = 0;
79
80    uint32_t tmp;
81    if (readAt(offset, &tmp, 4) != 4) {
82        return false;
83    }
84
85    *x = ntohl(tmp);
86
87    return true;
88}
89
90bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
91    *x = 0;
92
93    uint64_t tmp;
94    if (readAt(offset, &tmp, 8) != 8) {
95        return false;
96    }
97
98    *x = ntoh64(tmp);
99
100    return true;
101}
102
103status_t DataSource::getSize(off64_t *size) {
104    *size = 0;
105
106    return ERROR_UNSUPPORTED;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110
111Mutex DataSource::gSnifferMutex;
112List<DataSource::SnifferFunc> DataSource::gSniffers;
113bool DataSource::gSniffersRegistered = false;
114
115bool DataSource::sniff(
116        String8 *mimeType, float *confidence, sp<AMessage> *meta) {
117    *mimeType = "";
118    *confidence = 0.0f;
119    meta->clear();
120
121    {
122        Mutex::Autolock autoLock(gSnifferMutex);
123        if (!gSniffersRegistered) {
124            return false;
125        }
126    }
127
128    for (List<SnifferFunc>::iterator it = gSniffers.begin();
129         it != gSniffers.end(); ++it) {
130        String8 newMimeType;
131        float newConfidence;
132        sp<AMessage> newMeta;
133        if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
134            if (newConfidence > *confidence) {
135                *mimeType = newMimeType;
136                *confidence = newConfidence;
137                *meta = newMeta;
138            }
139        }
140    }
141
142    return *confidence > 0.0;
143}
144
145// static
146void DataSource::RegisterSniffer_l(SnifferFunc func) {
147    for (List<SnifferFunc>::iterator it = gSniffers.begin();
148         it != gSniffers.end(); ++it) {
149        if (*it == func) {
150            return;
151        }
152    }
153
154    gSniffers.push_back(func);
155}
156
157// static
158void DataSource::RegisterDefaultSniffers() {
159    Mutex::Autolock autoLock(gSnifferMutex);
160    if (gSniffersRegistered) {
161        return;
162    }
163
164    RegisterSniffer_l(SniffMPEG4);
165    RegisterSniffer_l(SniffMatroska);
166    RegisterSniffer_l(SniffOgg);
167    RegisterSniffer_l(SniffWAV);
168    RegisterSniffer_l(SniffFLAC);
169    RegisterSniffer_l(SniffAMR);
170    RegisterSniffer_l(SniffMPEG2TS);
171    RegisterSniffer_l(SniffMP3);
172    RegisterSniffer_l(SniffAAC);
173    RegisterSniffer_l(SniffMPEG2PS);
174    RegisterSniffer_l(SniffWVM);
175
176    char value[PROPERTY_VALUE_MAX];
177    if (property_get("drm.service.enabled", value, NULL)
178            && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
179        RegisterSniffer_l(SniffDRM);
180    }
181    gSniffersRegistered = true;
182}
183
184// static
185sp<DataSource> DataSource::CreateFromURI(
186        const sp<IMediaHTTPService> &httpService,
187        const char *uri,
188        const KeyedVector<String8, String8> *headers,
189        AString *sniffedMIME) {
190    if (sniffedMIME != NULL) {
191        *sniffedMIME = "";
192    }
193
194    bool isWidevine = !strncasecmp("widevine://", uri, 11);
195
196    sp<DataSource> source;
197    if (!strncasecmp("file://", uri, 7)) {
198        source = new FileSource(uri + 7);
199    } else if (!strncasecmp("http://", uri, 7)
200            || !strncasecmp("https://", uri, 8)
201            || isWidevine) {
202        sp<HTTPBase> httpSource = new MediaHTTP(httpService->makeHTTPConnection());
203
204        String8 tmp;
205        if (isWidevine) {
206            tmp = String8("http://");
207            tmp.append(uri + 11);
208
209            uri = tmp.string();
210        }
211
212        if (httpSource->connect(uri, headers) != OK) {
213            ALOGE("Failed to connect http source!");
214            return NULL;
215        }
216
217        if (!isWidevine) {
218            String8 cacheConfig;
219            bool disconnectAtHighwatermark;
220            if (headers != NULL) {
221                KeyedVector<String8, String8> copy = *headers;
222                NuCachedSource2::RemoveCacheSpecificHeaders(
223                        &copy, &cacheConfig, &disconnectAtHighwatermark);
224            }
225
226            sp<NuCachedSource2> cachedSource = new NuCachedSource2(
227                    httpSource,
228                    cacheConfig.isEmpty() ? NULL : cacheConfig.string());
229
230            String8 contentType = httpSource->getMIMEType();
231
232            if (strncasecmp(contentType.string(), "audio/", 6)) {
233                // We're not doing this for streams that appear to be audio-only
234                // streams to ensure that even low bandwidth streams start
235                // playing back fairly instantly.
236
237                // We're going to prefill the cache before trying to instantiate
238                // the extractor below, as the latter is an operation that otherwise
239                // could block on the datasource for a significant amount of time.
240                // During that time we'd be unable to abort the preparation phase
241                // without this prefill.
242
243                // Initially make sure we have at least 192 KB for the sniff
244                // to complete without blocking.
245                static const size_t kMinBytesForSniffing = 192 * 1024;
246
247                off64_t metaDataSize = -1ll;
248                for (;;) {
249                    status_t finalStatus;
250                    size_t cachedDataRemaining =
251                            cachedSource->approxDataRemaining(&finalStatus);
252
253                    if (finalStatus != OK || (metaDataSize >= 0
254                            && (off64_t)cachedDataRemaining >= metaDataSize)) {
255                        ALOGV("stop caching, status %d, "
256                                "metaDataSize %lld, cachedDataRemaining %zu",
257                                finalStatus, metaDataSize, cachedDataRemaining);
258                        break;
259                    }
260
261                    ALOGV("now cached %zu bytes of data", cachedDataRemaining);
262
263                    if (metaDataSize < 0
264                            && cachedDataRemaining >= kMinBytesForSniffing) {
265                        String8 tmp;
266                        float confidence;
267                        sp<AMessage> meta;
268                        if (!cachedSource->sniff(&tmp, &confidence, &meta)) {
269                            return NULL;
270                        }
271
272                        // We successfully identified the file's extractor to
273                        // be, remember this mime type so we don't have to
274                        // sniff it again when we call MediaExtractor::Create()
275                        if (sniffedMIME != NULL) {
276                            *sniffedMIME = tmp.string();
277                        }
278
279                        if (meta == NULL
280                                || !meta->findInt64("meta-data-size",
281                                     reinterpret_cast<int64_t*>(&metaDataSize))) {
282                            metaDataSize = kDefaultMetaSize;
283                        }
284
285                        if (metaDataSize < 0ll) {
286                            ALOGE("invalid metaDataSize = %lld bytes", metaDataSize);
287                            return NULL;
288                        }
289                    }
290
291                    usleep(200000);
292                }
293            }
294
295            source = cachedSource;
296        } else {
297            // We do not want that prefetching, caching, datasource wrapper
298            // in the widevine:// case.
299            source = httpSource;
300        }
301    } else if (!strncasecmp("data:", uri, 5)) {
302        source = DataURISource::Create(uri);
303    } else {
304        // Assume it's a filename.
305        source = new FileSource(uri);
306    }
307
308    if (source == NULL || source->initCheck() != OK) {
309        return NULL;
310    }
311
312    return source;
313}
314
315String8 DataSource::getMIMEType() const {
316    return String8("application/octet-stream");
317}
318
319}  // namespace android
320