diff --git a/Makefile b/Makefile index b5449f7c..d456399b 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,9 @@ generate: wire $(WIRE) ;\ } +test: wire + go test ./... + GO_TARGET=proto/livekit proto: protoc protoc-gen-go twirp-gen @{ \ diff --git a/go.mod b/go.mod index f59e2ad1..6203a9ac 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/pion/stun v0.3.5 github.com/pion/webrtc/v3 v3.0.0-beta.12 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.6.1 github.com/twitchtv/twirp v7.1.0+incompatible github.com/urfave/cli/v2 v2.2.0 github.com/urfave/negroni v1.0.0 diff --git a/pkg/rtc/mediaengine_test.go b/pkg/rtc/mediaengine_test.go new file mode 100644 index 00000000..0df9a506 --- /dev/null +++ b/pkg/rtc/mediaengine_test.go @@ -0,0 +1,65 @@ +package rtc + +import ( + "testing" + + "github.com/pion/webrtc/v3" + "github.com/stretchr/testify/assert" +) + +const sdpValue = `v=0 +o=- 884433216 1576829404 IN IP4 0.0.0.0 +s=- +t=0 0 +a=fingerprint:sha-256 1D:6B:6D:18:95:41:F9:BC:E4:AC:25:6A:26:A3:C8:09:D2:8C:EE:1B:7D:54:53:33:F7:E3:2C:0D:FE:7A:9D:6B +a=group:BUNDLE 0 1 2 +m=audio 9 UDP/TLS/RTP/SAVPF 0 8 111 9 +c=IN IP4 0.0.0.0 +a=mid:0 +a=rtpmap:0 PCMU/8000 +a=rtpmap:8 PCMA/8000 +a=rtpmap:111 opus/48000/2 +a=fmtp:111 minptime=10;useinbandfec=1 +a=rtpmap:9 G722/8000 +a=ssrc:1823804162 cname:pion1 +a=ssrc:1823804162 msid:pion1 audio +a=ssrc:1823804162 mslabel:pion1 +a=ssrc:1823804162 label:audio +a=msid:pion1 audio +m=video 9 UDP/TLS/RTP/SAVPF 105 115 135 +c=IN IP4 0.0.0.0 +a=mid:1 +a=rtpmap:105 VP8/90000 +a=rtpmap:115 H264/90000 +a=fmtp:115 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f +a=rtpmap:135 VP9/90000 +a=ssrc:2949882636 cname:pion2 +a=ssrc:2949882636 msid:pion2 video +a=ssrc:2949882636 mslabel:pion2 +a=ssrc:2949882636 label:video +a=msid:pion2 video +m=application 9 DTLS/SCTP 5000 +c=IN IP4 0.0.0.0 +a=mid:2 +a=sctpmap:5000 webrtc-datachannel 1024 +` + +func TestPopulateFromSDP(t *testing.T) { + m := MediaEngine{} + assertCodecWithPayloadType := func(name string, payloadType uint8) { + for _, c := range m.GetCodecsByName(name) { + if c.PayloadType == payloadType && c.Name == name { + return + } + } + t.Fatalf("Failed to find codec(%s) with PayloadType(%d)", name, payloadType) + } + + m.RegisterDefaultCodecs() + assert.NoError(t, m.PopulateFromSDP(webrtc.SessionDescription{SDP: sdpValue})) + + assertCodecWithPayloadType(webrtc.Opus, 111) + assertCodecWithPayloadType(webrtc.VP8, 105) + assertCodecWithPayloadType(webrtc.H264, 115) + assertCodecWithPayloadType(webrtc.VP9, 135) +} diff --git a/pkg/sfu/helpers_test.go b/pkg/sfu/helpers_test.go new file mode 100644 index 00000000..010152c5 --- /dev/null +++ b/pkg/sfu/helpers_test.go @@ -0,0 +1,129 @@ +package sfu + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestVP8Helper_Unmarshal(t *testing.T) { + type args struct { + payload []byte + } + tests := []struct { + name string + args args + wantErr bool + checkTemporal bool + temporalSupport bool + checkKeyFrame bool + keyFrame bool + checkPictureID bool + pictureID uint16 + checkTlzIdx bool + tlzIdx uint8 + checkTempID bool + temporalID uint8 + }{ + { + name: "Empty or nil payload must return error", + args: args{payload: []byte{}}, + wantErr: true, + }, + { + name: "Small payloads must return errors", + args: args{payload: []byte{0x0, 0x1, 0x2}}, + wantErr: true, + }, + { + name: "Temporal must be supported by setting T bit to 1", + args: args{payload: []byte{0xff, 0x20, 0x1, 0x2, 0x3, 0x4}}, + checkTemporal: true, + temporalSupport: true, + }, + { + name: "Picture must be ID 7 bits by setting M bit to 0 and present by I bit set to 1", + args: args{payload: []byte{0xff, 0xff, 0x11, 0x2, 0x3, 0x4}}, + checkPictureID: true, + pictureID: 17, + }, + { + name: "Picture ID must be 15 bits by setting M bit to 1 and present by I bit set to 1", + args: args{payload: []byte{0xff, 0xff, 0x92, 0x67, 0x3, 0x4, 0x5}}, + checkPictureID: true, + pictureID: 4711, + }, + { + name: "Temporal level zero index must be present if L set to 1", + args: args{payload: []byte{0xff, 0xff, 0xff, 0xfd, 0xb4, 0x4, 0x5}}, + checkTlzIdx: true, + tlzIdx: 180, + }, + { + name: "Temporal index must be present and used if T bit set to 1", + args: args{payload: []byte{0xff, 0xff, 0xff, 0xfd, 0xb4, 0x9f, 0x5, 0x6}}, + checkTempID: true, + temporalID: 2, + }, + { + name: "Check if packet is a keyframe by looking at P bit set to 0", + args: args{payload: []byte{0xff, 0xff, 0xff, 0xfd, 0xb4, 0x9f, 0x94, 0x1}}, + checkKeyFrame: true, + keyFrame: true, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + p := &VP8Helper{} + if err := p.Unmarshal(tt.args.payload); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.checkTemporal { + assert.Equal(t, tt.temporalSupport, p.TemporalSupported) + } + if tt.checkKeyFrame { + assert.Equal(t, tt.keyFrame, p.IsKeyFrame) + } + if tt.checkPictureID { + assert.Equal(t, tt.pictureID, p.PictureID) + } + if tt.checkTlzIdx { + assert.Equal(t, tt.tlzIdx, p.TL0PICIDX) + } + if tt.checkTempID { + assert.Equal(t, tt.temporalID, p.TID) + } + }) + } +} + +func Test_timeToNtp(t *testing.T) { + type args struct { + ns int64 + } + tests := []struct { + name string + args args + wantNTP uint64 + }{ + { + name: "Must return correct NTP time", + args: args{ + ns: time.Unix(1602391458, 1234).UnixNano(), + }, + wantNTP: 16369753560730047667, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + gotNTP := timeToNtp(tt.args.ns) + if gotNTP != tt.wantNTP { + t.Errorf("timeToNtp() gotFraction = %v, want %v", gotNTP, tt.wantNTP) + } + println(gotNTP >> 16) + }) + } +} diff --git a/pkg/sfu/packetqueue_test.go b/pkg/sfu/packetqueue_test.go new file mode 100644 index 00000000..43cc92b7 --- /dev/null +++ b/pkg/sfu/packetqueue_test.go @@ -0,0 +1,147 @@ +package sfu + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/pion/rtcp" + "github.com/pion/rtp" +) + +var TestPackets = []*rtp.Packet{ + { + Header: rtp.Header{ + SequenceNumber: 1, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 3, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 4, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 6, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 7, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 10, + }, + }, +} + +func Test_queue_nack(t *testing.T) { + type fields struct { + headSN uint16 + } + tests := []struct { + name string + fields fields + want *rtcp.NackPair + }{ + { + name: "Most generate correct nack packet", + fields: fields{}, + want: &rtcp.NackPair{ + PacketID: 2, + LostPackets: 100, + }, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + q := &queue{ + headSN: tt.fields.headSN, + } + for _, p := range TestPackets { + diff := p.SequenceNumber - q.headSN + for i := 1; i < int(diff); i++ { + q.push(nil) + q.counter++ + } + q.headSN = p.SequenceNumber + q.counter++ + q.push(p) + } + if got := q.nack(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("nack() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_queue(t *testing.T) { + q := &queue{} + for _, p := range TestPackets { + p := p + assert.NotPanics(t, func() { + q.AddPacket(p, true) + }) + } + var expectedSN uint16 + expectedSN = 6 + assert.Equal(t, expectedSN, q.GetPacket(6).SequenceNumber) + + np := &rtp.Packet{ + Header: rtp.Header{ + SequenceNumber: 8, + }, + } + expectedSN = 8 + q.AddPacket(np, false) + assert.Equal(t, expectedSN, q.GetPacket(8).SequenceNumber) +} + +func Test_queue_edges(t *testing.T) { + var TestPackets = []*rtp.Packet{ + { + Header: rtp.Header{ + SequenceNumber: 65533, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 65534, + }, + }, + { + Header: rtp.Header{ + SequenceNumber: 2, + }, + }, + } + q := &queue{} + q.headSN = 65532 + for _, p := range TestPackets { + p := p + assert.NotPanics(t, func() { + q.AddPacket(p, true) + }) + } + assert.Equal(t, 6, q.size) + var expectedSN uint16 + expectedSN = 65534 + assert.Equal(t, expectedSN, q.GetPacket(expectedSN).SequenceNumber) + + np := &rtp.Packet{ + Header: rtp.Header{ + SequenceNumber: 65535, + }, + } + q.AddPacket(np, false) + assert.Equal(t, expectedSN+1, q.GetPacket(expectedSN+1).SequenceNumber) +}