package main import ( "context" "errors" "fmt" "io" "log" "mxl-player/internal/imgui" "mxl-player/internal/playback" "mxl-player/internal/renderer" "mxl-player/internal/sdl" "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 ) const ( placeholderWidth uint32 = 1 placeholderHeight uint32 = 1 placeholderStride uint32 = 4 ) const ( initialRetryDelay = 500 * time.Millisecond maxRetryDelay = 5 * time.Second ) 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 ] [-v ] [-a ] [options]") fmt.Println(" [-g ] [-p ] [--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 ] [-v ] [-a ] [options]") fmt.Fprintln(w, "Try 'mxl-player -h' for more information.") } func checkMXLargs(args appArgs) { checkDomain := func(label, domain string) { if domain == "" { fmt.Fprintf(os.Stderr, "%s domain is required when its UUID is configured\n", label) printUsage(os.Stderr) os.Exit(2) } info, err := os.Stat(domain) if err != nil || !info.IsDir() { fmt.Fprintf(os.Stderr, "Invalid %s MXL domain: %s\n", label, domain) fmt.Fprintln(os.Stderr, "Domain must be a directory in tmpfs") printUsage(os.Stderr) os.Exit(2) } } if args.VideoFlowId != "" { checkDomain("video", args.VideoDomain) } if args.AudioFlowId != "" { checkDomain("audio", args.AudioDomain) } } 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.SortFlags = false flagSet.Usage = func() { printUsage(os.Stderr) } flagSet.BoolVarP(&args.ShowHelp, "help", "h", false, "Show help message and exit") flagSet.StringVarP( &args.Domain, "domain", "d", "", "Default MXL domain for feeds without a specific domain", ) flagSet.StringVar(&args.VideoDomain, "video-domain", "", "MXL domain for the video feed") flagSet.StringVar(&args.AudioDomain, "audio-domain", "", "MXL domain for the audio feed") flagSet.StringVarP(&args.VideoFlowId, "video", "v", "", "Video flow UUID") flagSet.StringVarP(&args.AudioFlowId, "audio", "a", "", "Audio flow UUID") flagSet.BoolVarP( &args.SyncRequested, "sync", "s", false, "Start configured audio and video as a synchronized group", ) flagSet.IntVar( &args.MaxAttempts, "max-attempts", 0, "Maximum connection attempts per playback lifecycle; 0 retries indefinitely", ) 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.MaxAttempts < 0 { fmt.Fprintln(os.Stderr, "--max-attempts cannot be negative") printUsage(os.Stderr) os.Exit(2) } retryPolicy := playback.RetryPolicy{ MaxAttempts: args.MaxAttempts, InitialDelay: initialRetryDelay, MaxDelay: maxRetryDelay, } if err := retryPolicy.Validate(); err != nil { fmt.Fprintln(os.Stderr, "invalid retry configuration:", err) os.Exit(2) } if args.VideoDomain == "" { args.VideoDomain = args.Domain } if args.AudioDomain == "" { args.AudioDomain = args.Domain } if !args.ListAudio && !args.ListGPU { checkMXLargs(args) } 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() // Create renderer r, err := renderer.New(renderer.Config{ PhysDevice: vkPhysDevice, Device: vkDevice, Queue: vkQueue, Surface: vkSurf, Window: windowHandler, GraphicsFamily: gfx, VideoWidth: placeholderWidth, VideoHeight: placeholderHeight, VideoStride: placeholderStride, }) if err != nil { panic(err) } defer r.Destroy() // Create GUI backend 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()) } if err := r.StageFrame( []byte{0, 0, 0, 255}, placeholderWidth, placeholderHeight, placeholderStride, ); err != nil { panic(err) } defer vkDevice.WaitIdle() // GUI state (accessible from doReconnect + goroutine) var ( videoDomainStr string = args.VideoDomain audioDomainStr string = args.AudioDomain videoStr string = args.VideoFlowId audioStr string = args.AudioFlowId showStats bool = true ) videoActive := args.VideoFlowId != "" audioActive := args.AudioFlowId != "" ctx, cancel := context.WithCancel(context.Background()) defer cancel() player, err := newPlayerPlayback(sdlAudioDevice, retryPolicy) if err != nil { panic(err) } videoBridge := player.Video statusStore := player.Status syncRequested := args.SyncRequested enqueueCommand := func(command playback.SessionCommand) { select { case player.Commands <- command: default: log.Printf("playback command queue is full; ignoring command %d", command.Kind) } } doReconnect := func() { videoActive = videoStr != "" audioActive = audioStr != "" videoConfig := playback.FeedConfig{} if videoActive { videoConfig = playback.FeedConfig{ Domain: videoDomainStr, UUID: videoStr, Active: true, } } audioConfig := playback.FeedConfig{} if audioActive { audioConfig = playback.FeedConfig{ Domain: audioDomainStr, UUID: audioStr, Active: true, } } enqueueCommand(playback.SessionCommand{ Kind: playback.CommandSetSession, Session: playback.SessionConfig{ Video: videoConfig, Audio: audioConfig, SyncRequested: syncRequested, }, }) } playbackDone := make(chan error, 1) go func() { playbackDone <- player.Controller.Run( ctx, args.playbackConfig(), player.Commands, ) }() running := true resized := false fullscreen := args.IsFullscreen if fullscreen { sdl.SetWindowFullscreen(windowHandler, true) } var ( displayedVideoWidth uint32 = placeholderWidth displayedVideoHeight uint32 = placeholderHeight displayedVideoStride uint32 = placeholderStride hasDisplayedVideo bool = false 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 } var shownIndex uint64 hasFrame := false frameCtx, frameCancel := context.WithTimeout(ctx, 100*time.Millisecond) pendingFrame, frameErr := videoBridge.Next(frameCtx) frameCancel() if pendingFrame != nil { var stageErr error if r != nil { stageErr = r.StageFrame( pendingFrame.Frame.Payload, pendingFrame.Frame.Width, pendingFrame.Frame.Height, pendingFrame.Frame.Stride, ) } // Release the borrowed payload before reacting to a staging error pendingFrame.Complete(stageErr) if stageErr != nil { panic(stageErr) } shownIndex = pendingFrame.Frame.Index displayedVideoWidth = pendingFrame.Frame.Width displayedVideoHeight = pendingFrame.Frame.Height displayedVideoStride = pendingFrame.Frame.Stride hasDisplayedVideo = true hasFrame = true } else if frameErr != nil && !errors.Is(frameErr, context.DeadlineExceeded) && !errors.Is(frameErr, context.Canceled) { panic(frameErr) } // 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 hasDisplayedVideo { cimgui.Text(fmt.Sprintf( "Video: %dx%d", displayedVideoWidth, displayedVideoHeight, )) } 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("Video domain", "/dev/shm/mxl", &videoDomainStr, 0, nil) cimgui.InputTextWithHint("Audio domain", "/dev/shm/mxl", &audioDomainStr, 0, nil) cimgui.InputTextWithHint("Video UUID", "", &videoStr, 0, nil) cimgui.InputTextWithHint("Audio UUID", "", &audioStr, 0, nil) if snapshot, ok := player.Controller.Snapshot(); ok { videoActive = snapshot.Desired.Video.Active audioActive = snapshot.Desired.Audio.Active syncRequested = snapshot.Desired.SyncRequested } if cimgui.Button("Connect") { doReconnect() } cimgui.SameLine() cimgui.Checkbox("Show stats", &showStats) if cimgui.Checkbox("Synchronize", &syncRequested) { kind := playback.CommandDisableSync if syncRequested { kind = playback.CommandEnableSync } enqueueCommand(playback.SessionCommand{Kind: kind}) } if videoActive { if cimgui.Button("Stop video") { videoActive = false enqueueCommand(playback.SessionCommand{Kind: playback.CommandStopVideo}) } } if !videoActive && videoStr != "" { cimgui.SameLine() if cimgui.Button("Resume video") { videoActive = true enqueueCommand(playback.SessionCommand{Kind: playback.CommandResumeVideo}) } } if videoStr != "" { if cimgui.Button("Remove video") { videoActive = false videoStr = "" enqueueCommand(playback.SessionCommand{Kind: playback.CommandRemoveVideo}) } } if videoActive { cimgui.Text("Video desired: active") } else if videoStr != "" { cimgui.Text("Video desired: stopped") } else { cimgui.Text("Video desired: not configured") } if status, ok := statusStore.Snapshot(playback.UnitVideo); ok { cimgui.Text(fmt.Sprintf("Video actual: %s", status.State)) cimgui.Text(fmt.Sprintf("Attempt: %d, failed: %d", status.Attempt, status.FailedAttempts)) if status.RetryIn > 0 { cimgui.Text(fmt.Sprintf("Retry in: %s", status.RetryIn.Round(time.Millisecond))) } if status.Err != nil { cimgui.TextWrapped(status.Err.Error()) } } else { cimgui.Text("Video actual: not started") } if audioActive { if cimgui.Button("Stop audio") { audioActive = false enqueueCommand(playback.SessionCommand{Kind: playback.CommandStopAudio}) } } if !audioActive && audioStr != "" { if cimgui.Button("Resume audio") { audioActive = true enqueueCommand(playback.SessionCommand{Kind: playback.CommandResumeAudio}) } } if audioStr != "" { if cimgui.Button("Remove audio") { audioActive = false audioStr = "" enqueueCommand(playback.SessionCommand{Kind: playback.CommandRemoveAudio}) } } if audioActive { cimgui.Text("Audio desired: active") } else if audioStr != "" { cimgui.Text("Audio desired: stopped") } else { cimgui.Text("Audio desired: not configured") } if status, ok := statusStore.Snapshot(playback.UnitAudio); ok { cimgui.Text(fmt.Sprintf("Audio actual: %s", status.State)) cimgui.Text(fmt.Sprintf("Attempt: %d, failed: %d", status.Attempt, status.FailedAttempts)) if status.RetryIn > 0 { cimgui.Text(fmt.Sprintf("Retry in: %s", status.RetryIn.Round(time.Millisecond))) } if status.Err != nil { cimgui.TextWrapped(status.Err.Error()) } } else { cimgui.Text("Audio actual: not started") } if status, ok := statusStore.Snapshot(playback.UnitSync); ok { cimgui.Text(fmt.Sprintf("Sync actual: %s", status.State)) if status.Err != nil { cimgui.TextWrapped(status.Err.Error()) } } cimgui.End() gui.EndFrame() lastFrame = time.Now() // end of test widget err := r.DrawFrame( displayedVideoWidth, displayedVideoHeight, displayedVideoStride, ) 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) } } cancel() if err := <-playbackDone; err != nil && !errors.Is(err, context.Canceled) { log.Printf("playback controller: %v", err) } if err := player.Close(); err != nil { log.Printf("close playback: %v", err) } }