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 all commits
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
8 changes: 8 additions & 0 deletions src/devices/Interop/Unix/Interop.Libraries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

internal partial class Interop
{
private const string LibcLibrary = "libc";
}
37 changes: 37 additions & 0 deletions src/devices/Interop/Unix/Libc/Interop.ioctl.macros.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
const int _IOC_NRBITS = 8;
const int _IOC_TYPEBITS = 8;
const int _IOC_SIZEBITS = 14;
const int _IOC_DIRBITS = 2;

const int _IOC_NRMASK = (1 << _IOC_NRBITS) - 1;
const int _IOC_TYPEMASK = (1 << _IOC_TYPEBITS) - 1;
const int _IOC_SIZEMASK = (1 << _IOC_SIZEBITS) - 1;
const int _IOC_DIRMASK = (1 << _IOC_DIRBITS) - 1;

const int _IOC_NRSHIFT = 0;
const int _IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS;
const int _IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS;
const int _IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS;

const int _IOC_NONE = 0;
const int _IOC_WRITE = 1;
const int _IOC_READ = 2;

internal static int _IOC(int dir, int type, int nr, int size)
=> ((dir) << _IOC_DIRSHIFT) | ((type) << _IOC_TYPESHIFT) | ((nr) << _IOC_NRSHIFT) | ((size) << _IOC_SIZESHIFT);

internal static int _IO(int type, int nr) => _IOC(_IOC_NONE, type, nr, 0);
internal static int _IOR(int type, int nr, Type size) => _IOC(_IOC_READ, type, nr, _IOC_TYPECHECK(size));
internal static int _IOW(int type, int nr, Type size) => _IOC(_IOC_WRITE, type, nr, _IOC_TYPECHECK(size));
internal static int _IOWR(int type, int nr, Type size) => _IOC(_IOC_READ | _IOC_WRITE, type, nr, _IOC_TYPECHECK(size));
internal static int _IOC_TYPECHECK(Type t) => Marshal.SizeOf(t);
}
49 changes: 49 additions & 0 deletions src/devices/Interop/Unix/Libc/Interop.libc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
[DllImport(LibcLibrary, SetLastError = true)]
internal static extern int ioctl(int fd, int request, IntPtr argp);

[DllImport(LibcLibrary, SetLastError = true)]
internal static extern int open([MarshalAs(UnmanagedType.LPStr)] string pathname, FileOpenFlags flags);

[DllImport(LibcLibrary)]
internal static extern int close(int fd);

[DllImport(LibcLibrary, SetLastError = true)]
internal static extern IntPtr mmap(IntPtr addr, int length, MemoryMappedProtections prot, MemoryMappedFlags flags, int fd, int offset);

[DllImport(LibcLibrary)]
internal static extern int munmap(IntPtr addr, int length);
}

internal enum FileOpenFlags
{
O_RDONLY = 0x00,
O_RDWR = 0x02,
O_NONBLOCK = 0x800,
O_SYNC = 0x101000
}

[Flags]
internal enum MemoryMappedProtections
{
PROT_NONE = 0x0,
PROT_READ = 0x1,
PROT_WRITE = 0x2,
PROT_EXEC = 0x4
}

[Flags]
internal enum MemoryMappedFlags
{
MAP_SHARED = 0x01,
MAP_PRIVATE = 0x02,
MAP_FIXED = 0x10
}
57 changes: 57 additions & 0 deletions src/devices/Interop/Unix/Libc/Interop.videodev2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

internal enum VideoSettings : int
{
VIDIOC_QUERYCAP = -2140645888,
VIDIOC_ENUM_FMT = -1069525502,
VIDIOC_CROPCAP = -1070836166,
VIDIOC_G_CROP = -1072409029,
VIDIOC_S_CROP = 1075074620,
VIDIOC_G_FMT = -1060350460,
VIDIOC_S_FMT = -1060350459,
VIDIOC_REQBUFS = -1072409080,
VIDIOC_QUERYBUF = -1069263351,
VIDIOC_STREAMON = 1074026002,
VIDIOC_STREAMOFF = 1074026003,
VIDIOC_QBUF = -1069263345,
VIDIOC_DQBUF = -1069263343,
VIDIOC_ENUM_FRAMESIZES = -1070836150,
VIDIOC_G_CTRL = -1073195493,
VIDIOC_S_CTRL = -1073195492,
VIDIOC_QUERYCTRL = -1069263324,
}

/// <summary>
/// videodev2.h Request Definition
/// </summary>
internal class RawVideoSettings
{
public static int VIDIOC_QUERYCAP = Interop._IOR('V', 0, typeof(v4l2_capability));
public static int VIDIOC_ENUM_FMT = Interop._IOWR('V', 2, typeof(v4l2_fmtdesc));
public static int VIDIOC_G_FMT = Interop._IOWR('V', 4, typeof(v4l2_format));
public static int VIDIOC_S_FMT = Interop._IOWR('V', 5, typeof(v4l2_format));
public static int VIDIOC_REQBUFS = Interop._IOWR('V', 8, typeof(v4l2_requestbuffers));
public static int VIDIOC_QUERYBUF = Interop._IOWR('V', 9, typeof(v4l2_buffer));
public static int VIDIOC_OVERLAY = Interop._IOW('V', 14, typeof(int));
public static int VIDIOC_QBUF = Interop._IOWR('V', 15, typeof(v4l2_buffer));
public static int VIDIOC_DQBUF = Interop._IOWR('V', 17, typeof(v4l2_buffer));
public static int VIDIOC_STREAMON = Interop._IOW('V', 18, typeof(int));
public static int VIDIOC_STREAMOFF = Interop._IOW('V', 19, typeof(int));
public static int VIDIOC_G_CTRL = Interop._IOWR('V', 27, typeof(v4l2_control));
public static int VIDIOC_S_CTRL = Interop._IOWR('V', 28, typeof(v4l2_control));
public static int VIDIOC_QUERYCTRL = Interop._IOWR('V', 36, typeof(v4l2_queryctrl));
public static int VIDIOC_G_INPUT = Interop._IOR('V', 38, typeof(int));
public static int VIDIOC_S_INPUT = Interop._IOWR('V', 39, typeof(int));
public static int VIDIOC_G_OUTPUT = Interop._IOR('V', 46, typeof(int));
public static int VIDIOC_S_OUTPUT = Interop._IOWR('V', 47, typeof(int));
public static int VIDIOC_CROPCAP = Interop._IOWR('V', 58, typeof(v4l2_cropcap));
public static int VIDIOC_G_CROP = Interop._IOWR('V', 59, typeof(v4l2_crop));
public static int VIDIOC_S_CROP = Interop._IOW('V', 60, typeof(v4l2_crop));
public static int VIDIOC_TRY_FMT = Interop._IOWR('V', 64, typeof(v4l2_format));
public static int VIDIOC_G_PRIORITY = Interop._IOR('V', 67, typeof(uint));
public static int VIDIOC_S_PRIORITY = Interop._IOW('V', 68, typeof(uint));
public static int VIDIOC_ENUM_FRAMESIZES = Interop._IOWR('V', 74, typeof(v4l2_frmsizeenum));
public static int VIDIOC_PREPARE_BUF = Interop._IOWR('V', 93, typeof(v4l2_buffer));
}
Loading