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