handlers.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dezukvm
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. func (d *DezukVM) HandleVideoStreams(w http.ResponseWriter, r *http.Request, instanceUuid string) {
  7. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  8. if err != nil {
  9. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  10. return
  11. }
  12. targetInstance.usbCaptureDevice.ServeVideoStream(w, r)
  13. }
  14. func (d *DezukVM) HandleAudioStreams(w http.ResponseWriter, r *http.Request, instanceUuid string) {
  15. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  16. if err != nil {
  17. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  18. return
  19. }
  20. pcmDevicePath := targetInstance.captureConfig.AudioDeviceName
  21. targetInstance.usbCaptureDevice.AudioStreamingHandler(w, r, pcmDevicePath)
  22. }
  23. func (d *DezukVM) HandleHIDEvents(w http.ResponseWriter, r *http.Request, instanceUuid string) {
  24. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  25. if err != nil {
  26. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  27. return
  28. }
  29. targetInstance.usbKVMController.HIDWebSocketHandler(w, r)
  30. }
  31. // HandleMassStorageSideSwitch handles the request to switch the USB mass storage side.
  32. // there is only two state for the USB mass storage side, KVM side or Remote side.
  33. // isKvmSide = true means switch to KVM side, otherwise switch to Remote side.
  34. func (d *DezukVM) HandleMassStorageSideSwitch(w http.ResponseWriter, r *http.Request, instanceUuid string, isKvmSide bool) {
  35. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  36. if err != nil {
  37. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  38. return
  39. }
  40. if targetInstance.auxMCUController == nil {
  41. http.Error(w, "Auxiliary MCU controller not initialized or missing", http.StatusInternalServerError)
  42. return
  43. }
  44. if isKvmSide {
  45. err = targetInstance.auxMCUController.SwitchUSBToKVM()
  46. } else {
  47. err = targetInstance.auxMCUController.SwitchUSBToRemote()
  48. }
  49. if err != nil {
  50. http.Error(w, "Failed to switch USB mass storage side: "+err.Error(), http.StatusInternalServerError)
  51. return
  52. }
  53. w.WriteHeader(http.StatusOK)
  54. w.Write([]byte("OK"))
  55. }
  56. func (d *DezukVM) HandleListInstances(w http.ResponseWriter, r *http.Request) {
  57. instances := []map[string]interface{}{}
  58. for _, instance := range d.UsbKvmInstance {
  59. instances = append(instances, map[string]interface{}{
  60. "uuid": instance.UUID(),
  61. "video_capture_dev": instance.Config.VideoCaptureDevicePath,
  62. "audio_capture_dev": instance.Config.AudioCaptureDevicePath,
  63. "video_resolution_width": instance.Config.CaptureVideoResolutionWidth,
  64. "video_resolution_height": instance.Config.CaptureeVideoResolutionHeight,
  65. "video_framerate": instance.Config.CaptureeVideoFPS,
  66. "audio_sample_rate": instance.Config.CaptureAudioSampleRate,
  67. "audio_channels": instance.Config.CaptureAudioChannels,
  68. "stream_info": instance.usbCaptureDevice.GetStreamInfo(),
  69. "usb_kvm_device": instance.Config.USBKVMDevicePath,
  70. "aux_mcu_device": instance.Config.AuxMCUDevicePath,
  71. "usb_mass_storage_side": instance.auxMCUController.GetUSBMassStorageSide(),
  72. })
  73. }
  74. w.Header().Set("Content-Type", "application/json")
  75. json.NewEncoder(w).Encode(instances)
  76. }