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