Home | History | Annotate | Download | only in libstagefright
      1 /*
      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 
     17 #include "include/AMRExtractor.h"
     18 
     19 #if CHROMIUM_AVAILABLE
     20 #include "include/chromium_http_stub.h"
     21 #endif
     22 
     23 #include "include/AACExtractor.h"
     24 #include "include/DRMExtractor.h"
     25 #include "include/FLACExtractor.h"
     26 #include "include/HTTPBase.h"
     27 #include "include/MP3Extractor.h"
     28 #include "include/MPEG2PSExtractor.h"
     29 #include "include/MPEG2TSExtractor.h"
     30 #include "include/MPEG4Extractor.h"
     31 #include "include/NuCachedSource2.h"
     32 #include "include/OggExtractor.h"
     33 #include "include/WAVExtractor.h"
     34 #include "include/WVMExtractor.h"
     35 
     36 #include "matroska/MatroskaExtractor.h"
     37 
     38 #include <media/stagefright/foundation/AMessage.h>
     39 #include <media/stagefright/DataSource.h>
     40 #include <media/stagefright/FileSource.h>
     41 #include <media/stagefright/MediaErrors.h>
     42 #include <utils/String8.h>
     43 
     44 #include <cutils/properties.h>
     45 
     46 namespace android {
     47 
     48 bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
     49     *x = 0;
     50 
     51     uint8_t byte[2];
     52     if (readAt(offset, byte, 2) != 2) {
     53         return false;
     54     }
     55 
     56     *x = (byte[0] << 8) | byte[1];
     57 
     58     return true;
     59 }
     60 
     61 bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
     62     *x = 0;
     63 
     64     uint8_t byte[3];
     65     if (readAt(offset, byte, 3) != 3) {
     66         return false;
     67     }
     68 
     69     *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
     70 
     71     return true;
     72 }
     73 
     74 bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
     75     *x = 0;
     76 
     77     uint32_t tmp;
     78     if (readAt(offset, &tmp, 4) != 4) {
     79         return false;
     80     }
     81 
     82     *x = ntohl(tmp);
     83 
     84     return true;
     85 }
     86 
     87 bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
     88     *x = 0;
     89 
     90     uint64_t tmp;
     91     if (readAt(offset, &tmp, 8) != 8) {
     92         return false;
     93     }
     94 
     95     *x = ntoh64(tmp);
     96 
     97     return true;
     98 }
     99 
    100 status_t DataSource::getSize(off64_t *size) {
    101     *size = 0;
    102 
    103     return ERROR_UNSUPPORTED;
    104 }
    105 
    106 ////////////////////////////////////////////////////////////////////////////////
    107 
    108 Mutex DataSource::gSnifferMutex;
    109 List<DataSource::SnifferFunc> DataSource::gSniffers;
    110 bool DataSource::gSniffersRegistered = false;
    111 
    112 bool DataSource::sniff(
    113         String8 *mimeType, float *confidence, sp<AMessage> *meta) {
    114     *mimeType = "";
    115     *confidence = 0.0f;
    116     meta->clear();
    117 
    118     {
    119         Mutex::Autolock autoLock(gSnifferMutex);
    120         if (!gSniffersRegistered) {
    121             return false;
    122         }
    123     }
    124 
    125     for (List<SnifferFunc>::iterator it = gSniffers.begin();
    126          it != gSniffers.end(); ++it) {
    127         String8 newMimeType;
    128         float newConfidence;
    129         sp<AMessage> newMeta;
    130         if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
    131             if (newConfidence > *confidence) {
    132                 *mimeType = newMimeType;
    133                 *confidence = newConfidence;
    134                 *meta = newMeta;
    135             }
    136         }
    137     }
    138 
    139     return *confidence > 0.0;
    140 }
    141 
    142 // static
    143 void DataSource::RegisterSniffer_l(SnifferFunc func) {
    144     for (List<SnifferFunc>::iterator it = gSniffers.begin();
    145          it != gSniffers.end(); ++it) {
    146         if (*it == func) {
    147             return;
    148         }
    149     }
    150 
    151     gSniffers.push_back(func);
    152 }
    153 
    154 // static
    155 void DataSource::RegisterDefaultSniffers() {
    156     Mutex::Autolock autoLock(gSnifferMutex);
    157     if (gSniffersRegistered) {
    158         return;
    159     }
    160 
    161     RegisterSniffer_l(SniffMPEG4);
    162     RegisterSniffer_l(SniffMatroska);
    163     RegisterSniffer_l(SniffOgg);
    164     RegisterSniffer_l(SniffWAV);
    165     RegisterSniffer_l(SniffFLAC);
    166     RegisterSniffer_l(SniffAMR);
    167     RegisterSniffer_l(SniffMPEG2TS);
    168     RegisterSniffer_l(SniffMP3);
    169     RegisterSniffer_l(SniffAAC);
    170     RegisterSniffer_l(SniffMPEG2PS);
    171     RegisterSniffer_l(SniffWVM);
    172 
    173     char value[PROPERTY_VALUE_MAX];
    174     if (property_get("drm.service.enabled", value, NULL)
    175             && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
    176         RegisterSniffer_l(SniffDRM);
    177     }
    178     gSniffersRegistered = true;
    179 }
    180 
    181 // static
    182 sp<DataSource> DataSource::CreateFromURI(
    183         const char *uri, const KeyedVector<String8, String8> *headers) {
    184     bool isWidevine = !strncasecmp("widevine://", uri, 11);
    185 
    186     sp<DataSource> source;
    187     if (!strncasecmp("file://", uri, 7)) {
    188         source = new FileSource(uri + 7);
    189     } else if (!strncasecmp("http://", uri, 7)
    190             || !strncasecmp("https://", uri, 8)
    191             || isWidevine) {
    192         sp<HTTPBase> httpSource = HTTPBase::Create();
    193 
    194         String8 tmp;
    195         if (isWidevine) {
    196             tmp = String8("http://");
    197             tmp.append(uri + 11);
    198 
    199             uri = tmp.string();
    200         }
    201 
    202         if (httpSource->connect(uri, headers) != OK) {
    203             return NULL;
    204         }
    205 
    206         if (!isWidevine) {
    207             String8 cacheConfig;
    208             bool disconnectAtHighwatermark;
    209             if (headers != NULL) {
    210                 KeyedVector<String8, String8> copy = *headers;
    211                 NuCachedSource2::RemoveCacheSpecificHeaders(
    212                         &copy, &cacheConfig, &disconnectAtHighwatermark);
    213             }
    214 
    215             source = new NuCachedSource2(
    216                     httpSource,
    217                     cacheConfig.isEmpty() ? NULL : cacheConfig.string());
    218         } else {
    219             // We do not want that prefetching, caching, datasource wrapper
    220             // in the widevine:// case.
    221             source = httpSource;
    222         }
    223 
    224 # if CHROMIUM_AVAILABLE
    225     } else if (!strncasecmp("data:", uri, 5)) {
    226         source = createDataUriSource(uri);
    227 #endif
    228     } else {
    229         // Assume it's a filename.
    230         source = new FileSource(uri);
    231     }
    232 
    233     if (source == NULL || source->initCheck() != OK) {
    234         return NULL;
    235     }
    236 
    237     return source;
    238 }
    239 
    240 String8 DataSource::getMIMEType() const {
    241     return String8("application/octet-stream");
    242 }
    243 
    244 }  // namespace android
    245