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