plugin_screencast_dshow_producer.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (C) 2014 Mamadou DIOP
  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_screencast_dshow_producer_t*)(self))
  25. typedef struct plugin_screencast_dshow_producer_s {
  26. TMEDIA_DECLARE_PRODUCER;
  27. DSGrabber* grabber;
  28. INT64 previewHwnd;
  29. tsk_bool_t started;
  30. tsk_bool_t mute;
  31. tsk_bool_t create_on_ui_thread;
  32. }
  33. plugin_screencast_dshow_producer_t;
  34. // Producer callback (From DirectShow Grabber to our plugin)
  35. static int plugin_video_dshow_plugin_cb(const void* callback_data, const void* buffer, tsk_size_t size)
  36. {
  37. const plugin_screencast_dshow_producer_t* producer = (const plugin_screencast_dshow_producer_t*)callback_data;
  38. if (producer && TMEDIA_PRODUCER(producer)->enc_cb.callback) {
  39. TMEDIA_PRODUCER(producer)->enc_cb.callback(TMEDIA_PRODUCER(producer)->enc_cb.callback_data, buffer, size);
  40. }
  41. return 0;
  42. }
  43. /* ============ Media Producer Interface ================= */
  44. static int plugin_screencast_dshow_producer_set(tmedia_producer_t *self, const tmedia_param_t* param)
  45. {
  46. int ret = 0;
  47. plugin_screencast_dshow_producer_t* producer = (plugin_screencast_dshow_producer_t*)self;
  48. if(!producer || !param) {
  49. TSK_DEBUG_ERROR("Invalid parameter");
  50. return -1;
  51. }
  52. if(param->value_type == tmedia_pvt_int64) {
  53. if(tsk_striequals(param->key, "local-hwnd")) {
  54. DSPRODUCER(producer)->previewHwnd = (INT64)*((int64_t*)param->value);
  55. if(DSPRODUCER(producer)->grabber && DSPRODUCER(self)->grabber->preview) {
  56. DSPRODUCER(producer)->grabber->preview->attach(DSPRODUCER(producer)->previewHwnd);
  57. }
  58. }
  59. }
  60. else if(param->value_type == tmedia_pvt_int32) {
  61. if(tsk_striequals(param->key, "mute")) {
  62. producer->mute = (TSK_TO_INT32((uint8_t*)param->value) != 0);
  63. if(producer->started) {
  64. if(producer->mute) {
  65. producer->grabber->pause();
  66. }
  67. else {
  68. producer->grabber->start();
  69. }
  70. }
  71. }
  72. else if(tsk_striequals(param->key, "create-on-current-thead")) {
  73. producer->create_on_ui_thread = *((int32_t*)param->value) ? tsk_false : tsk_true;
  74. }
  75. else if(tsk_striequals(param->key, "plugin-firefox")) {
  76. TSK_DEBUG_INFO("'plugin-firefox' ignored for screencast");
  77. }
  78. }
  79. return ret;
  80. }
  81. static int plugin_screencast_dshow_producer_prepare(tmedia_producer_t* self, const tmedia_codec_t* codec)
  82. {
  83. plugin_screencast_dshow_producer_t* producer = (plugin_screencast_dshow_producer_t*)self;
  84. if(!producer || !codec && codec->plugin) {
  85. TSK_DEBUG_ERROR("Invalid parameter");
  86. return -1;
  87. }
  88. TMEDIA_PRODUCER(producer)->video.fps = TMEDIA_CODEC_VIDEO(codec)->out.fps;
  89. TMEDIA_PRODUCER(producer)->video.width = TMEDIA_CODEC_VIDEO(codec)->out.width;
  90. TMEDIA_PRODUCER(producer)->video.height = TMEDIA_CODEC_VIDEO(codec)->out.height;
  91. return 0;
  92. }
  93. static int plugin_screencast_dshow_producer_start(tmedia_producer_t* self)
  94. {
  95. plugin_screencast_dshow_producer_t* producer = (plugin_screencast_dshow_producer_t*)self;
  96. HRESULT hr = S_OK;
  97. if (!producer) {
  98. TSK_DEBUG_ERROR("Invalid parameter");
  99. return -1;
  100. }
  101. if (producer->started) {
  102. return 0;
  103. }
  104. // create grabber on ALWAYS current thread
  105. if (!producer->grabber) {
  106. static BOOL __isDisplayFalse = FALSE;
  107. static BOOL __isScreenCastTrue = TRUE;
  108. if(producer->create_on_ui_thread) {
  109. createOnUIThead(reinterpret_cast<HWND>((void*)DSPRODUCER(producer)->previewHwnd), (void**)&producer->grabber, __isDisplayFalse, __isScreenCastTrue);
  110. }
  111. else {
  112. createOnCurrentThead(reinterpret_cast<HWND>((void*)DSPRODUCER(producer)->previewHwnd), (void**)&producer->grabber, __isDisplayFalse, __isScreenCastTrue);
  113. }
  114. if (!producer->grabber) {
  115. TSK_DEBUG_ERROR("Failed to create grabber");
  116. return -2;
  117. }
  118. }
  119. // set parameters
  120. producer->grabber->setCaptureParameters((int)TMEDIA_PRODUCER(producer)->video.width, (int)TMEDIA_PRODUCER(producer)->video.height, TMEDIA_PRODUCER(producer)->video.fps);
  121. // set callback function
  122. producer->grabber->setCallback(plugin_video_dshow_plugin_cb, producer);
  123. // attach preview
  124. if (producer->grabber->preview) {
  125. if (producer->previewHwnd) {
  126. producer->grabber->preview->attach(producer->previewHwnd);
  127. }
  128. producer->grabber->preview->setSize((int)TMEDIA_PRODUCER(producer)->video.width, (int)TMEDIA_PRODUCER(producer)->video.height);
  129. }
  130. // start grabber
  131. if (!producer->mute) {
  132. producer->grabber->start();
  133. }
  134. producer->started = tsk_true;
  135. return 0;
  136. }
  137. static int plugin_screencast_dshow_producer_pause(tmedia_producer_t* self)
  138. {
  139. plugin_screencast_dshow_producer_t* producer = (plugin_screencast_dshow_producer_t*)self;
  140. if(!producer) {
  141. TSK_DEBUG_ERROR("Invalid parameter");
  142. return -1;
  143. }
  144. if(!producer->grabber) {
  145. TSK_DEBUG_ERROR("Invalid internal grabber");
  146. return -2;
  147. }
  148. producer->grabber->pause();
  149. return 0;
  150. }
  151. static int plugin_screencast_dshow_producer_stop(tmedia_producer_t* self)
  152. {
  153. plugin_screencast_dshow_producer_t* producer = (plugin_screencast_dshow_producer_t*)self;
  154. if(!self) {
  155. TSK_DEBUG_ERROR("Invalid parameter");
  156. return -1;
  157. }
  158. if(!producer->started) {
  159. return 0;
  160. }
  161. if(!producer->grabber) {
  162. TSK_DEBUG_ERROR("Invalid internal grabber");
  163. return -2;
  164. }
  165. producer->grabber->stop();
  166. producer->started = tsk_false;
  167. return 0;
  168. }
  169. //
  170. // WaveAPI producer object definition
  171. //
  172. /* constructor */
  173. static tsk_object_t* plugin_screencast_dshow_producer_ctor(tsk_object_t * self, va_list * app)
  174. {
  175. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  176. plugin_screencast_dshow_producer_t *producer = (plugin_screencast_dshow_producer_t *)self;
  177. if(producer) {
  178. /* init base */
  179. tmedia_producer_init(TMEDIA_PRODUCER(producer));
  180. TMEDIA_PRODUCER(producer)->video.chroma = tmedia_chroma_bgr24; // RGB24 on x86 (little endians) stored as BGR24
  181. /* init self with default values*/
  182. producer->create_on_ui_thread = tsk_true;
  183. TMEDIA_PRODUCER(producer)->video.fps = 15;
  184. TMEDIA_PRODUCER(producer)->video.width = 352;
  185. TMEDIA_PRODUCER(producer)->video.height = 288;
  186. }
  187. return self;
  188. }
  189. /* destructor */
  190. static tsk_object_t* plugin_screencast_dshow_producer_dtor(tsk_object_t * self)
  191. {
  192. plugin_screencast_dshow_producer_t *producer = (plugin_screencast_dshow_producer_t *)self;
  193. if(producer) {
  194. /* stop */
  195. if(producer->started) {
  196. plugin_screencast_dshow_producer_stop((tmedia_producer_t*)self);
  197. }
  198. /* for safety */
  199. if(producer->grabber) {
  200. producer->grabber->setCallback(tsk_null, tsk_null);
  201. }
  202. /* deinit base */
  203. tmedia_producer_deinit(TMEDIA_PRODUCER(producer));
  204. /* deinit self */
  205. SAFE_DELETE_PTR(producer->grabber);
  206. }
  207. return self;
  208. }
  209. /* object definition */
  210. static const tsk_object_def_t plugin_screencast_dshow_producer_def_s = {
  211. sizeof(plugin_screencast_dshow_producer_t),
  212. plugin_screencast_dshow_producer_ctor,
  213. plugin_screencast_dshow_producer_dtor,
  214. tsk_null,
  215. };
  216. /* plugin definition*/
  217. static const tmedia_producer_plugin_def_t plugin_screencast_dshow_producer_plugin_def_s = {
  218. &plugin_screencast_dshow_producer_def_s,
  219. tmedia_bfcp_video,
  220. "Microsoft DirectShow producer (ScrenCast)",
  221. plugin_screencast_dshow_producer_set,
  222. plugin_screencast_dshow_producer_prepare,
  223. plugin_screencast_dshow_producer_start,
  224. plugin_screencast_dshow_producer_pause,
  225. plugin_screencast_dshow_producer_stop
  226. };
  227. const tmedia_producer_plugin_def_t *plugin_screencast_dshow_producer_plugin_def_t = &plugin_screencast_dshow_producer_plugin_def_s;