opentalk-compositor 0.18.0

Compositor for the OpenTalk Recorder and Obelisk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
//
// SPDX-License-Identifier: EUPL-1.2

use anyhow::Context;
use glib::types::StaticType;

glib::wrapper! {
    pub struct MatroskaS3Sink(ObjectSubclass<imp::MatroskaS3Sink>)
        @extends gst::Object, gst::Element, gst_base::BaseSink;
}

// This should be 5 MiB, regarding the S3 specifications
pub(crate) const DEFAULT_CHUNK_SIZE: u64 = 5 * 1024 * 1024;

/// # Errors
///
/// Returns an error if Gstreamer is not initialized or this function was already called in this proccess.
pub fn register() -> anyhow::Result<()> {
    gst::Element::register(
        None,
        "opentalk-matroska-s3-sink",
        gst::Rank::NONE,
        MatroskaS3Sink::static_type(),
    )
    .context("Failed to register opentalk-matroska-s3-sink")
}

mod imp {
    use std::{
        io::{self, Cursor, Write},
        mem::take,
        sync::LazyLock,
    };

    use anyhow::{Context, Result};
    use glib::{object::ObjectExt, value::ToValue, BorrowedObject, ParamSpecBuilderExt};
    use gst::{
        format::Bytes,
        glib::{self, subclass::Signal},
        prelude::StaticType,
        subclass::prelude::{
            ElementImpl, GstObjectImpl, ObjectImpl, ObjectSubclass, ObjectSubclassExt,
        },
        EventView, Format, GenericFormattedValue, QueryViewMut, StreamError,
    };
    use gst_base::subclass::prelude::{BaseSinkImpl, BaseSinkImplExt};
    use parking_lot::Mutex;

    use super::DEFAULT_CHUNK_SIZE;

    static CAT: LazyLock<gst::DebugCategory> = LazyLock::new(|| {
        gst::DebugCategory::new(
            "opentalk-matroska-s3-sink",
            gst::DebugColorFlags::empty(),
            Some("OpenTalk Matroska Chunks"),
        )
    });

    #[derive(Default)]
    pub struct MatroskaS3Sink {
        inner: Mutex<Inner>,
    }

    struct Inner {
        // Properties
        chunk_size: u64,

        // State
        matroska_header: Vec<u8>,

        current_buffer: Cursor<Vec<u8>>,
        total_bytes_produced: u64,

        first_s3_chunk: Vec<u8>,
        // is extended until larger than 5MiB or EOS is received
        current_s3_chunk: Vec<u8>,

        total_s3_chunks_produced: u64,
    }

    impl Default for Inner {
        fn default() -> Self {
            Self {
                chunk_size: DEFAULT_CHUNK_SIZE,
                matroska_header: vec![],
                current_buffer: Cursor::new(vec![]),
                total_bytes_produced: 0,
                first_s3_chunk: vec![],
                current_s3_chunk: vec![],
                total_s3_chunks_produced: 0,
            }
        }
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MatroskaS3Sink {
        const NAME: &'static str = "OpenTalkMatroskaUploadSink";
        type Type = super::MatroskaS3Sink;
        type ParentType = gst_base::BaseSink;
    }

    impl ObjectImpl for MatroskaS3Sink {
        fn signals() -> &'static [glib::subclass::Signal] {
            static SIGNALS: LazyLock<Vec<Signal>> = LazyLock::new(|| {
                vec![Signal::builder("part")
                    .param_types([u64::static_type(), glib::Bytes::static_type()])
                    .build()]
            });

            &SIGNALS
        }

        fn properties() -> &'static [glib::ParamSpec] {
            static PROPERTIES: LazyLock<Vec<glib::ParamSpec>> = LazyLock::new(|| {
                vec![glib::ParamSpecUInt64::builder("chunk-size")
                    .readwrite()
                    .blurb("chunk size in bytes")
                    .default_value(DEFAULT_CHUNK_SIZE)
                    .build()]
            });

            &PROPERTIES
        }

        fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
            match pspec.name() {
                "chunk-size" => match value.get::<u64>() {
                    Ok(value) => self.inner.lock().chunk_size = value,
                    Err(e) => {
                        gst::error!(CAT, imp = self, "Failed to set paramter 'chunk-size', {e}");
                    }
                },
                name => gst::error!(CAT, imp = self, "Unknown property '{name}'"),
            }
        }

        fn property(&self, _id: usize, _pspec: &glib::ParamSpec) -> glib::Value {
            self.inner.lock().chunk_size.to_value()
        }
    }

    impl GstObjectImpl for MatroskaS3Sink {}

    impl ElementImpl for MatroskaS3Sink {
        fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
            static ELEMENT_METADATA: LazyLock<gst::subclass::ElementMetadata> = LazyLock::new(
                || {
                    gst::subclass::ElementMetadata::new(
                    "OpenTalkMatroskaS3Sink",
                    "Sink/Network",
                    "Split a matroska stream into chunks that can be uploaded using the S3 multipart API",
                    "Konstantin Baltruschat"
                )
                },
            );

            Some(&*ELEMENT_METADATA)
        }

        fn pad_templates() -> &'static [gst::PadTemplate] {
            static PAD_TEMPLATES: LazyLock<Vec<gst::PadTemplate>> = LazyLock::new(|| {
                let sink_pad_template = gst::PadTemplate::new(
                    "sink",
                    gst::PadDirection::Sink,
                    gst::PadPresence::Always,
                    &gst::Caps::new_any(),
                )
                .expect("unable to create PadTemplate sink for matroskas3sink");

                vec![sink_pad_template]
            });

            &PAD_TEMPLATES
        }
    }

    impl BaseSinkImpl for MatroskaS3Sink {
        fn event(&self, event: gst::Event) -> bool {
            match event.view() {
                EventView::Segment(segment) => {
                    let segment = segment.segment();

                    self.inner.lock().handle_segment(segment);
                }
                EventView::Eos(_) => self.inner.lock().finish(&self.obj()),
                _ => (),
            }

            self.parent_event(event)
        }

        fn query(&self, query: &mut gst::QueryRef) -> bool {
            if let QueryViewMut::Seeking(seeking) = query.view_mut() {
                seeking.set(
                    seeking.format() == Format::Bytes,
                    Bytes::from_u64(0),
                    GenericFormattedValue::Bytes(None),
                );
                true
            } else {
                self.parent_query(query)
            }
        }

        fn render(&self, buffer: &gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> {
            if let Err(e) = self.inner.lock().render_buffer(&self.obj(), buffer) {
                gst::element_error!(
                    self.obj(),
                    StreamError::Failed,
                    ("Failed to render_buffer: {e:?}")
                );

                Err(gst::FlowError::Error)
            } else {
                Ok(gst::FlowSuccess::Ok)
            }
        }
    }

    impl Inner {
        fn render_buffer(
            &mut self,
            obj: &BorrowedObject<super::MatroskaS3Sink>,
            buffer: &gst::Buffer,
        ) -> Result<gst::FlowSuccess> {
            let readable = buffer.map_readable().context("Failed to map buffer")?;

            self.write(obj, &readable)?;

            Ok(gst::FlowSuccess::Ok)
        }

        fn handle_segment(
            &mut self,
            segment: &gst::FormattedSegment<GenericFormattedValue>,
        ) -> bool {
            let GenericFormattedValue::Bytes(Some(segment_start)) = segment.start() else {
                return false;
            };

            self.seek(*segment_start);

            true
        }

        fn write(
            &mut self,
            obj: &BorrowedObject<super::MatroskaS3Sink>,
            buf: &[u8],
        ) -> io::Result<usize> {
            // Skip searching large buffers to optimize performance.
            // The condition below ensures that we only search for the Matroska cluster's magic bytes in small buffers (less than 400 bytes).
            // This is based on the observation that the header chunks containing these magic bytes are typically around 100 bytes.
            // By limiting the search to smaller buffers, we avoid the expensive operation of scanning large data chunks,
            // which can significantly slow down the process. This threshold of 400 bytes provides a good balance,
            // allowing us to catch the header while minimizing unnecessary computation.
            if buf.len() < 400 {
                // Check if a new matroska cluster is written by identifying these magic bytes
                const MATROSKA_MAGIC_BYTES: [u8; 4] = [0x1F, 0x43, 0xB6, 0x75];
                let buf_contains_new_cluster: bool =
                    buf.windows(4).any(|w| w == MATROSKA_MAGIC_BYTES);

                if buf_contains_new_cluster {
                    self.total_bytes_produced += self.current_buffer.get_ref().len() as u64;

                    if self.matroska_header.is_empty() {
                        self.matroska_header
                            .clone_from(self.current_buffer.get_ref());
                    }

                    self.current_s3_chunk
                        .extend_from_slice(self.current_buffer.get_ref());
                    self.current_buffer.set_position(0);
                    self.current_buffer.get_mut().clear();

                    self.maybe_submit_s3(obj);
                }
            }

            self.current_buffer.write(buf)
        }

        fn seek(&mut self, pos: u64) {
            if let Some(new_pos) = pos.checked_sub(self.total_bytes_produced) {
                self.current_buffer.set_position(new_pos);
            } else {
                self.current_s3_chunk
                    .extend_from_slice(self.current_buffer.get_ref());

                self.current_buffer = Cursor::new(self.matroska_header.clone());
                self.current_buffer.set_position(pos);
                self.total_bytes_produced = 0;
            }
        }

        fn maybe_submit_s3(&mut self, obj: &BorrowedObject<super::MatroskaS3Sink>) {
            if (self.current_s3_chunk.len() as u64) < self.chunk_size {
                return;
            }

            let bytes = glib::Bytes::from(&self.current_s3_chunk[..]);
            obj.emit_by_name::<()>("part", &[&self.total_s3_chunks_produced, &bytes]);

            self.total_s3_chunks_produced += 1;

            if self.first_s3_chunk.is_empty() {
                self.first_s3_chunk = take(&mut self.current_s3_chunk);
            } else {
                self.current_s3_chunk.clear();
            }
        }

        fn finish(&mut self, obj: &BorrowedObject<super::MatroskaS3Sink>) {
            if self.first_s3_chunk.is_empty() {
                // No chunks were emitted written yet, so use the current one
                self.first_s3_chunk = take(&mut self.current_s3_chunk);
            }

            if !self.current_s3_chunk.is_empty() {
                // There is a pending chunk, emit it
                let bytes = glib::Bytes::from(&self.current_s3_chunk[..]);
                obj.emit_by_name::<()>("part", &[&self.total_s3_chunks_produced, &bytes]);
            }

            // Update the first chunk we've ever emitted with the current buffer (which should contain the matroska header)
            Cursor::new(&mut self.first_s3_chunk)
                .write_all(self.current_buffer.get_ref())
                .expect("unable to write all current buffer into cursor for matroskas3sink");

            let bytes = glib::Bytes::from(&self.first_s3_chunk[..]);
            obj.emit_by_name::<()>("part", &[&0u64, &bytes]);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::BTreeMap, sync::Arc};

    use parking_lot::Mutex;

    use super::*;

    #[test]
    fn multipart_upload_simulator() {
        use gst::prelude::*;

        env_logger::init();
        gst::init().unwrap();
        register().unwrap();

        let main_loop = glib::MainLoop::new(None, false);

        let ml = main_loop.clone();
        std::thread::spawn(move || ml.run());

        let pipeline = gst::parse::launch(
            "
        audiotestsrc
                volume=0.1
                samplesperbuffer=480
                num-buffers=1000
            ! audio/x-raw,layout=interleaved,rate=48000
            ! opusenc
            ! mux.

        videotestsrc
                num-buffers=240
            ! video/x-raw,width=1920,height=1080,framerate=24/1
            ! vp8enc
            ! mux.

        webmmux
                name=mux
                writing-app=OpenTalk
                offset-to-zero=true
            ! opentalk-matroska-s3-sink name=sink  chunk-size=5000000 sync=false",
        )
        .unwrap();
        let pipeline = pipeline.downcast::<gst::Pipeline>().unwrap();

        let multipart_upload_simulator = <Arc<Mutex<BTreeMap<u64, Vec<u8>>>>>::default();

        let sink = pipeline.by_name("sink").unwrap();
        let multipart_upload_simulator_ = multipart_upload_simulator.clone();
        sink.connect("part", false, move |v: &[glib::Value]| {
            let n = v[1].get::<u64>().unwrap();
            let b = v[2].get::<glib::Bytes>().unwrap();

            multipart_upload_simulator_.lock().insert(n, b.to_vec());

            None
        });

        pipeline.set_state(gst::State::Playing).unwrap();

        let bus = pipeline.bus().unwrap();

        std::thread::sleep(std::time::Duration::from_secs(12));

        pipeline.send_event(gst::event::Eos::new());

        for e in bus.iter_timed(None) {
            if let gst::MessageView::Eos(_) = e.view() {
                break;
            }
        }

        pipeline.set_state(gst::State::Null).unwrap();

        main_loop.quit();

        let mut file = vec![];

        for chunk in multipart_upload_simulator.lock().values() {
            file.extend_from_slice(chunk);
        }

        // expect at least 1MiB of data
        assert!(file.len() > 1_000_000, "suspicious file size");

        let _ = std::fs::write("test_output/multipart_upload_simulator.mkv", &file);
    }
}