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