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