blob: 1fb8b1a2467fce5e961cfe73de1c492825cd0514 [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
18#define LOG_TAG "MediaPlayerFactory"
19#include <utils/Log.h>
20
21#include <cutils/properties.h>
22#include <media/IMediaPlayer.h>
23#include <media/stagefright/foundation/ADebug.h>
24#include <utils/Errors.h>
25#include <utils/misc.h>
26
27#include "MediaPlayerFactory.h"
28
29#include "MidiFile.h"
30#include "TestPlayerStub.h"
31#include "StagefrightPlayer.h"
32#include "nuplayer/NuPlayerDriver.h"
33
34namespace android {
35
John Grossman44a7e422012-06-21 17:29:24 -070036Mutex MediaPlayerFactory::sLock;
37MediaPlayerFactory::tFactoryMap MediaPlayerFactory::sFactoryMap;
38bool MediaPlayerFactory::sInitComplete = false;
39
40status_t MediaPlayerFactory::registerFactory_l(IFactory* factory,
41 player_type type) {
42 if (NULL == factory) {
43 ALOGE("Failed to register MediaPlayerFactory of type %d, factory is"
44 " NULL.", type);
45 return BAD_VALUE;
46 }
47
48 if (sFactoryMap.indexOfKey(type) >= 0) {
49 ALOGE("Failed to register MediaPlayerFactory of type %d, type is"
50 " already registered.", type);
51 return ALREADY_EXISTS;
52 }
53
54 if (sFactoryMap.add(type, factory) < 0) {
55 ALOGE("Failed to register MediaPlayerFactory of type %d, failed to add"
56 " to map.", type);
57 return UNKNOWN_ERROR;
58 }
59
60 return OK;
61}
62
63player_type MediaPlayerFactory::getDefaultPlayerType() {
64 char value[PROPERTY_VALUE_MAX];
65 if (property_get("media.stagefright.use-nuplayer", value, NULL)
66 && (!strcmp("1", value) || !strcasecmp("true", value))) {
67 return NU_PLAYER;
68 }
69
70 return STAGEFRIGHT_PLAYER;
71}
72
73status_t MediaPlayerFactory::registerFactory(IFactory* factory,
74 player_type type) {
75 Mutex::Autolock lock_(&sLock);
76 return registerFactory_l(factory, type);
77}
78
79void MediaPlayerFactory::unregisterFactory(player_type type) {
80 Mutex::Autolock lock_(&sLock);
81 sFactoryMap.removeItem(type);
82}
83
84#define GET_PLAYER_TYPE_IMPL(a...) \
85 Mutex::Autolock lock_(&sLock); \
86 \
87 player_type ret = STAGEFRIGHT_PLAYER; \
88 float bestScore = 0.0; \
89 \
90 for (size_t i = 0; i < sFactoryMap.size(); ++i) { \
91 \
92 IFactory* v = sFactoryMap.valueAt(i); \
93 float thisScore; \
94 CHECK(v != NULL); \
95 thisScore = v->scoreFactory(a, bestScore); \
96 if (thisScore > bestScore) { \
97 ret = sFactoryMap.keyAt(i); \
98 bestScore = thisScore; \
99 } \
100 } \
101 \
102 if (0.0 == bestScore) { \
Andreas Huber198a8932013-02-05 13:16:39 -0800103 ret = getDefaultPlayerType(); \
John Grossman44a7e422012-06-21 17:29:24 -0700104 } \
105 \
106 return ret;
107
108player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
109 const char* url) {
110 GET_PLAYER_TYPE_IMPL(client, url);
111}
112
113player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
114 int fd,
115 int64_t offset,
116 int64_t length) {
117 GET_PLAYER_TYPE_IMPL(client, fd, offset, length);
118}
119
120player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
121 const sp<IStreamSource> &source) {
122 GET_PLAYER_TYPE_IMPL(client, source);
123}
124
125#undef GET_PLAYER_TYPE_IMPL
126
127sp<MediaPlayerBase> MediaPlayerFactory::createPlayer(
128 player_type playerType,
129 void* cookie,
130 notify_callback_f notifyFunc) {
131 sp<MediaPlayerBase> p;
132 IFactory* factory;
133 status_t init_result;
134 Mutex::Autolock lock_(&sLock);
135
136 if (sFactoryMap.indexOfKey(playerType) < 0) {
137 ALOGE("Failed to create player object of type %d, no registered"
138 " factory", playerType);
139 return p;
140 }
141
142 factory = sFactoryMap.valueFor(playerType);
143 CHECK(NULL != factory);
144 p = factory->createPlayer();
145
146 if (p == NULL) {
147 ALOGE("Failed to create player object of type %d, create failed",
148 playerType);
149 return p;
150 }
151
152 init_result = p->initCheck();
153 if (init_result == NO_ERROR) {
154 p->setNotifyCallback(cookie, notifyFunc);
155 } else {
156 ALOGE("Failed to create player object of type %d, initCheck failed"
157 " (res = %d)", playerType, init_result);
158 p.clear();
159 }
160
161 return p;
162}
163
164/*****************************************************************************
165 * *
166 * Built-In Factory Implementations *
167 * *
168 *****************************************************************************/
169
170class StagefrightPlayerFactory :
171 public MediaPlayerFactory::IFactory {
172 public:
173 virtual float scoreFactory(const sp<IMediaPlayer>& client,
174 int fd,
175 int64_t offset,
176 int64_t length,
177 float curScore) {
178 char buf[20];
179 lseek(fd, offset, SEEK_SET);
180 read(fd, buf, sizeof(buf));
181 lseek(fd, offset, SEEK_SET);
182
183 long ident = *((long*)buf);
184
185 // Ogg vorbis?
186 if (ident == 0x5367674f) // 'OggS'
187 return 1.0;
188
189 return 0.0;
190 }
191
192 virtual sp<MediaPlayerBase> createPlayer() {
193 ALOGV(" create StagefrightPlayer");
194 return new StagefrightPlayer();
195 }
196};
197
198class NuPlayerFactory : public MediaPlayerFactory::IFactory {
199 public:
200 virtual float scoreFactory(const sp<IMediaPlayer>& client,
201 const char* url,
202 float curScore) {
203 static const float kOurScore = 0.8;
204
205 if (kOurScore <= curScore)
206 return 0.0;
207
208 if (!strncasecmp("http://", url, 7)
209 || !strncasecmp("https://", url, 8)) {
210 size_t len = strlen(url);
211 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
212 return kOurScore;
213 }
214
215 if (strstr(url,"m3u8")) {
216 return kOurScore;
217 }
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100218
219 if ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) || strstr(url, ".sdp?")) {
220 return kOurScore;
221 }
John Grossman44a7e422012-06-21 17:29:24 -0700222 }
223
224 if (!strncasecmp("rtsp://", url, 7)) {
225 return kOurScore;
226 }
227
228 return 0.0;
229 }
230
231 virtual float scoreFactory(const sp<IMediaPlayer>& client,
232 const sp<IStreamSource> &source,
233 float curScore) {
234 return 1.0;
235 }
236
237 virtual sp<MediaPlayerBase> createPlayer() {
238 ALOGV(" create NuPlayer");
239 return new NuPlayerDriver;
240 }
241};
242
243class SonivoxPlayerFactory : public MediaPlayerFactory::IFactory {
244 public:
245 virtual float scoreFactory(const sp<IMediaPlayer>& client,
246 const char* url,
247 float curScore) {
248 static const float kOurScore = 0.4;
249 static const char* const FILE_EXTS[] = { ".mid",
250 ".midi",
251 ".smf",
252 ".xmf",
Dongwon Kang5c5f8d92012-09-05 19:37:13 +0900253 ".mxmf",
John Grossman44a7e422012-06-21 17:29:24 -0700254 ".imy",
255 ".rtttl",
256 ".rtx",
257 ".ota" };
258 if (kOurScore <= curScore)
259 return 0.0;
260
261 // use MidiFile for MIDI extensions
262 int lenURL = strlen(url);
263 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
264 int len = strlen(FILE_EXTS[i]);
265 int start = lenURL - len;
266 if (start > 0) {
267 if (!strncasecmp(url + start, FILE_EXTS[i], len)) {
268 return kOurScore;
269 }
270 }
271 }
272
273 return 0.0;
274 }
275
276 virtual float scoreFactory(const sp<IMediaPlayer>& client,
277 int fd,
278 int64_t offset,
279 int64_t length,
280 float curScore) {
281 static const float kOurScore = 0.8;
282
283 if (kOurScore <= curScore)
284 return 0.0;
285
286 // Some kind of MIDI?
287 EAS_DATA_HANDLE easdata;
288 if (EAS_Init(&easdata) == EAS_SUCCESS) {
289 EAS_FILE locator;
290 locator.path = NULL;
291 locator.fd = fd;
292 locator.offset = offset;
293 locator.length = length;
294 EAS_HANDLE eashandle;
295 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
296 EAS_CloseFile(easdata, eashandle);
297 EAS_Shutdown(easdata);
298 return kOurScore;
299 }
300 EAS_Shutdown(easdata);
301 }
302
303 return 0.0;
304 }
305
306 virtual sp<MediaPlayerBase> createPlayer() {
307 ALOGV(" create MidiFile");
308 return new MidiFile();
309 }
310};
311
312class TestPlayerFactory : public MediaPlayerFactory::IFactory {
313 public:
314 virtual float scoreFactory(const sp<IMediaPlayer>& client,
315 const char* url,
316 float curScore) {
317 if (TestPlayerStub::canBeUsed(url)) {
318 return 1.0;
319 }
320
321 return 0.0;
322 }
323
324 virtual sp<MediaPlayerBase> createPlayer() {
325 ALOGV("Create Test Player stub");
326 return new TestPlayerStub();
327 }
328};
329
John Grossman44a7e422012-06-21 17:29:24 -0700330void MediaPlayerFactory::registerBuiltinFactories() {
331 Mutex::Autolock lock_(&sLock);
332
333 if (sInitComplete)
334 return;
335
336 registerFactory_l(new StagefrightPlayerFactory(), STAGEFRIGHT_PLAYER);
337 registerFactory_l(new NuPlayerFactory(), NU_PLAYER);
338 registerFactory_l(new SonivoxPlayerFactory(), SONIVOX_PLAYER);
339 registerFactory_l(new TestPlayerFactory(), TEST_PLAYER);
340
John Grossman44a7e422012-06-21 17:29:24 -0700341 sInitComplete = true;
342}
343
344} // namespace android