blob: 90a596cc06ab17d54885acc8fa3a1654cfa8d6ad [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 Hubere46b7be2009-07-14 16:56:47 -070028#include <media/stagefright/DataSource.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080029#include <media/stagefright/FileSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070030#include <media/stagefright/MediaErrors.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070031#include <utils/String8.h>
32
33namespace android {
34
Andreas Huberbe06d262009-08-14 14:37:10 -070035bool DataSource::getUInt16(off_t offset, uint16_t *x) {
36 *x = 0;
37
38 uint8_t byte[2];
Andreas Huber9a12baf2009-10-23 10:22:30 -070039 if (readAt(offset, byte, 2) != 2) {
Andreas Huberbe06d262009-08-14 14:37:10 -070040 return false;
41 }
42
43 *x = (byte[0] << 8) | byte[1];
44
45 return true;
46}
47
Andreas Hubere46b7be2009-07-14 16:56:47 -070048status_t DataSource::getSize(off_t *size) {
49 *size = 0;
50
51 return ERROR_UNSUPPORTED;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55
56Mutex DataSource::gSnifferMutex;
57List<DataSource::SnifferFunc> DataSource::gSniffers;
58
59bool DataSource::sniff(String8 *mimeType, float *confidence) {
60 *mimeType = "";
61 *confidence = 0.0f;
62
63 Mutex::Autolock autoLock(gSnifferMutex);
64 for (List<SnifferFunc>::iterator it = gSniffers.begin();
65 it != gSniffers.end(); ++it) {
66 String8 newMimeType;
67 float newConfidence;
68 if ((*it)(this, &newMimeType, &newConfidence)) {
69 if (newConfidence > *confidence) {
70 *mimeType = newMimeType;
71 *confidence = newConfidence;
72 }
73 }
74 }
75
76 return *confidence > 0.0;
77}
78
79// static
80void DataSource::RegisterSniffer(SnifferFunc func) {
81 Mutex::Autolock autoLock(gSnifferMutex);
82
83 for (List<SnifferFunc>::iterator it = gSniffers.begin();
84 it != gSniffers.end(); ++it) {
85 if (*it == func) {
86 return;
87 }
88 }
89
90 gSniffers.push_back(func);
91}
92
93// static
94void DataSource::RegisterDefaultSniffers() {
95 RegisterSniffer(SniffMP3);
96 RegisterSniffer(SniffMPEG4);
Andreas Huber5a65a6e2009-09-08 16:07:15 -070097 RegisterSniffer(SniffAMR);
Andreas Huber6bce6d82009-11-03 16:00:58 -080098 RegisterSniffer(SniffWAV);
Andreas Huber388379f2010-05-07 10:35:13 -070099 RegisterSniffer(SniffOgg);
Andreas Huber072f5242010-05-20 14:56:53 -0700100 RegisterSniffer(SniffMatroska);
Andreas Hubera557b242010-06-07 13:05:37 -0700101 RegisterSniffer(SniffMPEG2TS);
Andreas Hubere46b7be2009-07-14 16:56:47 -0700102}
103
Andreas Huberaee3c632010-01-11 15:35:19 -0800104// static
Andreas Huber433c9ac2010-01-27 16:49:05 -0800105sp<DataSource> DataSource::CreateFromURI(
106 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huberaee3c632010-01-11 15:35:19 -0800107 sp<DataSource> source;
108 if (!strncasecmp("file://", uri, 7)) {
109 source = new FileSource(uri + 7);
110 } else if (!strncasecmp("http://", uri, 7)) {
Andreas Huber4d61f602010-06-10 11:17:50 -0700111 sp<NuHTTPDataSource> httpSource = new NuHTTPDataSource;
Andreas Huber3a53dc52010-06-11 09:57:46 -0700112 if (httpSource->connect(uri, headers) != OK) {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800113 return NULL;
114 }
Andreas Huber4d61f602010-06-10 11:17:50 -0700115 source = new NuCachedSource2(httpSource);
Andreas Huberaee3c632010-01-11 15:35:19 -0800116 } else {
117 // Assume it's a filename.
118 source = new FileSource(uri);
119 }
120
121 if (source == NULL || source->initCheck() != OK) {
122 return NULL;
123 }
124
125 return source;
126}
127
Andreas Hubere46b7be2009-07-14 16:56:47 -0700128} // namespace android