usbkvm.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "imuslab.com/remdeskvm/remdeskd/mod/remdesaux"
  9. "imuslab.com/remdeskvm/remdeskd/mod/usbcapture"
  10. )
  11. func startUsbKvmMode() error {
  12. log.Println("Starting in USB KVM mode...")
  13. //Start the HID controller
  14. err := usbKVM.Connect()
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. //Start auxiliary MCU connections
  19. auxMCU, err = remdesaux.NewAuxOutbandController(*usbAuxilaryDeviceName, *usbAuxBaudRate)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. //Try get the UUID from the auxiliary MCU
  24. uuid, err := auxMCU.GetUUID()
  25. if err != nil {
  26. log.Println("Get UUID failed:", err)
  27. } else {
  28. log.Println("Auxiliary MCU found with UUID:", uuid)
  29. //Register the AUX routes if success
  30. registerLocalAuxRoutes()
  31. }
  32. // Initiate the video capture device
  33. videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
  34. DeviceName: *captureDeviceName,
  35. AudioConfig: usbcapture.GetDefaultAudioConfig(),
  36. })
  37. if err != nil {
  38. log.Fatalf("Failed to create video capture instance: %v", err)
  39. }
  40. //Get device information for debug
  41. usbcapture.PrintV4L2FormatInfo(*captureDeviceName)
  42. //Start the video capture device
  43. err = videoCapture.StartVideoCapture(&usbcapture.CaptureResolution{
  44. Width: 1920,
  45. Height: 1080,
  46. FPS: 10,
  47. })
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. // Handle program exit to close the HID controller
  52. c := make(chan os.Signal, 1)
  53. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  54. go func() {
  55. <-c
  56. log.Println("Shutting down usbKVM...")
  57. if usbKVM != nil {
  58. //usbKVM.Close()
  59. }
  60. if auxMCU != nil {
  61. auxMCU.Close()
  62. }
  63. log.Println("Shutting down capture device...")
  64. if videoCapture != nil {
  65. videoCapture.Close()
  66. }
  67. os.Exit(0)
  68. }()
  69. // Register the API routes
  70. registerAPIRoutes()
  71. addr := ":9000"
  72. log.Printf("Serving on http://localhost%s\n", addr)
  73. err = http.ListenAndServe(addr, nil)
  74. return err
  75. }