blob: 284e3bc47020a675ddbf61f31f3fd3afcaa0b83a [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 Huber57515f32009-10-23 09:55:10 -070021
Andreas Huberaee3c632010-01-11 15:35:19 -080022#include <media/stagefright/CachingDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070023#include <media/stagefright/DataSource.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080024#include <media/stagefright/FileSource.h>
25#include <media/stagefright/HTTPDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070026#include <media/stagefright/MediaErrors.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070027#include <utils/String8.h>
28
29namespace android {
30
Andreas Huberbe06d262009-08-14 14:37:10 -070031bool DataSource::getUInt16(off_t offset, uint16_t *x) {
32 *x = 0;
33
34 uint8_t byte[2];
Andreas Huber9a12baf2009-10-23 10:22:30 -070035 if (readAt(offset, byte, 2) != 2) {
Andreas Huberbe06d262009-08-14 14:37:10 -070036 return false;
37 }
38
39 *x = (byte[0] << 8) | byte[1];
40
41 return true;
42}
43
Andreas Hubere46b7be2009-07-14 16:56:47 -070044status_t DataSource::getSize(off_t *size) {
45 *size = 0;
46
47 return ERROR_UNSUPPORTED;
48}
49
50////////////////////////////////////////////////////////////////////////////////
51
52Mutex DataSource::gSnifferMutex;
53List<DataSource::SnifferFunc> DataSource::gSniffers;
54
55bool DataSource::sniff(String8 *mimeType, float *confidence) {
56 *mimeType = "";
57 *confidence = 0.0f;
58
59 Mutex::Autolock autoLock(gSnifferMutex);
60 for (List<SnifferFunc>::iterator it = gSniffers.begin();
61 it != gSniffers.end(); ++it) {
62 String8 newMimeType;
63 float newConfidence;
64 if ((*it)(this, &newMimeType, &newConfidence)) {
65 if (newConfidence > *confidence) {
66 *mimeType = newMimeType;
67 *confidence = newConfidence;
68 }
69 }
70 }
71
72 return *confidence > 0.0;
73}
74
75// static
76void DataSource::RegisterSniffer(SnifferFunc func) {
77 Mutex::Autolock autoLock(gSnifferMutex);
78
79 for (List<SnifferFunc>::iterator it = gSniffers.begin();
80 it != gSniffers.end(); ++it) {
81 if (*it == func) {
82 return;
83 }
84 }
85
86 gSniffers.push_back(func);
87}
88
89// static
90void DataSource::RegisterDefaultSniffers() {
91 RegisterSniffer(SniffMP3);
92 RegisterSniffer(SniffMPEG4);
Andreas Huber5a65a6e2009-09-08 16:07:15 -070093 RegisterSniffer(SniffAMR);
Andreas Huber6bce6d82009-11-03 16:00:58 -080094 RegisterSniffer(SniffWAV);
Andreas Hubere46b7be2009-07-14 16:56:47 -070095}
96
Andreas Huberaee3c632010-01-11 15:35:19 -080097// static
Andreas Huber433c9ac2010-01-27 16:49:05 -080098sp<DataSource> DataSource::CreateFromURI(
99 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huberaee3c632010-01-11 15:35:19 -0800100 sp<DataSource> source;
101 if (!strncasecmp("file://", uri, 7)) {
102 source = new FileSource(uri + 7);
103 } else if (!strncasecmp("http://", uri, 7)) {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800104 sp<HTTPDataSource> httpSource = new HTTPDataSource(uri, headers);
105 if (httpSource->connect() != OK) {
106 return NULL;
107 }
108 source = new CachingDataSource(httpSource, 32 * 1024, 20);
Andreas Huberaee3c632010-01-11 15:35:19 -0800109 } else {
110 // Assume it's a filename.
111 source = new FileSource(uri);
112 }
113
114 if (source == NULL || source->initCheck() != OK) {
115 return NULL;
116 }
117
118 return source;
119}
120
Andreas Hubere46b7be2009-07-14 16:56:47 -0700121} // namespace android