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