dshow_pin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. static void
  221. libAVPin_Free(libAVPin *this)
  222. {
  223. if (!this)
  224. return;
  225. av_freep(&this->imemvtbl);
  226. if (this->type.pbFormat) {
  227. CoTaskMemFree(this->type.pbFormat);
  228. this->type.pbFormat = NULL;
  229. }
  230. }
  231. DECLARE_CREATE(libAVPin, libAVPin_Setup(this, filter), libAVFilter *filter)
  232. DECLARE_DESTROY(libAVPin, libAVPin_Free)
  233. /*****************************************************************************
  234. * libAVMemInputPin
  235. ****************************************************************************/
  236. long WINAPI
  237. libAVMemInputPin_QueryInterface(libAVMemInputPin *this, const GUID *riid,
  238. void **ppvObject)
  239. {
  240. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  241. dshowdebug("libAVMemInputPin_QueryInterface(%p)\n", this);
  242. return libAVPin_QueryInterface(pin, riid, ppvObject);
  243. }
  244. unsigned long WINAPI
  245. libAVMemInputPin_AddRef(libAVMemInputPin *this)
  246. {
  247. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  248. dshowdebug("libAVMemInputPin_AddRef(%p)\n", this);
  249. return libAVPin_AddRef(pin);
  250. }
  251. unsigned long WINAPI
  252. libAVMemInputPin_Release(libAVMemInputPin *this)
  253. {
  254. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  255. dshowdebug("libAVMemInputPin_Release(%p)\n", this);
  256. return libAVPin_Release(pin);
  257. }
  258. long WINAPI
  259. libAVMemInputPin_GetAllocator(libAVMemInputPin *this, IMemAllocator **alloc)
  260. {
  261. dshowdebug("libAVMemInputPin_GetAllocator(%p)\n", this);
  262. return VFW_E_NO_ALLOCATOR;
  263. }
  264. long WINAPI
  265. libAVMemInputPin_NotifyAllocator(libAVMemInputPin *this, IMemAllocator *alloc,
  266. BOOL rdwr)
  267. {
  268. dshowdebug("libAVMemInputPin_NotifyAllocator(%p)\n", this);
  269. return S_OK;
  270. }
  271. long WINAPI
  272. libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *this,
  273. ALLOCATOR_PROPERTIES *props)
  274. {
  275. dshowdebug("libAVMemInputPin_GetAllocatorRequirements(%p)\n", this);
  276. return E_NOTIMPL;
  277. }
  278. long WINAPI
  279. libAVMemInputPin_Receive(libAVMemInputPin *this, IMediaSample *sample)
  280. {
  281. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  282. enum dshowDeviceType devtype = pin->filter->type;
  283. void *priv_data;
  284. AVFormatContext *s;
  285. uint8_t *buf;
  286. int buf_size; /* todo should be a long? */
  287. int index;
  288. int64_t curtime;
  289. int64_t orig_curtime;
  290. int64_t graphtime;
  291. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  292. IReferenceClock *clock = pin->filter->clock;
  293. int64_t dummy;
  294. struct dshow_ctx *ctx;
  295. dshowdebug("libAVMemInputPin_Receive(%p)\n", this);
  296. if (!sample)
  297. return E_POINTER;
  298. IMediaSample_GetTime(sample, &orig_curtime, &dummy);
  299. orig_curtime += pin->filter->start_time;
  300. IReferenceClock_GetTime(clock, &graphtime);
  301. if (devtype == VideoDevice) {
  302. /* PTS from video devices is unreliable. */
  303. IReferenceClock_GetTime(clock, &curtime);
  304. } else {
  305. IMediaSample_GetTime(sample, &curtime, &dummy);
  306. if(curtime > 400000000000000000LL) {
  307. /* initial frames sometimes start < 0 (shown as a very large number here,
  308. like 437650244077016960 which FFmpeg doesn't like.
  309. TODO figure out math. For now just drop them. */
  310. av_log(NULL, AV_LOG_DEBUG,
  311. "dshow dropping initial (or ending) audio frame with odd PTS too high %"PRId64"\n", curtime);
  312. return S_OK;
  313. }
  314. curtime += pin->filter->start_time;
  315. }
  316. buf_size = IMediaSample_GetActualDataLength(sample);
  317. IMediaSample_GetPointer(sample, &buf);
  318. priv_data = pin->filter->priv_data;
  319. s = priv_data;
  320. ctx = s->priv_data;
  321. index = pin->filter->stream_index;
  322. av_log(NULL, AV_LOG_VERBOSE, "dshow passing through packet of type %s size %8d "
  323. "timestamp %"PRId64" orig timestamp %"PRId64" graph timestamp %"PRId64" diff %"PRId64" %s\n",
  324. devtypename, buf_size, curtime, orig_curtime, graphtime, graphtime - orig_curtime, ctx->device_name[devtype]);
  325. pin->filter->callback(priv_data, index, buf, buf_size, curtime, devtype);
  326. return S_OK;
  327. }
  328. long WINAPI
  329. libAVMemInputPin_ReceiveMultiple(libAVMemInputPin *this,
  330. IMediaSample **samples, long n, long *nproc)
  331. {
  332. int i;
  333. dshowdebug("libAVMemInputPin_ReceiveMultiple(%p)\n", this);
  334. for (i = 0; i < n; i++)
  335. libAVMemInputPin_Receive(this, samples[i]);
  336. *nproc = n;
  337. return S_OK;
  338. }
  339. long WINAPI
  340. libAVMemInputPin_ReceiveCanBlock(libAVMemInputPin *this)
  341. {
  342. dshowdebug("libAVMemInputPin_ReceiveCanBlock(%p)\n", this);
  343. /* I swear I will not block. */
  344. return S_FALSE;
  345. }
  346. void
  347. libAVMemInputPin_Destroy(libAVMemInputPin *this)
  348. {
  349. libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
  350. dshowdebug("libAVMemInputPin_Destroy(%p)\n", this);
  351. libAVPin_Destroy(pin);
  352. }