Skip to content

Commit

Permalink
Adds auto incrementing count for StartAutomaticCapture, and GetServer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Jan 20, 2017
1 parent cc10d93 commit 5292374
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions WPILib/CameraServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static CameraServer Instance
}
}

private int m_defaultUsbDevice;
private string m_primarySourceName;
private Dictionary<string, VideoSource> m_sources;
private Dictionary<string, VideoSink> m_sinks;
Expand Down Expand Up @@ -346,6 +347,7 @@ private static void PutSourcePropertyValue(ITable table, VideoEvent evnt, bool i

private CameraServer()
{
m_defaultUsbDevice = 0;
m_lockObject = new object();
m_sources = new Dictionary<string, VideoSource>();
m_sinks = new Dictionary<string, VideoSink>();
Expand Down Expand Up @@ -542,14 +544,16 @@ private CameraServer()
/// <see cref="GetVideo()"/> to get access to the camera images
///
/// <para>
/// This overload calls <see cref="StartAutomaticCapture(int)"/> with device 0,
/// creating a camera named "USB Camera 0"
/// The first time this overload is called, it calls <see cref="StartAutomaticCapture(int)"/>
/// with device 0, creating a camera named "USB Camera 0". Subsequent calls increment the device
/// number (e.g. 1, 2, etc).
/// </para>
/// </remarks>
/// <returns>The <see cref="UsbCamera"/> object that was created to stream from</returns>
public UsbCamera StartAutomaticCapture()
{
return StartAutomaticCapture(0);
// returns new value, so subtract 1 to get old value
return StartAutomaticCapture(Interlocked.Increment(ref m_defaultUsbDevice) - 1);
}

/// <summary>
Expand Down Expand Up @@ -836,6 +840,41 @@ public void RemoveServer(string name)
}
}

/// <summary>
/// Get server for the primary camera feed.
/// </summary>
/// <remarks>
/// This is only valid to call after a camera feed has been added with
/// <see cref="StartAutomaticCapture()"/> or <see cref="AddServer(string)"/>.
/// </remarks>
/// <returns>VideoSink for the primary camera feed.</returns>
public VideoSink GetServer()
{
lock (m_lockObject)
{
if (m_primarySourceName == null)
{
throw new VideoException("no camera available");
}
return GetServer($"serve_{m_primarySourceName}");
}
}

/// <summary>
/// Gets a server by name
/// </summary>
/// <param name="name">Server name</param>
/// <returns>VideoSink if it exists, otherwise null</returns>
public VideoSink GetServer(string name)
{
lock (m_lockObject)
{
VideoSink sink;
m_sinks.TryGetValue(name, out sink);
return sink;
}
}

/// <summary>
/// Adds an already created camera.
/// </summary>
Expand Down

0 comments on commit 5292374

Please sign in to comment.