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