Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
Andreas Huber | 57515f3 | 2009-10-23 09:55:10 -0700 | [diff] [blame] | 17 | #include "include/AMRExtractor.h" |
| 18 | #include "include/MP3Extractor.h" |
| 19 | #include "include/MPEG4Extractor.h" |
Andreas Huber | 6bce6d8 | 2009-11-03 16:00:58 -0800 | [diff] [blame] | 20 | #include "include/WAVExtractor.h" |
Andreas Huber | eb5eef3 | 2010-05-04 11:46:42 -0700 | [diff] [blame^] | 21 | #include "include/VorbisExtractor.h" |
Andreas Huber | 57515f3 | 2009-10-23 09:55:10 -0700 | [diff] [blame] | 22 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 23 | #include <media/stagefright/CachingDataSource.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 24 | #include <media/stagefright/DataSource.h> |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 25 | #include <media/stagefright/FileSource.h> |
| 26 | #include <media/stagefright/HTTPDataSource.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 27 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 28 | #include <utils/String8.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 32 | bool DataSource::getUInt16(off_t offset, uint16_t *x) { |
| 33 | *x = 0; |
| 34 | |
| 35 | uint8_t byte[2]; |
Andreas Huber | 9a12baf | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 36 | if (readAt(offset, byte, 2) != 2) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
| 40 | *x = (byte[0] << 8) | byte[1]; |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 45 | status_t DataSource::getSize(off_t *size) { |
| 46 | *size = 0; |
| 47 | |
| 48 | return ERROR_UNSUPPORTED; |
| 49 | } |
| 50 | |
| 51 | //////////////////////////////////////////////////////////////////////////////// |
| 52 | |
| 53 | Mutex DataSource::gSnifferMutex; |
| 54 | List<DataSource::SnifferFunc> DataSource::gSniffers; |
| 55 | |
| 56 | bool DataSource::sniff(String8 *mimeType, float *confidence) { |
| 57 | *mimeType = ""; |
| 58 | *confidence = 0.0f; |
| 59 | |
| 60 | Mutex::Autolock autoLock(gSnifferMutex); |
| 61 | for (List<SnifferFunc>::iterator it = gSniffers.begin(); |
| 62 | it != gSniffers.end(); ++it) { |
| 63 | String8 newMimeType; |
| 64 | float newConfidence; |
| 65 | if ((*it)(this, &newMimeType, &newConfidence)) { |
| 66 | if (newConfidence > *confidence) { |
| 67 | *mimeType = newMimeType; |
| 68 | *confidence = newConfidence; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return *confidence > 0.0; |
| 74 | } |
| 75 | |
| 76 | // static |
| 77 | void DataSource::RegisterSniffer(SnifferFunc func) { |
| 78 | Mutex::Autolock autoLock(gSnifferMutex); |
| 79 | |
| 80 | for (List<SnifferFunc>::iterator it = gSniffers.begin(); |
| 81 | it != gSniffers.end(); ++it) { |
| 82 | if (*it == func) { |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | gSniffers.push_back(func); |
| 88 | } |
| 89 | |
| 90 | // static |
| 91 | void DataSource::RegisterDefaultSniffers() { |
| 92 | RegisterSniffer(SniffMP3); |
| 93 | RegisterSniffer(SniffMPEG4); |
Andreas Huber | 5a65a6e | 2009-09-08 16:07:15 -0700 | [diff] [blame] | 94 | RegisterSniffer(SniffAMR); |
Andreas Huber | 6bce6d8 | 2009-11-03 16:00:58 -0800 | [diff] [blame] | 95 | RegisterSniffer(SniffWAV); |
Andreas Huber | eb5eef3 | 2010-05-04 11:46:42 -0700 | [diff] [blame^] | 96 | RegisterSniffer(SniffVorbis); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 99 | // static |
Andreas Huber | 433c9ac | 2010-01-27 16:49:05 -0800 | [diff] [blame] | 100 | sp<DataSource> DataSource::CreateFromURI( |
| 101 | const char *uri, const KeyedVector<String8, String8> *headers) { |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 102 | sp<DataSource> source; |
| 103 | if (!strncasecmp("file://", uri, 7)) { |
| 104 | source = new FileSource(uri + 7); |
| 105 | } else if (!strncasecmp("http://", uri, 7)) { |
Andreas Huber | edbb4d8 | 2010-03-12 08:59:22 -0800 | [diff] [blame] | 106 | sp<HTTPDataSource> httpSource = new HTTPDataSource(uri, headers); |
| 107 | if (httpSource->connect() != OK) { |
| 108 | return NULL; |
| 109 | } |
Andreas Huber | a51250b | 2010-04-08 07:51:20 -0700 | [diff] [blame] | 110 | source = new CachingDataSource(httpSource, 64 * 1024, 10); |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 111 | } else { |
| 112 | // Assume it's a filename. |
| 113 | source = new FileSource(uri); |
| 114 | } |
| 115 | |
| 116 | if (source == NULL || source->initCheck() != OK) { |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | return source; |
| 121 | } |
| 122 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 123 | } // namespace android |