Debian Codegraph @

sys/winks/gstksvideosrc.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Ole André Vadla Ravnås <[email protected]m>
3 * 2009 Andres Colubri <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:element-ksvideosrc
23 * @title: ksvideosrc
24 *
25 * Provides low-latency video capture from WDM cameras on Windows.
26 *
27 * ## Example pipelines
28 * |[
29 * gst-launch-1.0 -v ksvideosrc do-stats=TRUE ! videoconvert ! dshowvideosink
30 * ]| Capture from a camera and render using dshowvideosink.
31 * |[
32 * gst-launch-1.0 -v ksvideosrc do-stats=TRUE ! image/jpeg, width=640, height=480
33 * ! jpegdec ! videoconvert ! dshowvideosink
34 * ]| Capture from an MJPEG camera and render using dshowvideosink.
35 *
36 */
37
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42 #include "gstksvideosrc.h"
43
44 #include "gstksclock.h"
45 #include "gstksvideodevice.h"
46 #include "kshelpers.h"
47 #include "ksvideohelpers.h"
48 #include "ksdeviceprovider.h"
49
50 #include <versionhelpers.h>
51
52 #define DEFAULT_DEVICE_PATH NULL
53 #define DEFAULT_DEVICE_NAME NULL
54 #define DEFAULT_DEVICE_INDEX -1
55 #define DEFAULT_DO_STATS FALSE
56 #define DEFAULT_ENABLE_QUIRKS TRUE
57
58 enum
59 {
60 PROP_0,
61 PROP_DEVICE_PATH,
62 PROP_DEVICE_NAME,
63 PROP_DEVICE_INDEX,
64 PROP_DO_STATS,
65 PROP_FPS,
66 PROP_ENABLE_QUIRKS,
67 };
68
69 GST_DEBUG_CATEGORY (gst_ks_debug);
70 #define GST_CAT_DEFAULT gst_ks_debug
71
72 #define KS_WORKER_LOCK(priv) g_mutex_lock (&priv->worker_lock)
73 #define KS_WORKER_UNLOCK(priv) g_mutex_unlock (&priv->worker_lock)
74 #define KS_WORKER_WAIT(priv) \
75 g_cond_wait (&priv->worker_notify_cond, &priv->worker_lock)
76 #define KS_WORKER_NOTIFY(priv) g_cond_signal (&priv->worker_notify_cond)
77 #define KS_WORKER_WAIT_FOR_RESULT(priv) \
78 g_cond_wait (&priv->worker_result_cond, &priv->worker_lock)
79 #define KS_WORKER_NOTIFY_RESULT(priv) \
80 g_cond_signal (&priv->worker_result_cond)
81
82 typedef enum
83 {
84 KS_WORKER_STATE_STARTING,
85 KS_WORKER_STATE_READY,
86 KS_WORKER_STATE_STOPPING,
87 KS_WORKER_STATE_ERROR,
88 } KsWorkerState;
89
90 struct _GstKsVideoSrcPrivate
91 {
92 /* Properties */
93 gchar *device_path;
94 gchar *device_name;
95 gint device_index;
96 gboolean do_stats;
97 gboolean enable_quirks;
98
99 /* State */
100 GstKsClock *ksclock;
101 GstKsVideoDevice *device;
102
103 gboolean running;
104
105 /* Worker thread */
106 GThread *worker_thread;
107 GMutex worker_lock;
108 GCond worker_notify_cond;
109 GCond worker_result_cond;
110 KsWorkerState worker_state;
111
112 GstCaps *worker_pending_caps;
113 gboolean worker_setcaps_result;
114
115 gboolean worker_pending_run;
116 gboolean worker_run_result;
117
118 gulong worker_error_code;
119
120 /* Statistics */
121 GstClockTime last_sampling;
122 guint count;
123 guint fps;
124 };
125
126 #define GST_KS_VIDEO_SRC_GET_PRIVATE(o) ((o)->priv)
127
128 static void gst_ks_video_src_finalize (GObject * object);
129 static void gst_ks_video_src_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
131 static void gst_ks_video_src_set_property (GObject * object, guint prop_id,
132 const GValue * value, GParamSpec * pspec);
133
134 G_GNUC_UNUSED static GArray
135 * gst_ks_video_src_get_device_name_values (GstKsVideoSrc * self);
136 static void gst_ks_video_src_reset (GstKsVideoSrc * self);
137
138 static GstStateChangeReturn gst_ks_video_src_change_state (GstElement * element,
139 GstStateChange transition);
140 static gboolean gst_ks_video_src_set_clock (GstElement * element,
141 GstClock * clock);
142
143 static GstCaps *gst_ks_video_src_get_caps (GstBaseSrc * basesrc,
144 GstCaps * filter);
145 static gboolean gst_ks_video_src_set_caps (GstBaseSrc * basesrc,
146 GstCaps * caps);
147 static GstCaps *gst_ks_video_src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
148 static gboolean gst_ks_video_src_query (GstBaseSrc * basesrc, GstQuery * query);
149 static gboolean gst_ks_video_src_unlock (GstBaseSrc * basesrc);
150 static gboolean gst_ks_video_src_unlock_stop (GstBaseSrc * basesrc);
151
152 static GstFlowReturn gst_ks_video_src_create (GstPushSrc * pushsrc,
153 GstBuffer ** buffer);
154 static GstBuffer *gst_ks_video_src_alloc_buffer (guint size, guint alignment,
155 gpointer user_data);
156
157 G_DEFINE_TYPE_WITH_PRIVATE (GstKsVideoSrc, gst_ks_video_src, GST_TYPE_PUSH_SRC);
158
159 static GstKsVideoSrcClass *parent_class = NULL;
160
161 static void
162 gst_ks_video_src_class_init (GstKsVideoSrcClass * klass)
163 {
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
166 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
167 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
168
169 parent_class = g_type_class_peek_parent (klass);
170
171 gst_element_class_set_static_metadata (gstelement_class, "KsVideoSrc",
172 "Source/Video/Hardware",
173 "Stream data from a video capture device through Windows kernel streaming",
174 "Ole André Vadla Ravnås <[email protected]>, "
175 "Haakon Sporsheim <[email protected]>, "
176 "Andres Colubri <[email protected]>");
177
178 gst_element_class_add_pad_template (gstelement_class,
179 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
180 ks_video_get_all_caps ()));
181
182 gobject_class->finalize = gst_ks_video_src_finalize;
183 gobject_class->get_property = gst_ks_video_src_get_property;
184 gobject_class->set_property = gst_ks_video_src_set_property;
185
186 gstelement_class->change_state =
187 GST_DEBUG_FUNCPTR (gst_ks_video_src_change_state);
188 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_ks_video_src_set_clock);
189
190 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_ks_video_src_get_caps);
191 gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_ks_video_src_set_caps);
192 gstbasesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_ks_video_src_fixate);
193 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_ks_video_src_query);
194 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_ks_video_src_unlock);
195 gstbasesrc_class->unlock_stop =
196 GST_DEBUG_FUNCPTR (gst_ks_video_src_unlock_stop);
197
198 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_ks_video_src_create);
199
200 g_object_class_install_property (gobject_class, PROP_DEVICE_PATH,
201 g_param_spec_string ("device-path", "Device Path",
202 "The device path", DEFAULT_DEVICE_PATH,
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
205 g_param_spec_string ("device-name", "Device Name",
206 "The human-readable device name", DEFAULT_DEVICE_NAME,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_DEVICE_INDEX,
209 g_param_spec_int ("device-index", "Device Index",
210 "The zero-based device index", -1, G_MAXINT, DEFAULT_DEVICE_INDEX,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_DO_STATS,
213 g_param_spec_boolean ("do-stats", "Enable statistics",
214 "Enable logging of statistics", DEFAULT_DO_STATS,
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_FPS,
217 g_param_spec_int ("fps", "Frames per second",
218 "Last measured framerate, if statistics are enabled",
219 -1, G_MAXINT, -1, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (gobject_class, PROP_ENABLE_QUIRKS,
221 g_param_spec_boolean ("enable-quirks", "Enable quirks",
222 "Enable driver-specific quirks", DEFAULT_ENABLE_QUIRKS,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224
225 gst_ks_debug_init ();
226 }
227
228 static void
229 gst_ks_video_src_init (GstKsVideoSrc * self)
230 {
231 GstBaseSrc *basesrc = GST_BASE_SRC (self);
232 GstKsVideoSrcPrivate *priv;
233
234 self->priv = gst_ks_video_src_get_instance_private (self);
235 priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
236
237 gst_base_src_set_live (basesrc, TRUE);
238 gst_base_src_set_format (basesrc, GST_FORMAT_TIME);
239
240 gst_ks_video_src_reset (self);
241
242 priv->device_path = DEFAULT_DEVICE_PATH;
243 priv->device_name = DEFAULT_DEVICE_NAME;
244 priv->device_index = DEFAULT_DEVICE_INDEX;
245 priv->do_stats = DEFAULT_DO_STATS;
246 priv->enable_quirks = DEFAULT_ENABLE_QUIRKS;
247
248 /* MediaFoundation does not support MinGW build */
249 #ifdef _MSC_VER
250 {
251 static gsize deprecated_warn = 0;
252
253 if (g_once_init_enter (&deprecated_warn)) {
254 if (IsWindows8OrGreater ()) {
255 g_warning ("\"ksvideosrc\" is deprecated and will be removed"
256 "in the future. Use \"mfvideosrc\" element instead");
257 }
258 g_once_init_leave (&deprecated_warn, 1);
259 }
260 }
261 #endif
262 }
263
264 static void
265 gst_ks_video_src_finalize (GObject * object)
266 {
267 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (object);
268 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
269
270 g_free (priv->device_name);
271 g_free (priv->device_path);
272
273 G_OBJECT_CLASS (parent_class)->finalize (object);
274 }
275
276 static void
277 gst_ks_video_src_get_property (GObject * object, guint prop_id,
278 GValue * value, GParamSpec * pspec)
279 {
280 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (object);
281 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
282
283 switch (prop_id) {
284 case PROP_DEVICE_PATH:
285 g_value_set_string (value, priv->device_path);
286 break;
287 case PROP_DEVICE_NAME:
288 g_value_set_string (value, priv->device_name);
289 break;
290 case PROP_DEVICE_INDEX:
291 g_value_set_int (value, priv->device_index);
292 break;
293 case PROP_DO_STATS:
294 GST_OBJECT_LOCK (object);
295 g_value_set_boolean (value, priv->do_stats);
296 GST_OBJECT_UNLOCK (object);
297 break;
298 case PROP_FPS:
299 GST_OBJECT_LOCK (object);
300 g_value_set_int (value, priv->fps);
301 GST_OBJECT_UNLOCK (object);
302 break;
303 case PROP_ENABLE_QUIRKS:
304 g_value_set_boolean (value, priv->enable_quirks);
305 break;
306 default:
307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 break;
309 }
310 }
311
312 static void
313 gst_ks_video_src_set_property (GObject * object, guint prop_id,
314 const GValue * value, GParamSpec * pspec)
315 {
316 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (object);
317 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
318
319 switch (prop_id) {
320 case PROP_DEVICE_PATH:
321 {
322 const gchar *device_path = g_value_get_string (value);
323 g_free (priv->device_path);
324 priv->device_path = NULL;
325 if (device_path && strlen (device_path) != 0)
326 priv->device_path = g_value_dup_string (value);
327 }
328 break;
329 case PROP_DEVICE_NAME:
330 {
331 const gchar *device_name = g_value_get_string (value);
332 g_free (priv->device_name);
333 priv->device_name = NULL;
334 if (device_name && strlen (device_name) != 0)
335 priv->device_name = g_strdup (device_name);
336 }
337 break;
338 case PROP_DEVICE_INDEX:
339 priv->device_index = g_value_get_int (value);
340 break;
341 case PROP_DO_STATS:
342 GST_OBJECT_LOCK (object);
343 priv->do_stats = g_value_get_boolean (value);
344 GST_OBJECT_UNLOCK (object);
345 break;
346 case PROP_ENABLE_QUIRKS:
347 priv->enable_quirks = g_value_get_boolean (value);
348 break;
349 default:
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 break;
352 }
353 }
354
355 static void
356 gst_ks_video_src_reset (GstKsVideoSrc * self)
357 {
358 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
359
360 /* Reset statistics */
361 priv->last_sampling = GST_CLOCK_TIME_NONE;
362 priv->count = 0;
363 priv->fps = -1;
364
365 priv->running = FALSE;
366 }
367
368 static void
369 gst_ks_video_src_apply_driver_quirks (GstKsVideoSrc * self)
370 {
371 HMODULE mod;
372
373 /*
374 * Logitech's driver software injects the following DLL into all processes
375 * spawned. This DLL does some nasty tricks, sitting in between the
376 * application and the low-level ntdll API (NtCreateFile, NtClose,
377 * NtDeviceIoControlFile, NtDuplicateObject, etc.), making all sorts
378 * of assumptions.
379 *
380 * The only regression that this quirk causes is that the video effects
381 * feature doesn't work.
382 */
383 mod = GetModuleHandle ("LVPrcInj.dll");
384 if (mod != NULL) {
385 GST_DEBUG_OBJECT (self, "Logitech DLL detected, neutralizing it");
386
387 /*
388 * We know that no-one's actually keeping this handle around to decrement
389 * its reference count, so we'll take care of that job. The DLL's DllMain
390 * implementation takes care of rolling back changes when it gets unloaded,
391 * so this seems to be the cleanest and most future-proof way that we can
392 * get rid of it...
393 */
394 FreeLibrary (mod);
395
396 /* Paranoia: verify that it's no longer there */
397 mod = GetModuleHandle ("LVPrcInj.dll");
398 if (mod != NULL)
399 GST_WARNING_OBJECT (self, "failed to neutralize Logitech DLL");
400 }
401 }
402
403 /*FIXME: when we have a devices API replacement */
404 G_GNUC_UNUSED static GArray *
405 gst_ks_video_src_get_device_name_values (GstKsVideoSrc * self)
406 {
407 GList *devices, *cur;
408 GArray *array = g_array_new (TRUE, TRUE, sizeof (GValue));
409
410 devices = ks_enumerate_devices (&KSCATEGORY_VIDEO, &KSCATEGORY_CAPTURE);
411 if (devices == NULL)
412 return array;
413
414 devices = ks_video_device_list_sort_cameras_first (devices);
415
416 for (cur = devices; cur != NULL; cur = cur->next) {
417 GValue value = { 0, };
418 KsDeviceEntry *entry = cur->data;
419
420 g_value_init (&value, G_TYPE_STRING);
421 g_value_set_string (&value, entry->name);
422 g_array_append_val (array, value);
423 g_value_unset (&value);
424
425 ks_device_entry_free (entry);
426 }
427
428 g_list_free (devices);
429 return array;
430 }
431
432 static gboolean
433 gst_ks_video_src_open_device (GstKsVideoSrc * self)
434 {
435 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
436 GstKsVideoDevice *device = NULL;
437 GList *devices, *cur;
438
439 g_assert (priv->device == NULL);
440
441 devices = ks_enumerate_devices (&KSCATEGORY_VIDEO, &KSCATEGORY_CAPTURE);
442 if (devices == NULL)
443 goto error_no_devices;
444
445 devices = ks_video_device_list_sort_cameras_first (devices);
446
447 for (cur = devices; cur != NULL; cur = cur->next) {
448 KsDeviceEntry *entry = cur->data;
449
450 GST_DEBUG_OBJECT (self, "device %d: name='%s' path='%s'",
451 entry->index, entry->name, entry->path);
452 }
453
454 for (cur = devices; cur != NULL; cur = cur->next) {
455 KsDeviceEntry *entry = cur->data;
456 gboolean match;
457
458 if (device != NULL) {
459 ks_device_entry_free (entry);
460 continue;
461 }
462 if (priv->device_path != NULL) {
463 match = g_ascii_strcasecmp (entry->path, priv->device_path) == 0;
464 } else if (priv->device_name != NULL) {
465 match = g_ascii_strcasecmp (entry->name, priv->device_name) == 0;
466 } else if (priv->device_index >= 0) {
467 match = entry->index == priv->device_index;
468 } else {
469 match = TRUE; /* pick the first entry */
470 }
471
472 if (match) {
473 priv->ksclock = g_object_new (GST_TYPE_KS_CLOCK, NULL);
474 if (priv->ksclock != NULL && gst_ks_clock_open (priv->ksclock)) {
475 GstClock *clock = GST_ELEMENT_CLOCK (self);
476 if (clock != NULL)
477 gst_ks_clock_provide_master_clock (priv->ksclock, clock);
478 } else {
479 GST_WARNING_OBJECT (self, "failed to create/open KsClock");
480 g_object_unref (priv->ksclock);
481 priv->ksclock = NULL;
482 }
483
484 device = gst_ks_video_device_new (entry->path, priv->ksclock,
485 gst_ks_video_src_alloc_buffer, self);
486 }
487
488 ks_device_entry_free (entry);
489 }
490
491 g_list_free (devices);
492
493 if (device == NULL)
494 goto error_no_match;
495
496 if (!gst_ks_video_device_open (device))
497 goto error_open;
498
499 priv->device = device;
500
501 return TRUE;
502
503 /* ERRORS */
504 error_no_devices:
505 {
506 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
507 ("No video capture devices found"), (NULL));
508 return FALSE;
509 }
510 error_no_match:
511 {
512 if (priv->device_path != NULL) {
513 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
514 ("Specified video capture device with path '%s' not found",
515 priv->device_path), (NULL));
516 } else if (priv->device_name != NULL) {
517 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
518 ("Specified video capture device with name '%s' not found",
519 priv->device_name), (NULL));
520 } else {
521 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
522 ("Specified video capture device with index %d not found",
523 priv->device_index), (NULL));
524 }
525 return FALSE;
526 }
527 error_open:
528 {
529 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ,
530 ("Failed to open device"), (NULL));
531 g_object_unref (device);
532 return FALSE;
533 }
534 }
535
536 static void
537 gst_ks_video_src_close_device (GstKsVideoSrc * self)
538 {
539 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
540
541 g_assert (priv->device != NULL);
542
543 gst_ks_video_device_close (priv->device);
544 g_object_unref (priv->device);
545 priv->device = NULL;
546
547 if (priv->ksclock != NULL) {
548 gst_ks_clock_close (priv->ksclock);
549 g_object_unref (priv->ksclock);
550 priv->ksclock = NULL;
551 }
552
553 gst_ks_video_src_reset (self);
554 }
555
556 /*
557 * Worker thread that takes care of starting, configuring and stopping things.
558 *
559 * This is needed because Logitech's driver software injects a DLL that
560 * intercepts API functions like NtCreateFile, NtClose, NtDeviceIoControlFile
561 * and NtDuplicateObject so that they can provide in-place video effects to
562 * existing applications. Their assumption is that at least one thread tainted
563 * by their code stays around for the lifetime of the capture.
564 */
565 static gpointer
566 gst_ks_video_src_worker_func (gpointer data)
567 {
568 GstKsVideoSrc *self = data;
569 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
570
571 if (!gst_ks_video_src_open_device (self))
572 goto open_failed;
573
574 KS_WORKER_LOCK (priv);
575 priv->worker_state = KS_WORKER_STATE_READY;
576 KS_WORKER_NOTIFY_RESULT (priv);
577
578 while (priv->worker_state != KS_WORKER_STATE_STOPPING) {
579 KS_WORKER_WAIT (priv);
580
581 if (priv->worker_pending_caps != NULL) {
582 priv->worker_setcaps_result =
583 gst_ks_video_device_set_caps (priv->device,
584 priv->worker_pending_caps);
585
586 priv->worker_pending_caps = NULL;
587 KS_WORKER_NOTIFY_RESULT (priv);
588 } else if (priv->worker_pending_run) {
589 if (priv->ksclock != NULL)
590 gst_ks_clock_start (priv->ksclock);
591 priv->worker_run_result = gst_ks_video_device_set_state (priv->device,
592 KSSTATE_RUN, &priv->worker_error_code);
593
594 priv->worker_pending_run = FALSE;
595 KS_WORKER_NOTIFY_RESULT (priv);
596 }
597 }
598
599 KS_WORKER_UNLOCK (priv);
600
601 gst_ks_video_src_close_device (self);
602
603 return NULL;
604
605 /* ERRORS */
606 open_failed:
607 {
608 KS_WORKER_LOCK (priv);
609 priv->worker_state = KS_WORKER_STATE_ERROR;
610 KS_WORKER_NOTIFY_RESULT (priv);
611 KS_WORKER_UNLOCK (priv);
612
613 return NULL;
614 }
615 }
616
617 static gboolean
618 gst_ks_video_src_start_worker (GstKsVideoSrc * self)
619 {
620 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
621 gboolean result;
622
623 g_mutex_init (&priv->worker_lock);
624 g_cond_init (&priv->worker_notify_cond);
625 g_cond_init (&priv->worker_result_cond);
626
627 priv->worker_pending_caps = NULL;
628 priv->worker_pending_run = FALSE;
629
630 priv->worker_state = KS_WORKER_STATE_STARTING;
631 priv->worker_thread =
632 g_thread_new ("ks-worker", gst_ks_video_src_worker_func, self);
633
634 KS_WORKER_LOCK (priv);
635 while (priv->worker_state < KS_WORKER_STATE_READY)
636 KS_WORKER_WAIT_FOR_RESULT (priv);
637 result = priv->worker_state == KS_WORKER_STATE_READY;
638 KS_WORKER_UNLOCK (priv);
639
640 return result;
641 }
642
643 static void
644 gst_ks_video_src_stop_worker (GstKsVideoSrc * self)
645 {
646 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
647
648 KS_WORKER_LOCK (priv);
649 priv->worker_state = KS_WORKER_STATE_STOPPING;
650 KS_WORKER_NOTIFY (priv);
651 KS_WORKER_UNLOCK (priv);
652
653 g_thread_join (priv->worker_thread);
654 priv->worker_thread = NULL;
655
656 g_cond_clear (&priv->worker_result_cond);
657 g_cond_clear (&priv->worker_notify_cond);
658 g_mutex_clear (&priv->worker_lock);
659 }
660
661 static GstStateChangeReturn
662 gst_ks_video_src_change_state (GstElement * element, GstStateChange transition)
663 {
664 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (element);
665 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
666 GstStateChangeReturn ret;
667
668 switch (transition) {
669 case GST_STATE_CHANGE_NULL_TO_READY:
670 if (priv->enable_quirks)
671 gst_ks_video_src_apply_driver_quirks (self);
672 if (!gst_ks_video_src_start_worker (self))
673 goto open_failed;
674 break;
675 default:
676 break;
677 }
678
679 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
680
681 switch (transition) {
682 case GST_STATE_CHANGE_READY_TO_NULL:
683 gst_ks_video_src_stop_worker (self);
684 break;
685 default:
686 break;
687 }
688
689 return ret;
690
691 /* ERRORS */
692 open_failed:
693 {
694 gst_ks_video_src_stop_worker (self);
695 return GST_STATE_CHANGE_FAILURE;
696 }
697 }
698
699 static gboolean
700 gst_ks_video_src_set_clock (GstElement * element, GstClock * clock)
701 {
702 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (element);
703 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
704
705 GST_OBJECT_LOCK (element);
706 if (clock != NULL && priv->ksclock != NULL)
707 gst_ks_clock_provide_master_clock (priv->ksclock, clock);
708 GST_OBJECT_UNLOCK (element);
709
710 return GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
711 }
712
713 static GstCaps *
714 gst_ks_video_src_get_caps (GstBaseSrc * basesrc, GstCaps * filter)
715 {
716 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (basesrc);
717 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
718
719 if (priv->device != NULL)
720 return gst_ks_video_device_get_available_caps (priv->device);
721 else
722 return NULL; /* BaseSrc will return template caps */
723 }
724
725 static gboolean
726 gst_ks_video_src_set_caps (GstBaseSrc * basesrc, GstCaps * caps)
727 {
728 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (basesrc);
729 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
730
731 if (priv->device == NULL)
732 return FALSE;
733
734 KS_WORKER_LOCK (priv);
735 priv->worker_pending_caps = caps;
736 KS_WORKER_NOTIFY (priv);
737 while (priv->worker_pending_caps == caps)
738 KS_WORKER_WAIT_FOR_RESULT (priv);
739 KS_WORKER_UNLOCK (priv);
740
741 GST_DEBUG ("Result is %d", priv->worker_setcaps_result);
742 return priv->worker_setcaps_result;
743 }
744
745 static GstCaps *
746 gst_ks_video_src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
747 {
748 GstStructure *structure;
749 GstCaps *fixated_caps;
750 gint i;
751
752 fixated_caps = gst_caps_make_writable (caps);
753
754 for (i = 0; i < gst_caps_get_size (fixated_caps); ++i) {
755 structure = gst_caps_get_structure (fixated_caps, i);
756 gst_structure_fixate_field_nearest_int (structure, "width", G_MAXINT);
757 gst_structure_fixate_field_nearest_int (structure, "height", G_MAXINT);
758 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
759 G_MAXINT, 1);
760 }
761
762 return gst_caps_fixate (fixated_caps);
763 }
764
765 static gboolean
766 gst_ks_video_src_query (GstBaseSrc * basesrc, GstQuery * query)
767 {
768 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (basesrc);
769 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
770 gboolean result = FALSE;
771
772 switch (GST_QUERY_TYPE (query)) {
773 case GST_QUERY_LATENCY:{
774 GstClockTime min_latency, max_latency;
775
776 if (priv->device == NULL)
777 goto beach;
778
779 result = gst_ks_video_device_get_latency (priv->device, &min_latency,
780 &max_latency);
781 if (!result)
782 goto beach;
783
784 GST_DEBUG_OBJECT (self, "reporting latency of min %" GST_TIME_FORMAT
785 " max %" GST_TIME_FORMAT,
786 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
787
788 gst_query_set_latency (query, TRUE, min_latency, max_latency);
789 break;
790 }
791 default:
792 result = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
793 break;
794 }
795
796 beach:
797 return result;
798 }
799
800 static gboolean
801 gst_ks_video_src_unlock (GstBaseSrc * basesrc)
802 {
803 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (basesrc);
804 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
805
806 GST_DEBUG_OBJECT (self, "%s", G_STRFUNC);
807
808 gst_ks_video_device_cancel (priv->device);
809 return TRUE;
810 }
811
812 static gboolean
813 gst_ks_video_src_unlock_stop (GstBaseSrc * basesrc)
814 {
815 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (basesrc);
816 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
817
818 GST_DEBUG_OBJECT (self, "%s", G_STRFUNC);
819
820 gst_ks_video_device_cancel_stop (priv->device);
821 return TRUE;
822 }
823
824 static gboolean
825 gst_ks_video_src_timestamp_buffer (GstKsVideoSrc * self, GstBuffer * buf,
826 GstClockTime presentation_time)
827 {
828 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
829 GstClockTime duration;
830 GstClock *clock;
831 GstClockTime timestamp, base_time;
832
833 /* Don't timestamp muxed streams */
834 if (gst_ks_video_device_stream_is_muxed (priv->device)) {
835 duration = timestamp = GST_CLOCK_TIME_NONE;
836 goto timestamp;
837 }
838
839 duration = gst_ks_video_device_get_duration (priv->device);
840
841 GST_OBJECT_LOCK (self);
842 clock = GST_ELEMENT_CLOCK (self);
843 if (clock != NULL) {
844 gst_object_ref (clock);
845 base_time = GST_ELEMENT (self)->base_time;
846 } else {
847 timestamp = GST_CLOCK_TIME_NONE;
848 }
849 GST_OBJECT_UNLOCK (self);
850
851 if (clock != NULL) {
852 /* The time according to the current clock */
853 timestamp = gst_clock_get_time (clock) - base_time;
854 if (timestamp > duration)
855 timestamp -= duration;
856 else
857 timestamp = 0;
858
859 gst_object_unref (clock);
860 clock = NULL;
861 }
862
863 timestamp:
864 GST_BUFFER_PTS (buf) = timestamp;
865 GST_BUFFER_DTS (buf) = GST_CLOCK_TIME_NONE;
866 GST_BUFFER_DURATION (buf) = duration;
867
868 return TRUE;
869 }
870
871 static void
872 gst_ks_video_src_update_statistics (GstKsVideoSrc * self)
873 {
874 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
875 GstClock *clock;
876
877 GST_OBJECT_LOCK (self);
878 clock = GST_ELEMENT_CLOCK (self);
879 if (clock != NULL)
880 gst_object_ref (clock);
881 GST_OBJECT_UNLOCK (self);
882
883 if (clock != NULL) {
884 GstClockTime now = gst_clock_get_time (clock);
885 gst_object_unref (clock);
886
887 priv->count++;
888
889 if (GST_CLOCK_TIME_IS_VALID (priv->last_sampling)) {
890 if (now - priv->last_sampling >= GST_SECOND) {
891 GST_OBJECT_LOCK (self);
892 priv->fps = priv->count;
893 GST_OBJECT_UNLOCK (self);
894
895 g_object_notify (G_OBJECT (self), "fps");
896
897 priv->last_sampling = now;
898 priv->count = 0;
899 }
900 } else {
901 priv->last_sampling = now;
902 }
903 }
904 }
905
906 static GstFlowReturn
907 gst_ks_video_src_create (GstPushSrc * pushsrc, GstBuffer ** buf)
908 {
909 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (pushsrc);
910 GstKsVideoSrcPrivate *priv = GST_KS_VIDEO_SRC_GET_PRIVATE (self);
911 GstFlowReturn result;
912 GstClockTime presentation_time;
913 gulong error_code;
914 gchar *error_str;
915
916 g_assert (priv->device != NULL);
917
918 if (!gst_ks_video_device_has_caps (priv->device))
919 goto error_no_caps;
920
921 if (G_UNLIKELY (!priv->running)) {
922 KS_WORKER_LOCK (priv);
923 priv->worker_pending_run = TRUE;
924 KS_WORKER_NOTIFY (priv);
925 while (priv->worker_pending_run)
926 KS_WORKER_WAIT_FOR_RESULT (priv);
927 priv->running = priv->worker_run_result;
928 error_code = priv->worker_error_code;
929 KS_WORKER_UNLOCK (priv);
930
931 if (!priv->running)
932 goto error_start_capture;
933 }
934
935 do {
936 if (*buf != NULL) {
937 gst_buffer_unref (*buf);
938 *buf = NULL;
939 }
940
941 result = gst_ks_video_device_read_frame (priv->device, buf,
942 &presentation_time, &error_code, &error_str);
943 if (G_UNLIKELY (result != GST_FLOW_OK))
944 goto error_read_frame;
945 }
946 while (!gst_ks_video_src_timestamp_buffer (self, *buf, presentation_time));
947
948 if (G_UNLIKELY (priv->do_stats))
949 gst_ks_video_src_update_statistics (self);
950
951 if (!gst_ks_video_device_postprocess_frame (priv->device, buf)) {
952 GST_ELEMENT_ERROR (self, RESOURCE, FAILED, ("Postprocessing failed"),
953 ("Postprocessing failed"));
954 return GST_FLOW_ERROR;
955 }
956
957 return GST_FLOW_OK;
958
959 /* ERRORS */
960 error_no_caps:
961 {
962 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
963 ("not negotiated"), ("maybe setcaps failed?"));
964
965 return GST_FLOW_ERROR;
966 }
967 error_start_capture:
968 {
969 const gchar *debug_str = "failed to change pin state to KSSTATE_RUN";
970
971 switch (error_code) {
972 case ERROR_FILE_NOT_FOUND:
973 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
974 ("failed to start capture (device unplugged)"), ("%s", debug_str));
975 break;
976 case ERROR_NO_SYSTEM_RESOURCES:
977 GST_ELEMENT_ERROR (self, RESOURCE, BUSY,
978 ("failed to start capture (device already in use)"), ("%s",
979 debug_str));
980 break;
981 default:
982 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
983 ("failed to start capture (0x%08x)", (guint) error_code), ("%s",
984 debug_str));
985 break;
986 }
987
988 return GST_FLOW_ERROR;
989 }
990 error_read_frame:
991 {
992 if (result == GST_FLOW_ERROR) {
993 if (error_str != NULL) {
994 GST_ELEMENT_ERROR (self, RESOURCE, READ,
995 ("read failed: %s [0x%08x]", error_str, (guint) error_code),
996 ("gst_ks_video_device_read_frame failed"));
997 }
998 } else if (result == GST_FLOW_CUSTOM_ERROR) {
999 GST_ELEMENT_ERROR (self, RESOURCE, READ,
1000 ("read failed"), ("gst_ks_video_device_read_frame failed"));
1001 }
1002
1003 g_free (error_str);
1004
1005 return result;
1006 }
1007 }
1008
1009 static GstBuffer *
1010 gst_ks_video_src_alloc_buffer (guint size, guint alignment, gpointer user_data)
1011 {
1012 GstKsVideoSrc *self = GST_KS_VIDEO_SRC (user_data);
1013 GstBuffer *buf;
1014 GstAllocationParams params = { 0, alignment - 1, 0, 0, };
1015
1016 buf = gst_buffer_new_allocate (NULL, size, &params);
1017 if (buf == NULL)
1018 goto error_alloc_buffer;
1019
1020 return buf;
1021
1022 error_alloc_buffer:
1023 {
1024 GST_ELEMENT_ERROR (self, CORE, PAD, ("alloc_buffer failed"), (NULL));
1025
1026 return NULL;
1027 }
1028 }
1029
1030 static gboolean
1031 plugin_init (GstPlugin * plugin)
1032 {
1033 gst_ks_debug_init ();
1034
1035 if (!gst_element_register (plugin, "ksvideosrc",
1036 GST_RANK_PRIMARY, GST_TYPE_KS_VIDEO_SRC))
1037 return FALSE;
1038
1039 if (!gst_device_provider_register (plugin, "ksdeviceprovider",
1040 GST_RANK_PRIMARY, GST_TYPE_KS_DEVICE_PROVIDER))
1041 return FALSE;
1042
1043 return TRUE;
1044 }
1045
1046 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1047 GST_VERSION_MINOR,
1048 winks,
1049 "Windows kernel streaming plugin",
1050 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)