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/FragmentedMP4Extractor.h"
27#include "include/HTTPBase.h"
28#include "include/MP3Extractor.h"
29#include "include/MPEG2PSExtractor.h"
30#include "include/MPEG2TSExtractor.h"
31#include "include/MPEG4Extractor.h"
32#include "include/NuCachedSource2.h"
33#include "include/OggExtractor.h"
34#include "include/WAVExtractor.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(SniffFragmentedMP4);
115    RegisterSniffer(SniffMatroska);
116    RegisterSniffer(SniffOgg);
117    RegisterSniffer(SniffWAV);
118    RegisterSniffer(SniffFLAC);
119    RegisterSniffer(SniffAMR);
120    RegisterSniffer(SniffMPEG2TS);
121    RegisterSniffer(SniffMP3);
122    RegisterSniffer(SniffAAC);
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    bool isWidevine = !strncasecmp("widevine://", uri, 11);
137
138    sp<DataSource> source;
139    if (!strncasecmp("file://", uri, 7)) {
140        source = new FileSource(uri + 7);
141    } else if (!strncasecmp("http://", uri, 7)
142            || !strncasecmp("https://", uri, 8)
143            || isWidevine) {
144        sp<HTTPBase> httpSource = HTTPBase::Create();
145
146        String8 tmp;
147        if (isWidevine) {
148            tmp = String8("http://");
149            tmp.append(uri + 11);
150
151            uri = tmp.string();
152        }
153
154        if (httpSource->connect(uri, headers) != OK) {
155            return NULL;
156        }
157
158        if (!isWidevine) {
159            String8 cacheConfig;
160            bool disconnectAtHighwatermark;
161            if (headers != NULL) {
162                KeyedVector<String8, String8> copy = *headers;
163                NuCachedSource2::RemoveCacheSpecificHeaders(
164                        &copy, &cacheConfig, &disconnectAtHighwatermark);
165            }
166
167            source = new NuCachedSource2(
168                    httpSource,
169                    cacheConfig.isEmpty() ? NULL : cacheConfig.string());
170        } else {
171            // We do not want that prefetching, caching, datasource wrapper
172            // in the widevine:// case.
173            source = httpSource;
174        }
175
176# if CHROMIUM_AVAILABLE
177    } else if (!strncasecmp("data:", uri, 5)) {
178        source = createDataUriSource(uri);
179#endif
180    } else {
181        // Assume it's a filename.
182        source = new FileSource(uri);
183    }
184
185    if (source == NULL || source->initCheck() != OK) {
186        return NULL;
187    }
188
189    return source;
190}
191
192String8 DataSource::getMIMEType() const {
193    return String8("application/octet-stream");
194}
195
196}  // namespace android
197