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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
use thiserror::Error;
use glib::prelude::*;
use glib::translate::*;
use crate::auto::EncodingAudioProfile;
use crate::auto::EncodingContainerProfile;
use crate::auto::EncodingProfile;
use crate::auto::EncodingVideoProfile;
trait EncodingProfileBuilderCommon {
fn set_allow_dynamic_output(&self, allow_dynamic_output: bool);
fn set_description(&self, description: Option<&str>);
fn set_enabled(&self, enabled: bool);
fn set_format(&self, format: &gst::Caps);
fn set_name(&self, name: Option<&str>);
fn set_presence(&self, presence: u32);
fn set_preset(&self, preset: Option<&str>);
fn set_preset_name(&self, preset_name: Option<&str>);
}
impl<O: IsA<EncodingProfile>> EncodingProfileBuilderCommon for O {
#[doc(alias = "gst_encoding_profile_set_allow_dynamic_output")]
fn set_allow_dynamic_output(&self, allow_dynamic_output: bool) {
unsafe {
ffi::gst_encoding_profile_set_allow_dynamic_output(
self.as_ref().to_glib_none().0,
allow_dynamic_output.into_glib(),
);
}
}
#[doc(alias = "gst_encoding_profile_set_description")]
fn set_description(&self, description: Option<&str>) {
let description = description.to_glib_none();
unsafe {
ffi::gst_encoding_profile_set_description(
self.as_ref().to_glib_none().0,
description.0,
);
}
}
#[doc(alias = "gst_encoding_profile_set_enabled")]
fn set_enabled(&self, enabled: bool) {
unsafe {
ffi::gst_encoding_profile_set_enabled(
self.as_ref().to_glib_none().0,
enabled.into_glib(),
);
}
}
#[doc(alias = "gst_encoding_profile_set_format")]
fn set_format(&self, format: &gst::Caps) {
unsafe {
ffi::gst_encoding_profile_set_format(
self.as_ref().to_glib_none().0,
format.to_glib_none().0,
);
}
}
#[doc(alias = "gst_encoding_profile_set_name")]
fn set_name(&self, name: Option<&str>) {
let name = name.to_glib_none();
unsafe {
ffi::gst_encoding_profile_set_name(self.as_ref().to_glib_none().0, name.0);
}
}
#[doc(alias = "gst_encoding_profile_set_presence")]
fn set_presence(&self, presence: u32) {
unsafe {
ffi::gst_encoding_profile_set_presence(self.as_ref().to_glib_none().0, presence);
}
}
#[doc(alias = "gst_encoding_profile_set_preset")]
fn set_preset(&self, preset: Option<&str>) {
let preset = preset.to_glib_none();
unsafe {
ffi::gst_encoding_profile_set_preset(self.as_ref().to_glib_none().0, preset.0);
}
}
#[doc(alias = "gst_encoding_profile_set_preset_name")]
fn set_preset_name(&self, preset_name: Option<&str>) {
let preset_name = preset_name.to_glib_none();
unsafe {
ffi::gst_encoding_profile_set_preset_name(
self.as_ref().to_glib_none().0,
preset_name.0,
);
}
}
}
trait EncodingProfileHasRestrictionSetter {
fn set_restriction(&self, restriction: Option<&gst::Caps>);
}
pub trait EncodingProfileHasRestrictionGetter {
#[doc(alias = "get_restriction")]
fn restriction(&self) -> Option<gst::Caps>;
}
macro_rules! declare_encoding_profile_has_restriction(
($name:ident) => {
impl EncodingProfileHasRestrictionSetter for $name {
#[doc(alias = "gst_encoding_profile_set_restriction")]
fn set_restriction(&self, restriction: Option<&gst::Caps>) {
let profile: &EncodingProfile = glib::object::Cast::upcast_ref(self);
unsafe {
let restriction = match restriction {
Some(restriction) => restriction.to_glib_full(),
None => gst::ffi::gst_caps_new_any(),
};
ffi::gst_encoding_profile_set_restriction(
profile.to_glib_none().0,
restriction,
);
}
}
}
impl EncodingProfileHasRestrictionGetter for $name {
#[doc(alias = "gst_encoding_profile_get_restriction")]
fn restriction(&self) -> Option<gst::Caps> {
let profile: &EncodingProfile = glib::object::Cast::upcast_ref(self);
unsafe {
from_glib_full(ffi::gst_encoding_profile_get_restriction(
profile.to_glib_none().0,
))
}
}
}
}
);
impl EncodingAudioProfile {
#[doc(alias = "gst_encoding_audio_profile_new")]
fn new(
format: &gst::Caps,
preset: Option<&str>,
restriction: Option<&gst::Caps>,
presence: u32,
) -> EncodingAudioProfile {
assert_initialized_main_thread!();
let preset = preset.to_glib_none();
let restriction = restriction.to_glib_none();
unsafe {
from_glib_full(ffi::gst_encoding_audio_profile_new(
format.to_glib_none().0,
preset.0,
restriction.0,
presence,
))
}
}
}
declare_encoding_profile_has_restriction!(EncodingAudioProfile);
impl EncodingVideoProfile {
#[doc(alias = "gst_encoding_video_profile_new")]
fn new(
format: &gst::Caps,
preset: Option<&str>,
restriction: Option<&gst::Caps>,
presence: u32,
) -> EncodingVideoProfile {
assert_initialized_main_thread!();
let preset = preset.to_glib_none();
let restriction = restriction.to_glib_none();
unsafe {
from_glib_full(ffi::gst_encoding_video_profile_new(
format.to_glib_none().0,
preset.0,
restriction.0,
presence,
))
}
}
#[doc(alias = "gst_encoding_video_profile_set_pass")]
fn set_pass(&self, pass: u32) {
unsafe {
ffi::gst_encoding_video_profile_set_pass(self.to_glib_none().0, pass);
}
}
#[doc(alias = "gst_encoding_video_profile_set_variableframerate")]
fn set_variableframerate(&self, variableframerate: bool) {
unsafe {
ffi::gst_encoding_video_profile_set_variableframerate(
self.to_glib_none().0,
variableframerate.into_glib(),
);
}
}
}
declare_encoding_profile_has_restriction!(EncodingVideoProfile);
impl EncodingContainerProfile {
#[doc(alias = "gst_encoding_container_profile_new")]
fn new(
name: Option<&str>,
description: Option<&str>,
format: &gst::Caps,
preset: Option<&str>,
) -> EncodingContainerProfile {
assert_initialized_main_thread!();
let name = name.to_glib_none();
let description = description.to_glib_none();
let preset = preset.to_glib_none();
unsafe {
from_glib_full(ffi::gst_encoding_container_profile_new(
name.0,
description.0,
format.to_glib_none().0,
preset.0,
))
}
}
#[doc(alias = "gst_encoding_container_profile_add_profile")]
fn add_profile<P: IsA<EncodingProfile>>(
&self,
profile: &P,
) -> Result<(), glib::error::BoolError> {
unsafe {
glib::result_from_gboolean!(
ffi::gst_encoding_container_profile_add_profile(
self.to_glib_none().0,
profile.as_ref().to_glib_full(),
),
"Failed to add profile",
)
}
}
}
#[derive(Debug, Clone, Error)]
#[error("failed to build encoding profile")]
pub struct EncodingProfileBuilderError(());
#[derive(Debug)]
struct EncodingProfileBuilderCommonData<'a> {
name: Option<&'a str>,
description: Option<&'a str>,
format: Option<&'a gst::Caps>,
preset: Option<&'a str>,
preset_name: Option<&'a str>,
presence: u32,
allow_dynamic_output: bool,
enabled: bool,
}
impl<'a> EncodingProfileBuilderCommonData<'a> {
fn new() -> EncodingProfileBuilderCommonData<'a> {
EncodingProfileBuilderCommonData {
name: None,
description: None,
format: None,
preset: None,
preset_name: None,
presence: 0,
allow_dynamic_output: true,
enabled: true,
}
}
}
pub trait EncodingProfileBuilder<'a>: Sized {
fn name(self, name: &'a str) -> Self;
fn description(self, description: &'a str) -> Self;
fn format(self, format: &'a gst::Caps) -> Self;
fn preset(self, preset: &'a str) -> Self;
fn preset_name(self, preset_name: &'a str) -> Self;
fn presence(self, presence: u32) -> Self;
fn allow_dynamic_output(self, allow: bool) -> Self;
fn enabled(self, enabled: bool) -> Self;
}
macro_rules! declare_encoding_profile_builder_common(
($name:ident) => {
impl<'a> Default for $name<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'a> EncodingProfileBuilder<'a> for $name<'a> {
fn name(mut self, name: &'a str) -> $name<'a> {
self.base.name = Some(name);
self
}
fn description(mut self, description: &'a str) -> $name<'a> {
self.base.description = Some(description);
self
}
fn format(mut self, format: &'a gst::Caps) -> $name<'a> {
self.base.format = Some(format);
self
}
fn preset(mut self, preset: &'a str) -> $name<'a> {
self.base.preset = Some(preset);
self
}
fn preset_name(mut self, preset_name: &'a str) -> $name<'a> {
self.base.preset_name = Some(preset_name);
self
}
fn presence(mut self, presence: u32) -> $name<'a> {
self.base.presence = presence;
self
}
fn allow_dynamic_output(mut self, allow: bool) -> $name<'a> {
self.base.allow_dynamic_output = allow;
self
}
fn enabled(mut self, enabled: bool) -> $name<'a> {
self.base.enabled = enabled;
self
}
}
}
);
fn set_common_fields<T: EncodingProfileBuilderCommon>(
profile: &T,
base_data: &EncodingProfileBuilderCommonData,
) {
skip_assert_initialized!();
profile.set_name(base_data.name);
profile.set_description(base_data.description);
profile.set_preset(base_data.preset);
profile.set_preset_name(base_data.preset_name);
profile.set_allow_dynamic_output(base_data.allow_dynamic_output);
profile.set_enabled(base_data.enabled);
profile.set_presence(base_data.presence);
}
#[derive(Debug)]
pub struct EncodingAudioProfileBuilder<'a> {
base: EncodingProfileBuilderCommonData<'a>,
restriction: Option<&'a gst::Caps>,
}
declare_encoding_profile_builder_common!(EncodingAudioProfileBuilder);
impl<'a> EncodingAudioProfileBuilder<'a> {
pub fn new() -> Self {
EncodingAudioProfileBuilder {
base: EncodingProfileBuilderCommonData::new(),
restriction: None,
}
}
pub fn restriction(mut self, restriction: &'a gst::Caps) -> Self {
self.restriction = Some(restriction);
self
}
pub fn build(self) -> Result<EncodingAudioProfile, EncodingProfileBuilderError> {
if self.base.format.is_none() {
return Err(EncodingProfileBuilderError(()));
}
let profile = EncodingAudioProfile::new(
self.base.format.unwrap(),
self.base.preset,
self.restriction,
self.base.presence,
);
set_common_fields(&profile, &self.base);
Ok(profile)
}
}
#[derive(Debug)]
pub struct EncodingVideoProfileBuilder<'a> {
base: EncodingProfileBuilderCommonData<'a>,
restriction: Option<&'a gst::Caps>,
pass: u32,
variable_framerate: bool,
}
declare_encoding_profile_builder_common!(EncodingVideoProfileBuilder);
impl<'a> EncodingVideoProfileBuilder<'a> {
pub fn new() -> Self {
EncodingVideoProfileBuilder {
base: EncodingProfileBuilderCommonData::new(),
restriction: None,
pass: 0,
variable_framerate: false,
}
}
pub fn pass(mut self, pass: u32) -> Self {
self.pass = pass;
self
}
pub fn variable_framerate(mut self, variable_framerate: bool) -> Self {
self.variable_framerate = variable_framerate;
self
}
pub fn restriction(mut self, restriction: &'a gst::Caps) -> Self {
self.restriction = Some(restriction);
self
}
pub fn build(self) -> Result<EncodingVideoProfile, EncodingProfileBuilderError> {
if self.base.format.is_none() {
return Err(EncodingProfileBuilderError(()));
}
let video_profile = EncodingVideoProfile::new(
self.base.format.unwrap(),
self.base.preset,
self.restriction,
self.base.presence,
);
video_profile.set_pass(self.pass);
video_profile.set_variableframerate(self.variable_framerate);
set_common_fields(&video_profile, &self.base);
Ok(video_profile)
}
}
#[derive(Debug)]
pub struct EncodingContainerProfileBuilder<'a> {
base: EncodingProfileBuilderCommonData<'a>,
profiles: Vec<EncodingProfile>,
}
declare_encoding_profile_builder_common!(EncodingContainerProfileBuilder);
impl<'a> EncodingContainerProfileBuilder<'a> {
pub fn new() -> Self {
EncodingContainerProfileBuilder {
base: EncodingProfileBuilderCommonData::new(),
profiles: Vec::new(),
}
}
pub fn build(self) -> Result<EncodingContainerProfile, EncodingProfileBuilderError> {
if self.base.format.is_none() {
return Err(EncodingProfileBuilderError(()));
}
let container_profile = EncodingContainerProfile::new(
self.base.name,
self.base.description,
self.base.format.unwrap(),
self.base.preset,
);
for profile in self.profiles {
container_profile
.add_profile(&profile)
.map_err(|_error| EncodingProfileBuilderError(()))?;
}
set_common_fields(&container_profile, &self.base);
Ok(container_profile)
}
pub fn add_profile<P: IsA<EncodingProfile>>(mut self, profile: &P) -> Self {
self.profiles.push(profile.as_ref().clone());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::auto::EncodingContainerProfile;
use crate::auto::EncodingVideoProfile;
use crate::prelude::*;
const AUDIO_PROFILE_NAME: &str = "audio-profile";
const AUDIO_PROFILE_DESCRIPTION: &str = "audio-profile-description";
const PRESET: &str = "preset";
const PRESET_NAME: &str = "preset-name";
const PRESENCE: u32 = 5;
const ALLOW_DYNAMIC_OUTPUT: bool = false;
const ENABLED: bool = false;
const VIDEO_PROFILE_NAME: &str = "video-profile";
const VIDEO_PROFILE_DESCRIPTION: &str = "video-profile-description";
const CONTAINER_PROFILE_NAME: &str = "container-profile";
const CONTAINER_PROFILE_DESCRIPTION: &str = "container-profile-description";
const PASS: u32 = 8;
const VARIABLE_FRAMERATE: bool = true;
#[test]
fn test_encoding_audio_profile_builder() {
gst::init().unwrap();
let caps = gst::Caps::new_simple("audio/x-raw", &[]);
let restriction = gst::Caps::new_simple("audio/x-raw", &[("format", &"S32LE")]);
let audio_profile = EncodingAudioProfileBuilder::new()
.name(AUDIO_PROFILE_NAME)
.description(AUDIO_PROFILE_DESCRIPTION)
.format(&caps)
.preset(PRESET)
.preset_name(PRESET_NAME)
.restriction(&restriction)
.presence(PRESENCE)
.allow_dynamic_output(ALLOW_DYNAMIC_OUTPUT)
.enabled(ENABLED)
.build()
.unwrap();
assert_eq!(audio_profile.name().unwrap(), AUDIO_PROFILE_NAME);
assert_eq!(
audio_profile.description().unwrap(),
AUDIO_PROFILE_DESCRIPTION
);
assert_eq!(audio_profile.format(), caps);
assert_eq!(audio_profile.preset().unwrap(), PRESET);
assert_eq!(audio_profile.preset_name().unwrap(), PRESET_NAME);
assert_eq!(audio_profile.restriction().unwrap(), restriction);
assert_eq!(audio_profile.presence(), PRESENCE);
assert_eq!(audio_profile.allows_dynamic_output(), ALLOW_DYNAMIC_OUTPUT);
assert_eq!(audio_profile.is_enabled(), ENABLED);
let restriction = gst::Caps::new_simple("audio/x-raw", &[("format", &"S32BE")]);
audio_profile.set_restriction(Some(&restriction));
assert_eq!(audio_profile.restriction().unwrap(), restriction);
}
#[test]
fn test_encoding_video_profile_builder() {
gst::init().unwrap();
let caps = gst::Caps::new_simple("video/x-raw", &[]);
let restriction = gst::Caps::new_simple("video/x-raw", &[("format", &"RGBA")]);
let video_profile = EncodingVideoProfileBuilder::new()
.name(VIDEO_PROFILE_NAME)
.description(VIDEO_PROFILE_DESCRIPTION)
.format(&caps)
.preset(PRESET)
.preset_name(PRESET_NAME)
.restriction(&restriction)
.presence(PRESENCE)
.allow_dynamic_output(ALLOW_DYNAMIC_OUTPUT)
.enabled(ENABLED)
.pass(PASS)
.variable_framerate(VARIABLE_FRAMERATE)
.build()
.unwrap();
assert_eq!(video_profile.name().unwrap(), VIDEO_PROFILE_NAME);
assert_eq!(
video_profile.description().unwrap(),
VIDEO_PROFILE_DESCRIPTION
);
assert_eq!(video_profile.format(), caps);
assert_eq!(video_profile.preset().unwrap(), PRESET);
assert_eq!(video_profile.preset_name().unwrap(), PRESET_NAME);
assert_eq!(video_profile.restriction().unwrap(), restriction);
assert_eq!(video_profile.presence(), PRESENCE);
assert_eq!(video_profile.allows_dynamic_output(), ALLOW_DYNAMIC_OUTPUT);
assert_eq!(video_profile.is_enabled(), ENABLED);
let video_profile: EncodingVideoProfile =
glib::object::Cast::downcast(video_profile).ok().unwrap();
assert_eq!(video_profile.is_variableframerate(), VARIABLE_FRAMERATE);
assert_eq!(video_profile.pass(), PASS);
let restriction = gst::Caps::new_simple("video/x-raw", &[("format", &"NV12")]);
video_profile.set_restriction(Some(&restriction));
assert_eq!(video_profile.restriction().unwrap(), restriction);
}
#[test]
fn test_encoding_container_profile_builder() {
gst::init().unwrap();
let container_caps = gst::Caps::new_simple("container/x-caps", &[]);
let video_caps = gst::Caps::new_simple("video/x-raw", &[]);
let audio_caps = gst::Caps::new_simple("audio/x-raw", &[]);
let video_profile = EncodingVideoProfileBuilder::new()
.name(VIDEO_PROFILE_NAME)
.description(VIDEO_PROFILE_DESCRIPTION)
.format(&video_caps)
.build()
.unwrap();
let audio_profile = EncodingAudioProfileBuilder::new()
.name(AUDIO_PROFILE_NAME)
.description(AUDIO_PROFILE_DESCRIPTION)
.format(&audio_caps)
.build()
.unwrap();
let profile = EncodingContainerProfileBuilder::new()
.name(CONTAINER_PROFILE_NAME)
.description(CONTAINER_PROFILE_DESCRIPTION)
.format(&container_caps)
.preset(PRESET)
.preset_name(PRESET_NAME)
.presence(PRESENCE)
.allow_dynamic_output(ALLOW_DYNAMIC_OUTPUT)
.enabled(ENABLED)
.add_profile(&audio_profile)
.add_profile(&video_profile)
.build()
.unwrap();
assert_eq!(profile.name().unwrap(), CONTAINER_PROFILE_NAME);
assert_eq!(
profile.description().unwrap(),
CONTAINER_PROFILE_DESCRIPTION
);
assert_eq!(profile.format(), container_caps);
assert_eq!(profile.preset().unwrap(), PRESET);
assert_eq!(profile.preset_name().unwrap(), PRESET_NAME);
assert_eq!(profile.presence(), PRESENCE);
assert_eq!(profile.allows_dynamic_output(), ALLOW_DYNAMIC_OUTPUT);
assert_eq!(profile.is_enabled(), ENABLED);
let container_profile: EncodingContainerProfile =
glib::object::Cast::downcast(profile).ok().unwrap();
assert!(container_profile.contains_profile(&video_profile));
assert!(container_profile.contains_profile(&audio_profile));
}
}