summaryrefslogtreecommitdiff
path: root/sys/winks/ksdeviceprovider.c
blob: 28e9b5b6699975dba1487cfc20932f11196eb050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/* GStreamer
 * Copyright (C) 2015 Руслан Ижбулатов <lrn1986@gmail.com>
 *
 * ksdeviceprovider.c: Kernel Streaming device probing and monitoring
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstksvideosrc.h"
#include "ksdeviceprovider.h"

#include <string.h>

#include <dbt.h>                /* for DBT_* consts and [_]DEV_* structs */
#include <devguid.h>            /* for GUID_DEVCLASS_WCEUSBS */
#include <setupapi.h>           /* for DIGCF_ALLCLASSES */

#include <gst/gst.h>

#include "kshelpers.h"
#include "ksvideohelpers.h"


GST_DEBUG_CATEGORY_EXTERN (gst_ks_debug);
#define GST_CAT_DEFAULT gst_ks_debug


static GstDevice *gst_ks_device_new (guint id,
    const gchar * device_name, GstCaps * caps, const gchar * device_path,
    GstKsDeviceType type);

G_DEFINE_TYPE (GstKsDeviceProvider, gst_ks_device_provider,
    GST_TYPE_DEVICE_PROVIDER);

static GList *gst_ks_device_provider_probe (GstDeviceProvider * provider);
static gboolean gst_ks_device_provider_start (GstDeviceProvider * provider);
static void gst_ks_device_provider_stop (GstDeviceProvider * provider);

static void
gst_ks_device_provider_class_init (GstKsDeviceProviderClass * klass)
{
  GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);

  dm_class->probe = gst_ks_device_provider_probe;
  dm_class->start = gst_ks_device_provider_start;
  dm_class->stop = gst_ks_device_provider_stop;

  gst_device_provider_class_set_static_metadata (dm_class,
      "KernelStreaming Device Provider", "Sink/Source/Audio/Video",
      "List and provide KernelStreaming source and sink devices",
      "Руслан Ижбулатов <lrn1986@gmail.com>");
}

static void
gst_ks_device_provider_init (GstKsDeviceProvider * self)
{
}

