• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "gstmfconfig.h"
26 
27 #include "gstmfsourceobject.h"
28 
29 #if GST_MF_WINAPI_APP
30 #include "gstmfcapturewinrt.h"
31 #endif
32 #if GST_MF_WINAPI_DESKTOP
33 #include "gstmfsourcereader.h"
34 #endif
35 
36 GST_DEBUG_CATEGORY_EXTERN (gst_mf_source_object_debug);
37 #define GST_CAT_DEFAULT gst_mf_source_object_debug
38 
39 enum
40 {
41   PROP_0,
42   PROP_DEVICE_PATH,
43   PROP_DEVICE_NAME,
44   PROP_DEVICE_INDEX,
45   PROP_SOURCE_TYPE,
46 };
47 
48 #define DEFAULT_DEVICE_PATH         NULL
49 #define DEFAULT_DEVICE_NAME         NULL
50 #define DEFAULT_DEVICE_INDEX        -1
51 #define DEFAULT_SOURCE_TYPE        GST_MF_SOURCE_TYPE_VIDEO
52 
53 GType
gst_mf_source_type_get_type(void)54 gst_mf_source_type_get_type (void)
55 {
56   static GType source_type = 0;
57 
58   static const GEnumValue source_types[] = {
59     {GST_MF_SOURCE_TYPE_VIDEO, "Video", "video"},
60     {0, NULL, NULL}
61   };
62 
63   if (!source_type) {
64     source_type = g_enum_register_static ("GstMFSourceMode", source_types);
65   }
66 
67   return source_type;
68 }
69 
70 static void gst_mf_source_object_finalize (GObject * object);
71 static void gst_mf_source_object_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73 static void gst_mf_source_object_set_property (GObject * object, guint prop_id,
74     const GValue * value, GParamSpec * pspec);
75 
76 #define gst_mf_source_object_parent_class parent_class
77 G_DEFINE_ABSTRACT_TYPE (GstMFSourceObject, gst_mf_source_object,
78     GST_TYPE_OBJECT);
79 
80 static void
gst_mf_source_object_class_init(GstMFSourceObjectClass * klass)81 gst_mf_source_object_class_init (GstMFSourceObjectClass * klass)
82 {
83   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
84 
85   gobject_class->finalize = gst_mf_source_object_finalize;
86   gobject_class->get_property = gst_mf_source_object_get_property;
87   gobject_class->set_property = gst_mf_source_object_set_property;
88 
89   g_object_class_install_property (gobject_class, PROP_DEVICE_PATH,
90       g_param_spec_string ("device-path", "Device Path",
91           "The device path", DEFAULT_DEVICE_PATH,
92           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
93   g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
94       g_param_spec_string ("device-name", "Device Name",
95           "The human-readable device name", DEFAULT_DEVICE_NAME,
96           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
97   g_object_class_install_property (gobject_class, PROP_DEVICE_INDEX,
98       g_param_spec_int ("device-index", "Device Index",
99           "The zero-based device index", -1, G_MAXINT, DEFAULT_DEVICE_INDEX,
100           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
101   g_object_class_install_property (gobject_class, PROP_SOURCE_TYPE,
102       g_param_spec_enum ("source-type", "Source Type",
103           "Source Type", GST_TYPE_MF_SOURCE_TYPE,
104           DEFAULT_SOURCE_TYPE,
105           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
106 }
107 
108 static void
gst_mf_source_object_init(GstMFSourceObject * self)109 gst_mf_source_object_init (GstMFSourceObject * self)
110 {
111   self->device_index = DEFAULT_DEVICE_INDEX;
112   self->source_type = DEFAULT_SOURCE_TYPE;
113 
114   g_weak_ref_init (&self->client, NULL);
115 }
116 
117 static void
gst_mf_source_object_finalize(GObject * object)118 gst_mf_source_object_finalize (GObject * object)
119 {
120   GstMFSourceObject *self = GST_MF_SOURCE_OBJECT (object);
121 
122   g_free (self->device_path);
123   g_free (self->device_name);
124 
125   g_weak_ref_clear (&self->client);
126 
127   G_OBJECT_CLASS (parent_class)->finalize (object);
128 }
129 
130 static void
gst_mf_source_object_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)131 gst_mf_source_object_get_property (GObject * object, guint prop_id,
132     GValue * value, GParamSpec * pspec)
133 {
134   GstMFSourceObject *self = GST_MF_SOURCE_OBJECT (object);
135 
136   switch (prop_id) {
137     case PROP_DEVICE_PATH:
138       g_value_set_string (value, self->device_path);
139       break;
140     case PROP_DEVICE_NAME:
141       g_value_set_string (value, self->device_name);
142       break;
143     case PROP_DEVICE_INDEX:
144       g_value_set_int (value, self->device_index);
145       break;
146     case PROP_SOURCE_TYPE:
147       g_value_set_enum (value, self->source_type);
148       break;
149     default:
150       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151       break;
152   }
153 }
154 
155 static void
gst_mf_source_object_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)156 gst_mf_source_object_set_property (GObject * object, guint prop_id,
157     const GValue * value, GParamSpec * pspec)
158 {
159   GstMFSourceObject *self = GST_MF_SOURCE_OBJECT (object);
160 
161   switch (prop_id) {
162     case PROP_DEVICE_PATH:
163       g_free (self->device_path);
164       self->device_path = g_value_dup_string (value);
165       break;
166     case PROP_DEVICE_NAME:
167       g_free (self->device_name);
168       self->device_name = g_value_dup_string (value);
169       break;
170     case PROP_DEVICE_INDEX:
171       self->device_index = g_value_get_int (value);
172       break;
173     case PROP_SOURCE_TYPE:
174       self->source_type = g_value_get_enum (value);
175       break;
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181 
182 gboolean
gst_mf_source_object_start(GstMFSourceObject * object)183 gst_mf_source_object_start (GstMFSourceObject * object)
184 {
185   GstMFSourceObjectClass *klass;
186 
187   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), FALSE);
188 
189   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
190   g_assert (klass->start != NULL);
191 
192   return klass->start (object);
193 }
194 
195 gboolean
gst_mf_source_object_stop(GstMFSourceObject * object)196 gst_mf_source_object_stop (GstMFSourceObject * object)
197 {
198   GstMFSourceObjectClass *klass;
199 
200   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), FALSE);
201 
202   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
203   g_assert (klass->stop != NULL);
204 
205   return klass->stop (object);
206 }
207 
208 GstFlowReturn
gst_mf_source_object_fill(GstMFSourceObject * object,GstBuffer * buffer)209 gst_mf_source_object_fill (GstMFSourceObject * object, GstBuffer * buffer)
210 {
211   GstMFSourceObjectClass *klass;
212 
213   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), GST_FLOW_ERROR);
214   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
215 
216   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
217   g_assert (klass->fill != NULL);
218 
219   return klass->fill (object, buffer);
220 }
221 
222 GstFlowReturn
gst_mf_source_object_create(GstMFSourceObject * object,GstBuffer ** buffer)223 gst_mf_source_object_create (GstMFSourceObject * object, GstBuffer ** buffer)
224 {
225   GstMFSourceObjectClass *klass;
226 
227   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), GST_FLOW_ERROR);
228   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
229 
230   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
231   g_assert (klass->create != NULL);
232 
233   return klass->create (object, buffer);
234 }
235 
236 void
gst_mf_source_object_set_flushing(GstMFSourceObject * object,gboolean flushing)237 gst_mf_source_object_set_flushing (GstMFSourceObject * object,
238     gboolean flushing)
239 {
240   GstMFSourceObjectClass *klass;
241 
242   g_return_if_fail (GST_IS_MF_SOURCE_OBJECT (object));
243 
244   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
245 
246   if (flushing) {
247     if (klass->unlock)
248       klass->unlock (object);
249   } else {
250     if (klass->unlock_stop)
251       klass->unlock_stop (object);
252   }
253 }
254 
255 gboolean
gst_mf_source_object_set_caps(GstMFSourceObject * object,GstCaps * caps)256 gst_mf_source_object_set_caps (GstMFSourceObject * object, GstCaps * caps)
257 {
258   GstMFSourceObjectClass *klass;
259 
260   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), FALSE);
261 
262   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
263   g_assert (klass->set_caps != NULL);
264 
265   return klass->set_caps (object, caps);
266 }
267 
268 GstCaps *
gst_mf_source_object_get_caps(GstMFSourceObject * object)269 gst_mf_source_object_get_caps (GstMFSourceObject * object)
270 {
271   GstMFSourceObjectClass *klass;
272 
273   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), NULL);
274 
275   klass = GST_MF_SOURCE_OBJECT_GET_CLASS (object);
276   g_assert (klass->get_caps != NULL);
277 
278   return klass->get_caps (object);
279 }
280 
281 gboolean
gst_mf_source_object_set_client(GstMFSourceObject * object,GstElement * client)282 gst_mf_source_object_set_client (GstMFSourceObject * object,
283     GstElement * client)
284 {
285   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), FALSE);
286 
287   g_weak_ref_set (&object->client, client);
288 
289   return TRUE;
290 }
291 
292 GstClockTime
gst_mf_source_object_get_running_time(GstMFSourceObject * object)293 gst_mf_source_object_get_running_time (GstMFSourceObject * object)
294 {
295   GstElement *client = NULL;
296   GstClockTime timestamp = GST_CLOCK_TIME_NONE;
297 
298   g_return_val_if_fail (GST_IS_MF_SOURCE_OBJECT (object), GST_CLOCK_TIME_NONE);
299 
300   client = (GstElement *) g_weak_ref_get (&object->client);
301   if (client) {
302     GstClockTime basetime = client->base_time;
303     GstClock *clock;
304 
305     clock = gst_element_get_clock (client);
306     if (clock) {
307       GstClockTime now;
308 
309       now = gst_clock_get_time (clock);
310       timestamp = now - basetime;
311       gst_object_unref (clock);
312     }
313 
314     gst_object_unref (client);
315   }
316 
317   return timestamp;
318 }
319 
320 static gboolean
gst_mf_source_object_use_winrt_api(void)321 gst_mf_source_object_use_winrt_api (void)
322 {
323   static gsize check_once = 0;
324   static gboolean ret = FALSE;
325 
326   if (g_once_init_enter (&check_once)) {
327 #if (!GST_MF_WINAPI_APP)
328     /* WinRT is not supported, always false */
329     ret = FALSE;
330 #else
331 #if (!GST_MF_WINAPI_DESKTOP)
332     /* WinRT is supported but desktop API was disabled,
333      * always true */
334     ret = TRUE;
335 #else
336     /* Both app and desktop APIs were enabled, check user choice */
337     {
338       const gchar *env;
339 
340       env = g_getenv ("GST_USE_MF_WINRT_CAPTURE");
341       if (env && g_str_has_prefix (env, "1"))
342         ret = TRUE;
343       else
344         ret = FALSE;
345     }
346 #endif
347 #endif
348     g_once_init_leave (&check_once, 1);
349   }
350 
351   return ret;
352 }
353 
354 GstMFSourceObject *
gst_mf_source_object_new(GstMFSourceType type,gint device_index,const gchar * device_name,const gchar * device_path,gpointer dispatcher)355 gst_mf_source_object_new (GstMFSourceType type, gint device_index,
356     const gchar * device_name, const gchar * device_path, gpointer dispatcher)
357 {
358 #if (!GST_MF_WINAPI_APP)
359   GST_INFO ("Try IMFSourceReader implementation");
360   return gst_mf_source_reader_new (type,
361       device_index, device_name, device_path);
362 #else
363 #if (!GST_MF_WINAPI_DESKTOP)
364   GST_INFO ("Try WinRT implementation");
365   return gst_mf_capture_winrt_new (type,
366       device_index, device_name, device_path, dispatcher);
367 #else
368   if (gst_mf_source_object_use_winrt_api ()) {
369     GST_INFO ("Both Desktop and WinRT APIs were enabled, user choice: WinRT");
370     return gst_mf_capture_winrt_new (type,
371         device_index, device_name, device_path, dispatcher);
372   } else {
373     GST_INFO
374         ("Both Desktop and WinRT APIs were enabled, default: IMFSourceReader");
375     return gst_mf_source_reader_new (type,
376         device_index, device_name, device_path);
377   }
378 #endif
379 #endif
380   g_assert_not_reached ();
381 
382   return NULL;
383 }
384 
385 gint
gst_mf_source_object_caps_compare(GstCaps * caps1,GstCaps * caps2)386 gst_mf_source_object_caps_compare (GstCaps * caps1, GstCaps * caps2)
387 {
388   GstStructure *s1, *s2;
389   const gchar *n1, *n2;
390   gboolean m1_is_raw, m2_is_raw;
391   gint w1 = 0, h1 = 0, w2 = 0, h2 = 0;
392   gint r1, r2;
393   gint num1 = 0, den1 = 1, num2 = 0, den2 = 1;
394   gint fraction_cmp;
395 
396   /* sorting priority
397    * - raw video > comprssed
398    *   - raw video format
399    * - higher resolution
400    * - higher framerate
401    */
402   s1 = gst_caps_get_structure (caps1, 0);
403   n1 = gst_structure_get_name (s1);
404 
405   s2 = gst_caps_get_structure (caps2, 0);
406   n2 = gst_structure_get_name (s2);
407 
408   m1_is_raw = g_strcmp0 (n1, "video/x-raw") == 0;
409   m2_is_raw = g_strcmp0 (n2, "video/x-raw") == 0;
410 
411   if (m1_is_raw && !m2_is_raw)
412     return -1;
413   else if (!m1_is_raw && m2_is_raw)
414     return 1;
415 
416   /* if both are raw formats */
417   if (m1_is_raw) {
418     gint format_cmp = g_strcmp0 (gst_structure_get_string (s1, "format"),
419         gst_structure_get_string (s2, "format"));
420     if (format_cmp)
421       return format_cmp;
422   }
423 
424   /* resolution */
425   gst_structure_get_int (s1, "width", &w1);
426   gst_structure_get_int (s1, "height", &h1);
427   gst_structure_get_int (s2, "width", &w2);
428   gst_structure_get_int (s2, "height", &h2);
429 
430   r1 = w1 * h1;
431   r2 = w2 * h2;
432 
433   /* higher resolution first */
434   if (r1 != r2)
435     return r2 - r1;
436 
437   gst_structure_get_fraction (s1, "framerate", &num1, &den1);
438   gst_structure_get_fraction (s2, "framerate", &num2, &den2);
439 
440   fraction_cmp = gst_util_fraction_compare (num1, den1, num2, den2);
441 
442   /* higher framerate first */
443   return fraction_cmp * -1;
444 }
445