2 * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "kshelpers.h"
22 /* This plugin is from the era of Windows XP and uses APIs that have been
23 * deprecated since then. Let's pretend we're Windows XP too so that Windows
24 * lets us use that deprecated API. */
27 #define NTDDI_VERSION NTDDI_WINXP
28 #define _WIN32_WINNT _WIN32_WINNT_WINXP
34 GST_DEBUG_CATEGORY_EXTERN (gst_ks_debug);
35 #define GST_CAT_DEFAULT gst_ks_debug
37 #ifndef STATIC_KSPROPSETID_Wave_Queued
38 #define STATIC_KSPROPSETID_Wave_Queued \
39 0x16a15b10L, 0x16f0, 0x11d0, { 0xa1, 0x95, 0x00, 0x20, 0xaf, 0xd1, 0x56, 0xe4 }
40 DEFINE_GUIDSTRUCT ("16a15b10-16f0-11d0-a195-0020afd156e4",
41 KSPROPSETID_Wave_Queued);
45 ks_is_valid_handle (HANDLE h)
47 return (h != INVALID_HANDLE_VALUE && h != NULL);
51 ks_enumerate_devices (const GUID * devtype, const GUID * direction_category)
57 devinfo = SetupDiGetClassDevsW (devtype, NULL, NULL,
58 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
59 if (!ks_is_valid_handle (devinfo))
60 return NULL; /* no devices */
64 SP_DEVICE_INTERFACE_DATA if_data = { 0, };
65 SP_DEVICE_INTERFACE_DATA if_alias_data = { 0, };
66 SP_DEVICE_INTERFACE_DETAIL_DATA_W *if_detail_data;
67 DWORD if_detail_data_size;
68 SP_DEVINFO_DATA devinfo_data = { 0, };
71 if_data.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
73 success = SetupDiEnumDeviceInterfaces (devinfo, NULL, devtype, i, &if_data);
74 if (!success) /* all devices enumerated? */
77 if_alias_data.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
79 SetupDiGetDeviceInterfaceAlias (devinfo, &if_data, direction_category,
84 if_detail_data_size = (MAX_PATH - 1) * sizeof (gunichar2);
85 if_detail_data = g_malloc0 (if_detail_data_size);
86 if_detail_data->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA_W);
88 devinfo_data.cbSize = sizeof (SP_DEVINFO_DATA);
90 success = SetupDiGetDeviceInterfaceDetailW (devinfo, &if_data,
91 if_detail_data, if_detail_data_size, &req_size, &devinfo_data);
96 entry = g_new0 (KsDeviceEntry, 1);
99 g_utf16_to_utf8 (if_detail_data->DevicePath, -1, NULL, NULL, NULL);
101 if (SetupDiGetDeviceRegistryPropertyW (devinfo, &devinfo_data,
102 SPDRP_FRIENDLYNAME, NULL, (BYTE *) buf, sizeof (buf), NULL)) {
103 entry->name = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
106 if (entry->name == NULL) {
107 if (SetupDiGetDeviceRegistryPropertyW (devinfo, &devinfo_data,
108 SPDRP_DEVICEDESC, NULL, (BYTE *) buf, sizeof (buf), NULL)) {
109 entry->name = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
113 if (entry->name != NULL)
114 result = g_list_prepend (result, entry);
116 ks_device_entry_free (entry);
119 g_free (if_detail_data);
122 SetupDiDestroyDeviceInfoList (devinfo);
124 return g_list_reverse (result);
128 ks_device_entry_free (KsDeviceEntry * entry)
133 g_free (entry->path);
134 g_free (entry->name);
140 ks_device_list_free (GList * devices)
144 for (cur = devices; cur != NULL; cur = cur->next)
145 ks_device_entry_free (cur->data);
147 g_list_free (devices);
151 ks_sync_device_io_control (HANDLE device, gulong io_control_code,
152 gpointer in_buffer, gulong in_buffer_size, gpointer out_buffer,
153 gulong out_buffer_size, gulong * bytes_returned, gulong * error)
155 OVERLAPPED overlapped = { 0, };
158 overlapped.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
160 success = DeviceIoControl (device, io_control_code, in_buffer,
161 in_buffer_size, out_buffer, out_buffer_size, bytes_returned, &overlapped);
165 if ((err = GetLastError ()) == ERROR_IO_PENDING) {
166 success = GetOverlappedResult (device, &overlapped, bytes_returned, TRUE);
168 err = GetLastError ();
175 CloseHandle (overlapped.hEvent);
177 return success ? TRUE : FALSE;
181 ks_filter_get_pin_property (HANDLE filter_handle, gulong pin_id,
182 GUID prop_set, gulong prop_id, gpointer value, gulong value_size,
186 DWORD bytes_returned = 0;
188 memset (&prop, 0, sizeof (KSP_PIN));
191 prop.Property.Set = prop_set;
192 prop.Property.Id = prop_id;
193 prop.Property.Flags = KSPROPERTY_TYPE_GET;
195 return ks_sync_device_io_control (filter_handle, IOCTL_KS_PROPERTY, &prop,
196 sizeof (prop), value, value_size, &bytes_returned, error);
200 ks_filter_get_pin_property_multi (HANDLE filter_handle, gulong pin_id,
201 GUID prop_set, gulong prop_id, KSMULTIPLE_ITEM ** items, gulong * error)
204 DWORD items_size = 0, bytes_written = 0;
208 memset (&prop, 0, sizeof (KSP_PIN));
212 prop.Property.Set = prop_set;
213 prop.Property.Id = prop_id;
214 prop.Property.Flags = KSPROPERTY_TYPE_GET;
216 ret = ks_sync_device_io_control (filter_handle, IOCTL_KS_PROPERTY,
217 &prop.Property, sizeof (prop), NULL, 0, &items_size, &err);
218 if (!ret && err != ERROR_INSUFFICIENT_BUFFER && err != ERROR_MORE_DATA)
221 *items = g_malloc0 (items_size);
223 ret = ks_sync_device_io_control (filter_handle, IOCTL_KS_PROPERTY, &prop,
224 sizeof (prop), *items, items_size, &bytes_written, &err);
241 ks_object_query_property (HANDLE handle, GUID prop_set, gulong prop_id,
242 gulong prop_flags, gpointer * value, gulong * value_size, gulong * error)
245 DWORD req_value_size = 0, bytes_written = 0;
249 memset (&prop, 0, sizeof (KSPROPERTY));
254 prop.Flags = prop_flags;
256 if (value_size == NULL || *value_size == 0) {
257 ret = ks_sync_device_io_control (handle, IOCTL_KS_PROPERTY,
258 &prop, sizeof (prop), NULL, 0, &req_value_size, &err);
259 if (!ret && err != ERROR_INSUFFICIENT_BUFFER && err != ERROR_MORE_DATA)
262 req_value_size = *value_size;
265 *value = g_malloc0 (req_value_size);
267 ret = ks_sync_device_io_control (handle, IOCTL_KS_PROPERTY, &prop,
268 sizeof (prop), *value, req_value_size, &bytes_written, &err);
272 if (value_size != NULL)
273 *value_size = bytes_written;
284 if (value_size != NULL)
291 ks_object_get_property (HANDLE handle, GUID prop_set, gulong prop_id,
292 gpointer * value, gulong * value_size, gulong * error)
294 return ks_object_query_property (handle, prop_set, prop_id,
295 KSPROPERTY_TYPE_GET, value, value_size, error);
299 ks_object_set_property (HANDLE handle, GUID prop_set, gulong prop_id,
300 gpointer value, gulong value_size, gulong * error)
303 DWORD bytes_returned;
305 memset (&prop, 0, sizeof (KSPROPERTY));
308 prop.Flags = KSPROPERTY_TYPE_SET;
310 return ks_sync_device_io_control (handle, IOCTL_KS_PROPERTY, &prop,
311 sizeof (prop), value, value_size, &bytes_returned, error);
315 ks_object_get_supported_property_sets (HANDLE handle, GUID ** propsets,
324 if (ks_object_query_property (handle, GUID_NULL, 0,
325 KSPROPERTY_TYPE_SETSUPPORT, (void *) propsets, &size, &error)) {
326 if (size % sizeof (GUID) == 0) {
327 *len = size / sizeof (GUID);
339 ks_object_set_connection_state (HANDLE handle, KSSTATE state, gulong * error)
341 return ks_object_set_property (handle, KSPROPSETID_Connection,
342 KSPROPERTY_CONNECTION_STATE, &state, sizeof (state), error);
346 ks_guid_to_string (const GUID * guid)
348 return g_strdup_printf ("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
349 (guint) guid->Data1, (guint) guid->Data2, (guint) guid->Data3,
350 (guint) guid->Data4[0], (guint) guid->Data4[1], (guint) guid->Data4[2],
351 (guint) guid->Data4[3], (guint) guid->Data4[4], (guint) guid->Data4[5],
352 (guint) guid->Data4[6], (guint) guid->Data4[7]);
356 ks_state_to_string (KSSTATE state)
360 return "KSSTATE_STOP";
361 case KSSTATE_ACQUIRE:
362 return "KSSTATE_ACQUIRE";
364 return "KSSTATE_PAUSE";
366 return "KSSTATE_RUN";
368 g_assert_not_reached ();
374 #define CHECK_OPTIONS_FLAG(flag) \
375 if (flags & KSSTREAM_HEADER_OPTIONSF_##flag)\
378 g_string_append (str, "|");\
379 g_string_append (str, G_STRINGIFY (flag));\
380 flags &= ~KSSTREAM_HEADER_OPTIONSF_##flag;\
384 ks_options_flags_to_string (gulong flags)
388 str = g_string_sized_new (128);
390 CHECK_OPTIONS_FLAG (SPLICEPOINT);
391 CHECK_OPTIONS_FLAG (PREROLL);
392 CHECK_OPTIONS_FLAG (DATADISCONTINUITY);
393 CHECK_OPTIONS_FLAG (TYPECHANGED);
394 CHECK_OPTIONS_FLAG (TIMEVALID);
395 CHECK_OPTIONS_FLAG (TIMEDISCONTINUITY);
396 CHECK_OPTIONS_FLAG (FLUSHONPAUSE);
397 CHECK_OPTIONS_FLAG (DURATIONVALID);
398 CHECK_OPTIONS_FLAG (ENDOFSTREAM);
399 CHECK_OPTIONS_FLAG (BUFFEREDTRANSFER);
400 CHECK_OPTIONS_FLAG (VRAM_DATA_TRANSFER);
401 CHECK_OPTIONS_FLAG (LOOPEDDATA);
404 g_string_append_printf (str, "|0x%08x", (guint) flags);
406 return g_string_free (str, FALSE);
413 } KsPropertySetMapping;
415 #ifndef STATIC_KSPROPSETID_GM
416 #define STATIC_KSPROPSETID_GM \
417 0xAF627536, 0xE719, 0x11D2, { 0x8A, 0x1D, 0x00, 0x60, 0x97, 0xD2, 0xDF, 0x5D }
419 #ifndef STATIC_KSPROPSETID_Jack
420 #define STATIC_KSPROPSETID_Jack \
421 0x4509F757, 0x2D46, 0x4637, { 0x8E, 0x62, 0xCE, 0x7D, 0xB9, 0x44, 0xF5, 0x7B }
424 #ifndef STATIC_PROPSETID_VIDCAP_SELECTOR
425 #define STATIC_PROPSETID_VIDCAP_SELECTOR \
426 0x1ABDAECA, 0x68B6, 0x4F83, { 0x93, 0x71, 0xB4, 0x13, 0x90, 0x7C, 0x7B, 0x9F }
428 #ifndef STATIC_PROPSETID_EXT_DEVICE
429 #define STATIC_PROPSETID_EXT_DEVICE \
430 0xB5730A90, 0x1A2C, 0x11cf, { 0x8c, 0x23, 0x00, 0xAA, 0x00, 0x6B, 0x68, 0x14 }
432 #ifndef STATIC_PROPSETID_EXT_TRANSPORT
433 #define STATIC_PROPSETID_EXT_TRANSPORT \
434 0xA03CD5F0, 0x3045, 0x11cf, { 0x8c, 0x44, 0x00, 0xAA, 0x00, 0x6B, 0x68, 0x14 }
436 #ifndef STATIC_PROPSETID_TIMECODE_READER
437 #define STATIC_PROPSETID_TIMECODE_READER \
438 0x9B496CE1, 0x811B, 0x11cf, { 0x8C, 0x77, 0x00, 0xAA, 0x00, 0x6B, 0x68, 0x14 }
441 /* GCC warns about this, but it seems to be correct and MSVC doesn't warn about
442 * it. XXX: Check again after the toolchain is updated:
443 * https://gitlab.freedesktop.org/gstreamer/cerbero/merge_requests/69 */
445 #pragma GCC diagnostic ignored "-Wmissing-braces"
447 static const KsPropertySetMapping known_property_sets[] = {
448 {{STATIC_KSPROPSETID_General}, "General"},
449 {{STATIC_KSPROPSETID_MediaSeeking}, "MediaSeeking"},
450 {{STATIC_KSPROPSETID_Topology}, "Topology"},
451 {{STATIC_KSPROPSETID_GM}, "GM"},
452 {{STATIC_KSPROPSETID_Pin}, "Pin"},
453 {{STATIC_KSPROPSETID_Quality}, "Quality"},
454 {{STATIC_KSPROPSETID_Connection}, "Connection"},
455 {{STATIC_KSPROPSETID_MemoryTransport}, "MemoryTransport"},
456 {{STATIC_KSPROPSETID_StreamAllocator}, "StreamAllocator"},
457 {{STATIC_KSPROPSETID_StreamInterface}, "StreamInterface"},
458 {{STATIC_KSPROPSETID_Stream}, "Stream"},
459 {{STATIC_KSPROPSETID_Clock}, "Clock"},
461 {{STATIC_KSPROPSETID_DirectSound3DListener}, "DirectSound3DListener"},
462 {{STATIC_KSPROPSETID_DirectSound3DBuffer}, "DirectSound3DBuffer"},
463 {{STATIC_KSPROPSETID_Hrtf3d}, "Hrtf3d"},
464 {{STATIC_KSPROPSETID_Itd3d}, "Itd3d"},
465 {{STATIC_KSPROPSETID_Bibliographic}, "Bibliographic"},
466 {{STATIC_KSPROPSETID_TopologyNode}, "TopologyNode"},
467 {{STATIC_KSPROPSETID_RtAudio}, "RtAudio"},
468 {{STATIC_KSPROPSETID_DrmAudioStream}, "DrmAudioStream"},
469 {{STATIC_KSPROPSETID_Audio}, "Audio"},
470 {{STATIC_KSPROPSETID_Acoustic_Echo_Cancel}, "Acoustic_Echo_Cancel"},
471 {{STATIC_KSPROPSETID_Wave_Queued}, "Wave_Queued"},
472 {{STATIC_KSPROPSETID_Wave}, "Wave"},
473 {{STATIC_KSPROPSETID_WaveTable}, "WaveTable"},
474 {{STATIC_KSPROPSETID_Cyclic}, "Cyclic"},
475 {{STATIC_KSPROPSETID_Sysaudio}, "Sysaudio"},
476 {{STATIC_KSPROPSETID_Sysaudio_Pin}, "Sysaudio_Pin"},
477 {{STATIC_KSPROPSETID_AudioGfx}, "AudioGfx"},
478 {{STATIC_KSPROPSETID_Linear}, "Linear"},
479 {{STATIC_KSPROPSETID_Mpeg2Vid}, "Mpeg2Vid"},
480 {{STATIC_KSPROPSETID_AC3}, "AC3"},
481 {{STATIC_KSPROPSETID_AudioDecoderOut}, "AudioDecoderOut"},
482 {{STATIC_KSPROPSETID_DvdSubPic}, "DvdSubPic"},
483 {{STATIC_KSPROPSETID_CopyProt}, "CopyProt"},
484 {{STATIC_KSPROPSETID_VBICAP_PROPERTIES}, "VBICAP_PROPERTIES"},
485 {{STATIC_KSPROPSETID_VBICodecFiltering}, "VBICodecFiltering"},
486 {{STATIC_KSPROPSETID_VramCapture}, "VramCapture"},
487 {{STATIC_KSPROPSETID_OverlayUpdate}, "OverlayUpdate"},
488 {{STATIC_KSPROPSETID_VPConfig}, "VPConfig"},
489 {{STATIC_KSPROPSETID_VPVBIConfig}, "VPVBIConfig"},
490 {{STATIC_KSPROPSETID_TSRateChange}, "TSRateChange"},
491 {{STATIC_KSPROPSETID_Jack}, "Jack"},
493 {{STATIC_PROPSETID_ALLOCATOR_CONTROL}, "ALLOCATOR_CONTROL"},
494 {{STATIC_PROPSETID_VIDCAP_VIDEOPROCAMP}, "VIDCAP_VIDEOPROCAMP"},
495 {{STATIC_PROPSETID_VIDCAP_SELECTOR}, "VIDCAP_SELECTOR"},
496 {{STATIC_PROPSETID_TUNER}, "TUNER"},
497 {{STATIC_PROPSETID_VIDCAP_VIDEOENCODER}, "VIDCAP_VIDEOENCODER"},
498 {{STATIC_PROPSETID_VIDCAP_VIDEODECODER}, "VIDCAP_VIDEODECODER"},
499 {{STATIC_PROPSETID_VIDCAP_CAMERACONTROL}, "VIDCAP_CAMERACONTROL"},
500 {{STATIC_PROPSETID_EXT_DEVICE}, "EXT_DEVICE"},
501 {{STATIC_PROPSETID_EXT_TRANSPORT}, "EXT_TRANSPORT"},
502 {{STATIC_PROPSETID_TIMECODE_READER}, "TIMECODE_READER"},
503 {{STATIC_PROPSETID_VIDCAP_CROSSBAR}, "VIDCAP_CROSSBAR"},
504 {{STATIC_PROPSETID_VIDCAP_TVAUDIO}, "VIDCAP_TVAUDIO"},
505 {{STATIC_PROPSETID_VIDCAP_VIDEOCOMPRESSION}, "VIDCAP_VIDEOCOMPRESSION"},
506 {{STATIC_PROPSETID_VIDCAP_VIDEOCONTROL}, "VIDCAP_VIDEOCONTROL"},
507 {{STATIC_PROPSETID_VIDCAP_DROPPEDFRAMES}, "VIDCAP_DROPPEDFRAMES"},
511 ks_property_set_to_string (const GUID * guid)
516 i < sizeof (known_property_sets) / sizeof (known_property_sets[0]); i++) {
517 if (IsEqualGUID (guid, &known_property_sets[i].guid))
518 return g_strdup_printf ("KSPROPSETID_%s", known_property_sets[i].name);
521 return ks_guid_to_string (guid);