blob: 3f69c119f69adb4fc4422e5c6191ee7c84dc9514 [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) { \
103 bestScore = getDefaultPlayerType(); \
104 } \
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 }
218 }
219
220 if (!strncasecmp("rtsp://", url, 7)) {
221 return kOurScore;
222 }
223
224 return 0.0;
225 }
226
227 virtual float scoreFactory(const sp<IMediaPlayer>& client,
228 const sp<IStreamSource> &source,
229 float curScore) {
230 return 1.0;
231 }
232
233 virtual sp<MediaPlayerBase> createPlayer() {
234 ALOGV(" create NuPlayer");
235 return new NuPlayerDriver;
236 }
237};
238
239class SonivoxPlayerFactory : public MediaPlayerFactory::IFactory {
240 public:
241 virtual float scoreFactory(const sp<IMediaPlayer>& client,
242 const char* url,
243 float curScore) {
244 static const float kOurScore = 0.4;
245 static const char* const FILE_EXTS[] = { ".mid",
246 ".midi",
247 ".smf",
248 ".xmf",
Dongwon Kang5c5f8d92012-09-05 19:37:13 +0900249 ".mxmf",
John Grossman44a7e422012-06-21 17:29:24 -0700250 ".imy",
251 ".rtttl",
252 ".rtx",
253 ".ota" };
254 if (kOurScore <= curScore)
255 return 0.0;
256
257 // use MidiFile for MIDI extensions
258 int lenURL = strlen(url);
259 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
260 int len = strlen(FILE_EXTS[i]);
261 int start = lenURL - len;
262 if (start > 0) {
263 if (!strncasecmp(url + start, FILE_EXTS[i], len)) {
264 return kOurScore;
265 }
266 }
267 }
268
269 return 0.0;
270 }
271
272 virtual float scoreFactory(const sp<IMediaPlayer>& client,
273 int fd,
274 int64_t offset,
275 int64_t length,
276 float curScore) {
277 static const float kOurScore = 0.8;
278
279 if (kOurScore <= curScore)
280 return 0.0;
281
282 // Some kind of MIDI?
283 EAS_DATA_HANDLE easdata;
284 if (EAS_Init(&easdata) == EAS_SUCCESS) {
285 EAS_FILE locator;
286 locator.path = NULL;
287 locator.fd = fd;
288 locator.offset = offset;
289 locator.length = length;
290 EAS_HANDLE eashandle;
291 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
292 EAS_CloseFile(easdata, eashandle);
293 EAS_Shutdown(easdata);
294 return kOurScore;
295 }
296 EAS_Shutdown(easdata);
297 }
298
299 return 0.0;
300 }
301
302 virtual sp<MediaPlayerBase> createPlayer() {
303 ALOGV(" create MidiFile");
304 return new MidiFile();
305 }
306};
307
308class TestPlayerFactory : public MediaPlayerFactory::IFactory {
309 public:
310 virtual float scoreFactory(const sp<IMediaPlayer>& client,
311 const char* url,
312 float curScore) {
313 if (TestPlayerStub::canBeUsed(url)) {
314 return 1.0;
315 }
316
317 return 0.0;
318 }
319
320 virtual sp<MediaPlayerBase> createPlayer() {
321 ALOGV("Create Test Player stub");
322 return new TestPlayerStub();
323 }
324};
325
John Grossman44a7e422012-06-21 17:29:24 -0700326void MediaPlayerFactory::registerBuiltinFactories() {
327 Mutex::Autolock lock_(&sLock);
328
329 if (sInitComplete)
330 return;
331
332 registerFactory_l(new StagefrightPlayerFactory(), STAGEFRIGHT_PLAYER);
333 registerFactory_l(new NuPlayerFactory(), NU_PLAYER);
334 registerFactory_l(new SonivoxPlayerFactory(), SONIVOX_PLAYER);
335 registerFactory_l(new TestPlayerFactory(), TEST_PLAYER);
336
John Grossman44a7e422012-06-21 17:29:24 -0700337 sInitComplete = true;
338}
339
340} // namespace android