DataSource.cpp revision 693d271e62a3726689ff68f4505ba49228eb94b2
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 <media/stagefright/DataSource.h>
18#include <media/stagefright/MediaErrors.h>
19#include <media/stagefright/MP3Extractor.h>
20#include <media/stagefright/MPEG4Extractor.h>
21#include <utils/String8.h>
22
23namespace android {
24
25bool DataSource::getUInt16(off_t offset, uint16_t *x) {
26    *x = 0;
27
28    uint8_t byte[2];
29    if (read_at(offset, byte, 2) != 2) {
30        return false;
31    }
32
33    *x = (byte[0] << 8) | byte[1];
34
35    return true;
36}
37
38status_t DataSource::getSize(off_t *size) {
39    *size = 0;
40
41    return ERROR_UNSUPPORTED;
42}
43
44////////////////////////////////////////////////////////////////////////////////
45
46Mutex DataSource::gSnifferMutex;
47List<DataSource::SnifferFunc> DataSource::gSniffers;
48
49bool DataSource::sniff(String8 *mimeType, float *confidence) {
50    *mimeType = "";
51    *confidence = 0.0f;
52
53    Mutex::Autolock autoLock(gSnifferMutex);
54    for (List<SnifferFunc>::iterator it = gSniffers.begin();
55         it != gSniffers.end(); ++it) {
56        String8 newMimeType;
57        float newConfidence;
58        if ((*it)(this, &newMimeType, &newConfidence)) {
59            if (newConfidence > *confidence) {
60                *mimeType = newMimeType;
61                *confidence = newConfidence;
62            }
63        }
64    }
65
66    return *confidence > 0.0;
67}
68
69// static
70void DataSource::RegisterSniffer(SnifferFunc func) {
71    Mutex::Autolock autoLock(gSnifferMutex);
72
73    for (List<SnifferFunc>::iterator it = gSniffers.begin();
74         it != gSniffers.end(); ++it) {
75        if (*it == func) {
76            return;
77        }
78    }
79
80    gSniffers.push_back(func);
81}
82
83// static
84void DataSource::RegisterDefaultSniffers() {
85    RegisterSniffer(SniffMP3);
86    RegisterSniffer(SniffMPEG4);
87}
88
89}  // namespace android
90