From 9c3d441d160fe8970b4cf75fb943be96dea55c71 Mon Sep 17 00:00:00 2001 From: Marcel Battista Date: Sun, 28 Feb 2021 23:12:03 +0000 Subject: [PATCH 1/4] removes small lags from video live streamings --- server/internal/gst/gst.go | 8 ++++++-- server/internal/webrtc/webrtc.go | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/server/internal/gst/gst.go b/server/internal/gst/gst.go index c2ed8f00..df052fa0 100644 --- a/server/internal/gst/gst.go +++ b/server/internal/gst/gst.go @@ -90,7 +90,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri if pipelineSrc != "" { pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice) } else { - pipelineStr = fmt.Sprintf(videoSrc+"vp8enc target-bitrate=%d cpu-used=-5 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=30 auto-alt-ref=true"+pipelineStr, pipelineDevice, fps, bitrate*1000) + pipelineStr = fmt.Sprintf(videoSrc+"vp8enc target-bitrate=%d cpu-used=4 end-usage=cbr threads=4 deadline=1 undershoot=95 buffer-size=%d buffer-initial-size=%d buffer-optimal-size=%d keyframe-max-dist=180 min-quantizer=3 max-quantizer=40"+pipelineStr, pipelineDevice, fps, bitrate*1000, bitrate*6, bitrate*4, bitrate*5) } case "VP9": // https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html?gi-language=c @@ -131,7 +131,11 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri return nil, err } - pipelineStr = fmt.Sprintf(videoSrc+"video/x-raw,format=I420 ! x264enc threads=4 bitrate=%d byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice, fps, bitrate) + vbvbuf := uint(1000) + if bitrate > 1000 { + vbvbuf = bitrate + } + pipelineStr = fmt.Sprintf(videoSrc+"video/x-raw,format=NV12 ! x264enc threads=4 bitrate=%d key-int-max=60 vbv-buf-capacity=%d byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"+pipelineStr, pipelineDevice, fps, bitrate, vbvbuf) case "Opus": // https://gstreamer.freedesktop.org/documentation/opus/opusenc.html // gstreamer1.0-plugins-base diff --git a/server/internal/webrtc/webrtc.go b/server/internal/webrtc/webrtc.go index d688f9ca..209dc13e 100644 --- a/server/internal/webrtc/webrtc.go +++ b/server/internal/webrtc/webrtc.go @@ -146,11 +146,13 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri Msg("connection state has changed") }) - if _, err = connection.AddTrack(manager.videoTrack); err != nil { + rtpVideo, err := connection.AddTrack(manager.videoTrack); + if err != nil { return "", manager.config.ICELite, manager.config.ICEServers, err } - if _, err = connection.AddTrack(manager.audioTrack); err != nil { + rtpAudio, err := connection.AddTrack(manager.audioTrack); + if err != nil { return "", manager.config.ICELite, manager.config.ICEServers, err } @@ -211,6 +213,25 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri return "", manager.config.ICELite, manager.config.ICEServers, err } + go func() { + rtcpBuf := make([]byte, 1500) + for { + if _, _, rtcpErr := rtpVideo.Read(rtcpBuf); rtcpErr != nil { + return + } + } + }() + + go func() { + rtcpBuf := make([]byte, 1500) + for { + if _, _, rtcpErr := rtpAudio.Read(rtcpBuf); rtcpErr != nil { + return + } + } + }() + + return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil } From 6e2b5084e73a687bf2df09c8d251a8d1d884b3fb Mon Sep 17 00:00:00 2001 From: Marcel Battista Date: Mon, 1 Mar 2021 01:07:31 +0000 Subject: [PATCH 2/4] fixes https://github.com/m1k1o/neko/issues/22 --- server/internal/webrtc/peer.go | 2 +- server/internal/webrtc/webrtc.go | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/server/internal/webrtc/peer.go b/server/internal/webrtc/peer.go index 9ca5797c..b8d11332 100644 --- a/server/internal/webrtc/peer.go +++ b/server/internal/webrtc/peer.go @@ -28,7 +28,7 @@ func (peer *Peer) WriteData(v interface{}) error { } func (peer *Peer) Destroy() error { - if peer.connection != nil && peer.connection.ConnectionState() == webrtc.PeerConnectionStateConnected { + if peer.connection != nil && peer.connection.ConnectionState() != webrtc.PeerConnectionStateClosed { if err := peer.connection.Close(); err != nil { return err } diff --git a/server/internal/webrtc/webrtc.go b/server/internal/webrtc/webrtc.go index 209dc13e..e6a862d0 100644 --- a/server/internal/webrtc/webrtc.go +++ b/server/internal/webrtc/webrtc.go @@ -169,17 +169,20 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { case webrtc.PeerConnectionStateDisconnected: + manager.logger.Info().Str("id", id).Msg("peer disconnected") + manager.sessions.Destroy(id) case webrtc.PeerConnectionStateFailed: - manager.logger.Info().Str("id", id).Msg("peer disconnected") + manager.logger.Info().Str("id", id).Msg("peer failed") + manager.sessions.Destroy(id) + case webrtc.PeerConnectionStateClosed: + manager.logger.Info().Str("id", id).Msg("peer closed") manager.sessions.Destroy(id) - break case webrtc.PeerConnectionStateConnected: manager.logger.Info().Str("id", id).Msg("peer connected") if err = session.SetConnected(true); err != nil { manager.logger.Warn().Err(err).Msg("unable to set connected on peer") manager.sessions.Destroy(id) } - break } }) From 377ecd7d060dfe91106c9fccb6a83323e1d3f7be Mon Sep 17 00:00:00 2001 From: m1k1o Date: Mon, 1 Mar 2021 14:31:16 +0100 Subject: [PATCH 3/4] fix indentation. --- server/internal/webrtc/webrtc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/internal/webrtc/webrtc.go b/server/internal/webrtc/webrtc.go index e6a862d0..7ee72a30 100644 --- a/server/internal/webrtc/webrtc.go +++ b/server/internal/webrtc/webrtc.go @@ -169,8 +169,8 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { case webrtc.PeerConnectionStateDisconnected: - manager.logger.Info().Str("id", id).Msg("peer disconnected") - manager.sessions.Destroy(id) + manager.logger.Info().Str("id", id).Msg("peer disconnected") + manager.sessions.Destroy(id) case webrtc.PeerConnectionStateFailed: manager.logger.Info().Str("id", id).Msg("peer failed") manager.sessions.Destroy(id) From 69e0979aa02b55010ed7f98fae9f64ace8966c0b Mon Sep 17 00:00:00 2001 From: m1k1o Date: Mon, 1 Mar 2021 14:31:27 +0100 Subject: [PATCH 4/4] fix log severity. --- server/internal/webrtc/webrtc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/internal/webrtc/webrtc.go b/server/internal/webrtc/webrtc.go index 7ee72a30..b1feeee6 100644 --- a/server/internal/webrtc/webrtc.go +++ b/server/internal/webrtc/webrtc.go @@ -172,7 +172,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri manager.logger.Info().Str("id", id).Msg("peer disconnected") manager.sessions.Destroy(id) case webrtc.PeerConnectionStateFailed: - manager.logger.Info().Str("id", id).Msg("peer failed") + manager.logger.Warn().Str("id", id).Msg("peer failed") manager.sessions.Destroy(id) case webrtc.PeerConnectionStateClosed: manager.logger.Info().Str("id", id).Msg("peer closed")