Skip to content

Commit

Permalink
Add --framerate and --tuningfile Camera options (#2165)
Browse files Browse the repository at this point in the history
* add missing commonly required camera options (framerate/tuningfile)

* fix OperationCanceledException causing libcamera not to exit, and allow continuous capture with PipeWriter
  • Loading branch information
CodedBeard authored Dec 21, 2023
1 parent 6467994 commit b4f71f7
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/devices/Camera/Camera/Settings/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,9 @@ public enum Command
/// The command for the --frames option
/// </summary>
Frames,

/// <summary>
/// The command for the --framerate option
/// </summary>
Framerate,
}
34 changes: 34 additions & 0 deletions src/devices/Camera/Camera/Settings/CommandOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,40 @@ public CommandOptionsBuilder WithMJPEGVideoOptions(int quality)
return this;
}

/// <summary>
/// Adds the option to capture a video stream at a certain Framerate.
/// </summary>
/// <param name="rate">The number of frames per second, if not specified uses the camera default</param>
/// <returns>A reference to this instance.</returns>
public CommandOptionsBuilder WithVideoFramerate(int rate)
{
AddFramerate(rate);
return this;
}

/// <summary>
/// Adds the option to load a tuning file for the camera
/// </summary>
/// <param name="path"> The path to the tuning file.</param>
/// <returns>A reference to this instance.</returns>
public CommandOptionsBuilder WithTuningFile(string path)
{
AddTuningFile(path);
return this;
}

private void AddFramerate(int rate)
{
var cmd = GetByCategory(CommandCategory.Video, Command.Framerate);
_commands.Add(new CommandOptionAndValue(cmd, rate.ToString()));
}

private void AddTuningFile(string path)
{
var cmd = Get(Command.TuningFile);
_commands.Add(new CommandOptionAndValue(cmd, path));
}

private void AddListCameras()
{
var cmd = Get(Command.ListCameras);
Expand Down
4 changes: 4 additions & 0 deletions src/devices/Camera/Camera/Settings/LibcameraAppsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,9 @@ public class LibcameraAppsSettings

new CommandOption(CommandCategory.Video, Command.Frames,
"--frames", "Record exactly this many frames <number>", CommandInputType.Int),

new CommandOption(CommandCategory.Video, Command.Framerate,
"--framerate", "Set the frames per second captured <number>", CommandInputType.Int),

};
}
2 changes: 1 addition & 1 deletion src/devices/Camera/samples/Camera.Samples/Capture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public async Task CaptureTimelapse()
// The ContinuousRunAsync method offload the capture on a separate thread
// the first await is tied the thread being run
// the second await is tied to the capture
var task = await proc.ContinuousRunAsync(args, null);
var task = await proc.ContinuousRunAsync(args, default(Stream));
await task;
}

Expand Down
44 changes: 36 additions & 8 deletions src/devices/Common/Iot/Device/Common/ProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public void Kill()
{
if (_process != null && !_process.HasExited)
{
#if NETSTANDARD2_0
#if NETSTANDARD2_0
_process.Kill();
#else
#else
_process.Kill(true);
#endif
#endif
if (!_process.HasExited)
{
_process.WaitForExit(5000);
Expand Down Expand Up @@ -129,11 +129,11 @@ public async Task ExecuteAsync(string argsString, Stream? target)

if (target == null)
{
#if NETSTANDARD2_0
#if NETSTANDARD2_0
_process.WaitForExit();
#else
#else
await _process.WaitForExitAsync(_cts.Token);
#endif
#endif
return;
}

Expand All @@ -145,11 +145,10 @@ public async Task ExecuteAsync(string argsString, Stream? target)
{
await _process.StandardOutput.BaseStream.CopyToAsync(target, _processSettings.BufferSize, _cts.Token);
}

_process.WaitForExit(_processSettings.MaxMillisecondsToWaitAfterProcessCompletes);
}
finally
{
_process?.WaitForExit(_processSettings.MaxMillisecondsToWaitAfterProcessCompletes);
_process?.Dispose();
_process = null;
}
Expand Down Expand Up @@ -213,6 +212,34 @@ public async Task<Task> ContinuousRunAsync(string argsString, Stream? target)
}, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}

/// <summary>
/// Runs the execute on a separate thread
/// </summary>
/// <param name="arguments">The array of command line arguments.</param>
/// <param name="target">The pipe that will receive the output of the process.</param>
/// <returns>A task that represent the new thread communicating with the process.
/// The returned value is the task that represents the output being copied to the target pipe</returns>
public Task<Task> ContinuousRunAsync(string[] arguments, PipeWriter target)
{
var argsString = string.Join(" ", arguments);
return ContinuousRunAsync(argsString, target);
}

/// <summary>
/// Runs the execute on a separate thread
/// </summary>
/// <param name="argsString">A string will the complete command line of the process.</param>
/// <param name="target">The pipe that will receive the output of the process.</param>
/// <returns>A task that represent the new thread communicating with the process.
/// The returned value is the task that represents the output being copied to the target pipe</returns>
public async Task<Task> ContinuousRunAsync(string argsString, PipeWriter target)
{
return await Task.Factory.StartNew(async () =>
{
await ExecuteAsync(argsString, target);
}, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}

/// <summary>
/// Execute the process with a number of arguments. The target Pipe
/// receive the stdout of the process
Expand Down Expand Up @@ -243,6 +270,7 @@ public async Task ExecuteAsync(string argsString, PipeWriter target)
}
finally
{
_process?.WaitForExit(_processSettings.MaxMillisecondsToWaitAfterProcessCompletes);
_process?.Dispose();
_process = null;
}
Expand Down

0 comments on commit b4f71f7

Please sign in to comment.