001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2016. All Rights Reserved.                             */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.cscore;
009
010/// A source for video that provides a sequence of frames.  Each frame may
011/// consist of multiple images (e.g. from a stereo or depth camera); these
012/// are called channels.
013public class VideoSource {
014  public enum Kind {
015    kUnknown(0), kUsb(1), kHttp(2), kCv(4);
016    private int value;
017
018    private Kind(int value) {
019      this.value = value;
020    }
021
022    public int getValue() {
023      return value;
024    }
025  }
026
027  public static Kind getKindFromInt(int kind) {
028    switch (kind) {
029      case 1: return Kind.kUsb;
030      case 2: return Kind.kHttp;
031      case 4: return Kind.kCv;
032      default: return Kind.kUnknown;
033    }
034  }
035
036  protected VideoSource(int handle) {
037    m_handle = handle;
038  }
039
040  public synchronized void free() {
041    if (m_handle != 0) {
042      CameraServerJNI.releaseSource(m_handle);
043    }
044    m_handle = 0;
045  }
046
047  public boolean isValid() {
048    return m_handle != 0;
049  }
050
051  public int getHandle() {
052    return m_handle;
053  }
054
055  public boolean equals(Object other) {
056    if (this == other) return true;
057    if (other == null) return false;
058    if (getClass() != other.getClass()) return false;
059    VideoSource source = (VideoSource) other;
060    return m_handle == source.m_handle;
061  }
062
063  public int hashCode() {
064    return m_handle;
065  }
066
067  /// Get the kind of the source.
068  public Kind getKind() {
069    return getKindFromInt(CameraServerJNI.getSourceKind(m_handle));
070  }
071
072  /// Get the name of the source.  The name is an arbitrary identifier
073  /// provided when the source is created, and should be unique.
074  public String getName() {
075    return CameraServerJNI.getSourceName(m_handle);
076  }
077
078  /// Get the source description.  This is source-kind specific.
079  public String getDescription() {
080    return CameraServerJNI.getSourceDescription(m_handle);
081  }
082
083  /// Get the last time a frame was captured.
084  public long getLastFrameTime() {
085    return CameraServerJNI.getSourceLastFrameTime(m_handle);
086  }
087
088  /// Is the source currently connected to whatever is providing the images?
089  public boolean isConnected() {
090    return CameraServerJNI.isSourceConnected(m_handle);
091  }
092
093  /// Get a property.
094  /// @param name Property name
095  /// @return Property contents (of kind Property::kNone if no property with
096  ///         the given name exists)
097  public VideoProperty getProperty(String name) {
098    return new VideoProperty(CameraServerJNI.getSourceProperty(m_handle, name));
099  }
100
101  /// Enumerate all properties of this source.
102  public VideoProperty[] enumerateProperties() {
103    int[] handles = CameraServerJNI.enumerateSourceProperties(m_handle);
104    VideoProperty[] rv = new VideoProperty[handles.length];
105    for (int i=0; i<handles.length; i++) {
106      rv[i] = new VideoProperty(handles[i]);
107    }
108    return rv;
109  }
110
111  /// Get the current video mode.
112  public VideoMode getVideoMode() {
113    return CameraServerJNI.getSourceVideoMode(m_handle);
114  }
115
116  /// Set the video mode.
117  /// @param mode Video mode
118  public boolean setVideoMode(VideoMode mode) {
119    return CameraServerJNI.setSourceVideoMode(m_handle, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps);
120  }
121
122  /// Set the video mode.
123  /// @param pixelFormat desired pixel format
124  /// @param width desired width
125  /// @param height desired height
126  /// @param fps desired FPS
127  /// @return True if set successfully
128  public boolean setVideoMode(VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
129    return CameraServerJNI.setSourceVideoMode(m_handle, pixelFormat.getValue(), width, height, fps);
130  }
131
132  /// Set the pixel format.
133  /// @param pixelFormat desired pixel format
134  /// @return True if set successfully
135  public boolean setPixelFormat(VideoMode.PixelFormat pixelFormat) {
136    return CameraServerJNI.setSourcePixelFormat(m_handle, pixelFormat.getValue());
137  }
138
139  /// Set the resolution.
140  /// @param width desired width
141  /// @param height desired height
142  /// @return True if set successfully
143  public boolean setResolution(int width, int height) {
144    return CameraServerJNI.setSourceResolution(m_handle, width, height);
145  }
146
147  /// Set the frames per second (FPS).
148  /// @param fps desired FPS
149  /// @return True if set successfully
150  public boolean setFPS(int fps) {
151    return CameraServerJNI.setSourceFPS(m_handle, fps);
152  }
153
154  /// Enumerate all known video modes for this source.
155  public VideoMode[] enumerateVideoModes() {
156    return CameraServerJNI.enumerateSourceVideoModes(m_handle);
157  }
158
159  /// Enumerate all sinks connected to this source.
160  /// @return Vector of sinks.
161  public VideoSink[] enumerateSinks() {
162    int[] handles = CameraServerJNI.enumerateSourceSinks(m_handle);
163    VideoSink[] rv = new VideoSink[handles.length];
164    for (int i=0; i<handles.length; i++) {
165      rv[i] = new VideoSink(handles[i]);
166    }
167    return rv;
168  }
169
170  /// Enumerate all existing sources.
171  /// @return Vector of sources.
172  public static VideoSource[] enumerateSources() {
173    int[] handles = CameraServerJNI.enumerateSources();
174    VideoSource[] rv = new VideoSource[handles.length];
175    for (int i=0; i<handles.length; i++) {
176      rv[i] = new VideoSource(handles[i]);
177    }
178    return rv;
179  }
180
181  protected int m_handle;
182}