dezukvm.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package dezukvm
  2. import (
  3. "errors"
  4. "imuslab.com/dezukvm/dezukvmd/mod/usbcapture"
  5. )
  6. // NewKvmHostInstance creates a new instance of DezukVM, which can manage multiple USB KVM devices.
  7. func NewKvmHostInstance(option *RuntimeOptions) *DezukVM {
  8. return &DezukVM{
  9. UsbKvmInstance: []*UsbKvmDeviceInstance{},
  10. occupiedUUIDs: make(map[string]bool),
  11. option: option,
  12. }
  13. }
  14. // AddUsbKvmDevice adds a new USB KVM device instance to the DezukVM manager.
  15. func (d *DezukVM) AddUsbKvmDevice(config *UsbKvmDeviceOption) error {
  16. //Build the capture config from the device option
  17. // Audio config
  18. if config.AudioCaptureDevicePath == "" {
  19. return errors.New("audio capture device path is not specified")
  20. }
  21. defaultAudioConfig := usbcapture.GetDefaultAudioConfig()
  22. if config.CaptureAudioSampleRate == 0 {
  23. config.CaptureAudioSampleRate = defaultAudioConfig.SampleRate
  24. }
  25. if config.CaptureAudioChannels == 0 {
  26. config.CaptureAudioChannels = defaultAudioConfig.Channels
  27. }
  28. if config.CaptureAudioBytesPerSample == 0 {
  29. config.CaptureAudioBytesPerSample = defaultAudioConfig.BytesPerSample
  30. }
  31. if config.CaptureAudioFrameSize == 0 {
  32. config.CaptureAudioFrameSize = defaultAudioConfig.FrameSize
  33. }
  34. //Remap the audio config
  35. audioCaptureCfg := &usbcapture.AudioConfig{
  36. SampleRate: config.CaptureAudioSampleRate,
  37. Channels: config.CaptureAudioChannels,
  38. BytesPerSample: config.CaptureAudioBytesPerSample,
  39. FrameSize: config.CaptureAudioFrameSize,
  40. }
  41. //Setup video capture configs
  42. if config.VideoCaptureDevicePath == "" {
  43. return errors.New("video capture device path is not specified")
  44. }
  45. if config.CaptureVideoResolutionWidth == 0 {
  46. config.CaptureVideoResolutionWidth = 1920
  47. }
  48. if config.CaptureeVideoResolutionHeight == 0 {
  49. config.CaptureeVideoResolutionHeight = 1080
  50. }
  51. if config.CaptureeVideoFPS == 0 {
  52. config.CaptureeVideoFPS = 25
  53. }
  54. // capture config
  55. captureCfg := &usbcapture.Config{
  56. VideoDeviceName: config.VideoCaptureDevicePath,
  57. AudioDeviceName: config.AudioCaptureDevicePath,
  58. AudioConfig: audioCaptureCfg,
  59. }
  60. // video resolution config
  61. videoResolutionConfig := &usbcapture.CaptureResolution{
  62. Width: config.CaptureVideoResolutionWidth,
  63. Height: config.CaptureeVideoResolutionHeight,
  64. FPS: config.CaptureeVideoFPS,
  65. }
  66. instance := &UsbKvmDeviceInstance{
  67. Config: config,
  68. captureConfig: captureCfg,
  69. videoResoltuionConfig: videoResolutionConfig,
  70. uuid: "", // Will be set when starting the instance
  71. usbKVMController: nil,
  72. auxMCUController: nil,
  73. usbCaptureDevice: nil,
  74. parent: d,
  75. }
  76. d.UsbKvmInstance = append(d.UsbKvmInstance, instance)
  77. return nil
  78. }
  79. // RemoveUsbKvmDevice removes a USB KVM device instance by its UUID.
  80. func (d *DezukVM) RemoveUsbKvmDevice(uuid string) error {
  81. for i, dev := range d.UsbKvmInstance {
  82. if dev.UUID() == uuid {
  83. d.UsbKvmInstance = append(d.UsbKvmInstance[:i], d.UsbKvmInstance[i+1:]...)
  84. return nil
  85. }
  86. }
  87. return errors.New("target USB KVM device not found")
  88. }
  89. func (d *DezukVM) StartAllUsbKvmDevices() error {
  90. for _, instance := range d.UsbKvmInstance {
  91. err := instance.Start()
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }
  98. func (d *DezukVM) StopAllUsbKvmDevices() error {
  99. for _, instance := range d.UsbKvmInstance {
  100. err := instance.Stop()
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. }
  107. func (d *DezukVM) GetInstanceByUUID(uuid string) (*UsbKvmDeviceInstance, error) {
  108. for _, instance := range d.UsbKvmInstance {
  109. if instance.UUID() == uuid {
  110. return instance, nil
  111. }
  112. }
  113. return nil, errors.New("instance with specified UUID not found")
  114. }
  115. func (d *DezukVM) Close() error {
  116. return d.StopAllUsbKvmDevices()
  117. }