• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2007 Sebastien Moutte <sebastien@moutte.net>
3  *
4  * gstdshow.cpp:
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include <gst/video/video-format.h>
23 
24 #include "gstdshow.h"
25 #include "gstdshowfakesink.h"
26 #include "gstdshowvideosrc.h"
27 
28 GST_DEBUG_CATEGORY_EXTERN (dshowsrcwrapper_debug);
29 #define GST_CAT_DEFAULT dshowsrcwrapper_debug
30 
31 gchar *
wchar_to_gchar(WCHAR * w)32 wchar_to_gchar (WCHAR * w)
33 {
34   return g_utf16_to_utf8 ((const gunichar2 *) w, wcslen (w), NULL, NULL, NULL);
35 }
36 
37 const GUID MEDIASUBTYPE_I420
38     = { 0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B,
39     0x71}
40 };
41 
42 void
gst_dshow_free_mediatype(AM_MEDIA_TYPE * pmt)43 gst_dshow_free_mediatype (AM_MEDIA_TYPE * pmt)
44 {
45   if (pmt != NULL) {
46     if (pmt->cbFormat != 0) {
47       CoTaskMemFree ((PVOID) pmt->pbFormat);
48       pmt->cbFormat = 0;
49       pmt->pbFormat = NULL;
50     }
51     if (pmt->pUnk != NULL) {
52       /* Unnecessary because pUnk should not be used, but safest. */
53       pmt->pUnk->Release ();
54       pmt->pUnk = NULL;
55     }
56 
57     CoTaskMemFree (pmt);
58   }
59 }
60 
61 void
gst_dshow_free_pin_mediatype(gpointer pt)62 gst_dshow_free_pin_mediatype (gpointer pt)
63 {
64   GstCapturePinMediaType *pin_mediatype = (GstCapturePinMediaType *) pt;
65   if (pin_mediatype) {
66     if (pin_mediatype->capture_pin) {
67       pin_mediatype->capture_pin->Release ();
68       pin_mediatype->capture_pin = NULL;
69     }
70     if (pin_mediatype->mediatype) {
71       gst_dshow_free_mediatype (pin_mediatype->mediatype);
72       pin_mediatype->mediatype = NULL;
73     }
74   }
75 }
76 
77 GstCapturePinMediaType *
gst_dshow_new_pin_mediatype(IPin * pin)78 gst_dshow_new_pin_mediatype (IPin * pin)
79 {
80   GstCapturePinMediaType *pin_mediatype = g_new0 (GstCapturePinMediaType, 1);
81 
82   pin->AddRef ();
83   pin_mediatype->capture_pin = pin;
84 
85   return pin_mediatype;
86 }
87 
88 GstCapturePinMediaType *
gst_dshow_new_pin_mediatype_from_enum_mediatypes(IPin * pin,IEnumMediaTypes * enum_mediatypes)89 gst_dshow_new_pin_mediatype_from_enum_mediatypes (IPin * pin, IEnumMediaTypes *enum_mediatypes)
90 {
91   GstCapturePinMediaType *pin_mediatype = gst_dshow_new_pin_mediatype (pin);
92   VIDEOINFOHEADER *video_info = NULL;
93 
94   HRESULT hres = enum_mediatypes->Next (1, &pin_mediatype->mediatype, NULL);
95   if (hres != S_OK || !pin_mediatype->mediatype) {
96     gst_dshow_free_pin_mediatype (pin_mediatype);
97     return NULL;
98   }
99 
100   video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat;
101 
102   pin_mediatype->defaultWidth = video_info->bmiHeader.biWidth;
103   pin_mediatype->defaultHeight = video_info->bmiHeader.biHeight;
104   pin_mediatype->defaultFPS = (gint) (10000000 / video_info->AvgTimePerFrame);
105   pin_mediatype->granularityWidth = 1;
106   pin_mediatype->granularityHeight = 1;
107 
108   return pin_mediatype;
109 }
110 
111 GstCapturePinMediaType *
gst_dshow_new_pin_mediatype_from_streamcaps(IPin * pin,gint id,IAMStreamConfig * streamcaps)112 gst_dshow_new_pin_mediatype_from_streamcaps (IPin * pin, gint id, IAMStreamConfig * streamcaps)
113 {
114   GstCapturePinMediaType *pin_mediatype = gst_dshow_new_pin_mediatype (pin);
115   VIDEOINFOHEADER *video_info = NULL;
116 
117   HRESULT hres = streamcaps->GetStreamCaps (id, &pin_mediatype->mediatype,
118       (BYTE *) & pin_mediatype->vscc);
119   if (FAILED (hres) || !pin_mediatype->mediatype) {
120     gst_dshow_free_pin_mediatype (pin_mediatype);
121     return NULL;
122   }
123 
124   video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat;
125 
126   pin_mediatype->defaultWidth = video_info->bmiHeader.biWidth;
127   pin_mediatype->defaultHeight = video_info->bmiHeader.biHeight;
128   pin_mediatype->defaultFPS = (gint) (10000000 / video_info->AvgTimePerFrame);
129   pin_mediatype->granularityWidth = pin_mediatype->vscc.OutputGranularityX;
130   pin_mediatype->granularityHeight = pin_mediatype->vscc.OutputGranularityY;
131 
132   return pin_mediatype;
133 }
134 
135 void
gst_dshow_free_pins_mediatypes(GList * pins_mediatypes)136 gst_dshow_free_pins_mediatypes (GList * pins_mediatypes)
137 {
138   g_list_free_full (pins_mediatypes,
139       (GDestroyNotify) gst_dshow_free_pin_mediatype);
140 }
141 
142 gboolean
gst_dshow_check_mediatype(AM_MEDIA_TYPE * media_type,const GUID sub_type,const GUID format_type)143 gst_dshow_check_mediatype (AM_MEDIA_TYPE * media_type, const GUID sub_type,
144     const GUID format_type)
145 {
146   RPC_STATUS rpcstatus;
147 
148   g_return_val_if_fail (media_type != NULL, FALSE);
149 
150   return
151       UuidCompare (&media_type->subtype, (UUID *) & sub_type,
152       &rpcstatus) == 0 && rpcstatus == RPC_S_OK &&
153       //IsEqualGUID (&media_type->subtype, &sub_type)
154       UuidCompare (&media_type->formattype, (UUID *) & format_type,
155       &rpcstatus) == 0 && rpcstatus == RPC_S_OK;
156 }
157 
158 gboolean
gst_dshow_get_pin_from_filter(IBaseFilter * filter,PIN_DIRECTION pindir,IPin ** pin)159 gst_dshow_get_pin_from_filter (IBaseFilter * filter, PIN_DIRECTION pindir,
160     IPin ** pin)
161 {
162   gboolean ret = FALSE;
163   IEnumPins *enumpins = NULL;
164   IPin *pintmp = NULL;
165   HRESULT hres;
166   *pin = NULL;
167 
168   hres = filter->EnumPins (&enumpins);
169   if (FAILED (hres)) {
170     return ret;
171   }
172 
173   while (enumpins->Next (1, &pintmp, NULL) == S_OK) {
174     PIN_DIRECTION pindirtmp;
175     hres = pintmp->QueryDirection (&pindirtmp);
176     if (hres == S_OK && pindir == pindirtmp) {
177       *pin = pintmp;
178       ret = TRUE;
179       break;
180     }
181     pintmp->Release ();
182   }
183   enumpins->Release ();
184 
185   return ret;
186 }
187 
188 gboolean
gst_dshow_find_filter(CLSID input_majortype,CLSID input_subtype,CLSID output_majortype,CLSID output_subtype,gchar * prefered_filter_name,IBaseFilter ** filter)189 gst_dshow_find_filter (CLSID input_majortype, CLSID input_subtype,
190     CLSID output_majortype, CLSID output_subtype,
191     gchar * prefered_filter_name, IBaseFilter ** filter)
192 {
193   gboolean ret = FALSE;
194   HRESULT hres;
195   GUID arrayInTypes[2];
196   GUID arrayOutTypes[2];
197   IFilterMapper2 *mapper = NULL;
198   IEnumMoniker *enum_moniker = NULL;
199   IMoniker *moniker = NULL;
200   ULONG fetched;
201   gchar *prefered_filter_upper = NULL;
202   gboolean exit = FALSE;
203 
204   /* initialize output parameter */
205   if (filter)
206     *filter = NULL;
207 
208   /* create a private copy of preferred filter substring in upper case */
209   if (prefered_filter_name) {
210     prefered_filter_upper = g_strdup (prefered_filter_name);
211     _strupr (prefered_filter_upper);
212   }
213 
214   hres = CoCreateInstance (CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
215       IID_IFilterMapper2, (void **) &mapper);
216   if (FAILED (hres))
217     goto clean;
218 
219   memcpy (&arrayInTypes[0], &input_majortype, sizeof (CLSID));
220   memcpy (&arrayInTypes[1], &input_subtype, sizeof (CLSID));
221   memcpy (&arrayOutTypes[0], &output_majortype, sizeof (CLSID));
222   memcpy (&arrayOutTypes[1], &output_subtype, sizeof (CLSID));
223 
224   hres =
225       mapper->EnumMatchingFilters (&enum_moniker, 0, FALSE,
226       MERIT_DO_NOT_USE + 1, TRUE, 1, arrayInTypes, NULL, NULL, FALSE, TRUE, 1,
227       arrayOutTypes, NULL, NULL);
228   if (FAILED (hres))
229     goto clean;
230 
231   enum_moniker->Reset ();
232 
233   while (hres = enum_moniker->Next (1, &moniker, &fetched), hres == S_OK
234       && !exit) {
235     IBaseFilter *filter_temp = NULL;
236     IPropertyBag *property_bag = NULL;
237     gchar *friendly_name = NULL;
238 
239     hres =
240         moniker->BindToStorage (NULL, NULL, IID_IPropertyBag,
241         (void **) &property_bag);
242     if (SUCCEEDED (hres) && property_bag) {
243       VARIANT varFriendlyName;
244       VariantInit (&varFriendlyName);
245 
246       hres = property_bag->Read (L"FriendlyName", &varFriendlyName, NULL);
247       if (hres == S_OK && varFriendlyName.bstrVal) {
248         friendly_name = wchar_to_gchar (varFriendlyName.bstrVal);
249         if (friendly_name)
250           _strupr (friendly_name);
251         SysFreeString (varFriendlyName.bstrVal);
252       }
253       property_bag->Release ();
254     }
255 
256     hres =
257         moniker->BindToObject (NULL, NULL, IID_IBaseFilter,
258         (void **) &filter_temp);
259     if (SUCCEEDED (hres) && filter_temp) {
260       ret = TRUE;
261       if (filter) {
262         if (*filter)
263           (*filter)->Release ();
264 
265         *filter = filter_temp;
266         (*filter)->AddRef ();
267 
268         if (prefered_filter_upper && friendly_name &&
269             strstr (friendly_name, prefered_filter_upper))
270           exit = TRUE;
271       }
272 
273       /* if we just want to know if the formats are supported OR
274          if we don't care about what will be the filter used
275          => we can stop enumeration */
276       if (!filter || !prefered_filter_upper)
277         exit = TRUE;
278 
279       filter_temp->Release ();
280     }
281 
282     g_free (friendly_name);
283     moniker->Release ();
284   }
285 
286 clean:
287   g_free (prefered_filter_upper);
288   if (enum_moniker)
289     enum_moniker->Release ();
290   if (mapper)
291     mapper->Release ();
292 
293   return ret;
294 }
295 
296 void
gst_dshow_device_entry_free(DshowDeviceEntry * entry)297 gst_dshow_device_entry_free (DshowDeviceEntry * entry)
298 {
299   if (entry) {
300     g_free (entry->device);
301     entry->device = NULL;
302     g_free (entry->device_name);
303     entry->device_name = NULL;
304     if (entry->caps) {
305       gst_caps_unref (entry->caps);
306       entry->caps = NULL;
307     }
308     if (entry->moniker) {
309       entry->moniker->Release ();
310       entry->moniker = NULL;
311     }
312   }
313 }
314 
315 void
gst_dshow_device_list_free(GList * devices)316 gst_dshow_device_list_free (GList * devices)
317 {
318   GList *cur;
319 
320   for (cur = devices; cur != NULL; cur = cur->next)
321     gst_dshow_device_entry_free ((DshowDeviceEntry *) cur->data);
322 
323   g_list_free (devices);
324 }
325 
326 GList *
gst_dshow_enumerate_devices(const GUID * device_category,gboolean getcaps)327 gst_dshow_enumerate_devices (const GUID * device_category, gboolean getcaps)
328 {
329   GList *result = NULL;
330   ICreateDevEnum *devices_enum = NULL;
331   IEnumMoniker *enum_moniker = NULL;
332   IMoniker *moniker = NULL;
333   HRESULT hres = S_FALSE;
334   ULONG fetched;
335   gint devidx = -1;
336 
337   hres = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
338       IID_ICreateDevEnum, (void **) &devices_enum);
339   if (hres != S_OK) {
340     GST_ERROR ("Failed to create System Device Enumerator");
341     goto clean;
342   }
343 
344   hres = devices_enum->CreateClassEnumerator (*device_category,
345       &enum_moniker, 0);
346   if (hres != S_OK || !enum_moniker) {
347     GST_ERROR ("Failed to create audio/video class device enumerator");
348     goto clean;
349   }
350 
351   enum_moniker->Reset ();
352 
353   while (enum_moniker->Next (1, &moniker, &fetched) == S_OK) {
354     IPropertyBag *property_bag = NULL;
355     hres = moniker->BindToStorage (NULL, NULL, IID_IPropertyBag,
356         (void **) &property_bag);
357     if (SUCCEEDED (hres) && property_bag) {
358       VARIANT varFriendlyName;
359       VariantInit (&varFriendlyName);
360 
361       hres = property_bag->Read (L"FriendlyName", &varFriendlyName, NULL);
362       if (hres == S_OK && varFriendlyName.bstrVal) {
363         gchar *friendly_name = wchar_to_gchar (varFriendlyName.bstrVal);
364 
365         devidx++;
366         GST_DEBUG ("Found device idx=%d: device-name='%s'",
367             devidx, friendly_name);
368 
369         WCHAR *wszDisplayName = NULL;
370         hres = moniker->GetDisplayName (NULL, NULL, &wszDisplayName);
371         if (hres == S_OK && wszDisplayName) {
372           DshowDeviceEntry *entry = g_new0 (DshowDeviceEntry, 1);
373           gchar *device_path = NULL;
374           GstCaps *caps = NULL;
375 
376           device_path = wchar_to_gchar (wszDisplayName);
377           CoTaskMemFree (wszDisplayName);
378 
379           /* getting caps can be slow, so make it optional when enumerating */
380           if (getcaps) {
381             IBindCtx *lpbc = NULL;
382             hres = CreateBindCtx (0, &lpbc);
383             if (SUCCEEDED (hres)) {
384               IBaseFilter *video_cap_filter = NULL;
385               hres = moniker->BindToObject (lpbc, NULL, IID_IBaseFilter,
386                 (LPVOID *) & video_cap_filter);
387               if (video_cap_filter) {
388                 caps = gst_dshowvideosrc_getcaps_from_capture_filter (video_cap_filter, NULL);
389                 video_cap_filter->Release ();
390               }
391               lpbc->Release ();
392             }
393           }
394 
395           entry->device = device_path;
396           entry->device_name = friendly_name;
397           entry->device_index = devidx;
398           entry->caps = caps;
399           entry->moniker = moniker;
400           moniker = NULL;
401           result = g_list_append (result, entry);
402         } else {
403           g_free (friendly_name);
404         }
405         SysFreeString (varFriendlyName.bstrVal);
406       }
407       property_bag->Release ();
408     }
409     if (moniker) {
410       moniker->Release ();
411     }
412   }
413 
414 clean:
415   if (enum_moniker) {
416     enum_moniker->Release ();
417   }
418 
419   if (devices_enum) {
420     devices_enum->Release ();
421   }
422 
423   return result;
424 }
425 
426 DshowDeviceEntry *
gst_dshow_select_device(const GUID * device_category,const gchar * device,const gchar * device_name,const gint device_index)427 gst_dshow_select_device (const GUID * device_category,
428     const gchar * device, const gchar * device_name, const gint device_index)
429 {
430   GList *devices = NULL;
431   GList *item = NULL;
432   DshowDeviceEntry *selected = NULL;
433 
434   GST_DEBUG ("Trying to select device-index=%d, device-name='%s', device='%s'",
435     device_index, device_name, device);
436 
437   devices = gst_dshow_enumerate_devices (&CLSID_VideoInputDeviceCategory, FALSE);
438 
439   for (item = devices; item != NULL; item = item->next) {
440     DshowDeviceEntry *entry = (DshowDeviceEntry *) item->data;
441 
442     /* device will be used first, then device-name, then device-index */
443     if (device && g_strcmp0 (device, entry->device) == 0) {
444       selected = entry;
445       break;
446     } else if (!device && device_name && g_strcmp0 (device_name, entry->device_name) == 0) {
447       selected = entry;
448       break;
449     } else if (!device && !device_name && device_index == entry->device_index) {
450       selected = entry;
451       break;
452     }
453   }
454 
455   if (selected) {
456     devices = g_list_remove (devices, selected);
457     GST_DEBUG ("Selected device-index=%d, device-name='%s', device='%s'",
458       selected->device_index, selected->device_name, selected->device);
459   } else {
460     GST_DEBUG ("No matching device found");
461   }
462 
463   gst_dshow_device_list_free (devices);
464 
465   return selected;
466 }
467 
468 IBaseFilter *
gst_dshow_create_capture_filter(IMoniker * moniker)469 gst_dshow_create_capture_filter (IMoniker *moniker)
470 {
471   HRESULT hres = S_OK;
472   IBindCtx *lpbc = NULL;
473   IBaseFilter *video_cap_filter = NULL;
474 
475   g_assert (moniker != NULL);
476 
477   hres = CreateBindCtx (0, &lpbc);
478   if (SUCCEEDED (hres)) {
479     hres = moniker->BindToObject (lpbc, NULL, IID_IBaseFilter,
480         (LPVOID *) & video_cap_filter);
481     lpbc->Release ();
482   }
483 
484   return video_cap_filter;
485 }
486 
487 gchar *
gst_dshow_getdevice_from_devicename(const GUID * device_category,gchar ** device_name,gint * device_index)488 gst_dshow_getdevice_from_devicename (const GUID * device_category,
489     gchar ** device_name, gint * device_index)
490 {
491   gchar *ret = NULL;
492   ICreateDevEnum *devices_enum = NULL;
493   IEnumMoniker *enum_moniker = NULL;
494   IMoniker *moniker = NULL;
495   HRESULT hres = S_FALSE;
496   ULONG fetched;
497   gboolean bfound = FALSE;
498   gint devidx = -1;
499 
500   hres = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
501       IID_ICreateDevEnum, (void **) &devices_enum);
502   if (hres != S_OK) {
503     GST_ERROR ("Failed to create System Device Enumerator");
504     goto clean;
505   }
506 
507   hres = devices_enum->CreateClassEnumerator (*device_category,
508       &enum_moniker, 0);
509   if (hres != S_OK || !enum_moniker) {
510     GST_ERROR ("Failed to create audio/video class device enumerator");
511     goto clean;
512   }
513 
514   enum_moniker->Reset ();
515 
516   while (hres = enum_moniker->Next (1, &moniker, &fetched), hres == S_OK
517       && !bfound) {
518     IPropertyBag *property_bag = NULL;
519     hres =
520         moniker->BindToStorage (NULL, NULL, IID_IPropertyBag,
521         (void **) &property_bag);
522     if (SUCCEEDED (hres) && property_bag) {
523       VARIANT varFriendlyName;
524       VariantInit (&varFriendlyName);
525 
526       hres = property_bag->Read (L"FriendlyName", &varFriendlyName, NULL);
527       if (hres == S_OK && varFriendlyName.bstrVal) {
528         gchar *friendly_name = wchar_to_gchar (varFriendlyName.bstrVal);
529 
530         devidx++;
531         GST_DEBUG ("Found device idx=%d: device-name='%s'",
532             devidx, friendly_name);
533 
534         if ((!*device_name || !**device_name) && devidx == *device_index) {
535           g_free (*device_name);
536           *device_name = g_strdup (friendly_name);
537         }
538 
539         if ((*device_name && **device_name)
540             && _stricmp (*device_name, friendly_name) == 0) {
541           WCHAR *wszDisplayName = NULL;
542           hres = moniker->GetDisplayName (NULL, NULL, &wszDisplayName);
543           if (hres == S_OK && wszDisplayName) {
544             *device_index = devidx;
545             ret = wchar_to_gchar (wszDisplayName);
546             CoTaskMemFree (wszDisplayName);
547           }
548           bfound = TRUE;
549         }
550         SysFreeString (varFriendlyName.bstrVal);
551       }
552       property_bag->Release ();
553     }
554     moniker->Release ();
555   }
556 
557 clean:
558   if (enum_moniker) {
559     enum_moniker->Release ();
560   }
561 
562   if (devices_enum) {
563     devices_enum->Release ();
564   }
565 
566   return ret;
567 }
568 
569 gboolean
gst_dshow_show_propertypage(IBaseFilter * base_filter)570 gst_dshow_show_propertypage (IBaseFilter * base_filter)
571 {
572   gboolean ret = FALSE;
573   ISpecifyPropertyPages *pProp = NULL;
574   HRESULT hres =
575       base_filter->QueryInterface (IID_ISpecifyPropertyPages, (void **) &pProp);
576   if (SUCCEEDED (hres)) {
577     /* Get the filter's name and IUnknown pointer. */
578     FILTER_INFO FilterInfo;
579     CAUUID caGUID;
580     IUnknown *pFilterUnk = NULL;
581     hres = base_filter->QueryFilterInfo (&FilterInfo);
582     base_filter->QueryInterface (IID_IUnknown, (void **) &pFilterUnk);
583 
584     /* Show the page. */
585     pProp->GetPages (&caGUID);
586     pProp->Release ();
587     OleCreatePropertyFrame (GetDesktopWindow (), 0, 0, FilterInfo.achName,
588         1, &pFilterUnk, caGUID.cElems, caGUID.pElems, 0, 0, NULL);
589 
590     pFilterUnk->Release ();
591     FilterInfo.pGraph->Release ();
592     CoTaskMemFree (caGUID.pElems);
593   }
594   return ret;
595 }
596 
597 GstVideoFormat
gst_dshow_guid_to_gst_video_format(AM_MEDIA_TYPE * mediatype)598 gst_dshow_guid_to_gst_video_format (AM_MEDIA_TYPE *mediatype)
599 {
600   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_I420, FORMAT_VideoInfo))
601     return GST_VIDEO_FORMAT_I420;
602 
603   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_RGB24, FORMAT_VideoInfo))
604     return GST_VIDEO_FORMAT_BGR;
605 
606   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_YUY2, FORMAT_VideoInfo))
607     return GST_VIDEO_FORMAT_YUY2;
608 
609   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_UYVY, FORMAT_VideoInfo))
610     return GST_VIDEO_FORMAT_UYVY;
611 
612   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_RGB32, FORMAT_VideoInfo))
613     return GST_VIDEO_FORMAT_BGRx;
614 
615   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_RGB565, FORMAT_VideoInfo))
616     return GST_VIDEO_FORMAT_BGR16;
617 
618   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_RGB555, FORMAT_VideoInfo))
619     return GST_VIDEO_FORMAT_BGR15;
620 
621   if (gst_dshow_check_mediatype (mediatype, MEDIASUBTYPE_RGB8, FORMAT_VideoInfo))
622     return GST_VIDEO_FORMAT_GRAY8;
623 
624   return GST_VIDEO_FORMAT_UNKNOWN;
625 }
626 
627 gboolean
gst_dshow_is_pin_connected(IPin * pin)628 gst_dshow_is_pin_connected (IPin * pin)
629 {
630   IPin *tmp_pin = NULL;
631   gboolean res;
632   HRESULT hres;
633 
634   g_assert (pin);
635   hres = pin->ConnectedTo (&tmp_pin);
636   res = (hres != VFW_E_NOT_CONNECTED);
637   if (tmp_pin)
638     tmp_pin->Release ();
639 
640   return res;
641 }
642 
643 GstCaps *
gst_dshow_new_video_caps(GstVideoFormat video_format,const gchar * name,GstCapturePinMediaType * pin_mediatype)644 gst_dshow_new_video_caps (GstVideoFormat video_format, const gchar * name,
645     GstCapturePinMediaType * pin_mediatype)
646 {
647   GstCaps *video_caps = NULL;
648   GstStructure *video_structure = NULL;
649   gint min_w, max_w;
650   gint min_h, max_h;
651   gint min_fr, max_fr;
652 
653   /* raw video format */
654   switch (video_format) {
655     case GST_VIDEO_FORMAT_BGR:
656       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("BGR"));
657       break;
658     case GST_VIDEO_FORMAT_I420:
659       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("I420"));
660 	  break;
661     case GST_VIDEO_FORMAT_YUY2:
662       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("YUY2"));
663       break;
664     case GST_VIDEO_FORMAT_UYVY:
665       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("UYVY"));
666       break;
667     case GST_VIDEO_FORMAT_BGRx:
668       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("BGRx"));
669       break;
670     case GST_VIDEO_FORMAT_BGR16:
671       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("BGR16"));
672       break;
673     case GST_VIDEO_FORMAT_BGR15:
674       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("BGR15"));
675       break;
676     case GST_VIDEO_FORMAT_GRAY8:
677       video_caps = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("GRAY8"));
678       break;
679     default:
680       break;
681   }
682 
683   /* other video format */
684   if (!video_caps) {
685     if (g_ascii_strncasecmp (name, "video/x-dv, systemstream=FALSE", 31) == 0) {
686       video_caps = gst_caps_new_simple ("video/x-dv",
687           "systemstream", G_TYPE_BOOLEAN, FALSE,
688           "format", G_TYPE_STRING, "dvsd",
689           NULL);
690     } else if (g_ascii_strncasecmp (name, "video/x-dv, systemstream=TRUE", 31) == 0) {
691       video_caps = gst_caps_new_simple ("video/x-dv",
692           "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
693       return video_caps;
694     } else if (g_ascii_strncasecmp (name, "image/jpeg", 10) == 0) {
695       video_caps = gst_caps_new_simple ("image/jpeg", NULL);
696     } else if (g_ascii_strncasecmp (name, "video/x-h264", 12) == 0) {
697       video_caps = gst_caps_new_simple ("video/x-h264", NULL);
698     }
699   }
700 
701   if (!video_caps)
702     return NULL;
703 
704   video_structure = gst_caps_get_structure (video_caps, 0);
705 
706   /* Hope GST_TYPE_INT_RANGE_STEP will exits in future gstreamer releases  */
707   /* because we could use :  */
708   /* "width", GST_TYPE_INT_RANGE_STEP, video_default->minWidth, video_default->maxWidth,  video_default->granularityWidth */
709   /* instead of : */
710   /* "width", GST_TYPE_INT_RANGE, video_default->minWidth, video_default->maxWidth */
711 
712   /* For framerate we do not need a step (granularity) because  */
713   /* "The IAMStreamConfig::SetFormat method will set the frame rate to the closest  */
714   /* value that the filter supports" as it said in the VIDEO_STREAM_CONFIG_CAPS dshwo doc */
715 
716   min_w = pin_mediatype->vscc.MinOutputSize.cx;
717   max_w = pin_mediatype->vscc.MaxOutputSize.cx;
718   min_h = pin_mediatype->vscc.MinOutputSize.cy;
719   max_h = pin_mediatype->vscc.MaxOutputSize.cy;
720   min_fr = (gint) (10000000 / pin_mediatype->vscc.MaxFrameInterval);
721   max_fr = (gint)(10000000 / pin_mediatype->vscc.MinFrameInterval);
722 
723   if (min_w == max_w)
724     gst_structure_set (video_structure, "width", G_TYPE_INT, min_w, NULL);
725   else
726      gst_structure_set (video_structure,
727        "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
728 
729   if (min_h == max_h)
730     gst_structure_set (video_structure, "height", G_TYPE_INT, min_h, NULL);
731   else
732      gst_structure_set (video_structure,
733        "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
734 
735   if (min_fr == max_fr)
736     gst_structure_set (video_structure, "framerate",
737         GST_TYPE_FRACTION, min_fr, 1, NULL);
738   else
739      gst_structure_set (video_structure, "framerate",
740          GST_TYPE_FRACTION_RANGE, min_fr, 1, max_fr, 1, NULL);
741 
742   return video_caps;
743 }
744 
gst_dshow_configure_latency(IPin * pCapturePin,guint bufSizeMS)745 bool gst_dshow_configure_latency (IPin *pCapturePin, guint bufSizeMS)
746 {
747   HRESULT hr;
748   ALLOCATOR_PROPERTIES alloc_prop;
749   IAMBufferNegotiation * pNeg = NULL;
750   hr = pCapturePin->QueryInterface(IID_IAMBufferNegotiation, (void **)&pNeg);
751 
752   if(!SUCCEEDED (hr))
753     return FALSE;
754 
755   alloc_prop.cbAlign = -1;  // -1 means no preference.
756   alloc_prop.cbBuffer = bufSizeMS;
757   alloc_prop.cbPrefix = -1;
758   alloc_prop.cBuffers = -1;
759   hr = pNeg->SuggestAllocatorProperties (&alloc_prop);
760   return SUCCEEDED (hr);
761 }
762