blob: 79dedcab169489088ecfe5a26adeb2d7ca335249 [file] [log] [blame]
Gloria Wangc2c22e72010-11-01 15:53:16 -07001/*
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
Jeff Tinker73104112011-07-01 07:13:42 -070036/* 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
Gloria Wangc2c22e72010-11-01 15:53:16 -070043namespace android {
44
Jeff Tinker73104112011-07-01 07:13:42 -070045static Mutex gWVMutex;
Gloria Wangc2c22e72010-11-01 15:53:16 -070046
47WVMExtractor::WVMExtractor(const sp<DataSource> &source)
Jeff Tinkerd0d19db2011-12-07 20:23:20 -080048 : mDataSource(source)
49{
50 Mutex::Autolock autoLock(gWVMutex);
Gloria Wangc2c22e72010-11-01 15:53:16 -070051
Jeff Tinkerd0d19db2011-12-07 20:23:20 -080052 if (!getVendorLibHandle()) {
53 return;
Gloria Wangc2c22e72010-11-01 15:53:16 -070054 }
55
Jeffrey Tinker82646412011-05-21 18:19:17 -070056 typedef WVMLoadableExtractor *(*GetInstanceFunc)(sp<DataSource>);
Gloria Wangc2c22e72010-11-01 15:53:16 -070057 GetInstanceFunc getInstanceFunc =
Jeff Tinker73104112011-07-01 07:13:42 -070058 (GetInstanceFunc) dlsym(gVendorLibHandle,
Gloria Wangc2c22e72010-11-01 15:53:16 -070059 "_ZN7android11GetInstanceENS_2spINS_10DataSourceEEE");
60
61 if (getInstanceFunc) {
Gloria Wangc2c22e72010-11-01 15:53:16 -070062 mImpl = (*getInstanceFunc)(source);
63 CHECK(mImpl != NULL);
64 } else {
65 LOGE("Failed to locate GetInstance in libwvm.so");
66 }
67}
68
Jeff Tinkerd0d19db2011-12-07 20:23:20 -080069bool WVMExtractor::getVendorLibHandle()
70{
71 if (gVendorLibHandle == NULL) {
72 gVendorLibHandle = dlopen("libwvm.so", RTLD_NOW);
73 }
74
75 if (gVendorLibHandle == NULL) {
76 LOGE("Failed to open libwvm.so");
77 }
78
79 return gVendorLibHandle != NULL;
80}
81
Gloria Wangc2c22e72010-11-01 15:53:16 -070082WVMExtractor::~WVMExtractor() {
83}
84
85size_t WVMExtractor::countTracks() {
86 return (mImpl != NULL) ? mImpl->countTracks() : 0;
87}
88
89sp<MediaSource> WVMExtractor::getTrack(size_t index) {
90 if (mImpl == NULL) {
91 return NULL;
92 }
93 return mImpl->getTrack(index);
94}
95
96sp<MetaData> WVMExtractor::getTrackMetaData(size_t index, uint32_t flags) {
97 if (mImpl == NULL) {
98 return NULL;
99 }
100 return mImpl->getTrackMetaData(index, flags);
101}
102
103sp<MetaData> WVMExtractor::getMetaData() {
104 if (mImpl == NULL) {
105 return NULL;
106 }
107 return mImpl->getMetaData();
108}
109
Andreas Huber77d8dc22011-04-21 10:06:43 -0700110int64_t WVMExtractor::getCachedDurationUs(status_t *finalStatus) {
Jeffrey Tinker82646412011-05-21 18:19:17 -0700111 if (mImpl == NULL) {
112 return 0;
113 }
Andreas Huber77d8dc22011-04-21 10:06:43 -0700114
Jeffrey Tinker82646412011-05-21 18:19:17 -0700115 return mImpl->getCachedDurationUs(finalStatus);
Andreas Huber77d8dc22011-04-21 10:06:43 -0700116}
117
118void WVMExtractor::setAdaptiveStreamingMode(bool adaptive) {
Jeffrey Tinker82646412011-05-21 18:19:17 -0700119 if (mImpl != NULL) {
120 mImpl->setAdaptiveStreamingMode(adaptive);
121 }
Andreas Huber77d8dc22011-04-21 10:06:43 -0700122}
123
Jeff Tinkerd0d19db2011-12-07 20:23:20 -0800124bool SniffWVM(
125 const sp<DataSource> &source, String8 *mimeType, float *confidence,
126 sp<AMessage> *) {
127
128 Mutex::Autolock autoLock(gWVMutex);
129
130 if (!WVMExtractor::getVendorLibHandle()) {
131 return false;
132 }
133
134 typedef WVMLoadableExtractor *(*SnifferFunc)(sp<DataSource>);
135 SnifferFunc snifferFunc =
136 (SnifferFunc) dlsym(gVendorLibHandle,
137 "_ZN7android15IsWidevineMediaENS_2spINS_10DataSourceEEE");
138
139 if (snifferFunc) {
140 if ((*snifferFunc)(source)) {
141 *mimeType = MEDIA_MIMETYPE_CONTAINER_WVM;
142 *confidence = 10.0f;
143 return true;
144 }
145 } else {
146 LOGE("IsWidevineMedia not found in libwvm.so");
147 }
148
149 return false;
150}
151
Gloria Wangc2c22e72010-11-01 15:53:16 -0700152} //namespace android
153