我对 Gstreamer 很熟悉,但对 Rust 很陌生,
TLDR;我希望能够在使用之前将 PadProbeId 初始化为默认值。
细节:
我有一个 Bin(包含音频 + 视频编码器和 hlssink)。
我已经能够将这个 bin 添加到管道中并且它工作正常。
我遇到的问题是流的音频是可选的,我只想add_probe()
在音频可用时才这样做。以下是我尝试实现的简化版本
let mut audio_probe_id: PadProbeId;
let mut tee_audio_pad: Pad;
if media_info.audio_available {
// get encoded audio from the tee
tee_audio_pad = audio_tee.request_pad_simple("src_%u").unwrap();
audio_probe_id = tee_audio_pad.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| {
gst::PadProbeReturn::Ok
}).unwrap();
// link the audio_tee.src to enc_bin ghost pad
let audio_sink_pad = enc_bin.static_pad("audio").unwrap();
tee_audio_pad.link(&audio_sink_pad).unwrap();
}
enc_bin.call_async(move |bin| {
bin.sync_state_with_parent().unwrap();
if media_info.audio_available {
tee_audio_pad.remove_probe(audio_probe_id);
}
}
然而,由于 Rust 编译器限制使用未初始化的变量,它不允许我audio_probe_id
在没有初始化的情况下使用。
我试图像这样初始化它;let mut audio_probe_id: PadProbeId = PadProbeId(NonZeroU64(u64::MAX));
. 但是编译器抱怨它是一个私有字段。
error[E0423]: cannot initialize a tuple struct which contains private fields
非常感谢你的帮助!
像这样拥有空变量的生锈方法是使用Option
,但在您的情况下,使用单个条件会更简单:
if media_info.audio_available {
// get encoded audio from the tee
let tee_audio_pad = audio_tee.request_pad_simple("src_%u").unwrap();
let audio_probe_id = tee_audio_pad.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, |_pad, _info| {
gst::PadProbeReturn::Ok
}).unwrap();
// link the audio_tee.src to enc_bin ghost pad
let audio_sink_pad = enc_bin.static_pad("audio").unwrap();
tee_audio_pad.link(&audio_sink_pad).unwrap();
enc_bin.call_async(move |bin| {
bin.sync_state_with_parent().unwrap();
tee_audio_pad.remove_probe(audio_probe_id);
}
} else {
enc_bin.call_async(move |bin| {
bin.sync_state_with_parent().unwrap();
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句