dshow_pin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * DirectShow capture interface
  3. * Copyright (c) 2010 Ramiro Polla
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "dshow_capture.h"
  22. #include <stddef.h>
  23. #define imemoffset offsetof(libAVPin, imemvtbl)
  24. DECLARE_QUERYINTERFACE(libAVPin,
  25. { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
  26. DECLARE_ADDREF(libAVPin)
  27. DECLARE_RELEASE(libAVPin)
  28. long WINAPI
  29. libAVPin_Connect(libAVPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
  30. {
  31. dshowdebug("libAVPin_Connect(%p, %p, %p)\n", this, pin, type);
  32. /* Input pins receive connections. */
  33. return S_FALSE;
  34. }
  35. long WINAPI
  36. libAVPin_ReceiveConnection(libAVPin *this, IPin *pin,
  37. const AM_MEDIA_TYPE *type)
  38. {
  39. enum dshowDeviceType devtype = this->filter->type;
  40. dshowdebug("libAVPin_ReceiveConnection(%p)\n", this);
  41. if (!pin)
  42. return E_POINTER;
  43. if (this->connectedto)
  44. return VFW_E_ALREADY_CONNECTED;
  45. ff_print_AM_MEDIA_TYPE(type);
  46. if (devtype == VideoDevice) {
  47. if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video))
  48. return VFW_E_TYPE_NOT_ACCEPTED;
  49. } else {
  50. if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
  51. return VFW_E_TYPE_NOT_ACCEPTED;
  52. }
  53. IPin_AddRef(pin);
  54. this->connectedto = pin;
  55. ff_copy_dshow_media_type(&this->type, type);
  56. return S_OK;
  57. }
  58. long WINAPI
  59. libAVPin_Disconnect(libAVPin *this)
  60. {
  61. dshowdebug("libAVPin_Disconnect(%p)\n", this);
  62. if (this->filter->state != State_Stopped)
  63. return VFW_E_NOT_STOPPED;
  64. if (!this->connectedto)
  65. return S_FALSE;
  66. IPin_Release(this->connectedto);
  67. this->connectedto = NULL;
  68. return S_OK;
  69. }
  70. long WINAPI
  71. libAVPin_ConnectedTo(libAVPin *this, IPin **pin)
  72. {
  73. dshowdebug("libAVPin_ConnectedTo(%p)\n", this);
  74. if (!pin)
  75. return E_POINTER;
  76. if (!this->connectedto)
  77. return VFW_E_NOT_CONNECTED;
  78. IPin_AddRef(this->connectedto);
  79. *pin = this->connectedto;
  80. return S_OK;
  81. }
  82. long WINAPI
  83. libAVPin_ConnectionMediaType(libAVPin *this, AM_MEDIA_TYPE *type)
  84. {
  85. dshowdebug("libAVPin_ConnectionMediaType(%p)\n", this);
  86. if (!type)
  87. return E_POINTER;
  88. if (!this->connectedto)
  89. return VFW_E_NOT_CONNECTED;
  90. return ff_copy_dshow_media_type(type, &this->type);
  91. }
  92. long WINAPI
  93. libAVPin_QueryPinInfo(libAVPin *this, PIN_INFO *info)
  94. {
  95. dshowdebug("libAVPin_QueryPinInfo(%p)\n", this);
  96. if (!info)
  97. return E_POINTER;
  98. if (this->filter)
  99. libAVFilter_AddRef(this->filter);
  100. info->pFilter = (IBaseFilter *) this->filter;
  101. info->dir = PINDIR_INPUT;
  102. wcscpy(info->achName, L"Capture");
  103. return S_OK;
  104. }
  105. long WINAPI
  106. libAVPin_QueryDirection(libAVPin *this, PIN_DIRECTION *dir)
  107. {
  108. dshowdebug("libAVPin_QueryDirection(%p)\n", this);
  109. if (!dir)
  110. return E_POINTER;
  111. *dir = PINDIR_INPUT;
  112. return S_OK;
  113. }
  114. long WINAPI
  115. libAVPin_QueryId(libAVPin *this, wchar_t **id)
  116. {
  117. dshowdebug("libAVPin_QueryId(%p)\n", this);
  118. if (!id)
  119. return E_POINTER;
  120. *id = wcsdup(L"libAV Pin");
  121. return S_OK;
  122. }
  123. long WINAPI
  124. libAVPin_QueryAccept(libAVPin *this, const AM_MEDIA_TYPE *type)
  125. {
  126. dshowdebug("libAVPin_QueryAccept(%p)\n", this);
  127. return S_FALSE;
  128. }
  129. long WINAPI
  130. libAVPin_EnumMediaTypes(libAVPin *this, IEnumMediaTypes **enumtypes)
  131. {
  132. const AM_MEDIA_TYPE *type = NULL;
  133. libAVEnumMediaTypes *new;
  134. dshowdebug("libAVPin_EnumMediaTypes(%p)\n", this);
  135. if (!enumtypes)
  136. return E_POINTER;
  137. new = libAVEnumMediaTypes_Create(type);
  138. if (!new)
  139. return E_OUTOFMEMORY;
  140. *enumtypes = (IEnumMediaTypes *) new;
  141. return S_OK;
  142. }
  143. long WINAPI
  144. libAVPin_QueryInternalConnections(libAVPin *this, IPin **pin,
  145. unsigned long *npin)
  146. {
  147. dshowdebug("libAVPin_QueryInternalConnections(%p)\n", this);
  148. return E_NOTIMPL;
  149. }
  150. long WINAPI
  151. libAVPin_EndOfStream(libAVPin *this)
  152. {
  153. dshowdebug("libAVPin_EndOfStream(%p)\n", this);
  154. /* I don't care. */
  155. return S_OK;
  156. }
  157. long WINAPI
  158. libAVPin_BeginFlush(libAVPin *this)
  159. {
  160. dshowdebug("libAVPin_BeginFlush(%p)\n", this);
  161. /* I don't care. */
  162. return S_OK;
  163. }
  164. long WINAPI
  165. libAVPin_EndFlush(libAVPin *this)
  166. {
  167. dshowdebug("libAVPin_EndFlush(%p)\n", this);
  168. /* I don't care. */
  169. return S_OK;
  170. }
  171. long WINAPI
  172. libAVPin_NewSegment(libAVPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
  173. double rate)
  174. {
  175. dshowdebug("libAVPin_NewSegment(%p)\n", this);
  176. /* I don't care. */
  177. return S_OK;
  178. }
  179. static int
  180. libAVPin_Setup(libAVPin *this, libAVFilter *filter)
  181. {
  182. IPinVtbl *vtbl = this->vtbl;
  183. IMemInputPinVtbl *imemvtbl;
  184. if (!filter)
  185. return 0;
  186. imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
  187. if (!imemvtbl)
  188. return 0;
  189. SETVTBL(imemvtbl, libAVMemInputPin, QueryInterface);
  190. SETVTBL(imemvtbl, libAVMemInputPin, AddRef);
  191. SETVTBL(imemvtbl, libAVMemInputPin, Release);
  192. SETVTBL(imemvtbl, libAVMemInputPin, GetAllocator);
  193. SETVTBL(imemvtbl, libAVMemInputPin, NotifyAllocator);
  194. SETVTBL(imemvtbl, libAVMemInputPin, GetAllocatorRequirements);
  195. SETVTBL(imemvtbl, libAVMemInputPin, Receive);
  196. SETVTBL(imemvtbl, libAVMemInputPin, ReceiveMultiple);
  197. SETVTBL(imemvtbl, libAVMemInputPin, ReceiveCanBlock);
  198. this->imemvtbl = imemvtbl;
  199. SETVTBL(vtbl, libAVPin, QueryInterface);
  200. SETVTBL(vtbl, libAVPin, AddRef);
  201. SETVTBL(vtbl, libAVPin, Release);
  202. SETVTBL(vtbl, libAVPin, Connect);
  203. SETVTBL(vtbl, libAVPin, ReceiveConnection);
  204. SETVTBL(vtbl, libAVPin, Disconnect);
  205. SETVTBL(vtbl, libAVPin, ConnectedTo);
  206. SETVTBL(vtbl, libAVPin, ConnectionMediaType);
  207. SETVTBL(vtbl, libAVPin, QueryPinInfo);
  208. SETVTBL(vtbl, libAVPin, QueryDirection);
  209. SETVTBL(vtbl, libAVPin, QueryId);
  210. SETVTBL(vtbl, libAVPin, QueryAccept);
  211. SETVTBL(vtbl, libAVPin, EnumMediaTypes);
  212. SETVTBL(vtbl, libAVPin, QueryInternalConnections);
  213. SETVTBL(vtbl, libAVPin, EndOfStream);
  214. SETVTBL(vtbl, libAVPin, BeginFlush);
  215. SETVTBL(vtbl, libAVPin, EndFlush);
  216. SETVTBL(vtbl, libAVPin, NewSegment);
  217. this->filter = filter;
  218. return 1;
  219. }
  220. DECLARE_CREATE(libAVPin, libAVPin_Setup(this, filter), libAVFilter *filter)
  221. DECLARE_DESTROY(libAVPin, nothing)
  222. /*****************************************************************************
  223. * libAVMemInputPin
  224. ****************************************************************************/
  225. long WINAPI
  226. libAVMemInputPin_QueryInterface(libAVMemInputPin *this, const GUID *riid,
  227. void **ppvObject)
  228. {
  229. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  230. dshowdebug("libAVMemInputPin_QueryInterface(%p)\n", this);
  231. return libAVPin_QueryInterface(pin, riid, ppvObject);
  232. }
  233. unsigned long WINAPI
  234. libAVMemInputPin_AddRef(libAVMemInputPin *this)
  235. {
  236. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  237. dshowdebug("libAVMemInputPin_AddRef(%p)\n", this);
  238. return libAVPin_AddRef(pin);
  239. }
  240. unsigned long WINAPI
  241. libAVMemInputPin_Release(libAVMemInputPin *this)
  242. {
  243. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  244. dshowdebug("libAVMemInputPin_Release(%p)\n", this);
  245. return libAVPin_Release(pin);
  246. }
  247. long WINAPI
  248. libAVMemInputPin_GetAllocator(libAVMemInputPin *this, IMemAllocator **alloc)
  249. {
  250. dshowdebug("libAVMemInputPin_GetAllocator(%p)\n", this);
  251. return VFW_E_NO_ALLOCATOR;
  252. }
  253. long WINAPI
  254. libAVMemInputPin_NotifyAllocator(libAVMemInputPin *this, IMemAllocator *alloc,
  255. BOOL rdwr)
  256. {
  257. dshowdebug("libAVMemInputPin_NotifyAllocator(%p)\n", this);
  258. return S_OK;
  259. }
  260. long WINAPI
  261. libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *this,
  262. ALLOCATOR_PROPERTIES *props)
  263. {
  264. dshowdebug("libAVMemInputPin_GetAllocatorRequirements(%p)\n", this);
  265. return E_NOTIMPL;
  266. }
  267. long WINAPI
  268. libAVMemInputPin_Receive(libAVMemInputPin *this, IMediaSample *sample)
  269. {
  270. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  271. enum dshowDeviceType devtype = pin->filter->type;
  272. void *priv_data;
  273. uint8_t *buf;
  274. int buf_size; /* todo should be a long? */
  275. int index;
  276. int64_t curtime;
  277. int64_t orig_curtime;
  278. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  279. IReferenceClock *clock = pin->filter->clock;
  280. int64_t dummy;
  281. dshowdebug("libAVMemInputPin_Receive(%p)\n", this);
  282. if (!sample)
  283. return E_POINTER;
  284. IMediaSample_GetTime(sample, &orig_curtime, &dummy);
  285. orig_curtime += pin->filter->start_time;
  286. if (devtype == VideoDevice) {
  287. /* PTS from video devices is unreliable. */
  288. IReferenceClock_GetTime(clock, &curtime);
  289. } else {
  290. IMediaSample_GetTime(sample, &curtime, &dummy);
  291. if(curtime > 400000000000000000LL) {
  292. /* initial frames sometimes start < 0 (shown as a very large number here,
  293. like 437650244077016960 which FFmpeg doesn't like.
  294. TODO figure out math. For now just drop them. */
  295. av_log(NULL, AV_LOG_DEBUG,
  296. "dshow dropping initial (or ending) audio frame with odd PTS too high %"PRId64"\n", curtime);
  297. return S_OK;
  298. }
  299. curtime += pin->filter->start_time;
  300. }
  301. buf_size = IMediaSample_GetActualDataLength(sample);
  302. IMediaSample_GetPointer(sample, &buf);
  303. priv_data = pin->filter->priv_data;
  304. index = pin->filter->stream_index;
  305. av_log(NULL, AV_LOG_VERBOSE, "dshow passing through packet of type %s size %6d timestamp %"PRId64" orig timestamp %"PRId64"\n",
  306. devtypename, buf_size, curtime, orig_curtime);
  307. pin->filter->callback(priv_data, index, buf, buf_size, curtime, devtype);
  308. return S_OK;
  309. }
  310. long WINAPI
  311. libAVMemInputPin_ReceiveMultiple(libAVMemInputPin *this,
  312. IMediaSample **samples, long n, long *nproc)
  313. {
  314. int i;
  315. dshowdebug("libAVMemInputPin_ReceiveMultiple(%p)\n", this);
  316. for (i = 0; i < n; i++)
  317. libAVMemInputPin_Receive(this, samples[i]);
  318. *nproc = n;
  319. return S_OK;
  320. }
  321. long WINAPI
  322. libAVMemInputPin_ReceiveCanBlock(libAVMemInputPin *this)
  323. {
  324. dshowdebug("libAVMemInputPin_ReceiveCanBlock(%p)\n", this);
  325. /* I swear I will not block. */
  326. return S_FALSE;
  327. }
  328. void
  329. libAVMemInputPin_Destroy(libAVMemInputPin *this)
  330. {
  331. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  332. dshowdebug("libAVMemInputPin_Destroy(%p)\n", this);
  333. libAVPin_Destroy(pin);
  334. }