debug needed

This commit is contained in:
Sense T
2022-09-26 03:04:07 +00:00
parent 0ce8374276
commit 4c80f8a25e
39 changed files with 8233 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
package webrtcconnection
import (
"github.com/pion/mediadevices"
"github.com/pion/mediadevices/pkg/codec/opus"
"github.com/pion/mediadevices/pkg/codec/x264"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/webrtc/v3"
"github.com/sirupsen/logrus"
)
const DefaultStreamID = "ace-server"
type Connection struct {
option *Options
}
func New(o *Options) *Connection {
return &Connection{
option: o,
}
}
func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDescription, error) {
logrus.Debug("received offer ", offer.Type.String())
codecSelector, err := setupCodec(c.option.Video.BPS, c.option.Audio.BPS)
if err != nil {
return nil, err
}
me := &webrtc.MediaEngine{}
codecSelector.Populate(me)
api := webrtc.NewAPI(webrtc.WithMediaEngine(me))
rtc, err := api.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: c.option.STUNServers,
},
},
})
if err != nil {
return nil, err
}
rtc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
logrus.Debug("connection state has changed: ", connectionState.String())
switch connectionState {
case webrtc.ICEConnectionStateFailed:
fallthrough
case webrtc.ICEConnectionStateClosed:
rtc.Close()
}
})
s, err := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
Video: func(mtc *mediadevices.MediaTrackConstraints) {
mtc.Height = prop.IntExact(c.option.Video.Height)
mtc.Width = prop.IntExact(c.option.Video.Width)
},
Audio: func(mtc *mediadevices.MediaTrackConstraints) {},
Codec: codecSelector,
})
if err != nil {
return nil, err
}
for _, track := range s.GetTracks() {
track.OnEnded(func(err error) {
logrus.Errorf("Track (ID: %s) ended with error: %v", track.ID(), err)
})
_, err := rtc.AddTransceiverFromTrack(track, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
if err != nil {
logrus.Error(err)
}
}
rtc.OnDataChannel(dataChannel)
if err := rtc.SetRemoteDescription(*offer); err != nil {
return nil, err
}
logrus.Debug("offer set")
answer, err := rtc.CreateAnswer(nil)
if err != nil {
return nil, err
}
gatherComplete := webrtc.GatheringCompletePromise(rtc)
if err := rtc.SetLocalDescription(answer); err != nil {
return nil, err
}
logrus.Debug("answer set")
<-gatherComplete
defer logrus.Debug("regist complete")
return rtc.LocalDescription(), nil
}
func setupCodec(videoBPS, audioBPS int) (*mediadevices.CodecSelector, error) {
x264Prarm, err := x264.NewParams()
if err != nil {
return nil, err
}
x264Prarm.BitRate = videoBPS
opusParam, err := opus.NewParams()
if err != nil {
return nil, err
}
opusParam.BitRate = audioBPS
codecSelector := mediadevices.NewCodecSelector(
mediadevices.WithAudioEncoders(&opusParam),
mediadevices.WithVideoEncoders(&x264Prarm),
)
return codecSelector, nil
}

View File

@@ -0,0 +1,50 @@
package webrtcconnection
import (
"encoding/json"
"time"
"git.sense-t.eu.org/ACE/ace/servers/qemuserver"
"github.com/pion/webrtc/v3"
"github.com/sirupsen/logrus"
)
func dataChannel(d *webrtc.DataChannel) {
d.OnOpen(func() {
for {
status := qemuserver.GetStatus().String()
currentTime := time.Now().UnixMilli()
b, err := json.Marshal(map[string]any{
"qemu_status": status,
"server_time": currentTime,
})
if err != nil {
logrus.Errorf(
"failed to parse to json on '%s-%d' with error: %v",
d.Label(), *d.ID(), err,
)
}
if err := d.Send(b); err != nil {
logrus.Errorf(
"failed to send qemu status to '%s-%d' with error: %v",
d.Label(), *d.ID(), err,
)
}
time.Sleep(time.Second)
}
})
d.OnMessage(func(msg webrtc.DataChannelMessage) {
logrus.Debugf("received %d bytes message from '%s-%d'", len(msg.Data), d.Label(), *d.ID())
if !msg.IsString {
return
}
if err := qemuserver.SendEvent(msg.Data); err != nil {
logrus.Errorf(
"cannot parse message from '%s-%d' to qemu controll event: %v",
d.Label(), *d.ID(), err,
)
}
})
}

View File

@@ -0,0 +1,7 @@
package webrtcconnection
import "github.com/sirupsen/logrus"
func init() {
logrus.Info("webrtc connection module initialized")
}

View File

@@ -0,0 +1,27 @@
package webrtcconnection
type Options struct {
STUNServers []string `yaml:"stun_servers"`
Video struct {
Height int `yaml:"height"`
Width int `yaml:"width"`
BPS int `yaml:"bps"`
} `yaml:"video"`
Audio struct {
BPS int `yaml:"bps"`
} `yaml:"audio"`
}
func ExampleOptions() *Options {
options := &Options{
STUNServers: []string{
"stun:stun.l.google.com:19302",
"stun:wetofu.me:3478",
},
}
options.Video.BPS = 500_000
options.Video.Height = 768
options.Video.Width = 1024
options.Audio.BPS = 96_000
return options
}