Add comments and improve repackaging
This commit is contained in:
+43
-32
@@ -161,9 +161,8 @@ func Dial(rawURL string) (core.Producer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if client.isHEVC {
|
if client.isHEVC {
|
||||||
// We need to replace the SDP codecs with the real ones from Skill.
|
// Tuya responds with H264/90000 even for HEVC streams
|
||||||
// The actual media comes via DataChannel, not RTP tracks.
|
// So we need to replace video codecs with HEVC ones from API
|
||||||
|
|
||||||
for _, media := range client.conn.Medias {
|
for _, media := range client.conn.Medias {
|
||||||
if media.Kind == core.KindVideo {
|
if media.Kind == core.KindVideo {
|
||||||
codecs := client.api.GetVideoCodecs()
|
codecs := client.api.GetVideoCodecs()
|
||||||
@@ -173,6 +172,9 @@ func Dial(rawURL string) (core.Producer, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Audio codecs from API as well
|
||||||
|
// Tuya responds with multiple audio codecs (PCMU, PCMA)
|
||||||
|
// But the quality is bad if we use PCMU and skill only has PCMA
|
||||||
for _, media := range client.conn.Medias {
|
for _, media := range client.conn.Medias {
|
||||||
if media.Kind == core.KindAudio {
|
if media.Kind == core.KindAudio {
|
||||||
codecs := client.api.GetAudioCodecs()
|
codecs := client.api.GetAudioCodecs()
|
||||||
@@ -356,41 +358,50 @@ func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Rece
|
|||||||
|
|
||||||
sender := core.NewSender(media, codec)
|
sender := core.NewSender(media, codec)
|
||||||
|
|
||||||
// Frame size affects audio delay with Tuya cameras:
|
switch track.Codec.Name {
|
||||||
// Browser sends standard 20ms frames (160 bytes for G.711), but this causes
|
case core.CodecPCMA, core.CodecPCMU, core.CodecPCM, core.CodecPCML:
|
||||||
// up to 4s delay on some Tuya cameras. Increasing to 240 bytes (30ms) reduces
|
// Frame size affects audio delay with Tuya cameras:
|
||||||
// delay to ~2s. Higher values (320+ bytes) don't work and cause issues.
|
// Browser sends standard 20ms frames (160 bytes for G.711), but this causes
|
||||||
// Using 240 bytes (30ms) as optimal balance between latency and stability.
|
// up to 4s delay on some Tuya cameras. Increasing to 240 bytes (30ms) reduces
|
||||||
frameSize := 240
|
// delay to ~2s. Higher values (320+ bytes) don't work and cause issues.
|
||||||
|
// Using 240 bytes (30ms) as optimal balance between latency and stability.
|
||||||
|
frameSize := 240
|
||||||
|
|
||||||
var buf []byte
|
var buf []byte
|
||||||
var seq uint16
|
var seq uint16
|
||||||
var ts uint32
|
var ts uint32
|
||||||
|
|
||||||
sender.Handler = func(packet *rtp.Packet) {
|
sender.Handler = func(packet *rtp.Packet) {
|
||||||
buf = append(buf, packet.Payload...)
|
buf = append(buf, packet.Payload...)
|
||||||
|
|
||||||
for len(buf) >= frameSize {
|
for len(buf) >= frameSize {
|
||||||
payload := buf[:frameSize]
|
payload := buf[:frameSize]
|
||||||
|
|
||||||
pkt := &rtp.Packet{
|
pkt := &rtp.Packet{
|
||||||
Header: rtp.Header{
|
Header: rtp.Header{
|
||||||
Version: 2,
|
Version: 2,
|
||||||
Marker: true,
|
Marker: true,
|
||||||
PayloadType: payloadType,
|
PayloadType: payloadType,
|
||||||
SequenceNumber: seq,
|
SequenceNumber: seq,
|
||||||
Timestamp: ts,
|
Timestamp: ts,
|
||||||
SSRC: packet.SSRC,
|
SSRC: packet.SSRC,
|
||||||
},
|
},
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
|
}
|
||||||
|
|
||||||
|
seq++
|
||||||
|
ts += uint32(frameSize)
|
||||||
|
buf = buf[frameSize:]
|
||||||
|
|
||||||
|
c.conn.Send += pkt.MarshalSize()
|
||||||
|
_ = localTrack.WriteRTP(payloadType, pkt)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
seq++
|
default:
|
||||||
ts += uint32(frameSize)
|
sender.Handler = func(packet *rtp.Packet) {
|
||||||
buf = buf[frameSize:]
|
c.conn.Send += packet.MarshalSize()
|
||||||
|
_ = localTrack.WriteRTP(payloadType, packet)
|
||||||
c.conn.Send += pkt.MarshalSize()
|
|
||||||
_ = localTrack.WriteRTP(payloadType, pkt)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user