add audio support, trying to manage MSD device
This commit is contained in:
77
http/hw/rtc/listener.go
Normal file
77
http/hw/rtc/listener.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"net"
|
||||
"rkkvm/config"
|
||||
|
||||
"github.com/pion/webrtc/v4"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func initUDPListener(host string, port int) (*net.UDPConn, error) {
|
||||
l, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP(host), Port: port})
|
||||
if err != nil {
|
||||
return nil, ErrWebRTCParam("failed to init webrtc listener: %v", err)
|
||||
}
|
||||
|
||||
// Increase the UDP receive buffer size
|
||||
// Default UDP buffer sizes vary on different operating systems
|
||||
bufferSize := 300000 // 300KB
|
||||
err = l.SetReadBuffer(bufferSize)
|
||||
if err != nil {
|
||||
return nil, ErrWebRTCParam("failed to set read buffer: %v", err)
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func InitListener(host string, port int, aPort int) (*RTC, error) {
|
||||
vl, err := initUDPListener(host, port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
al, err := initUDPListener(host, aPort)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mimeType := ""
|
||||
switch config.Get().Video.Codec {
|
||||
case config.StreamSourceH264:
|
||||
mimeType = webrtc.MimeTypeH264
|
||||
case config.StreamSourceHevc: // WebRTC currently has no official support for H265
|
||||
mimeType = webrtc.MimeTypeH265
|
||||
default:
|
||||
return nil, ErrWebRTCParam("unknown video codec: %s", config.Get().Video.Codec)
|
||||
}
|
||||
|
||||
video, _ := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: mimeType}, "video", "rkkvm")
|
||||
audio, _ := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus}, "audio", "rkkvm")
|
||||
rtc = &RTC{
|
||||
videoListener: vl,
|
||||
audioListener: al,
|
||||
peers: make(map[string]*webrtc.PeerConnection),
|
||||
videoTrack: video,
|
||||
audioTrack: audio,
|
||||
}
|
||||
|
||||
return rtc, nil
|
||||
}
|
||||
|
||||
func listenerRead(l *net.UDPConn, track *webrtc.TrackLocalStaticRTP) {
|
||||
buf := make([]byte, 1600) // Buffer to hold incoming RTP packets
|
||||
|
||||
for {
|
||||
n, _, err := l.ReadFrom(buf)
|
||||
if err != nil {
|
||||
log.Errorf("error reading from UDP: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = track.Write(buf[:n])
|
||||
if err != nil {
|
||||
log.Errorf("failed to send RTP to peer: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,184 +1,139 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
// https://github.com/pion/example-webrtc-applications/blob/master/sfu-ws/main.go
|
||||
|
||||
var rtc *RTC
|
||||
|
||||
func Get() *RTC {
|
||||
return rtc
|
||||
}
|
||||
|
||||
var ErrPeerClosedConn = errors.New("webrtc: peer closed conn")
|
||||
var ErrWebRTC = errors.New("webrtc")
|
||||
var ErrWebRTCParam = func(format string, args ...any) error {
|
||||
return fmt.Errorf("%w: "+format, args...)
|
||||
}
|
||||
var ErrPeerClosedConn = ErrWebRTCParam("peer closed conn")
|
||||
|
||||
type RTC struct {
|
||||
l *net.UDPConn
|
||||
peer *webrtc.PeerConnection
|
||||
track *webrtc.TrackLocalStaticRTP
|
||||
sender *webrtc.RTPSender
|
||||
localSession string
|
||||
peers map[string]*webrtc.PeerConnection
|
||||
videoListener *net.UDPConn
|
||||
audioListener *net.UDPConn
|
||||
videoTrack *webrtc.TrackLocalStaticRTP
|
||||
audioTrack *webrtc.TrackLocalStaticRTP
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func (r *RTC) AddPeer(p *webrtc.PeerConnection, offer webrtc.SessionDescription) (*webrtc.SessionDescription, error) {
|
||||
peerID := uuid.New().String()
|
||||
r.m.Lock()
|
||||
r.peers[peerID] = p
|
||||
r.m.Unlock()
|
||||
|
||||
p.OnConnectionStateChange(func(connState webrtc.PeerConnectionState) {
|
||||
if connState == webrtc.PeerConnectionStateFailed || connState == webrtc.PeerConnectionStateClosed {
|
||||
r.m.Lock()
|
||||
defer r.m.Unlock()
|
||||
delete(r.peers, peerID)
|
||||
p.Close()
|
||||
|
||||
peers := make([]string, 0, len(r.peers))
|
||||
for p := range r.peers {
|
||||
peers = append(peers, p)
|
||||
}
|
||||
log.WithField("peers", peers).Infof("Peer %s disconnected and resources cleaned up.", peerID)
|
||||
}
|
||||
})
|
||||
|
||||
vSender, err := p.AddTrack(r.videoTrack)
|
||||
if err != nil {
|
||||
return nil, ErrWebRTCParam("failed to add video track: %v", err)
|
||||
}
|
||||
processRTCP(vSender)
|
||||
aSender, err := p.AddTrack(r.audioTrack)
|
||||
if err != nil {
|
||||
return nil, ErrWebRTCParam("failed to add audio track: %v", err)
|
||||
}
|
||||
processRTCP(aSender)
|
||||
|
||||
if err := p.SetRemoteDescription(offer); err != nil {
|
||||
return nil, ErrWebRTCParam("failed to set remote description: %v", err)
|
||||
}
|
||||
|
||||
answer, err := p.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
return nil, ErrWebRTCParam("failed to create answer: %v", err)
|
||||
}
|
||||
gatherComplete := webrtc.GatheringCompletePromise(p)
|
||||
|
||||
if err := p.SetLocalDescription(answer); err != nil {
|
||||
return nil, ErrWebRTCParam("failed to set local description: %v", err)
|
||||
}
|
||||
<-gatherComplete
|
||||
|
||||
return p.LocalDescription(), nil
|
||||
}
|
||||
|
||||
func (r *RTC) VideoListenerRead() {
|
||||
listenerRead(r.videoListener, r.videoTrack)
|
||||
}
|
||||
|
||||
func (r *RTC) AudioListenerRead() {
|
||||
listenerRead(r.audioListener, r.audioTrack)
|
||||
}
|
||||
|
||||
func (r *RTC) Close() error {
|
||||
return r.l.Close()
|
||||
r.videoListener.Close()
|
||||
r.audioListener.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read incoming RTCP packets
|
||||
// Before these packets are returned they are processed by interceptors. For things
|
||||
// like NACK this needs to be called.
|
||||
func (r *RTC) Read() {
|
||||
rtcpBuf := make([]byte, 1500)
|
||||
for {
|
||||
if _, _, rtcpErr := r.sender.Read(rtcpBuf); rtcpErr != nil {
|
||||
log.Errorf("failed to read RTCP packet: %v", rtcpErr)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Init(host string, port int) (*RTC, error) {
|
||||
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||
func NewPeer() (*webrtc.PeerConnection, error) {
|
||||
peer, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create peer connection: %v", err)
|
||||
if err == nil {
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peer.OnICEConnectionStateChange(func(connState webrtc.ICEConnectionState) {
|
||||
log.Infof("Connection State has changed %s", connState.String())
|
||||
|
||||
if connState == webrtc.ICEConnectionStateFailed {
|
||||
if closeErr := peer.Close(); closeErr != nil {
|
||||
panic(closeErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Open a UDP Listener for RTP Packets on port 5004
|
||||
l, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP(host), Port: port})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to init webrtc listener: %v", err)
|
||||
}
|
||||
|
||||
// Increase the UDP receive buffer size
|
||||
// Default UDP buffer sizes vary on different operating systems
|
||||
bufferSize := 300000 // 300KB
|
||||
err = l.SetReadBuffer(bufferSize)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to set read buffer: %v", err)
|
||||
}
|
||||
|
||||
track, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264}, "video", "pion")
|
||||
if err != nil { // it should never happens
|
||||
panic(fmt.Sprintf("failed to create video track: %v", err))
|
||||
}
|
||||
|
||||
rtpSender, err := peerConnection.AddTrack(track)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to add track to peer connection: %v", err)
|
||||
}
|
||||
|
||||
r := &RTC{
|
||||
peer: peerConnection,
|
||||
sender: rtpSender,
|
||||
track: track,
|
||||
l: l,
|
||||
}
|
||||
rtc = r
|
||||
|
||||
return r, nil
|
||||
return peer, err
|
||||
}
|
||||
|
||||
func (r *RTC) Handshake(clientSession string) (string, error) {
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
r.peer.OnICEConnectionStateChange(func(connState webrtc.ICEConnectionState) {
|
||||
log.Infof("Connection State has changed %s", connState.String())
|
||||
// Read incoming RTCP packets
|
||||
// Before these packets are retuned they are processed by interceptors. For things
|
||||
// like NACK this needs to be called.
|
||||
func processRTCP(rtpSender *webrtc.RTPSender) {
|
||||
go func() {
|
||||
rtcpBuf := make([]byte, 1500)
|
||||
|
||||
if connState == webrtc.ICEConnectionStateFailed {
|
||||
if closeErr := r.peer.Close(); closeErr != nil {
|
||||
panic(closeErr)
|
||||
for {
|
||||
if _, _, rtcpErr := rtpSender.Read(rtcpBuf); rtcpErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Wait for the offer to be pasted
|
||||
offer := webrtc.SessionDescription{}
|
||||
decode(clientSession, &offer)
|
||||
fmt.Printf("Offer: %+v\n", offer)
|
||||
|
||||
// Set the remote SessionDescription
|
||||
if err := r.peer.SetRemoteDescription(offer); err != nil {
|
||||
return "", fmt.Errorf("failed to set remote session description: %v", err)
|
||||
}
|
||||
|
||||
// Create answer
|
||||
answer, err := r.peer.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create answer: %v", err)
|
||||
}
|
||||
|
||||
// Create channel that is blocked until ICE Gathering is complete
|
||||
gatherComplete := webrtc.GatheringCompletePromise(r.peer)
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
if err = r.peer.SetLocalDescription(answer); err != nil {
|
||||
return "", fmt.Errorf("failed to set local description: %v", err)
|
||||
}
|
||||
|
||||
// Block until ICE Gathering is complete, disabling trickle ICE
|
||||
// we do this because we only can exchange one signaling message
|
||||
// in a production application you should exchange ICE Candidates via OnICECandidate
|
||||
<-gatherComplete
|
||||
|
||||
r.localSession = encode(r.peer.LocalDescription())
|
||||
return r.localSession, nil
|
||||
}
|
||||
|
||||
func (r *RTC) Listen() error {
|
||||
// Read RTP packets forever and send them to the WebRTC Client
|
||||
inboundRTPPacket := make([]byte, 1600) // UDP MTU
|
||||
for {
|
||||
n, _, err := r.l.ReadFrom(inboundRTPPacket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error during read: %v", err)
|
||||
}
|
||||
|
||||
if _, err = r.track.Write(inboundRTPPacket[:n]); err != nil {
|
||||
if errors.Is(err, io.ErrClosedPipe) {
|
||||
// The peerConnection has been closed.
|
||||
return ErrPeerClosedConn
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to send RTP packet to client: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// JSON encode + base64 a SessionDescription
|
||||
func encode(obj *webrtc.SessionDescription) string {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Decode a base64 and unmarshal JSON into a SessionDescription
|
||||
func decode(in string, obj *webrtc.SessionDescription) {
|
||||
b, err := base64.StdEncoding.DecodeString(in)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(b, obj); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user