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