webrtc: start/stop ffmpeg procees if webrtc has clients

This commit is contained in:
Artem
2024-11-05 23:49:26 +01:00
parent c228921237
commit c5f77df6b0
2 changed files with 11 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"rkkvm/external/ffmpeg"
"sync"
"github.com/google/uuid"
@@ -37,6 +38,10 @@ func (r *RTC) AddPeer(p *webrtc.PeerConnection, offer webrtc.SessionDescription)
peerID := uuid.New().String()
r.m.Lock()
r.peers[peerID] = p
if len(r.peers) == 1 {
ffmpeg.GetFFmpeg().Start()
log.Info("FFmpeg process started")
}
r.m.Unlock()
p.OnConnectionStateChange(func(connState webrtc.PeerConnectionState) {
@@ -44,6 +49,10 @@ func (r *RTC) AddPeer(p *webrtc.PeerConnection, offer webrtc.SessionDescription)
r.m.Lock()
defer r.m.Unlock()
delete(r.peers, peerID)
if len(r.peers) == 0 {
ffmpeg.GetFFmpeg().Stop()
log.Info("No clients anymore, stop ffmpeg process")
}
p.Close()
peers := make([]string, 0, len(r.peers))