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