File: h264writer_test.go

package info (click to toggle)
golang-github-pion-webrtc.v3 3.1.56-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,428 kB
  • sloc: javascript: 595; sh: 28; makefile: 5
file content (152 lines) | stat: -rw-r--r-- 3,176 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package h264writer

import (
	"bytes"
	"errors"
	"testing"

	"github.com/pion/rtp"
	"github.com/stretchr/testify/assert"
)

type writerCloser struct {
	bytes.Buffer
}

var errClose = errors.New("close error")

func (w *writerCloser) Close() error {
	return errClose
}

func TestNewWith(t *testing.T) {
	writer := &writerCloser{}
	h264Writer := NewWith(writer)
	assert.NotNil(t, h264Writer.Close())
}

func TestIsKeyFrame(t *testing.T) {
	tests := []struct {
		name    string
		payload []byte
		want    bool
	}{
		{
			"When given a non-keyframe; it should return false",
			[]byte{0x27, 0x90, 0x90},
			false,
		},
		{
			"When given a SPS packetized with STAP-A; it should return true",
			[]byte{0x38, 0x00, 0x03, 0x27, 0x90, 0x90, 0x00, 0x05, 0x28, 0x90, 0x90, 0x90, 0x90},
			true,
		},
		{
			"When given a SPS with no packetization; it should return true",
			[]byte{0x27, 0x90, 0x90, 0x00},
			true,
		},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			got := isKeyFrame(tt.payload)
			assert.Equal(t, tt.want, got)
		})
	}
}

func TestWriteRTP(t *testing.T) {
	tests := []struct {
		name        string
		payload     []byte
		hasKeyFrame bool
		wantBytes   []byte
		wantErr     error
		reuseWriter bool
	}{
		{
			"When given an empty payload; it should return nil",
			[]byte{},
			false,
			[]byte{},
			nil,
			false,
		},
		{
			"When no keyframe is defined; it should discard the packet",
			[]byte{0x25, 0x90, 0x90},
			false,
			[]byte{},
			nil,
			false,
		},
		{
			"When a valid Single NAL Unit packet is given; it should unpack it without error",
			[]byte{0x27, 0x90, 0x90},
			true,
			[]byte{0x00, 0x00, 0x00, 0x01, 0x27, 0x90, 0x90},
			nil,
			false,
		},
		{
			"When a valid STAP-A packet is given; it should unpack it without error",
			[]byte{0x38, 0x00, 0x03, 0x27, 0x90, 0x90, 0x00, 0x05, 0x28, 0x90, 0x90, 0x90, 0x90},
			true,
			[]byte{0x00, 0x00, 0x00, 0x01, 0x27, 0x90, 0x90, 0x00, 0x00, 0x00, 0x01, 0x28, 0x90, 0x90, 0x90, 0x90},
			nil,
			false,
		},
		{
			"When a valid FU-A start packet is given; it should unpack it without error",
			[]byte{0x3C, 0x85, 0x90, 0x90, 0x90},
			true,
			[]byte{},
			nil,
			true,
		},
		{
			"When a valid FU-A end packet is given; it should unpack it without error",
			[]byte{0x3C, 0x45, 0x90, 0x90, 0x90},
			true,
			[]byte{0x00, 0x00, 0x00, 0x01, 0x25, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90},
			nil,
			false,
		},
	}

	var reuseWriter *bytes.Buffer
	var reuseH264Writer *H264Writer

	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			writer := &bytes.Buffer{}
			h264Writer := &H264Writer{
				hasKeyFrame: tt.hasKeyFrame,
				writer:      writer,
			}
			if reuseWriter != nil {
				writer = reuseWriter
			}
			if reuseH264Writer != nil {
				h264Writer = reuseH264Writer
			}

			assert.Equal(t, tt.wantErr, h264Writer.WriteRTP(&rtp.Packet{
				Payload: tt.payload,
			}))
			assert.True(t, bytes.Equal(tt.wantBytes, writer.Bytes()))

			if !tt.reuseWriter {
				assert.Nil(t, h264Writer.Close())
				reuseWriter = nil
				reuseH264Writer = nil
			} else {
				reuseWriter = writer
				reuseH264Writer = h264Writer
			}
		})
	}
}