static GstDevice *
new_video_source (const KsDeviceEntry * info)
{
  GstCaps *caps;
  HANDLE filter_handle;
  GList *media_types;
  GList *cur;

  g_assert (info->path != NULL);

  caps = gst_caps_new_empty ();

  filter_handle = CreateFile (info->path,
      GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  if (!ks_is_valid_handle (filter_handle))
    goto error;

  media_types = ks_video_probe_filter_for_caps (filter_handle);

  for (cur = media_types; cur != NULL; cur = cur->next) {
    KsVideoMediaType *media_type = cur->data;

    gst_caps_append (caps, gst_caps_copy (media_type->translated_caps));

    ks_video_media_type_free (media_type);
  }

  CloseHandle (filter_handle);
  g_list_free (media_types);

  return gst_ks_device_new (info->index, info->name,
      caps, info->path, GST_KS_DEVICE_TYPE_VIDEO_SOURCE);
error:
  gst_caps_unref (caps);
  return NULL;
}

static GList *
gst_ks_device_provider_probe (GstDeviceProvider * provider)
{
  /*GstKsDeviceProvider *self = GST_KS_DEVICE_PROVIDER (provider); */
  GList *devices, *cur;
  GList *result;

  result = NULL;

  devices = ks_enumerate_devices (&KSCATEGORY_VIDEO, &KSCATEGORY_CAPTURE);
  if (devices == NULL)
    return result;

  devices = ks_video_device_list_sort_cameras_first (devices);

  for (cur = devices; cur != NULL; cur = cur->next) {
    GstDevice *source;
    KsDeviceEntry *entry = cur->data;

    source = new_video_source (entry);
    if (source)
      result = g_list_prepend (result, gst_object_ref_sink (source));

    ks_device_entry_free (entry);
  }

  result = g_list_reverse (result);

  g_list_free (devices);

  return result;
}

static const gchar *
get_dev_type (DEV_BROADCAST_HDR * dev_msg_header)
{
  switch (dev_msg_header->dbch_devicetype) {
    case DBT_DEVTYP_DEVICEINTERFACE:
      return "Device interface class";
    case DBT_DEVTYP_HANDLE:
      return "Filesystem handle";
    case DBT_DEVTYP_OEM:
      return "OEM or IHV device type";
    case DBT_DEVTYP_PORT:
      return "Port device";
    case DBT_DEVTYP_VOLUME:
      return "Logical volume";
    default:
      return "Unknown device type";
  }
}

#define KS_MSG_WINDOW_CLASS "gst_winks_device_msg_window"
#define WM_QUITTHREAD (WM_USER + 0)

static void unreg_msg_window_class (ATOM class_id, const char *class_name,
    HINSTANCE inst);

static HDEVNOTIFY
register_device_interface (GstKsDeviceProvider * self,
    GUID interface_class_guid, HWND window_handle)
{
  DEV_BROADCAST_DEVICEINTERFACE notification_filter;
  HDEVNOTIFY notification_handle;
  DWORD error;

  memset (&notification_filter, 0, sizeof (notification_filter));
  notification_filter.dbcc_size = sizeof (notification_filter);
  notification_filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  notification_filter.dbcc_classguid = interface_class_guid;

  notification_handle = RegisterDeviceNotificationW (window_handle,
      &notification_filter,
      DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
  error = GetLastError ();

  if (notification_handle == NULL)
    GST_ERROR_OBJECT (self,
        "Could not register for a device notification: %lu", error);

  return notification_handle;
}

static INT_PTR WINAPI
msg_window_message_proc (HWND window_handle, UINT message,
    WPARAM wparam, LPARAM lparam)
{
  LRESULT result;
  LONG_PTR user_data;
  GstKsDeviceProvider *self;
  PDEV_BROADCAST_DEVICEINTERFACE bcdi;
  DEV_BROADCAST_HDR *dev_msg_header;
  struct _DEV_BROADCAST_USERDEFINED *user_dev_msg_header;
  CREATESTRUCT *create_data;
  DWORD error;
  HINSTANCE inst;
  GstKsDevice *dev;
  GstDevice *source;
  GList *item;
  GstDeviceProvider *provider;
  GList *devices;
  gchar *guid_str;

  result = TRUE;

  switch (message) {
    case WM_CREATE:
      create_data = (CREATESTRUCT *) lparam;

      if (create_data->lpCreateParams == NULL) {
        /* DO SOMETHING!! */
      }

      self = GST_KS_DEVICE_PROVIDER (create_data->lpCreateParams);

      SetLastError (0);
      SetWindowLongPtr (window_handle, GWLP_USERDATA, (LONG_PTR) self);
      error = GetLastError ();
      if (error != NO_ERROR) {
        GST_ERROR_OBJECT (self,
            "Could not attach user data to the message window: %lu", error);
        DestroyWindow (window_handle);
        inst = (HINSTANCE) GetModuleHandle (NULL);
        GST_OBJECT_LOCK (self);
        unreg_msg_window_class (self->message_window_class, KS_MSG_WINDOW_CLASS,
            inst);
        self->message_window_class = 0;
        GST_OBJECT_UNLOCK (self);
      }
      result = FALSE;
      break;
    case WM_DEVICECHANGE:
      GST_DEBUG ("WM_DEVICECHANGE for %x %x", (unsigned int) wparam,
          (unsigned int) lparam);

      user_data = GetWindowLongPtr (window_handle, GWLP_USERDATA);
      if (user_data == 0)
        break;

      self = GST_KS_DEVICE_PROVIDER (user_data);
      provider = GST_DEVICE_PROVIDER (self);

      dev_msg_header = (DEV_BROADCAST_HDR *) lparam;

      switch (wparam) {
        case DBT_CONFIGCHANGECANCELED:
          GST_DEBUG_OBJECT (self, "DBT_CONFIGCHANGECANCELED for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_CONFIGCHANGED:
          GST_DEBUG_OBJECT (self, "DBT_CONFIGCHANGED for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_CUSTOMEVENT:
          GST_DEBUG_OBJECT (self, "DBT_CUSTOMEVENT for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_DEVICEARRIVAL:
          GST_DEBUG_OBJECT (self, "DBT_DEVICEARRIVAL for %s",
              get_dev_type (dev_msg_header));

          if (dev_msg_header->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE)
            break;

          bcdi = (PDEV_BROADCAST_DEVICEINTERFACE) lparam;
          guid_str = ks_guid_to_string (&bcdi->dbcc_classguid);
          GST_INFO_OBJECT (self, "New device, class interface GUID %s, path %s",
              guid_str, bcdi->dbcc_name);
          g_free (guid_str);
          break;
        case DBT_DEVICEQUERYREMOVE:
          GST_DEBUG_OBJECT (self, "DBT_DEVICEQUERYREMOVE for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_DEVICEQUERYREMOVEFAILED:
          GST_DEBUG_OBJECT (self, "DBT_DEVICEQUERYREMOVEFAILED for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_DEVICEREMOVECOMPLETE:
          GST_DEBUG_OBJECT (self, "DBT_DEVICEREMOVECOMPLETE for %s",
              get_dev_type (dev_msg_header));

          if (dev_msg_header->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE)
            break;

          bcdi = (PDEV_BROADCAST_DEVICEINTERFACE) lparam;

          guid_str = ks_guid_to_string (&bcdi->dbcc_classguid);
          GST_INFO_OBJECT (self,
              "Removed device, class interface GUID %s, path %s", guid_str,
              bcdi->dbcc_name);
          g_free (guid_str);
          break;
        case DBT_DEVICEREMOVEPENDING:
          GST_DEBUG_OBJECT (self, "DBT_DEVICEREMOVEPENDING for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_DEVICETYPESPECIFIC:
          GST_DEBUG_OBJECT (self, "DBT_DEVICETYPESPECIFIC for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_DEVNODES_CHANGED:
          GST_DEBUG_OBJECT (self, "DBT_DEVNODES_CHANGED for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_QUERYCHANGECONFIG:
          GST_DEBUG_OBJECT (self, "DBT_QUERYCHANGECONFIG for %s",
              get_dev_type (dev_msg_header));
          break;
        case DBT_USERDEFINED:
          user_dev_msg_header = (struct _DEV_BROADCAST_USERDEFINED *) lparam;
          dev_msg_header =
              (DEV_BROADCAST_HDR *) & user_dev_msg_header->dbud_dbh;
          GST_DEBUG_OBJECT (self, "DBT_USERDEFINED for %s: %s",
              get_dev_type (dev_msg_header), user_dev_msg_header->dbud_szName);
          break;
        default:
          break;
      }

      switch (wparam) {
        case DBT_DEVICEARRIVAL:
          if (dev_msg_header->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE)
            break;

          bcdi = (PDEV_BROADCAST_DEVICEINTERFACE) lparam;

          /* Since both video and audio capture device declare KSCATEGORY_CAPTURE, we filter on
             KSCATEGORY_VIDEO here. To add audio support we should accept also KSCATEGORY_AUDIO. */
          if (!IsEqualGUID (&bcdi->dbcc_classguid, &KSCATEGORY_VIDEO))
            break;

          devices =
              ks_enumerate_devices (&bcdi->dbcc_classguid, &KSCATEGORY_CAPTURE);
          if (devices == NULL)
            break;

          source = NULL;
          for (item = devices; item != NULL; item = item->next) {
            KsDeviceEntry *entry = item->data;
            GST_DEBUG_OBJECT (self, "Listed device %s = %s", entry->name,
                entry->path);

            if ((source == NULL) &&
                (g_ascii_strcasecmp (entry->path, bcdi->dbcc_name) == 0))
              source = new_video_source (entry);        /* Or audio source, not implemented yet */

            ks_device_entry_free (entry);
          }

          if (source)
            gst_device_provider_device_add (GST_DEVICE_PROVIDER (self), source);

          g_list_free (devices);
          break;
        case DBT_DEVICEREMOVECOMPLETE:
          if (dev_msg_header->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE)
            break;

          bcdi = (PDEV_BROADCAST_DEVICEINTERFACE) lparam;
          dev = NULL;

          GST_OBJECT_LOCK (self);
          for (item = provider->devices; item; item = item->next) {
            dev = item->data;

            if (g_ascii_strcasecmp (dev->path, bcdi->dbcc_name) == 0) {
              guid_str = gst_device_get_display_name (GST_DEVICE (dev));
              GST_INFO_OBJECT (self, "Device matches to %s", guid_str);
              g_free (guid_str);
              gst_object_ref (dev);
              break;
            }
            dev = NULL;
          }
          GST_OBJECT_UNLOCK (self);

          if (dev) {
            gst_device_provider_device_remove (GST_DEVICE_PROVIDER (self),
                GST_DEVICE (dev));
            gst_object_unref (dev);
          }
          break;
        default:
          break;
      }
      result = FALSE;
      break;
    case WM_DESTROY:
      PostQuitMessage (0);
      result = FALSE;
      break;
    case WM_QUITTHREAD:
      DestroyWindow (window_handle);
      result = FALSE;
      break;
    default:
      result = DefWindowProc (window_handle, message, wparam, lparam);
      break;
  }

  return result;
}

static ATOM
reg_msg_window_class (const char *class_name, HINSTANCE inst)
{
  WNDCLASSEXA classex;

  memset (&classex, 0, sizeof (classex));
  classex.cbSize = sizeof (classex);
  classex.hInstance = inst;
  classex.lpfnWndProc = (WNDPROC) msg_window_message_proc;
  classex.lpszClassName = class_name;

  return RegisterClassExA (&classex);
}

static void
unreg_msg_window_class (ATOM class_id, const char *class_name, HINSTANCE inst)
{
  if (class_id != 0)
    UnregisterClassA ((LPCSTR) MAKELPARAM (class_id, 0), inst);
  else
    UnregisterClassA (class_name, inst);
}

static gpointer
ks_provider_msg_window_thread (gpointer dat)
{
  GstKsDeviceProvider *self;
  MSG msg;
  ATOM wnd_class;
  BOOL message_status;
  HINSTANCE inst;
  HANDLE msg_window = NULL;
  DWORD error;
  HDEVNOTIFY devnotify = NULL;

  g_return_val_if_fail (dat != NULL, NULL);

  self = GST_KS_DEVICE_PROVIDER (dat);

  GST_DEBUG_OBJECT (self, "Entering message window thread: %p",
      g_thread_self ());

  GST_OBJECT_LOCK (self);
  wnd_class = self->message_window_class;
  GST_OBJECT_UNLOCK (self);

  inst = (HINSTANCE) GetModuleHandle (NULL);

  msg_window = CreateWindowExA (0,
      wnd_class != 0 ? (LPCSTR) MAKELPARAM (wnd_class, 0) : KS_MSG_WINDOW_CLASS,
      "", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, inst, self);
  error = GetLastError ();

  if (msg_window == NULL) {
    GST_ERROR_OBJECT (self, "Could not create a message window: %lu", error);
    GST_OBJECT_LOCK (self);
    unreg_msg_window_class (wnd_class, KS_MSG_WINDOW_CLASS, inst);
    self->message_window_class = 0;
    SetEvent (self->wakeup_event);
    GST_OBJECT_UNLOCK (self);
    return NULL;
  }

  GST_OBJECT_LOCK (self);
  self->message_window = msg_window;

  devnotify =
      register_device_interface (self, GUID_DEVCLASS_WCEUSBS, msg_window);
  if (devnotify == NULL) {
    DestroyWindow (msg_window);
    unreg_msg_window_class (wnd_class, KS_MSG_WINDOW_CLASS, inst);
    self->message_window_class = 0;
    self->message_window = NULL;
    SetEvent (self->wakeup_event);
    GST_OBJECT_UNLOCK (self);
    return NULL;
  }

  self->device_notify_handle = devnotify;
  SetEvent (self->wakeup_event);
  GST_OBJECT_UNLOCK (self);

  while ((message_status = GetMessage (&msg, NULL, 0, 0)) != 0) {
    if (message_status < 0 || msg.message == WM_QUIT)
      break;
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }

  GST_DEBUG_OBJECT (self, "Exiting internal window thread: %p",
      g_thread_self ());

  return NULL;
}

static gboolean
gst_ks_device_provider_start (GstDeviceProvider * provider)
{
  ATOM wnd_class = 0;
  HINSTANCE inst;
  HANDLE wakeup_event;
  HWND message_window;
  DWORD error;
  GList *devs;
  GList *dev;
  GstKsDeviceProvider *self = GST_KS_DEVICE_PROVIDER (provider);

  GST_OBJECT_LOCK (self);
  g_assert (self->message_window == NULL);
  GST_OBJECT_UNLOCK (self);

  /* We get notifications on *change*, so before we get to that,
   * we need to obtain a complete list of devices, which we will
   * watch for changes.
   */
  devs = gst_ks_device_provider_probe (provider);
  for (dev = devs; dev; dev = dev->next) {
    if (dev->data)
      gst_device_provider_device_add (provider, (GstDevice *) dev->data);
  }
  g_list_free (devs);

  inst = (HINSTANCE) GetModuleHandle (NULL);

  wakeup_event = CreateEvent (NULL, TRUE, FALSE, NULL);
  error = GetLastError ();
  if (wakeup_event == NULL) {
    GST_OBJECT_LOCK (self);
    GST_ERROR_OBJECT (self, "Could not create a wakeup event: %lu", error);
    GST_OBJECT_UNLOCK (self);
    return FALSE;
  }

  wnd_class = reg_msg_window_class (KS_MSG_WINDOW_CLASS, inst);
  error = GetLastError ();

  if ((wnd_class == 0) && (error != ERROR_CLASS_ALREADY_EXISTS)) {
    GST_ERROR_OBJECT (self,
        "Could not register message window class: %lu", error);
    CloseHandle (wakeup_event);
    return FALSE;
  }

  GST_OBJECT_LOCK (self);
  self->message_window_class = wnd_class;
  self->wakeup_event = wakeup_event;

  self->message_thread =
      g_thread_new ("ks-device-provider-message-window-thread",
      (GThreadFunc) ks_provider_msg_window_thread, self);
  if (self->message_thread == NULL) {
    GST_ERROR_OBJECT (self, "Could not create message window thread");
    unreg_msg_window_class (wnd_class, KS_MSG_WINDOW_CLASS, inst);
    self->message_window_class = 0;
    CloseHandle (self->wakeup_event);
    GST_OBJECT_UNLOCK (self);
    return FALSE;
  }
  GST_OBJECT_UNLOCK (self);

  if (WaitForSingleObject (wakeup_event, INFINITE) != WAIT_OBJECT_0) {
    GST_ERROR_OBJECT (self,
        "Failed to wait for the message thread to initialize");
  }

  GST_OBJECT_LOCK (self);
  CloseHandle (self->wakeup_event);
  self->wakeup_event = NULL;
  message_window = self->message_window;
  GST_OBJECT_UNLOCK (self);

  if (message_window == NULL)
    return FALSE;

  return TRUE;
}

static void
gst_ks_device_provider_stop (GstDeviceProvider * provider)
{
  HINSTANCE inst;
  GThread *message_thread;
  GstKsDeviceProvider *self = GST_KS_DEVICE_PROVIDER (provider);

  GST_OBJECT_LOCK (self);

  g_assert (self->message_window != NULL);

  UnregisterDeviceNotification (self->device_notify_handle);
  self->device_notify_handle = NULL;
  PostMessage (self->message_window, WM_QUITTHREAD, 0, 0);
  message_thread = self->message_thread;
  GST_OBJECT_UNLOCK (self);

  g_thread_join (message_thread);

  GST_OBJECT_LOCK (self);
  self->message_window = NULL;
  self->message_thread = NULL;

  inst = (HINSTANCE) GetModuleHandle (NULL);

  unreg_msg_window_class (self->message_window_class, KS_MSG_WINDOW_CLASS,
      inst);

  self->message_window_class = 0;
  GST_OBJECT_UNLOCK (self);
}

enum
{
  PROP_PATH = 1
};

G_DEFINE_TYPE (GstKsDevice, gst_ks_device, GST_TYPE_DEVICE);

static void gst_ks_device_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_ks_device_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_ks_device_finalize (GObject * object);
static GstElement *gst_ks_device_create_element (GstDevice * device,
    const gchar * name);
static gboolean gst_ks_device_reconfigure_element (GstDevice * device,
    GstElement * element);

static void
gst_ks_device_class_init (GstKsDeviceClass * klass)
{
  GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  dev_class->create_element = gst_ks_device_create_element;
  dev_class->reconfigure_element = gst_ks_device_reconfigure_element;

  object_class->get_property = gst_ks_device_get_property;
  object_class->set_property = gst_ks_device_set_property;
  object_class->finalize = gst_ks_device_finalize;

  g_object_class_install_property (object_class, PROP_PATH,
      g_param_spec_string ("path", "System device path",
          "The system path to the device", "",
          G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

static void
gst_ks_device_init (GstKsDevice * device)
{
}

static void
gst_ks_device_finalize (GObject * object)
{
  GstKsDevice *device = GST_KS_DEVICE (object);

  g_free (device->path);

  G_OBJECT_CLASS (gst_ks_device_parent_class)->finalize (object);
}

static GstElement *
gst_ks_device_create_element (GstDevice * device, const gchar * name)
{
  GstKsDevice *ks_dev = GST_KS_DEVICE (device);
  GstElement *elem;

  elem = gst_element_factory_make (ks_dev->element, name);
  g_object_set (elem, "device-path", ks_dev->path, NULL);

  return elem;
}

static gboolean
gst_ks_device_reconfigure_element (GstDevice * device, GstElement * element)
{
  GstKsDevice *ks_dev = GST_KS_DEVICE (device);

  if (!strcmp (ks_dev->element, "ksvideosrc")) {
    if (!GST_IS_KS_VIDEO_SRC (element))
      return FALSE;
/*
  } else if (!strcmp (ks_dev->element, "ksaudiosrc")) {
    if (!GST_IS_KS_AUDIO_SRC (element))
      return FALSE;
  } else if (!strcmp (ks_dev->element, "ksaudiosink")) {
    if (!GST_IS_KS_AUDIO_SINK (element))
      return FALSE;
*/
  } else {
    g_assert_not_reached ();
  }

  g_object_set (element, "path", ks_dev->path, NULL);

  return TRUE;
}

static GstDevice *
gst_ks_device_new (guint device_index, const gchar * device_name,
    GstCaps * caps, const gchar * device_path, GstKsDeviceType type)
{
  GstKsDevice *gstdev;
  const gchar *element = NULL;
  const gchar *klass = NULL;

  g_return_val_if_fail (device_name, NULL);
  g_return_val_if_fail (device_path, NULL);
  g_return_val_if_fail (caps, NULL);


  switch (type) {
    case GST_KS_DEVICE_TYPE_VIDEO_SOURCE:
      element = "ksvideosrc";
      klass = "Video/Source";
      break;
    case GST_KS_DEVICE_TYPE_AUDIO_SOURCE:
      element = "ksaudiosrc";
      klass = "Audio/Source";
      break;
    case GST_KS_DEVICE_TYPE_AUDIO_SINK:
      element = "ksaudiosink";
      klass = "Audio/Sink";
      break;
    default:
      g_assert_not_reached ();
      break;
  }


  gstdev = g_object_new (GST_TYPE_KS_DEVICE,
      "display-name", device_name, "caps", caps, "device-class", klass,
      "path", device_path, NULL);

  gstdev->type = type;
  gstdev->device_index = device_index;
  gstdev->path = g_strdup (device_path);
  gstdev->element = element;

  return GST_DEVICE (gstdev);
}


static void
gst_ks_device_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstKsDevice *device;

  device = GST_KS_DEVICE_CAST (object);

  switch (prop_id) {
    case PROP_PATH:
      g_value_set_string (value, device->path);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}


static void
gst_ks_device_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstKsDevice *device;

  device = GST_KS_DEVICE_CAST (object);

  switch (prop_id) {
    case PROP_PATH:
      device->path = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}