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