diff --git a/lib/audiodriver/wavfifo.go b/lib/audiodriver/wavfifo.go index dd63514..245ff33 100644 --- a/lib/audiodriver/wavfifo.go +++ b/lib/audiodriver/wavfifo.go @@ -73,6 +73,7 @@ func (w *WavFIFODriver) Properties() []prop.Media { } func (w *WavFIFODriver) AudioRecord(p prop.Media) (audio.Reader, error) { + logrus.Debugf("wave header: %v", w.waveHeader) offset := FmtHeaderOffset + FmtHeaderIDSize + FmtHeaderChunkSizeSize + int64(w.waveHeader.Size) + DataChunkIDSize + DataChunkSizeSize if _, err := w.f.Seek( offset, @@ -105,6 +106,7 @@ func (w *WavFIFODriver) AudioRecord(p prop.Media) (audio.Reader, error) { case <-w.closed: return nil, func() {}, io.EOF case pcmData, ok := <-pcm: + logrus.Debug("got %d bytes pcm data", len(pcmData)) if !ok { return nil, func() {}, io.ErrClosedPipe } diff --git a/lib/webrtcconnection/connection.go b/lib/webrtcconnection/connection.go index 29e6fa6..14456ea 100644 --- a/lib/webrtcconnection/connection.go +++ b/lib/webrtcconnection/connection.go @@ -4,7 +4,6 @@ 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" ) @@ -56,8 +55,10 @@ func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDe 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) + /* + mtc.Height = prop.IntExact(c.option.Video.Height) + mtc.Width = prop.IntExact(c.option.Video.Width) + */ }, Audio: func(mtc *mediadevices.MediaTrackConstraints) {}, Codec: codecSelector, diff --git a/servers/qemuserver/options.go b/servers/qemuserver/options.go index ff0a9be..7596494 100644 --- a/servers/qemuserver/options.go +++ b/servers/qemuserver/options.go @@ -31,8 +31,6 @@ func ExampleOptions() *Options { } func (o *Options) MakeFIFO() error { - if err := os.Remove(o.AudioPipe); err != nil { - return err - } - return syscall.Mkfifo(o.AudioDevice, 0600) + os.Remove(o.AudioPipe) + return syscall.Mkfifo(o.AudioPipe, 0600) } diff --git a/servers/qemuserver/server.go b/servers/qemuserver/server.go index a0c47df..baf0ebc 100644 --- a/servers/qemuserver/server.go +++ b/servers/qemuserver/server.go @@ -91,19 +91,6 @@ func (s *Server) startCapture(qemu *qemu.Domain) { if err := s.options.MakeFIFO(); err != nil { logrus.Fatal("failed to make pipe file: ", err) } - if _, err := 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) - } - logrus.Debug("audio capture set") if err := driver.GetManager().Register( audiodriver.New(s.options.AudioPipe), @@ -126,6 +113,19 @@ func (s *Server) startCapture(qemu *qemu.Domain) { logrus.Fatal("video initialize failed: ", err) } + if _, err := 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) + } + logrus.Debug("audio capture set") } func Setup(o *Options) { diff --git a/servers/webserver/handlers.go b/servers/webserver/handlers.go index 83b43af..298d883 100644 --- a/servers/webserver/handlers.go +++ b/servers/webserver/handlers.go @@ -25,7 +25,7 @@ func (s *Server) getInstruction(c *gin.Context) { } func (s *Server) exchangeSDP(c *gin.Context) { - var offer *webrtc.SessionDescription + offer := &webrtc.SessionDescription{} if err := c.BindJSON(offer); err != nil { c.JSON(http.StatusBadRequest, Response{ Succeed: false, diff --git a/servers/webserver/routers.go b/servers/webserver/routers.go index 378750f..2026ded 100644 --- a/servers/webserver/routers.go +++ b/servers/webserver/routers.go @@ -1,12 +1,30 @@ package webserver -import "github.com/gin-contrib/static" +import ( + "strings" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" +) func (s *Server) setupRoute() { - s.webServer. - Use(static.Serve("/", newFS())). - POST("/api/v1/sdp", s.exchangeSDP). - GET("/api/v1/instruction", s.getInstruction). - GET("/api/v1/iceserver/url", s.getICEConfig). - GET("/api/v1/name", s.getName) + apiHandler := gin.New() + groupV1 := apiHandler.Group("/api/v1") + { + groupV1. + POST("/sdp", s.exchangeSDP). + GET("/instruction", s.getInstruction). + GET("/iceserver/url", s.getICEConfig). + GET("/name", s.getName) + } + + s.webServer.Use(func(ctx *gin.Context) { + path := ctx.Request.RequestURI + logrus.Debug(path) + if strings.HasPrefix(path, "/api") { + apiHandler.HandleContext(ctx) + } else { + staticFileHandler()(ctx) + } + }) } diff --git a/servers/webserver/static.go b/servers/webserver/static.go index 681329b..af18fc9 100644 --- a/servers/webserver/static.go +++ b/servers/webserver/static.go @@ -5,33 +5,33 @@ import ( "io/fs" "net/http" "strings" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" ) //go:generate cp -r ../../web/dist ./ //go:embed dist var staticFiles embed.FS -type ginFS struct { - fs http.FileSystem -} - -func newFS() *ginFS { - f, _ := fs.Sub(staticFiles, "dist") - return &ginFS{ - fs: http.FS(f), +func staticFileHandler() gin.HandlerFunc { + sf, err := fs.Sub(staticFiles, "dist") + if err != nil { + logrus.Fatal("compile error: ", err) } -} -func (s *ginFS) Exists(prefix string, filepath string) bool { - if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { - if _, err := s.fs.Open(p); err != nil { - return false + fs := http.FileServer(http.FS(sf)) + + return func(ctx *gin.Context) { + defer ctx.Abort() + filename := strings.TrimLeft(ctx.Request.RequestURI, "/") + + logrus.Debug("static file: ", filename) + + if filename == "" { + filename = "index.html" } - return true - } - return false -} -func (s *ginFS) Open(name string) (http.File, error) { - return s.fs.Open(name) + fs.ServeHTTP(ctx.Writer, ctx.Request) + } }