Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/media/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (i *Input) onPadAdded(_ *gst.Element, pad *gst.Pad) {
state := timingState
i.registerGatePad(padName, state)
i.addGateProbe(pad, padName, state)
i.addSegmentEventProbe(pad, padName)
i.addSegmentEventProbe(pad, padName, state)
}

// Gather bitrate stats & attach latency meta from the pipeline
Expand Down
60 changes: 23 additions & 37 deletions pkg/media/input_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type padTimingState struct {
windowStreamAccum time.Duration
windowElapsedAccum time.Duration
steadyWindowCount int

segmentStart atomic.Duration
}

func (i *Input) addGateProbe(pad *gst.Pad, padName string, state *padTimingState) {
Expand Down Expand Up @@ -96,50 +98,29 @@ func (i *Input) addGateProbe(pad *gst.Pad, padName string, state *padTimingState
})
}

func (i *Input) addSegmentEventProbe(pad *gst.Pad, padName string) {
logSegmentEvent := func(direction string, seg *gst.Segment) {
if seg == nil {
logger.Debugw("nil segment event received", "pad", padName, "direction", direction)
return
}

fields := []interface{}{
"pad", padName,
"direction", direction,
"format", seg.GetFormat(),
"rate", seg.GetRate(),
"appliedRate", seg.GetAppliedRate(),
"base", seg.GetBase(),
"start", seg.GetStart(),
"stop", seg.GetStop(),
"time", seg.GetTime(),
"position", seg.GetPosition(),
}

if seg.GetFormat() == gst.FormatTime {
fields = append(fields,
"baseDur", time.Duration(seg.GetBase()),
"startDur", time.Duration(seg.GetStart()),
"stopDur", time.Duration(seg.GetStop()),
"timeDur", time.Duration(seg.GetTime()),
"positionDur", time.Duration(seg.GetPosition()),
)
}

logger.Debugw("segment event received", fields...)
}

func (i *Input) addSegmentEventProbe(pad *gst.Pad, padName string, state *padTimingState) {
pad.AddProbe(gst.PadProbeTypeEventDownstream, func(_ *gst.Pad, info *gst.PadProbeInfo) gst.PadProbeReturn {
event := info.GetEvent()
if event == nil {
if event == nil || event.Type() != gst.EventTypeSegment {
return gst.PadProbeOK
}

switch event.Type() {
case gst.EventTypeSegment:
logSegmentEvent("downstream", event.ParseSegment())
seg := event.ParseSegment()
if seg == nil {
logger.Debugw("segment event missing data", "pad", padName)
return gst.PadProbeOK
}

state.segmentStart.Store(time.Duration(seg.GetStart()))
logger.Debugw("segment event received",
"pad", padName,
"format", seg.GetFormat(),
"start", seg.GetStart(),
"startDur", time.Duration(seg.GetStart()),
"time", seg.GetTime(),
"timeDur", time.Duration(seg.GetTime()),
"rate", seg.GetRate(),
)
return gst.PadProbeOK
})
}
Expand Down Expand Up @@ -292,6 +273,11 @@ func applyPadOffset(buffer *gst.Buffer, state *padTimingState, pts time.Duration
return false
}

// by design, every buffer must be preceded by SEGMENT (and SEGMENT is sticky),
// so downstream elements will push STREAM_START → CAPS → SEGMENT before any buffers.
// By the time buffers are processed, the segment start time will be set.
adj += state.segmentStart.Load()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you comment on the flow here? Are we certain applyPadOffset always gets called after addSegmentEventProbe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - by design every buffer must be preceded by SEGMENT (and SEGMENT is sticky), so downstream elements will push STREAM_START → CAPS → SEGMENT before any buffers. Adding a short comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the comment


buffer.SetPresentationTimestamp(gst.ClockTime(adj))
return true
}
Loading