Skip to content

Commit f9b6768

Browse files
committed
fix crash during startup on 32bit platforms (#86)
1 parent 5517a2b commit f9b6768

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ func newClient(p *program, nconn net.Conn) *client {
138138
func (c *client) close() {
139139
delete(c.p.clients, c)
140140

141-
atomic.AddInt64(&c.p.countClient, -1)
141+
atomic.AddInt64(c.p.countClient, -1)
142142

143143
switch c.state {
144144
case clientStatePlay:
145-
atomic.AddInt64(&c.p.countReader, -1)
145+
atomic.AddInt64(c.p.countReader, -1)
146146
c.p.readersMap.remove(c)
147147

148148
case clientStateRecord:
149-
atomic.AddInt64(&c.p.countPublisher, -1)
149+
atomic.AddInt64(c.p.countPublisher, -1)
150150

151151
if c.streamProtocol == gortsplib.StreamProtocolUDP {
152152
for _, track := range c.streamTracks {

main.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ type program struct {
3131
clients map[*client]struct{}
3232
udpPublishersMap *udpPublishersMap
3333
readersMap *readersMap
34-
countClient int64
35-
countPublisher int64
36-
countReader int64
34+
// use pointers to avoid a crash on 32bit platforms
35+
// https://github.com/golang/go/issues/9959
36+
countClient *int64
37+
countPublisher *int64
38+
countReader *int64
3739

3840
metricsGather chan metricsGatherReq
3941
clientNew chan net.Conn
@@ -80,18 +82,30 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
8082
clients: make(map[*client]struct{}),
8183
udpPublishersMap: newUdpPublisherMap(),
8284
readersMap: newReadersMap(),
83-
metricsGather: make(chan metricsGatherReq),
84-
clientNew: make(chan net.Conn),
85-
clientClose: make(chan *client),
86-
clientDescribe: make(chan clientDescribeReq),
87-
clientAnnounce: make(chan clientAnnounceReq),
88-
clientSetupPlay: make(chan clientSetupPlayReq),
89-
clientPlay: make(chan *client),
90-
clientRecord: make(chan *client),
91-
sourceReady: make(chan *source),
92-
sourceNotReady: make(chan *source),
93-
terminate: make(chan struct{}),
94-
done: make(chan struct{}),
85+
countClient: func() *int64 {
86+
v := int64(0)
87+
return &v
88+
}(),
89+
countPublisher: func() *int64 {
90+
v := int64(0)
91+
return &v
92+
}(),
93+
countReader: func() *int64 {
94+
v := int64(0)
95+
return &v
96+
}(),
97+
metricsGather: make(chan metricsGatherReq),
98+
clientNew: make(chan net.Conn),
99+
clientClose: make(chan *client),
100+
clientDescribe: make(chan clientDescribeReq),
101+
clientAnnounce: make(chan clientAnnounceReq),
102+
clientSetupPlay: make(chan clientSetupPlayReq),
103+
clientPlay: make(chan *client),
104+
clientRecord: make(chan *client),
105+
sourceReady: make(chan *source),
106+
sourceNotReady: make(chan *source),
107+
terminate: make(chan struct{}),
108+
done: make(chan struct{}),
95109
}
96110

97111
p.log("rtsp-simple-server %s", Version)
@@ -140,9 +154,9 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
140154
}
141155

142156
func (p *program) log(format string, args ...interface{}) {
143-
countClient := atomic.LoadInt64(&p.countClient)
144-
countPublisher := atomic.LoadInt64(&p.countPublisher)
145-
countReader := atomic.LoadInt64(&p.countReader)
157+
countClient := atomic.LoadInt64(p.countClient)
158+
countPublisher := atomic.LoadInt64(p.countPublisher)
159+
countReader := atomic.LoadInt64(p.countReader)
146160

147161
log.Printf(fmt.Sprintf("[%d/%d/%d] "+format, append([]interface{}{countClient,
148162
countPublisher, countReader}, args...)...))
@@ -184,15 +198,15 @@ outer:
184198

185199
case req := <-p.metricsGather:
186200
req.res <- &metricsData{
187-
countClient: p.countClient,
188-
countPublisher: p.countPublisher,
189-
countReader: p.countReader,
201+
countClient: atomic.LoadInt64(p.countClient),
202+
countPublisher: atomic.LoadInt64(p.countPublisher),
203+
countReader: atomic.LoadInt64(p.countReader),
190204
}
191205

192206
case conn := <-p.clientNew:
193207
c := newClient(p, conn)
194208
p.clients[c] = struct{}{}
195-
atomic.AddInt64(&p.countClient, 1)
209+
atomic.AddInt64(p.countClient, 1)
196210
c.log("connected")
197211

198212
case client := <-p.clientClose:
@@ -246,12 +260,12 @@ outer:
246260
req.res <- nil
247261

248262
case client := <-p.clientPlay:
249-
atomic.AddInt64(&p.countReader, 1)
263+
atomic.AddInt64(p.countReader, 1)
250264
client.state = clientStatePlay
251265
p.readersMap.add(client)
252266

253267
case client := <-p.clientRecord:
254-
atomic.AddInt64(&p.countPublisher, 1)
268+
atomic.AddInt64(p.countPublisher, 1)
255269
client.state = clientStateRecord
256270

257271
if client.streamProtocol == gortsplib.StreamProtocolUDP {

0 commit comments

Comments
 (0)