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