ace/servers/qemuserver/server.go

203 lines
4.3 KiB
Go
Raw Normal View History

2022-09-26 03:04:07 +00:00
package qemuserver
import (
"fmt"
"net/url"
2022-09-27 02:21:06 +00:00
"os"
"time"
2022-09-26 03:04:07 +00:00
"git.sense-t.eu.org/ACE/ace/lib/audiodriver"
"git.sense-t.eu.org/ACE/ace/lib/qemuconnection"
"github.com/digitalocean/go-qemu/qemu"
"github.com/digitalocean/go-qemu/qmp"
"github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/driver/vncdriver"
"github.com/sirupsen/logrus"
)
type Server struct {
options *Options
QmpConnector struct {
RX chan *qemuconnection.Event
TX chan qemu.Status
}
2022-09-27 02:21:06 +00:00
qemu *qemu.Domain
audioHeader chan *audiodriver.WavHeader
pcm chan [audiodriver.BufferSize]byte
2022-09-26 03:04:07 +00:00
}
var DefaultServer *Server
2022-09-26 08:07:25 +00:00
func NewServer(o *Options) (*Server, error) {
if err := o.MakeFIFO(); err != nil {
return nil, err
}
2022-09-26 03:04:07 +00:00
server := &Server{
2022-09-27 02:21:06 +00:00
options: o,
audioHeader: make(chan *audiodriver.WavHeader, 1),
pcm: make(chan [audiodriver.BufferSize]byte),
2022-09-26 03:04:07 +00:00
}
server.QmpConnector.RX = make(chan *qemuconnection.Event)
server.QmpConnector.TX = make(chan qemu.Status)
2022-09-26 08:07:25 +00:00
u, err := url.Parse(o.QmpAddress)
2022-09-26 03:04:07 +00:00
if err != nil {
2022-09-26 08:07:25 +00:00
return nil, err
2022-09-26 03:04:07 +00:00
}
var address string
if u.Scheme == "unix" {
address = u.Path
} else {
address = u.Host
}
logrus.Debugf("trying to connect qmp with %s://%s", u.Scheme, address)
2022-09-26 08:07:25 +00:00
qmpConnection, err := qmp.NewSocketMonitor(u.Scheme, address, o.Timeout)
2022-09-26 03:04:07 +00:00
if err != nil {
2022-09-26 08:07:25 +00:00
return nil, err
2022-09-26 03:04:07 +00:00
}
if err := qmpConnection.Connect(); err != nil {
2022-09-26 08:07:25 +00:00
return nil, err
2022-09-26 03:04:07 +00:00
}
logrus.Debug("qmp connected")
2022-09-26 08:07:25 +00:00
qemu, err := qemu.NewDomain(qmpConnection, o.Name)
2022-09-26 03:04:07 +00:00
if err != nil {
2022-09-26 08:07:25 +00:00
return nil, err
2022-09-26 03:04:07 +00:00
}
2022-09-26 08:07:25 +00:00
server.qemu = qemu
2022-09-27 02:21:06 +00:00
audio := audiodriver.New()
2022-09-26 08:07:25 +00:00
if err := driver.GetManager().Register(
2022-09-27 02:21:06 +00:00
audio,
2022-09-26 08:07:25 +00:00
driver.Info{
Label: "audioFifo",
DeviceType: driver.Microphone,
Priority: driver.PriorityNormal,
},
); err != nil {
return nil, err
}
2022-09-27 02:21:06 +00:00
audio.PCM = server.pcm
audio.WaveHeader = server.audioHeader
2022-09-26 08:07:25 +00:00
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
}
2022-09-26 03:04:07 +00:00
2022-09-26 08:07:25 +00:00
func (s *Server) Run() error {
logrus.Debug("qemu server running")
defer logrus.Debug("qemu server exit")
defer s.qemu.Close()
2022-09-27 02:21:06 +00:00
s.startCapture()
2022-09-26 03:04:07 +00:00
logrus.Debug("qemu capture start")
for ev := range s.QmpConnector.RX {
if ev.Type == qemuconnection.QueryStatusEvent {
2022-09-26 08:07:25 +00:00
status, err := s.qemu.Status()
2022-09-26 03:04:07 +00:00
if err != nil {
logrus.Error("get qemu status error: ", err)
continue
}
s.QmpConnector.TX <- status
continue
}
for _, cmd := range ev.ToQemuCommand() {
2022-09-26 08:07:25 +00:00
_, err := s.qemu.Run(cmd)
2022-09-26 03:04:07 +00:00
if err != nil {
logrus.Error("run command error: ", err)
}
}
}
return nil
}
2022-09-26 08:07:25 +00:00
func (s *Server) startCapture() {
2022-09-27 04:32:23 +00:00
const waveHeaderSize = 16
waveHeader := &audiodriver.WavHeader{
Size: waveHeaderSize,
AudioFormat: 1,
NumChannels: 2,
SampleRate: 44100,
BlockAlign: 4,
BitsPerSample: 16,
}
2022-09-27 02:21:06 +00:00
go func() {
f, err := os.Open(s.options.AudioPipe)
if err != nil {
logrus.Fatal(err)
}
defer f.Close()
logrus.Debug("start reading from fifo")
s.audioHeader <- waveHeader
close(s.audioHeader) // only once
2022-09-27 04:32:23 +00:00
// skip to pcm data, for 44 bytes.
var _dataChunkHeader [audiodriver.FmtHeaderOffset +
audiodriver.FmtHeaderIDSize + audiodriver.FmtHeaderChunkSizeSize + waveHeaderSize +
audiodriver.DataChunkIDSize + audiodriver.DataChunkSizeSize]byte
2022-09-27 02:21:06 +00:00
if _, err := f.Read(_dataChunkHeader[:]); err != nil {
logrus.Fatal(err)
}
defer close(s.pcm)
for {
var b [audiodriver.BufferSize]byte
if _, err := f.Read(b[:]); err != nil {
logrus.Error(err)
}
select {
case s.pcm <- b:
case <-time.After(waveHeader.GetLatnecy()):
}
}
}()
2022-09-26 03:04:07 +00:00
go func() {
2022-09-27 02:21:06 +00:00
if _, err := s.qemu.Run(qmp.Command{
Execute: "human-monitor-command",
Args: map[string]string{
"command-line": fmt.Sprintf(
"wavcapture %s %s",
s.options.AudioPipe,
s.options.AudioDevice,
),
},
}); err != nil {
logrus.Fatal("run audio command failed: ", err)
2022-09-26 03:04:07 +00:00
}
2022-09-27 02:21:06 +00:00
logrus.Debug("audio capture set")
2022-09-26 03:04:07 +00:00
}()
}
2022-09-27 02:21:06 +00:00
func (s *Server) SendEvent(b []byte) error {
2022-09-26 03:04:07 +00:00
ev, err := qemuconnection.ParseEvent(b)
if err != nil {
return err
}
2022-09-27 02:21:06 +00:00
s.QmpConnector.RX <- ev
2022-09-26 03:04:07 +00:00
return nil
}
2022-09-27 02:21:06 +00:00
func (s *Server) GetStatus() qemu.Status {
s.QmpConnector.RX <- &qemuconnection.Event{
2022-09-26 03:04:07 +00:00
Type: qemuconnection.QueryStatusEvent,
}
2022-09-27 02:21:06 +00:00
return <-s.QmpConnector.TX
2022-09-26 03:04:07 +00:00
}