Debian Codegraph @

src/xrt/auxiliary/gstreamer/gst_sink.c

raw symbols SCIP
1 // Copyright 2019-2022, Collabora, Ltd.cxx . . $ `<file>/src/xrt/auxiliary/gstreamer/gst_sink.c`/
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4 * @file
5 * @brief An @ref xrt_frame_sink that does gst things.
6 * @author Jakob Bornecrantz <[email protected]>
7 * @author Aaron Boxer <[email protected]>
8 * @author Moshi Turner <[email protected]>
9 * @ingroup aux_util
10 */
11
12 #include "util/u_trace_marker.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_trace_marker.h`/
13 #include "util/u_misc.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_misc.h`/
14 #include "util/u_debug.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_debug.h`/
15 #include "util/u_format.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_format.h`/
16
17 #include "gstreamer/gst_sink.h"cxx . . $ `<file>/src/xrt/auxiliary/gstreamer/gst_sink.h`/
18 #include "gstreamer/gst_pipeline.h"cxx . . $ `<file>/src/xrt/auxiliary/gstreamer/gst_pipeline.h`/
19 #include "gstreamer/gst_internal.h"cxx . . $ `<file>/src/xrt/auxiliary/gstreamer/gst_internal.h`/
20 #include "gst/video/video-format.h"
21 #include "gst/video/gstvideometa.h"
22 #include "gst/app/gstappsink.h"
23 #include "gst/app/gstappsrc.h"
24
25 #include <assert.h>
26
27
28 /*
29 *
30 * Internal sink functions.
31 *
32 */
33
34 static void
35 wrapped_buffer_destroycxx . . $ wrapped_buffer_destroy(5d22bdacc48458e8).(gpointercxx . . $ gpointer# data)
36 {
37 struct xrt_framecxx . . $ xrt_frame# *xf = (struct xrt_framecxx . . $ xrt_frame# *)data;
38
39 U_LOG_Tcxx . . $ `src/xrt/auxiliary/util/u_logging.h:292:9`!("Called");
40
41 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&xf, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
42 }
43
44 static GstVideoFormatcxx . . $ GstVideoFormat#
45 gst_fmt_from_xf_formatcxx . . $ gst_fmt_from_xf_format(bf846e6c4ef3bcd9).(enum xrt_formatcxx . . $ xrt_format# format_in)
46 {
47 switch (format_in) {
48 case XRT_FORMAT_R8G8B8cxx . . $ XRT_FORMAT_R8G8B8.: return GST_VIDEO_FORMAT_RGBcxx . . $ GST_VIDEO_FORMAT_RGB.;
49 case XRT_FORMAT_R8G8B8A8cxx . . $ XRT_FORMAT_R8G8B8A8.: return GST_VIDEO_FORMAT_RGBAcxx . . $ GST_VIDEO_FORMAT_RGBA.;
50 case XRT_FORMAT_R8G8B8X8cxx . . $ XRT_FORMAT_R8G8B8X8.: return GST_VIDEO_FORMAT_RGBxcxx . . $ GST_VIDEO_FORMAT_RGBx.;
51 case XRT_FORMAT_YUYV422cxx . . $ XRT_FORMAT_YUYV422.: return GST_VIDEO_FORMAT_YUY2cxx . . $ GST_VIDEO_FORMAT_YUY2.;
52 case XRT_FORMAT_L8cxx . . $ XRT_FORMAT_L8.: return GST_VIDEO_FORMAT_GRAY8cxx . . $ GST_VIDEO_FORMAT_GRAY8.;
53 default: assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); return GST_VIDEO_FORMAT_UNKNOWNcxx . . $ GST_VIDEO_FORMAT_UNKNOWN.;
54 }
55 }
56
57 static void
58 complain_if_wrong_image_sizecxx . . $ complain_if_wrong_image_size(f2271a9b8bde25fb).(struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs, struct xrt_framecxx . . $ xrt_frame# *xf)
59 {
60 if (!gs->need_even_dimscxx . . $ gstreamer_sink#need_even_dims.) {
61 return;
62 }
63
64 // libx264 is the actual source of this requirement; it refuses to handle odd widths/heights when encoding I420
65 // subsampled content. OpenH264 should work, but it's easy enough to just force all users of this code to
66 // provide normal-sized inputs.
67 if (xf->widthcxx . . $ xrt_frame#width. % 2 == 1) {
68 if (!gs->have_padded_widthcxx . . $ gstreamer_sink#have_padded_width.) {
69 U_LOG_Wcxx . . $ `src/xrt/auxiliary/util/u_logging.h:301:9`!("Image width needs to be divisible by 2!");
70 }
71 } else if (gs->have_padded_widthcxx . . $ gstreamer_sink#have_padded_width.) {
72 U_LOG_Wcxx . . $ `src/xrt/auxiliary/util/u_logging.h:301:9`!("Image width changed after we added padding!");
73 }
74
75 if (xf->heightcxx . . $ xrt_frame#height. % 2 == 1) {
76 if (!gs->have_padded_heightcxx . . $ gstreamer_sink#have_padded_height.) {
77 U_LOG_Wcxx . . $ `src/xrt/auxiliary/util/u_logging.h:301:9`!("Image height needs to be divisible by 2!");
78 }
79 } else if (gs->have_padded_heightcxx . . $ gstreamer_sink#have_padded_height.) {
80 U_LOG_Wcxx . . $ `src/xrt/auxiliary/util/u_logging.h:301:9`!("Image height changed after we added padding!");
81 }
82 }
83
84 static void
85 push_framecxx . . $ push_frame(1e8c9dd18059651).(struct xrt_frame_sinkcxx . . $ xrt_frame_sink# *xfs, struct xrt_framecxx . . $ xrt_frame# *xf)
86 {
87 SINK_TRACE_MARKERcxx . . $ `src/xrt/auxiliary/util/u_trace_marker.h:116:9`!();
88 struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs = (struct gstreamer_sinkcxx . . $ gstreamer_sink# *)xfs;
89
90 complain_if_wrong_image_sizecxx . . $ complain_if_wrong_image_size(f2271a9b8bde25fb).(gs, xf);
91
92 GstBuffercxx . . $ GstBuffer# *buffer;
93 GstFlowReturncxx . . $ GstFlowReturn# ret;
94
95 U_LOG_Tcxx . . $ `src/xrt/auxiliary/util/u_logging.h:292:9`!(
96 "Called"
97 "\n\tformat: %s"
98 "\n\twidth: %u"
99 "\n\theight: %u",
100 u_format_str(xf->format), xf->width, xf->height);
101
102 /* We need to take a reference on the frame to keep it alive. */
103 struct xrt_framecxx . . $ xrt_frame# *taken = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
104 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&taken, xf);
105
106 /* Wrap the frame that we now hold a reference to. */
107 buffer = gst_buffer_new_wrapped_fullcxx . . $ gst_buffer_new_wrapped_full(e5df8b7170cb6ef6).( //
108 0, // GstMemoryFlags flags
109 (gpointercxx . . $ gpointer#)xf->datacxx . . $ xrt_frame#data., // gpointer data
110 taken->sizecxx . . $ xrt_frame#size., // gsize maxsize
111 0, // gsize offset
112 taken->sizecxx . . $ xrt_frame#size., // gsize size
113 taken, // gpointer user_data
114 wrapped_buffer_destroycxx . . $ wrapped_buffer_destroy(5d22bdacc48458e8).); // GDestroyNotify notify
115
116 int stride = xf->stridecxx . . $ xrt_frame#stride.;
117
118 gsizecxx . . $ gsize# offsets[4] = {0, 0, 0, 0};
119 gintcxx . . $ gint# strides[4] = {stride, 0, 0, 0};
120 gst_buffer_add_video_meta_fullcxx . . $ gst_buffer_add_video_meta_full(c853e8d7dac44350).(buffer, GST_VIDEO_FRAME_FLAG_NONEcxx . . $ GST_VIDEO_FRAME_FLAG_NONE., gst_fmt_from_xf_formatcxx . . $ gst_fmt_from_xf_format(bf846e6c4ef3bcd9).(xf->formatcxx . . $ xrt_frame#format.), xf->widthcxx . . $ xrt_frame#width.,
121 xf->heightcxx . . $ xrt_frame#height., 1, offsets, strides);
122
123 //! Get the timestampe from the frame.
124 uint64_tcxx . . $ uint64_t# xtimestamp_ns = xf->timestampcxx . . $ xrt_frame#timestamp.;
125
126 // Use the first frame as offset.
127 if (gs->offset_nscxx . . $ gstreamer_sink#offset_ns. == 0) {
128 gs->offset_nscxx . . $ gstreamer_sink#offset_ns. = xtimestamp_ns;
129 }
130
131 // Need to be offset or gstreamer becomes sad.
132 GST_BUFFER_PTScxx . . $ `<external>/3149383433777588881/gstbuffer.h:89:9`!(buffer) = xtimestamp_ns - gs->offset_nscxx . . $ gstreamer_sink#offset_ns.;
133
134 // Duration is measured from last time stamp.
135 GST_BUFFER_DURATIONcxx . . $ `<external>/3149383433777588881/gstbuffer.h:119:9`!(buffer) = xtimestamp_ns - gs->timestamp_nscxx . . $ gstreamer_sink#timestamp_ns.;
136 gs->timestamp_nscxx . . $ gstreamer_sink#timestamp_ns. = xtimestamp_ns;
137
138 // All done, send it to the gstreamer pipeline.
139 ret = gst_app_src_push_buffercxx . . $ gst_app_src_push_buffer(8a134d883ad2fb9b).((GstAppSrccxx . . $ GstAppSrc# *)gs->appsrccxx . . $ gstreamer_sink#appsrc., buffer);
140 if (ret != GST_FLOW_OKcxx . . $ GST_FLOW_OK.) {
141 U_LOG_Ecxx . . $ `src/xrt/auxiliary/util/u_logging.h:304:9`!("Got GST error '%i'", ret);
142 }
143 }
144
145 static void
146 enough_datacxx . . $ enough_data(ad20f951dd3036f1).(GstElementcxx . . $ GstElement# *appsrc, gpointercxx . . $ gpointer# udata)
147 {
148 // Debugging code.
149 U_LOG_Tcxx . . $ `src/xrt/auxiliary/util/u_logging.h:292:9`!("Called");
150 }
151
152 static void
153 break_apartcxx . . $ break_apart(4381388d16accfed).(struct xrt_frame_nodecxx . . $ xrt_frame_node# *node)
154 {
155 struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs = container_ofcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:149:9`!(node, struct gstreamer_sink, node);
156
157 /*
158 * This function is called when we are shutting down, after returning
159 * from this function you are not allowed to call any other nodes in the
160 * graph. But it must be safe for other nodes to call any normal
161 * functions on us. Once the context is done calling break_aprt on all
162 * objects it will call destroy on them.
163 */
164
165 (void)gs;
166 }
167
168 static void
169 destroycxx . . $ destroy(4381388d16accfed).(struct xrt_frame_nodecxx . . $ xrt_frame_node# *node)
170 {
171 struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs = container_ofcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:149:9`!(node, struct gstreamer_sink, node);
172
173 /*
174 * All of the nodes has been broken apart and none of our functions will
175 * be called, it's now safe to destroy and free ourselves.
176 */
177
178 freecxx . . $ free(5d22bdacc48458e8).(gs);
179 }
180
181
182 /*
183 *
184 * Exported functions.
185 *
186 */
187
188 void
189 gstreamer_sink_send_eoscxx . . $ gstreamer_sink_send_eos(55f5df0031740bbc).(struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs)
190 {
191 gst_element_send_eventcxx . . $ gst_element_send_event(166c0defca7d2549).(gs->appsrccxx . . $ gstreamer_sink#appsrc., gst_event_new_eoscxx . . $ gst_event_new_eos(a48510a589508d3).());
192 }
193
194 uint64_tcxx . . $ uint64_t#
195 gstreamer_sink_get_timestamp_offsetcxx . . $ gstreamer_sink_get_timestamp_offset(2f2d48a605f91e63).(struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs)
196 {
197 return gs->offset_nscxx . . $ gstreamer_sink#offset_ns.;
198 }
199
200 void
201 gstreamer_sink_create_with_pipelinecxx . . $ gstreamer_sink_create_with_pipeline(cfc5f6a1503e4418).(struct gstreamer_pipelinecxx . . $ gstreamer_pipeline# *gp,
202 uint32_tcxx . . $ uint32_t# width,
203 uint32_tcxx . . $ uint32_t# height,
204 enum xrt_formatcxx . . $ xrt_format# format,
205 const char *appsrc_name,
206 struct gstreamer_sinkcxx . . $ gstreamer_sink# **out_gs,
207 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# **out_xfs)
208 {
209 const char *format_str = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
210 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! need_even_dims = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
211
212 switch (format) {
213 case XRT_FORMAT_R8G8B8cxx . . $ XRT_FORMAT_R8G8B8.: format_str = "RGB"; break;
214 case XRT_FORMAT_R8G8B8A8cxx . . $ XRT_FORMAT_R8G8B8A8.: format_str = "RGBA"; break;
215 case XRT_FORMAT_R8G8B8X8cxx . . $ XRT_FORMAT_R8G8B8X8.: format_str = "RGBx"; break;
216 case XRT_FORMAT_YUYV422cxx . . $ XRT_FORMAT_YUYV422.:
217 format_str = "YUY2";
218 need_even_dims = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
219 break;
220 case XRT_FORMAT_L8cxx . . $ XRT_FORMAT_L8.:
221 format_str = "GRAY8";
222 need_even_dims = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
223 break;
224 default: assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); break;
225 }
226
227 struct gstreamer_sinkcxx . . $ gstreamer_sink# *gs = U_TYPED_CALLOCcxx . . $ `src/xrt/auxiliary/util/u_misc.h:47:9`!(struct gstreamer_sink);
228 gs->basecxx . . $ gstreamer_sink#base..push_framecxx . . $ xrt_frame_sink#push_frame. = push_framecxx . . $ push_frame(1e8c9dd18059651).;
229 gs->nodecxx . . $ gstreamer_sink#node..break_apartcxx . . $ xrt_frame_node#break_apart. = break_apartcxx . . $ break_apart(4381388d16accfed).;
230 gs->nodecxx . . $ gstreamer_sink#node..destroycxx . . $ xrt_frame_node#destroy. = destroycxx . . $ destroy(4381388d16accfed).;
231 gs->gpcxx . . $ gstreamer_sink#gp. = gp;
232 gs->appsrccxx . . $ gstreamer_sink#appsrc. = gst_bin_get_by_namecxx . . $ gst_bin_get_by_name(de0ce9e12a4cf4ab).(GST_BINcxx . . $ `<external>/18072804031300516349/gstbin.h:37:9`!(gp->pipeline), appsrc_name);
233 gs->need_even_dimscxx . . $ gstreamer_sink#need_even_dims. = need_even_dims;
234
235 if (need_even_dims) {
236 /* Pad out height and width to multiple of 2 */
237 GstElementcxx . . $ GstElement# *vbox = gst_bin_get_by_namecxx . . $ gst_bin_get_by_name(de0ce9e12a4cf4ab).(GST_BINcxx . . $ `<external>/18072804031300516349/gstbin.h:37:9`!(gp->pipeline), "vbox");
238 if (vbox != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
239 if (height % 2) {
240 g_object_setcxx . . $ g_object_set(1ac911bdc4e9a14d).(G_OBJECTcxx . . $ `<external>/1341695927569185362/gobject.h:54:9`!(vbox), "bottom", -1, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
241 gs->have_padded_heightcxx . . $ gstreamer_sink#have_padded_height. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
242 }
243 if (width % 2) {
244 g_object_setcxx . . $ g_object_set(1ac911bdc4e9a14d).(G_OBJECTcxx . . $ `<external>/1341695927569185362/gobject.h:54:9`!(vbox), "width", -1, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
245 gs->have_padded_widthcxx . . $ gstreamer_sink#have_padded_width. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
246 }
247 gst_object_unrefcxx . . $ gst_object_unref(5d22bdacc48458e8).(vbox);
248 } else if (height % 2 || width % 2) {
249 U_LOG_Wcxx . . $ `src/xrt/auxiliary/util/u_logging.h:301:9`!("Image height or width needs padding. Please add `videobox name=vbox' to the pipeline");
250 }
251 }
252
253
254 GstCapscxx . . $ GstCaps# *caps = gst_caps_new_simplecxx . . $ gst_caps_new_simple(1cce4213aed9d52d).( //
255 "video/x-raw", //
256 "format", G_TYPE_STRINGcxx . . $ `<external>/9567554376676449156/gtype.h:165:9`!, format_str, //
257 "width", G_TYPE_INTcxx . . $ `<external>/9567554376676449156/gtype.h:105:9`!, width, //
258 "height", G_TYPE_INTcxx . . $ `<external>/9567554376676449156/gtype.h:105:9`!, height, //
259 "framerate", GST_TYPE_FRACTIONcxx . . $ `<external>/14168157542697705303/gstvalue.h:385:9`!, 0, 1, //
260 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
261
262 g_object_setcxx . . $ g_object_set(1ac911bdc4e9a14d).(G_OBJECTcxx . . $ `<external>/1341695927569185362/gobject.h:54:9`!(gs->appsrc), //
263 "caps", caps, //
264 "stream-type", GST_APP_STREAM_TYPE_STREAMcxx . . $ GST_APP_STREAM_TYPE_STREAM., //
265 "format", GST_FORMAT_TIMEcxx . . $ GST_FORMAT_TIME., //
266 "is-live", TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!, //
267 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
268
269 g_signal_connectcxx . . $ `<external>/15952772511652638895/gsignal.h:548:9`!(G_OBJECTcxx . . $ `<external>/1341695927569185362/gobject.h:54:9`!(gs->appsrc), "enough-data", G_CALLBACKcxx . . $ `<external>/16473872192229286895/gclosure.h:73:9`!(enough_data), gs);
270
271 /*
272 * Add ourselves to the context so we are destroyed.
273 * This is done once we know everything is completed.
274 */
275 xrt_frame_context_addcxx . . $ xrt_frame_context_add(cc43192741dca22).(gp->xfctxcxx . . $ gstreamer_pipeline#xfctx., &gs->nodecxx . . $ gstreamer_sink#node.);
276
277 *out_gs = gs;
278 *out_xfs = &gs->basecxx . . $ gstreamer_sink#base.;
279 }