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