mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-11-03 01:43:48 +08:00
fix crash during startup on 32bit platforms (#86)
This commit is contained in:
@@ -138,15 +138,15 @@ func newClient(p *program, nconn net.Conn) *client {
|
|||||||
func (c *client) close() {
|
func (c *client) close() {
|
||||||
delete(c.p.clients, c)
|
delete(c.p.clients, c)
|
||||||
|
|
||||||
atomic.AddInt64(&c.p.countClient, -1)
|
atomic.AddInt64(c.p.countClient, -1)
|
||||||
|
|
||||||
switch c.state {
|
switch c.state {
|
||||||
case clientStatePlay:
|
case clientStatePlay:
|
||||||
atomic.AddInt64(&c.p.countReader, -1)
|
atomic.AddInt64(c.p.countReader, -1)
|
||||||
c.p.readersMap.remove(c)
|
c.p.readersMap.remove(c)
|
||||||
|
|
||||||
case clientStateRecord:
|
case clientStateRecord:
|
||||||
atomic.AddInt64(&c.p.countPublisher, -1)
|
atomic.AddInt64(c.p.countPublisher, -1)
|
||||||
|
|
||||||
if c.streamProtocol == gortsplib.StreamProtocolUDP {
|
if c.streamProtocol == gortsplib.StreamProtocolUDP {
|
||||||
for _, track := range c.streamTracks {
|
for _, track := range c.streamTracks {
|
||||||
|
|||||||
62
main.go
62
main.go
@@ -31,9 +31,11 @@ type program struct {
|
|||||||
clients map[*client]struct{}
|
clients map[*client]struct{}
|
||||||
udpPublishersMap *udpPublishersMap
|
udpPublishersMap *udpPublishersMap
|
||||||
readersMap *readersMap
|
readersMap *readersMap
|
||||||
countClient int64
|
// use pointers to avoid a crash on 32bit platforms
|
||||||
countPublisher int64
|
// https://github.com/golang/go/issues/9959
|
||||||
countReader int64
|
countClient *int64
|
||||||
|
countPublisher *int64
|
||||||
|
countReader *int64
|
||||||
|
|
||||||
metricsGather chan metricsGatherReq
|
metricsGather chan metricsGatherReq
|
||||||
clientNew chan net.Conn
|
clientNew chan net.Conn
|
||||||
@@ -80,18 +82,30 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
|
|||||||
clients: make(map[*client]struct{}),
|
clients: make(map[*client]struct{}),
|
||||||
udpPublishersMap: newUdpPublisherMap(),
|
udpPublishersMap: newUdpPublisherMap(),
|
||||||
readersMap: newReadersMap(),
|
readersMap: newReadersMap(),
|
||||||
metricsGather: make(chan metricsGatherReq),
|
countClient: func() *int64 {
|
||||||
clientNew: make(chan net.Conn),
|
v := int64(0)
|
||||||
clientClose: make(chan *client),
|
return &v
|
||||||
clientDescribe: make(chan clientDescribeReq),
|
}(),
|
||||||
clientAnnounce: make(chan clientAnnounceReq),
|
countPublisher: func() *int64 {
|
||||||
clientSetupPlay: make(chan clientSetupPlayReq),
|
v := int64(0)
|
||||||
clientPlay: make(chan *client),
|
return &v
|
||||||
clientRecord: make(chan *client),
|
}(),
|
||||||
sourceReady: make(chan *source),
|
countReader: func() *int64 {
|
||||||
sourceNotReady: make(chan *source),
|
v := int64(0)
|
||||||
terminate: make(chan struct{}),
|
return &v
|
||||||
done: make(chan struct{}),
|
}(),
|
||||||
|
metricsGather: make(chan metricsGatherReq),
|
||||||
|
clientNew: make(chan net.Conn),
|
||||||
|
clientClose: make(chan *client),
|
||||||
|
clientDescribe: make(chan clientDescribeReq),
|
||||||
|
clientAnnounce: make(chan clientAnnounceReq),
|
||||||
|
clientSetupPlay: make(chan clientSetupPlayReq),
|
||||||
|
clientPlay: make(chan *client),
|
||||||
|
clientRecord: make(chan *client),
|
||||||
|
sourceReady: make(chan *source),
|
||||||
|
sourceNotReady: make(chan *source),
|
||||||
|
terminate: make(chan struct{}),
|
||||||
|
done: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
p.log("rtsp-simple-server %s", Version)
|
p.log("rtsp-simple-server %s", Version)
|
||||||
@@ -140,9 +154,9 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) log(format string, args ...interface{}) {
|
func (p *program) log(format string, args ...interface{}) {
|
||||||
countClient := atomic.LoadInt64(&p.countClient)
|
countClient := atomic.LoadInt64(p.countClient)
|
||||||
countPublisher := atomic.LoadInt64(&p.countPublisher)
|
countPublisher := atomic.LoadInt64(p.countPublisher)
|
||||||
countReader := atomic.LoadInt64(&p.countReader)
|
countReader := atomic.LoadInt64(p.countReader)
|
||||||
|
|
||||||
log.Printf(fmt.Sprintf("[%d/%d/%d] "+format, append([]interface{}{countClient,
|
log.Printf(fmt.Sprintf("[%d/%d/%d] "+format, append([]interface{}{countClient,
|
||||||
countPublisher, countReader}, args...)...))
|
countPublisher, countReader}, args...)...))
|
||||||
@@ -184,15 +198,15 @@ outer:
|
|||||||
|
|
||||||
case req := <-p.metricsGather:
|
case req := <-p.metricsGather:
|
||||||
req.res <- &metricsData{
|
req.res <- &metricsData{
|
||||||
countClient: p.countClient,
|
countClient: atomic.LoadInt64(p.countClient),
|
||||||
countPublisher: p.countPublisher,
|
countPublisher: atomic.LoadInt64(p.countPublisher),
|
||||||
countReader: p.countReader,
|
countReader: atomic.LoadInt64(p.countReader),
|
||||||
}
|
}
|
||||||
|
|
||||||
case conn := <-p.clientNew:
|
case conn := <-p.clientNew:
|
||||||
c := newClient(p, conn)
|
c := newClient(p, conn)
|
||||||
p.clients[c] = struct{}{}
|
p.clients[c] = struct{}{}
|
||||||
atomic.AddInt64(&p.countClient, 1)
|
atomic.AddInt64(p.countClient, 1)
|
||||||
c.log("connected")
|
c.log("connected")
|
||||||
|
|
||||||
case client := <-p.clientClose:
|
case client := <-p.clientClose:
|
||||||
@@ -246,12 +260,12 @@ outer:
|
|||||||
req.res <- nil
|
req.res <- nil
|
||||||
|
|
||||||
case client := <-p.clientPlay:
|
case client := <-p.clientPlay:
|
||||||
atomic.AddInt64(&p.countReader, 1)
|
atomic.AddInt64(p.countReader, 1)
|
||||||
client.state = clientStatePlay
|
client.state = clientStatePlay
|
||||||
p.readersMap.add(client)
|
p.readersMap.add(client)
|
||||||
|
|
||||||
case client := <-p.clientRecord:
|
case client := <-p.clientRecord:
|
||||||
atomic.AddInt64(&p.countPublisher, 1)
|
atomic.AddInt64(p.countPublisher, 1)
|
||||||
client.state = clientStateRecord
|
client.state = clientStateRecord
|
||||||
|
|
||||||
if client.streamProtocol == gortsplib.StreamProtocolUDP {
|
if client.streamProtocol == gortsplib.StreamProtocolUDP {
|
||||||
|
|||||||
Reference in New Issue
Block a user