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