DataSource.cpp revision d354d8d1b09503c0166c1f3e626cda72a3eeb83c
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//#define LOG_NDEBUG 0 17#define LOG_TAG "DataSource" 18 19#include "include/AMRExtractor.h" 20 21#include "include/AACExtractor.h" 22#include "include/DRMExtractor.h" 23#include "include/FLACExtractor.h" 24#include "include/HTTPBase.h" 25#include "include/MP3Extractor.h" 26#include "include/MPEG2PSExtractor.h" 27#include "include/MPEG2TSExtractor.h" 28#include "include/MPEG4Extractor.h" 29#include "include/NuCachedSource2.h" 30#include "include/OggExtractor.h" 31#include "include/WAVExtractor.h" 32#include "include/WVMExtractor.h" 33 34#include "matroska/MatroskaExtractor.h" 35 36#include <media/IMediaHTTPConnection.h> 37#include <media/IMediaHTTPService.h> 38#include <media/stagefright/foundation/ADebug.h> 39#include <media/stagefright/foundation/AMessage.h> 40#include <media/stagefright/DataSource.h> 41#include <media/stagefright/DataURISource.h> 42#include <media/stagefright/FileSource.h> 43#include <media/stagefright/MediaErrors.h> 44#include <media/stagefright/MediaHTTP.h> 45#include <utils/String8.h> 46 47#include <cutils/properties.h> 48 49namespace android { 50 51bool DataSource::getUInt16(off64_t offset, uint16_t *x) { 52 *x = 0; 53 54 uint8_t byte[2]; 55 if (readAt(offset, byte, 2) != 2) { 56 return false; 57 } 58 59 *x = (byte[0] << 8) | byte[1]; 60 61 return true; 62} 63 64bool DataSource::getUInt24(off64_t offset, uint32_t *x) { 65 *x = 0; 66 67 uint8_t byte[3]; 68 if (readAt(offset, byte, 3) != 3) { 69 return false; 70 } 71 72 *x = (byte[0] << 16) | (byte[1] << 8) | byte[2]; 73 74 return true; 75} 76 77bool DataSource::getUInt32(off64_t offset, uint32_t *x) { 78 *x = 0; 79 80 uint32_t tmp; 81 if (readAt(offset, &tmp, 4) != 4) { 82 return false; 83 } 84 85 *x = ntohl(tmp); 86 87 return true; 88} 89 90bool DataSource::getUInt64(off64_t offset, uint64_t *x) { 91 *x = 0; 92 93 uint64_t tmp; 94 if (readAt(offset, &tmp, 8) != 8) { 95 return false; 96 } 97 98 *x = ntoh64(tmp); 99 100 return true; 101} 102 103status_t DataSource::getSize(off64_t *size) { 104 *size = 0; 105 106 return ERROR_UNSUPPORTED; 107} 108 109//////////////////////////////////////////////////////////////////////////////// 110 111Mutex DataSource::gSnifferMutex; 112List<DataSource::SnifferFunc> DataSource::gSniffers; 113bool DataSource::gSniffersRegistered = false; 114 115bool DataSource::sniff( 116 String8 *mimeType, float *confidence, sp<AMessage> *meta) { 117 *mimeType = ""; 118 *confidence = 0.0f; 119 meta->clear(); 120 121 { 122 Mutex::Autolock autoLock(gSnifferMutex); 123 if (!gSniffersRegistered) { 124 return false; 125 } 126 } 127 128 for (List<SnifferFunc>::iterator it = gSniffers.begin(); 129 it != gSniffers.end(); ++it) { 130 String8 newMimeType; 131 float newConfidence; 132 sp<AMessage> newMeta; 133 if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) { 134 if (newConfidence > *confidence) { 135 *mimeType = newMimeType; 136 *confidence = newConfidence; 137 *meta = newMeta; 138 } 139 } 140 } 141 142 return *confidence > 0.0; 143} 144 145// static 146void DataSource::RegisterSniffer_l(SnifferFunc func) { 147 for (List<SnifferFunc>::iterator it = gSniffers.begin(); 148 it != gSniffers.end(); ++it) { 149 if (*it == func) { 150 return; 151 } 152 } 153 154 gSniffers.push_back(func); 155} 156 157// static 158void DataSource::RegisterDefaultSniffers() { 159 Mutex::Autolock autoLock(gSnifferMutex); 160 if (gSniffersRegistered) { 161 return; 162 } 163 164 RegisterSniffer_l(SniffMPEG4); 165 RegisterSniffer_l(SniffMatroska); 166 RegisterSniffer_l(SniffOgg); 167 RegisterSniffer_l(SniffWAV); 168 RegisterSniffer_l(SniffFLAC); 169 RegisterSniffer_l(SniffAMR); 170 RegisterSniffer_l(SniffMPEG2TS); 171 RegisterSniffer_l(SniffMP3); 172 RegisterSniffer_l(SniffAAC); 173 RegisterSniffer_l(SniffMPEG2PS); 174 RegisterSniffer_l(SniffWVM); 175 176 char value[PROPERTY_VALUE_MAX]; 177 if (property_get("drm.service.enabled", value, NULL) 178 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { 179 RegisterSniffer_l(SniffDRM); 180 } 181 gSniffersRegistered = true; 182} 183 184// static 185sp<DataSource> DataSource::CreateFromURI( 186 const sp<IMediaHTTPService> &httpService, 187 const char *uri, 188 const KeyedVector<String8, String8> *headers, 189 String8 *contentType) { 190 if (contentType != NULL) { 191 *contentType = ""; 192 } 193 194 bool isWidevine = !strncasecmp("widevine://", uri, 11); 195 196 sp<DataSource> source; 197 if (!strncasecmp("file://", uri, 7)) { 198 source = new FileSource(uri + 7); 199 } else if (!strncasecmp("http://", uri, 7) 200 || !strncasecmp("https://", uri, 8) 201 || isWidevine) { 202 sp<HTTPBase> httpSource = new MediaHTTP(httpService->makeHTTPConnection()); 203 204 String8 tmp; 205 if (isWidevine) { 206 tmp = String8("http://"); 207 tmp.append(uri + 11); 208 209 uri = tmp.string(); 210 } 211 212 String8 cacheConfig; 213 bool disconnectAtHighwatermark; 214 KeyedVector<String8, String8> nonCacheSpecificHeaders; 215 if (headers != NULL) { 216 nonCacheSpecificHeaders = *headers; 217 NuCachedSource2::RemoveCacheSpecificHeaders( 218 &nonCacheSpecificHeaders, 219 &cacheConfig, 220 &disconnectAtHighwatermark); 221 } 222 223 if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) { 224 ALOGE("Failed to connect http source!"); 225 return NULL; 226 } 227 228 if (!isWidevine) { 229 if (contentType != NULL) { 230 *contentType = httpSource->getMIMEType(); 231 } 232 233 source = new NuCachedSource2( 234 httpSource, 235 cacheConfig.isEmpty() ? NULL : cacheConfig.string(), 236 disconnectAtHighwatermark); 237 } else { 238 // We do not want that prefetching, caching, datasource wrapper 239 // in the widevine:// case. 240 source = httpSource; 241 } 242 } else if (!strncasecmp("data:", uri, 5)) { 243 source = DataURISource::Create(uri); 244 } else { 245 // Assume it's a filename. 246 source = new FileSource(uri); 247 } 248 249 if (source == NULL || source->initCheck() != OK) { 250 return NULL; 251 } 252 253 return source; 254} 255 256String8 DataSource::getMIMEType() const { 257 return String8("application/octet-stream"); 258} 259 260} // namespace android 261