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