Skip to content

Commit 2b2f8fc

Browse files
committed
speed up routing of udp frames
1 parent d0c2c3a commit 2b2f8fc

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

main.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,19 @@ type programEventTerminate struct{}
163163
func (programEventTerminate) isProgramEvent() {}
164164

165165
type program struct {
166-
conf *conf
167-
logFile *os.File
168-
metrics *metrics
169-
serverRtsp *serverTcp
170-
serverRtp *serverUdp
171-
serverRtcp *serverUdp
172-
sources []*source
173-
clients map[*client]struct{}
174-
paths map[string]*path
175-
cmds []*exec.Cmd
176-
publisherCount int
177-
readerCount int
166+
conf *conf
167+
logFile *os.File
168+
metrics *metrics
169+
serverRtsp *serverTcp
170+
serverRtp *serverUdp
171+
serverRtcp *serverUdp
172+
sources []*source
173+
clients map[*client]struct{}
174+
udpClientPublishers map[ipKey]*client
175+
paths map[string]*path
176+
cmds []*exec.Cmd
177+
publisherCount int
178+
readerCount int
178179

179180
events chan programEvent
180181
done chan struct{}
@@ -200,11 +201,12 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
200201
}
201202

202203
p := &program{
203-
conf: conf,
204-
clients: make(map[*client]struct{}),
205-
paths: make(map[string]*path),
206-
events: make(chan programEvent),
207-
done: make(chan struct{}),
204+
conf: conf,
205+
clients: make(map[*client]struct{}),
206+
udpClientPublishers: make(map[ipKey]*client),
207+
paths: make(map[string]*path),
208+
events: make(chan programEvent),
209+
done: make(chan struct{}),
208210
}
209211

210212
if _, ok := p.conf.logDestinationsParsed[logDestinationFile]; ok {
@@ -419,17 +421,19 @@ outer:
419421
case programEventClientRecord:
420422
p.publisherCount += 1
421423
evt.client.state = clientStateRecord
424+
p.udpClientPublishers[makeIpKey(evt.client.ip())] = evt.client
422425
p.paths[evt.client.pathId].publisherSetReady()
423426
close(evt.done)
424427

425428
case programEventClientRecordStop:
426429
p.publisherCount -= 1
427430
evt.client.state = clientStatePreRecord
431+
delete(p.udpClientPublishers, makeIpKey(evt.client.ip()))
428432
p.paths[evt.client.pathId].publisherSetNotReady()
429433
close(evt.done)
430434

431435
case programEventClientFrameUdp:
432-
client, trackId := p.findClientPublisher(evt.addr, evt.streamType)
436+
client, trackId := p.findUdpClientPublisher(evt.addr, evt.streamType)
433437
if client == nil {
434438
continue
435439
}
@@ -544,31 +548,22 @@ func (p *program) findConfForPath(path string) *confPath {
544548
return nil
545549
}
546550

547-
func (p *program) findClientPublisher(addr *net.UDPAddr, streamType gortsplib.StreamType) (*client, int) {
548-
for _, path := range p.paths {
549-
cl, ok := path.publisher.(*client)
550-
if !ok {
551-
continue
552-
}
553-
554-
if cl.streamProtocol != gortsplib.StreamProtocolUdp ||
555-
cl.state != clientStateRecord ||
556-
!cl.ip().Equal(addr.IP) {
557-
continue
558-
}
559-
560-
for i, t := range cl.streamTracks {
551+
func (p *program) findUdpClientPublisher(addr *net.UDPAddr, streamType gortsplib.StreamType) (*client, int) {
552+
c, ok := p.udpClientPublishers[makeIpKey(addr.IP)]
553+
if ok {
554+
for i, t := range c.streamTracks {
561555
if streamType == gortsplib.StreamTypeRtp {
562556
if t.rtpPort == addr.Port {
563-
return cl, i
557+
return c, i
564558
}
565559
} else {
566560
if t.rtcpPort == addr.Port {
567-
return cl, i
561+
return c, i
568562
}
569563
}
570564
}
571565
}
566+
572567
return nil, -1
573568
}
574569

utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,17 @@ func splitPath(path string) (string, string, error) {
147147

148148
return comps[0], comps[1], nil
149149
}
150+
151+
// use a fixed-size array for ip comparison
152+
type ipKey [net.IPv6len]byte
153+
154+
func makeIpKey(ip net.IP) ipKey {
155+
var ret ipKey
156+
if len(ip) == net.IPv4len {
157+
copy(ret[0:], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}) // v4InV6Prefix
158+
copy(ret[12:], ip)
159+
} else {
160+
copy(ret[:], ip)
161+
}
162+
return ret
163+
}

0 commit comments

Comments
 (0)