DataSource.cpp revision 2944eca607304a095ea43ba2b8f0b9de61249f9f
120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber/*
220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Copyright (C) 2009 The Android Open Source Project
320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * you may not use this file except in compliance with the License.
620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * You may obtain a copy of the License at
720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber *
1020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Unless required by applicable law or agreed to in writing, software
1120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * See the License for the specific language governing permissions and
1420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * limitations under the License.
1520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber */
1620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
1766326a5ee0869f1ee4d136a477e6effba428b3cbAndreas Huber#include "include/AMRExtractor.h"
18f8374dec590223ebdd6959b26d9ba90749dd8328Andreas Huber#include "include/AVIExtractor.h"
1966326a5ee0869f1ee4d136a477e6effba428b3cbAndreas Huber#include "include/MP3Extractor.h"
2066326a5ee0869f1ee4d136a477e6effba428b3cbAndreas Huber#include "include/MPEG4Extractor.h"
210dba73763a04d39faf999dcc5ef12af3c99535a7Andreas Huber#include "include/WAVExtractor.h"
22ee7ff20e69498ebd53dd9717a0f984188341a75eAndreas Huber#include "include/OggExtractor.h"
232944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber#include "include/MPEG2PSExtractor.h"
24cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber#include "include/MPEG2TSExtractor.h"
250a5baa9b411fe086013d2a5e9126ed63fbad046cAndreas Huber#include "include/NuCachedSource2.h"
261156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber#include "include/HTTPBase.h"
27dcd25efb46c41c8d24a0a9cf61fb57f84149709eGloria Wang#include "include/DRMExtractor.h"
28856990b491d84b7ed4fefe337485c8997ba9dd02Glenn Kasten#include "include/FLACExtractor.h"
2950c44c79d2d7dd6cd1485d9d939f67f80b8da1caGloria Wang#include "include/AACExtractor.h"
3066326a5ee0869f1ee4d136a477e6effba428b3cbAndreas Huber
31093437c388e5dff6903a3d43f2ca9f8a1ba4744aAndreas Huber#include "matroska/MatroskaExtractor.h"
32093437c388e5dff6903a3d43f2ca9f8a1ba4744aAndreas Huber
335a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber#include <media/stagefright/foundation/AMessage.h>
3420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/DataSource.h>
35fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber#include <media/stagefright/FileSource.h>
3620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MediaErrors.h>
3720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/String8.h>
3820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
39a89082944308805b0def7de87c67e370e74b8789Gloria Wang#include <cutils/properties.h>
40a89082944308805b0def7de87c67e370e74b8789Gloria Wang
4120111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android {
4220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
43c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongbool DataSource::getUInt16(off64_t offset, uint16_t *x) {
44693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber    *x = 0;
45693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber
46693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber    uint8_t byte[2];
4734769bc913e9f6bb138e666d94a9d685bf3da217Andreas Huber    if (readAt(offset, byte, 2) != 2) {
48693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber        return false;
49693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber    }
50693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber
51693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber    *x = (byte[0] << 8) | byte[1];
52693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber
53693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber    return true;
54693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber}
55693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber
56c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongstatus_t DataSource::getSize(off64_t *size) {
5720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *size = 0;
5820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
5920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return ERROR_UNSUPPORTED;
6020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
6120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber////////////////////////////////////////////////////////////////////////////////
6320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
6420111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMutex DataSource::gSnifferMutex;
6520111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberList<DataSource::SnifferFunc> DataSource::gSniffers;
6620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
675a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huberbool DataSource::sniff(
685a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber        String8 *mimeType, float *confidence, sp<AMessage> *meta) {
6920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *mimeType = "";
7020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    *confidence = 0.0f;
715a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber    meta->clear();
7220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
7320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    Mutex::Autolock autoLock(gSnifferMutex);
7420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    for (List<SnifferFunc>::iterator it = gSniffers.begin();
7520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber         it != gSniffers.end(); ++it) {
7620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        String8 newMimeType;
7720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        float newConfidence;
785a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber        sp<AMessage> newMeta;
795a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber        if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
8020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber            if (newConfidence > *confidence) {
8120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber                *mimeType = newMimeType;
8220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber                *confidence = newConfidence;
835a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber                *meta = newMeta;
8420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber            }
8520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        }
8620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
8720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
8820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    return *confidence > 0.0;
8920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
9020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
9120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber// static
9220111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid DataSource::RegisterSniffer(SnifferFunc func) {
9320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    Mutex::Autolock autoLock(gSnifferMutex);
9420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
9520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    for (List<SnifferFunc>::iterator it = gSniffers.begin();
9620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber         it != gSniffers.end(); ++it) {
9720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        if (*it == func) {
9820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber            return;
9920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        }
10020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    }
10120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
10220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    gSniffers.push_back(func);
10320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
10420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
10520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber// static
10620111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid DataSource::RegisterDefaultSniffers() {
10720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber    RegisterSniffer(SniffMPEG4);
108093437c388e5dff6903a3d43f2ca9f8a1ba4744aAndreas Huber    RegisterSniffer(SniffMatroska);
1095a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber    RegisterSniffer(SniffOgg);
1105a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber    RegisterSniffer(SniffWAV);
111856990b491d84b7ed4fefe337485c8997ba9dd02Glenn Kasten    RegisterSniffer(SniffFLAC);
1125a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber    RegisterSniffer(SniffAMR);
113cda17c606b0fe3ccda4dc68a6d43882410ea2462Andreas Huber    RegisterSniffer(SniffMPEG2TS);
1145a1c3529e4fa2f8a11054181294e0ce79fff8dd3Andreas Huber    RegisterSniffer(SniffMP3);
11550c44c79d2d7dd6cd1485d9d939f67f80b8da1caGloria Wang    RegisterSniffer(SniffAAC);
116f8374dec590223ebdd6959b26d9ba90749dd8328Andreas Huber    RegisterSniffer(SniffAVI);
1172944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber    RegisterSniffer(SniffMPEG2PS);
118a89082944308805b0def7de87c67e370e74b8789Gloria Wang
119a89082944308805b0def7de87c67e370e74b8789Gloria Wang    char value[PROPERTY_VALUE_MAX];
120a89082944308805b0def7de87c67e370e74b8789Gloria Wang    if (property_get("drm.service.enabled", value, NULL)
121a89082944308805b0def7de87c67e370e74b8789Gloria Wang            && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
122a89082944308805b0def7de87c67e370e74b8789Gloria Wang        RegisterSniffer(SniffDRM);
123a89082944308805b0def7de87c67e370e74b8789Gloria Wang    }
12420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}
12520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
126fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber// static
1275561ccf4a8db88a2e44eac1b3ed13b4ff53a7f20Andreas Hubersp<DataSource> DataSource::CreateFromURI(
1285561ccf4a8db88a2e44eac1b3ed13b4ff53a7f20Andreas Huber        const char *uri, const KeyedVector<String8, String8> *headers) {
129fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    sp<DataSource> source;
130fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    if (!strncasecmp("file://", uri, 7)) {
131fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber        source = new FileSource(uri + 7);
1328cb0c4168bf4b678e4a6edfcf409247016be20d5Andreas Huber    } else if (!strncasecmp("http://", uri, 7)
1338cb0c4168bf4b678e4a6edfcf409247016be20d5Andreas Huber            || !strncasecmp("https://", uri, 8)) {
1341156dc913a5ba7b2bc86489468d4914430f03d14Andreas Huber        sp<HTTPBase> httpSource = HTTPBase::Create();
13579f77ef3b0a37660ba8c5bcb2dfbfda3860f2135Andreas Huber        if (httpSource->connect(uri, headers) != OK) {
136e94bd14078d327ef2f800e69907efce641a13272Andreas Huber            return NULL;
137e94bd14078d327ef2f800e69907efce641a13272Andreas Huber        }
1380a5baa9b411fe086013d2a5e9126ed63fbad046cAndreas Huber        source = new NuCachedSource2(httpSource);
139fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    } else {
140fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber        // Assume it's a filename.
141fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber        source = new FileSource(uri);
142fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    }
143fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber
144fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    if (source == NULL || source->initCheck() != OK) {
145fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber        return NULL;
146fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    }
147fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber
148fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber    return source;
149fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber}
150fc9ba09e3bb368f823d473f5e2bb9aa32dba6289Andreas Huber
1516511c9755c3a3360ba869772600c7aae048a7ffcAndreas HuberString8 DataSource::getMIMEType() const {
1526511c9755c3a3360ba869772600c7aae048a7ffcAndreas Huber    return String8("application/octet-stream");
1536511c9755c3a3360ba869772600c7aae048a7ffcAndreas Huber}
1546511c9755c3a3360ba869772600c7aae048a7ffcAndreas Huber
15520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}  // namespace android
156