1 /* Copyright (C) <2020> Philippe Normand <philn@igalia.com>
2 * Copyright (C) <2021> Thibault Saunier <tsaunier@igalia.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it under the terms of the
5 * GNU Library General Public License as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU Library General Public License along with this
13 * library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
14 * Boston, MA 02110-1301, USA.
15 */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <sys/mman.h>
21 #include <sys/types.h>
22 #include "gstwpeextension.h"
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #define gst_wpe_audio_sink_parent_class parent_class
29 GST_DEBUG_CATEGORY (wpe_audio_sink_debug);
30 #define GST_CAT_DEFAULT wpe_audio_sink_debug
31
32 struct _GstWpeAudioSink
33 {
34 GstBaseSink parent;
35
36 guint32 id;
37 GCancellable *cancellable;;
38
39 gchar *caps;
40
41 GMutex buf_lock;
42 GCond buf_cond;
43 GUnixFDList *fdlist;
44 };
45
46 static guint id = -1; /* atomic */
47
48 G_DEFINE_TYPE_WITH_CODE (GstWpeAudioSink, gst_wpe_audio_sink,
49 GST_TYPE_BASE_SINK, GST_DEBUG_CATEGORY_INIT (wpe_audio_sink_debug,
50 "wpeaudio_sink", 0, "WPE Sink"););
51
52 static GstStaticPadTemplate audio_sink_factory =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_PAD_SINK,
55 GST_PAD_ALWAYS,
56 GST_STATIC_CAPS ("audio/x-raw"));
57
58 static void
message_consumed_cb(GObject * source_object,GAsyncResult * res,GstWpeAudioSink * self)59 message_consumed_cb (GObject * source_object, GAsyncResult * res,
60 GstWpeAudioSink * self)
61 {
62 g_mutex_lock (&self->buf_lock);
63 g_cond_broadcast (&self->buf_cond);
64 g_mutex_unlock (&self->buf_lock);
65 }
66
67 static GstFlowReturn
render(GstBaseSink * sink,GstBuffer * buf)68 render (GstBaseSink * sink, GstBuffer * buf)
69 {
70 gsize written_bytes;
71 static int init = 0;
72 char filename[1024];
73 const gint *fds;
74 WebKitUserMessage *msg;
75 GstMapInfo info;
76 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (sink);
77
78 if (!self->caps) {
79 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
80 ("Trying to render buffer before caps were set"), (NULL));
81
82 return GST_FLOW_ERROR;
83 }
84
85 if (!gst_buffer_map (buf, &info, GST_MAP_READ)) {
86 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to map input buffer"),
87 (NULL));
88
89 return GST_FLOW_ERROR;
90 }
91
92 if (!self->fdlist) {
93 gint fds[1] = { -1 };
94
95 #ifdef HAVE_MEMFD_CREATE
96 fds[0] = memfd_create ("gstwpe-shm", MFD_CLOEXEC);
97 #endif
98
99 if (fds[0] < 0) {
100 /* allocate shm pool */
101 snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (),
102 "gstwpe-shm", init++, "XXXXXX");
103
104 fds[0] = g_mkstemp (filename);
105 if (fds[0] < 0) {
106 gst_buffer_unmap (buf, &info);
107 GST_ELEMENT_ERROR (self, RESOURCE, READ,
108 ("opening temp file %s failed: %s", filename, strerror (errno)),
109 (NULL));
110 return GST_FLOW_ERROR;
111 }
112
113 unlink (filename);
114 }
115
116 if (fds[0] <= 0)
117 goto write_error;
118
119 self->fdlist = g_unix_fd_list_new_from_array (fds, 1);
120 msg = webkit_user_message_new_with_fd_list ("gstwpe.set_shm",
121 g_variant_new ("(u)", self->id), self->fdlist);
122 gst_wpe_extension_send_message (msg, self->cancellable, NULL, NULL);
123 }
124
125 fds = g_unix_fd_list_peek_fds (self->fdlist, NULL);
126 if (ftruncate (fds[0], info.size) == -1)
127 goto write_error;
128
129 written_bytes = write (fds[0], info.data, info.size);
130 if (written_bytes < 0)
131 goto write_error;
132
133 if (written_bytes != info.size)
134 goto write_error;
135
136 if (lseek (fds[0], 0, SEEK_SET) == -1)
137 goto write_error;
138
139 msg = webkit_user_message_new ("gstwpe.new_buffer",
140 g_variant_new ("(ut)", self->id, info.size));
141
142 g_mutex_lock (&self->buf_lock);
143 gst_wpe_extension_send_message (msg, self->cancellable,
144 (GAsyncReadyCallback) message_consumed_cb, self);
145 g_cond_wait (&self->buf_cond, &self->buf_lock);
146 g_mutex_unlock (&self->buf_lock);
147
148 gst_buffer_unmap (buf, &info);
149
150 return GST_FLOW_OK;
151
152 write_error:
153 gst_buffer_unmap (buf, &info);
154 GST_ELEMENT_ERROR (self, RESOURCE, WRITE, ("Couldn't write memfd: %s",
155 strerror (errno)), (NULL));
156
157 return GST_FLOW_ERROR;
158 }
159
160 static gboolean
set_caps(GstBaseSink * sink,GstCaps * caps)161 set_caps (GstBaseSink * sink, GstCaps * caps)
162 {
163 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (sink);
164 gchar *stream_id;
165
166 if (self->caps) {
167 GST_ERROR_OBJECT (sink, "Renegotiation is not supported yet");
168
169 return FALSE;
170 }
171
172 self->caps = gst_caps_to_string (caps);
173 self->id = g_atomic_int_add (&id, 1);
174 stream_id = gst_pad_get_stream_id (GST_BASE_SINK_PAD (sink));
175 gst_wpe_extension_send_message (webkit_user_message_new ("gstwpe.new_stream",
176 g_variant_new ("(uss)", self->id, self->caps, stream_id)),
177 self->cancellable, NULL, NULL);
178 g_free (stream_id);
179
180 return TRUE;
181 }
182
183 static gboolean
unlock(GstBaseSink * sink)184 unlock (GstBaseSink * sink)
185 {
186 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (sink);
187
188 g_cancellable_cancel (self->cancellable);
189
190 return TRUE;
191 }
192
193 static gboolean
unlock_stop(GstBaseSink * sink)194 unlock_stop (GstBaseSink * sink)
195 {
196 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (sink);
197 GCancellable *cancellable = self->cancellable;
198
199 self->cancellable = g_cancellable_new ();
200 g_object_unref (cancellable);
201
202 return TRUE;
203 }
204
205 static void
_cancelled_cb(GCancellable * _cancellable,GstWpeAudioSink * self)206 _cancelled_cb (GCancellable * _cancellable, GstWpeAudioSink * self)
207 {
208 g_mutex_lock (&self->buf_lock);
209 g_cond_broadcast (&self->buf_cond);
210 g_mutex_unlock (&self->buf_lock);
211 }
212
213 static gboolean
stop(GstBaseSink * sink)214 stop (GstBaseSink * sink)
215 {
216 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (sink);
217
218 if (!self->caps) {
219 GST_DEBUG_OBJECT (sink, "Stopped before started");
220
221 return TRUE;
222 }
223
224 /* Stop processing and claim buffers back */
225 g_cancellable_cancel (self->cancellable);
226
227 GST_DEBUG_OBJECT (sink, "Stopping %d", self->id);
228 gst_wpe_extension_send_message (webkit_user_message_new ("gstwpe.stop",
229 g_variant_new_uint32 (self->id)), self->cancellable, NULL, NULL);
230
231 return TRUE;
232 }
233
234 static GstStateChangeReturn
change_state(GstElement * element,GstStateChange transition)235 change_state (GstElement * element, GstStateChange transition)
236 {
237 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (element);
238
239 switch (transition) {
240 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
241 {
242 if (g_cancellable_is_cancelled (self->cancellable)) {
243 GCancellable *cancellable = self->cancellable;
244 self->cancellable = g_cancellable_new ();
245
246 g_object_unref (cancellable);
247 }
248 break;
249 }
250 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
251 g_cancellable_cancel (self->cancellable);
252
253 gst_wpe_extension_send_message (webkit_user_message_new ("gstwpe.pause",
254 g_variant_new_uint32 (self->id)), NULL, NULL, NULL);
255
256 break;
257 default:
258 break;
259 }
260
261 return GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS,
262 change_state, (element, transition), GST_STATE_CHANGE_SUCCESS);
263 }
264
265 static void
dispose(GObject * object)266 dispose (GObject * object)
267 {
268 GstWpeAudioSink *self = GST_WPE_AUDIO_SINK (object);
269
270 g_clear_object (&self->cancellable);
271 g_clear_pointer (&self->caps, g_free);
272 }
273
274 static void
gst_wpe_audio_sink_init(GstWpeAudioSink * self)275 gst_wpe_audio_sink_init (GstWpeAudioSink * self)
276 {
277 GstElementClass *klass = GST_ELEMENT_GET_CLASS (self);
278 GstPadTemplate *pad_template =
279 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
280 g_return_if_fail (pad_template != NULL);
281
282 self->cancellable = g_cancellable_new ();
283 g_cancellable_connect (self->cancellable,
284 G_CALLBACK (_cancelled_cb), self, NULL);
285 }
286
287 static void
gst_wpe_audio_sink_class_init(GstWpeAudioSinkClass * klass)288 gst_wpe_audio_sink_class_init (GstWpeAudioSinkClass * klass)
289 {
290 GstPadTemplate *tmpl;
291 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
292 GObjectClass *object_class = G_OBJECT_CLASS (klass);
293 GstBaseSinkClass *gstbasesink_class = (GstBaseSinkClass *) klass;
294
295 object_class->dispose = dispose;
296
297 gst_element_class_set_static_metadata (gstelement_class,
298 "WPE internal audio sink", "Sink/Audio",
299 "Internal sink to be used in wpe when running inside gstwpe",
300 "Thibault Saunier <tsaunier@igalia.com>");
301
302 tmpl = gst_static_pad_template_get (&audio_sink_factory);
303 gst_element_class_add_pad_template (gstelement_class, tmpl);
304
305 gstelement_class->change_state = GST_DEBUG_FUNCPTR (change_state);
306 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (stop);
307 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (unlock);
308 gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (unlock_stop);
309 gstbasesink_class->render = GST_DEBUG_FUNCPTR (render);
310 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (set_caps);
311 }
312