mirror of
https://github.com/sipeed/NanoKVM.git
synced 2026-09-11 00:22:56 -05:00
Route Direct and WebRTC consumers through one H264 capture source to avoid duplicate VENC reads. Overlap WebRTC capture with ordered RTP/SRTP writes and packetize each access unit once for all WebRTC clients. Add libopencv_video.so.409 into dl_lib.
114 lines
2.2 KiB
Go
114 lines
2.2 KiB
Go
package webrtc
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/pion/webrtc/v4"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"sync"
|
|
)
|
|
|
|
const h264SDPFmtpLine = "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d001f"
|
|
|
|
func NewClient(ws *websocket.Conn, videoConn *webrtc.PeerConnection) *Client {
|
|
return &Client{
|
|
ws: ws,
|
|
video: videoConn,
|
|
mutex: sync.Mutex{},
|
|
}
|
|
}
|
|
|
|
func (c *Client) Close() {
|
|
if c.video != nil {
|
|
if err := c.video.Close(); err != nil {
|
|
log.Debugf("failed to close video peer connection: %s", err)
|
|
}
|
|
}
|
|
|
|
if c.ws != nil {
|
|
if err := c.ws.Close(); err != nil {
|
|
log.Debugf("failed to close websocket: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Client) WriteMessage(event string, data string) error {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
|
|
message := &Message{
|
|
Event: event,
|
|
Data: data,
|
|
}
|
|
|
|
if err := c.ws.WriteJSON(message); err != nil {
|
|
log.Errorf("failed to send message %s: %v", event, err)
|
|
return err
|
|
}
|
|
|
|
log.Debugf("sent message %s", event)
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) ReadMessage() (*Message, error) {
|
|
_, raw, err := c.ws.ReadMessage()
|
|
if err != nil {
|
|
log.Errorf("failed to read message: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
var message Message
|
|
if err := json.Unmarshal(raw, &message); err != nil {
|
|
log.Errorf("failed to unmarshal message: %v", err)
|
|
return nil, nil
|
|
}
|
|
|
|
return &message, nil
|
|
}
|
|
|
|
func (c *Client) AddTrack() error {
|
|
// video track
|
|
videoTrack, err := webrtc.NewTrackLocalStaticRTP(
|
|
webrtc.RTPCodecCapability{
|
|
MimeType: webrtc.MimeTypeH264,
|
|
ClockRate: 90000,
|
|
SDPFmtpLine: h264SDPFmtpLine,
|
|
},
|
|
"video",
|
|
"pion-video",
|
|
)
|
|
if err != nil {
|
|
log.Errorf("failed to create video track: %s", err)
|
|
return err
|
|
}
|
|
|
|
videoSender, err := c.video.AddTrack(videoTrack)
|
|
if err != nil {
|
|
log.Errorf("failed to add video track: %s", err)
|
|
return err
|
|
}
|
|
go startRTCPReader(videoSender)
|
|
|
|
track := &Track{
|
|
video: videoTrack,
|
|
}
|
|
|
|
c.mutex.Lock()
|
|
c.track = track
|
|
c.mutex.Unlock()
|
|
|
|
return nil
|
|
}
|
|
|
|
func startRTCPReader(sender *webrtc.RTPSender) {
|
|
rtcpBuf := make([]byte, 1500)
|
|
for {
|
|
if _, _, err := sender.Read(rtcpBuf); err != nil {
|
|
log.Debugf("RTCP reader error: %v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|