winegstreamer: Introduce new link_src_to_element / link_element_to_sink helpers.
[wine.git] / dlls / winegstreamer / unixlib.c
blob7111402bf0ac1c33b6d4357c124e32a2583cc3ba
1 /*
2 * winegstreamer Unix library interface
4 * Copyright 2020-2021 Zebediah Figura for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #if 0
22 #pragma makedep unix
23 #endif
25 #include "config.h"
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
31 #include <gst/gst.h>
32 #include <gst/video/video.h>
33 #include <gst/audio/audio.h>
34 #include <gst/tag/tag.h>
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "winternl.h"
39 #include "dshow.h"
41 #include "unix_private.h"
43 /* GStreamer callbacks may be called on threads not created by Wine, and
44 * therefore cannot access the Wine TEB. This means that we must use GStreamer
45 * debug logging instead of Wine debug logging. In order to be safe we forbid
46 * any use of Wine debug logging in this entire file. */
48 GST_DEBUG_CATEGORY(wine);
50 GstElement *create_element(const char *name, const char *plugin_set)
52 GstElement *element;
54 if (!(element = gst_element_factory_make(name, NULL)))
55 fprintf(stderr, "winegstreamer: failed to create %s, are %u-bit GStreamer \"%s\" plugins installed?\n",
56 name, 8 * (unsigned int)sizeof(void *), plugin_set);
57 return element;
60 GstElement *find_element(GstElementFactoryListType type, GstCaps *src_caps, GstCaps *sink_caps)
62 GstElement *element = NULL;
63 GList *tmp, *transforms;
64 const gchar *name;
66 if (!(transforms = gst_element_factory_list_get_elements(type, GST_RANK_MARGINAL)))
67 goto done;
69 tmp = gst_element_factory_list_filter(transforms, src_caps, GST_PAD_SINK, FALSE);
70 gst_plugin_feature_list_free(transforms);
71 if (!(transforms = tmp))
72 goto done;
74 tmp = gst_element_factory_list_filter(transforms, sink_caps, GST_PAD_SRC, FALSE);
75 gst_plugin_feature_list_free(transforms);
76 if (!(transforms = tmp))
77 goto done;
79 transforms = g_list_sort(transforms, gst_plugin_feature_rank_compare_func);
80 for (tmp = transforms; tmp != NULL && element == NULL; tmp = tmp->next)
82 name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(tmp->data));
83 if (!(element = gst_element_factory_create(GST_ELEMENT_FACTORY(tmp->data), NULL)))
84 GST_WARNING("Failed to create %s element.", name);
86 gst_plugin_feature_list_free(transforms);
88 done:
89 if (element)
91 GST_DEBUG("Created %s element %p.", name, element);
93 else
95 gchar *src_str = gst_caps_to_string(src_caps), *sink_str = gst_caps_to_string(sink_caps);
96 GST_WARNING("Failed to create element matching caps %s / %s.", src_str, sink_str);
97 g_free(sink_str);
98 g_free(src_str);
101 return element;
104 bool append_element(GstElement *container, GstElement *element, GstElement **first, GstElement **last)
106 gchar *name = gst_element_get_name(element);
107 bool success = false;
109 if (!gst_bin_add(GST_BIN(container), element) ||
110 !gst_element_sync_state_with_parent(element) ||
111 (*last && !gst_element_link(*last, element)))
113 GST_ERROR("Failed to link %s element.", name);
115 else
117 GST_DEBUG("Linked %s element %p.", name, element);
118 if (!*first)
119 *first = element;
120 *last = element;
121 success = true;
124 g_free(name);
125 return success;
128 bool link_src_to_element(GstPad *src_pad, GstElement *element)
130 GstPadLinkReturn ret;
131 GstPad *sink_pad;
133 if (!(sink_pad = gst_element_get_static_pad(element, "sink")))
135 gchar *name = gst_element_get_name(element);
136 GST_ERROR("Failed to find sink pad on %s", name);
137 g_free(name);
138 return false;
140 if ((ret = gst_pad_link(src_pad, sink_pad)))
142 gchar *src_name = gst_pad_get_name(src_pad), *sink_name = gst_pad_get_name(sink_pad);
143 GST_ERROR("Failed to link element pad %s with pad %s", src_name, sink_name);
144 g_free(sink_name);
145 g_free(src_name);
147 gst_object_unref(sink_pad);
148 return !ret;
151 bool link_element_to_sink(GstElement *element, GstPad *sink_pad)
153 GstPadLinkReturn ret;
154 GstPad *src_pad;
156 if (!(src_pad = gst_element_get_static_pad(element, "src")))
158 gchar *name = gst_element_get_name(element);
159 GST_ERROR("Failed to find src pad on %s", name);
160 g_free(name);
161 return false;
163 if ((ret = gst_pad_link(src_pad, sink_pad)))
165 gchar *src_name = gst_pad_get_name(src_pad), *sink_name = gst_pad_get_name(sink_pad);
166 GST_ERROR("Failed to link pad %s with element pad %s", src_name, sink_name);
167 g_free(sink_name);
168 g_free(src_name);
170 gst_object_unref(src_pad);
171 return !ret;
174 NTSTATUS wg_init_gstreamer(void *arg)
176 char arg0[] = "wine";
177 char arg1[] = "--gst-disable-registry-fork";
178 char *args[] = {arg0, arg1, NULL};
179 int argc = ARRAY_SIZE(args) - 1;
180 char **argv = args;
181 GError *err;
183 if (!gst_init_check(&argc, &argv, &err))
185 fprintf(stderr, "winegstreamer: failed to initialize GStreamer: %s\n", err->message);
186 g_error_free(err);
187 return STATUS_UNSUCCESSFUL;
190 GST_DEBUG_CATEGORY_INIT(wine, "WINE", GST_DEBUG_FG_RED, "Wine GStreamer support");
192 GST_INFO("GStreamer library version %s; wine built with %d.%d.%d.",
193 gst_version_string(), GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO);
194 return STATUS_SUCCESS;