Skip to content

Commit

Permalink
Fix H264Writer writing 0 length payloads
Browse files Browse the repository at this point in the history
Before we would call Write even if no bytes were available.
  • Loading branch information
Sean-Der authored Feb 11, 2025
1 parent 46565ff commit 1c45355
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/media/h264writer/h264writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/media/h264writer/h264writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 1c45355

Please sign in to comment.