usbcapture.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package usbcapture
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. // NewInstance creates a new video capture instance
  7. func NewInstance(config *Config) (*Instance, error) {
  8. if config == nil {
  9. return nil, fmt.Errorf("config cannot be nil")
  10. }
  11. //Check if the video device exists
  12. if _, err := os.Stat(config.VideoDeviceName); os.IsNotExist(err) {
  13. return nil, fmt.Errorf("video device %s does not exist", config.VideoDeviceName)
  14. } else if err != nil {
  15. return nil, fmt.Errorf("failed to check video device: %w", err)
  16. }
  17. //Check if the device file actualy points to a video device
  18. isValidDevice, err := checkVideoCaptureDevice(config.VideoDeviceName)
  19. if err != nil {
  20. return nil, fmt.Errorf("failed to check video device: %w", err)
  21. }
  22. if !isValidDevice {
  23. return nil, fmt.Errorf("device %s is not a video capture device", config.VideoDeviceName)
  24. }
  25. //Get the supported resolutions of the video device
  26. formatInfo, err := GetV4L2FormatInfo(config.VideoDeviceName)
  27. if err != nil {
  28. return nil, fmt.Errorf("failed to get video device format info: %w", err)
  29. }
  30. if len(formatInfo) == 0 {
  31. return nil, fmt.Errorf("no supported formats found for device %s", config.VideoDeviceName)
  32. }
  33. return &Instance{
  34. Config: config,
  35. Capturing: false,
  36. SupportedResolutions: formatInfo,
  37. // Videos
  38. camera: nil,
  39. pixfmt: 0,
  40. width: 0,
  41. height: 0,
  42. streamInfo: "",
  43. //Audio
  44. audiostopchan: make(chan bool, 1),
  45. // Access control
  46. videoTakeoverChan: make(chan bool, 1),
  47. accessCount: 0,
  48. }, nil
  49. }
  50. // GetStreamInfo returns the stream information string
  51. func (i *Instance) GetStreamInfo() string {
  52. return i.streamInfo
  53. }
  54. // IsCapturing checks if the camera is currently capturing video
  55. func (i *Instance) IsCapturing() bool {
  56. return i.Capturing
  57. }
  58. // IsAudioStreaming checks if the audio is currently being captured
  59. func (i *Instance) IsAudioStreaming() bool {
  60. return i.isAudioStreaming
  61. }
  62. // Close closes the camera device and releases resources
  63. func (i *Instance) Close() error {
  64. if i.camera != nil {
  65. i.StopCapture()
  66. }
  67. return nil
  68. }