webrtcsrc: use @watch instead of @to-owned

@to-owned increases refcount of the element, which prevents the object from proper destruction, as the initial refcount with ElementFactory::make is larger than 1.

Instead, use @watch to create a weak reference and unbind the closure automatically if the object gets destroyed

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1355>
This commit is contained in:
Maksym Khomenko 2023-10-11 11:54:51 +03:00
parent 3fc6220009
commit 5b03f7d7b0

View file

@ -555,6 +555,8 @@ impl WebRTCSrc {
} }
fn connect_signaller(&self, signaller: &Signallable) { fn connect_signaller(&self, signaller: &Signallable) {
let instance = &*self.obj();
let _ = self let _ = self
.state .state
.lock() .lock()
@ -564,10 +566,10 @@ impl WebRTCSrc {
error: signaller.connect_closure( error: signaller.connect_closure(
"error", "error",
false, false,
glib::closure!(@to-owned self as this => move | glib::closure!(@watch instance => move |
_signaller: glib::Object, error: String| { _signaller: glib::Object, error: String| {
gst::element_error!( gst::element_error!(
this.obj(), instance,
gst::StreamError::Failed, gst::StreamError::Failed,
["Signalling error: {}", error] ["Signalling error: {}", error]
); );
@ -577,12 +579,13 @@ impl WebRTCSrc {
session_started: signaller.connect_closure( session_started: signaller.connect_closure(
"session-started", "session-started",
false, false,
glib::closure!(@to-owned self as this => move | glib::closure!(@watch instance => move |
_signaller: glib::Object, _signaller: glib::Object,
session_id: &str, session_id: &str,
_peer_id: &str| { _peer_id: &str| {
gst::info!(CAT, imp: this, "Session started: {session_id}"); let imp = instance.imp();
this.state.lock().unwrap().session_id = gst::info!(CAT, imp: imp, "Session started: {session_id}");
imp.state.lock().unwrap().session_id =
Some(session_id.to_string()); Some(session_id.to_string());
}), }),
), ),
@ -590,9 +593,9 @@ impl WebRTCSrc {
session_ended: signaller.connect_closure( session_ended: signaller.connect_closure(
"session-ended", "session-ended",
false, false,
glib::closure!(@to-owned self as this => move |_signaler: glib::Object, _session_id: &str|{ glib::closure!(@watch instance => move |_signaler: glib::Object, _session_id: &str|{
this.state.lock().unwrap().session_id = None; instance.imp().state.lock().unwrap().session_id = None;
this.obj().iterate_src_pads().into_iter().for_each(|pad| instance.iterate_src_pads().into_iter().for_each(|pad|
{ if let Err(e) = pad.map(|pad| pad.push_event(gst::event::Eos::new())) { { if let Err(e) = pad.map(|pad| pad.push_event(gst::event::Eos::new())) {
gst::error!(CAT, "Could not send EOS: {e:?}"); gst::error!(CAT, "Could not send EOS: {e:?}");
}} }}
@ -605,24 +608,22 @@ impl WebRTCSrc {
request_meta: signaller.connect_closure( request_meta: signaller.connect_closure(
"request-meta", "request-meta",
false, false,
glib::closure!(@to-owned self as this => move | glib::closure!(@watch instance => move |
_signaller: glib::Object| -> Option<gst::Structure> { _signaller: glib::Object| -> Option<gst::Structure> {
let meta = this.settings.lock().unwrap().meta.clone(); instance.imp().settings.lock().unwrap().meta.clone()
meta
}), }),
), ),
session_description: signaller.connect_closure( session_description: signaller.connect_closure(
"session-description", "session-description",
false, false,
glib::closure!(@to-owned self as this => move | glib::closure!(@watch instance => move |
_signaller: glib::Object, _signaller: glib::Object,
_peer_id: &str, _peer_id: &str,
desc: &gst_webrtc::WebRTCSessionDescription| { desc: &gst_webrtc::WebRTCSessionDescription| {
assert_eq!(desc.type_(), gst_webrtc::WebRTCSDPType::Offer); assert_eq!(desc.type_(), gst_webrtc::WebRTCSDPType::Offer);
this.handle_offer(desc); instance.imp().handle_offer(desc);
}), }),
), ),
@ -632,13 +633,13 @@ impl WebRTCSrc {
handle_ice: signaller.connect_closure( handle_ice: signaller.connect_closure(
"handle-ice", "handle-ice",
false, false,
glib::closure!(@to-owned self as this => move | glib::closure!(@watch instance => move |
_signaller: glib::Object, _signaller: glib::Object,
peer_id: &str, peer_id: &str,
sdp_m_line_index: u32, sdp_m_line_index: u32,
_sdp_mid: Option<String>, _sdp_mid: Option<String>,
candidate: &str| { candidate: &str| {
this.handle_ice(peer_id, Some(sdp_m_line_index), None, candidate); instance.imp().handle_ice(peer_id, Some(sdp_m_line_index), None, candidate);
}), }),
), ),
}); });