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