blob: d7dd4e52a08c44f92f7487aeb4df55e88721ffff [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"
Gloria Wangd5770912010-06-22 13:55:38 -070022#include "include/DRMExtractor.h"
Andreas Huber57515f32009-10-23 09:55:10 -070023
Andreas Huberaee3c632010-01-11 15:35:19 -080024#include <media/stagefright/CachingDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070025#include <media/stagefright/DataSource.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080026#include <media/stagefright/FileSource.h>
27#include <media/stagefright/HTTPDataSource.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070028#include <media/stagefright/MediaErrors.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070029#include <utils/String8.h>
30
31namespace android {
32
Andreas Huberbe06d262009-08-14 14:37:10 -070033bool DataSource::getUInt16(off_t offset, uint16_t *x) {
34 *x = 0;
35
36 uint8_t byte[2];
Andreas Huber9a12baf2009-10-23 10:22:30 -070037 if (readAt(offset, byte, 2) != 2) {
Andreas Huberbe06d262009-08-14 14:37:10 -070038 return false;
39 }
40
41 *x = (byte[0] << 8) | byte[1];
42
43 return true;
44}
45
Andreas Hubere46b7be2009-07-14 16:56:47 -070046status_t DataSource::getSize(off_t *size) {
47 *size = 0;
48
49 return ERROR_UNSUPPORTED;
50}
51
52////////////////////////////////////////////////////////////////////////////////
53
54Mutex DataSource::gSnifferMutex;
55List<DataSource::SnifferFunc> DataSource::gSniffers;
56
57bool DataSource::sniff(String8 *mimeType, float *confidence) {
58 *mimeType = "";
59 *confidence = 0.0f;
60
61 Mutex::Autolock autoLock(gSnifferMutex);
62 for (List<SnifferFunc>::iterator it = gSniffers.begin();
63 it != gSniffers.end(); ++it) {
64 String8 newMimeType;
65 float newConfidence;
66 if ((*it)(this, &newMimeType, &newConfidence)) {
67 if (newConfidence > *confidence) {
68 *mimeType = newMimeType;
69 *confidence = newConfidence;
70 }
71 }
72 }
73
74 return *confidence > 0.0;
75}
76
77// static
78void DataSource::RegisterSniffer(SnifferFunc func) {
79 Mutex::Autolock autoLock(gSnifferMutex);
80
81 for (List<SnifferFunc>::iterator it = gSniffers.begin();
82 it != gSniffers.end(); ++it) {
83 if (*it == func) {
84 return;
85 }
86 }
87
88 gSniffers.push_back(func);
89}
90
91// static
92void DataSource::RegisterDefaultSniffers() {
93 RegisterSniffer(SniffMP3);
94 RegisterSniffer(SniffMPEG4);
Andreas Huber5a65a6e2009-09-08 16:07:15 -070095 RegisterSniffer(SniffAMR);
Andreas Huber6bce6d82009-11-03 16:00:58 -080096 RegisterSniffer(SniffWAV);
Andreas Huber388379f2010-05-07 10:35:13 -070097 RegisterSniffer(SniffOgg);
Gloria Wangd5770912010-06-22 13:55:38 -070098 RegisterSniffer(SniffDRM);
Andreas Hubere46b7be2009-07-14 16:56:47 -070099}
100
Andreas Huberaee3c632010-01-11 15:35:19 -0800101// static
Andreas Huber433c9ac2010-01-27 16:49:05 -0800102sp<DataSource> DataSource::CreateFromURI(
103 const char *uri, const KeyedVector<String8, String8> *headers) {
Andreas Huberaee3c632010-01-11 15:35:19 -0800104 sp<DataSource> source;
105 if (!strncasecmp("file://", uri, 7)) {
106 source = new FileSource(uri + 7);
107 } else if (!strncasecmp("http://", uri, 7)) {
Andreas Huberedbb4d82010-03-12 08:59:22 -0800108 sp<HTTPDataSource> httpSource = new HTTPDataSource(uri, headers);
109 if (httpSource->connect() != OK) {
110 return NULL;
111 }
Andreas Hubera51250b2010-04-08 07:51:20 -0700112 source = new CachingDataSource(httpSource, 64 * 1024, 10);
Andreas Huberaee3c632010-01-11 15:35:19 -0800113 } else {
114 // Assume it's a filename.
115 source = new FileSource(uri);
116 }
117
118 if (source == NULL || source->initCheck() != OK) {
119 return NULL;
120 }
121
122 return source;
123}
124
Andreas Hubere46b7be2009-07-14 16:56:47 -0700125} // namespace android