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