|
| 1 | +// Copyright 2026 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sdk |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/go-gst/go-gst/gst" |
| 22 | + "github.com/go-gst/go-gst/gst/app" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +// TestShouldSendEOS pins the cleanup decision in AppWriter.start(). |
| 27 | +// |
| 28 | +// CS-1547: the guard used to be playing.IsBroken(), which asks "did GStreamer's |
| 29 | +// PLAYING notification reach us?" rather than "is this appsrc linked into the |
| 30 | +// pipeline?". Those differ during shutdown, because CloseWriters() sets closing |
| 31 | +// before GStreamer finishes the async state change and submitOp() then drops the |
| 32 | +// notification - permanently. The middle case below is the frozen pipeline. |
| 33 | +func TestShouldSendEOS(t *testing.T) { |
| 34 | + t.Run("never added to the pipeline", func(t *testing.T) { |
| 35 | + w := &AppWriter{} |
| 36 | + |
| 37 | + require.False(t, w.shouldSendEOS(), |
| 38 | + "no appsrc in the pipeline means nothing downstream is waiting for EOS") |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("added to the pipeline but PLAYING never delivered", func(t *testing.T) { |
| 42 | + w := &AppWriter{} |
| 43 | + w.MarkAddedToPipeline() |
| 44 | + |
| 45 | + require.False(t, w.playing.IsBroken(), |
| 46 | + "precondition: this is the race - the PLAYING notification was dropped") |
| 47 | + require.True(t, w.shouldSendEOS(), |
| 48 | + "CS-1547: the appsrc is linked into the pipeline, so cleanup must send EOS "+ |
| 49 | + "even though we were never told it reached PLAYING") |
| 50 | + |
| 51 | + // this state - owing EOS without ever having been told PLAYING - is exactly |
| 52 | + // what AppWriter.start() logs as "appsrc never reported PLAYING, sending EOS |
| 53 | + // anyway", the signal that the race occurred and was handled |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("PLAYING implies added to the pipeline", func(t *testing.T) { |
| 57 | + w := &AppWriter{} |
| 58 | + w.Playing() |
| 59 | + |
| 60 | + require.True(t, w.addedToPipeline.IsBroken(), |
| 61 | + "an appsrc cannot reach PLAYING without being linked into the pipeline") |
| 62 | + require.True(t, w.shouldSendEOS(), |
| 63 | + "the normal path must keep sending EOS exactly as before") |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +const ( |
| 68 | + testAudioCaps = "audio/x-raw,format=S16LE,layout=interleaved,rate=48000,channels=1" |
| 69 | + testFrameSize = 1920 // 20ms of 48kHz mono S16LE |
| 70 | + testFrameDuration = 20 * time.Millisecond |
| 71 | + eosWaitTimeout = 5 * time.Second |
| 72 | +) |
| 73 | + |
| 74 | +type mixerTestPipeline struct { |
| 75 | + pipeline *gst.Pipeline |
| 76 | + mixer *gst.Element |
| 77 | +} |
| 78 | + |
| 79 | +func newMixerTestPipeline(t *testing.T) *mixerTestPipeline { |
| 80 | + t.Helper() |
| 81 | + gst.Init(nil) |
| 82 | + |
| 83 | + pipeline, err := gst.NewPipeline("cs-1547") |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + mixer, err := gst.NewElement("audiomixer") |
| 87 | + require.NoError(t, err) |
| 88 | + sink, err := gst.NewElement("fakesink") |
| 89 | + require.NoError(t, err) |
| 90 | + require.NoError(t, sink.SetProperty("sync", false)) |
| 91 | + |
| 92 | + require.NoError(t, pipeline.AddMany(mixer, sink)) |
| 93 | + require.NoError(t, mixer.Link(sink)) |
| 94 | + |
| 95 | + p := &mixerTestPipeline{pipeline: pipeline, mixer: mixer} |
| 96 | + t.Cleanup(func() { _ = pipeline.SetState(gst.StateNull) }) |
| 97 | + return p |
| 98 | +} |
| 99 | + |
| 100 | +// addTrack links a new appsrc into the pipeline, exactly like OnTrackAdded(). |
| 101 | +func (p *mixerTestPipeline) addTrack(t *testing.T, name string) *app.Source { |
| 102 | + t.Helper() |
| 103 | + |
| 104 | + elem, err := gst.NewElementWithName("appsrc", "app_"+name) |
| 105 | + require.NoError(t, err) |
| 106 | + conv, err := gst.NewElement("audioconvert") |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + src := app.SrcFromElement(elem) |
| 110 | + src.SetCaps(gst.NewCapsFromString(testAudioCaps)) |
| 111 | + src.SetArg("format", "time") |
| 112 | + require.NoError(t, elem.SetProperty("is-live", false)) |
| 113 | + |
| 114 | + require.NoError(t, p.pipeline.AddMany(elem, conv)) |
| 115 | + require.NoError(t, gst.ElementLinkMany(elem, conv, p.mixer)) |
| 116 | + |
| 117 | + // dynamic add: bring the new branch up to the pipeline's state |
| 118 | + require.True(t, elem.SyncStateWithParent()) |
| 119 | + require.True(t, conv.SyncStateWithParent()) |
| 120 | + |
| 121 | + return src |
| 122 | +} |
| 123 | + |
| 124 | +func pushSilence(t *testing.T, src *app.Source, frames int, startPTS time.Duration) time.Duration { |
| 125 | + t.Helper() |
| 126 | + |
| 127 | + pts := startPTS |
| 128 | + silence := make([]byte, testFrameSize) |
| 129 | + for i := 0; i < frames; i++ { |
| 130 | + b := gst.NewBufferFromBytes(silence) |
| 131 | + b.SetPresentationTimestamp(gst.ClockTime(uint64(pts))) |
| 132 | + b.SetDuration(gst.ClockTime(uint64(testFrameDuration))) |
| 133 | + require.Equal(t, gst.FlowOK, src.PushBuffer(b)) |
| 134 | + pts += testFrameDuration |
| 135 | + } |
| 136 | + return pts |
| 137 | +} |
| 138 | + |
| 139 | +// waitForEOS returns true if the pipeline reached EOS within eosWaitTimeout. |
| 140 | +func (p *mixerTestPipeline) waitForEOS(t *testing.T) bool { |
| 141 | + t.Helper() |
| 142 | + |
| 143 | + bus := p.pipeline.GetPipelineBus() |
| 144 | + deadline := time.Now().Add(eosWaitTimeout) |
| 145 | + for time.Now().Before(deadline) { |
| 146 | + msg := bus.TimedPopFiltered(gst.ClockTime(uint64(500*time.Millisecond)), gst.MessageEOS|gst.MessageError) |
| 147 | + if msg == nil { |
| 148 | + continue |
| 149 | + } |
| 150 | + switch msg.Type() { |
| 151 | + case gst.MessageEOS: |
| 152 | + return true |
| 153 | + case gst.MessageError: |
| 154 | + t.Fatalf("pipeline error: %s", msg.String()) |
| 155 | + } |
| 156 | + } |
| 157 | + return false |
| 158 | +} |
| 159 | + |
| 160 | +// TestMissingEOSFreezesPipeline: the late writer never sends EOS |
| 161 | +// (cleanup guard skipped), so the mixer never forwards EOS and shutdown hangs. |
| 162 | +func TestMissingEOSFreezesPipeline(t *testing.T) { |
| 163 | + p := newMixerTestPipeline(t) |
| 164 | + |
| 165 | + srcA := p.addTrack(t, "A") |
| 166 | + require.NoError(t, p.pipeline.SetState(gst.StatePlaying)) |
| 167 | + pts := pushSilence(t, srcA, 10, 0) |
| 168 | + |
| 169 | + // a subscription lands inside the CloseWriters() window: the appsrc is |
| 170 | + // linked into the running pipeline |
| 171 | + srcB := p.addTrack(t, "B") |
| 172 | + pushSilence(t, srcB, 2, pts) |
| 173 | + |
| 174 | + // shutdown: A drains and sends EOS, B's cleanup skipped EndStream() |
| 175 | + // because playing.IsBroken() == false |
| 176 | + require.Equal(t, gst.FlowOK, srcA.EndStream()) |
| 177 | + |
| 178 | + require.False(t, p.waitForEOS(t), |
| 179 | + "CS-1547: expected the pipeline to hang - it should NOT reach EOS while app_B never sent EOS") |
| 180 | +} |
| 181 | + |
| 182 | +// TestEOSFromAllWritersCompletes is the control: with EndStream() on |
| 183 | +// every appsrc that was linked into the pipeline, EOS propagates immediately. |
| 184 | +func TestEOSFromAllWritersCompletes(t *testing.T) { |
| 185 | + p := newMixerTestPipeline(t) |
| 186 | + |
| 187 | + srcA := p.addTrack(t, "A") |
| 188 | + require.NoError(t, p.pipeline.SetState(gst.StatePlaying)) |
| 189 | + pts := pushSilence(t, srcA, 10, 0) |
| 190 | + |
| 191 | + srcB := p.addTrack(t, "B") |
| 192 | + pushSilence(t, srcB, 2, pts) |
| 193 | + |
| 194 | + require.Equal(t, gst.FlowOK, srcA.EndStream()) |
| 195 | + require.Equal(t, gst.FlowOK, srcB.EndStream()) |
| 196 | + |
| 197 | + require.True(t, p.waitForEOS(t), "pipeline should reach EOS once every appsrc sent EOS") |
| 198 | +} |
| 199 | + |
| 200 | +// TestEOSFromNotYetPlayingAppsrc is the premise the fix rests on: EOS |
| 201 | +// from an appsrc that was linked and asked to start, but has not been reported |
| 202 | +// as PLAYING, still reaches the mixer. |
| 203 | +// |
| 204 | +// This is the state a track is in during the race. AddSourceBin links the new |
| 205 | +// bin and calls SyncStateWithParent (gstreamer/bin.go), so the element is on its |
| 206 | +// way to PLAYING via PAUSED while our Go code is still waiting for a bus message |
| 207 | +// that CloseWriters will cause to be dropped. Sending EOS from there works. |
| 208 | +// |
| 209 | +// Boundary worth knowing: an appsrc left in NULL - never asked to change state - |
| 210 | +// accepts EndStream() with FlowOK but never propagates it, and the pipeline hangs. |
| 211 | +// egress never links a bin without syncing its state; if that ever changes, EOS |
| 212 | +// alone will not be enough to unblock shutdown. |
| 213 | +func TestEOSFromNotYetPlayingAppsrc(t *testing.T) { |
| 214 | + p := newMixerTestPipeline(t) |
| 215 | + |
| 216 | + srcA := p.addTrack(t, "A") |
| 217 | + require.NoError(t, p.pipeline.SetState(gst.StatePlaying)) |
| 218 | + pts := pushSilence(t, srcA, 10, 0) |
| 219 | + |
| 220 | + // linked and started, but only as far as PAUSED - PLAYING was never confirmed |
| 221 | + elem, err := gst.NewElementWithName("appsrc", "app_B") |
| 222 | + require.NoError(t, err) |
| 223 | + conv, err := gst.NewElement("audioconvert") |
| 224 | + require.NoError(t, err) |
| 225 | + |
| 226 | + srcB := app.SrcFromElement(elem) |
| 227 | + srcB.SetCaps(gst.NewCapsFromString(testAudioCaps)) |
| 228 | + srcB.SetArg("format", "time") |
| 229 | + require.NoError(t, elem.SetProperty("is-live", false)) |
| 230 | + require.NoError(t, p.pipeline.AddMany(elem, conv)) |
| 231 | + require.NoError(t, gst.ElementLinkMany(elem, conv, p.mixer)) |
| 232 | + require.NoError(t, elem.SetState(gst.StatePaused)) |
| 233 | + require.NoError(t, conv.SetState(gst.StatePaused)) |
| 234 | + require.Equal(t, gst.StatePaused, elem.GetCurrentState(), "precondition: never reported PLAYING") |
| 235 | + |
| 236 | + pushSilence(t, srcB, 2, pts) |
| 237 | + require.Equal(t, gst.FlowOK, srcA.EndStream()) |
| 238 | + require.Equal(t, gst.FlowOK, srcB.EndStream()) |
| 239 | + |
| 240 | + require.True(t, p.waitForEOS(t), |
| 241 | + "EOS from a linked-but-not-yet-PLAYING appsrc must still complete the pipeline - "+ |
| 242 | + "this is what makes addedToPipeline a valid guard") |
| 243 | +} |
0 commit comments