blob: 49eac62ae2f23542c40e514106e1e23d555ed227 [file] [log] [blame]
Andreas Hubere46b7be2009-07-14 16:56:47 -07001/*
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 Huber57515f32009-10-23 09:55:10 -070017#include "include/AMRExtractor.h"
18#include "include/MP3Extractor.h"
19#include "include/MPEG4Extractor.h"
Andreas Huber6bce6d82009-11-03 16:00:58 -080020#include "include/WAVExtractor.h"
Andreas Huber388379f2010-05-07 10:35:13 -070021#include "include/OggExtractor.h"
Andreas Hubera557b242010-06-07 13:05:37 -070022#include "include/MPEG2TSExtractor.h"
Andreas Huber4d61f602010-06-10 11:17:50 -070023#include "include/NuCachedSource2.h"
24#include "include/NuHTTPDataSource.h"
Andreas Huber57515f32009-10-23 09:55:10 -070025
Andreas Huber072f5242010-05-20 14:56:53 -070026#include "matroska/MatroskaExtractor.h"
27
Andreas Huberefdd0882010-08-25 11:09:41 -070028#include <media/stagefright/foundation/AMessage.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070029#include <media/stagefright/DataSource.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080030#include <media/stagefright/FileSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070031#include <media/stagefright/MediaErrors.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070032#include <utils/String8.h>
33
34namespace android {
35
Andreas Huberbe06d262009-08-14 14:37:10 -070036bool DataSource::getUInt16(off_t offset, uint16_t *x) {
37 *x = 0;
38
39 uint8_t byte[2];
Andreas Huber9a12baf2009-10-23 10:22:30 -070040 if (readAt(offset, byte, 2) != 2) {
Andreas Huberbe06d262009-08-14 14:37:10 -070041 return false;
42 }
43
44 *x = (byte[0] << 8) | byte[1];
45
46 return true;
47}
48
Andreas Hubere46b7be2009-07-14 16:56:47 -070049status_t DataSource::getSize(off_t *size) {
50 *size = 0;
51
52 return ERROR_UNSUPPORTED;
53}
54
55////////////////////////////////////////////////////////////////////////////////
56
57Mutex DataSource::gSnifferMutex;
58List<DataSource::SnifferFunc> DataSource::gSniffers;
59
Andreas Huberefdd0882010-08-25 11:09:41 -070060bool DataSource::sniff(
61 String8 *mimeType, float *confidence, sp<AMessage> *meta) {
Andreas Hubere46b7be2009-07-14 16:56:47 -070062 *mimeType = "";
63 *confidence = 0.0f;
Andreas Huberefdd0882010-08-25 11:09:41 -070064 meta->clear();
Andreas Hubere46b7be2009-07-14 16:56:47 -070065
66 Mutex::Autolock autoLock(gSnifferMutex);
67 for (List<SnifferFunc>::iterator it = gSniffers.begin();
68 it != gSniffers.end(); ++it) {
69 String8 newMimeType;
70 float newConfidence;
Andreas Huberefdd0882010-08-25 11:09:41 -070071 sp<AMessage> newMeta;
72 if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
Andreas Hubere46b7be2009-07-14 16:56:47 -070073 if (newConfidence > *confidence) {
74 *mimeType = newMimeType;
75 *confidence = newConfidence;
Andreas Huberefdd0882010-08-25 11:09:41 -070076 *meta = newMeta;
Andreas Hubere46b7be2009-07-14 16:56:47 -070077 }
78 }
79 }
80
81 return *confidence > 0.0;
82}
83
84// static
85void 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
99void DataSource::RegisterDefaultSniffers() {
Andreas Hubere46b7be2009-07-14 16:56:47 -0700100 RegisterSniffer(SniffMPEG4);
Andreas Huber072f5242010-05-20 14:56:53 -0700101 RegisterSniffer(SniffMatroska);
Andreas Huberefdd0882010-08-25 11:09:41 -0700102 RegisterSniffer(SniffOgg);
103 RegisterSniffer(SniffWAV);
104 RegisterSniffer(SniffAMR);
Andreas Hubera557b242010-06-07 13:05:37 -0700105 RegisterSniffer(SniffMPEG2TS);
Andreas Huberefdd0882010-08-25 11:09:41 -0700106 RegisterSniffer(SniffMP3);
Andreas Hubere46b7be2009-07-14 16:56:47 -0700107}
108
Andreas Huberaee3c632010-01-11 15:35:19 -0800109// static
Andreas Huber433c9ac2010-01-27 16:49:05 -0800110sp<DataSource> DataSource::CreateFromURI(
111 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huberaee3c632010-01-11 15:35:19 -0800112 sp<DataSource> source;
113 if (!strncasecmp("file://", uri, 7)) {
114 source = new FileSource(uri + 7);
115 } else if (!strncasecmp("http://", uri, 7)) {
Andreas Huber4d61f602010-06-10 11:17:50 -0700116 sp<NuHTTPDataSource> httpSource = new NuHTTPDataSource;
Andreas Huber3a53dc52010-06-11 09:57:46 -0700117 if (httpSource->connect(uri, headers) != OK) {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800118 return NULL;
119 }
Andreas Huber4d61f602010-06-10 11:17:50 -0700120 source = new NuCachedSource2(httpSource);
Andreas Huberaee3c632010-01-11 15:35:19 -0800121 } 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
Andreas Hubere46b7be2009-07-14 16:56:47 -0700133} // namespace android