DSOutputStream.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/DSOutputStream.h"
  19. #include "internals/DSOutputFilter.h"
  20. #include "internals/DSUtils.h"
  21. #include <iostream>
  22. #include "tsk_memory.h"
  23. using namespace std;
  24. #define DEFAULT_FPS 15
  25. #define MEMCPY_WORKAROUND 1
  26. // Overlay
  27. #define OVERLAY 0
  28. #define OVERLAY_TEXT TEXT("Press ESC to exit full screen mode")
  29. #define OVERLAY_DURATION 3 // in seconds
  30. DSOutputStream::DSOutputStream(HRESULT *phr, DSOutputFilter *pParent, LPCWSTR pPinName)
  31. : CSourceStream(_T("DSOutputStream"), phr, pParent, pPinName)
  32. {
  33. #if !(defined(_WIN32_WCE) && defined(_DEBUG))
  34. CAutoLock cAutoLock(m_pFilter->pStateLock());
  35. #endif
  36. this->buffer = NULL;
  37. this->buffer_size = NULL;
  38. this->frameNumber = 0;
  39. this->frameLength = (1000)/DEFAULT_FPS;
  40. this->fps = DEFAULT_FPS;
  41. this->width = 352;
  42. this->height = 288;
  43. this->overlay = false;
  44. this->paintBuffer = NULL;
  45. this->paintDC = NULL;
  46. this->hDibSection = NULL;
  47. this->hObject = NULL;
  48. this->mutex = tsk_mutex_create();
  49. }
  50. DSOutputStream::~DSOutputStream()
  51. {
  52. TSK_FREE(this->buffer);
  53. tsk_mutex_destroy(&this->mutex);
  54. // TODO : Is there anything to free ???
  55. }
  56. void DSOutputStream::setFps(int fps_)
  57. {
  58. this->fps = fps_;
  59. this->frameLength = (1000)/this->fps;
  60. }
  61. void DSOutputStream::showOverlay(int value)
  62. {
  63. if (value == 0) {
  64. this->overlay = false;
  65. }
  66. this->overlay = (value > 0);
  67. }
  68. HRESULT DSOutputStream::setImageFormat(UINT width, UINT height)
  69. {
  70. if ((this->width == width) && (this->height == height)) {
  71. return S_FALSE;
  72. }
  73. this->width = width;
  74. this->height = height;
  75. this->frameNumber = 0;
  76. return S_OK;
  77. }
  78. bool DSOutputStream::getImageFormat(UINT &width, UINT &height)
  79. {
  80. width = this->width;
  81. height = this->height;
  82. return true;
  83. }
  84. HRESULT DSOutputStream::GetMediaType(CMediaType *pMediaType)
  85. {
  86. HRESULT hr = S_OK;
  87. #if !(defined(_WIN32_WCE) && defined(_DEBUG))
  88. CAutoLock lock(m_pFilter->pStateLock());
  89. #endif
  90. ZeroMemory(pMediaType, sizeof(CMediaType));
  91. VIDEOINFO *pvi = (VIDEOINFO *)pMediaType->AllocFormatBuffer(sizeof(VIDEOINFO));
  92. if (NULL == pvi) {
  93. return E_OUTOFMEMORY;
  94. }
  95. ZeroMemory(pvi, sizeof(VIDEOINFO));
  96. pvi->bmiHeader.biCompression = BI_RGB;
  97. pvi->bmiHeader.biBitCount = 24;
  98. pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  99. pvi->bmiHeader.biWidth = this->width;
  100. pvi->bmiHeader.biHeight = this->height;
  101. pvi->bmiHeader.biPlanes = 1;
  102. pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader);
  103. pvi->bmiHeader.biClrImportant = 0;
  104. // Frame rate
  105. pvi->AvgTimePerFrame = DS_MILLIS_TO_100NS(1000/this->fps);
  106. SetRectEmpty(&(pvi->rcSource)); // we want the whole image area rendered.
  107. SetRectEmpty(&(pvi->rcTarget)); // no particular destination rectangle
  108. pMediaType->SetType(&MEDIATYPE_Video);
  109. pMediaType->SetFormatType(&FORMAT_VideoInfo);
  110. pMediaType->SetTemporalCompression(FALSE);
  111. pMediaType->SetSubtype(&MEDIASUBTYPE_RGB24);
  112. pMediaType->SetSampleSize(pvi->bmiHeader.biSizeImage);
  113. bitmapInfo.bmiHeader = pvi->bmiHeader;
  114. return hr;
  115. }
  116. HRESULT DSOutputStream::DecideBufferSize(IMemAllocator *pMemAlloc, ALLOCATOR_PROPERTIES *pProperties)
  117. {
  118. CheckPointer(pMemAlloc, E_POINTER);
  119. CheckPointer(pProperties, E_POINTER);
  120. #if !(defined(_WIN32_WCE) && defined(_DEBUG))
  121. CAutoLock cAutoLock(m_pFilter->pStateLock());
  122. #endif
  123. HRESULT hr = NOERROR;
  124. VIDEOINFO *pvi = (VIDEOINFO *) m_mt.Format();
  125. pProperties->cBuffers = 1;
  126. pProperties->cbBuffer = pvi->bmiHeader.biSizeImage;
  127. // Ask the allocator to reserve us some sample memory. NOTE: the function
  128. // can succeed (return NOERROR) but still not have allocated the
  129. // memory that we requested, so we must check we got whatever we wanted.
  130. ALLOCATOR_PROPERTIES Actual;
  131. hr = pMemAlloc->SetProperties(pProperties,&Actual);
  132. if(FAILED(hr)) {
  133. return hr;
  134. }
  135. // Is this allocator unsuitable?
  136. if(Actual.cbBuffer < pProperties->cbBuffer) {
  137. return E_FAIL;
  138. }
  139. // Make sure that we have only 1 buffer (we erase the ball in the
  140. // old buffer to save having to zero a 200k+ buffer every time
  141. // we draw a frame)
  142. return NOERROR;
  143. }
  144. HRESULT DSOutputStream::OnThreadCreate()
  145. {
  146. #if OVERLAY
  147. hDibSection = CreateDIBSection(NULL, (BITMAPINFO *) &bitmapInfo, DIB_RGB_COLORS, &paintBuffer, NULL, 0);
  148. HDC hDC = GetDC(NULL);
  149. paintDC = CreateCompatibleDC(hDC);
  150. SetMapMode(paintDC, GetMapMode(hDC));
  151. SetBkMode(paintDC, TRANSPARENT);
  152. SetTextColor(paintDC, RGB(255,255,255));
  153. hObject = SelectObject(paintDC, hDibSection);
  154. #endif
  155. return CSourceStream::OnThreadCreate();
  156. }
  157. HRESULT DSOutputStream::OnThreadDestroy()
  158. {
  159. #if OVERLAY
  160. if (paintDC) {
  161. DeleteDC(paintDC);
  162. }
  163. if (hObject) {
  164. DeleteObject(hObject);
  165. }
  166. if (paintBuffer) {
  167. //delete[] paintBuffer; // will be done
  168. //paintBuffer = NULL;
  169. }
  170. #endif
  171. return CSourceStream::OnThreadDestroy();
  172. }
  173. inline HRESULT DSOutputStream::DrawOverLay(void *pBuffer, long lSize)
  174. {
  175. // called only #if OVERLAY
  176. CopyMemory(paintBuffer, pBuffer, lSize);
  177. // Draw the current frame
  178. #ifdef _WIN32_WCE
  179. #else
  180. if( !TextOut( paintDC, 0, 0, OVERLAY_TEXT, (int)_tcslen( OVERLAY_TEXT ) ) ) {
  181. return E_FAIL;
  182. }
  183. #endif
  184. CopyMemory(pBuffer, paintBuffer, lSize);
  185. return S_OK;
  186. }
  187. static __inline void TransfertBuffer(void* src, void* dest, long lSize)
  188. {
  189. __try {
  190. #if MEMCPY_WORKAROUND
  191. //#ifdef _WIN32_WCE
  192. memmove(dest, src, lSize);
  193. /*#else
  194. unsigned char * pDst = (unsigned char *) dest;
  195. if(src){
  196. unsigned char const * pSrc = (unsigned char const *) src;
  197. for( register int i=0; ((i< lSize) && src); i++) *pDst++ = *pSrc++;
  198. }else{
  199. for( register int i=0; i< lSize; i++) *pDst++ = 0;
  200. }
  201. #endif*/
  202. #else
  203. CopyMemory(dest, src, lSize); //BUGGY
  204. #endif
  205. }
  206. __except(EXCEPTION_ACCESS_VIOLATION == GetExceptionCode()) {
  207. //ZeroMemory(dest, sizeof(void*));
  208. }
  209. }
  210. HRESULT DSOutputStream::FillBuffer(IMediaSample *pSample)
  211. {
  212. CheckPointer(pSample, E_POINTER);
  213. #if !(defined(_WIN32_WCE) && defined(_DEBUG))
  214. CAutoLock lock(m_pFilter->pStateLock());
  215. #endif
  216. HRESULT hr;
  217. BYTE *pBuffer = NULL;
  218. long lSize, lDataSize;
  219. hr = pSample->GetPointer(&pBuffer);
  220. if (SUCCEEDED(hr)) {
  221. lDataSize = lSize = pSample->GetSize();
  222. // Check that we're still using video
  223. //ASSERT(m_mt.formattype == FORMAT_VideoInfo);
  224. if (this->buffer) {
  225. #if OVERLAY
  226. if (this->overlay) {
  227. DrawOverLay(this->buffer, lSize);
  228. }
  229. #endif
  230. // Why try do not work, see: http://msdn2.microsoft.com/en-us/library/xwtb73ad(vs.80).aspx
  231. this->lockBuffer();
  232. lDataSize = TSK_MIN(lSize, this->buffer_size);
  233. TransfertBuffer(this->buffer, (void*)pBuffer, lDataSize);
  234. this->unlockBuffer();
  235. }
  236. else {
  237. // Avoid caching last image
  238. memset((void*)pBuffer, NULL, lSize);
  239. }
  240. REFERENCE_TIME rtStart = DS_MILLIS_TO_100NS(this->frameNumber * this->frameLength);
  241. REFERENCE_TIME rtStop = rtStart + DS_MILLIS_TO_100NS(this->frameLength);
  242. this->frameNumber++;
  243. pSample->SetTime(&rtStart, &rtStop);
  244. //pSample->SetMediaTime(&rtStart, &rtStop);
  245. pSample->SetActualDataLength(lDataSize);
  246. pSample->SetPreroll(FALSE);
  247. pSample->SetDiscontinuity(FALSE);
  248. }
  249. // Set TRUE on every sample for uncompressed frames (KEYFRAME)
  250. pSample->SetSyncPoint(TRUE);
  251. return S_OK;
  252. }