001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.vision.camera;
006
007/** Video event. */
008public class VideoEvent {
009  /** VideoEvent kind. */
010  public enum Kind {
011    /** Unknown video event. */
012    kUnknown(0x0000),
013    /** Source Created event. */
014    kSourceCreated(0x0001),
015    /** Source Destroyed event. */
016    kSourceDestroyed(0x0002),
017    /** Source Connected event. */
018    kSourceConnected(0x0004),
019    /** Source Disconnected event. */
020    kSourceDisconnected(0x0008),
021    /** Source Video Modes Updated event. */
022    kSourceVideoModesUpdated(0x0010),
023    /** Source VideoMode Changed event. */
024    kSourceVideoModeChanged(0x0020),
025    /** Source Property Created event. */
026    kSourcePropertyCreated(0x0040),
027    /** Source Property Value Updated event. */
028    kSourcePropertyValueUpdated(0x0080),
029    /** Source Property Choices Updated event. */
030    kSourcePropertyChoicesUpdated(0x0100),
031    /** Sink Source Changed event. */
032    kSinkSourceChanged(0x0200),
033    /** Sink Created event. */
034    kSinkCreated(0x0400),
035    /** Sink Destroyed event. */
036    kSinkDestroyed(0x0800),
037    /** Sink Enabled event. */
038    kSinkEnabled(0x1000),
039    /** Sink Disabled event. */
040    kSinkDisabled(0x2000),
041    /** Network Interfaces Changed event. */
042    kNetworkInterfacesChanged(0x4000),
043    /** Telemetry Updated event. */
044    kTelemetryUpdated(0x8000),
045    /** Sink Property Created event. */
046    kSinkPropertyCreated(0x10000),
047    /** Sink Property Value Updated event. */
048    kSinkPropertyValueUpdated(0x20000),
049    /** Sink Property Choices Updated event. */
050    kSinkPropertyChoicesUpdated(0x40000),
051    /** Usb Cameras Changed event. */
052    kUsbCamerasChanged(0x80000);
053
054    private final int value;
055
056    Kind(int value) {
057      this.value = value;
058    }
059
060    /**
061     * Returns the kind value.
062     *
063     * @return The kind value.
064     */
065    public int getValue() {
066      return value;
067    }
068  }
069
070  /**
071   * Convert from the numerical representation of kind to an enum type.
072   *
073   * @param kind The numerical representation of kind
074   * @return The kind
075   */
076  public static Kind getKindFromInt(int kind) {
077    return switch (kind) {
078      case 0x0001 -> Kind.kSourceCreated;
079      case 0x0002 -> Kind.kSourceDestroyed;
080      case 0x0004 -> Kind.kSourceConnected;
081      case 0x0008 -> Kind.kSourceDisconnected;
082      case 0x0010 -> Kind.kSourceVideoModesUpdated;
083      case 0x0020 -> Kind.kSourceVideoModeChanged;
084      case 0x0040 -> Kind.kSourcePropertyCreated;
085      case 0x0080 -> Kind.kSourcePropertyValueUpdated;
086      case 0x0100 -> Kind.kSourcePropertyChoicesUpdated;
087      case 0x0200 -> Kind.kSinkSourceChanged;
088      case 0x0400 -> Kind.kSinkCreated;
089      case 0x0800 -> Kind.kSinkDestroyed;
090      case 0x1000 -> Kind.kSinkEnabled;
091      case 0x2000 -> Kind.kSinkDisabled;
092      case 0x4000 -> Kind.kNetworkInterfacesChanged;
093      case 0x10000 -> Kind.kSinkPropertyCreated;
094      case 0x20000 -> Kind.kSinkPropertyValueUpdated;
095      case 0x40000 -> Kind.kSinkPropertyChoicesUpdated;
096      case 0x80000 -> Kind.kUsbCamerasChanged;
097      default -> Kind.kUnknown;
098    };
099  }
100
101  VideoEvent(
102      int kind,
103      int source,
104      int sink,
105      String name,
106      int pixelFormat,
107      int width,
108      int height,
109      int fps,
110      int property,
111      int propertyKind,
112      int value,
113      String valueStr,
114      int listener) {
115    this.kind = getKindFromInt(kind);
116    this.sourceHandle = source;
117    this.sinkHandle = sink;
118    this.name = name;
119    this.mode = new VideoMode(pixelFormat, width, height, fps);
120    this.propertyHandle = property;
121    this.propertyKind = VideoProperty.getKindFromInt(propertyKind);
122    this.value = value;
123    this.valueStr = valueStr;
124    this.listener = listener;
125  }
126
127  /** The video event kind. */
128  public Kind kind;
129
130  /**
131   * The source handle.
132   *
133   * <p>Valid for kSource* and kSink* respectively.
134   */
135  public int sourceHandle;
136
137  /** The sink handle. */
138  public int sinkHandle;
139
140  /** Source/sink/property name. */
141  public String name;
142
143  // Fields for kSourceVideoModeChanged event.
144
145  /** New source video mode. */
146  public VideoMode mode;
147
148  // Fields for kSourceProperty* events.
149
150  /** Source property handle. */
151  public int propertyHandle;
152
153  /** Source property kind. */
154  public VideoProperty.Kind propertyKind;
155
156  /** Event value. */
157  public int value;
158
159  /** Event value as a string. */
160  public String valueStr;
161
162  /** Listener that was triggered. */
163  public int listener;
164
165  /**
166   * Returns the source associated with the event (if any).
167   *
168   * @return The source associated with the event (if any).
169   */
170  public VideoSource getSource() {
171    return new VideoSource(CameraServerJNI.copySource(sourceHandle));
172  }
173
174  /**
175   * Returns the sink associated with the event (if any).
176   *
177   * @return The sink associated with the event (if any).
178   */
179  public VideoSink getSink() {
180    return new VideoSink(CameraServerJNI.copySink(sinkHandle));
181  }
182
183  /**
184   * Returns the property associated with the event (if any).
185   *
186   * @return The property associated with the event (if any).
187   */
188  public VideoProperty getProperty() {
189    return new VideoProperty(propertyHandle, propertyKind);
190  }
191}