va: VA-API H.264 decoder and infrastructure
[platform/upstream/gstreamer.git] / sys / va / gstvadevice.c
1 /* GStreamer
2  * Copyright (C) 2020 Igalia, S.L.
3  *     Author: Víctor Jáquez <vjaquez@igalia.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 "gstvadevice.h"
26
27 #include <gudev/gudev.h>
28 #include "gstvadisplay_drm.h"
29
30 #define GST_CAT_DEFAULT gstva_debug
31 GST_DEBUG_CATEGORY_EXTERN (gstva_debug);
32
33 GST_DEFINE_MINI_OBJECT_TYPE (GstVaDevice, gst_va_device);
34
35 static void
36 gst_va_device_free (GstVaDevice * device)
37 {
38   if (device->display)
39     gst_object_unref (device->display);
40   g_free (device->render_device_path);
41   g_free (device);
42 }
43
44 static GstVaDevice *
45 gst_va_device_new (GstVaDisplay * display, const gchar * render_device_path)
46 {
47   GstVaDevice *device = g_new0 (GstVaDevice, 1);
48
49   gst_mini_object_init (GST_MINI_OBJECT_CAST (device), 0, GST_TYPE_VA_DEVICE,
50       NULL, NULL, (GstMiniObjectFreeFunction) gst_va_device_free);
51
52   /* take ownership */
53   device->display = display;
54   device->render_device_path = g_strdup (render_device_path);
55
56   return device;
57 }
58
59 GList *
60 gst_va_device_find_devices (void)
61 {
62   GUdevClient *client;
63   GList *udev_devices, *dev;
64   GQueue devices = G_QUEUE_INIT;
65
66   client = g_udev_client_new (NULL);
67   udev_devices = g_udev_client_query_by_subsystem (client, "drm");
68
69   for (dev = udev_devices; dev; dev = g_list_next (dev)) {
70     GstVaDisplay *dpy;
71     GUdevDevice *udev = (GUdevDevice *) dev->data;
72     const gchar *path = g_udev_device_get_device_file (udev);
73     const gchar *name = g_udev_device_get_name (udev);
74
75     if (!path || !g_str_has_prefix (name, "renderD")) {
76       GST_LOG ("Ignoring %s in %s", name, path);
77       continue;
78     }
79
80     if (!(dpy = gst_va_display_drm_new_from_path (path)))
81       continue;
82
83     GST_INFO ("Found VA-API device: %s", path);
84     g_queue_push_tail (&devices, gst_va_device_new (dpy, path));
85   }
86
87   g_list_free_full (udev_devices, g_object_unref);
88   g_object_unref (client);
89
90   return devices.head;
91 }
92
93 void
94 gst_va_device_list_free (GList * devices)
95 {
96   g_list_free_full (devices, (GDestroyNotify) gst_mini_object_unref);
97 }