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