use RWMutex instead of Mutex

This commit is contained in:
aler9 2019-12-28 23:53:09 +01:00
parent b02a15b8df
commit 076f5977bd
2 changed files with 7 additions and 7 deletions

10
main.go
View File

@ -16,7 +16,7 @@ type program struct {
rtspPort int
rtpPort int
rtcpPort int
mutex sync.Mutex
mutex sync.RWMutex
rtspl *rtspListener
rtpl *udpListener
rtcpl *udpListener
@ -36,8 +36,8 @@ func newProgram(rtspPort int, rtpPort int, rtcpPort int) (*program, error) {
var err error
p.rtpl, err = newUdpListener(rtpPort, "RTP", func(l *udpListener, buf []byte) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.mutex.RLock()
defer p.mutex.RUnlock()
for c := range p.clients {
if c.state == "PLAY" {
l.nconn.WriteTo(buf, &net.UDPAddr{
@ -52,8 +52,8 @@ func newProgram(rtspPort int, rtpPort int, rtcpPort int) (*program, error) {
}
p.rtcpl, err = newUdpListener(rtcpPort, "RTCP", func(l *udpListener, buf []byte) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.mutex.RLock()
defer p.mutex.RUnlock()
for c := range p.clients {
if c.state == "PLAY" {
l.nconn.WriteTo(buf, &net.UDPAddr{

View File

@ -125,8 +125,8 @@ func (c *rtspClient) run(wg sync.WaitGroup) {
}
sdp, err := func() ([]byte, error) {
c.p.mutex.Lock()
defer c.p.mutex.Unlock()
c.p.mutex.RLock()
defer c.p.mutex.RUnlock()
if len(c.p.streamSdp) == 0 {
return nil, fmt.Errorf("no one is streaming")