main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "embed"
  4. "flag"
  5. "io/fs"
  6. "log"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. "imuslab.com/remdeskvm/remdeskd/mod/remdeshid"
  12. "imuslab.com/remdeskvm/remdeskd/mod/usbcapture"
  13. )
  14. const development = true
  15. var (
  16. usbKVMDeviceName = flag.String("usbkvm", "/dev/ttyUSB0", "USB KVM device file path")
  17. usbKVMBaudRate = flag.Int("baudrate", 115200, "USB KVM baud rate")
  18. captureDeviceName = flag.String("capture", "/dev/video0", "Video capture device file path")
  19. usbKVM *remdeshid.Controller
  20. videoCapture *usbcapture.Instance
  21. )
  22. /* Web Server Static Files */
  23. //go:embed www
  24. var embeddedFiles embed.FS
  25. var webfs http.FileSystem
  26. func init() {
  27. // Initiate the web server static files
  28. if development {
  29. webfs = http.Dir("./www")
  30. } else {
  31. // Embed the ./www folder and trim the prefix
  32. subFS, err := fs.Sub(embeddedFiles, "www")
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. webfs = http.FS(subFS)
  37. }
  38. }
  39. func main() {
  40. flag.Parse()
  41. // Initiate the HID controller
  42. usbKVM = remdeshid.NewHIDController(&remdeshid.Config{
  43. PortName: *usbKVMDeviceName,
  44. BaudRate: *usbKVMBaudRate,
  45. })
  46. //Start the HID controller
  47. err := usbKVM.Connect()
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. // Initiate the video capture device
  52. videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
  53. DeviceName: *captureDeviceName,
  54. })
  55. if err != nil {
  56. log.Fatalf("Failed to create video capture instance: %v", err)
  57. }
  58. //Get device information for debug
  59. usbcapture.PrintV4L2FormatInfo(*captureDeviceName)
  60. //Start the video capture device
  61. err = videoCapture.StartVideoCapture(&usbcapture.CaptureResolution{
  62. Width: 1920,
  63. Height: 1080,
  64. FPS: 25,
  65. })
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. // Handle program exit to close the HID controller
  70. c := make(chan os.Signal, 1)
  71. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  72. go func() {
  73. <-c
  74. log.Println("Shutting down usbKVM...")
  75. //usbKVM.Close() //To fix close stuck layer
  76. log.Println("Shutting down capture device...")
  77. videoCapture.Close()
  78. os.Exit(0)
  79. }()
  80. // Start the web server
  81. http.Handle("/", http.FileServer(webfs))
  82. http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
  83. http.HandleFunc("/stream", videoCapture.ServeVideoStream)
  84. addr := ":9000"
  85. log.Printf("Serving on http://localhost%s\n", addr)
  86. log.Fatal(http.ListenAndServe(addr, nil))
  87. }