DataSource.cpp revision 9d2f386dd2885eaffa11fd494ae258bb09fe6397
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#include "include/AVIExtractor.h"
19
20#if CHROMIUM_AVAILABLE
21#include "include/DataUriSource.h"
22#endif
23
24#include "include/MP3Extractor.h"
25#include "include/MPEG4Extractor.h"
26#include "include/WAVExtractor.h"
27#include "include/OggExtractor.h"
28#include "include/MPEG2PSExtractor.h"
29#include "include/MPEG2TSExtractor.h"
30#include "include/NuCachedSource2.h"
31#include "include/HTTPBase.h"
32#include "include/DRMExtractor.h"
33#include "include/FLACExtractor.h"
34#include "include/AACExtractor.h"
35#include "include/WVMExtractor.h"
36
37#include "matroska/MatroskaExtractor.h"
38
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/DataSource.h>
41#include <media/stagefright/FileSource.h>
42#include <media/stagefright/MediaErrors.h>
43#include <utils/String8.h>
44
45#include <cutils/properties.h>
46
47namespace android {
48
49bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
50    *x = 0;
51
52    uint8_t byte[2];
53    if (readAt(offset, byte, 2) != 2) {
54        return false;
55    }
56
57    *x = (byte[0] << 8) | byte[1];
58
59    return true;
60}
61
62status_t DataSource::getSize(off64_t *size) {
63    *size = 0;
64
65    return ERROR_UNSUPPORTED;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69
70Mutex DataSource::gSnifferMutex;
71List<DataSource::SnifferFunc> DataSource::gSniffers;
72
73bool DataSource::sniff(
74        String8 *mimeType, float *confidence, sp<AMessage> *meta) {
75    *mimeType = "";
76    *confidence = 0.0f;
77    meta->clear();
78
79    Mutex::Autolock autoLock(gSnifferMutex);
80    for (List<SnifferFunc>::iterator it = gSniffers.begin();
81         it != gSniffers.end(); ++it) {
82        String8 newMimeType;
83        float newConfidence;
84        sp<AMessage> newMeta;
85        if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
86            if (newConfidence > *confidence) {
87                *mimeType = newMimeType;
88                *confidence = newConfidence;
89                *meta = newMeta;
90            }
91        }
92    }
93
94    return *confidence > 0.0;
95}
96
97// static
98void DataSource::RegisterSniffer(SnifferFunc func) {
99    Mutex::Autolock autoLock(gSnifferMutex);
100
101    for (List<SnifferFunc>::iterator it = gSniffers.begin();
102         it != gSniffers.end(); ++it) {
103        if (*it == func) {
104            return;
105        }
106    }
107
108    gSniffers.push_back(func);
109}
110
111// static
112void DataSource::RegisterDefaultSniffers() {
113    RegisterSniffer(SniffMPEG4);
114    RegisterSniffer(SniffMatroska);
115    RegisterSniffer(SniffOgg);
116    RegisterSniffer(SniffWAV);
117    RegisterSniffer(SniffFLAC);
118    RegisterSniffer(SniffAMR);
119    RegisterSniffer(SniffMPEG2TS);
120    RegisterSniffer(SniffMP3);
121    RegisterSniffer(SniffAAC);
122    RegisterSniffer(SniffAVI);
123    RegisterSniffer(SniffMPEG2PS);
124    RegisterSniffer(SniffWVM);
125
126    char value[PROPERTY_VALUE_MAX];
127    if (property_get("drm.service.enabled", value, NULL)
128            && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
129        RegisterSniffer(SniffDRM);
130    }
131}
132
133// static
134sp<DataSource> DataSource::CreateFromURI(
135        const char *uri, const KeyedVector<String8, String8> *headers) {
136    sp<DataSource> source;
137    if (!strncasecmp("file://", uri, 7)) {
138        source = new FileSource(uri + 7);
139    } else if (!strncasecmp("http://", uri, 7)
140            || !strncasecmp("https://", uri, 8)) {
141        sp<HTTPBase> httpSource = HTTPBase::Create();
142        if (httpSource->connect(uri, headers) != OK) {
143            return NULL;
144        }
145        source = new NuCachedSource2(httpSource);
146# if CHROMIUM_AVAILABLE
147    } else if (!strncasecmp("data:", uri, 5)) {
148        source = new DataUriSource(uri);
149#endif
150    } else {
151        // Assume it's a filename.
152        source = new FileSource(uri);
153    }
154
155    if (source == NULL || source->initCheck() != OK) {
156        return NULL;
157    }
158
159    return source;
160}
161
162String8 DataSource::getMIMEType() const {
163    return String8("application/octet-stream");
164}
165
166}  // namespace android
167