debug needed

This commit is contained in:
TonyChyi 2022-09-26 16:07:25 +08:00
parent bd0812cc42
commit 821c0cffb8
12 changed files with 148 additions and 171 deletions

View File

@ -38,10 +38,16 @@ func (s *Server) Run() error {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
qemuserver.Setup(s.options.Qemu) if err := qemuserver.Setup(s.options.Qemu); err != nil {
webserver.Setup(s.options.WEBServer) return err
<-make(chan int) }
return nil
webServer, err := webserver.NewServer(s.options.WEBServer)
if err != nil {
return err
}
return webServer.Run()
} }
func runServer(c *cli.Context) error { func runServer(c *cli.Context) error {

View File

@ -77,14 +77,10 @@ func makeControlCommand(t int) []qmp.Command {
} }
func makeHMCommand(cmdTemplate string, args ...any) qmp.Command { func makeHMCommand(cmdTemplate string, args ...any) qmp.Command {
template := qmp.Command{ return qmp.Command{
Execute: "human-monitor-command", Execute: "human-monitor-command",
Args: CommandLine{
Command: fmt.Sprintf(cmdTemplate, args...),
},
} }
command := CommandLine{
Command: fmt.Sprintf(cmdTemplate, args...),
}
template.Args = command
return template
} }

View File

@ -1,7 +0,0 @@
package qemuconnection
import "github.com/sirupsen/logrus"
func init() {
logrus.Info("qemu client control events module loaded")
}

View File

@ -12,26 +12,41 @@ const DefaultStreamID = "ace-server"
type Connection struct { type Connection struct {
option *Options option *Options
api *webrtc.API
stream mediadevices.MediaStream
} }
func New(o *Options) *Connection { func New(o *Options) (*Connection, error) {
return &Connection{ connection := &Connection{
option: o, option: o,
} }
} codecSelector, err := setupCodec(o.Video.BPS, o.Audio.BPS)
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 { if err != nil {
return nil, err return nil, err
} }
me := &webrtc.MediaEngine{} me := &webrtc.MediaEngine{}
codecSelector.Populate(me) codecSelector.Populate(me)
api := webrtc.NewAPI(webrtc.WithMediaEngine(me))
rtc, err := api.NewPeerConnection(webrtc.Configuration{ connection.api = webrtc.NewAPI(webrtc.WithMediaEngine(me))
s, err := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
Video: func(mtc *mediadevices.MediaTrackConstraints) {},
Audio: func(mtc *mediadevices.MediaTrackConstraints) {},
Codec: codecSelector,
})
if err != nil {
return nil, err
}
connection.stream = s
return connection, nil
}
func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDescription, error) {
logrus.Debug("received offer ", offer.Type.String())
rtc, err := c.api.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{ ICEServers: []webrtc.ICEServer{
{ {
URLs: c.option.STUNServers, URLs: c.option.STUNServers,
@ -53,21 +68,7 @@ func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDe
} }
}) })
s, err := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{ for _, track := range c.stream.GetTracks() {
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) { track.OnEnded(func(err error) {
logrus.Errorf("Track (ID: %s) ended with error: %v", track.ID(), err) logrus.Errorf("Track (ID: %s) ended with error: %v", track.ID(), err)
}) })

View File

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

View File

