blob: 475422dff0ad37be3f25c50fb5a220f2ee392c13 [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 Huber072f5242010-05-20 14:56:53 -070023#include "matroska/MatroskaExtractor.h"
24
Andreas Huberaee3c632010-01-11 15:35:19 -080025#include <media/stagefright/CachingDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070026#include <media/stagefright/DataSource.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080027#include <media/stagefright/FileSource.h>
28#include <media/stagefright/HTTPDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070029#include <media/stagefright/MediaErrors.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070030#include <utils/String8.h>
31
32namespace android {
33
Andreas Huberbe06d262009-08-14 14:37:10 -070034bool DataSource::getUInt16(off_t offset, uint16_t *x) {
35 *x = 0;
36
37 uint8_t byte[2];
Andreas Huber9a12baf2009-10-23 10:22:30 -070038 if (readAt(offset, byte, 2) != 2) {
Andreas Huberbe06d262009-08-14 14:37:10 -070039 return false;
40 }
41
42 *x = (byte[0] << 8) | byte[1];
43
44 return true;
45}
46
Andreas Hubere46b7be2009-07-14 16:56:47 -070047status_t DataSource::getSize(off_t *size) {
48 *size = 0;
49
50 return ERROR_UNSUPPORTED;
51}
52
53////////////////////////////////////////////////////////////////////////////////
54
55Mutex DataSource::gSnifferMutex;
56List<DataSource::SnifferFunc> DataSource::gSniffers;
57
58bool DataSource::sniff(String8 *mimeType, float *confidence) {
59 *mimeType = "";
60 *confidence = 0.0f;
61
62 Mutex::Autolock autoLock(gSnifferMutex);
63 for (List<SnifferFunc>::iterator it = gSniffers.begin();
64 it != gSniffers.end(); ++it) {
65 String8 newMimeType;
66 float newConfidence;
67 if ((*it)(this, &newMimeType, &newConfidence)) {
68 if (newConfidence > *confidence) {
69 *mimeType = newMimeType;
70 *confidence = newConfidence;
71 }
72 }
73 }
74
75 return *confidence > 0.0;
76}
77
78// static
79void DataSource::RegisterSniffer(SnifferFunc func) {
80 Mutex::Autolock autoLock(gSnifferMutex);
81
82 for (List<SnifferFunc>::iterator it = gSniffers.begin();
83 it != gSniffers.end(); ++it) {
84 if (*it == func) {
85 return;
86 }
87 }
88
89 gSniffers.push_back(func);
90}
91
92// static
93void DataSource::RegisterDefaultSniffers() {
94 RegisterSniffer(SniffMP3);
95 RegisterSniffer(SniffMPEG4);
Andreas Huber5a65a6e2009-09-08 16:07:15 -070096 RegisterSniffer(SniffAMR);
Andreas Huber6bce6d82009-11-03 16:00:58 -080097 RegisterSniffer(SniffWAV);
Andreas Huber388379f2010-05-07 10:35:13 -070098 RegisterSniffer(SniffOgg);
Andreas Huber072f5242010-05-20 14:56:53 -070099 RegisterSniffer(SniffMatroska);
Andreas Hubere46b7be2009-07-14 16:56:47 -0700100}
101
Andreas Huberaee3c632010-01-11 15:35:19 -0800102// static
Andreas Huber433c9ac2010-01-27 16:49:05 -0800103sp<DataSource> DataSource::CreateFromURI(
104 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huberaee3c632010-01-11 15:35:19 -0800105 sp<DataSource> source;
106 if (!strncasecmp("file://", uri, 7)) {
107 source = new FileSource(uri + 7);
108 } else if (!strncasecmp("http://", uri, 7)) {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800109 sp<HTTPDataSource> httpSource = new HTTPDataSource(uri, headers);
110 if (httpSource->connect() != OK) {
111 return NULL;
112 }
Andreas Hubera51250b2010-04-08 07:51:20 -0700113 source = new CachingDataSource(httpSource, 64 * 1024, 10);
Andreas Huberaee3c632010-01-11 15:35:19 -0800114 } else {
115 // Assume it's a filename.
116 source = new FileSource(uri);
117 }
118
119 if (source == NULL || source->initCheck() != OK) {
120 return NULL;
121 }
122
123 return source;
124}
125
Andreas Hubere46b7be2009-07-14 16:56:47 -0700126} // namespace android