DataSource.cpp revision e51770946feca174d2d65811f773dcfc3d66ad3b
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 19#if CHROMIUM_AVAILABLE 20#include "include/DataUriSource.h" 21#endif 22 23#include "include/MP3Extractor.h" 24#include "include/MPEG4Extractor.h" 25#include "include/WAVExtractor.h" 26#include "include/OggExtractor.h" 27#include "include/MPEG2PSExtractor.h" 28#include "include/MPEG2TSExtractor.h" 29#include "include/NuCachedSource2.h" 30#include "include/HTTPBase.h" 31#include "include/DRMExtractor.h" 32#include "include/FLACExtractor.h" 33#include "include/AACExtractor.h" 34#include "include/WVMExtractor.h" 35 36#include "matroska/MatroskaExtractor.h" 37 38#include <media/stagefright/foundation/AMessage.h> 39#include <media/stagefright/DataSource.h> 40#include <media/stagefright/FileSource.h> 41#include <media/stagefright/MediaErrors.h> 42#include <utils/String8.h> 43 44#include <cutils/properties.h> 45 46namespace android { 47 48bool DataSource::getUInt16(off64_t offset, uint16_t *x) { 49 *x = 0; 50 51 uint8_t byte[2]; 52 if (readAt(offset, byte, 2) != 2) { 53 return false; 54 } 55 56 *x = (byte[0] << 8) | byte[1]; 57 58 return true; 59} 60 61status_t DataSource::getSize(off64_t *size) { 62 *size = 0; 63 64 return ERROR_UNSUPPORTED; 65} 66 67//////////////////////////////////////////////////////////////////////////////// 68 69Mutex DataSource::gSnifferMutex; 70List<DataSource::SnifferFunc> DataSource::gSniffers; 71 72bool DataSource::sniff( 73 String8 *mimeType, float *confidence, sp<AMessage> *meta) { 74 *mimeType = ""; 75 *confidence = 0.0f; 76 meta->clear(); 77 78 Mutex::Autolock autoLock(gSnifferMutex); 79 for (List<SnifferFunc>::iterator it = gSniffers.begin(); 80 it != gSniffers.end(); ++it) { 81 String8 newMimeType; 82 float newConfidence; 83 sp<AMessage> newMeta; 84 if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) { 85 if (newConfidence > *confidence) { 86 *mimeType = newMimeType; 87 *confidence = newConfidence; 88 *meta = newMeta; 89 } 90 } 91 } 92 93 return *confidence > 0.0; 94} 95 96// static 97void DataSource::RegisterSniffer(SnifferFunc func) { 98 Mutex::Autolock autoLock(gSnifferMutex); 99 100 for (List<SnifferFunc>::iterator it = gSniffers.begin(); 101 it != gSniffers.end(); ++it) { 102 if (*it == func) { 103 return; 104 } 105 } 106 107 gSniffers.push_back(func); 108} 109 110// static 111void DataSource::RegisterDefaultSniffers() { 112 RegisterSniffer(SniffMPEG4); 113 RegisterSniffer(SniffMatroska); 114 RegisterSniffer(SniffOgg); 115 RegisterSniffer(SniffWAV); 116 RegisterSniffer(SniffFLAC); 117 RegisterSniffer(SniffAMR); 118 RegisterSniffer(SniffMPEG2TS); 119 RegisterSniffer(SniffMP3); 120 RegisterSniffer(SniffAAC); 121 RegisterSniffer(SniffMPEG2PS); 122 RegisterSniffer(SniffWVM); 123 124 char value[PROPERTY_VALUE_MAX]; 125 if (property_get("drm.service.enabled", value, NULL) 126 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { 127 RegisterSniffer(SniffDRM); 128 } 129} 130 131// static 132sp<DataSource> DataSource::CreateFromURI( 133 const char *uri, const KeyedVector<String8, String8> *headers) { 134 bool isWidevine = !strncasecmp("widevine://", uri, 11); 135 136 sp<DataSource> source; 137 if (!strncasecmp("file://", uri, 7)) { 138 source = new FileSource(uri + 7); 139 } else if (!strncasecmp("http://", uri, 7) 140 || !strncasecmp("https://", uri, 8) 141 || isWidevine) { 142 sp<HTTPBase> httpSource = HTTPBase::Create(); 143 144 String8 tmp; 145 if (isWidevine) { 146 tmp = String8("http://"); 147 tmp.append(uri + 11); 148 149 uri = tmp.string(); 150 } 151 152 if (httpSource->connect(uri, headers) != OK) { 153 return NULL; 154 } 155 156 if (!isWidevine) { 157 String8 cacheConfig; 158 bool disconnectAtHighwatermark; 159 if (headers != NULL) { 160 KeyedVector<String8, String8> copy = *headers; 161 NuCachedSource2::RemoveCacheSpecificHeaders( 162 ©, &cacheConfig, &disconnectAtHighwatermark); 163 } 164 165 source = new NuCachedSource2( 166 httpSource, 167 cacheConfig.isEmpty() ? NULL : cacheConfig.string()); 168 } else { 169 // We do not want that prefetching, caching, datasource wrapper 170 // in the widevine:// case. 171 source = httpSource; 172 } 173 174# if CHROMIUM_AVAILABLE 175 } else if (!strncasecmp("data:", uri, 5)) { 176 source = new DataUriSource(uri); 177#endif 178 } else { 179 // Assume it's a filename. 180 source = new FileSource(uri); 181 } 182 183 if (source == NULL || source->initCheck() != OK) { 184 return NULL; 185 } 186 187 return source; 188} 189 190String8 DataSource::getMIMEType() const { 191 return String8("application/octet-stream"); 192} 193 194} // namespace android 195