DSGrabber.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "internals/DSCaptureUtils.h"
  22. #include "internals/Resizer.h"
  23. #include "internals/DSUtils.h"
  24. #include "internals/DSCaptureGraph.h"
  25. #if !defined(_WIN32_WCE)
  26. # include "internals/DSScreenCastGraph.h"
  27. #endif
  28. #include "tsk_debug.h"
  29. #include <assert.h>
  30. using namespace std;
  31. DSGrabber::DSGrabber(HRESULT *hr, BOOL _screenCast)
  32. : mutex_buffer(NULL), preview(NULL)
  33. , screenCast(_screenCast)
  34. {
  35. #if defined(_WIN32_WCE)
  36. assert(!screenCast);
  37. this->graph = new DSCaptureGraph(this, hr);
  38. CHECK_HR((*hr));
  39. #else
  40. this->graph = screenCast ? dynamic_cast<DSBaseCaptureGraph*>(new DSScreenCastGraph(this, hr)) : dynamic_cast<DSBaseCaptureGraph*>(new DSCaptureGraph(this, hr));
  41. CHECK_HR((*hr));
  42. this->preview = new DSDisplay(hr);
  43. #endif
  44. // Init the bitmap info header with default values
  45. memset(&(this->bitmapInfo), 0, sizeof(BITMAPINFOHEADER));
  46. this->bitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
  47. this->bitmapInfo.biWidth = 352;
  48. this->bitmapInfo.biHeight = 288;
  49. this->bitmapInfo.biPlanes = 1;
  50. this->bitmapInfo.biBitCount = 24;
  51. this->bitmapInfo.biCompression = 0;
  52. this->bitmapInfo.biXPelsPerMeter = 0;
  53. this->bitmapInfo.biYPelsPerMeter = 0;
  54. this->bitmapInfo.biClrUsed = 0;
  55. this->bitmapInfo.biClrImportant = 0;
  56. this->plugin_cb = NULL;
  57. this->buffer = NULL;
  58. this->mutex_buffer = tsk_mutex_create();
  59. bail:
  60. ;
  61. }
  62. DSGrabber::~DSGrabber()
  63. {
  64. SAFE_DELETE_PTR ( this->graph );
  65. SAFE_DELETE_PTR ( this->preview );
  66. SAFE_DELETE_ARRAY ( this->buffer );
  67. tsk_mutex_destroy(&this->mutex_buffer);
  68. }
  69. void DSGrabber::setCaptureDevice(const std::string &devicePath)
  70. {
  71. this->graph->setSource(devicePath);
  72. }
  73. void DSGrabber::setCallback(tmedia_producer_enc_cb_f callback, const void* callback_data)
  74. {
  75. this->plugin_cb = callback;
  76. this->plugin_cb_data = callback_data;
  77. }
  78. void DSGrabber::start()
  79. {
  80. if(this->graph->isPaused()) {
  81. this->graph->start();
  82. this->preview->start();
  83. return;
  84. }
  85. if (!this->graph->isRunning()) {
  86. first_buffer = true;
  87. if(this->preview) {
  88. this->preview->start();
  89. }
  90. this->graph->connect();
  91. this->graph->start();
  92. }
  93. }
  94. void DSGrabber::pause()
  95. {
  96. if(this->graph && this->graph->isRunning()) {
  97. this->graph->pause();
  98. this->preview->pause();
  99. }
  100. }
  101. void DSGrabber::stop()
  102. {
  103. if (this->graph->isRunning()) {
  104. if(this->preview) {
  105. this->preview->stop();
  106. }
  107. this->graph->stop();
  108. this->graph->disconnect();
  109. }
  110. }
  111. bool DSGrabber::setCaptureParameters(int w, int h, int f)
  112. {
  113. tsk_mutex_lock(this->mutex_buffer);
  114. // Store the framerate
  115. this->fps = f;
  116. this->width = w;
  117. this->height = h;
  118. // Store the required dimensions
  119. this->bitmapInfo.biWidth = this->width;
  120. this->bitmapInfo.biHeight = this->height;
  121. this->bitmapInfo.biBitCount = 24;
  122. this->bitmapInfo.biSizeImage = (this->width * this->height * 3);
  123. // Change the intermediate buffer
  124. SAFE_DELETE_ARRAY ( this->buffer );
  125. this->buffer = new BYTE[this->bitmapInfo.biSizeImage];
  126. memset(this->buffer,0,this->bitmapInfo.biSizeImage);
  127. // Find closest matching format to drive the source filter
  128. DSCaptureFormat *fmt = NULL;
  129. int score = 0;
  130. std::vector<DSCaptureFormat> *formats = this->graph->getFormats();
  131. std::vector<DSCaptureFormat>::iterator iter;
  132. std::vector<DSCaptureFormat>::iterator last = formats->end();
  133. for(iter = formats->begin(); iter != last; iter++) {
  134. int value = (*iter).getMatchScore(this->width, this->height);
  135. if (value > score || !fmt) {
  136. score = value;
  137. fmt = &(*iter);
  138. }
  139. }
  140. // Setup source filter in the graph
  141. HRESULT hr = this->graph->setParameters(fmt, this->fps);
  142. // Set preview parameters
  143. if(this->preview) {
  144. this->preview->setFps(this->fps);
  145. this->preview->setSize(this->width, this->height);
  146. }
  147. tsk_mutex_unlock(this->mutex_buffer);
  148. return SUCCEEDED(hr);
  149. }
  150. void DSGrabber::setPluginFirefox(bool value)
  151. {
  152. if(this->preview) {
  153. this->preview->setPluginFirefox(value);
  154. }
  155. }
  156. bool DSGrabber::setCaptureParameters(int format, int f)
  157. {
  158. int w, h;
  159. // Get size from the format
  160. VIDEOFORMAT_TO_SIZE(format, w, h);
  161. return this->setCaptureParameters(w, h, f);
  162. }
  163. int DSGrabber::getFramerate()
  164. {
  165. return this->fps;
  166. }
  167. HRESULT DSGrabber::getConnectedMediaType(AM_MEDIA_TYPE *mediaType)
  168. {
  169. if (!this->graph || !mediaType) {
  170. return E_INVALIDARG;
  171. }
  172. return this->graph->getConnectedMediaType(mediaType);
  173. }
  174. HRESULT DSGrabber::SampleCB(double SampleTime, IMediaSample *pSample)
  175. {
  176. return S_OK;
  177. }
  178. HRESULT DSGrabber::BufferCB(double SampleTime, BYTE *pBuffer, long BufferLen)
  179. {
  180. HRESULT hr;
  181. tsk_mutex_lock(this->mutex_buffer);
  182. AM_MEDIA_TYPE mediaType;
  183. hr = this->graph->getConnectedMediaType(&mediaType);
  184. if (FAILED(hr) || !this->buffer) {
  185. return hr;
  186. }
  187. if(first_buffer) {
  188. first_buffer = false;
  189. tsk_mutex_unlock(this->mutex_buffer);
  190. return hr;
  191. }
  192. // Examine the format block.
  193. if ((mediaType.formattype == FORMAT_VideoInfo) && (mediaType.cbFormat >= sizeof(VIDEOINFOHEADER)) && (mediaType.pbFormat != NULL) ) {
  194. VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER *>(mediaType.pbFormat);
  195. BITMAPINFOHEADER* bih = &pVih->bmiHeader;
  196. //int framerate = pVih->AvgTimePerFrame;
  197. if( (bih->biHeight == this->bitmapInfo.biHeight) && (bih->biWidth == this->bitmapInfo.biWidth) && (bih->biBitCount == this->bitmapInfo.biBitCount) ) {
  198. memmove(this->buffer, pBuffer, this->bitmapInfo.biSizeImage);
  199. }
  200. else {
  201. ResizeRGB(
  202. bih,
  203. (const unsigned char *) pBuffer,
  204. &this->bitmapInfo,
  205. (unsigned char *) this->buffer,
  206. this->width,
  207. this->height);
  208. }
  209. // for the network
  210. if(this->plugin_cb) {
  211. this->plugin_cb(this->plugin_cb_data, this->buffer, (this->width*this->height*3));
  212. }
  213. // for the preview
  214. if(this->preview) {
  215. this->preview->handleVideoFrame(this->buffer, this->width, this->height);
  216. }
  217. }
  218. // Free the format
  219. #ifdef _WIN32_WCE
  220. // Nothing had been allocated
  221. #else
  222. FreeMediaType(mediaType);
  223. #endif
  224. tsk_mutex_unlock(this->mutex_buffer);
  225. return hr;
  226. }
  227. HRESULT DSGrabber::QueryInterface(REFIID iid, LPVOID *ppv)
  228. {
  229. #ifdef _WIN32_WCE
  230. assert(1==0);
  231. #else
  232. if( iid == IID_ISampleGrabberCB || iid == IID_IUnknown ) {
  233. *ppv = (void *) static_cast<ISampleGrabberCB*>(this);
  234. return NOERROR;
  235. }
  236. #endif
  237. return E_NOINTERFACE;
  238. }
  239. ULONG DSGrabber::AddRef()
  240. {
  241. return 2;
  242. }
  243. ULONG DSGrabber::Release()
  244. {
  245. return 1;
  246. }