WVMExtractor.cpp revision 9d2f386dd2885eaffa11fd494ae258bb09fe6397
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/Utils.h>
25#include <media/stagefright/DataSource.h>
26#include <media/stagefright/MediaSource.h>
27#include <media/stagefright/MediaDefs.h>
28#include <media/stagefright/MetaData.h>
29#include <media/stagefright/MediaErrors.h>
30#include <media/stagefright/MediaBuffer.h>
31#include <media/stagefright/MediaDebug.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 */
41void *gVendorLibHandle = NULL;
42
43namespace android {
44
45static Mutex gWVMutex;
46
47WVMExtractor::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        CHECK(source->DrmInitialization(MEDIA_MIMETYPE_CONTAINER_WVM) != NULL);
63        mImpl = (*getInstanceFunc)(source);
64        CHECK(mImpl != NULL);
65        setDrmFlag(true);
66    } else {
67        ALOGE("Failed to locate GetInstance in libwvm.so");
68    }
69}
70
71bool WVMExtractor::getVendorLibHandle()
72{
73    if (gVendorLibHandle == NULL) {
74        gVendorLibHandle = dlopen("libwvm.so", RTLD_NOW);
75    }
76
77    if (gVendorLibHandle == NULL) {
78        ALOGE("Failed to open libwvm.so");
79    }
80
81    return gVendorLibHandle != NULL;
82}
83
84WVMExtractor::~WVMExtractor() {
85}
86
87size_t WVMExtractor::countTracks() {
88    return (mImpl != NULL) ? mImpl->countTracks() : 0;
89}
90
91sp<MediaSource> WVMExtractor::getTrack(size_t index) {
92    if (mImpl == NULL) {
93        return NULL;
94    }
95    return mImpl->getTrack(index);
96}
97
98sp<MetaData> WVMExtractor::getTrackMetaData(size_t index, uint32_t flags) {
99    if (mImpl == NULL) {
100        return NULL;
101    }
102    return mImpl->getTrackMetaData(index, flags);
103}
104
105sp<MetaData> WVMExtractor::getMetaData() {
106    if (mImpl == NULL) {
107        return NULL;
108    }
109    return mImpl->getMetaData();
110}
111
112int64_t WVMExtractor::getCachedDurationUs(status_t *finalStatus) {
113    if (mImpl == NULL) {
114        return 0;
115    }
116
117    return mImpl->getCachedDurationUs(finalStatus);
118}
119
120void WVMExtractor::setAdaptiveStreamingMode(bool adaptive) {
121    if (mImpl != NULL) {
122        mImpl->setAdaptiveStreamingMode(adaptive);
123    }
124}
125
126bool SniffWVM(
127    const sp<DataSource> &source, String8 *mimeType, float *confidence,
128        sp<AMessage> *) {
129
130    Mutex::Autolock autoLock(gWVMutex);
131
132    if (!WVMExtractor::getVendorLibHandle()) {
133        return false;
134    }
135
136    typedef WVMLoadableExtractor *(*SnifferFunc)(const sp<DataSource>&);
137    SnifferFunc snifferFunc =
138        (SnifferFunc) dlsym(gVendorLibHandle,
139                            "_ZN7android15IsWidevineMediaERKNS_2spINS_10DataSourceEEE");
140
141    if (snifferFunc) {
142        if ((*snifferFunc)(source)) {
143            *mimeType = MEDIA_MIMETYPE_CONTAINER_WVM;
144            *confidence = 10.0f;
145            return true;
146        }
147    } else {
148        ALOGE("IsWidevineMedia not found in libwvm.so");
149    }
150
151    return false;
152}
153
154} //namespace android
155
156