Skip to content

Commit 1c45355

Browse files
authored
Fix H264Writer writing 0 length payloads
Before we would call Write even if no bytes were available.
1 parent 46565ff commit 1c45355

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pkg/media/h264writer/h264writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (h *H264Writer) WriteRTP(packet *rtp.Packet) error {
6262
}
6363

6464
data, err := h.cachedPacket.Unmarshal(packet.Payload)
65-
if err != nil {
65+
if err != nil || len(data) == 0 {
6666
return err
6767
}
6868

pkg/media/h264writer/h264writer_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,40 @@ func TestWriteRTP(t *testing.T) {
153153
})
154154
}
155155
}
156+
157+
type writerCounter struct {
158+
writeCount int
159+
}
160+
161+
func (w *writerCounter) Write([]byte) (int, error) {
162+
w.writeCount++
163+
164+
return 0, nil
165+
}
166+
167+
func (w *writerCounter) Close() error {
168+
return nil
169+
}
170+
171+
func TestNoZeroWrite(t *testing.T) {
172+
payloads := [][]byte{
173+
{0x1c, 0x80, 0x01, 0x02, 0x03},
174+
{0x1c, 0x00, 0x04, 0x05, 0x06},
175+
{0x1c, 0x00, 0x07, 0x08, 0x09},
176+
{0x1c, 0x00, 0x10, 0x11, 0x12},
177+
{0x1c, 0x40, 0x13, 0x14, 0x15},
178+
}
179+
180+
writer := &writerCounter{}
181+
h264Writer := &H264Writer{
182+
hasKeyFrame: true,
183+
writer: writer,
184+
}
185+
186+
for i := range payloads {
187+
assert.NoError(t, h264Writer.WriteRTP(&rtp.Packet{
188+
Payload: payloads[i],
189+
}))
190+
}
191+
assert.Equal(t, 1, writer.writeCount)
192+
}

0 commit comments

Comments
 (0)