plugin_video_dshow_consumer.cxx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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/DSDisplay.h"
  19. #include "internals/DSUtils.h"
  20. #include "tinymedia/tmedia_consumer.h"
  21. #include "tsk_safeobj.h"
  22. #include "tsk_string.h"
  23. #include "tsk_debug.h"
  24. #define DSCONSUMER(self) ((plugin_video_dshow_consumer_t*)(self))
  25. // Whether to use Direct3D device for direct rendering or DirectShow graph and custom source
  26. // Using DirectShow (DS) introduce delay when the input fps is different than the one in the custom src.
  27. // It's very hard to have someting accurate when using DS because the input FPS change depending on the congestion control. D3D is the best choice as frames are displayed as they arrive
  28. #if !defined(PLUGIN_DS_CV_USE_D3D9) && !defined(_WIN32_WCE)
  29. # define PLUGIN_DS_CV_USE_D3D9 1
  30. #endif
  31. /******* ********/
  32. #if PLUGIN_DS_CV_USE_D3D9
  33. #include <d3d9.h>
  34. #include <dxva2api.h>
  35. #ifdef _MSC_VER
  36. #pragma comment(lib, "d3d9")
  37. #endif
  38. const DWORD NUM_BACK_BUFFERS = 2;
  39. #undef SafeRelease
  40. #define SafeRelease(ppT) \
  41. { \
  42. if (*ppT) \
  43. { \
  44. (*ppT)->Release(); \
  45. *ppT = NULL; \
  46. } \
  47. }
  48. #undef CHECK_HR
  49. // In CHECK_HR(x) When (x) is a function it will be executed twice when used in "TSK_DEBUG_ERROR(x)" and "If(x)"
  50. #define CHECK_HR(x) { HRESULT __hr__ = (x); if (FAILED(__hr__)) { TSK_DEBUG_ERROR("Operation Failed (%08x)", __hr__); goto bail; } }
  51. typedef struct _DSRatio {
  52. DWORD Numerator;
  53. DWORD Denominator;
  54. } DSRatio;
  55. static HRESULT CreateDeviceD3D9(
  56. HWND hWnd,
  57. IDirect3DDevice9** ppDevice,
  58. IDirect3D9 **ppD3D,
  59. D3DPRESENT_PARAMETERS &d3dpp
  60. );
  61. static HRESULT TestCooperativeLevel(
  62. struct plugin_video_dshow_consumer_s *pSelf
  63. );
  64. static HRESULT CreateSwapChain(
  65. HWND hWnd,
  66. UINT32 nFrameWidth,
  67. UINT32 nFrameHeight,
  68. IDirect3DDevice9* pDevice,
  69. IDirect3DSwapChain9 **ppSwapChain);
  70. static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  71. static inline HWND Window(struct plugin_video_dshow_consumer_s *pSelf);
  72. static inline LONG Width(const RECT& r);
  73. static inline LONG Height(const RECT& r);
  74. static inline RECT CorrectAspectRatio(const RECT& src, const DSRatio& srcPAR);
  75. static inline RECT LetterBoxRect(const RECT& rcSrc, const RECT& rcDst);
  76. static inline HRESULT UpdateDestinationRect(struct plugin_video_dshow_consumer_s *pSelf, BOOL bForce = FALSE);
  77. static HRESULT ResetDevice(struct plugin_video_dshow_consumer_s *pSelf, BOOL bUpdateDestinationRect = FALSE);
  78. static HRESULT SetFullscreen(struct plugin_video_dshow_consumer_s *pSelf, BOOL bFullScreen);
  79. static HWND CreateFullScreenWindow(struct plugin_video_dshow_consumer_s *pSelf);
  80. static HRESULT HookWindow(struct plugin_video_dshow_consumer_s *pSelf, HWND hWnd);
  81. static HRESULT UnhookWindow(struct plugin_video_dshow_consumer_s *pSelf);
  82. typedef struct plugin_video_dshow_consumer_s {
  83. TMEDIA_DECLARE_CONSUMER;
  84. BOOL bStarted, bPrepared, bPaused, bFullScreen, bWindowHooked;
  85. BOOL bPluginFireFox, bPluginWebRTC4All;
  86. HWND hWindow;
  87. WNDPROC wndProc;
  88. HWND hWindowFullScreen;
  89. RECT rcWindow;
  90. RECT rcDest;
  91. DSRatio pixelAR;
  92. UINT32 nNegWidth;
  93. UINT32 nNegHeight;
  94. UINT32 nNegFps;
  95. D3DLOCKED_RECT rcLock;
  96. IDirect3DDevice9* pDevice;
  97. IDirect3D9 *pD3D;
  98. IDirect3DSwapChain9 *pSwapChain;
  99. D3DPRESENT_PARAMETERS d3dpp;
  100. TSK_DECLARE_SAFEOBJ;
  101. }
  102. plugin_video_dshow_consumer_t;
  103. static int _plugin_video_dshow_consumer_unprepare(plugin_video_dshow_consumer_t* pSelf);
  104. /* ============ Media Consumer Interface ================= */
  105. static int plugin_video_dshow_consumer_set(tmedia_consumer_t *self, const tmedia_param_t* param)
  106. {
  107. int ret = 0;
  108. HRESULT hr = S_OK;
  109. plugin_video_dshow_consumer_t* pSelf = (plugin_video_dshow_consumer_t*)self;
  110. if(!self || !param) {
  111. TSK_DEBUG_ERROR("Invalid parameter");
  112. CHECK_HR(hr = E_POINTER);
  113. }
  114. if(param->value_type == tmedia_pvt_int64) {
  115. if(tsk_striequals(param->key, "remote-hwnd")) {
  116. HWND hWnd = reinterpret_cast<HWND>((INT64)*((int64_t*)param->value));
  117. if(hWnd != pSelf->hWindow) {
  118. tsk_safeobj_lock(pSelf); // block consumer thread
  119. pSelf->hWindow = hWnd;
  120. if(pSelf->bPrepared) {
  121. hr = ResetDevice(pSelf);
  122. }
  123. tsk_safeobj_unlock(pSelf); // unblock consumer thread
  124. }
  125. }
  126. }
  127. else if(param->value_type == tmedia_pvt_int32) {
  128. if(tsk_striequals(param->key, "fullscreen")) {
  129. BOOL bFullScreen = !!*((int32_t*)param->value);
  130. TSK_DEBUG_INFO("[MF video consumer] Full Screen = %d", bFullScreen);
  131. CHECK_HR(hr = SetFullscreen(pSelf, bFullScreen));
  132. }
  133. else if(tsk_striequals(param->key, "create-on-current-thead")) {
  134. // DSCONSUMER(self)->create_on_ui_thread = *((int32_t*)param->value) ? tsk_false : tsk_true;
  135. }
  136. else if(tsk_striequals(param->key, "plugin-firefox")) {
  137. pSelf->bPluginFireFox = (*((int32_t*)param->value) != 0);
  138. }
  139. else if(tsk_striequals(param->key, "plugin-webrtc4all")) {
  140. pSelf->bPluginWebRTC4All = (*((int32_t*)param->value) != 0);
  141. }
  142. }
  143. CHECK_HR(hr);
  144. bail:
  145. return SUCCEEDED(hr) ? 0 : -1;
  146. }
  147. static int plugin_video_dshow_consumer_prepare(tmedia_consumer_t* self, const tmedia_codec_t* codec)
  148. {
  149. plugin_video_dshow_consumer_t* pSelf = (plugin_video_dshow_consumer_t*)self;
  150. if(!pSelf || !codec && codec->plugin) {
  151. TSK_DEBUG_ERROR("Invalid parameter");
  152. return -1;
  153. }
  154. if(pSelf->bPrepared) {
  155. TSK_DEBUG_WARN("D3D9 video consumer already prepared");
  156. return -1;
  157. }
  158. HRESULT hr = S_OK;
  159. HWND hWnd = Window(pSelf);
  160. TMEDIA_CONSUMER(pSelf)->video.fps = TMEDIA_CODEC_VIDEO(codec)->in.fps;
  161. TMEDIA_CONSUMER(pSelf)->video.in.width = TMEDIA_CODEC_VIDEO(codec)->in.width;
  162. TMEDIA_CONSUMER(pSelf)->video.in.height = TMEDIA_CODEC_VIDEO(codec)->in.height;
  163. if(!TMEDIA_CONSUMER(pSelf)->video.display.width) {
  164. TMEDIA_CONSUMER(pSelf)->video.display.width = TMEDIA_CONSUMER(pSelf)->video.in.width;
  165. }
  166. if(!TMEDIA_CONSUMER(pSelf)->video.display.height) {
  167. TMEDIA_CONSUMER(pSelf)->video.display.height = TMEDIA_CONSUMER(pSelf)->video.in.height;
  168. }
  169. pSelf->nNegFps = (UINT32)TMEDIA_CONSUMER(pSelf)->video.fps;
  170. pSelf->nNegWidth = (UINT32)TMEDIA_CONSUMER(pSelf)->video.display.width;
  171. pSelf->nNegHeight = (UINT32)TMEDIA_CONSUMER(pSelf)->video.display.height;
  172. TSK_DEBUG_INFO("D3D9 video consumer: fps=%d, width=%d, height=%d",
  173. pSelf->nNegFps,
  174. pSelf->nNegWidth,
  175. pSelf->nNegHeight);
  176. TMEDIA_CONSUMER(pSelf)->video.display.chroma = tmedia_chroma_rgb32;
  177. TMEDIA_CONSUMER(pSelf)->decoder.codec_id = tmedia_codec_id_none; // means accept RAW fames
  178. // The window handle is not created until the call is connect (incoming only) - At least on Internet Explorer 10
  179. if(hWnd && !pSelf->bPluginWebRTC4All) {
  180. CHECK_HR(hr = CreateDeviceD3D9(hWnd, &pSelf->pDevice, &pSelf->pD3D, pSelf->d3dpp));
  181. CHECK_HR(hr = CreateSwapChain(hWnd, pSelf->nNegWidth, pSelf->nNegHeight, pSelf->pDevice, &pSelf->pSwapChain));
  182. }
  183. else {
  184. if(hWnd && pSelf->bPluginWebRTC4All) {
  185. TSK_DEBUG_INFO("[MF consumer] HWND is defined but we detected webrtc4all...delaying D3D9 device creating until session get connected");
  186. }
  187. else {
  188. TSK_DEBUG_WARN("Delaying D3D9 device creation because HWND is not defined yet");
  189. }
  190. }
  191. bail:
  192. pSelf->bPrepared = SUCCEEDED(hr);
  193. return pSelf->bPrepared ? 0 : -1;
  194. }
  195. static int plugin_video_dshow_consumer_start(tmedia_consumer_t* self)
  196. {
  197. plugin_video_dshow_consumer_t* pSelf = (plugin_video_dshow_consumer_t*)self;
  198. if(!pSelf) {
  199. TSK_DEBUG_ERROR("Invalid parameter");
  200. return -1;
  201. }
  202. if(pSelf->bStarted) {
  203. TSK_DEBUG_INFO("D3D9 video consumer already started");
  204. return 0;
  205. }
  206. if(!pSelf->bPrepared) {
  207. TSK_DEBUG_ERROR("D3D9 video consumer not prepared");
  208. return -1;
  209. }
  210. HRESULT hr = S_OK;
  211. pSelf->bPaused = false;
  212. pSelf->bStarted = true;
  213. return SUCCEEDED(hr) ? 0 : -1;
  214. }
  215. static int plugin_video_dshow_consumer_consume(tmedia_consumer_t* self, const void* buffer, tsk_size_t size, const tsk_object_t* proto_hdr)
  216. {
  217. plugin_video_dshow_consumer_t* pSelf = (plugin_video_dshow_consumer_t*)self;
  218. HRESULT hr = S_OK;
  219. HWND hWnd = Window(pSelf);
  220. IDirect3DSurface9 *pSurf = NULL;
  221. IDirect3DSurface9 *pBB = NULL;
  222. if(!pSelf) {
  223. TSK_DEBUG_ERROR("Invalid parameter");
  224. return -1; // because of the mutex lock do it here
  225. }
  226. tsk_safeobj_lock(pSelf);
  227. if(!buffer || !size) {
  228. TSK_DEBUG_ERROR("Invalid parameter");
  229. CHECK_HR(hr = E_INVALIDARG);
  230. }
  231. if(!pSelf->bStarted) {
  232. TSK_DEBUG_INFO("D3D9 video consumer not started");
  233. CHECK_HR(hr = E_FAIL);
  234. }
  235. if(!hWnd) {
  236. TSK_DEBUG_INFO("Do not draw frame because HWND not set");
  237. goto bail; // not an error as the application can decide to set the HWND at any time
  238. }
  239. if (!pSelf->bWindowHooked) {
  240. // Do not hook "hWnd" as it could be the fullscreen handle which is always hooked.
  241. CHECK_HR(hr = HookWindow(pSelf, pSelf->hWindow));
  242. }
  243. if(!pSelf->pDevice || !pSelf->pD3D || !pSelf->pSwapChain) {
  244. if(pSelf->pDevice || pSelf->pD3D || pSelf->pSwapChain) {
  245. CHECK_HR(hr = E_POINTER); // They must be "all null" or "all valid"
  246. }
  247. if(hWnd) {
  248. // means HWND was not set but defined now
  249. pSelf->nNegWidth = (UINT32)TMEDIA_CONSUMER(pSelf)->video.in.width;
  250. pSelf->nNegHeight = (UINT32)TMEDIA_CONSUMER(pSelf)->video.in.height;
  251. CHECK_HR(hr = CreateDeviceD3D9(hWnd, &pSelf->pDevice, &pSelf->pD3D, pSelf->d3dpp));
  252. CHECK_HR(hr = CreateSwapChain(hWnd, pSelf->nNegWidth, pSelf->nNegHeight, pSelf->pDevice, &pSelf->pSwapChain));
  253. }
  254. }
  255. if(pSelf->nNegWidth != TMEDIA_CONSUMER(pSelf)->video.in.width || pSelf->nNegHeight != TMEDIA_CONSUMER(pSelf)->video.in.height) {
  256. TSK_DEBUG_INFO("Negotiated and input video sizes are different:%d#%d or %d#%d",
  257. pSelf->nNegWidth, TMEDIA_CONSUMER(pSelf)->video.in.width,
  258. pSelf->nNegHeight, TMEDIA_CONSUMER(pSelf)->video.in.height);
  259. // Update media type
  260. SafeRelease(&pSelf->pSwapChain);
  261. CHECK_HR(hr = CreateSwapChain(hWnd, (UINT32)TMEDIA_CONSUMER(pSelf)->video.in.width, (UINT32)TMEDIA_CONSUMER(pSelf)->video.in.height, pSelf->pDevice, &pSelf->pSwapChain));
  262. pSelf->nNegWidth = (UINT32)TMEDIA_CONSUMER(pSelf)->video.in.width;
  263. pSelf->nNegHeight = (UINT32)TMEDIA_CONSUMER(pSelf)->video.in.height;
  264. // Update Destination will do noting if the window size haven't changed.
  265. // Force updating the destination rect if negotiated size change
  266. CHECK_HR(hr = UpdateDestinationRect(pSelf, TRUE/* Force */));
  267. }
  268. if(((pSelf->nNegWidth * pSelf->nNegHeight) << 2) != size) {
  269. TSK_DEBUG_ERROR("%u not valid as input size", size);
  270. CHECK_HR(hr = E_FAIL);
  271. }
  272. CHECK_HR(hr = TestCooperativeLevel(pSelf));
  273. CHECK_HR(hr = UpdateDestinationRect(pSelf));
  274. CHECK_HR(hr = pSelf->pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurf));
  275. CHECK_HR(hr = pSurf->LockRect(&pSelf->rcLock, NULL, D3DLOCK_NOSYSLOCK ));
  276. // Fast copy() using MMX, SSE, or SSE2
  277. // Only available on Vista or later: Use LoadLibrary() to get a pointer to the function
  278. /*hr = MFCopyImage(
  279. (BYTE*)pSelf->rcLock.pBits,
  280. pSelf->rcLock.Pitch,
  281. (BYTE*)buffer,
  282. (pSelf->nNegWidth << 2),
  283. (pSelf->nNegWidth << 2),
  284. pSelf->nNegHeight
  285. );*/
  286. if(pSelf->rcLock.Pitch == (pSelf->nNegWidth << 2)) {
  287. memcpy(pSelf->rcLock.pBits, buffer, size);
  288. }
  289. else {
  290. const BYTE* pSrcPtr = (const BYTE*)buffer;
  291. BYTE* pDstPtr = (BYTE*)pSelf->rcLock.pBits;
  292. UINT32 nDstPitch = pSelf->rcLock.Pitch;
  293. UINT32 nSrcPitch = (pSelf->nNegWidth << 2);
  294. for(UINT32 i = 0; i < pSelf->nNegHeight; ++i) {
  295. memcpy(pDstPtr, pSrcPtr, nSrcPitch);
  296. pDstPtr += nDstPitch;
  297. pSrcPtr += nSrcPitch;
  298. }
  299. }
  300. if(FAILED(hr)) {
  301. // unlock() before leaving
  302. pSurf->UnlockRect();
  303. CHECK_HR(hr);
  304. }
  305. CHECK_HR(hr = pSurf->UnlockRect());
  306. // Color fill the back buffer
  307. CHECK_HR(hr = pSelf->pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBB));
  308. CHECK_HR(hr = pSelf->pDevice->ColorFill(pBB, NULL, D3DCOLOR_XRGB(0xFF, 0xFF, 0xFF)));
  309. // Resize keeping aspect ratio and Blit the frame (required)
  310. hr = pSelf->pDevice->StretchRect(
  311. pSurf,
  312. NULL,
  313. pBB,
  314. &pSelf->rcDest/*NULL*/,
  315. D3DTEXF_LINEAR
  316. ); // could fail when display is being resized
  317. if(SUCCEEDED(hr)) {
  318. // Present the frame
  319. CHECK_HR(hr = pSelf->pDevice->Present(NULL, NULL, NULL, NULL));
  320. }
  321. else {
  322. TSK_DEBUG_INFO("StretchRect returned ...%x", hr);
  323. }
  324. bail:
  325. SafeRelease(&pSurf);
  326. SafeRelease(&pBB);
  327. tsk_safeobj_unlock(pSelf);
  328. return SUCCEEDED(hr) ? 0 : -1;
  329. }
  330. static int plugin_video_dshow_consumer_pause(tmedia_consumer_t* self)
  331. {
  332. plugin_video_dshow_consumer_t* pSelf = (plugin_video_dshow_consumer_t*)self;
  333. if(!pSelf) {
  334. TSK_DEBUG_ERROR("Invalid parameter");
  335. return -1;
  336. }
  337. if(!pSelf->bStarted) {
  338. TSK_DEBUG_INFO("MF video producer not started");
  339. return 0;
  340. }
  341. HRESULT hr = S_OK;
  342. pSelf->bPaused = true;
  343. return SUCCEEDED(hr) ? 0 : -1;
  344. }
  345. static int plugin_video_dshow_consumer_stop(tmedia_consumer_t* self)
  346. {
  347. plugin_video_dshow_consumer_t* pSelf = (plugin_video_dshow_consumer_t*)self;
  348. if(!pSelf) {
  349. TSK_DEBUG_ERROR("Invalid parameter");
  350. return -1;
  351. }
  352. HRESULT hr = S_OK;
  353. pSelf->bStarted = false;
  354. pSelf->bPaused = false;
  355. if(pSelf->hWindowFullScreen) {
  356. ::InvalidateRect(pSelf->hWindowFullScreen, NULL, FALSE);
  357. ::ShowWindow(pSelf->hWindowFullScreen, SW_HIDE);
  358. }
  359. // next start() will be called after prepare()
  360. return _plugin_video_dshow_consumer_unprepare(pSelf);
  361. }
  362. static int _plugin_video_dshow_consumer_unprepare(plugin_video_dshow_consumer_t* pSelf)
  363. {
  364. if(!pSelf) {
  365. TSK_DEBUG_ERROR("Invalid parameter");
  366. return -1;
  367. }
  368. if(pSelf->bStarted) {
  369. // plugin_win_mf_producer_video_stop(TMEDIA_PRODUCER(pSelf));
  370. TSK_DEBUG_ERROR("Consumer must be stopped before calling unprepare");
  371. return -1;
  372. }
  373. UnhookWindow(pSelf);
  374. SafeRelease(&pSelf->pDevice);
  375. SafeRelease(&pSelf->pD3D);
  376. SafeRelease(&pSelf->pSwapChain);
  377. pSelf->bPrepared = false;
  378. return 0;
  379. }
  380. //
  381. // D3D9 video consumer object definition
  382. //
  383. /* constructor */
  384. static tsk_object_t* plugin_video_dshow_consumer_ctor(tsk_object_t * self, va_list * app)
  385. {
  386. plugin_video_dshow_consumer_t *pSelf = (plugin_video_dshow_consumer_t *)self;
  387. if(pSelf) {
  388. /* init base */
  389. tmedia_consumer_init(TMEDIA_CONSUMER(pSelf));
  390. TMEDIA_CONSUMER(pSelf)->video.display.chroma = tmedia_chroma_rgb32;
  391. TMEDIA_CONSUMER(pSelf)->decoder.codec_id = tmedia_codec_id_none; // means accept RAW fames
  392. /* init self */
  393. tsk_safeobj_init(pSelf);
  394. TMEDIA_CONSUMER(pSelf)->video.fps = 15;
  395. TMEDIA_CONSUMER(pSelf)->video.display.width = 0; // use codec value
  396. TMEDIA_CONSUMER(pSelf)->video.display.height = 0; // use codec value
  397. TMEDIA_CONSUMER(pSelf)->video.display.auto_resize = tsk_true;
  398. pSelf->pixelAR.Denominator = pSelf->pixelAR.Numerator = 1;
  399. }
  400. return self;
  401. }
  402. /* destructor */
  403. static tsk_object_t* plugin_video_dshow_consumer_dtor(tsk_object_t * self)
  404. {
  405. plugin_video_dshow_consumer_t *pSelf = (plugin_video_dshow_consumer_t *)self;
  406. if (pSelf) {
  407. /* stop */
  408. if (pSelf->bStarted) {
  409. plugin_video_dshow_consumer_stop(TMEDIA_CONSUMER(pSelf));
  410. }
  411. /* deinit base */
  412. tmedia_consumer_deinit(TMEDIA_CONSUMER(pSelf));
  413. /* deinit self */
  414. _plugin_video_dshow_consumer_unprepare(pSelf);
  415. tsk_safeobj_deinit(pSelf);
  416. }
  417. return self;
  418. }
  419. /* object definition */
  420. static const tsk_object_def_t plugin_video_dshow_consumer_def_s = {
  421. sizeof(plugin_video_dshow_consumer_t),
  422. plugin_video_dshow_consumer_ctor,
  423. plugin_video_dshow_consumer_dtor,
  424. tsk_null,
  425. };
  426. /* plugin definition*/
  427. static const tmedia_consumer_plugin_def_t plugin_video_dshow_consumer_plugin_def_s = {
  428. &plugin_video_dshow_consumer_def_s,
  429. tmedia_video,
  430. "Microsoft DirectShow consumer (D3D9)",
  431. plugin_video_dshow_consumer_set,
  432. plugin_video_dshow_consumer_prepare,
  433. plugin_video_dshow_consumer_start,
  434. plugin_video_dshow_consumer_consume,
  435. plugin_video_dshow_consumer_pause,
  436. plugin_video_dshow_consumer_stop
  437. };
  438. const tmedia_consumer_plugin_def_t *plugin_video_dshow_consumer_plugin_def_t = &plugin_video_dshow_consumer_plugin_def_s;
  439. // Helper functions
  440. static HRESULT CreateDeviceD3D9(
  441. HWND hWnd,
  442. IDirect3DDevice9** ppDevice,
  443. IDirect3D9 **ppD3D,
  444. D3DPRESENT_PARAMETERS &d3dpp
  445. )
  446. {
  447. HRESULT hr = S_OK;
  448. D3DDISPLAYMODE mode = { 0 };
  449. D3DPRESENT_PARAMETERS pp = {0};
  450. if(!ppDevice || *ppDevice || !ppD3D || *ppD3D) {
  451. CHECK_HR(hr = E_POINTER);
  452. }
  453. if(!(*ppD3D = Direct3DCreate9(D3D_SDK_VERSION))) {
  454. CHECK_HR(hr = E_OUTOFMEMORY);
  455. }
  456. CHECK_HR(hr = (*ppD3D)->GetAdapterDisplayMode(
  457. D3DADAPTER_DEFAULT,
  458. &mode
  459. ));
  460. CHECK_HR(hr = (*ppD3D)->CheckDeviceType(
  461. D3DADAPTER_DEFAULT,
  462. D3DDEVTYPE_HAL,
  463. mode.Format,
  464. D3DFMT_X8R8G8B8,
  465. TRUE // windowed
  466. ));
  467. pp.BackBufferFormat = D3DFMT_X8R8G8B8;
  468. pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  469. pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  470. pp.Windowed = TRUE;
  471. pp.hDeviceWindow = hWnd;
  472. CHECK_HR(hr = (*ppD3D)->CreateDevice(
  473. D3DADAPTER_DEFAULT,
  474. D3DDEVTYPE_HAL,
  475. hWnd,
  476. D3DCREATE_HARDWARE_VERTEXPROCESSING,
  477. &pp,
  478. ppDevice
  479. ));
  480. d3dpp = pp;
  481. bail:
  482. if(FAILED(hr)) {
  483. SafeRelease(ppD3D);
  484. SafeRelease(ppDevice);
  485. }
  486. return hr;
  487. }
  488. static HRESULT TestCooperativeLevel(
  489. struct plugin_video_dshow_consumer_s *pSelf
  490. )
  491. {
  492. HRESULT hr = S_OK;
  493. if (!pSelf || !pSelf->pDevice) {
  494. CHECK_HR(hr = E_POINTER);
  495. }
  496. switch((hr = pSelf->pDevice->TestCooperativeLevel())) {
  497. case D3D_OK: {
  498. break;
  499. }
  500. case D3DERR_DEVICELOST: {
  501. hr = S_OK;
  502. break;
  503. }
  504. case D3DERR_DEVICENOTRESET: {
  505. hr = ResetDevice(pSelf, TRUE);
  506. break;
  507. }
  508. default: {
  509. break;
  510. }
  511. }
  512. CHECK_HR(hr);
  513. bail:
  514. return hr;
  515. }
  516. static HRESULT CreateSwapChain(
  517. HWND hWnd,
  518. UINT32 nFrameWidth,
  519. UINT32 nFrameHeight,
  520. IDirect3DDevice9* pDevice,
  521. IDirect3DSwapChain9 **ppSwapChain
  522. )
  523. {
  524. HRESULT hr = S_OK;
  525. D3DPRESENT_PARAMETERS pp = { 0 };
  526. if(!pDevice || !ppSwapChain || *ppSwapChain) {
  527. CHECK_HR(hr = E_POINTER);
  528. }
  529. pp.BackBufferWidth = nFrameWidth;
  530. pp.BackBufferHeight = nFrameHeight;
  531. pp.Windowed = TRUE;
  532. pp.SwapEffect = D3DSWAPEFFECT_FLIP;
  533. pp.hDeviceWindow = hWnd;
  534. pp.BackBufferFormat = D3DFMT_X8R8G8B8;
  535. pp.Flags =
  536. D3DPRESENTFLAG_VIDEO | D3DPRESENTFLAG_DEVICECLIP |
  537. D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  538. pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  539. pp.BackBufferCount = NUM_BACK_BUFFERS;
  540. CHECK_HR(hr = pDevice->CreateAdditionalSwapChain(&pp, ppSwapChain));
  541. bail:
  542. return hr;
  543. }
  544. static inline HWND Window(struct plugin_video_dshow_consumer_s *pSelf)
  545. {
  546. return pSelf ? (pSelf->bFullScreen ? pSelf->hWindowFullScreen : pSelf->hWindow) : NULL;
  547. }
  548. static inline LONG Width(const RECT& r)
  549. {
  550. return r.right - r.left;
  551. }
  552. static inline LONG Height(const RECT& r)
  553. {
  554. return r.bottom - r.top;
  555. }
  556. //-----------------------------------------------------------------------------
  557. // CorrectAspectRatio
  558. //
  559. // Converts a rectangle from the source's pixel aspect ratio (PAR) to 1:1 PAR.
  560. // Returns the corrected rectangle.
  561. //
  562. // For example, a 720 x 486 rect with a PAR of 9:10, when converted to 1x1 PAR,
  563. // is stretched to 720 x 540.
  564. // Copyright (C) Microsoft
  565. //-----------------------------------------------------------------------------
  566. static inline RECT CorrectAspectRatio(const RECT& src, const DSRatio& srcPAR)
  567. {
  568. // Start with a rectangle the same size as src, but offset to the origin (0,0).
  569. RECT rc = {0, 0, src.right - src.left, src.bottom - src.top};
  570. if ((srcPAR.Numerator != 1) || (srcPAR.Denominator != 1)) {
  571. // Correct for the source's PAR.
  572. if (srcPAR.Numerator > srcPAR.Denominator) {
  573. // The source has "wide" pixels, so stretch the width.
  574. rc.right = MulDiv(rc.right, srcPAR.Numerator, srcPAR.Denominator);
  575. }
  576. else if (srcPAR.Numerator < srcPAR.Denominator) {
  577. // The source has "tall" pixels, so stretch the height.
  578. rc.bottom = MulDiv(rc.bottom, srcPAR.Denominator, srcPAR.Numerator);
  579. }
  580. // else: PAR is 1:1, which is a no-op.
  581. }
  582. return rc;
  583. }
  584. //-------------------------------------------------------------------
  585. // LetterBoxDstRect
  586. //
  587. // Takes a src rectangle and constructs the largest possible
  588. // destination rectangle within the specifed destination rectangle
  589. // such thatthe video maintains its current shape.
  590. //
  591. // This function assumes that pels are the same shape within both the
  592. // source and destination rectangles.
  593. // Copyright (C) Microsoft
  594. //-------------------------------------------------------------------
  595. static inline RECT LetterBoxRect(const RECT& rcSrc, const RECT& rcDst)
  596. {
  597. // figure out src/dest scale ratios
  598. int iSrcWidth = Width(rcSrc);
  599. int iSrcHeight = Height(rcSrc);
  600. int iDstWidth = Width(rcDst);
  601. int iDstHeight = Height(rcDst);
  602. int iDstLBWidth;
  603. int iDstLBHeight;
  604. if (MulDiv(iSrcWidth, iDstHeight, iSrcHeight) <= iDstWidth) {
  605. // Column letter boxing ("pillar box")
  606. iDstLBWidth = MulDiv(iDstHeight, iSrcWidth, iSrcHeight);
  607. iDstLBHeight = iDstHeight;
  608. }
  609. else {
  610. // Row letter boxing.
  611. iDstLBWidth = iDstWidth;
  612. iDstLBHeight = MulDiv(iDstWidth, iSrcHeight, iSrcWidth);
  613. }
  614. // Create a centered rectangle within the current destination rect
  615. RECT rc;
  616. LONG left = rcDst.left + ((iDstWidth - iDstLBWidth) >> 1);
  617. LONG top = rcDst.top + ((iDstHeight - iDstLBHeight) >> 1);
  618. SetRect(&rc, left, top, left + iDstLBWidth, top + iDstLBHeight);
  619. return rc;
  620. }
  621. static inline HRESULT UpdateDestinationRect(plugin_video_dshow_consumer_t *pSelf, BOOL bForce /*= FALSE*/)
  622. {
  623. HRESULT hr = S_OK;
  624. HWND hwnd = Window(pSelf);
  625. if(!pSelf) {
  626. CHECK_HR(hr = E_POINTER);
  627. }
  628. if(!hwnd) {
  629. CHECK_HR(hr = E_HANDLE);
  630. }
  631. RECT rcClient;
  632. GetClientRect(hwnd, &rcClient);
  633. // only update destination if window size changed
  634. if(bForce || (rcClient.bottom != pSelf->rcWindow.bottom || rcClient.left != pSelf->rcWindow.left || rcClient.right != pSelf->rcWindow.right || rcClient.top != pSelf->rcWindow.top)) {
  635. CHECK_HR(hr = ResetDevice(pSelf));
  636. pSelf->rcWindow = rcClient;
  637. #if 1
  638. RECT rcSrc = { 0, 0, pSelf->nNegWidth, pSelf->nNegHeight };
  639. rcSrc = CorrectAspectRatio(rcSrc, pSelf->pixelAR);
  640. pSelf->rcDest = LetterBoxRect(rcSrc, rcClient);
  641. #else
  642. long w = rcClient.right - rcClient.left;
  643. long h = rcClient.bottom - rcClient.top;
  644. float ratio = ((float)pSelf->nNegWidth/(float)pSelf->nNegHeight);
  645. // (w/h)=ratio =>
  646. // 1) h=w/ratio
  647. // and
  648. // 2) w=h*ratio
  649. pSelf->rcDest.right = (int)(w/ratio) > h ? (int)(h * ratio) : w;
  650. pSelf->rcDest.bottom = (int)(pSelf->rcDest.right/ratio) > h ? h : (int)(pSelf->rcDest.right/ratio);
  651. pSelf->rcDest.left = ((w - pSelf->rcDest.right) >> 1);
  652. pSelf->rcDest.top = ((h - pSelf->rcDest.bottom) >> 1);
  653. #endif
  654. //::InvalidateRect(hwnd, NULL, FALSE);
  655. }
  656. bail:
  657. return hr;
  658. }
  659. static HRESULT ResetDevice(plugin_video_dshow_consumer_t *pSelf, BOOL bUpdateDestinationRect /*= FALSE*/)
  660. {
  661. HRESULT hr = S_OK;
  662. tsk_safeobj_lock(pSelf);
  663. HWND hWnd = Window(pSelf);
  664. if (pSelf->pDevice) {
  665. D3DPRESENT_PARAMETERS d3dpp = pSelf->d3dpp;
  666. hr = pSelf->pDevice->Reset(&d3dpp);
  667. if (FAILED(hr)) {
  668. SafeRelease(&pSelf->pDevice);
  669. SafeRelease(&pSelf->pD3D);
  670. SafeRelease(&pSelf->pSwapChain);
  671. }
  672. }
  673. if (pSelf->pDevice == NULL && hWnd) {
  674. CHECK_HR(hr = CreateDeviceD3D9(hWnd, &pSelf->pDevice, &pSelf->pD3D, pSelf->d3dpp));
  675. CHECK_HR(hr = CreateSwapChain(hWnd, pSelf->nNegWidth, pSelf->nNegHeight, pSelf->pDevice, &pSelf->pSwapChain));
  676. }
  677. if(bUpdateDestinationRect) { // endless loop guard
  678. CHECK_HR(hr = UpdateDestinationRect(pSelf));
  679. }
  680. bail:
  681. tsk_safeobj_unlock(pSelf);
  682. return hr;
  683. }
  684. static HRESULT SetFullscreen(struct plugin_video_dshow_consumer_s *pSelf, BOOL bFullScreen)
  685. {
  686. HRESULT hr = S_OK;
  687. if(!pSelf) {
  688. CHECK_HR(hr = E_POINTER);
  689. }
  690. if(pSelf->bFullScreen != bFullScreen) {
  691. tsk_safeobj_lock(pSelf);
  692. if(bFullScreen) {
  693. HWND hWnd = CreateFullScreenWindow(pSelf);
  694. if(hWnd) {
  695. ::ShowWindow(hWnd, SW_SHOWDEFAULT);
  696. ::UpdateWindow(hWnd);
  697. }
  698. }
  699. else if(pSelf->hWindowFullScreen) {
  700. ::ShowWindow(pSelf->hWindowFullScreen, SW_HIDE);
  701. }
  702. pSelf->bFullScreen = bFullScreen;
  703. if(pSelf->bPrepared) {
  704. hr = ResetDevice(pSelf);
  705. }
  706. tsk_safeobj_unlock(pSelf);
  707. CHECK_HR(hr);
  708. }
  709. bail:
  710. return hr;
  711. }
  712. static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  713. {
  714. switch(uMsg) {
  715. case WM_CREATE:
  716. case WM_SIZE:
  717. case WM_MOVE: {
  718. struct plugin_video_dshow_consumer_s* pSelf = dynamic_cast<struct plugin_video_dshow_consumer_s*>((struct plugin_video_dshow_consumer_s*)GetPropA(hWnd, "Self"));
  719. if (pSelf) {
  720. }
  721. break;
  722. }
  723. #if 0
  724. case WM_PAINT: {
  725. PAINTSTRUCT ps;
  726. HDC hdc = BeginPaint(hWnd, &ps);
  727. ps.fErase = FALSE;
  728. RECT rc;
  729. GetBoundsRect(hdc, &rc, 0);
  730. FillRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
  731. EndPaint(hWnd, &ps);
  732. break;
  733. }
  734. #endif
  735. case WM_ERASEBKGND: {
  736. return TRUE; // avoid background erasing.
  737. }
  738. case WM_CHAR:
  739. case WM_KEYUP: {
  740. struct plugin_video_dshow_consumer_s* pSelf = dynamic_cast<struct plugin_video_dshow_consumer_s*>((struct plugin_video_dshow_consumer_s*)GetPropA(hWnd, "Self"));
  741. if (pSelf) {
  742. SetFullscreen(pSelf, FALSE);
  743. }
  744. break;
  745. }
  746. }
  747. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  748. }
  749. static HWND CreateFullScreenWindow(struct plugin_video_dshow_consumer_s *pSelf)
  750. {
  751. HRESULT hr = S_OK;
  752. if(!pSelf) {
  753. return NULL;
  754. }
  755. if(!pSelf->hWindowFullScreen) {
  756. WNDCLASS wc = {0};
  757. wc.lpfnWndProc = WndProc;
  758. wc.hInstance = GetModuleHandle(NULL);
  759. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  760. wc.lpszClassName = L"WindowClass";
  761. RegisterClass(&wc);
  762. pSelf->hWindowFullScreen = ::CreateWindowEx(
  763. NULL,
  764. wc.lpszClassName,
  765. L"Doubango's Video Consumer Fullscreen",
  766. WS_EX_TOPMOST | WS_POPUP,
  767. 0, 0,
  768. GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
  769. NULL,
  770. NULL,
  771. GetModuleHandle(NULL),
  772. NULL);
  773. SetPropA(pSelf->hWindowFullScreen, "Self", pSelf);
  774. }
  775. return pSelf->hWindowFullScreen;
  776. }
  777. static HRESULT HookWindow(plugin_video_dshow_consumer_t *pSelf, HWND hWnd)
  778. {
  779. HRESULT hr = S_OK;
  780. tsk_safeobj_lock(pSelf);
  781. CHECK_HR(hr = UnhookWindow(pSelf));
  782. if ((pSelf->hWindow = hWnd)) {
  783. pSelf->wndProc = (WNDPROC)SetWindowLongPtr(pSelf->hWindow, GWLP_WNDPROC, (LONG_PTR)WndProc);
  784. if (!pSelf->wndProc) {
  785. TSK_DEBUG_ERROR("HookWindowLongPtr() failed with errcode=%d", GetLastError());
  786. CHECK_HR(hr = E_FAIL);
  787. }
  788. pSelf->bWindowHooked = TRUE;
  789. }
  790. bail:
  791. tsk_safeobj_unlock(pSelf);
  792. return S_OK;
  793. }
  794. static HRESULT UnhookWindow(struct plugin_video_dshow_consumer_s *pSelf)
  795. {
  796. tsk_safeobj_lock(pSelf);
  797. if (pSelf->hWindow && pSelf->wndProc) {
  798. SetWindowLongPtr(pSelf->hWindow, GWLP_WNDPROC, (LONG_PTR)pSelf->wndProc);
  799. pSelf->wndProc = NULL;
  800. }
  801. if(pSelf->hWindow) {
  802. ::InvalidateRect(pSelf->hWindow, NULL, FALSE);
  803. }
  804. pSelf->bWindowHooked = FALSE;
  805. tsk_safeobj_unlock(pSelf);
  806. return S_OK;
  807. }
  808. #else /* !PLUGIN_DS_CV_USE_D3D9 */
  809. typedef struct plugin_video_dshow_consumer_s {
  810. TMEDIA_DECLARE_CONSUMER;
  811. DSDisplay* display;
  812. INT64 window;
  813. tsk_bool_t plugin_firefox;
  814. tsk_bool_t started;
  815. tsk_bool_t create_on_ui_thread;
  816. }
  817. plugin_video_dshow_consumer_t;
  818. /* ============ Media Consumer Interface ================= */
  819. static int plugin_video_dshow_consumer_set(tmedia_consumer_t *self, const tmedia_param_t* param)
  820. {
  821. int ret = 0;
  822. if(!self || !param) {
  823. TSK_DEBUG_ERROR("Invalid parameter");
  824. return -1;
  825. }
  826. if(param->value_type == tmedia_pvt_int64) {
  827. if(tsk_striequals(param->key, "remote-hwnd")) {
  828. DSCONSUMER(self)->window = (INT64)*((int64_t*)param->value);
  829. if(DSCONSUMER(self)->display) {
  830. if(DSCONSUMER(self)->window) {
  831. DSCONSUMER(self)->display->attach(DSCONSUMER(self)->window);
  832. }
  833. else {
  834. DSCONSUMER(self)->display->detach();
  835. }
  836. }
  837. }
  838. }
  839. else if(param->value_type == tmedia_pvt_int32) {
  840. if(tsk_striequals(param->key, "fullscreen")) {
  841. if(DSCONSUMER(self)->display) {
  842. DSCONSUMER(self)->display->setFullscreen(*((int32_t*)param->value) != 0);
  843. }
  844. }
  845. else if(tsk_striequals(param->key, "create-on-current-thead")) {
  846. DSCONSUMER(self)->create_on_ui_thread = *((int32_t*)param->value) ? tsk_false : tsk_true;
  847. }
  848. else if(tsk_striequals(param->key, "plugin-firefox")) {
  849. DSCONSUMER(self)->plugin_firefox = (*((int32_t*)param->value) != 0);
  850. if(DSCONSUMER(self)->display) {
  851. DSCONSUMER(self)->display->setPluginFirefox((DSCONSUMER(self)->plugin_firefox == tsk_true));
  852. }
  853. }
  854. }
  855. return ret;
  856. }
  857. static int plugin_video_dshow_consumer_prepare(tmedia_consumer_t* self, const tmedia_codec_t* codec)
  858. {
  859. plugin_video_dshow_consumer_t* consumer = (plugin_video_dshow_consumer_t*)self;
  860. if(!consumer || !codec && codec->plugin) {
  861. TSK_DEBUG_ERROR("Invalid parameter");
  862. return -1;
  863. }
  864. TMEDIA_CONSUMER(consumer)->video.fps = TMEDIA_CODEC_VIDEO(codec)->in.fps;
  865. TMEDIA_CONSUMER(consumer)->video.in.width = TMEDIA_CODEC_VIDEO(codec)->in.width;
  866. TMEDIA_CONSUMER(consumer)->video.in.height = TMEDIA_CODEC_VIDEO(codec)->in.height;
  867. if(!TMEDIA_CONSUMER(consumer)->video.display.width) {
  868. TMEDIA_CONSUMER(consumer)->video.display.width = TMEDIA_CONSUMER(consumer)->video.in.width;
  869. }
  870. if(!TMEDIA_CONSUMER(consumer)->video.display.height) {
  871. TMEDIA_CONSUMER(consumer)->video.display.height = TMEDIA_CONSUMER(consumer)->video.in.height;
  872. }
  873. return 0;
  874. }
  875. static int plugin_video_dshow_consumer_start(tmedia_consumer_t* self)
  876. {
  877. plugin_video_dshow_consumer_t* consumer = (plugin_video_dshow_consumer_t*)self;
  878. if(!consumer) {
  879. TSK_DEBUG_ERROR("Invalid parameter");
  880. return -1;
  881. }
  882. if(consumer->started) {
  883. return 0;
  884. }
  885. // create display on UI thread
  886. if(!consumer->display) {
  887. if (consumer->create_on_ui_thread) {
  888. createOnUIThead(reinterpret_cast<HWND>((void*)consumer->window), (void**)&consumer->display, true, false);
  889. }
  890. else {
  891. createOnCurrentThead(reinterpret_cast<HWND>((void*)consumer->window), (void**)&consumer->display, true, false);
  892. }
  893. if(!consumer->display) {
  894. TSK_DEBUG_ERROR("Failed to create display");
  895. return -2;
  896. }
  897. }
  898. // Set parameters
  899. consumer->display->setPluginFirefox((consumer->plugin_firefox == tsk_true));
  900. consumer->display->setFps(TMEDIA_CONSUMER(consumer)->video.fps);
  901. // do not change the display size: see hook()
  902. // consumer->display->setSize(TMEDIA_CONSUMER(consumer)->video.display.width, TMEDIA_CONSUMER(consumer)->video.display.height);
  903. if(consumer->window) {
  904. consumer->display->attach(consumer->window);
  905. }
  906. // Start display
  907. consumer->display->start();
  908. consumer->started = tsk_true;
  909. return 0;
  910. }
  911. static int plugin_video_dshow_consumer_consume(tmedia_consumer_t* self, const void* buffer, tsk_size_t size, const tsk_object_t* proto_hdr)
  912. {
  913. plugin_video_dshow_consumer_t* consumer = (plugin_video_dshow_consumer_t*)self;
  914. if(consumer && consumer->display && buffer) {
  915. consumer->display->handleVideoFrame(buffer, TMEDIA_CONSUMER(consumer)->video.display.width, TMEDIA_CONSUMER(consumer)->video.display.height);
  916. return 0;
  917. }
  918. else {
  919. TSK_DEBUG_ERROR("Invalid parameter");
  920. return -1;
  921. }
  922. }
  923. static int plugin_video_dshow_consumer_pause(tmedia_consumer_t* self)
  924. {
  925. plugin_video_dshow_consumer_t* consumer = (plugin_video_dshow_consumer_t*)self;
  926. if(!consumer) {
  927. TSK_DEBUG_ERROR("Invalid parameter");
  928. return -1;
  929. }
  930. if(!consumer->display) {
  931. TSK_DEBUG_ERROR("Invalid internal grabber");
  932. return -2;
  933. }
  934. //consumer->display->pause();
  935. return 0;
  936. }
  937. static int plugin_video_dshow_consumer_stop(tmedia_consumer_t* self)
  938. {
  939. plugin_video_dshow_consumer_t* consumer = (plugin_video_dshow_consumer_t*)self;
  940. if(!self) {
  941. TSK_DEBUG_ERROR("Invalid parameter");
  942. return -1;
  943. }
  944. if(!consumer->started) {
  945. return 0;
  946. }
  947. if(!consumer->display) {
  948. TSK_DEBUG_ERROR("Invalid internal display");
  949. return -2;
  950. }
  951. TSK_DEBUG_INFO("Before stopping DirectShow consumer");
  952. consumer->display->stop();
  953. consumer->started = tsk_false;
  954. TSK_DEBUG_INFO("After stopping DirectShow consumer");
  955. return 0;
  956. }
  957. //
  958. // DirectShow consumer object definition
  959. //
  960. /* constructor */
  961. static tsk_object_t* plugin_video_dshow_consumer_ctor(tsk_object_t * self, va_list * app)
  962. {
  963. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  964. plugin_video_dshow_consumer_t *consumer = (plugin_video_dshow_consumer_t *)self;
  965. if(consumer) {
  966. /* init base */
  967. tmedia_consumer_init(TMEDIA_CONSUMER(consumer));
  968. TMEDIA_CONSUMER(consumer)->video.display.chroma = tmedia_chroma_bgr24; // RGB24 on x86 (little endians) stored as BGR24
  969. /* init self */
  970. consumer->create_on_ui_thread = tsk_true;
  971. TMEDIA_CONSUMER(consumer)->video.fps = 15;
  972. TMEDIA_CONSUMER(consumer)->video.display.width = 352;
  973. TMEDIA_CONSUMER(consumer)->video.display.height = 288;
  974. TMEDIA_CONSUMER(consumer)->video.display.auto_resize = tsk_true;
  975. }
  976. return self;
  977. }
  978. /* destructor */
  979. static tsk_object_t* plugin_video_dshow_consumer_dtor(tsk_object_t * self)
  980. {
  981. plugin_video_dshow_consumer_t *consumer = (plugin_video_dshow_consumer_t *)self;
  982. if(consumer) {
  983. /* stop */
  984. if(consumer->started) {
  985. plugin_video_dshow_consumer_stop((tmedia_consumer_t*)self);
  986. }
  987. /* deinit base */
  988. tmedia_consumer_deinit(TMEDIA_CONSUMER(consumer));
  989. /* deinit self */
  990. SAFE_DELETE_PTR(consumer->display);
  991. }
  992. return self;
  993. }
  994. /* object definition */
  995. static const tsk_object_def_t plugin_video_dshow_consumer_def_s = {
  996. sizeof(plugin_video_dshow_consumer_t),
  997. plugin_video_dshow_consumer_ctor,
  998. plugin_video_dshow_consumer_dtor,
  999. tsk_null,
  1000. };
  1001. /* plugin definition*/
  1002. static const tmedia_consumer_plugin_def_t plugin_video_dshow_consumer_plugin_def_s = {
  1003. &plugin_video_dshow_consumer_def_s,
  1004. tmedia_video,
  1005. "Microsoft DirectShow consumer (using custom source)",
  1006. plugin_video_dshow_consumer_set,
  1007. plugin_video_dshow_consumer_prepare,
  1008. plugin_video_dshow_consumer_start,
  1009. plugin_video_dshow_consumer_consume,
  1010. plugin_video_dshow_consumer_pause,
  1011. plugin_video_dshow_consumer_stop
  1012. };
  1013. const tmedia_consumer_plugin_def_t *plugin_video_dshow_consumer_plugin_def_t = &plugin_video_dshow_consumer_plugin_def_s;
  1014. #endif /* PLUGIN_DS_CV_USE_D3D9 */