MediaPlayerFactory.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. //#define LOG_NDEBUG 0
  18. #define LOG_TAG "MediaPlayerFactory"
  19. #include <utils/Log.h>
  20. #include <cutils/properties.h>
  21. #include <media/DataSource.h>
  22. #include <media/IMediaPlayer.h>
  23. #include <media/stagefright/FileSource.h>
  24. #include <media/stagefright/foundation/ADebug.h>
  25. #include <utils/Errors.h>
  26. #include <utils/misc.h>
  27. #include "MediaPlayerFactory.h"
  28. #include "TestPlayerStub.h"
  29. #include "nuplayer/NuPlayerDriver.h"
  30. namespace android {
  31. Mutex MediaPlayerFactory::sLock;
  32. MediaPlayerFactory::tFactoryMap MediaPlayerFactory::sFactoryMap;
  33. bool MediaPlayerFactory::sInitComplete = false;
  34. status_t MediaPlayerFactory::registerFactory_l(IFactory* factory,
  35. player_type type) {
  36. if (NULL == factory) {
  37. ALOGE("Failed to register MediaPlayerFactory of type %d, factory is"
  38. " NULL.", type);
  39. return BAD_VALUE;
  40. }
  41. if (sFactoryMap.indexOfKey(type) >= 0) {
  42. ALOGE("Failed to register MediaPlayerFactory of type %d, type is"
  43. " already registered.", type);
  44. return ALREADY_EXISTS;
  45. }
  46. if (sFactoryMap.add(type, factory) < 0) {
  47. ALOGE("Failed to register MediaPlayerFactory of type %d, failed to add"
  48. " to map.", type);
  49. return UNKNOWN_ERROR;
  50. }
  51. return OK;
  52. }
  53. static player_type getDefaultPlayerType() {
  54. return NU_PLAYER;
  55. }
  56. status_t MediaPlayerFactory::registerFactory(IFactory* factory,
  57. player_type type) {
  58. Mutex::Autolock lock_(&sLock);
  59. return registerFactory_l(factory, type);
  60. }
  61. void MediaPlayerFactory::unregisterFactory(player_type type) {
  62. Mutex::Autolock lock_(&sLock);
  63. sFactoryMap.removeItem(type);
  64. }
  65. #define GET_PLAYER_TYPE_IMPL(a...) \
  66. Mutex::Autolock lock_(&sLock); \
  67. \
  68. player_type ret = STAGEFRIGHT_PLAYER; \
  69. float bestScore = 0.0; \
  70. \
  71. for (size_t i = 0; i < sFactoryMap.size(); ++i) { \
  72. \
  73. IFactory* v = sFactoryMap.valueAt(i); \
  74. float thisScore; \
  75. CHECK(v != NULL); \
  76. thisScore = v->scoreFactory(a, bestScore); \
  77. if (thisScore > bestScore) { \
  78. ret = sFactoryMap.keyAt(i); \
  79. bestScore = thisScore; \
  80. } \
  81. } \
  82. \
  83. if (0.0 == bestScore) { \
  84. ret = getDefaultPlayerType(); \
  85. } \
  86. \
  87. return ret;
  88. player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
  89. const char* url) {
  90. GET_PLAYER_TYPE_IMPL(client, url);
  91. }
  92. player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
  93. int fd,
  94. int64_t offset,
  95. int64_t length) {
  96. GET_PLAYER_TYPE_IMPL(client, fd, offset, length);
  97. }
  98. player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
  99. const sp<IStreamSource> &source) {
  100. GET_PLAYER_TYPE_IMPL(client, source);
  101. }
  102. player_type MediaPlayerFactory::getPlayerType(const sp<IMediaPlayer>& client,
  103. const sp<DataSource> &source) {
  104. GET_PLAYER_TYPE_IMPL(client, source);
  105. }
  106. #undef GET_PLAYER_TYPE_IMPL
  107. sp<MediaPlayerBase> MediaPlayerFactory::createPlayer(
  108. player_type playerType,
  109. const sp<MediaPlayerBase::Listener> &listener,
  110. pid_t pid) {
  111. sp<MediaPlayerBase> p;
  112. IFactory* factory;
  113. status_t init_result;
  114. Mutex::Autolock lock_(&sLock);
  115. if (sFactoryMap.indexOfKey(playerType) < 0) {
  116. ALOGE("Failed to create player object of type %d, no registered"
  117. " factory", playerType);
  118. return p;
  119. }
  120. factory = sFactoryMap.valueFor(playerType);
  121. CHECK(NULL != factory);
  122. p = factory->createPlayer(pid);
  123. if (p == NULL) {
  124. ALOGE("Failed to create player object of type %d, create failed",
  125. playerType);
  126. return p;
  127. }
  128. init_result = p->initCheck();
  129. if (init_result == NO_ERROR) {
  130. p->setNotifyCallback(listener);
  131. } else {
  132. ALOGE("Failed to create player object of type %d, initCheck failed"
  133. " (res = %d)", playerType, init_result);
  134. p.clear();
  135. }
  136. return p;
  137. }
  138. /*****************************************************************************
  139. * *
  140. * Built-In Factory Implementations *
  141. * *
  142. *****************************************************************************/
  143. class NuPlayerFactory : public MediaPlayerFactory::IFactory {
  144. public:
  145. virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
  146. const char* url,
  147. float curScore) {
  148. static const float kOurScore = 0.8;
  149. if (kOurScore <= curScore)
  150. return 0.0;
  151. if (!strncasecmp("http://", url, 7)
  152. || !strncasecmp("https://", url, 8)
  153. || !strncasecmp("file://", url, 7)) {
  154. size_t len = strlen(url);
  155. if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
  156. return kOurScore;
  157. }
  158. if (strstr(url,"m3u8")) {
  159. return kOurScore;
  160. }
  161. if ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) || strstr(url, ".sdp?")) {
  162. return kOurScore;
  163. }
  164. }
  165. if (!strncasecmp("rtsp://", url, 7)) {
  166. return kOurScore;
  167. }
  168. return 0.0;
  169. }
  170. virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
  171. const sp<IStreamSource>& /*source*/,
  172. float /*curScore*/) {
  173. return 1.0;
  174. }
  175. virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
  176. const sp<DataSource>& /*source*/,
  177. float /*curScore*/) {
  178. // Only NuPlayer supports setting a DataSource source directly.
  179. return 1.0;
  180. }
  181. virtual sp<MediaPlayerBase> createPlayer(pid_t pid) {
  182. ALOGV(" create NuPlayer");
  183. return new NuPlayerDriver(pid);
  184. }
  185. };
  186. class TestPlayerFactory : public MediaPlayerFactory::IFactory {
  187. public:
  188. virtual float scoreFactory(const sp<IMediaPlayer>& /*client*/,
  189. const char* url,
  190. float /*curScore*/) {
  191. if (TestPlayerStub::canBeUsed(url)) {
  192. return 1.0;
  193. }
  194. return 0.0;
  195. }
  196. virtual sp<MediaPlayerBase> createPlayer(pid_t /* pid */) {
  197. ALOGV("Create Test Player stub");
  198. return new TestPlayerStub();
  199. }
  200. };
  201. void MediaPlayerFactory::registerBuiltinFactories() {
  202. Mutex::Autolock lock_(&sLock);
  203. if (sInitComplete)
  204. return;
  205. IFactory* factory = new NuPlayerFactory();
  206. if (registerFactory_l(factory, NU_PLAYER) != OK)
  207. delete factory;
  208. factory = new TestPlayerFactory();
  209. if (registerFactory_l(factory, TEST_PLAYER) != OK)
  210. delete factory;
  211. sInitComplete = true;
  212. }
  213. } // namespace android