Fix H264Writer writing 0 length payloads

Before we would call Write even if no bytes were available.
This commit is contained in:
Sean DuBois
2025-02-11 14:57:36 -05:00
committed by GitHub
parent 46565ffd95
commit 1c45355b8c
2 changed files with 38 additions and 1 deletions

View File

@@ -62,7 +62,7 @@ func (h *H264Writer) WriteRTP(packet *rtp.Packet) error {
}
data, err := h.cachedPacket.Unmarshal(packet.Payload)
if err != nil {
if err != nil || len(data) == 0 {
return err
}

View File

@@ -153,3 +153,40 @@ func TestWriteRTP(t *testing.T) {
})
}
}
type writerCounter struct {
writeCount int
}
func (w *writerCounter) Write([]byte) (int, error) {
w.writeCount++
return 0, nil
}
func (w *writerCounter) Close() error {
return nil
}
func TestNoZeroWrite(t *testing.T) {
payloads := [][]byte{
{0x1c, 0x80, 0x01, 0x02, 0x03},
{0x1c, 0x00, 0x04, 0x05, 0x06},
{0x1c, 0x00, 0x07, 0x08, 0x09},
{0x1c, 0x00, 0x10, 0x11, 0x12},
{0x1c, 0x40, 0x13, 0x14, 0x15},
}
writer := &writerCounter{}
h264Writer := &H264Writer{
hasKeyFrame: true,
writer: writer,
}
for i := range payloads {
assert.NoError(t, h264Writer.WriteRTP(&rtp.Packet{
Payload: payloads[i],
}))
}
assert.Equal(t, 1, writer.writeCount)
}