Skip to content

Commit

Permalink
Run dotnet format
Browse files Browse the repository at this point in the history
  • Loading branch information
Speykious committed Oct 29, 2022
1 parent 040cb44 commit 1c77abf
Show file tree
Hide file tree
Showing 44 changed files with 4,882 additions and 4,924 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ csharp_style_prefer_switch_expression = false:warning
csharp_style_prefer_pattern_matching = false:warning
csharp_style_implicit_object_creation_when_type_is_apparent = false:warning
csharp_prefer_braces = when_multiline:warning
csharp_style_namespace_declarations = file_scoped:warning

# Naming Rules

Expand Down
381 changes: 190 additions & 191 deletions SeeShark.Example.Ascii/Program.cs

Large diffs are not rendered by default.

247 changes: 123 additions & 124 deletions SeeShark.Example.Stats/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,155 +8,154 @@
using SeeShark.Device;
using static SeeShark.FFmpeg.FFmpegManager;

namespace SeeShark.Example.Stats
namespace SeeShark.Example.Stats;

class Program
{
class Program
static Camera? karen;
static CameraManager? manager;

static void Main(string[] args)
{
static Camera? karen;
static CameraManager? manager;
// Casually displaying "Oof :(" when exiting the program with force.
Console.CancelKeyPress += (object? _sender, ConsoleCancelEventArgs e) =>
{
Console.Error.WriteLine("\n\n");
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Oof :(");
Console.ResetColor();
Dispose();
};

// You can add your own path for FFmpeg libraries here!
SetupFFmpeg(
AppDomain.CurrentDomain.BaseDirectory,
"/usr/lib",
"/usr/lib64"
);

static void Main(string[] args)
manager = new CameraManager();

string devicePath;
if (args.Length < 1)
{
// Casually displaying "Oof :(" when exiting the program with force.
Console.CancelKeyPress += (object? _sender, ConsoleCancelEventArgs e) =>
{
Console.Error.WriteLine("\n\n");
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Oof :(");
Console.ResetColor();
Dispose();
};

// You can add your own path for FFmpeg libraries here!
SetupFFmpeg(
AppDomain.CurrentDomain.BaseDirectory,
"/usr/lib",
"/usr/lib64"
);

manager = new CameraManager();

string devicePath;
if (args.Length < 1)
/// Select an available camera device.
/// <see cref="CameraManager.Devices"/> only gets filled when the camera manager is instanciated,
/// since it is not watching devices by default.
while (true)
{
/// Select an available camera device.
/// <see cref="CameraManager.Devices"/> only gets filled when the camera manager is instanciated,
/// since it is not watching devices by default.
while (true)
Console.WriteLine("\nDevices available:");
for (int i = 0; i < manager.Devices.Count; i++)
Console.WriteLine($"| #{i}: {manager.Devices[i]}");

Console.Write("\nChoose a camera by index: ");
Console.Out.Flush();
if (int.TryParse(Console.ReadLine(), out int index) && index < manager.Devices.Count && index >= 0)
{
Console.WriteLine("\nDevices available:");
for (int i = 0; i < manager.Devices.Count; i++)
Console.WriteLine($"| #{i}: {manager.Devices[i]}");

Console.Write("\nChoose a camera by index: ");
Console.Out.Flush();
if (int.TryParse(Console.ReadLine(), out int index) && index < manager.Devices.Count && index >= 0)
{
devicePath = manager.Devices[index].Path;
break;
}
devicePath = manager.Devices[index].Path;
break;
}
}
else
{
devicePath = args[0];
}
}
else
{
devicePath = args[0];
}

/// You can get a <see cref="Camera"/> from either a string
/// representing the device path, or a <see cref="CameraInfo">.
/// You can get a <see cref="Camera"/> from either a string
/// representing the device path, or a <see cref="CameraInfo">.

// Unfortunately, she saw the manager
karen = manager.GetDevice(devicePath);
// Unfortunately, she saw the manager
karen = manager.GetDevice(devicePath);

/// Attach our <see cref="OnNewFrame"/> method to the camera's frame event handler,
/// so that we can process every coming frame the way we want.
karen.OnFrame += OnFrameEventHandler;
/// Attach our <see cref="OnNewFrame"/> method to the camera's frame event handler,
/// so that we can process every coming frame the way we want.
karen.OnFrame += OnFrameEventHandler;

Console.WriteLine($"Camera chosen: {karen.Info}");
Console.WriteLine("Press Space or P to play/pause the camera.");
Console.WriteLine("Press Enter or Q or Escape to exit the program.");
Console.WriteLine($"Camera chosen: {karen.Info}");
Console.WriteLine("Press Space or P to play/pause the camera.");
Console.WriteLine("Press Enter or Q or Escape to exit the program.");

// I could have written a simple `while (true)`, but then I used a `switch` statement.
// If only C# had labelled loops like Rust :(
for (var loop = true; loop;)
// I could have written a simple `while (true)`, but then I used a `switch` statement.
// If only C# had labelled loops like Rust :(
for (var loop = true; loop;)
{
Console.WriteLine("\x1b[2K\rCamera is {0}", karen.IsPlaying ? "Playing" : "Paused");
var cki = Console.ReadKey(true);
switch (cki.Key)
{
Console.WriteLine("\x1b[2K\rCamera is {0}", karen.IsPlaying ? "Playing" : "Paused");
var cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.P:
case ConsoleKey.Spacebar:
if (karen.IsPlaying)
karen.StopCapture();
else
karen.StartCapture();
Console.CursorVisible = !karen.IsPlaying;
break;

case ConsoleKey.Q:
case ConsoleKey.Enter:
case ConsoleKey.Escape:
Console.CursorVisible = true;
loop = false;
break;
}
case ConsoleKey.P:
case ConsoleKey.Spacebar:
if (karen.IsPlaying)
karen.StopCapture();
else
karen.StartCapture();
Console.CursorVisible = !karen.IsPlaying;
break;

case ConsoleKey.Q:
case ConsoleKey.Enter:
case ConsoleKey.Escape:
Console.CursorVisible = true;
loop = false;
break;
}

Dispose();

// Unless you filmed a shark with your camera, no.
Console.WriteLine("\n\nDid you SeeShark? :)");
}

static long frameCount = 0;
static double fps = 0;
static double minFps = double.PositiveInfinity;
static double maxFps = double.NegativeInfinity;
Dispose();

// Unless you filmed a shark with your camera, no.
Console.WriteLine("\n\nDid you SeeShark? :)");
}

/// <summary>
/// Stopwatch used to measure the FPS.
/// </summary>
static readonly Stopwatch watch = new Stopwatch();
static long frameCount = 0;
static double fps = 0;
static double minFps = double.PositiveInfinity;
static double maxFps = double.NegativeInfinity;

/// <summary>
/// Our custom frame event callback.
/// Each time it is triggered, it will display some simple stats in the console.
/// </summary>
public static void OnFrameEventHandler(object? _sender, FrameEventArgs e)
{
// Don't redraw the frame if it's not new.
if (e.Status != DecodeStatus.NewFrame)
return;

var frame = e.Frame;
/// <summary>
/// Stopwatch used to measure the FPS.
/// </summary>
static readonly Stopwatch watch = new Stopwatch();

#region Stats
if (frameCount == 0)
{
watch.Start();
}
else
{
fps = TimeSpan.TicksPerSecond * 100 / (double)watch.ElapsedTicks;
minFps = fps < minFps ? fps : minFps;
maxFps = fps > maxFps ? fps : maxFps;
Console.WriteLine($"\x1b[2K\r{frame.Width}x{frame.Height} @ {fps:#.##} fps [{minFps} - {maxFps}]");
watch.Restart();
}
#endregion
/// <summary>
/// Our custom frame event callback.
/// Each time it is triggered, it will display some simple stats in the console.
/// </summary>
public static void OnFrameEventHandler(object? _sender, FrameEventArgs e)
{
// Don't redraw the frame if it's not new.
if (e.Status != DecodeStatus.NewFrame)
return;

frameCount++;
}
var frame = e.Frame;

/// <summary>
/// Dispose our <see cref="IDisposable"/> objects.
/// </summary>
public static void Dispose()
#region Stats
if (frameCount == 0)
{
watch.Start();
}
else
{
karen?.StopCapture();
karen?.Dispose();
manager?.Dispose();
fps = TimeSpan.TicksPerSecond * 100 / (double)watch.ElapsedTicks;
minFps = fps < minFps ? fps : minFps;
maxFps = fps > maxFps ? fps : maxFps;
Console.WriteLine($"\x1b[2K\r{frame.Width}x{frame.Height} @ {fps:#.##} fps [{minFps} - {maxFps}]");
watch.Restart();
}
#endregion

frameCount++;
}

/// <summary>
/// Dispose our <see cref="IDisposable"/> objects.
/// </summary>
public static void Dispose()
{
karen?.StopCapture();
karen?.Dispose();
manager?.Dispose();
}
}
39 changes: 19 additions & 20 deletions SeeShark/Decode/DecodeStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
// This file is part of SeeShark.
// SeeShark is licensed under the BSD 3-Clause License. See LICENSE for details.

namespace SeeShark.Decode
namespace SeeShark.Decode;

/// <summary>
/// Describes the decoding status of a given <see cref="VideoStreamDecoder"/>
/// after calling its <see cref="VideoStreamDecoder.TryDecodeNextFrame"/> method.
/// </summary>
public enum DecodeStatus
{
/// <summary>
/// Describes the decoding status of a given <see cref="VideoStreamDecoder"/>
/// after calling its <see cref="VideoStreamDecoder.TryDecodeNextFrame"/> method.
/// A new frame has been returned.
/// </summary>
public enum DecodeStatus
{
/// <summary>
/// A new frame has been returned.
/// </summary>
NewFrame,
/// <summary>
/// No new frame is available at the moment.
/// The given <see cref="VideoStreamDecoder"/> is expected to try decoding a new frame again.
/// </summary>
NoFrameAvailable,
/// <summary>
/// Decoder reached the end of the stream.
/// The given <see cref="VideoStreamDecoder"/> is expected to stop decoding.
/// </summary>
EndOfStream,
}
NewFrame,
/// <summary>
/// No new frame is available at the moment.
/// The given <see cref="VideoStreamDecoder"/> is expected to try decoding a new frame again.
/// </summary>
NoFrameAvailable,
/// <summary>
/// Decoder reached the end of the stream.
/// The given <see cref="VideoStreamDecoder"/> is expected to stop decoding.
/// </summary>
EndOfStream,
}
Loading

0 comments on commit 1c77abf

Please sign in to comment.