MediaPlayerFactory.cpp revision b47558f87ee9bd4fce11a2d5bc213bd1aa9031a8
1/* 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 36Mutex 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 // TODO: remove this EXPERIMENTAL developer settings property 71 if (property_get("persist.sys.media.use-nuplayer", value, NULL) 72 && !strcasecmp("true", value)) { 73 return NU_PLAYER; 74 } 75 76 return STAGEFRIGHT_PLAYER; 77} 78 79status_t MediaPlayerFactory::registerFactory(IFactory* factory, 80 player_type type) { 81 Mutex::Autolock lock_(&sLock); 82 return registerFactory_l(factory, type); 83} 84 85void MediaPlayerFactory::unregisterFactory(player_type type) { 86 Mutex::Autolock lock_(&sLock); 87 sFactoryMap.removeItem(type); 88} 89 90#define GET_PLAYER_TYPE_IMPL(a...) \ 91 Mutex::Autolock lock_(&sLock); \ 92 \ 93 player_type ret = STAGEFRIGHT_PLAYER; \ 94 float bestScore = 0.0; \ 95 \ 96 for (size_t i = 0; i < sFactoryMap.size(); ++i) { \ 97 \ 98 IFactory* v = sFactoryMap.valueAt(i); \ 99 float thisScore; \ 100 CHECK(v != NULL); \ 101 thisScore = v->scoreFactory(a, bestScore); \ 102 if (thisScore > bestScore) { \ 103 ret = sFactoryMap.keyAt(i); \ 104 bestScore = thisScore; \ 105 } \ 106 } \ 107 \ 108 if (0.0 == bestScore) { \ 109 ret = getDefaultPlayerType(); \ 110 } \ 111 \ 112 return ret; 113 114player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client, 115 const char* url) { 116 GET_PLAYER_TYPE_IMPL(client, url); 117} 118 119player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client, 120 int fd, 121 int64_t offset, 122 int64_t length) { 123 GET_PLAYER_TYPE_IMPL(client, fd, offset, length); 124} 125 126player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client, 127 const sp<IStreamSource> &source) { 128 GET_PLAYER_TYPE_IMPL(client, source); 129} 130 131#undef GET_PLAYER_TYPE_IMPL 132 133sp<MediaPlayerBase> MediaPlayerFactory::createPlayer( 134 player_type playerType, 135 void* cookie, 136 notify_callback_f notifyFunc) { 137 sp<MediaPlayerBase> p; 138 IFactory* factory; 139 status_t init_result; 140 Mutex::Autolock lock_(&sLock); 141 142 if (sFactoryMap.indexOfKey(playerType) < 0) { 143 ALOGE("Failed to create player object of type %d, no registered" 144 " factory", playerType); 145 return p; 146 } 147 148 factory = sFactoryMap.valueFor(playerType); 149 CHECK(NULL != factory); 150 p = factory->createPlayer(); 151 152 if (p == NULL) { 153 ALOGE("Failed to create player object of type %d, create failed", 154 playerType); 155 return p; 156 } 157 158 init_result = p->initCheck(); 159 if (init_result == NO_ERROR) { 160 p->setNotifyCallback(cookie, notifyFunc); 161 } else { 162 ALOGE("Failed to create player object of type %d, initCheck failed" 163 " (res = %d)", playerType, init_result); 164 p.clear(); 165 } 166 167 return p; 168} 169 170/***************************************************************************** 171 * * 172 * Built-In Factory Implementations * 173 * * 174 *****************************************************************************/ 175 176class StagefrightPlayerFactory : 177 public MediaPlayerFactory::IFactory { 178 public: 179 virtual float scoreFactory(const sp<IMediaPlayer>& client, 180 int fd, 181 int64_t offset, 182 int64_t length, 183 float curScore) { 184 char buf[20]; 185 lseek(fd, offset, SEEK_SET); 186 read(fd, buf, sizeof(buf)); 187 lseek(fd, offset, SEEK_SET); 188 189 long ident = *((long*)buf); 190 191 // Ogg vorbis? 192 if (ident == 0x5367674f) // 'OggS' 193 return 1.0; 194 195 return 0.0; 196 } 197 198 virtual sp<MediaPlayerBase> createPlayer() { 199 ALOGV(" create StagefrightPlayer"); 200 return new StagefrightPlayer(); 201 } 202}; 203 204class NuPlayerFactory : public MediaPlayerFactory::IFactory { 205 public: 206 virtual float scoreFactory(const sp<IMediaPlayer>& client, 207 const char* url, 208 float curScore) { 209 static const float kOurScore = 0.8; 210 211 if (kOurScore <= curScore) 212 return 0.0; 213 214 if (!strncasecmp("http://", url, 7) 215 || !strncasecmp("https://", url, 8) 216 || !strncasecmp("file://", url, 7)) { 217 size_t len = strlen(url); 218 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 219 return kOurScore; 220 } 221 222 if (strstr(url,"m3u8")) { 223 return kOurScore; 224 } 225 226 if ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) || strstr(url, ".sdp?")) { 227 return kOurScore; 228 } 229 } 230 231 if (!strncasecmp("rtsp://", url, 7)) { 232 return kOurScore; 233 } 234 235 return 0.0; 236 } 237 238 virtual float scoreFactory(const sp<IMediaPlayer>& client, 239 const sp<IStreamSource> &source, 240 float curScore) { 241 return 1.0; 242 } 243 244 virtual sp<MediaPlayerBase> createPlayer() { 245 ALOGV(" create NuPlayer"); 246 return new NuPlayerDriver; 247 } 248}; 249 250class SonivoxPlayerFactory : public MediaPlayerFactory::IFactory { 251 public: 252 virtual float scoreFactory(const sp<IMediaPlayer>& client, 253 const char* url, 254 float curScore) { 255 static const float kOurScore = 0.4; 256 static const char* const FILE_EXTS[] = { ".mid", 257 ".midi", 258 ".smf", 259 ".xmf", 260 ".mxmf", 261 ".imy", 262 ".rtttl", 263 ".rtx", 264 ".ota" }; 265 if (kOurScore <= curScore) 266 return 0.0; 267 268 // use MidiFile for MIDI extensions 269 int lenURL = strlen(url); 270 for (int i = 0; i < NELEM(FILE_EXTS); ++i) { 271 int len = strlen(FILE_EXTS[i]); 272 int start = lenURL - len; 273 if (start > 0) { 274 if (!strncasecmp(url + start, FILE_EXTS[i], len)) { 275 return kOurScore; 276 } 277 } 278 } 279 280 return 0.0; 281 } 282 283 virtual float scoreFactory(const sp<IMediaPlayer>& client, 284 int fd, 285 int64_t offset, 286 int64_t length, 287 float curScore) { 288 static const float kOurScore = 0.8; 289 290 if (kOurScore <= curScore) 291 return 0.0; 292 293 // Some kind of MIDI? 294 EAS_DATA_HANDLE easdata; 295 if (EAS_Init(&easdata) == EAS_SUCCESS) { 296 EAS_FILE locator; 297 locator.path = NULL; 298 locator.fd = fd; 299 locator.offset = offset; 300 locator.length = length; 301 EAS_HANDLE eashandle; 302 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) { 303 EAS_CloseFile(easdata, eashandle); 304 EAS_Shutdown(easdata); 305 return kOurScore; 306 } 307 EAS_Shutdown(easdata); 308 } 309 310 return 0.0; 311 } 312 313 virtual sp<MediaPlayerBase> createPlayer() { 314 ALOGV(" create MidiFile"); 315 return new MidiFile(); 316 } 317}; 318 319class TestPlayerFactory : public MediaPlayerFactory::IFactory { 320 public: 321 virtual float scoreFactory(const sp<IMediaPlayer>& client, 322 const char* url, 323 float curScore) { 324 if (TestPlayerStub::canBeUsed(url)) { 325 return 1.0; 326 } 327 328 return 0.0; 329 } 330 331 virtual sp<MediaPlayerBase> createPlayer() { 332 ALOGV("Create Test Player stub"); 333 return new TestPlayerStub(); 334 } 335}; 336 337void MediaPlayerFactory::registerBuiltinFactories() { 338 Mutex::Autolock lock_(&sLock); 339 340 if (sInitComplete) 341 return; 342 343 registerFactory_l(new StagefrightPlayerFactory(), STAGEFRIGHT_PLAYER); 344 registerFactory_l(new NuPlayerFactory(), NU_PLAYER); 345 registerFactory_l(new SonivoxPlayerFactory(), SONIVOX_PLAYER); 346 registerFactory_l(new TestPlayerFactory(), TEST_PLAYER); 347 348 sInitComplete = true; 349} 350 351} // namespace android 352