ProxyPluginMgr.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #include "ProxyPluginMgr.h"
  23. #include "ProxyConsumer.h"
  24. #include "ProxyProducer.h"
  25. //
  26. // "twrap_proxy_plugin_t" Declarations
  27. //
  28. typedef struct twrap_proxy_plugin_s {
  29. TSK_DECLARE_OBJECT;
  30. ProxyPlugin* plugin;
  31. }
  32. twrap_proxy_plugin_t;
  33. #define TWRAP_PROXY_PLUGIN(self) ((twrap_proxy_plugin_t*)(self))
  34. static int pred_find_plugin_by_value(const tsk_list_item_t *item, const void *proxyPlugin);
  35. static twrap_proxy_plugin_t* twrap_proxy_plugin_create(ProxyPlugin** plugin);
  36. //
  37. // "ProxyPluginMgr" Class Implementation
  38. //
  39. ProxyPluginMgr* ProxyPluginMgr::instance = tsk_null;
  40. static uint64_t __uniqueId = 0;
  41. ProxyPluginMgr::ProxyPluginMgr(ProxyPluginMgrCallback* _callback)
  42. :callback(_callback)
  43. {
  44. this->plugins = tsk_list_create();
  45. if(!this->callback) {
  46. TSK_DEBUG_WARN("Callback function is Null => You will have big problems as we won't check it before call");
  47. }
  48. }
  49. ProxyPluginMgr::~ProxyPluginMgr()
  50. {
  51. if(this == ProxyPluginMgr::instance) {
  52. ProxyPluginMgr::instance = tsk_null;
  53. }
  54. TSK_OBJECT_SAFE_FREE(this->plugins);
  55. }
  56. ProxyPluginMgr* ProxyPluginMgr::createInstance(ProxyPluginMgrCallback* pCallback)
  57. {
  58. if(!ProxyPluginMgr::instance) {
  59. ProxyPluginMgr::instance = new ProxyPluginMgr(pCallback);
  60. }
  61. else {
  62. TSK_DEBUG_WARN("Plugin instance already exist");
  63. ProxyPluginMgr::instance->callback = pCallback;
  64. }
  65. return ProxyPluginMgr::instance;
  66. }
  67. void ProxyPluginMgr::destroyInstance(ProxyPluginMgr** ppInstance)
  68. {
  69. if(ppInstance && *ppInstance) {
  70. bool bMatch = ProxyPluginMgr::instance && (*ppInstance == ProxyPluginMgr::instance);
  71. delete *ppInstance, *ppInstance = tsk_null;
  72. if(bMatch) {
  73. ProxyPluginMgr::instance = tsk_null;
  74. }
  75. }
  76. }
  77. ProxyPluginMgr* ProxyPluginMgr::getInstance()
  78. {
  79. if(!ProxyPluginMgr::instance) {
  80. TSK_DEBUG_ERROR("No instance of the manager could be found");
  81. }
  82. return ProxyPluginMgr::instance;
  83. }
  84. uint64_t ProxyPluginMgr::getUniqueId()
  85. {
  86. return ++__uniqueId;
  87. }
  88. int ProxyPluginMgr::addPlugin(ProxyPlugin** plugin)
  89. {
  90. twrap_proxy_plugin_t* twrap_plugin;
  91. int ret = -1;
  92. tsk_list_lock(this->plugins);
  93. if(!plugin || !*plugin) {
  94. TSK_DEBUG_ERROR("Invalid parameter");
  95. goto bail;
  96. }
  97. if(tsk_list_find_item_by_pred(this->plugins, pred_find_plugin_by_value, *plugin)) {
  98. TSK_DEBUG_ERROR("Plugin already exist");
  99. goto bail;
  100. }
  101. if((twrap_plugin = twrap_proxy_plugin_create(plugin))) {
  102. tsk_list_push_back_data(this->plugins, (void**)&twrap_plugin);
  103. ret = 0;
  104. }
  105. else {
  106. TSK_DEBUG_ERROR("Failed to create plugin");
  107. goto bail;
  108. }
  109. bail:
  110. tsk_list_unlock(this->plugins);
  111. return ret;
  112. }
  113. int ProxyPluginMgr::removePlugin(ProxyPlugin** plugin)
  114. {
  115. if(!plugin || !*plugin) {
  116. TSK_DEBUG_ERROR("Invalid parameter");
  117. return -1;
  118. }
  119. return this->removePlugin((*plugin)->getId());
  120. }
  121. const ProxyPlugin* ProxyPluginMgr::findPlugin(uint64_t id)
  122. {
  123. ProxyPlugin* ret = tsk_null;
  124. tsk_list_item_t* item;
  125. tsk_list_lock(this->plugins);
  126. tsk_list_foreach(item, this->plugins) {
  127. if(TWRAP_PROXY_PLUGIN(item->data)->plugin->getId() == id) {
  128. ret = TWRAP_PROXY_PLUGIN(item->data)->plugin;
  129. break;
  130. }
  131. }
  132. tsk_list_unlock(this->plugins);
  133. return ret;
  134. }
  135. const ProxyPlugin* ProxyPluginMgr::findPlugin(tsk_object_t* wrapped_plugin)
  136. {
  137. ProxyPlugin* ret = tsk_null;
  138. tsk_list_item_t* item;
  139. if(!wrapped_plugin) {
  140. TSK_DEBUG_ERROR("Invalid parameter");
  141. return tsk_null;
  142. }
  143. tsk_list_lock(this->plugins);
  144. tsk_list_foreach(item, this->plugins) {
  145. if(TWRAP_PROXY_PLUGIN(item->data)->plugin->isWrapping(wrapped_plugin)) {
  146. ret = TWRAP_PROXY_PLUGIN(item->data)->plugin;
  147. break;
  148. }
  149. }
  150. tsk_list_unlock(this->plugins);
  151. return ret;
  152. }
  153. int ProxyPluginMgr::removePlugin(uint64_t id)
  154. {
  155. tsk_list_item_t* item;
  156. tsk_list_lock(this->plugins);
  157. tsk_list_foreach(item, this->plugins) {
  158. if(TWRAP_PROXY_PLUGIN(item->data)->plugin->getId() == id) {
  159. tsk_list_remove_item(this->plugins, item);
  160. break;
  161. }
  162. }
  163. tsk_list_unlock(this->plugins);
  164. return 0;
  165. }
  166. const ProxyAudioConsumer* ProxyPluginMgr::findAudioConsumer(uint64_t id)
  167. {
  168. const ProxyPlugin* audioConsumer = this->findPlugin(id);
  169. if(audioConsumer && audioConsumer->getType() == twrap_proxy_plugin_audio_consumer) {
  170. return dyn_cast<const ProxyAudioConsumer*>(audioConsumer);
  171. }
  172. return tsk_null;
  173. }
  174. const ProxyVideoConsumer* ProxyPluginMgr::findVideoConsumer(uint64_t id)
  175. {
  176. const ProxyPlugin* videoConsumer = this->findPlugin(id);
  177. if(videoConsumer && videoConsumer->getType() == twrap_proxy_plugin_video_consumer) {
  178. return dyn_cast<const ProxyVideoConsumer*>(videoConsumer);
  179. }
  180. return tsk_null;
  181. }
  182. const ProxyAudioProducer* ProxyPluginMgr::findAudioProducer(uint64_t id)
  183. {
  184. const ProxyPlugin* audioProducer = this->findPlugin(id);
  185. if(audioProducer && audioProducer->getType() == twrap_proxy_plugin_audio_producer) {
  186. return dyn_cast<const ProxyAudioProducer*>(audioProducer);
  187. }
  188. return tsk_null;
  189. }
  190. const ProxyVideoProducer* ProxyPluginMgr::findVideoProducer(uint64_t id)
  191. {
  192. const ProxyPlugin* videoProducer = this->findPlugin(id);
  193. if(videoProducer && videoProducer->getType() == twrap_proxy_plugin_video_producer) {
  194. return dyn_cast<const ProxyVideoProducer*>(videoProducer);
  195. }
  196. return tsk_null;
  197. }
  198. //
  199. // "twrap_proxy_plugin_t" Implementations
  200. //
  201. static tsk_object_t* twrap_proxy_plugin_ctor(tsk_object_t * self, va_list * app)
  202. {
  203. twrap_proxy_plugin_t *_self = dyn_cast<twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(self));
  204. if(_self) {
  205. }
  206. return self;
  207. }
  208. static tsk_object_t* twrap_proxy_plugin_dtor(tsk_object_t * self)
  209. {
  210. twrap_proxy_plugin_t *_self = dyn_cast<twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(self));
  211. if(_self) {
  212. if(_self->plugin) {
  213. delete _self->plugin, _self->plugin = tsk_null;
  214. }
  215. }
  216. return self;
  217. }
  218. static int twrap_proxy_plugin_cmp(const tsk_object_t *_c1, const tsk_object_t *_c2)
  219. {
  220. const twrap_proxy_plugin_t *c1 = dyn_cast<const twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(_c1));
  221. const twrap_proxy_plugin_t *c2 = dyn_cast<const twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(_c2));
  222. if(c1 && c2) {
  223. return (c1->plugin == c2->plugin); // See "ProxyPlugin::operator =="
  224. }
  225. else if(!c1 && !c2) {
  226. return 0;
  227. }
  228. else {
  229. return -1;
  230. }
  231. }
  232. static const tsk_object_def_t twrap_proxy_plugin_def_s = {
  233. sizeof(twrap_proxy_plugin_t),
  234. twrap_proxy_plugin_ctor,
  235. twrap_proxy_plugin_dtor,
  236. twrap_proxy_plugin_cmp,
  237. };
  238. const tsk_object_def_t *twrap_proxy_plugin_def_t = &twrap_proxy_plugin_def_s;
  239. static int pred_find_plugin_by_value(const tsk_list_item_t *item, const void *proxyPlugin)
  240. {
  241. if(item && item->data) {
  242. const twrap_proxy_plugin_t *twrap_plugin = dyn_cast<const twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(item->data));
  243. return (twrap_plugin->plugin == dyn_cast<const ProxyPlugin *>((const ProxyPlugin*)proxyPlugin)) ? 0 : -1;
  244. }
  245. return -1;
  246. }
  247. static twrap_proxy_plugin_t* twrap_proxy_plugin_create(ProxyPlugin** plugin)
  248. {
  249. if(!plugin || !*plugin) {
  250. TSK_DEBUG_ERROR("Invalid parameter");
  251. return tsk_null;
  252. }
  253. twrap_proxy_plugin_t* twrap_plugin = (twrap_proxy_plugin_t*)tsk_object_new(twrap_proxy_plugin_def_t);
  254. if(!twrap_plugin) {
  255. TSK_DEBUG_ERROR("Failed to create new instance of 'twrap_proxy_plugin_t'");
  256. return tsk_null;
  257. }
  258. twrap_plugin->plugin = *plugin,
  259. *plugin = tsk_null;
  260. return twrap_plugin;
  261. }