Browse Source
* rtmp: improve MsgCommandAMF0 * rtmp: fix MsgSetPeerBandwidth * rtmp: add message tests * rtmp: replace implementation with new one * rtmp: rename handshake functions * rtmp: avoid calling useless function * rtmp: use time.Duration for PTSDelta * rtmp: fix decoding chunks with relevant size * rtmp: rewrite implementation of rtmp connection * rtmp: fix tests * rtmp: improve error message * rtmp: replace h264 config implementation * link against github.com/notedit/rtmp * normalize MessageStreamID * rtmp: make acknowledge optional * rtmp: fix decoding of chunk2 + chunk3 * avoid using encoding/binarypull/1060/head
45 changed files with 2017 additions and 1036 deletions
@ -0,0 +1,89 @@
@@ -0,0 +1,89 @@
|
||||
package h264conf |
||||
|
||||
import ( |
||||
"fmt" |
||||
) |
||||
|
||||
// Conf is a RTMP H264 configuration.
|
||||
type Conf struct { |
||||
SPS []byte |
||||
PPS []byte |
||||
} |
||||
|
||||
// Unmarshal decodes a Conf from bytes.
|
||||
func (c *Conf) Unmarshal(buf []byte) error { |
||||
if len(buf) < 8 { |
||||
return fmt.Errorf("invalid size 1") |
||||
} |
||||
|
||||
pos := 5 |
||||
|
||||
spsCount := buf[pos] & 0x1F |
||||
pos++ |
||||
if spsCount != 1 { |
||||
return fmt.Errorf("sps count != 1 is unsupported") |
||||
} |
||||
|
||||
spsLen := int(uint16(buf[pos])<<8 | uint16(buf[pos+1])) |
||||
pos += 2 |
||||
if (len(buf) - pos) < spsLen { |
||||
return fmt.Errorf("invalid size 2") |
||||
} |
||||
|
||||
c.SPS = buf[pos : pos+spsLen] |
||||
pos += spsLen |
||||
|
||||
if (len(buf) - pos) < 3 { |
||||
return fmt.Errorf("invalid size 3") |
||||
} |
||||
|
||||
ppsCount := buf[pos] |
||||
pos++ |
||||
if ppsCount != 1 { |
||||
return fmt.Errorf("pps count != 1 is unsupported") |
||||
} |
||||
|
||||
ppsLen := int(uint16(buf[pos])<<8 | uint16(buf[pos+1])) |
||||
pos += 2 |
||||
if (len(buf) - pos) < ppsLen { |
||||
return fmt.Errorf("invalid size") |
||||
} |
||||
|
||||
c.PPS = buf[pos : pos+ppsLen] |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// Marshal encodes a Conf into bytes.
|
||||
func (c Conf) Marshal() ([]byte, error) { |
||||
spsLen := len(c.SPS) |
||||
ppsLen := len(c.PPS) |
||||
|
||||
buf := make([]byte, 11+spsLen+ppsLen) |
||||
|
||||
buf[0] = 1 |
||||
buf[1] = c.SPS[1] |
||||
buf[2] = c.SPS[2] |
||||
buf[3] = c.SPS[3] |
||||
buf[4] = 3 | 0xFC |
||||
buf[5] = 1 | 0xE0 |
||||
pos := 6 |
||||
|
||||
buf[pos] = byte(spsLen >> 8) |
||||
buf[pos+1] = byte(spsLen) |
||||
pos += 2 |
||||
|
||||
copy(buf[pos:], c.SPS) |
||||
pos += spsLen |
||||
|
||||
buf[pos] = 1 |
||||
pos++ |
||||
|
||||
buf[pos] = byte(ppsLen >> 8) |
||||
buf[pos+1] = byte(ppsLen) |
||||
pos += 2 |
||||
|
||||
copy(buf[pos:], c.PPS) |
||||
|
||||
return buf, nil |
||||
} |
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
package h264conf |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
var decoded = Conf{ |
||||
SPS: []byte{0x45, 0x32, 0xA3, 0x08}, |
||||
PPS: []byte{0x45, 0x34}, |
||||
} |
||||
|
||||
var encoded = []byte{ |
||||
0x1, 0x32, 0xa3, 0x8, 0xff, 0xe1, 0x0, 0x4, 0x45, 0x32, 0xa3, 0x8, 0x1, 0x0, 0x2, 0x45, 0x34, |
||||
} |
||||
|
||||
func TestUnmarshal(t *testing.T) { |
||||
var dec Conf |
||||
err := dec.Unmarshal(encoded) |
||||
require.NoError(t, err) |
||||
require.Equal(t, decoded, dec) |
||||
} |
||||
|
||||
func TestMarshal(t *testing.T) { |
||||
enc, err := decoded.Marshal() |
||||
require.NoError(t, err) |
||||
require.Equal(t, encoded, enc) |
||||
} |
@ -0,0 +1,227 @@
@@ -0,0 +1,227 @@
|
||||
package message |
||||
|
||||
import ( |
||||
"bytes" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/notedit/rtmp/format/flv/flvio" |
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/aler9/rtsp-simple-server/internal/rtmp/bytecounter" |
||||
) |
||||
|
||||
var readWriterCases = []struct { |
||||
name string |
||||
dec Message |
||||
enc []byte |
||||
}{ |
||||
{ |
||||
"acknowledge", |
||||
&MsgAcknowledge{ |
||||
Value: 45953968, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x3, |
||||
0x0, 0x0, 0x0, 0x0, 0x2, 0xbd, 0x33, 0xb0, |
||||
}, |
||||
}, |
||||
{ |
||||
"audio", |
||||
&MsgAudio{ |
||||
ChunkStreamID: 7, |
||||
DTS: 6013806 * time.Millisecond, |
||||
MessageStreamID: 4534543, |
||||
Rate: flvio.SOUND_44Khz, |
||||
Depth: flvio.SOUND_16BIT, |
||||
Channels: flvio.SOUND_STEREO, |
||||
AACType: flvio.AAC_RAW, |
||||
Payload: []byte{0x5A, 0xC0, 0x77, 0x40}, |
||||
}, |
||||
[]byte{ |
||||
0x7, 0x5b, 0xc3, 0x6e, 0x0, 0x0, 0x6, 0x8, |
||||
0x0, 0x45, 0x31, 0xf, 0xaf, 0x1, 0x5a, 0xc0, |
||||
0x77, 0x40, |
||||
}, |
||||
}, |
||||
{ |
||||
"command amf0", |
||||
&MsgCommandAMF0{ |
||||
ChunkStreamID: 3, |
||||
MessageStreamID: 345243, |
||||
Name: "i8yythrergre", |
||||
CommandID: 56456, |
||||
Arguments: []interface{}{ |
||||
flvio.AMFMap{ |
||||
{K: "k1", V: "v1"}, |
||||
{K: "k2", V: "v2"}, |
||||
}, |
||||
nil, |
||||
}, |
||||
}, |
||||
[]byte{ |
||||
0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2f, 0x14, |
||||
0x0, 0x5, 0x44, 0x9b, 0x2, 0x0, 0xc, 0x69, |
||||
0x38, 0x79, 0x79, 0x74, 0x68, 0x72, 0x65, 0x72, |
||||
0x67, 0x72, 0x65, 0x0, 0x40, 0xeb, 0x91, 0x0, |
||||
0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x2, 0x6b, |
||||
0x31, 0x2, 0x0, 0x2, 0x76, 0x31, 0x0, 0x2, |
||||
0x6b, 0x32, 0x2, 0x0, 0x2, 0x76, 0x32, 0x0, |
||||
0x0, 0x9, 0x5, |
||||
}, |
||||
}, |
||||
{ |
||||
"data amf0", |
||||
&MsgDataAMF0{ |
||||
ChunkStreamID: 3, |
||||
MessageStreamID: 345243, |
||||
Payload: []interface{}{ |
||||
float64(234), |
||||
"string", |
||||
nil, |
||||
}, |
||||
}, |
||||
[]byte{ |
||||
0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x13, 0x12, |
||||
0x0, 0x5, 0x44, 0x9b, 0x0, 0x40, 0x6d, 0x40, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x6, |
||||
0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x05, |
||||
}, |
||||
}, |
||||
{ |
||||
"set chunk size", |
||||
&MsgSetChunkSize{ |
||||
Value: 10000, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x1, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x27, 0x10, |
||||
}, |
||||
}, |
||||
{ |
||||
"set peer bandwidth", |
||||
&MsgSetChunkSize{ |
||||
Value: 10000, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x1, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x27, 0x10, |
||||
}, |
||||
}, |
||||
{ |
||||
"set window ack size", |
||||
&MsgSetChunkSize{ |
||||
Value: 10000, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x1, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x27, 0x10, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control ping request", |
||||
&MsgUserControlPingRequest{ |
||||
ServerTime: 569834435, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x21, 0xf6, |
||||
0xfb, 0xc3, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control ping response", |
||||
&MsgUserControlPingResponse{ |
||||
ServerTime: 569834435, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x21, 0xf6, |
||||
0xfb, 0xc3, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control set buffer length", |
||||
&MsgUserControlSetBufferLength{ |
||||
StreamID: 35534, |
||||
BufferLength: 235345, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, |
||||
0x8a, 0xce, 0x0, 0x3, 0x97, 0x51, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control stream begin", |
||||
&MsgUserControlStreamBegin{ |
||||
StreamID: 35534, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
||||
0x8a, 0xce, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control stream dry", |
||||
&MsgUserControlStreamDry{ |
||||
StreamID: 35534, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, |
||||
0x8a, 0xce, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control stream eof", |
||||
&MsgUserControlStreamEOF{ |
||||
StreamID: 35534, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, |
||||
0x8a, 0xce, |
||||
}, |
||||
}, |
||||
{ |
||||
"user control stream is recorded", |
||||
&MsgUserControlStreamIsRecorded{ |
||||
StreamID: 35534, |
||||
}, |
||||
[]byte{ |
||||
0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x4, |
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, |
||||
0x8a, 0xce, |
||||
}, |
||||
}, |
||||
{ |
||||
"video", |
||||
&MsgVideo{ |
||||
ChunkStreamID: 6, |
||||
DTS: 2543534 * time.Millisecond, |
||||
MessageStreamID: 0x1000000, |
||||
IsKeyFrame: true, |
||||
H264Type: flvio.AVC_SEQHDR, |
||||
PTSDelta: 10 * time.Millisecond, |
||||
Payload: []byte{0x01, 0x02, 0x03}, |
||||
}, |
||||
[]byte{ |
||||
0x6, 0x26, 0xcf, 0xae, 0x0, 0x0, 0x8, 0x9, |
||||
0x1, 0x0, 0x0, 0x0, 0x17, 0x0, 0x0, 0x0, |
||||
0xa, 0x1, 0x2, 0x3, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
func TestReader(t *testing.T) { |
||||
for _, ca := range readWriterCases { |
||||
t.Run(ca.name, func(t *testing.T) { |
||||
r := NewReader(bytecounter.NewReader(bytes.NewReader(ca.enc)), nil) |
||||
dec, err := r.Read() |
||||
require.NoError(t, err) |
||||
require.Equal(t, ca.dec, dec) |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
package message |
||||
|
||||
import ( |
||||
"bytes" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/aler9/rtsp-simple-server/internal/rtmp/bytecounter" |
||||
) |
||||
|
||||
func TestWriter(t *testing.T) { |
||||
for _, ca := range readWriterCases { |
||||
t.Run(ca.name, func(t *testing.T) { |
||||
var buf bytes.Buffer |
||||
r := NewWriter(bytecounter.NewWriter(&buf), true) |
||||
err := r.Write(ca.dec) |
||||
require.NoError(t, err) |
||||
require.Equal(t, ca.enc, buf.Bytes()) |
||||
}) |
||||
} |
||||
} |
Loading…
Reference in new issue