plugin_video_dshow_producer.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (C) 2011-2013 Doubango Telecom <http://www.doubango.org>
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. */
  18. #include "internals/DSGrabber.h"
  19. #include "internals/DSDisplay.h"
  20. #include "internals/DSUtils.h"
  21. #include "tinymedia/tmedia_producer.h"
  22. #include "tsk_string.h"
  23. #include "tsk_debug.h"
  24. #define DSPRODUCER(self) ((plugin_video_dshow_producer_t*)(self))
  25. typedef struct plugin_video_dshow_producer_s {
  26. TMEDIA_DECLARE_PRODUCER;
  27. DSGrabber* grabber;
  28. INT64 previewHwnd;
  29. tsk_bool_t plugin_firefox;
  30. tsk_bool_t started;
  31. tsk_bool_t mute;
  32. tsk_bool_t create_on_ui_thread;
  33. }
  34. plugin_video_dshow_producer_t;
  35. // Producer callback (From DirectShow Grabber to our plugin)
  36. static int plugin_video_dshow_plugin_cb(const void* callback_data, const void* buffer, tsk_size_t size)
  37. {
  38. const plugin_video_dshow_producer_t* producer = (const plugin_video_dshow_producer_t*)callback_data;
  39. if(producer && TMEDIA_PRODUCER(producer)->enc_cb.callback) {
  40. TMEDIA_PRODUCER(producer)->enc_cb.callback(TMEDIA_PRODUCER(producer)->enc_cb.callback_data, buffer, size);
  41. }
  42. return 0;
  43. }
  44. /* ============ Media Producer Interface ================= */
  45. static int plugin_video_dshow_producer_set(tmedia_producer_t *self, const tmedia_param_t* param)
  46. {
  47. int ret = 0;
  48. plugin_video_dshow_producer_t* producer = (plugin_video_dshow_producer_t*)self;
  49. if(!producer || !param) {
  50. TSK_DEBUG_ERROR("Invalid parameter");
  51. return -1;
  52. }
  53. if(param->value_type == tmedia_pvt_int64) {
  54. if(tsk_striequals(param->key, "local-hwnd")) {
  55. DSPRODUCER(producer)->previewHwnd = (INT64)*((int64_t*)param->value);
  56. if(DSPRODUCER(producer)->grabber && DSPRODUCER(self)->grabber->preview) {
  57. DSPRODUCER(producer)->grabber->preview->attach(DSPRODUCER(producer)->previewHwnd);
  58. }
  59. }
  60. }
  61. else if(param->value_type == tmedia_pvt_int32) {
  62. if(tsk_striequals(param->key, "mute")) {
  63. producer->mute = (TSK_TO_INT32((uint8_t*)param->value) != 0);
  64. if(producer->started) {
  65. if (producer->mute) {
  66. producer->grabber->pause();
  67. }
  68. else {
  69. producer->grabber->start();
  70. }
  71. }
  72. }
  73. else if(tsk_striequals(param->key, "create-on-current-thead")) {
  74. producer->create_on_ui_thread = *((int32_t*)param->value) ? tsk_false : tsk_true;
  75. }
  76. else if(tsk_striequals(param->key, "plugin-firefox")) {
  77. producer->plugin_firefox = (*((int32_t*)param->value) != 0);
  78. if(producer->grabber) {
  79. producer->grabber->setPluginFirefox((producer->plugin_firefox == tsk_true));
  80. }
  81. }
  82. }
  83. return ret;
  84. }
  85. static int plugin_video_dshow_producer_prepare(tmedia_producer_t* self, const tmedia_codec_t* codec)
  86. {
  87. plugin_video_dshow_producer_t* producer = (plugin_video_dshow_producer_t*)self;
  88. if(!producer || !codec && codec->plugin) {
  89. TSK_DEBUG_ERROR("Invalid parameter");
  90. return -1;
  91. }
  92. TMEDIA_PRODUCER(producer)->video.fps = TMEDIA_CODEC_VIDEO(codec)->out.fps;
  93. TMEDIA_PRODUCER(producer)->video.width = TMEDIA_CODEC_VIDEO(codec)->out.width;
  94. TMEDIA_PRODUCER(producer)->video.height = TMEDIA_CODEC_VIDEO(codec)->out.height;
  95. return 0;
  96. }
  97. static int plugin_video_dshow_producer_start(tmedia_producer_t* self)
  98. {
  99. plugin_video_dshow_producer_t* producer = (plugin_video_dshow_producer_t*)self;
  100. if(!producer) {
  101. TSK_DEBUG_ERROR("Invalid parameter");
  102. return -1;
  103. }
  104. if (producer->started) {
  105. return 0;
  106. }
  107. // create grabber on UI thread
  108. if (!producer->grabber) {
  109. static BOOL __isDisplayFalse = FALSE;
  110. static BOOL __isScreenCastFalse = FALSE;
  111. if(producer->create_on_ui_thread) {
  112. createOnUIThead(reinterpret_cast<HWND>((void*)DSPRODUCER(producer)->previewHwnd), (void**)&producer->grabber, __isDisplayFalse, __isScreenCastFalse);
  113. }
  114. else {
  115. createOnCurrentThead(reinterpret_cast<HWND>((void*)DSPRODUCER(producer)->previewHwnd), (void**)&producer->grabber, __isDisplayFalse, __isScreenCastFalse);
  116. }
  117. if (!producer->grabber) {
  118. TSK_DEBUG_ERROR("Failed to create grabber");
  119. return -2;
  120. }
  121. }
  122. producer->grabber->setPluginFirefox((producer->plugin_firefox == tsk_true));
  123. //set Source device
  124. producer->grabber->setCaptureDevice("Null");
  125. // set parameters
  126. producer->grabber->setCaptureParameters((int)TMEDIA_PRODUCER(producer)->video.width, (int)TMEDIA_PRODUCER(producer)->video.height, TMEDIA_PRODUCER(producer)->video.fps);
  127. // set callback function
  128. producer->grabber->setCallback(plugin_video_dshow_plugin_cb, producer);
  129. // attach preview
  130. if(producer->grabber->preview) {
  131. if(producer->previewHwnd) {
  132. producer->grabber->preview->attach(producer->previewHwnd);
  133. }
  134. producer->grabber->preview->setSize((int)TMEDIA_PRODUCER(producer)->video.width, (int)TMEDIA_PRODUCER(producer)->video.height);
  135. }
  136. // start grabber
  137. if(!producer->mute) {
  138. producer->grabber->start();
  139. }
  140. producer->started = tsk_true;
  141. return 0;
  142. }
  143. static int plugin_video_dshow_producer_pause(tmedia_producer_t* self)
  144. {
  145. plugin_video_dshow_producer_t* producer = (plugin_video_dshow_producer_t*)self;
  146. if(!producer) {
  147. TSK_DEBUG_ERROR("Invalid parameter");
  148. return -1;
  149. }
  150. if(!producer->grabber) {
  151. TSK_DEBUG_ERROR("Invalid internal grabber");
  152. return -2;
  153. }
  154. producer->grabber->pause();
  155. return 0;
  156. }
  157. static int plugin_video_dshow_producer_stop(tmedia_producer_t* self)
  158. {
  159. plugin_video_dshow_producer_t* producer = (plugin_video_dshow_producer_t*)self;
  160. if(!self) {
  161. TSK_DEBUG_ERROR("Invalid parameter");
  162. return -1;
  163. }
  164. if(!producer->started) {
  165. return 0;
  166. }
  167. if(!producer->grabber) {
  168. TSK_DEBUG_ERROR("Invalid internal grabber");
  169. return -2;
  170. }
  171. producer->grabber->stop();
  172. producer->started = tsk_false;
  173. return 0;
  174. }
  175. //
  176. // WaveAPI producer object definition
  177. //
  178. /* constructor */
  179. static tsk_object_t* plugin_video_dshow_producer_ctor(tsk_object_t * self, va_list * app)
  180. {
  181. plugin_video_dshow_producer_t *producer = (plugin_video_dshow_producer_t *)self;
  182. if (producer) {
  183. /* init base */
  184. tmedia_producer_init(TMEDIA_PRODUCER(producer));
  185. TMEDIA_PRODUCER(producer)->video.chroma = tmedia_chroma_bgr24; // RGB24 on x86 (little endians) stored as BGR24
  186. /* init self with default values*/
  187. producer->create_on_ui_thread = tsk_true;
  188. TMEDIA_PRODUCER(producer)->video.fps = 15;
  189. TMEDIA_PRODUCER(producer)->video.width = 352;
  190. TMEDIA_PRODUCER(producer)->video.height = 288;
  191. }
  192. return self;
  193. }
  194. /* destructor */
  195. static tsk_object_t* plugin_video_dshow_producer_dtor(tsk_object_t * self)
  196. {
  197. plugin_video_dshow_producer_t *producer = (plugin_video_dshow_producer_t *)self;
  198. if(producer) {
  199. /* stop */
  200. if(producer->started) {
  201. plugin_video_dshow_producer_stop((tmedia_producer_t*)self);
  202. }
  203. /* for safety */
  204. if(producer->grabber) {
  205. producer->grabber->setCallback(tsk_null, tsk_null);
  206. }
  207. /* deinit base */
  208. tmedia_producer_deinit(TMEDIA_PRODUCER(producer));
  209. /* deinit self */
  210. SAFE_DELETE_PTR(producer->grabber);
  211. }
  212. return self;
  213. }
  214. /* object definition */
  215. static const tsk_object_def_t plugin_video_dshow_producer_def_s = {
  216. sizeof(plugin_video_dshow_producer_t),
  217. plugin_video_dshow_producer_ctor,
  218. plugin_video_dshow_producer_dtor,
  219. tsk_null,
  220. };
  221. /* plugin definition*/
  222. static const tmedia_producer_plugin_def_t plugin_video_dshow_producer_plugin_def_s = {
  223. &plugin_video_dshow_producer_def_s,
  224. tmedia_video,
  225. "Microsoft DirectShow producer",
  226. plugin_video_dshow_producer_set,
  227. plugin_video_dshow_producer_prepare,
  228. plugin_video_dshow_producer_start,
  229. plugin_video_dshow_producer_pause,
  230. plugin_video_dshow_producer_stop
  231. };
  232. const tmedia_producer_plugin_def_t *plugin_video_dshow_producer_plugin_def_t = &plugin_video_dshow_producer_plugin_def_s;