Home | History | Annotate | Download | only in libstagefright
      1 /*
      2  * Copyright (C) 2010 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 #define LOG_TAG "WVMExtractor"
     18 #include <utils/Log.h>
     19 
     20 #include "include/WVMExtractor.h"
     21 
     22 #include <arpa/inet.h>
     23 #include <utils/String8.h>
     24 #include <media/stagefright/foundation/ADebug.h>
     25 #include <media/stagefright/Utils.h>
     26 #include <media/stagefright/DataSource.h>
     27 #include <media/stagefright/MediaSource.h>
     28 #include <media/stagefright/MediaDefs.h>
     29 #include <media/stagefright/MetaData.h>
     30 #include <media/stagefright/MediaErrors.h>
     31 #include <media/stagefright/MediaBuffer.h>
     32 #include <dlfcn.h>
     33 
     34 #include <utils/Errors.h>
     35 
     36 /* The extractor lifetime is short - just long enough to get
     37  * the media sources constructed - so the shared lib needs to remain open
     38  * beyond the lifetime of the extractor.  So keep the handle as a global
     39  * rather than a member of the extractor
     40  */
     41 void *gVendorLibHandle = NULL;
     42 
     43 namespace android {
     44 
     45 static Mutex gWVMutex;
     46 
     47 WVMExtractor::WVMExtractor(const sp<DataSource> &source)
     48     : mDataSource(source)
     49 {
     50     Mutex::Autolock autoLock(gWVMutex);
     51 
     52     if (!getVendorLibHandle()) {
     53         return;
     54     }
     55 
     56     typedef WVMLoadableExtractor *(*GetInstanceFunc)(sp<DataSource>);
     57     GetInstanceFunc getInstanceFunc =
     58         (GetInstanceFunc) dlsym(gVendorLibHandle,
     59                 "_ZN7android11GetInstanceENS_2spINS_10DataSourceEEE");
     60 
     61     if (getInstanceFunc) {
     62         if (source->DrmInitialization(
     63                 MEDIA_MIMETYPE_CONTAINER_WVM) != NULL) {
     64             mImpl = (*getInstanceFunc)(source);
     65             CHECK(mImpl != NULL);
     66             setDrmFlag(true);
     67         } else {
     68             ALOGE("Drm manager failed to initialize.");
     69         }
     70     } else {
     71         ALOGE("Failed to locate GetInstance in libwvm.so");
     72     }
     73 }
     74 
     75 bool WVMExtractor::getVendorLibHandle()
     76 {
     77     if (gVendorLibHandle == NULL) {
     78         gVendorLibHandle = dlopen("libwvm.so", RTLD_NOW);
     79     }
     80 
     81     if (gVendorLibHandle == NULL) {
     82         ALOGE("Failed to open libwvm.so");
     83     }
     84 
     85     return gVendorLibHandle != NULL;
     86 }
     87 
     88 WVMExtractor::~WVMExtractor() {
     89 }
     90 
     91 size_t WVMExtractor::countTracks() {
     92     return (mImpl != NULL) ? mImpl->countTracks() : 0;
     93 }
     94 
     95 sp<MediaSource> WVMExtractor::getTrack(size_t index) {
     96     if (mImpl == NULL) {
     97         return NULL;
     98     }
     99     return mImpl->getTrack(index);
    100 }
    101 
    102 sp<MetaData> WVMExtractor::getTrackMetaData(size_t index, uint32_t flags) {
    103     if (mImpl == NULL) {
    104         return NULL;
    105     }
    106     return mImpl->getTrackMetaData(index, flags);
    107 }
    108 
    109 sp<MetaData> WVMExtractor::getMetaData() {
    110     if (mImpl == NULL) {
    111         return NULL;
    112     }
    113     return mImpl->getMetaData();
    114 }
    115 
    116 int64_t WVMExtractor::getCachedDurationUs(status_t *finalStatus) {
    117     if (mImpl == NULL) {
    118         return 0;
    119     }
    120 
    121     return mImpl->getCachedDurationUs(finalStatus);
    122 }
    123 
    124 void WVMExtractor::setAdaptiveStreamingMode(bool adaptive) {
    125     if (mImpl != NULL) {
    126         mImpl->setAdaptiveStreamingMode(adaptive);
    127     }
    128 }
    129 
    130 void WVMExtractor::setCryptoPluginMode(bool cryptoPluginMode) {
    131     if (mImpl != NULL) {
    132         mImpl->setCryptoPluginMode(cryptoPluginMode);
    133     }
    134 }
    135 
    136 void WVMExtractor::setUID(uid_t uid) {
    137     if (mImpl != NULL) {
    138         mImpl->setUID(uid);
    139     }
    140 }
    141 
    142 bool SniffWVM(
    143     const sp<DataSource> &source, String8 *mimeType, float *confidence,
    144         sp<AMessage> *) {
    145 
    146     Mutex::Autolock autoLock(gWVMutex);
    147 
    148     if (!WVMExtractor::getVendorLibHandle()) {
    149         return false;
    150     }
    151 
    152     typedef WVMLoadableExtractor *(*SnifferFunc)(const sp<DataSource>&);
    153     SnifferFunc snifferFunc =
    154         (SnifferFunc) dlsym(gVendorLibHandle,
    155                             "_ZN7android15IsWidevineMediaERKNS_2spINS_10DataSourceEEE");
    156 
    157     if (snifferFunc) {
    158         if ((*snifferFunc)(source)) {
    159             *mimeType = MEDIA_MIMETYPE_CONTAINER_WVM;
    160             *confidence = 10.0f;
    161             return true;
    162         }
    163     } else {
    164         ALOGE("IsWidevineMedia not found in libwvm.so");
    165     }
    166 
    167     return false;
    168 }
    169 
    170 } //namespace android
    171 
    172