Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using V4L2 to realize VideoDevice #664

Merged
merged 23 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/devices/Media/VideoDevice/Devices/UnixVideoDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
Expand Down Expand Up @@ -67,10 +66,7 @@ public UnixVideoDevice(VideoConnectionSettings settings)
/// <param name="path">Picture save path</param>
public override async Task CaptureAsync(string path)
{
using (CancellationTokenSource cts = new CancellationTokenSource())
{
await CaptureAsync(path, cts.Token);
}
await CaptureAsync(path, CancellationToken.None);
}

/// <summary>
Expand All @@ -87,24 +83,20 @@ await Task.Run(() =>
byte[] dataBuffer = ProcessCaptureData();
Close();

using (FileStream fs = new FileStream(path, FileMode.Create))
{
fs.Write(dataBuffer, 0, dataBuffer.Length);
fs.Flush();
}
using FileStream fs = new FileStream(path, FileMode.Create);
fs.Write(dataBuffer, 0, dataBuffer.Length);
fs.Flush();
}, token);
}

/// <summary>
/// Capture a picture from the video device.
/// </summary>
/// <returns>Picture stream</returns>
/// <param name="token">A cancellation token that can be used to cancel the work</param>
public override async Task<MemoryStream> CaptureAsync()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
{
return await CaptureAsync(cts.Token);
}
return await CaptureAsync(CancellationToken.None);
}

/// <summary>
Expand Down Expand Up @@ -161,7 +153,7 @@ public override VideoDeviceValue GetVideoDeviceValue(VideoDeviceValueType type)
/// Get all the pixel formats supported by the device.
/// </summary>
/// <returns>Supported pixel formats</returns>
public override List<PixelFormat> GetSupportedPixelFormats()
public override IEnumerable<PixelFormat> GetSupportedPixelFormats()
{
v4l2_fmtdesc fmtdesc = new v4l2_fmtdesc
{
Expand All @@ -184,7 +176,7 @@ public override List<PixelFormat> GetSupportedPixelFormats()
/// </summary>
/// <param name="format">Pixel format</param>
/// <returns>Supported resolution</returns>
public override List<(uint Width, uint Height)> GetPixelFormatResolutions(PixelFormat format)
public override IEnumerable<(uint Width, uint Height)> GetPixelFormatResolutions(PixelFormat format)
{
v4l2_frmsizeenum size = new v4l2_frmsizeenum()
{
Expand Down Expand Up @@ -310,7 +302,7 @@ private unsafe void SetVideoConnectionSettings()
}
}
};
var res = V4l2Struct(VideoSettings.VIDIOC_S_FMT, ref format);
V4l2Struct(VideoSettings.VIDIOC_S_FMT, ref format);

// Set exposure type
v4l2_control ctrl = new v4l2_control
Expand Down
4 changes: 2 additions & 2 deletions src/devices/Media/VideoDevice/VideoDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public abstract partial class VideoDevice : IDisposable
/// Get all the pixel formats supported by the device.
/// </summary>
/// <returns>Supported pixel formats</returns>
public abstract List<PixelFormat> GetSupportedPixelFormats();
public abstract IEnumerable<PixelFormat> GetSupportedPixelFormats();

/// <summary>
/// Get all the resolutions supported by the specified pixel format.
/// </summary>
/// <param name="format">Pixel format</param>
/// <returns>Supported resolution</returns>
public abstract List<(uint Width, uint Height)> GetPixelFormatResolutions(PixelFormat format);
public abstract IEnumerable<(uint Width, uint Height)> GetPixelFormatResolutions(PixelFormat format);

public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>preview</LangVersion>
<EnableDefaultItems>false</EnableDefaultItems>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.6.0-preview7.19362.9" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 7 additions & 3 deletions src/devices/Media/VideoDevice/samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System;
using System.Drawing;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
using Iot.Device.Media;

Expand Down Expand Up @@ -40,15 +40,19 @@ static async Task Main(string[] args)
var value = device.GetVideoDeviceValue(VideoDeviceValueType.Rotate);
Console.WriteLine($"{value.Name} Min: {value.Minimum} Max: {value.Maximum} Step: {value.Step} Default: {value.DefaultValue} Current: {value.CurrentValue}");

await device.CaptureAsync("/home/pi/jpg_direct_output.jpg");
string path = "/home/pi/images";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

await device.CaptureAsync($"{path}/jpg_direct_output.jpg");

// Change capture setting
device.Settings.PixelFormat = PixelFormat.YUV420;

// Convert pixel format
Color[] colors = VideoDevice.Yv12ToRgb(await device.CaptureAsync(), settings.CaptureSize);
Bitmap bitmap = VideoDevice.RgbToBitmap(settings.CaptureSize, colors);
bitmap.Save("/home/pi/yuyv_to_jpg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Save($"{path}/yuyv_to_jpg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\V4l2.csproj" />
<ProjectReference Include="..\VideoDevice.csproj" />
</ItemGroup>

</Project>