github.com/pion/webrtc/v4@v4.0.1/pkg/media/oggwriter/oggwriter_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package oggwriter 5 6 import ( 7 "bytes" 8 "io" 9 "testing" 10 11 "github.com/pion/rtp" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type oggWriterPacketTest struct { 16 buffer io.Writer 17 message string 18 messageClose string 19 packet *rtp.Packet 20 writer *OggWriter 21 err error 22 closeErr error 23 } 24 25 func TestOggWriter_AddPacketAndClose(t *testing.T) { 26 rawPkt := []byte{ 27 0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64, 28 0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x9e, 29 } 30 31 validPacket := &rtp.Packet{ 32 Header: rtp.Header{ 33 Marker: true, 34 Extension: true, 35 ExtensionProfile: 1, 36 Version: 2, 37 PayloadType: 111, 38 SequenceNumber: 27023, 39 Timestamp: 3653407706, 40 SSRC: 476325762, 41 CSRC: []uint32{}, 42 }, 43 Payload: rawPkt[20:], 44 } 45 assert.NoError(t, validPacket.SetExtension(0, []byte{0xFF, 0xFF, 0xFF, 0xFF})) 46 47 assert := assert.New(t) 48 49 // The linter misbehave and thinks this code is the same as the tests in ivf-writer_test 50 // nolint:dupl 51 addPacketTestCase := []oggWriterPacketTest{ 52 { 53 buffer: &bytes.Buffer{}, 54 message: "OggWriter shouldn't be able to write something to a closed file", 55 messageClose: "OggWriter should be able to close an already closed file", 56 packet: validPacket, 57 err: errFileNotOpened, 58 closeErr: nil, 59 }, 60 { 61 buffer: &bytes.Buffer{}, 62 message: "OggWriter shouldn't be able to write a nil packet", 63 messageClose: "OggWriter should be able to close the file", 64 packet: nil, 65 err: errInvalidNilPacket, 66 closeErr: nil, 67 }, 68 { 69 buffer: &bytes.Buffer{}, 70 message: "OggWriter should be able to write an Opus packet", 71 messageClose: "OggWriter should be able to close the file", 72 packet: validPacket, 73 err: nil, 74 closeErr: nil, 75 }, 76 { 77 buffer: nil, 78 message: "OggWriter shouldn't be able to write something to a closed file", 79 messageClose: "OggWriter should be able to close an already closed file", 80 packet: nil, 81 err: errFileNotOpened, 82 closeErr: nil, 83 }, 84 } 85 86 // First test case has a 'nil' file descriptor 87 writer, err := NewWith(addPacketTestCase[0].buffer, 48000, 2) 88 assert.Nil(err, "OggWriter should be created") 89 assert.NotNil(writer, "Writer shouldn't be nil") 90 err = writer.Close() 91 assert.Nil(err, "OggWriter should be able to close the file descriptor") 92 writer.stream = nil 93 addPacketTestCase[0].writer = writer 94 95 // Second test writes tries to write an empty packet 96 writer, err = NewWith(addPacketTestCase[1].buffer, 48000, 2) 97 assert.Nil(err, "OggWriter should be created") 98 assert.NotNil(writer, "Writer shouldn't be nil") 99 addPacketTestCase[1].writer = writer 100 101 // Third test writes tries to write a valid Opus packet 102 writer, err = NewWith(addPacketTestCase[2].buffer, 48000, 2) 103 assert.Nil(err, "OggWriter should be created") 104 assert.NotNil(writer, "Writer shouldn't be nil") 105 addPacketTestCase[2].writer = writer 106 107 // Fourth test tries to write to a nil stream 108 writer, err = NewWith(addPacketTestCase[3].buffer, 4800, 2) 109 assert.NotNil(err, "IVFWriter shouldn't be created") 110 assert.Nil(writer, "Writer should be nil") 111 addPacketTestCase[3].writer = writer 112 113 for _, t := range addPacketTestCase { 114 if t.writer != nil { 115 res := t.writer.WriteRTP(t.packet) 116 assert.Equal(t.err, res, t.message) 117 } 118 } 119 120 for _, t := range addPacketTestCase { 121 if t.writer != nil { 122 res := t.writer.Close() 123 assert.Equal(t.closeErr, res, t.messageClose) 124 } 125 } 126 } 127 128 func TestOggWriter_EmptyPayload(t *testing.T) { 129 buffer := &bytes.Buffer{} 130 131 writer, err := NewWith(buffer, 48000, 2) 132 assert.NoError(t, err) 133 134 assert.NoError(t, writer.WriteRTP(&rtp.Packet{Payload: []byte{}})) 135 } 136 137 func TestOggWriter_LargePayload(t *testing.T) { 138 rawPkt := bytes.Repeat([]byte{0x45}, 1000) 139 140 validPacket := &rtp.Packet{ 141 Header: rtp.Header{ 142 Marker: true, 143 Extension: true, 144 ExtensionProfile: 1, 145 Version: 2, 146 PayloadType: 111, 147 SequenceNumber: 27023, 148 Timestamp: 3653407706, 149 SSRC: 476325762, 150 CSRC: []uint32{}, 151 }, 152 Payload: rawPkt, 153 } 154 assert.NoError(t, validPacket.SetExtension(0, []byte{0xFF, 0xFF, 0xFF, 0xFF})) 155 156 writer, err := NewWith(&bytes.Buffer{}, 48000, 2) 157 assert.NoError(t, err, "OggWriter should be created") 158 assert.NotNil(t, writer, "Writer shouldn't be nil") 159 160 err = writer.WriteRTP(validPacket) 161 assert.NoError(t, err) 162 163 data := writer.createPage(rawPkt, pageHeaderTypeContinuationOfStream, 0, 1) 164 assert.Equal(t, uint8(4), data[26]) 165 }