github.com/pion/webrtc/v3@v3.2.24/pkg/media/rtpdump/writer_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package rtpdump 5 6 import ( 7 "bytes" 8 "errors" 9 "io" 10 "net" 11 "reflect" 12 "testing" 13 "time" 14 ) 15 16 func TestWriter(t *testing.T) { 17 buf := bytes.NewBuffer(nil) 18 19 writer, err := NewWriter(buf, Header{ 20 Start: time.Unix(9, 0), 21 Source: net.IPv4(2, 2, 2, 2), 22 Port: 2222, 23 }) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 if err := writer.WritePacket(Packet{ 29 Offset: time.Millisecond, 30 IsRTCP: false, 31 Payload: []byte{9}, 32 }); err != nil { 33 t.Fatal(err) 34 } 35 36 expected := append( 37 []byte("#!rtpplay1.0 2.2.2.2/2222\n"), 38 // header 39 0x00, 0x00, 0x00, 0x09, 40 0x00, 0x00, 0x00, 0x00, 41 0x02, 0x02, 0x02, 0x02, 42 0x08, 0xae, 0x00, 0x00, 43 // packet header 44 0x00, 0x09, 0x00, 0x01, 45 0x00, 0x00, 0x00, 0x01, 46 0x09, 47 ) 48 49 if got, want := buf.Bytes(), expected; !reflect.DeepEqual(got, want) { 50 t.Fatalf("wrote %v, want %v", got, want) 51 } 52 } 53 54 func TestRoundTrip(t *testing.T) { 55 buf := bytes.NewBuffer(nil) 56 57 packets := []Packet{ 58 { 59 Offset: time.Millisecond, 60 IsRTCP: false, 61 Payload: []byte{9}, 62 }, 63 { 64 Offset: 999 * time.Millisecond, 65 IsRTCP: true, 66 Payload: []byte{9}, 67 }, 68 } 69 hdr := Header{ 70 Start: time.Unix(9, 0).UTC(), 71 Source: net.IPv4(2, 2, 2, 2), 72 Port: 2222, 73 } 74 75 writer, err := NewWriter(buf, hdr) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 for _, pkt := range packets { 81 if err = writer.WritePacket(pkt); err != nil { 82 t.Fatal(err) 83 } 84 } 85 86 reader, hdr2, err := NewReader(buf) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 if got, want := hdr2, hdr; !reflect.DeepEqual(got, want) { 92 t.Fatalf("round trip: header=%v, want %v", got, want) 93 } 94 95 var packets2 []Packet 96 for { 97 pkt, err := reader.Next() 98 if errors.Is(err, io.EOF) { 99 break 100 } 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 packets2 = append(packets2, pkt) 106 } 107 108 if got, want := packets2, packets; !reflect.DeepEqual(got, want) { 109 t.Fatalf("round trip: packets=%v, want %v", got, want) 110 } 111 }