mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-11-25 11:01:10 +00:00
audio: add API to build raw audio caps
This commit is contained in:
parent
972002ceb2
commit
3ceb870790
2 changed files with 107 additions and 0 deletions
105
gstreamer-audio/src/functions.rs
Normal file
105
gstreamer-audio/src/functions.rs
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
// Copyright (C) 2020 Guillaume Desmottes <guillaume.desmottes@collabora.com>
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use glib;
|
||||||
|
use glib::ToSendValue;
|
||||||
|
use gst;
|
||||||
|
|
||||||
|
use std::i32;
|
||||||
|
|
||||||
|
pub fn audio_make_raw_caps(
|
||||||
|
formats: &[::AudioFormat],
|
||||||
|
layout: ::AudioLayout,
|
||||||
|
) -> gst::caps::Builder<gst::caps::NoFeature> {
|
||||||
|
assert_initialized_main_thread!();
|
||||||
|
|
||||||
|
let formats: Vec<glib::SendValue> = formats
|
||||||
|
.iter()
|
||||||
|
.map(|f| match f {
|
||||||
|
::AudioFormat::Encoded => panic!("Invalid encoded format"),
|
||||||
|
::AudioFormat::Unknown => panic!("Invalid unknown format"),
|
||||||
|
_ => f.to_string().to_send_value(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let builder = gst::caps::Caps::builder("audio/x-raw")
|
||||||
|
.field("format", &gst::List::from_owned(formats))
|
||||||
|
.field("rate", &gst::IntRange::<i32>::new(1, i32::MAX))
|
||||||
|
.field("channels", &gst::IntRange::<i32>::new(1, i32::MAX));
|
||||||
|
|
||||||
|
match layout {
|
||||||
|
::AudioLayout::Interleaved => builder.field("layout", &"interleaved"),
|
||||||
|
::AudioLayout::NonInterleaved => builder.field("layout", &"non-interleaved"),
|
||||||
|
::AudioLayout::__Unknown(_) => builder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use gst;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_caps() {
|
||||||
|
gst::init().unwrap();
|
||||||
|
|
||||||
|
let caps = audio_make_raw_caps(
|
||||||
|
&[::AudioFormat::S16be, ::AudioFormat::S16le],
|
||||||
|
::AudioLayout::Interleaved,
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
assert_eq!(caps.to_string(), "audio/x-raw, format=(string){ S16BE, S16LE }, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], layout=(string)interleaved");
|
||||||
|
|
||||||
|
#[cfg(feature = "v1_18")]
|
||||||
|
{
|
||||||
|
use glib::translate::{from_glib_full, ToGlib};
|
||||||
|
|
||||||
|
/* audio_make_raw_caps() is a re-implementation so ensure it returns the same caps as the C API */
|
||||||
|
let c_caps = unsafe {
|
||||||
|
let formats: Vec<gst_audio_sys::GstAudioFormat> =
|
||||||
|
[::AudioFormat::S16be, ::AudioFormat::S16le]
|
||||||
|
.iter()
|
||||||
|
.map(|f| f.to_glib())
|
||||||
|
.collect();
|
||||||
|
let caps = gst_audio_sys::gst_audio_make_raw_caps(
|
||||||
|
formats.as_ptr(),
|
||||||
|
formats.len() as u32,
|
||||||
|
gst_audio_sys::GST_AUDIO_LAYOUT_INTERLEAVED,
|
||||||
|
);
|
||||||
|
from_glib_full(caps)
|
||||||
|
};
|
||||||
|
assert_eq!(caps, c_caps);
|
||||||
|
}
|
||||||
|
|
||||||
|
let caps = audio_make_raw_caps(
|
||||||
|
&[::AudioFormat::S16be, ::AudioFormat::S16le],
|
||||||
|
::AudioLayout::NonInterleaved,
|
||||||
|
)
|
||||||
|
.field("rate", &16000)
|
||||||
|
.field("channels", &2)
|
||||||
|
.build();
|
||||||
|
assert_eq!(
|
||||||
|
caps.to_string(),
|
||||||
|
"audio/x-raw, format=(string){ S16BE, S16LE }, rate=(int)16000, channels=(int)2, layout=(string)non-interleaved"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "Invalid encoded format")]
|
||||||
|
fn audio_caps_encoded() {
|
||||||
|
gst::init().unwrap();
|
||||||
|
audio_make_raw_caps(&[::AudioFormat::Encoded], ::AudioLayout::Interleaved);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "Invalid unknown format")]
|
||||||
|
fn audio_caps_unknown() {
|
||||||
|
gst::init().unwrap();
|
||||||
|
audio_make_raw_caps(&[::AudioFormat::Unknown], ::AudioLayout::Interleaved);
|
||||||
|
}
|
||||||
|
}
|
|
@ -53,6 +53,8 @@ mod audio_channel_position;
|
||||||
pub use audio_channel_position::*;
|
pub use audio_channel_position::*;
|
||||||
#[cfg(any(feature = "v1_14", feature = "dox"))]
|
#[cfg(any(feature = "v1_14", feature = "dox"))]
|
||||||
mod audio_stream_align;
|
mod audio_stream_align;
|
||||||
|
mod functions;
|
||||||
|
pub use functions::*;
|
||||||
|
|
||||||
mod audio_decoder;
|
mod audio_decoder;
|
||||||
pub use audio_decoder::AudioDecoderExtManual;
|
pub use audio_decoder::AudioDecoderExtManual;
|
||||||
|
|
Loading…
Reference in a new issue