github.com/cnotch/ipchub@v1.1.0/service/rtsp/rtptransport_test.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package rtsp 6 7 import ( 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestRTPTransport_parseTransport(t *testing.T) { 14 tests := []struct { 15 name string 16 rtpType int 17 ts string 18 wantErr bool 19 }{ 20 { 21 "test1", 22 int(ChannelVideo), 23 "RTP/AVP;multicast;client_port=18888-18889", 24 false, 25 }, 26 { 27 "test2", 28 int(ChannelAudio), 29 "RTP/AVP;multicast;destination=232.248.88.236;source=192.168.1.154;port=16666-0;ttl=255", 30 false, 31 }, 32 } 33 var ts RTPTransport 34 for _, tt := range tests { 35 if err := ts.ParseTransport(tt.rtpType, tt.ts); (err != nil) != tt.wantErr { 36 t.Errorf("RTPTransport.parseTransport() error = %v, wantErr %v", err, tt.wantErr) 37 } 38 } 39 40 assert.Equal(t, 18888, ts.ClientPorts[int(ChannelVideo)]) 41 assert.Equal(t, 18889, ts.ClientPorts[int(ChannelVideo)+1]) 42 assert.Equal(t, PlaySession, ts.Mode, "play") 43 assert.Equal(t, RTPMulticast, ts.Type, "multicast") 44 assert.Equal(t, "232.248.88.236", ts.MulticastIP) 45 assert.Equal(t, "192.168.1.154", ts.Source) 46 assert.Equal(t, 255, ts.TTL) 47 assert.Equal(t, 16666, ts.Ports[int(ChannelAudio)]) 48 assert.Equal(t, 0, ts.Ports[int(ChannelAudio)+1]) 49 50 } 51 52 func TestRTPTransport_parseTransport_error(t *testing.T) { 53 tests := []struct { 54 name string 55 rtpType int 56 ts string 57 wantErr bool 58 }{ 59 { 60 "error", 61 int(ChannelVideo), 62 "RTP/AVP/TCP;multicast;client_port=18888-18889", 63 true, 64 }, 65 } 66 var ts RTPTransport 67 for _, tt := range tests { 68 if err := ts.ParseTransport(tt.rtpType, tt.ts); (err != nil) != tt.wantErr { 69 t.Errorf("RTPTransport.parseTransport() error = %v, wantErr %v", err, tt.wantErr) 70 } 71 } 72 } 73 74 func Benchmark_ParseTransport(b *testing.B) { 75 b.ResetTimer() 76 b.RunParallel(func(pb *testing.PB) { 77 for pb.Next() { 78 var ts RTPTransport 79 ts.ParseTransport(int(ChannelVideo), "RTP/AVP;multicast;destination=232.248.88.236;source=192.168.1.154;port=16666-0;ttl=255") 80 } 81 }) 82 }