@ -10,7 +10,7 @@ import (
) )
func init() { func init() {
logrus.Info("Starting...") logrus.SetReportCaller(true)
} }
func main() { func main() {

View File

@ -1,7 +0,0 @@
package qemuserver
import "github.com/sirupsen/logrus"
func init() {
logrus.Info("qemu server loaded")
}

View File

@ -19,25 +19,25 @@ type Server struct {
RX chan *qemuconnection.Event RX chan *qemuconnection.Event
TX chan qemu.Status TX chan qemu.Status
} }
qemu *qemu.Domain
} }
var DefaultServer *Server var DefaultServer *Server
func NewServer(o *Options) *Server { func NewServer(o *Options) (*Server, error) {
if err := o.MakeFIFO(); err != nil {
return nil, err
}
server := &Server{ server := &Server{
options: o, options: o,
} }
server.QmpConnector.RX = make(chan *qemuconnection.Event) server.QmpConnector.RX = make(chan *qemuconnection.Event)
server.QmpConnector.TX = make(chan qemu.Status) server.QmpConnector.TX = make(chan qemu.Status)
return server
}
func (s *Server) Run() error { u, err := url.Parse(o.QmpAddress)
logrus.Debug("qemu server running")
defer logrus.Debug("qemu server exit")
u, err := url.Parse(s.options.QmpAddress)
if err != nil { if err != nil {
return err return nil, err
} }
var address string var address string
if u.Scheme == "unix" { if u.Scheme == "unix" {
@ -46,29 +46,57 @@ func (s *Server) Run() error {
address = u.Host address = u.Host
} }
logrus.Debugf("trying to connect qmp with %s://%s", u.Scheme, address) logrus.Debugf("trying to connect qmp with %s://%s", u.Scheme, address)
qmpConnection, err := qmp.NewSocketMonitor(u.Scheme, address, s.options.Timeout) qmpConnection, err := qmp.NewSocketMonitor(u.Scheme, address, o.Timeout)
if err != nil { if err != nil {
return err return nil, err
} }
defer qmpConnection.Disconnect()
if err := qmpConnection.Connect(); err != nil { if err := qmpConnection.Connect(); err != nil {
return err return nil, err
} }
logrus.Debug("qmp connected") logrus.Debug("qmp connected")
qemu, err := qemu.NewDomain(qmpConnection, s.options.Name) qemu, err := qemu.NewDomain(qmpConnection, o.Name)
if err != nil { if err != nil {
return err return nil, err
} }
defer qemu.Close() server.qemu = qemu
go s.startCapture(qemu) if err := driver.GetManager().Register(
audiodriver.New(o.AudioPipe),
driver.Info{
Label: "audioFifo",
DeviceType: driver.Microphone,
Priority: driver.PriorityNormal,
},
); err != nil {
return nil, err
}
if err := driver.GetManager().Register(
vncdriver.NewVnc(o.VNCAddress),
driver.Info{
Label: "vnc",
DeviceType: driver.Camera,
Priority: driver.PriorityNormal,
},
); err != nil {
return nil, err
}
return server, nil
}
func (s *Server) Run() error {
logrus.Debug("qemu server running")
defer logrus.Debug("qemu server exit")
defer s.qemu.Close()
go s.startCapture()
logrus.Debug("qemu capture start") logrus.Debug("qemu capture start")
for ev := range s.QmpConnector.RX { for ev := range s.QmpConnector.RX {
if ev.Type == qemuconnection.QueryStatusEvent { if ev.Type == qemuconnection.QueryStatusEvent {
status, err := qemu.Status() status, err := s.qemu.Status()
if err != nil { if err != nil {
logrus.Error("get qemu status error: ", err) logrus.Error("get qemu status error: ", err)
continue continue
@ -77,43 +105,17 @@ func (s *Server) Run() error {
continue continue
} }
for _, cmd := range ev.ToQemuCommand() { for _, cmd := range ev.ToQemuCommand() {
_, err := qemu.Run(cmd) _, err := s.qemu.Run(cmd)
if err != nil { if err != nil {
logrus.Error("run command error: ", err) logrus.Error("run command error: ", err)
} }
} }
} }
return nil return nil
} }
func (s *Server) startCapture(qemu *qemu.Domain) { func (s *Server) startCapture() {
if err := s.options.MakeFIFO(); err != nil { if _, err := s.qemu.Run(qmp.Command{
logrus.Fatal("failed to make pipe file: ", err)
}
if err := driver.GetManager().Register(
audiodriver.New(s.options.AudioPipe),
driver.Info{
Label: "audioFifo",
DeviceType: driver.Microphone,
Priority: driver.PriorityNormal,
},
); err != nil {
logrus.Fatal("audio initialize failed: ", err)
}
if err := driver.GetManager().Register(
vncdriver.NewVnc(s.options.VNCAddress),
driver.Info{
Label: "vnc",
DeviceType: driver.Camera,
Priority: driver.PriorityNormal,
},
); err != nil {
logrus.Fatal("video initialize failed: ", err)
}
if _, err := qemu.Run(qmp.Command{
Execute: "human-monitor-command", Execute: "human-monitor-command",
Args: map[string]string{ Args: map[string]string{
"command-line": fmt.Sprintf( "command-line": fmt.Sprintf(
@ -128,13 +130,17 @@ func (s *Server) startCapture(qemu *qemu.Domain) {
logrus.Debug("audio capture set") logrus.Debug("audio capture set")
} }
func Setup(o *Options) { func Setup(o *Options) error {
DefaultServer = NewServer(o) DefaultServer, err := NewServer(o)
if err != nil {
return err
}
go func() { go func() {
if err := DefaultServer.Run(); err != nil { if err := DefaultServer.Run(); err != nil {
logrus.Fatal("cannot run qemuserver with error: ", err) logrus.Fatal("cannot run qemuserver with error: ", err)
} }
}() }()
return nil
} }
func SendEvent(b []byte) error { func SendEvent(b []byte) error {

View File

@ -1,7 +0,0 @@
package webserver
import "github.com/sirupsen/logrus"
func init() {
logrus.Info("web server loaded")
}

View File

@ -12,15 +12,18 @@ type Server struct {
rtcConnector *webrtcconnection.Connection rtcConnector *webrtcconnection.Connection
} }
var DefaultServer *Server func NewServer(o *Options) (*Server, error) {
rtc, err := webrtcconnection.New(o.WebRTC)
if err != nil {
return nil, err
}
func NewServer(o *Options) *Server {
s := &Server{ s := &Server{
options: o, options: o,
webServer: gin.New(), webServer: gin.New(),
rtcConnector: webrtcconnection.New(o.WebRTC), rtcConnector: rtc,
} }
return s return s, nil
} }
func (s *Server) Run() error { func (s *Server) Run() error {
@ -29,12 +32,3 @@ func (s *Server) Run() error {
s.setupRoute() s.setupRoute()
return s.webServer.Run(s.options.Listen) return s.webServer.Run(s.options.Listen)
} }
func Setup(o *Options) {
DefaultServer = NewServer(o)
go func() {
if err := DefaultServer.Run(); err != nil {
logrus.Fatal("cannot run webserver with error: ", err)
}
}()
}

View File

@ -26,8 +26,6 @@ func staticFileHandler() gin.HandlerFunc {
defer ctx.Abort() defer ctx.Abort()
filename := strings.TrimLeft(ctx.Request.RequestURI, "/") filename := strings.TrimLeft(ctx.Request.RequestURI, "/")
logrus.Debug("static file: ", filename)
if filename == "" { if filename == "" {
filename = "index.html" filename = "index.html"
} }

View File

@ -74,8 +74,9 @@ onMounted(() => {
}); });
pc.oniceconnectionstatechange = () => console.log(pc.iceConnectionState); pc.oniceconnectionstatechange = () => console.log(pc.iceConnectionState);
pc.addTransceiver("video"); pc.addTransceiver("video");
//pc.addTransceiver('audio') pc.addTransceiver("audio");
dataChannel = pc.createDataChannel("control");
const dataChannel = pc.createDataChannel("control");
dataChannel.onmessage = (e) => { dataChannel.onmessage = (e) => {
const d = JSON.parse(e.data); const d = JSON.parse(e.data);
store.delay = +new Date() - d.server_time; store.delay = +new Date() - d.server_time;
@ -86,48 +87,51 @@ onMounted(() => {
video.autoplay = true; video.autoplay = true;
video.controls = false; video.controls = false;
}; };
video.onmousemove = (ev) => {
dataChannel.send(
JSON.stringify(
makeEvent("mouseMove", {
dx: ev.clientX,
dy: ev.clientY,
dz: 0,
})
)
);
};
video.onmousedown = (ev) => {
dataChannel.send(
JSON.stringify(
makeEvent("mouseButton", {
button: ev.button << 1,
})
)
);
};
//video.onmousewheel = (ev) => {};
window.onkeydown = (ev) => {
let key = "";
if (ev.ctrlKey && ev.which !== 17) key = "ctrl-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (ev.shiftKey && ev.which !== 16) key = "shift-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (ev.altKey && ev.which !== 18) key = "alt-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (ev.metaKey && ev.which !== 91 && ev.which !== 93)
key = "meta-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (!ev.altKey && !ev.shiftKey && !ev.ctrlKey && !ev.metaKey)
key = "0x" + ev.which.toString(16);
dataChannel.send( dataChannel.onopen = () => {
JSON.stringify( video.onmousemove = (ev) => {
makeEvent("keyboard", { dataChannel.send(
key: key, JSON.stringify(
}) makeEvent("mouseMove", {
) dx: ev.clientX,
); dy: ev.clientY,
dz: 0,
})
)
);
};
video.onmousedown = (ev) => {
dataChannel.send(
JSON.stringify(
makeEvent("mouseButton", {
button: ev.button << 1,
})
)
);
};
//video.onmousewheel = (ev) => {};
window.onkeydown = (ev) => {
let key = "";
if (ev.ctrlKey && ev.which !== 17) key = "ctrl-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (ev.shiftKey && ev.which !== 16) key = "shift-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (ev.altKey && ev.which !== 18) key = "alt-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (ev.metaKey && ev.which !== 91 && ev.which !== 93)
key = "meta-" + ev.key;
else key = "0x" + ev.which.toString(16);
if (!ev.altKey && !ev.shiftKey && !ev.ctrlKey && !ev.metaKey)
key = "0x" + ev.which.toString(16);
dataChannel.send(
JSON.stringify(
makeEvent("keyboard", {
key: key,
})
)
);
};
}; };
pc.createOffer().then((offer) => { pc.createOffer().then((offer) => {