• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer command line playback testing utility - keyboard handling helpers
2  *
3  * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2013 Centricular Ltd
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "d3d11videosink-kb.h"
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <windows.h>
32 #include <io.h>
33 
34 #include <gst/gst.h>
35 
36 /* This is all not thread-safe, but doesn't have to be really */
37 static GstD3D11VideoSinkKbFunc kb_callback;
38 static gpointer kb_callback_data;
39 
40 typedef struct
41 {
42   GThread *thread;
43   HANDLE event_handle;
44   HANDLE console_handle;
45   gboolean closing;
46   GMutex lock;
47 } Win32KeyHandler;
48 
49 static Win32KeyHandler *win32_handler = NULL;
50 
51 static gboolean
gst_d3d11_video_sink_source_cb(Win32KeyHandler * handler)52 gst_d3d11_video_sink_source_cb (Win32KeyHandler * handler)
53 {
54   HANDLE h_input = handler->console_handle;
55   INPUT_RECORD buffer;
56   DWORD n;
57 
58   if (PeekConsoleInput (h_input, &buffer, 1, &n) && n == 1) {
59     ReadConsoleInput (h_input, &buffer, 1, &n);
60 
61     if (buffer.EventType == KEY_EVENT && buffer.Event.KeyEvent.bKeyDown) {
62       if (buffer.Event.KeyEvent.wVirtualKeyCode == VK_SPACE) {
63         kb_callback (' ', kb_callback_data);
64       } else {
65         kb_callback (buffer.Event.KeyEvent.uChar.AsciiChar, kb_callback_data);
66       }
67     }
68   }
69 
70   return G_SOURCE_REMOVE;
71 }
72 
73 static gpointer
gst_d3d11_video_sink_kb_thread(gpointer user_data)74 gst_d3d11_video_sink_kb_thread (gpointer user_data)
75 {
76   Win32KeyHandler *handler = (Win32KeyHandler *) user_data;
77   HANDLE handles[2];
78 
79   handles[0] = handler->event_handle;
80   handles[1] = handler->console_handle;
81 
82   if (!kb_callback)
83     return NULL;
84 
85   while (TRUE) {
86     DWORD ret = WaitForMultipleObjects (2, handles, FALSE, INFINITE);
87 
88     if (ret == WAIT_FAILED) {
89       GST_WARNING ("WaitForMultipleObject Failed");
90       return NULL;
91     }
92 
93     g_mutex_lock (&handler->lock);
94     if (handler->closing) {
95       g_mutex_unlock (&handler->lock);
96 
97       return NULL;
98     }
99     g_mutex_unlock (&handler->lock);
100 
101     g_idle_add ((GSourceFunc) gst_d3d11_video_sink_source_cb, handler);
102   }
103 
104   return NULL;
105 }
106 
107 gboolean
gst_d3d11_video_sink_kb_set_key_handler(GstD3D11VideoSinkKbFunc kb_func,gpointer user_data)108 gst_d3d11_video_sink_kb_set_key_handler (GstD3D11VideoSinkKbFunc kb_func,
109     gpointer user_data)
110 {
111   gint fd = _fileno (stdin);
112 
113   if (!_isatty (fd)) {
114     GST_INFO ("stdin is not connected to a terminal");
115     return FALSE;
116   }
117 
118   if (win32_handler) {
119     g_mutex_lock (&win32_handler->lock);
120     win32_handler->closing = TRUE;
121     g_mutex_unlock (&win32_handler->lock);
122 
123     SetEvent (win32_handler->event_handle);
124     g_thread_join (win32_handler->thread);
125     CloseHandle (win32_handler->event_handle);
126 
127     g_mutex_clear (&win32_handler->lock);
128     g_free (win32_handler);
129     win32_handler = NULL;
130   }
131 
132   if (kb_func) {
133     SECURITY_ATTRIBUTES sec_attrs;
134 
135     sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
136     sec_attrs.lpSecurityDescriptor = NULL;
137     sec_attrs.bInheritHandle = FALSE;
138 
139     win32_handler = g_new0 (Win32KeyHandler, 1);
140 
141     /* create cancellable event handle */
142     win32_handler->event_handle = CreateEvent (&sec_attrs, TRUE, FALSE, NULL);
143 
144     if (!win32_handler->event_handle) {
145       g_warning ("Couldn't create event handle\n");
146       g_free (win32_handler);
147       win32_handler = NULL;
148 
149       return FALSE;
150     }
151 
152     win32_handler->console_handle = GetStdHandle (STD_INPUT_HANDLE);
153     if (!win32_handler->console_handle) {
154       g_warning ("Couldn't get console handle\n");
155       CloseHandle (win32_handler->event_handle);
156       g_free (win32_handler);
157       win32_handler = NULL;
158 
159       return FALSE;
160     }
161 
162     g_mutex_init (&win32_handler->lock);
163     win32_handler->thread =
164         g_thread_new ("gst-play-kb", gst_d3d11_video_sink_kb_thread,
165         win32_handler);
166   }
167 
168   kb_callback = kb_func;
169   kb_callback_data = user_data;
170 
171   return TRUE;
172 }
173