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