760 lines
19 KiB
Go
760 lines
19 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"mxl-player/internal/imgui"
|
|
"mxl-player/internal/renderer"
|
|
"mxl-player/internal/sdl"
|
|
"mxl-player/internal/source"
|
|
"os"
|
|
"runtime"
|
|
"time"
|
|
"unsafe"
|
|
|
|
cimgui "github.com/AllenDang/cimgui-go/imgui"
|
|
vk "github.com/christerso/vulkan-go/vk"
|
|
pflag "github.com/spf13/pflag"
|
|
)
|
|
|
|
const (
|
|
APP_NAME = "MXL Player"
|
|
APP_VER = "0.1.0"
|
|
WIN_WIDTH int32 = 1280
|
|
WIN_HEIGHT int32 = 720
|
|
)
|
|
|
|
type appArgs struct {
|
|
ShowHelp bool
|
|
Domain string
|
|
VideoDomain string
|
|
AudioDomain string
|
|
VideoFlowId string
|
|
AudioFlowId string
|
|
IsFullscreen bool
|
|
PlaybackId uint32
|
|
GpuId uint32
|
|
IsVerbose bool
|
|
ListAudio bool
|
|
ListGPU bool
|
|
SyncRequested bool
|
|
MaxAttempts int
|
|
}
|
|
|
|
func printCliHelp(fs *pflag.FlagSet) {
|
|
fmt.Printf("%s %s\n", APP_NAME, APP_VER)
|
|
fmt.Println("Usage: mxl-player -d <domain> (-v <uuid> | -a <uuid>) [-f] [-h]")
|
|
fmt.Println(" [-g <gpu-id>] [-p <playback-id>] [--verbose]")
|
|
fmt.Println(" or: mxl-player [--list-playback] [--list-gpu]")
|
|
fmt.Println(" or: mxl-player (and set everything in GUI)")
|
|
fmt.Println()
|
|
fs.PrintDefaults()
|
|
}
|
|
|
|
func printUsage(w io.Writer) {
|
|
fmt.Fprintln(w, "Usage: mxl-player -d <domain> (-v <uuid> | -a <uuid>) [-f] [-h]")
|
|
fmt.Fprintln(w, "Try 'mxl-player -h' for more information.")
|
|
}
|
|
|
|
func checkMXLargs(args appArgs) {
|
|
if args.Domain == "" {
|
|
fmt.Fprintln(os.Stderr, "You should provide valid MXL domain and UUID of at least one flow")
|
|
printUsage(os.Stderr)
|
|
os.Exit(2)
|
|
}
|
|
|
|
fi, err := os.Stat(args.Domain)
|
|
if err != nil || !fi.IsDir() {
|
|
fmt.Fprintln(os.Stderr, "Invalid MXL domain:", args.Domain)
|
|
fmt.Fprintln(os.Stderr, "Domain must be a directory in tmpfs")
|
|
printUsage(os.Stderr)
|
|
os.Exit(2)
|
|
}
|
|
if args.VideoFlowId == "" && args.AudioFlowId == "" {
|
|
fmt.Fprintln(os.Stderr, "You must provide at least 1 MXL flow UUID")
|
|
printUsage(os.Stderr)
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// timelapse video: 5fbec3b1-1b0f-417d-9059-8b94a47197ed
|
|
// timelapse audio: 5fbec3b1-1b0f-417d-9059-8b94a47197ec
|
|
// f1 video: 5fbec3b1-1b0f-417d-9059-8b94a47197ef
|
|
// f1 audio: 5fbec3b1-1b0f-417d-9059-8b94a47197eb
|
|
var args appArgs
|
|
flagSet := pflag.NewFlagSet(APP_NAME, pflag.ContinueOnError)
|
|
flagSet.Usage = func() { printUsage(os.Stderr) }
|
|
flagSet.BoolVarP(&args.ShowHelp, "help", "h", false, "Show help message and exit")
|
|
flagSet.StringVarP(&args.Domain, "domain", "d", "", "MXL domain directory")
|
|
flagSet.StringVarP(&args.VideoFlowId, "video", "v", "", "Video flow UUID")
|
|
flagSet.StringVarP(&args.AudioFlowId, "audio", "a", "", "Audio flow UUID")
|
|
flagSet.BoolVarP(&args.IsFullscreen, "fullscreen", "f", false, "Run app in fullscreen mode")
|
|
flagSet.Uint32VarP(&args.GpuId, "gpu-id", "g", 0, "GPU id [TODO]")
|
|
flagSet.Uint32VarP(&args.PlaybackId, "playback-id", "p", 0, "Playback audio device id")
|
|
flagSet.BoolVar(&args.IsVerbose, "verbose", false, "Verbose output [TODO]")
|
|
flagSet.BoolVar(&args.ListAudio, "list-playback", false, "List audio playback devices and exit")
|
|
flagSet.BoolVar(&args.ListGPU, "list-gpu", false, "List GPUs and exit")
|
|
|
|
if err := flagSet.Parse(os.Args[1:]); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
printUsage(os.Stderr)
|
|
os.Exit(2)
|
|
}
|
|
if args.ShowHelp {
|
|
printCliHelp(flagSet)
|
|
return
|
|
}
|
|
if !args.ListAudio && !args.ListGPU {
|
|
checkMXLargs(args)
|
|
}
|
|
// end of cli args parse
|
|
|
|
runtime.LockOSThread()
|
|
if err := sdl.Load(); err != nil {
|
|
panic(err)
|
|
}
|
|
if !sdl.Init(sdl.InitVideo | sdl.InitAudio) {
|
|
log.Fatalf("SDL_Init: %s", sdl.GetError())
|
|
return
|
|
}
|
|
|
|
sdlAudioDevice := sdl.AudioDeviceDefaultPlayback
|
|
if args.PlaybackId != 0 {
|
|
sdlAudioDevice = args.PlaybackId
|
|
}
|
|
// List audio playback devices and exit
|
|
if args.ListAudio {
|
|
devs := sdl.GetAudioPlaybackDevices()
|
|
fmt.Println("Available playback audio devices")
|
|
fmt.Println("id name")
|
|
for _, d := range devs {
|
|
fmt.Println(d.ID, d.Name)
|
|
}
|
|
return
|
|
}
|
|
|
|
windowHandler := sdl.CreateWindow(fmt.Sprintf("%s %s", APP_NAME, APP_VER), WIN_WIDTH, WIN_HEIGHT, sdl.WindowVulkan|sdl.WindowResizable)
|
|
if windowHandler == 0 {
|
|
sdl.Quit()
|
|
log.Fatalf("SDL_CreateWindow: %s", sdl.GetError())
|
|
return
|
|
}
|
|
|
|
// ImGui init
|
|
gui := imgui.New()
|
|
defer gui.Destroy()
|
|
sdl.StartTextInput(windowHandler)
|
|
defer sdl.StopTextInput(windowHandler)
|
|
// fin on ImGui init
|
|
|
|
if err := vk.Load(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
sdlExtensions := sdl.VulkanGetInstanceExtensions()
|
|
if len(sdlExtensions) == 0 {
|
|
log.Fatal("sdlVulkanGetInstanceExtensions is 0")
|
|
return
|
|
}
|
|
var vkLayers []string
|
|
vkExtensions := append(sdlExtensions, vk.ExtDebugUtils)
|
|
vkInstance, err := vk.CreateInstance(vk.InstanceConfig{
|
|
ApplicationName: APP_NAME,
|
|
EngineName: "no engine",
|
|
Extensions: vkExtensions,
|
|
Layers: vkLayers,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("%s", err)
|
|
panic(err)
|
|
}
|
|
defer vkInstance.Destroy()
|
|
|
|
var vkSurface uint64
|
|
if !sdl.VulkanCreateSurface(windowHandler, uintptr(vkInstance), 0, &vkSurface) {
|
|
log.Fatalf("SDL_Vulkan_CreateSurface: %s", sdl.GetError())
|
|
return
|
|
}
|
|
vkSurf := vk.SurfaceKHR(vkSurface)
|
|
defer vkInstance.DestroySurface(vkSurf)
|
|
|
|
devices, err := vkInstance.EnumeratePhysicalDevices()
|
|
if err != nil || len(devices) == 0 {
|
|
panic("No Vulkan devices")
|
|
}
|
|
// List GPU's and exit
|
|
if args.ListGPU {
|
|
fmt.Println("Available Vulkan physical devices:")
|
|
fmt.Println("id name (type)")
|
|
for i, pd := range devices {
|
|
info := pd.Info()
|
|
fmt.Printf("%2d %s (%s)\n", i, info.Name, info.Type)
|
|
}
|
|
return
|
|
}
|
|
vkPhysDevice := devices[0]
|
|
|
|
gfx, err := vkPhysDevice.GraphicsFamily()
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
if !vkPhysDevice.SurfaceSupport(gfx, vkSurf) {
|
|
log.Fatalf("graphics queue cannot present")
|
|
return
|
|
}
|
|
|
|
vkDevice, vkQueue, err := vkPhysDevice.CreateDevice(vk.DeviceConfig{
|
|
GraphicsFamily: gfx,
|
|
Extensions: []string{"VK_KHR_swapchain"},
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer vkDevice.Destroy()
|
|
|
|
var (
|
|
syncSrc *source.SyncSource
|
|
videoSrc *source.Source
|
|
audioSrc *source.AudioSource
|
|
audioStream uintptr
|
|
audioBatch uint64
|
|
aChans uint64
|
|
)
|
|
|
|
interleaveAudio := func(samples [][]byte) []byte {
|
|
frameBytes := int(audioBatch) * int(aChans) * 4
|
|
out := make([]byte, frameBytes)
|
|
for ch := uint64(0); ch < aChans; ch++ {
|
|
srcBytes := samples[ch]
|
|
for i := uint64(0); i < audioBatch; i++ {
|
|
srcOff := i * 4
|
|
dstOff := (i*aChans + ch) * 4
|
|
if srcOff+4 <= uint64(len(srcBytes)) {
|
|
copy(out[dstOff:dstOff+4], srcBytes[srcOff:srcOff+4])
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
switch {
|
|
case args.VideoFlowId != "" && args.AudioFlowId != "":
|
|
syncSrc, err = source.OpenSync(args.Domain, args.VideoFlowId, args.AudioFlowId)
|
|
if err != nil {
|
|
log.Fatalf("sync source: %v", err)
|
|
}
|
|
aChans = syncSrc.Channels()
|
|
audioBatch = uint64(syncSrc.AudioRate().Num) / uint64(syncSrc.Rate().Num)
|
|
if audioBatch == 0 {
|
|
audioBatch = 1
|
|
}
|
|
audioStream = sdl.OpenAudioDeviceStream(sdlAudioDevice, sdl.AudioSpec{
|
|
Format: sdl.AudioF32,
|
|
Channels: int32(aChans),
|
|
Freq: int32(syncSrc.AudioRate().Num / syncSrc.AudioRate().Den),
|
|
})
|
|
if audioStream == 0 {
|
|
log.Fatalf("audio: %s", sdl.GetError())
|
|
}
|
|
sdl.ResumeAudioStreamDevice(audioStream)
|
|
fmt.Printf("sync: video %dx%d audio %dch batch=%d\n",
|
|
syncSrc.Width(), syncSrc.Height(), aChans, audioBatch)
|
|
case args.VideoFlowId != "":
|
|
videoSrc, err = source.Open(args.Domain, args.VideoFlowId)
|
|
if err != nil {
|
|
log.Fatalf("source: %v", err)
|
|
}
|
|
fmt.Printf("video: %dx%d stride=%d\n", videoSrc.Width(), videoSrc.Height(), videoSrc.Stride())
|
|
|
|
default:
|
|
audioSrc, err = source.OpenAudio(args.Domain, args.AudioFlowId)
|
|
if err != nil {
|
|
log.Fatalf("audio source: %v", err)
|
|
}
|
|
aChans = audioSrc.Channels()
|
|
audioBatch = uint64(audioSrc.Rate().Num) / (100 * uint64(audioSrc.Rate().Den))
|
|
if audioBatch == 0 {
|
|
audioBatch = 1
|
|
}
|
|
audioStream = sdl.OpenAudioDeviceStream(sdlAudioDevice, sdl.AudioSpec{
|
|
Format: sdl.AudioF32,
|
|
Channels: int32(aChans),
|
|
Freq: int32(audioSrc.Rate().Num / audioSrc.Rate().Den),
|
|
})
|
|
if audioStream == 0 {
|
|
log.Fatalf("audio: %s", sdl.GetError())
|
|
}
|
|
sdl.ResumeAudioStreamDevice(audioStream)
|
|
fmt.Printf("audio: %dch %d/%d Hz\n", aChans, audioSrc.Rate().Num, audioSrc.Rate().Den)
|
|
}
|
|
|
|
defer func() {
|
|
if syncSrc != nil {
|
|
_ = syncSrc.Close()
|
|
}
|
|
if videoSrc != nil {
|
|
_ = videoSrc.Close()
|
|
}
|
|
if audioSrc != nil {
|
|
_ = audioSrc.Close()
|
|
}
|
|
}()
|
|
if audioStream != 0 {
|
|
defer sdl.DestroyAudioStream(audioStream)
|
|
}
|
|
|
|
var r *renderer.Renderer
|
|
if args.VideoFlowId != "" {
|
|
var w, h, stride uint32
|
|
if syncSrc != nil {
|
|
w, h, stride = syncSrc.Width(), syncSrc.Height(), syncSrc.Stride()
|
|
} else {
|
|
w, h, stride = videoSrc.Width(), videoSrc.Height(), videoSrc.Stride()
|
|
}
|
|
r, err = renderer.New(renderer.Config{
|
|
PhysDevice: vkPhysDevice,
|
|
Device: vkDevice,
|
|
Queue: vkQueue,
|
|
Surface: vkSurf,
|
|
Window: windowHandler,
|
|
GraphicsFamily: gfx,
|
|
VideoWidth: w,
|
|
VideoHeight: h,
|
|
VideoStride: stride,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer r.Destroy()
|
|
guiBackend, err := imgui.NewVulkanBackend(
|
|
vkPhysDevice, vkDevice, vkQueue, r.CmdPool(), r.RenderPass())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer guiBackend.Destroy()
|
|
r.ImGuiDraw = func(cmd vk.CommandBuffer) {
|
|
guiBackend.RecordDraw(cmd, gui.LastDrawData())
|
|
}
|
|
defer vkDevice.WaitIdle()
|
|
}
|
|
|
|
// GUI state (accessible from doReconnect + goroutine)
|
|
var (
|
|
domainStr string = args.Domain
|
|
videoStr string = args.VideoFlowId
|
|
audioStr string = args.AudioFlowId
|
|
showStats bool = true
|
|
)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
type reconnectParams struct {
|
|
domain string
|
|
video string
|
|
audio string
|
|
}
|
|
// One control channel: grant (empty params) or reconnect (with params).
|
|
control := make(chan reconnectParams, 1)
|
|
staged := make(chan uint64)
|
|
|
|
reopen := func(params reconnectParams) error {
|
|
// Close current sources
|
|
if syncSrc != nil {
|
|
_ = syncSrc.Close()
|
|
syncSrc = nil
|
|
}
|
|
if videoSrc != nil {
|
|
_ = videoSrc.Close()
|
|
videoSrc = nil
|
|
}
|
|
if audioSrc != nil {
|
|
_ = audioSrc.Close()
|
|
audioSrc = nil
|
|
}
|
|
// Try once. Return error if fails — caller loops back to select
|
|
// and can pick up new reconnect params or a new grant.
|
|
if params.video != "" && params.audio != "" {
|
|
s, e := source.OpenSync(params.domain, params.video, params.audio)
|
|
if e == nil {
|
|
if r != nil {
|
|
newSize := vk.DeviceSize(s.Stride()) * vk.DeviceSize(s.Height())
|
|
if newSize != r.FrameSize() {
|
|
if e = r.RecreateBuffers(newSize); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
syncSrc = s
|
|
aChans = s.Channels()
|
|
audioBatch = uint64(s.AudioRate().Num) / uint64(s.Rate().Num)
|
|
if audioBatch == 0 {
|
|
audioBatch = 1
|
|
}
|
|
return nil
|
|
}
|
|
return e
|
|
} else if params.video != "" {
|
|
s, e := source.Open(params.domain, params.video)
|
|
if e == nil {
|
|
if r != nil {
|
|
newSize := vk.DeviceSize(s.Stride()) * vk.DeviceSize(s.Height())
|
|
if newSize != r.FrameSize() {
|
|
if e = r.RecreateBuffers(newSize); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
videoSrc = s
|
|
return nil
|
|
}
|
|
return e
|
|
} else if params.audio != "" {
|
|
s, e := source.OpenAudio(params.domain, params.audio)
|
|
if e == nil {
|
|
audioSrc = s
|
|
aChans = s.Channels()
|
|
audioBatch = uint64(s.Rate().Num) / (100 * uint64(s.Rate().Den))
|
|
if audioBatch == 0 {
|
|
audioBatch = 1
|
|
}
|
|
return nil
|
|
}
|
|
return e
|
|
}
|
|
return fmt.Errorf("reopen: no flow specified")
|
|
}
|
|
|
|
doReconnect := func() {
|
|
select {
|
|
case <-control:
|
|
default:
|
|
}
|
|
control <- reconnectParams{domain: domainStr, video: videoStr, audio: audioStr}
|
|
}
|
|
|
|
go func() {
|
|
// Audio-only mode: independent loop, no grant/staged handshake.
|
|
if audioSrc != nil && syncSrc == nil && videoSrc == nil {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case params := <-control:
|
|
if params.video != "" || params.audio != "" {
|
|
if rerr := reopen(params); rerr != nil {
|
|
if errors.Is(rerr, context.Canceled) {
|
|
return
|
|
}
|
|
log.Printf("source: reopen failed: %v, retrying", rerr)
|
|
select {
|
|
case <-time.After(500 * time.Millisecond):
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
select {
|
|
case control <- params:
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
continue
|
|
default:
|
|
}
|
|
queued := sdl.GetAudioStreamQueued(audioStream)
|
|
maxQueued := int32(audioBatch) * int32(aChans) * 4 * 20
|
|
if queued > maxQueued {
|
|
select {
|
|
case <-time.After(10 * time.Millisecond):
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
f, err := audioSrc.NextAudio(ctx, audioBatch, 20*time.Millisecond)
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) {
|
|
return
|
|
}
|
|
log.Printf("source: %v", err)
|
|
params := reconnectParams{domain: domainStr, video: videoStr, audio: audioStr}
|
|
select {
|
|
case <-control:
|
|
default:
|
|
}
|
|
select {
|
|
case <-time.After(500 * time.Millisecond):
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
select {
|
|
case control <- params:
|
|
default:
|
|
}
|
|
continue
|
|
}
|
|
if f.Samples != nil && audioStream != 0 {
|
|
sdl.PutAudioStreamData(audioStream, interleaveAudio(f.Samples))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Video (with or without sync) mode: grant/staged handshake.
|
|
for {
|
|
params := <-control
|
|
if params.video != "" || params.audio != "" {
|
|
// Reconnect request from Connect button or auto-retry.
|
|
select {
|
|
case <-control: // drain any pending grant
|
|
default:
|
|
}
|
|
if rerr := reopen(params); rerr != nil {
|
|
if errors.Is(rerr, context.Canceled) {
|
|
return
|
|
}
|
|
log.Printf("source: reopen failed: %v, retrying", rerr)
|
|
select {
|
|
case <-time.After(500 * time.Millisecond):
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
select {
|
|
case control <- params:
|
|
default:
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
var payload []byte
|
|
var grainIdx uint64
|
|
|
|
if syncSrc != nil {
|
|
vFrame, aFrame, err := syncSrc.NextSync(ctx, audioBatch, 200*time.Millisecond)
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) {
|
|
return
|
|
}
|
|
log.Printf("source: %v", err)
|
|
// Drain any pending grant, then send reconnect.
|
|
select {
|
|
case <-control:
|
|
default:
|
|
}
|
|
select {
|
|
case control <- reconnectParams{domain: domainStr, video: videoStr, audio: audioStr}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
payload = vFrame.Payload
|
|
grainIdx = vFrame.Index
|
|
if aFrame.Samples != nil && audioStream != 0 {
|
|
sdl.PutAudioStreamData(audioStream, interleaveAudio(aFrame.Samples))
|
|
}
|
|
|
|
} else if videoSrc != nil {
|
|
f, err := videoSrc.NextCtx(ctx, 200*time.Millisecond)
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) {
|
|
return
|
|
}
|
|
log.Printf("source: %v", err)
|
|
select {
|
|
case <-control:
|
|
default:
|
|
}
|
|
select {
|
|
case control <- reconnectParams{domain: domainStr, video: videoStr, audio: audioStr}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
payload = f.Payload
|
|
grainIdx = f.Index
|
|
}
|
|
|
|
if r != nil {
|
|
vk.CopyToMapped(r.StagingMapped(), payload)
|
|
}
|
|
select {
|
|
case staged <- grainIdx:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
running := true
|
|
resized := false
|
|
granted := false
|
|
fullscreen := args.IsFullscreen
|
|
if fullscreen {
|
|
sdl.SetWindowFullscreen(windowHandler, true)
|
|
}
|
|
var (
|
|
fps float64
|
|
lastIndex uint64
|
|
dropped uint64
|
|
frameCount uint64
|
|
lastReport time.Time
|
|
lastFrame time.Time
|
|
)
|
|
lastFrame = time.Now()
|
|
|
|
for running {
|
|
frameStart := time.Now()
|
|
var event [128]byte
|
|
for sdl.PollEvent(unsafe.Pointer(&event[0])) {
|
|
eventType := *(*uint32)(unsafe.Pointer(&event[0]))
|
|
switch eventType {
|
|
case sdl.EventQuit:
|
|
running = false
|
|
case sdl.EventWindowResized, sdl.EventPixelSizeChanged:
|
|
resized = true
|
|
case sdl.EventKeyDown:
|
|
if gui.IO().WantCaptureKeyboard() {
|
|
break
|
|
}
|
|
key := *(*int32)(unsafe.Pointer(&event[28]))
|
|
switch uint32(key) {
|
|
case sdl.KeyQ:
|
|
fallthrough
|
|
case sdl.KeyEscape:
|
|
running = false
|
|
case sdl.KeyF:
|
|
fullscreen = !fullscreen
|
|
sdl.SetWindowFullscreen(windowHandler, fullscreen)
|
|
resized = true
|
|
case sdl.KeyF1:
|
|
showStats = !showStats
|
|
}
|
|
}
|
|
gui.ProcessEvent(&event)
|
|
}
|
|
if !running {
|
|
break
|
|
}
|
|
if resized && r != nil {
|
|
if err := r.RecreateSwapchain(); err != nil {
|
|
if errors.Is(err, renderer.ErrMinimized) {
|
|
resized = true
|
|
continue
|
|
}
|
|
panic(err)
|
|
}
|
|
resized = false
|
|
}
|
|
if !granted {
|
|
select {
|
|
case control <- reconnectParams{}:
|
|
granted = true
|
|
case <-ctx.Done():
|
|
running = false
|
|
continue
|
|
}
|
|
}
|
|
var shownIndex uint64
|
|
hasFrame := false
|
|
select {
|
|
case shownIndex = <-staged:
|
|
granted = false
|
|
hasFrame = true
|
|
case <-ctx.Done():
|
|
running = false
|
|
continue
|
|
case <-time.After(100 * time.Millisecond):
|
|
// No frame staged. Reset granted so we re-grant on next iteration.
|
|
granted = false
|
|
}
|
|
|
|
// stats
|
|
if hasFrame {
|
|
if lastIndex != 0 && shownIndex > lastIndex {
|
|
if g := shownIndex - lastIndex - 1; g > 0 {
|
|
dropped += g
|
|
}
|
|
}
|
|
lastIndex = shownIndex
|
|
frameCount++
|
|
if now := time.Now(); now.Sub(lastReport) >= time.Second {
|
|
dt := now.Sub(lastReport).Seconds()
|
|
fps = float64(frameCount) / dt
|
|
fmt.Printf("fps=%.1f dropped=%d idx=%d frameTime=%.2fms\n",
|
|
fps, dropped, shownIndex, float64(now.Sub(frameStart).Microseconds())/1000.0)
|
|
frameCount = 0
|
|
dropped = 0
|
|
lastReport = now
|
|
}
|
|
}
|
|
// end of stats
|
|
if r != nil {
|
|
gui.BeginFrame(time.Since(lastFrame), int32(r.Extent().Width), int32(r.Extent().Height))
|
|
// test widget
|
|
// cimgui.Begin("Test")
|
|
if showStats {
|
|
cimgui.SetNextWindowPos(cimgui.Vec2{X: 10, Y: 10})
|
|
cimgui.SetNextWindowSize(cimgui.Vec2{X: 200, Y: 200})
|
|
cimgui.BeginV("Stats", &showStats,
|
|
cimgui.WindowFlagsNoTitleBar|cimgui.WindowFlagsNoResize|cimgui.WindowFlagsNoScrollbar)
|
|
cimgui.Text(fmt.Sprintf("FPS: %.1f", fps))
|
|
cimgui.Text(fmt.Sprintf("Dropped: %d", dropped))
|
|
cimgui.Text(fmt.Sprintf("Index: %d", shownIndex))
|
|
if videoSrc != nil {
|
|
cimgui.Text(fmt.Sprintf("Video: %dx%d %.2fp", videoSrc.Width(), videoSrc.Height(),
|
|
float32(videoSrc.Rate().Num/videoSrc.Rate().Den)))
|
|
}
|
|
if syncSrc != nil {
|
|
cimgui.Text(fmt.Sprintf("Video: %dx%d %.2fp", syncSrc.Width(), syncSrc.Height(),
|
|
float32(syncSrc.Rate().Num/syncSrc.Rate().Den)))
|
|
cimgui.Text(fmt.Sprintf("Audio: %dch %dkHz", syncSrc.Channels(), syncSrc.AudioRate().Num))
|
|
}
|
|
cimgui.Text("\nPress F1 to hide stats")
|
|
cimgui.Text("Q or Esc to quit")
|
|
cimgui.Text("F for fullscreen")
|
|
cimgui.End()
|
|
}
|
|
cimgui.Begin("Connection")
|
|
cimgui.InputTextWithHint("Domain", "/dev/shm/mxl", &domainStr, 0, nil)
|
|
cimgui.InputTextWithHint("Video UUID", "", &videoStr, 0, nil)
|
|
cimgui.InputTextWithHint("Audio UUID", "", &audioStr, 0, nil)
|
|
cimgui.Checkbox("Show stats", &showStats)
|
|
if cimgui.Button("Connect") {
|
|
doReconnect()
|
|
}
|
|
cimgui.End()
|
|
gui.EndFrame()
|
|
lastFrame = time.Now()
|
|
// end of test widget
|
|
var w, h, stride uint32
|
|
if syncSrc != nil {
|
|
w, h, stride = syncSrc.Width(), syncSrc.Height(), syncSrc.Stride()
|
|
} else if videoSrc != nil {
|
|
w, h, stride = videoSrc.Width(), videoSrc.Height(), videoSrc.Stride()
|
|
}
|
|
err := r.DrawFrame(w, h, stride)
|
|
if errors.Is(err, renderer.ErrOutOfDate) {
|
|
if rerr := r.RecreateSwapchain(); rerr != nil {
|
|
if errors.Is(rerr, renderer.ErrMinimized) {
|
|
resized = true
|
|
continue
|
|
}
|
|
panic(rerr)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
} else {
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|
|
}
|