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/** A sink that acts as a MJPEG-over-HTTP network server. */
008public class MjpegServer extends VideoSink {
009  /**
010   * Create a MJPEG-over-HTTP server sink.
011   *
012   * @param name Sink name (arbitrary unique identifier)
013   * @param listenAddress TCP listen address (empty string for all addresses)
014   * @param port TCP port number
015   */
016  public MjpegServer(String name, String listenAddress, int port) {
017    super(CameraServerJNI.createMjpegServer(name, listenAddress, port));
018  }
019
020  /**
021   * Create a MJPEG-over-HTTP server sink.
022   *
023   * @param name Sink name (arbitrary unique identifier)
024   * @param port TCP port number
025   */
026  public MjpegServer(String name, int port) {
027    this(name, "", port);
028  }
029
030  /**
031   * Get the listen address of the server.
032   *
033   * @return The listen address.
034   */
035  public String getListenAddress() {
036    return CameraServerJNI.getMjpegServerListenAddress(m_handle);
037  }
038
039  /**
040   * Get the port number of the server.
041   *
042   * @return The port number.
043   */
044  public int getPort() {
045    return CameraServerJNI.getMjpegServerPort(m_handle);
046  }
047
048  /**
049   * Set the stream resolution for clients that don't specify it.
050   *
051   * <p>It is not necessary to set this if it is the same as the source resolution.
052   *
053   * <p>Setting this different than the source resolution will result in increased CPU usage,
054   * particularly for MJPEG source cameras, as it will decompress, resize, and recompress the image,
055   * instead of using the camera's MJPEG image directly.
056   *
057   * @param width width, 0 for unspecified
058   * @param height height, 0 for unspecified
059   */
060  public void setResolution(int width, int height) {
061    CameraServerJNI.setProperty(CameraServerJNI.getSinkProperty(m_handle, "width"), width);
062    CameraServerJNI.setProperty(CameraServerJNI.getSinkProperty(m_handle, "height"), height);
063  }
064
065  /**
066   * Set the stream frames per second (FPS) for clients that don't specify it.
067   *
068   * <p>It is not necessary to set this if it is the same as the source FPS.
069   *
070   * @param fps FPS, 0 for unspecified
071   */
072  public void setFPS(int fps) {
073    CameraServerJNI.setProperty(CameraServerJNI.getSinkProperty(m_handle, "fps"), fps);
074  }
075
076  /**
077   * Set the compression for clients that don't specify it.
078   *
079   * <p>Setting this will result in increased CPU usage for MJPEG source cameras as it will
080   * decompress and recompress the image instead of using the camera's MJPEG image directly.
081   *
082   * @param quality JPEG compression quality (0-100), -1 for unspecified
083   */
084  public void setCompression(int quality) {
085    CameraServerJNI.setProperty(CameraServerJNI.getSinkProperty(m_handle, "compression"), quality);
086  }
087
088  /**
089   * Set the default compression used for non-MJPEG sources. If not set, 80 is used. This function
090   * has no effect on MJPEG source cameras; use SetCompression() instead to force recompression of
091   * MJPEG source images.
092   *
093   * @param quality JPEG compression quality (0-100)
094   */
095  public void setDefaultCompression(int quality) {
096    CameraServerJNI.setProperty(
097        CameraServerJNI.getSinkProperty(m_handle, "default_compression"), quality);
098  }
099}