Skip to content

Commit 9a6a813

Browse files
committed
fix panic when a client disconnects
1 parent 58e6f6d commit 9a6a813

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

main.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,12 @@ func newProgram(rtspPort int, rtpPort int, rtcpPort int) (*program, error) {
5555
}
5656

5757
func (p *program) run() {
58-
var wg sync.WaitGroup
58+
go p.rtpl.run()
59+
go p.rtcpl.run()
60+
go p.rtspl.run()
5961

60-
wg.Add(1)
61-
go p.rtpl.run(wg)
62-
63-
wg.Add(1)
64-
go p.rtcpl.run(wg)
65-
66-
wg.Add(1)
67-
go p.rtspl.run(wg)
68-
69-
wg.Wait()
62+
infty := make(chan struct{})
63+
<-infty
7064
}
7165

7266
func (p *program) handleRtp(buf []byte) {

rtsp_client.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/url"
1111
"strconv"
1212
"strings"
13-
"sync"
1413

1514
"rtsp-server/rtsp"
1615
)
@@ -40,29 +39,34 @@ func newRtspClient(p *program, nconn net.Conn) *rtspClient {
4039
}
4140

4241
func (c *rtspClient) close() error {
42+
// already deleted
43+
if _, ok := c.p.clients[c]; !ok {
44+
return nil
45+
}
46+
4347
delete(c.p.clients, c)
48+
c.nconn.Close()
4449

4550
if c.p.streamAuthor == c {
4651
c.p.streamAuthor = nil
4752
c.p.streamSdp = nil
4853

49-
// if the streamer has disconnected
54+
// if the publisher has disconnected
5055
// close all other connections
5156
for oc := range c.p.clients {
5257
oc.close()
5358
}
5459
}
5560

56-
return c.nconn.Close()
61+
return nil
5762
}
5863

5964
func (c *rtspClient) log(format string, args ...interface{}) {
6065
format = "[RTSP client " + c.nconn.RemoteAddr().String() + "] " + format
6166
log.Printf(format, args...)
6267
}
6368

64-
func (c *rtspClient) run(wg sync.WaitGroup) {
65-
defer wg.Done()
69+
func (c *rtspClient) run() {
6670
defer c.log("disconnected")
6771
defer func() {
6872
c.p.mutex.Lock()
@@ -278,7 +282,7 @@ func (c *rtspClient) run(wg sync.WaitGroup) {
278282
fmt.Sprintf("client_port=%d-%d", clientPort1, clientPort2),
279283
// use two fake server ports, since we do not want to receive feedback
280284
// from the client
281-
fmt.Sprintf("server_port=%d-%d", c.p.rtpPort + 2, c.p.rtcpPort + 2),
285+
fmt.Sprintf("server_port=%d-%d", c.p.rtpPort+2, c.p.rtcpPort+2),
282286
"ssrc=1234ABCD",
283287
}, ";"),
284288
"Session": "12345678",

rtsp_listener.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"log"
55
"net"
6-
"sync"
76
)
87

98
type rtspListener struct {
@@ -32,17 +31,14 @@ func (l *rtspListener) log(format string, args ...interface{}) {
3231
log.Printf("[RTSP listener] "+format, args...)
3332
}
3433

35-
func (l *rtspListener) run(wg sync.WaitGroup) {
36-
defer wg.Done()
37-
34+
func (l *rtspListener) run() {
3835
for {
3936
nconn, err := l.netl.AcceptTCP()
4037
if err != nil {
4138
break
4239
}
4340

4441
rsc := newRtspClient(l.p, nconn)
45-
wg.Add(1)
46-
go rsc.run(wg)
42+
go rsc.run()
4743
}
4844
}

udp_listener.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"log"
55
"net"
6-
"sync"
76
)
87

98
type udpListener struct {
@@ -34,9 +33,7 @@ func (l *udpListener) log(format string, args ...interface{}) {
3433
log.Printf("["+l.logPrefix+" listener] "+format, args...)
3534
}
3635

37-
func (l *udpListener) run(wg sync.WaitGroup) {
38-
defer wg.Done()
39-
36+
func (l *udpListener) run() {
4037
buf := make([]byte, 2048) // UDP MTU is 1400
4138

4239
for {

0 commit comments

Comments
 (0)