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