Fix race on pcm pack backchannel #432

This commit is contained in:
Alexey Khit
2023-06-28 20:25:40 +03:00
parent 39811d121b
commit 306451f94f
+11 -2
View File
@@ -3,15 +3,22 @@ package pcm
import ( import (
"github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtp" "github.com/pion/rtp"
"sync"
) )
func RepackBackchannel(handler core.HandlerFunc) core.HandlerFunc { func RepackBackchannel(handler core.HandlerFunc) core.HandlerFunc {
var buf []byte var buf []byte
var seq uint16 var seq uint16
// fix https://github.com/AlexxIT/go2rtc/issues/432
var mu sync.Mutex
return func(packet *rtp.Packet) { return func(packet *rtp.Packet) {
mu.Lock()
buf = append(buf, packet.Payload...) buf = append(buf, packet.Payload...)
if len(buf) < 1024 { if len(buf) < 1024 {
mu.Unlock()
return return
} }
@@ -27,9 +34,11 @@ func RepackBackchannel(handler core.HandlerFunc) core.HandlerFunc {
Payload: buf[:1024], Payload: buf[:1024],
} }
handler(pkt)
buf = buf[1024:] buf = buf[1024:]
seq++ seq++
mu.Unlock()
handler(pkt)
} }
} }