blob: f7c691df15aa8905a462a749944880ed7ec6ae36 [file] [log] [blame]
John Grossman44a7e422012-06-21 17:29:24 -07001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Marco Nelissen08b9e2d2014-12-16 12:46:34 -080018//#define LOG_NDEBUG 0
John Grossman44a7e422012-06-21 17:29:24 -070019#define LOG_TAG "MediaPlayerFactory"
20#include <utils/Log.h>
21
22#include <cutils/properties.h>
23#include <media/IMediaPlayer.h>
Lajos Molnarca7b4bc2014-12-05 17:14:54 -080024#include <media/stagefright/DataSource.h>
25#include <media/stagefright/FileSource.h>
John Grossman44a7e422012-06-21 17:29:24 -070026#include <media/stagefright/foundation/ADebug.h>
27#include <utils/Errors.h>
28#include <utils/misc.h>
Lajos Molnarca7b4bc2014-12-05 17:14:54 -080029#include <../libstagefright/include/WVMExtractor.h>
John Grossman44a7e422012-06-21 17:29:24 -070030
31#include "MediaPlayerFactory.h"
32
John Grossman44a7e422012-06-21 17:29:24 -070033#include "TestPlayerStub.h"
John Grossman44a7e422012-06-21 17:29:24 -070034#include "nuplayer/NuPlayerDriver.h"
35
36namespace android {
37
John Grossman44a7e422012-06-21 17:29:24 -070038Mutex MediaPlayerFactory::sLock;
39MediaPlayerFactory::tFactoryMap MediaPlayerFactory::sFactoryMap;
40bool MediaPlayerFactory::sInitComplete = false;
41
42status_t MediaPlayerFactory::registerFactory_l(IFactory* factory,
43 player_type type) {
44 if (NULL == factory) {
45 ALOGE("Failed to register MediaPlayerFactory of type %d, factory is"
46 " NULL.", type);
47 return BAD_VALUE;
48 }
49
50 if (sFactoryMap.indexOfKey(type) >= 0) {
51 ALOGE("Failed to register MediaPlayerFactory of type %d, type is"
52 " already registered.", type);
53 return ALREADY_EXISTS;
54 }
55
56 if (sFactoryMap.add(type, factory) < 0) {
57 ALOGE("Failed to register MediaPlayerFactory of type %d, failed to add"
58 " to map.", type);
59 return UNKNOWN_ERROR;
60 }
61
62 return OK;
63}
64
Andy Hung48f36a02014-09-03 23:28:52 +000065static player_type getDefaultPlayerType() {
Lajos Molnarf5bdd772014-07-27 21:22:11 -070066 return NU_PLAYER;
John Grossman44a7e422012-06-21 17:29:24 -070067}
68
69status_t MediaPlayerFactory::registerFactory(IFactory* factory,
70 player_type type) {
71 Mutex::Autolock lock_(&sLock);
72 return registerFactory_l(factory, type);
73}
74
75void MediaPlayerFactory::unregisterFactory(player_type type) {
76 Mutex::Autolock lock_(&sLock);
77 sFactoryMap.removeItem(type);
78}
79
80#define GET_PLAYER_TYPE_IMPL(a...) \
81 Mutex::Autolock lock_(&sLock); \
82 \
83 player_type ret = STAGEFRIGHT_PLAYER; \
84 float bestScore = 0.0; \
85 \
86 for (size_t i = 0; i < sFactoryMap.size(); ++i) { \
87 \
88 IFactory* v = sFactoryMap.valueAt(i); \
89 float thisScore; \
90 CHECK(v != NULL); \
91 thisScore = v->scoreFactory(a, bestScore); \
92 if (thisScore > bestScore) { \
93 ret = sFactoryMap.keyAt(i); \
94 bestScore = thisScore; \
95 } \
96 } \
97 \
98 if (0.0 == bestScore) { \
Andreas Huber198a8932013-02-05 13:16:39 -080099 ret = getDefaultPlayerType(); \
John Grossman44a7e422012-06-21 17:29:24 -0700100 } \
101 \
102 return ret;
103
104player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
105 const char* url) {
106 GET_PLAYER_TYPE_IMPL(client, url);
107}
108
109player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
110 int fd,
111 int64_t offset,
112 int64_t length) {
113 GET_PLAYER_TYPE_IMPL(client, fd, offset, length);
114}
115
116player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
117 const sp<IStreamSource> &source) {
118 GET_PLAYER_TYPE_IMPL(client, source);
119}
120
Chris Watkins99f31602015-03-20 13:06:33 -0700121player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
122 const sp<DataSource> &source) {
123 GET_PLAYER_TYPE_IMPL(client, source);
124}
125
John Grossman44a7e422012-06-21 17:29:24 -0700126#undef GET_PLAYER_TYPE_IMPL
127
128sp<MediaPlayerBase> MediaPlayerFactory::createPlayer(
129 player_type playerType,
130 void* cookie,
Ronghua Wu68845c12015-07-21 09:50:48 -0700131 notify_callback_f notifyFunc,
132 pid_t pid) {
John Grossman44a7e422012-06-21 17:29:24 -0700133 sp<MediaPlayerBase> p;
134 IFactory* factory;
135 status_t init_result;
136 Mutex::Autolock lock_(&sLock);
137
138 if (sFactoryMap.indexOfKey(playerType) < 0) {
139 ALOGE("Failed to create player object of type %d, no registered"
140 " factory", playerType);
141 return p;
142 }
143
144 factory = sFactoryMap.valueFor(playerType);
145 CHECK(NULL != factory);
Ronghua Wu68845c12015-07-21 09:50:48 -0700146 p = factory->createPlayer(pid);
John Grossman44a7e422012-06-21 17:29:24 -0700147
148 if (p == NULL) {
149 ALOGE("Failed to create player object of type %d, create failed",
150 playerType);
151 return p;
152 }
153
154 init_result = p->initCheck();
155 if (init_result == NO_ERROR) {
156 p->setNotifyCallback(cookie, notifyFunc);
157 } else {
158 ALOGE("Failed to create player object of type %d, initCheck failed"
159 " (res = %d)", playerType, init_result);
160 p.clear();
161 }
162
163 return p;
164}
165
166/*****************************************************************************
167 * *
168 * Built-In Factory Implementations *
169 * *
170 *****************************************************************************/
171
John Grossman44a7e422012-06-21 17:29:24 -0700172class NuPlayerFactory : public MediaPlayerFactory::IFactory {
173 public:
Mark Salyzyn247d9eb2014-06-23 14:14:40 -0700174 virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
John Grossman44a7e422012-06-21 17:29:24 -0700175 const char* url,
176 float curScore) {
177 static const float kOurScore = 0.8;
178
179 if (kOurScore <= curScore)
180 return 0.0;
181
182 if (!strncasecmp("http://", url, 7)
Andreas Huber99759402013-04-01 14:28:31 -0700183 || !strncasecmp("https://", url, 8)
184 || !strncasecmp("file://", url, 7)) {
John Grossman44a7e422012-06-21 17:29:24 -0700185 size_t len = strlen(url);
186 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
187 return kOurScore;
188 }
189
190 if (strstr(url,"m3u8")) {
191 return kOurScore;
192 }
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100193
194 if ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) || strstr(url, ".sdp?")) {
195 return kOurScore;
196 }
John Grossman44a7e422012-06-21 17:29:24 -0700197 }
198
199 if (!strncasecmp("rtsp://", url, 7)) {
200 return kOurScore;
201 }
202
203 return 0.0;
204 }
205
Mark Salyzyn247d9eb2014-06-23 14:14:40 -0700206 virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
207 const sp<IStreamSource>& /*source*/,
208 float /*curScore*/) {
John Grossman44a7e422012-06-21 17:29:24 -0700209 return 1.0;
210 }
211
Chris Watkins99f31602015-03-20 13:06:33 -0700212 virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
213 const sp<DataSource>& /*source*/,
214 float /*curScore*/) {
215 // Only NuPlayer supports setting a DataSource source directly.
216 return 1.0;
217 }
218
Ronghua Wu68845c12015-07-21 09:50:48 -0700219 virtual sp<MediaPlayerBase> createPlayer(pid_t pid) {
John Grossman44a7e422012-06-21 17:29:24 -0700220 ALOGV(" create NuPlayer");
Ronghua Wu68845c12015-07-21 09:50:48 -0700221 return new NuPlayerDriver(pid);
John Grossman44a7e422012-06-21 17:29:24 -0700222 }
223};
224
John Grossman44a7e422012-06-21 17:29:24 -0700225class TestPlayerFactory : public MediaPlayerFactory::IFactory {
226 public:
Mark Salyzyn247d9eb2014-06-23 14:14:40 -0700227 virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
John Grossman44a7e422012-06-21 17:29:24 -0700228 const char* url,
Mark Salyzyn247d9eb2014-06-23 14:14:40 -0700229 float /*curScore*/) {
John Grossman44a7e422012-06-21 17:29:24 -0700230 if (TestPlayerStub::canBeUsed(url)) {
231 return 1.0;
232 }
233
234 return 0.0;
235 }
236
Ronghua Wu68845c12015-07-21 09:50:48 -0700237 virtual sp<MediaPlayerBase> createPlayer(pid_t /* pid */) {
John Grossman44a7e422012-06-21 17:29:24 -0700238 ALOGV("Create Test Player stub");
239 return new TestPlayerStub();
240 }
241};
242
John Grossman44a7e422012-06-21 17:29:24 -0700243void MediaPlayerFactory::registerBuiltinFactories() {
244 Mutex::Autolock lock_(&sLock);
245
246 if (sInitComplete)
247 return;
248
Yunlian Jiang449d3f02017-06-27 15:51:41 -0700249 IFactory* factory = new NuPlayerFactory();
250 if (registerFactory_l(factory, NU_PLAYER) != OK)
251 delete factory;
252 factory = new TestPlayerFactory();
253 if (registerFactory_l(factory, TEST_PLAYER) != OK)
254 delete factory;
John Grossman44a7e422012-06-21 17:29:24 -0700255
John Grossman44a7e422012-06-21 17:29:24 -0700256 sInitComplete = true;
257}
258
259} // namespace android