Refactor RepackG711 to use configurable packet size

This commit is contained in:
seydx
2025-10-27 21:48:15 +01:00
parent 721ed98afb
commit 8142d2fc43
3 changed files with 12 additions and 9 deletions
+9 -6
View File
@@ -12,8 +12,11 @@ import (
// 1. Fixes WebRTC audio quality issue (monotonic timestamp)
// 2. Fixes Reolink Doorbell backchannel issue (zero timestamp)
// https://github.com/AlexxIT/go2rtc/issues/331
func RepackG711(zeroTS bool, handler core.HandlerFunc) core.HandlerFunc {
const PacketSize = 1024
func RepackG711(zeroTS bool, size int, handler core.HandlerFunc) core.HandlerFunc {
packetSize := 1024
if size > 0 {
packetSize = size
}
var buf []byte
var seq uint16
@@ -26,7 +29,7 @@ func RepackG711(zeroTS bool, handler core.HandlerFunc) core.HandlerFunc {
mu.Lock()
buf = append(buf, packet.Payload...)
if len(buf) < PacketSize {
if len(buf) < packetSize {
mu.Unlock()
return
}
@@ -39,7 +42,7 @@ func RepackG711(zeroTS bool, handler core.HandlerFunc) core.HandlerFunc {
SequenceNumber: seq,
SSRC: packet.SSRC,
},
Payload: buf[:PacketSize],
Payload: buf[:packetSize],
}
seq++
@@ -48,10 +51,10 @@ func RepackG711(zeroTS bool, handler core.HandlerFunc) core.HandlerFunc {
// don't have this strange devices for tests
if !zeroTS {
pkt.Timestamp = ts
ts += PacketSize
ts += uint32(packetSize)
}
buf = buf[PacketSize:]
buf = buf[packetSize:]
mu.Unlock()
+1 -1
View File
@@ -56,7 +56,7 @@ func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiv
if c.mode == core.ModeActiveProducer && track.Codec.Name == core.CodecPCMA {
// Fix Reolink Doorbell https://github.com/AlexxIT/go2rtc/issues/331
sender.Handler = pcm.RepackG711(true, sender.Handler)
sender.Handler = pcm.RepackG711(true, 0, sender.Handler)
}
sender.HandleRTP(track)
+2 -2
View File
@@ -32,7 +32,7 @@ func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiv
panic(core.Caller())
}
localTrack := c.getSenderTrack(media.ID)
localTrack := c.GetSenderTrack(media.ID)
if localTrack == nil {
return errors.New("webrtc: can't get track")
}
@@ -66,7 +66,7 @@ func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiv
case core.CodecPCMA, core.CodecPCMU, core.CodecPCM, core.CodecPCML:
// Fix audio quality https://github.com/AlexxIT/WebRTC/issues/500
// should be before ResampleToG711, because it will be called last
sender.Handler = pcm.RepackG711(false, sender.Handler)
sender.Handler = pcm.RepackG711(false, 0, sender.Handler)
if codec.ClockRate == 0 {
if codec.Name == core.CodecPCM || codec.Name == core.CodecPCML {