blob: a66f86b885fb787d722594a708857cff806bd9f4 [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 Huber57515f32009-10-23 09:55:10 -070022
Andreas Huberaee3c632010-01-11 15:35:19 -080023#include <media/stagefright/CachingDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070024#include <media/stagefright/DataSource.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080025#include <media/stagefright/FileSource.h>
26#include <media/stagefright/HTTPDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070027#include <media/stagefright/MediaErrors.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070028#include <utils/String8.h>
29
30namespace android {
31
Andreas Huberbe06d262009-08-14 14:37:10 -070032bool DataSource::getUInt16(off_t offset, uint16_t *x) {
33 *x = 0;
34
35 uint8_t byte[2];
Andreas Huber9a12baf2009-10-23 10:22:30 -070036 if (readAt(offset, byte, 2) != 2) {
Andreas Huberbe06d262009-08-14 14:37:10 -070037 return false;
38 }
39
40 *x = (byte[0] << 8) | byte[1];
41
42 return true;
43}
44
Andreas Hubere46b7be2009-07-14 16:56:47 -070045status_t DataSource::getSize(off_t *size) {
46 *size = 0;
47
48 return ERROR_UNSUPPORTED;
49}
50
51////////////////////////////////////////////////////////////////////////////////
52
53Mutex DataSource::gSnifferMutex;
54List<DataSource::SnifferFunc> DataSource::gSniffers;
55
56bool 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
77void 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
91void DataSource::RegisterDefaultSniffers() {
92 RegisterSniffer(SniffMP3);
93 RegisterSniffer(SniffMPEG4);
Andreas Huber5a65a6e2009-09-08 16:07:15 -070094 RegisterSniffer(SniffAMR);
Andreas Huber6bce6d82009-11-03 16:00:58 -080095 RegisterSniffer(SniffWAV);
Andreas Huber388379f2010-05-07 10:35:13 -070096 RegisterSniffer(SniffOgg);
Andreas Hubere46b7be2009-07-14 16:56:47 -070097}
98
Andreas Huberaee3c632010-01-11 15:35:19 -080099// static
Andreas Huber433c9ac2010-01-27 16:49:05 -0800100sp<DataSource> DataSource::CreateFromURI(
101 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huberaee3c632010-01-11 15:35:19 -0800102 sp<DataSource> source;
103 if (!strncasecmp("file://", uri, 7)) {
104 source = new FileSource(uri + 7);
105 } else if (!strncasecmp("http://", uri, 7)) {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800106 sp<HTTPDataSource> httpSource = new HTTPDataSource(uri, headers);
107 if (httpSource->connect() != OK) {
108 return NULL;
109 }
Andreas Hubera51250b2010-04-08 07:51:20 -0700110 source = new CachingDataSource(httpSource, 64 * 1024, 10);
Andreas Huberaee3c632010-01-11 15:35:19 -0800111 } 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 Hubere46b7be2009-07-14 16:56:47 -0700123} // namespace android