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 //#define LOG_NDEBUG 0
     17 #define LOG_TAG "DataSource"
     18 
     19 #include "include/AMRExtractor.h"
     20 
     21 #include "include/AACExtractor.h"
     22 #include "include/CallbackDataSource.h"
     23 #include "include/DRMExtractor.h"
     24 #include "include/FLACExtractor.h"
     25 #include "include/HTTPBase.h"
     26 #include "include/MidiExtractor.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/IMediaHTTPConnection.h>
     39 #include <media/IMediaHTTPService.h>
     40 #include <media/stagefright/foundation/ADebug.h>
     41 #include <media/stagefright/foundation/AMessage.h>
     42 #include <media/stagefright/DataSource.h>
     43 #include <media/stagefright/DataURISource.h>
     44 #include <media/stagefright/FileSource.h>
     45 #include <media/stagefright/MediaErrors.h>
     46 #include <media/stagefright/MediaHTTP.h>
     47 #include <utils/String8.h>
     48 
     49 #include <cutils/properties.h>
     50 
     51 #include <private/android_filesystem_config.h>
     52 
     53 namespace android {
     54 
     55 bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
     56     *x = 0;
     57 
     58     uint8_t byte[2];
     59     if (readAt(offset, byte, 2) != 2) {
     60         return false;
     61     }
     62 
     63     *x = (byte[0] << 8) | byte[1];
     64 
     65     return true;
     66 }
     67 
     68 bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
     69     *x = 0;
     70 
     71     uint8_t byte[3];
     72     if (readAt(offset, byte, 3) != 3) {
     73         return false;
     74     }
     75 
     76     *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
     77 
     78     return true;
     79 }
     80 
     81 bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
     82     *x = 0;
     83 
     84     uint32_t tmp;
     85     if (readAt(offset, &tmp, 4) != 4) {
     86         return false;
     87     }
     88 
     89     *x = ntohl(tmp);
     90 
     91     return true;
     92 }
     93 
     94 bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
     95     *x = 0;
     96 
     97     uint64_t tmp;
     98     if (readAt(offset, &tmp, 8) != 8) {
     99         return false;
    100     }
    101 
    102     *x = ntoh64(tmp);
    103 
    104     return true;
    105 }
    106 
    107 status_t DataSource::getSize(off64_t *size) {
    108     *size = 0;
    109 
    110     return ERROR_UNSUPPORTED;
    111 }
    112 
    113 ////////////////////////////////////////////////////////////////////////////////
    114 
    115 Mutex DataSource::gSnifferMutex;
    116 List<DataSource::SnifferFunc> DataSource::gSniffers;
    117 bool DataSource::gSniffersRegistered = false;
    118 
    119 bool DataSource::sniff(
    120         String8 *mimeType, float *confidence, sp<AMessage> *meta) {
    121     *mimeType = "";
    122     *confidence = 0.0f;
    123     meta->clear();
    124 
    125     {
    126         Mutex::Autolock autoLock(gSnifferMutex);
    127         if (!gSniffersRegistered) {
    128             return false;
    129         }
    130     }
    131 
    132     for (List<SnifferFunc>::iterator it = gSniffers.begin();
    133          it != gSniffers.end(); ++it) {
    134         String8 newMimeType;
    135         float newConfidence;
    136         sp<AMessage> newMeta;
    137         if ((*it)(this, &newMimeType, &newConfidence, &newMeta)) {
    138             if (newConfidence > *confidence) {
    139                 *mimeType = newMimeType;
    140                 *confidence = newConfidence;
    141                 *meta = newMeta;
    142             }
    143         }
    144     }
    145 
    146     return *confidence > 0.0;
    147 }
    148 
    149 // static
    150 void DataSource::RegisterSniffer_l(SnifferFunc func) {
    151     for (List<SnifferFunc>::iterator it = gSniffers.begin();
    152          it != gSniffers.end(); ++it) {
    153         if (*it == func) {
    154             return;
    155         }
    156     }
    157 
    158     gSniffers.push_back(func);
    159 }
    160 
    161 // static
    162 void DataSource::RegisterDefaultSniffers() {
    163     Mutex::Autolock autoLock(gSnifferMutex);
    164     if (gSniffersRegistered) {
    165         return;
    166     }
    167 
    168     RegisterSniffer_l(SniffMPEG4);
    169     RegisterSniffer_l(SniffMatroska);
    170     RegisterSniffer_l(SniffOgg);
    171     RegisterSniffer_l(SniffWAV);
    172     RegisterSniffer_l(SniffFLAC);
    173     RegisterSniffer_l(SniffAMR);
    174     RegisterSniffer_l(SniffMPEG2TS);
    175     RegisterSniffer_l(SniffMP3);
    176     RegisterSniffer_l(SniffAAC);
    177     RegisterSniffer_l(SniffMPEG2PS);
    178     if (getuid() == AID_MEDIA) {
    179         // WVM only in the media server process
    180         RegisterSniffer_l(SniffWVM);
    181     }
    182     RegisterSniffer_l(SniffMidi);
    183 
    184     char value[PROPERTY_VALUE_MAX];
    185     if (property_get("drm.service.enabled", value, NULL)
    186             && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
    187         RegisterSniffer_l(SniffDRM);
    188     }
    189     gSniffersRegistered = true;
    190 }
    191 
    192 // static
    193 sp<DataSource> DataSource::CreateFromURI(
    194         const sp<IMediaHTTPService> &httpService,
    195         const char *uri,
    196         const KeyedVector<String8, String8> *headers,
    197         String8 *contentType,
    198         HTTPBase *httpSource) {
    199     if (contentType != NULL) {
    200         *contentType = "";
    201     }
    202 
    203     bool isWidevine = !strncasecmp("widevine://", uri, 11);
    204 
    205     sp<DataSource> source;
    206     if (!strncasecmp("file://", uri, 7)) {
    207         source = new FileSource(uri + 7);
    208     } else if (!strncasecmp("http://", uri, 7)
    209             || !strncasecmp("https://", uri, 8)
    210             || isWidevine) {
    211         if (httpService == NULL) {
    212             ALOGE("Invalid http service!");
    213             return NULL;
    214         }
    215 
    216         if (httpSource == NULL) {
    217             sp<IMediaHTTPConnection> conn = httpService->makeHTTPConnection();
    218             if (conn == NULL) {
    219                 ALOGE("Failed to make http connection from http service!");
    220                 return NULL;
    221             }
    222             httpSource = new MediaHTTP(conn);
    223         }
    224 
    225         String8 tmp;
    226         if (isWidevine) {
    227             tmp = String8("http://");
    228             tmp.append(uri + 11);
    229 
    230             uri = tmp.string();
    231         }
    232 
    233         String8 cacheConfig;
    234         bool disconnectAtHighwatermark;
    235         KeyedVector<String8, String8> nonCacheSpecificHeaders;
    236         if (headers != NULL) {
    237             nonCacheSpecificHeaders = *headers;
    238             NuCachedSource2::RemoveCacheSpecificHeaders(
    239                     &nonCacheSpecificHeaders,
    240                     &cacheConfig,
    241                     &disconnectAtHighwatermark);
    242         }
    243 
    244         if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) {
    245             ALOGE("Failed to connect http source!");
    246             return NULL;
    247         }
    248 
    249         if (!isWidevine) {
    250             if (contentType != NULL) {
    251                 *contentType = httpSource->getMIMEType();
    252             }
    253 
    254             source = NuCachedSource2::Create(
    255                     httpSource,
    256                     cacheConfig.isEmpty() ? NULL : cacheConfig.string(),
    257                     disconnectAtHighwatermark);
    258         } else {
    259             // We do not want that prefetching, caching, datasource wrapper
    260             // in the widevine:// case.
    261             source = httpSource;
    262         }
    263     } else if (!strncasecmp("data:", uri, 5)) {
    264         source = DataURISource::Create(uri);
    265     } else {
    266         // Assume it's a filename.
    267         source = new FileSource(uri);
    268     }
    269 
    270     if (source == NULL || source->initCheck() != OK) {
    271         return NULL;
    272     }
    273 
    274     return source;
    275 }
    276 
    277 sp<DataSource> DataSource::CreateMediaHTTP(const sp<IMediaHTTPService> &httpService) {
    278     if (httpService == NULL) {
    279         return NULL;
    280     }
    281 
    282     sp<IMediaHTTPConnection> conn = httpService->makeHTTPConnection();
    283     if (conn == NULL) {
    284         return NULL;
    285     } else {
    286         return new MediaHTTP(conn);
    287     }
    288 }
    289 
    290 sp<DataSource> DataSource::CreateFromIDataSource(const sp<IDataSource> &source) {
    291     return new TinyCacheSource(new CallbackDataSource(source));
    292 }
    293 
    294 String8 DataSource::getMIMEType() const {
    295     return String8("application/octet-stream");
    296 }
    297 
    298 }  // namespace android
    299