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