webrtc: start/stop ffmpeg procees if webrtc has clients
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user