Improve play audio on RTSP backchannel
This commit is contained in:
@@ -45,6 +45,13 @@
|
|||||||
[video4linux2,v4l2 @ 0x7f7de7c58bc0] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960
|
[video4linux2,v4l2 @ 0x7f7de7c58bc0] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## TTS
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
tts: ffmpeg:#input=-re -f lavfi -i "flite=text='1 2 3 4 5 6 7 8 9 0'"#audio=pcma
|
||||||
|
```
|
||||||
|
|
||||||
## Useful links
|
## Useful links
|
||||||
|
|
||||||
- https://superuser.com/questions/564402/explanation-of-x264-tune
|
- https://superuser.com/questions/564402/explanation-of-x264-tune
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package streams
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -80,18 +82,20 @@ func (s *Stream) Play(source string) error {
|
|||||||
s.AddInternalProducer(src)
|
s.AddInternalProducer(src)
|
||||||
s.AddInternalConsumer(cons)
|
s.AddInternalConsumer(cons)
|
||||||
|
|
||||||
go func() {
|
|
||||||
_ = src.Start()
|
|
||||||
_ = dst.Stop()
|
|
||||||
s.RemoveProducer(src)
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_ = dst.Start()
|
_ = dst.Start()
|
||||||
_ = src.Stop()
|
_ = src.Stop()
|
||||||
s.RemoveInternalConsumer(cons)
|
s.RemoveInternalConsumer(cons)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_ = src.Start()
|
||||||
|
// little timeout before stop dst, so the buffer can be transferred
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
_ = dst.Stop()
|
||||||
|
s.RemoveProducer(src)
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+32
-23
@@ -74,19 +74,38 @@ func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiv
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
startVideoBuf = 32 * 1024 // 32KB
|
||||||
|
startAudioBuf = 2 * 1024 // 2KB
|
||||||
|
maxBuf = 1024 * 1024 // 1MB
|
||||||
|
rtpHdr = 12 // basic RTP header size
|
||||||
|
intHdr = 4 // interleaved header size
|
||||||
|
)
|
||||||
|
|
||||||
func (c *Conn) packetWriter(codec *core.Codec, channel, payloadType uint8) core.HandlerFunc {
|
func (c *Conn) packetWriter(codec *core.Codec, channel, payloadType uint8) core.HandlerFunc {
|
||||||
var buf []byte
|
var buf []byte
|
||||||
var n int
|
var n int
|
||||||
|
|
||||||
video := codec.IsVideo()
|
video := codec.IsVideo()
|
||||||
if video {
|
if video {
|
||||||
buf = make([]byte, 32*1024) // 32KB
|
buf = make([]byte, startVideoBuf)
|
||||||
} else {
|
} else {
|
||||||
buf = make([]byte, 2*1024) // 2KB
|
buf = make([]byte, startAudioBuf)
|
||||||
|
}
|
||||||
|
|
||||||
|
flushBuf := func() {
|
||||||
|
if err := c.conn.SetWriteDeadline(time.Now().Add(Timeout)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//log.Printf("[rtsp] channel:%2d write_size:%6d buffer_size:%6d", channel, n, len(buf))
|
||||||
|
if _, err := c.conn.Write(buf[:n]); err == nil {
|
||||||
|
c.send += n
|
||||||
|
}
|
||||||
|
n = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
handlerFunc := func(packet *rtp.Packet) {
|
handlerFunc := func(packet *rtp.Packet) {
|
||||||
if c.state == StateNone || !c.playOK {
|
if c.state == StateNone {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,16 +125,13 @@ func (c *Conn) packetWriter(codec *core.Codec, channel, payloadType uint8) core.
|
|||||||
packet.Marker = true // better to have marker on all audio packets
|
packet.Marker = true // better to have marker on all audio packets
|
||||||
}
|
}
|
||||||
|
|
||||||
size := 12 + len(packet.Payload)
|
size := rtpHdr + len(packet.Payload)
|
||||||
|
|
||||||
if n+4+size > len(buf) {
|
if l := len(buf); n+intHdr+size > l {
|
||||||
if len(buf) < 1024*1024 {
|
if l < maxBuf {
|
||||||
buf = append(buf, make([]byte, len(buf))...)
|
buf = append(buf, make([]byte, l)...) // double buffer size
|
||||||
} else {
|
} else {
|
||||||
if _, err := c.conn.Write(buf[:n]); err == nil {
|
flushBuf()
|
||||||
c.send += n
|
|
||||||
}
|
|
||||||
n = 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,21 +150,14 @@ func (c *Conn) packetWriter(codec *core.Codec, channel, payloadType uint8) core.
|
|||||||
|
|
||||||
n += 4 + size
|
n += 4 + size
|
||||||
|
|
||||||
if !packet.Marker {
|
if !packet.Marker || !c.playOK {
|
||||||
return // collect continious video packets to buffer
|
// collect continious video packets to buffer
|
||||||
}
|
// or wait OK for PLAY command for backchannel
|
||||||
|
//log.Printf("[rtsp] collecting buffer ok=%t", c.playOK)
|
||||||
if err := c.conn.SetWriteDeadline(time.Now().Add(Timeout)); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.Printf("[rtsp] channel:%2d write_size:%6d buffer_size:%6d", channel, n, len(buf))
|
flushBuf()
|
||||||
|
|
||||||
if _, err := c.conn.Write(buf[:n]); err == nil {
|
|
||||||
c.send += n
|
|
||||||
}
|
|
||||||
|
|
||||||
n = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !codec.IsRTP() {
|
if !codec.IsRTP() {
|
||||||
|
|||||||
Reference in New Issue
Block a user