From 64852648858dc1de855cdd455cff84bd37d0b58b Mon Sep 17 00:00:00 2001
From: Kipp Cannon <kipp.cannon@ligo.org>
Date: Fri, 27 Aug 2021 00:44:51 +0900
Subject: [PATCH] frameccp_channeldemux: move new caps code

- into src_pad_do_pending_events().  include logic to disable it so the heart beat code path still works.
- this is a step towards fixing the lack of stream ID handling
- take the opportunity to ensure every gst_event_push() operation in src_pad_do_pending_events() is properly error-checked.  there's still no error handling in the calling codes but that's a problem for another day.
---
 .../gst/framecpp/framecpp_channeldemux.cc     | 59 ++++++++++++-------
 1 file changed, 37 insertions(+), 22 deletions(-)

diff --git a/gstlal-ugly/gst/framecpp/framecpp_channeldemux.cc b/gstlal-ugly/gst/framecpp/framecpp_channeldemux.cc
index 8536d9afc5..20382a359f 100644
--- a/gstlal-ugly/gst/framecpp/framecpp_channeldemux.cc
+++ b/gstlal-ugly/gst/framecpp/framecpp_channeldemux.cc
@@ -518,18 +518,40 @@ static GstPad *get_src_pad(GstFrameCPPChannelDemux *element, const char *name, e
  */
 
 
-static gboolean src_pad_do_pending_events(GstFrameCPPChannelDemux *element, GstPad *pad)
+static gboolean src_pad_do_pending_events(GstFrameCPPChannelDemux *element, GstPad *pad,  GstCaps *caps)
 {
 	struct pad_state *pad_state = (struct pad_state *) gst_pad_get_element_private(pad);
 	gboolean success = TRUE;
 
 	g_assert(pad_state != NULL);
 
+	/*
+	 * do caps before tags to trigger a tag list update if needed.
+	 * if caps is NULL this is skipped.  need this case because they
+	 * are not known in the heart beat code path.
+	 */
+
+	if(caps) {
+		GstCaps *current_caps = gst_pad_get_current_caps(pad);
+		if(current_caps && gst_caps_is_equal(caps, current_caps)) {
+			/* nothing to do */
+			gst_caps_unref(current_caps);
+			gst_caps_unref(caps);
+		} else {
+			GST_LOG_OBJECT(pad, "new caps: %" GST_PTR_FORMAT, caps);
+			success = gst_pad_push_event(pad, gst_event_new_caps(caps));
+			if(current_caps)
+				gst_caps_unref(current_caps);
+			if(!success)
+				GST_ERROR_OBJECT(pad, "failed to push caps");
+		}
+	}
+
 	/*
 	 * forward most recent new segment event
 	 */
 
-	if(pad_state->need_new_segment && element->last_segment_event) {
+	if(success && pad_state->need_new_segment && element->last_segment_event) {
 		GST_LOG_OBJECT(pad, "push %" GST_PTR_FORMAT, element->last_segment_event);
 		gst_event_ref(element->last_segment_event);
 		success = gst_pad_push_event(pad, element->last_segment_event);
@@ -551,8 +573,11 @@ static gboolean src_pad_do_pending_events(GstFrameCPPChannelDemux *element, GstP
 		tag_list = gst_tag_list_make_writable(tag_list);
 		gst_tag_list_insert(tag_list, element->tag_list, GST_TAG_MERGE_KEEP);
 		GST_LOG_OBJECT(pad, "push new %" GST_PTR_FORMAT, tag_list);
-		gst_pad_push_event(pad, gst_event_new_tag(tag_list));
-		pad_state->need_tags = FALSE;
+		success = gst_pad_push_event(pad, gst_event_new_tag(tag_list));
+		if(!success)
+			GST_ERROR_OBJECT(pad, "failed to push tags");
+		else
+			pad_state->need_tags = FALSE;
 	}
 
 	return success;
@@ -567,7 +592,7 @@ static gboolean src_pad_do_pending_events(GstFrameCPPChannelDemux *element, GstP
 static GstFlowReturn frvect_to_buffer_and_push(GstFrameCPPChannelDemux *element, GstPad *pad, gst_framecpp_frvect vect, GstClockTime timestamp)
 {
 	struct pad_state *pad_state = (struct pad_state *) gst_pad_get_element_private(pad);
-	GstCaps *caps, *current_caps;
+	GstCaps *caps;
 	GstBuffer *buffer;
 	gint rate;
 	guint unit_size;
@@ -576,28 +601,17 @@ static GstFlowReturn frvect_to_buffer_and_push(GstFrameCPPChannelDemux *element,
 	g_assert(pad_state != NULL);
 
 	/*
-	 * update the source pad's caps if needed.  this is done before
-	 * other events to allow the tag list to get updated if needed.
+	 * compute caps for this vector
 	 */
 
 	caps = FrVect_get_caps(vect, &rate, &unit_size);
-	current_caps = gst_pad_get_current_caps(pad);
-	if(current_caps && gst_caps_is_equal(caps, current_caps)) {
-		/* nothing to do */
-		gst_caps_unref(current_caps);
-		gst_caps_unref(caps);
-	} else {
-		GST_LOG_OBJECT(pad, "new caps: %" GST_PTR_FORMAT, caps);
-		gst_pad_push_event(pad, gst_event_new_caps(caps));
-		if(current_caps)
-			gst_caps_unref(current_caps);
-	}
 
 	/*
-	 * do other pending events.  FIXME:  check for errors?
+	 * do pending events.  this takes ownership of the caps FIXME:
+	 * check for errors?
 	 */
 
-	src_pad_do_pending_events(element, pad);
+	src_pad_do_pending_events(element, pad, caps);
 
 	/*
 	 * convert FrVect to GstBuffer
@@ -670,10 +684,11 @@ static GstFlowReturn push_heart_beat(GstFrameCPPChannelDemux *element, GstPad *p
 	g_assert(pad_state != NULL);
 
 	/*
-	 * do pending events.  FIXME:  check for errors?
+	 * do pending events.  don't update caps.  FIXME:  check for
+	 * errors?
 	 */
 
-	src_pad_do_pending_events(element, pad);
+	src_pad_do_pending_events(element, pad, NULL);
 
 	/*
 	 * create heartbeat buffer for this pad
-- 
GitLab