mediamtx/internal/rtmp/rawmessage/writer_test.go

190 lines
4.8 KiB
Go
Raw Normal View History

2022-06-07 19:09:57 +00:00
package rawmessage
import (
"bytes"
"testing"
2022-06-08 18:47:36 +00:00
"github.com/aler9/rtsp-simple-server/internal/rtmp/bytecounter"
2022-06-07 19:09:57 +00:00
"github.com/aler9/rtsp-simple-server/internal/rtmp/chunk"
"github.com/stretchr/testify/require"
)
func TestWriter(t *testing.T) {
t.Run("chunk0 + chunk1", func(t *testing.T) {
var buf bytes.Buffer
2022-06-08 18:47:36 +00:00
w := NewWriter(bytecounter.NewWriter(&buf))
2022-06-07 19:09:57 +00:00
err := w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 64),
})
require.NoError(t, err)
var c0 chunk.Chunk0
2022-06-08 18:47:36 +00:00
err = c0.Read(&buf, 128)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk0{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
BodyLen: 64,
Body: bytes.Repeat([]byte{0x03}, 64),
}, c0)
err = w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576 + 15,
Type: chunk.MessageTypeSetWindowAckSize,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x04}, 64),
})
require.NoError(t, err)
var c1 chunk.Chunk1
2022-06-08 18:47:36 +00:00
err = c1.Read(&buf, 128)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk1{
ChunkStreamID: 27,
TimestampDelta: 15,
Type: chunk.MessageTypeSetWindowAckSize,
BodyLen: 64,
Body: bytes.Repeat([]byte{0x04}, 64),
}, c1)
})
t.Run("chunk0 + chunk2 + chunk3", func(t *testing.T) {
var buf bytes.Buffer
2022-06-08 18:47:36 +00:00
w := NewWriter(bytecounter.NewWriter(&buf))
2022-06-07 19:09:57 +00:00
err := w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 64),
})
require.NoError(t, err)
var c0 chunk.Chunk0
2022-06-08 18:47:36 +00:00
err = c0.Read(&buf, 128)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk0{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
BodyLen: 64,
Body: bytes.Repeat([]byte{0x03}, 64),
}, c0)
err = w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576 + 15,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x04}, 64),
})
require.NoError(t, err)
var c2 chunk.Chunk2
2022-06-08 18:47:36 +00:00
err = c2.Read(&buf, 64)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk2{
ChunkStreamID: 27,
TimestampDelta: 15,
Body: bytes.Repeat([]byte{0x04}, 64),
}, c2)
err = w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576 + 15 + 15,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x05}, 64),
})
require.NoError(t, err)
var c3 chunk.Chunk3
2022-06-08 18:47:36 +00:00
err = c3.Read(&buf, 64)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk3{
ChunkStreamID: 27,
Body: bytes.Repeat([]byte{0x05}, 64),
}, c3)
})
t.Run("chunk0 + chunk3", func(t *testing.T) {
var buf bytes.Buffer
2022-06-08 18:47:36 +00:00
w := NewWriter(bytecounter.NewWriter(&buf))
2022-06-07 19:09:57 +00:00
err := w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 192),
})
require.NoError(t, err)
var c0 chunk.Chunk0
2022-06-08 18:47:36 +00:00
err = c0.Read(&buf, 128)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk0{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
BodyLen: 192,
Body: bytes.Repeat([]byte{0x03}, 128),
}, c0)
var c3 chunk.Chunk3
2022-06-08 18:47:36 +00:00
err = c3.Read(&buf, 64)
2022-06-07 19:09:57 +00:00
require.NoError(t, err)
require.Equal(t, chunk.Chunk3{
ChunkStreamID: 27,
Body: bytes.Repeat([]byte{0x03}, 64),
}, c3)
})
}
2022-06-08 18:47:36 +00:00
func TestWriterAcknowledge(t *testing.T) {
2022-07-16 10:42:48 +00:00
for _, ca := range []string{"standard", "overflow"} {
t.Run(ca, func(t *testing.T) {
var buf bytes.Buffer
bcw := bytecounter.NewWriter(&buf)
w := NewWriter(bcw)
if ca == "overflow" {
bcw.SetCount(4294967096)
w.ackValue = 4294967096
}
w.SetChunkSize(65536)
w.SetWindowAckSize(100)
err := w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 200),
})
require.NoError(t, err)
err = w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576,
Type: chunk.MessageTypeSetPeerBandwidth,
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 200),
})
require.EqualError(t, err, "no acknowledge received within window")
2022-06-08 18:47:36 +00:00
})
}
}