dshow_pin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "libavutil/mem.h"
  22. #include "dshow_capture.h"
  23. #include <stddef.h>
  24. #define imemoffset offsetof(DShowPin, imemvtbl)
  25. DECLARE_QUERYINTERFACE(pin, DShowPin,
  26. { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
  27. DECLARE_ADDREF(pin, DShowPin)
  28. DECLARE_RELEASE(pin, DShowPin)
  29. long WINAPI ff_dshow_pin_Connect(DShowPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
  30. {
  31. dshowdebug("ff_dshow_pin_Connect(%p, %p, %p)\n", this, pin, type);
  32. /* Input pins receive connections. */
  33. return S_FALSE;
  34. }
  35. long WINAPI ff_dshow_pin_ReceiveConnection(DShowPin *this, IPin *pin,
  36. const AM_MEDIA_TYPE *type)
  37. {
  38. enum dshowDeviceType devtype = this->filter->type;
  39. dshowdebug("ff_dshow_pin_ReceiveConnection(%p)\n", this);
  40. if (!pin)
  41. return E_POINTER;
  42. if (this->connectedto)
  43. return VFW_E_ALREADY_CONNECTED;
  44. ff_print_AM_MEDIA_TYPE(type);
  45. if (devtype == VideoDevice) {
  46. if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video))
  47. return VFW_E_TYPE_NOT_ACCEPTED;
  48. } else {
  49. if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
  50. return VFW_E_TYPE_NOT_ACCEPTED;
  51. }
  52. IPin_AddRef(pin);
  53. this->connectedto = pin;
  54. ff_copy_dshow_media_type(&this->type, type);
  55. return S_OK;
  56. }
  57. long WINAPI ff_dshow_pin_Disconnect(DShowPin *this)
  58. {
  59. dshowdebug("ff_dshow_pin_Disconnect(%p)\n", this);
  60. if (this->filter->state != State_Stopped)
  61. return VFW_E_NOT_STOPPED;
  62. if (!this->connectedto)
  63. return S_FALSE;
  64. IPin_Release(this->connectedto);
  65. this->connectedto = NULL;
  66. return S_OK;
  67. }
  68. long WINAPI ff_dshow_pin_ConnectedTo(DShowPin *this, IPin **pin)
  69. {
  70. dshowdebug("ff_dshow_pin_ConnectedTo(%p)\n", this);
  71. if (!pin)
  72. return E_POINTER;
  73. if (!this->connectedto)
  74. return VFW_E_NOT_CONNECTED;
  75. IPin_AddRef(this->connectedto);
  76. *pin = this->connectedto;
  77. return S_OK;
  78. }
  79. long WINAPI ff_dshow_pin_ConnectionMediaType(DShowPin *this, AM_MEDIA_TYPE *type)
  80. {
  81. dshowdebug("ff_dshow_pin_ConnectionMediaType(%p)\n", this);
  82. if (!type)
  83. return E_POINTER;
  84. if (!this->connectedto)
  85. return VFW_E_NOT_CONNECTED;
  86. return ff_copy_dshow_media_type(type, &this->type);
  87. }
  88. long WINAPI ff_dshow_pin_QueryPinInfo(DShowPin *this, PIN_INFO *info)
  89. {
  90. dshowdebug("ff_dshow_pin_QueryPinInfo(%p)\n", this);
  91. if (!info)
  92. return E_POINTER;
  93. if (this->filter)
  94. ff_dshow_filter_AddRef(this->filter);
  95. info->pFilter = (IBaseFilter *) this->filter;
  96. info->dir = PINDIR_INPUT;
  97. wcscpy(info->achName, L"Capture");
  98. return S_OK;
  99. }
  100. long WINAPI ff_dshow_pin_QueryDirection(DShowPin *this, PIN_DIRECTION *dir)
  101. {
  102. dshowdebug("ff_dshow_pin_QueryDirection(%p)\n", this);
  103. if (!dir)
  104. return E_POINTER;
  105. *dir = PINDIR_INPUT;
  106. return S_OK;
  107. }
  108. long WINAPI ff_dshow_pin_QueryId(DShowPin *this, wchar_t **id)
  109. {
  110. dshowdebug("ff_dshow_pin_QueryId(%p)\n", this);
  111. if (!id)
  112. return E_POINTER;
  113. *id = wcsdup(L"libAV Pin");
  114. return S_OK;
  115. }
  116. long WINAPI ff_dshow_pin_QueryAccept(DShowPin *this, const AM_MEDIA_TYPE *type)
  117. {
  118. dshowdebug("ff_dshow_pin_QueryAccept(%p)\n", this);
  119. return S_FALSE;
  120. }
  121. long WINAPI ff_dshow_pin_EnumMediaTypes(DShowPin *this, IEnumMediaTypes **enumtypes)
  122. {
  123. const AM_MEDIA_TYPE *type = NULL;
  124. DShowEnumMediaTypes *new;
  125. dshowdebug("ff_dshow_pin_EnumMediaTypes(%p)\n", this);
  126. if (!enumtypes)
  127. return E_POINTER;
  128. new = ff_dshow_enummediatypes_Create(type);
  129. if (!new)
  130. return E_OUTOFMEMORY;
  131. *enumtypes = (IEnumMediaTypes *) new;
  132. return S_OK;
  133. }
  134. long WINAPI ff_dshow_pin_QueryInternalConnections(DShowPin *this, IPin **pin,
  135. unsigned long *npin)
  136. {
  137. dshowdebug("ff_dshow_pin_QueryInternalConnections(%p)\n", this);
  138. return E_NOTIMPL;
  139. }
  140. long WINAPI ff_dshow_pin_EndOfStream(DShowPin *this)
  141. {
  142. dshowdebug("ff_dshow_pin_EndOfStream(%p)\n", this);
  143. /* I don't care. */
  144. return S_OK;
  145. }
  146. long WINAPI ff_dshow_pin_BeginFlush(DShowPin *this)
  147. {
  148. dshowdebug("ff_dshow_pin_BeginFlush(%p)\n", this);
  149. /* I don't care. */
  150. return S_OK;
  151. }
  152. long WINAPI ff_dshow_pin_EndFlush(DShowPin *this)
  153. {
  154. dshowdebug("ff_dshow_pin_EndFlush(%p)\n", this);
  155. /* I don't care. */
  156. return S_OK;
  157. }
  158. long WINAPI ff_dshow_pin_NewSegment(DShowPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
  159. double rate)
  160. {
  161. dshowdebug("ff_dshow_pin_NewSegment(%p)\n", this);
  162. /* I don't care. */
  163. return S_OK;
  164. }
  165. static int ff_dshow_pin_Setup(DShowPin *this, DShowFilter *filter)
  166. {
  167. IPinVtbl *vtbl = this->vtbl;
  168. IMemInputPinVtbl *imemvtbl;
  169. if (!filter)
  170. return 0;
  171. imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
  172. if (!imemvtbl)
  173. return 0;
  174. SETVTBL(imemvtbl, meminputpin, QueryInterface);
  175. SETVTBL(imemvtbl, meminputpin, AddRef);
  176. SETVTBL(imemvtbl, meminputpin, Release);
  177. SETVTBL(imemvtbl, meminputpin, GetAllocator);
  178. SETVTBL(imemvtbl, meminputpin, NotifyAllocator);
  179. SETVTBL(imemvtbl, meminputpin, GetAllocatorRequirements);
  180. SETVTBL(imemvtbl, meminputpin, Receive);
  181. SETVTBL(imemvtbl, meminputpin, ReceiveMultiple);
  182. SETVTBL(imemvtbl, meminputpin, ReceiveCanBlock);
  183. this->imemvtbl = imemvtbl;
  184. SETVTBL(vtbl, pin, QueryInterface);
  185. SETVTBL(vtbl, pin, AddRef);
  186. SETVTBL(vtbl, pin, Release);
  187. SETVTBL(vtbl, pin, Connect);
  188. SETVTBL(vtbl, pin, ReceiveConnection);
  189. SETVTBL(vtbl, pin, Disconnect);
  190. SETVTBL(vtbl, pin, ConnectedTo);
  191. SETVTBL(vtbl, pin, ConnectionMediaType);
  192. SETVTBL(vtbl, pin, QueryPinInfo);
  193. SETVTBL(vtbl, pin, QueryDirection);
  194. SETVTBL(vtbl, pin, QueryId);
  195. SETVTBL(vtbl, pin, QueryAccept);
  196. SETVTBL(vtbl, pin, EnumMediaTypes);
  197. SETVTBL(vtbl, pin, QueryInternalConnections);
  198. SETVTBL(vtbl, pin, EndOfStream);
  199. SETVTBL(vtbl, pin, BeginFlush);
  200. SETVTBL(vtbl, pin, EndFlush);
  201. SETVTBL(vtbl, pin, NewSegment);
  202. this->filter = filter;
  203. return 1;
  204. }
  205. static void ff_dshow_pin_Free(DShowPin *this)
  206. {
  207. if (!this)
  208. return;
  209. av_freep(&this->imemvtbl);
  210. if (this->type.pbFormat) {
  211. CoTaskMemFree(this->type.pbFormat);
  212. this->type.pbFormat = NULL;
  213. }
  214. }
  215. DECLARE_CREATE(pin, DShowPin, ff_dshow_pin_Setup(this, filter), DShowFilter *filter)
  216. DECLARE_DESTROY(pin, DShowPin, ff_dshow_pin_Free)
  217. /*****************************************************************************
  218. * DShowMemInputPin
  219. ****************************************************************************/
  220. long WINAPI ff_dshow_meminputpin_QueryInterface(DShowMemInputPin *this, const GUID *riid,
  221. void **ppvObject)
  222. {
  223. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  224. dshowdebug("ff_dshow_meminputpin_QueryInterface(%p)\n", this);
  225. return ff_dshow_pin_QueryInterface(pin, riid, ppvObject);
  226. }
  227. unsigned long WINAPI ff_dshow_meminputpin_AddRef(DShowMemInputPin *this)
  228. {
  229. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  230. dshowdebug("ff_dshow_meminputpin_AddRef(%p)\n", this);
  231. return ff_dshow_pin_AddRef(pin);
  232. }
  233. unsigned long WINAPI ff_dshow_meminputpin_Release(DShowMemInputPin *this)
  234. {
  235. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  236. dshowdebug("ff_dshow_meminputpin_Release(%p)\n", this);
  237. return ff_dshow_pin_Release(pin);
  238. }
  239. long WINAPI ff_dshow_meminputpin_GetAllocator(DShowMemInputPin *this, IMemAllocator **alloc)
  240. {
  241. dshowdebug("ff_dshow_meminputpin_GetAllocator(%p)\n", this);
  242. return VFW_E_NO_ALLOCATOR;
  243. }
  244. long WINAPI ff_dshow_meminputpin_NotifyAllocator(DShowMemInputPin *this, IMemAllocator *alloc,
  245. BOOL rdwr)
  246. {
  247. dshowdebug("ff_dshow_meminputpin_NotifyAllocator(%p)\n", this);
  248. return S_OK;
  249. }
  250. long WINAPI ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *this,
  251. ALLOCATOR_PROPERTIES *props)
  252. {
  253. dshowdebug("ff_dshow_meminputpin_GetAllocatorRequirements(%p)\n", this);
  254. return E_NOTIMPL;
  255. }
  256. long WINAPI ff_dshow_meminputpin_Receive(DShowMemInputPin *this, IMediaSample *sample)
  257. {
  258. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  259. enum dshowDeviceType devtype = pin->filter->type;
  260. void *priv_data;
  261. AVFormatContext *s;
  262. uint8_t *buf;
  263. int buf_size; /* todo should be a long? */
  264. int index;
  265. int64_t chosentime = 0;
  266. int64_t sampletime = 0;
  267. int64_t graphtime = 0;
  268. int use_sample_time = 1;
  269. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  270. IReferenceClock *clock = pin->filter->clock;
  271. int64_t dummy;
  272. struct dshow_ctx *ctx;
  273. HRESULT hr;
  274. dshowdebug("ff_dshow_meminputpin_Receive(%p)\n", this);
  275. if (!sample)
  276. return E_POINTER;
  277. priv_data = pin->filter->priv_data;
  278. s = priv_data;
  279. ctx = s->priv_data;
  280. hr = IMediaSample_GetTime(sample, &sampletime, &dummy);
  281. IReferenceClock_GetTime(clock, &graphtime);
  282. if (devtype == VideoDevice && !ctx->use_video_device_timestamps) {
  283. /* PTS from video devices is unreliable. */
  284. chosentime = graphtime;
  285. use_sample_time = 0;
  286. } else {
  287. if (hr == VFW_E_SAMPLE_TIME_NOT_SET || sampletime == 0) {
  288. chosentime = graphtime;
  289. use_sample_time = 0;
  290. av_log(s, AV_LOG_DEBUG,
  291. "frame with missing sample timestamp encountered, falling back to graph timestamp\n");
  292. }
  293. else if (sampletime > 400000000000000000LL) {
  294. /* initial frames sometimes start < 0 (shown as a very large number here,
  295. like 437650244077016960 which FFmpeg doesn't like).
  296. TODO figure out math. For now just drop them. */
  297. av_log(s, AV_LOG_DEBUG,
  298. "dropping initial (or ending) sample with odd PTS too high %"PRId64"\n", sampletime);
  299. return S_OK;
  300. } else
  301. chosentime = sampletime;
  302. }
  303. // media sample time is relative to graph start time
  304. sampletime += pin->filter->start_time;
  305. if (use_sample_time)
  306. chosentime += pin->filter->start_time;
  307. buf_size = IMediaSample_GetActualDataLength(sample);
  308. IMediaSample_GetPointer(sample, &buf);
  309. index = pin->filter->stream_index;
  310. av_log(s, AV_LOG_VERBOSE, "passing through packet of type %s size %8d "
  311. "timestamp %"PRId64" orig timestamp %"PRId64" graph timestamp %"PRId64" diff %"PRId64" %s\n",
  312. devtypename, buf_size, chosentime, sampletime, graphtime, graphtime - sampletime, ctx->device_name[devtype]);
  313. pin->filter->callback(priv_data, index, buf, buf_size, chosentime, devtype);
  314. return S_OK;
  315. }
  316. long WINAPI ff_dshow_meminputpin_ReceiveMultiple(DShowMemInputPin *this,
  317. IMediaSample **samples, long n, long *nproc)
  318. {
  319. int i;
  320. dshowdebug("ff_dshow_meminputpin_ReceiveMultiple(%p)\n", this);
  321. for (i = 0; i < n; i++)
  322. ff_dshow_meminputpin_Receive(this, samples[i]);
  323. *nproc = n;
  324. return S_OK;
  325. }
  326. long WINAPI ff_dshow_meminputpin_ReceiveCanBlock(DShowMemInputPin *this)
  327. {
  328. dshowdebug("ff_dshow_meminputpin_ReceiveCanBlock(%p)\n", this);
  329. /* I swear I will not block. */
  330. return S_FALSE;
  331. }
  332. void ff_dshow_meminputpin_Destroy(DShowMemInputPin *this)
  333. {
  334. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  335. dshowdebug("ff_dshow_meminputpin_Destroy(%p)\n", this);
  336. ff_dshow_pin_Destroy(pin);
  337. }