diff --git a/Dependencies/Intel.Realsense.dll b/Dependencies/Intel.Realsense.dll index b36e7f4..b44f601 100644 Binary files a/Dependencies/Intel.Realsense.dll and b/Dependencies/Intel.Realsense.dll differ diff --git a/Dependencies/realsense2.dll b/Dependencies/realsense2.dll index d12761a..d1e0c2b 100644 Binary files a/Dependencies/realsense2.dll and b/Dependencies/realsense2.dll differ diff --git a/LightBuzz.RealSense/LightBuzz.RealSense.Unity/LightBuzz.RealSense.Unity.csproj b/LightBuzz.RealSense/LightBuzz.RealSense.Unity/LightBuzz.RealSense.Unity.csproj deleted file mode 100644 index 1a9ebaa..0000000 --- a/LightBuzz.RealSense/LightBuzz.RealSense.Unity/LightBuzz.RealSense.Unity.csproj +++ /dev/null @@ -1,65 +0,0 @@ - - - - - Debug - AnyCPU - {434C8C47-022D-4DDD-8413-14FF2BB0115E} - Library - Properties - LightBuzz.RealSense - LightBuzz.RealSense - v3.5 - 512 - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - bin\Release\LightBuzz.RealSense.xml - - - - ..\..\Dependencies\Intel.Realsense.dll - - - ..\..\Dependencies\Unity\LightBuzz.dll - - - - ..\..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll - - - - - CoordinateMapper.cs - - - CoordinateMapperExtensions.cs - - - DeviceConfiguration.cs - - - RealSenseDevice.cs - - - VideoStreamRequest.cs - - - - - \ No newline at end of file diff --git a/LightBuzz.RealSense/LightBuzz.RealSense.Unity/Properties/AssemblyInfo.cs b/LightBuzz.RealSense/LightBuzz.RealSense.Unity/Properties/AssemblyInfo.cs deleted file mode 100644 index ee125ac..0000000 --- a/LightBuzz.RealSense/LightBuzz.RealSense.Unity/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("LightBuzz.RealSense.Unity")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("LightBuzz.RealSense.Unity")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("434c8c47-022d-4ddd-8413-14ff2bb0115e")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/LightBuzz.RealSense/LightBuzz.RealSense.sln b/LightBuzz.RealSense/LightBuzz.RealSense.sln index 4c5ffb6..50489bc 100644 --- a/LightBuzz.RealSense/LightBuzz.RealSense.sln +++ b/LightBuzz.RealSense/LightBuzz.RealSense.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 15.0.28307.106 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightBuzz.RealSense", "LightBuzz.RealSense\LightBuzz.RealSense.csproj", "{57B1971A-F1CF-4429-9448-CFD8993D560C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightBuzz.RealSense.Unity", "LightBuzz.RealSense.Unity\LightBuzz.RealSense.Unity.csproj", "{434C8C47-022D-4DDD-8413-14FF2BB0115E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,10 +15,6 @@ Global {57B1971A-F1CF-4429-9448-CFD8993D560C}.Debug|Any CPU.Build.0 = Debug|Any CPU {57B1971A-F1CF-4429-9448-CFD8993D560C}.Release|Any CPU.ActiveCfg = Release|Any CPU {57B1971A-F1CF-4429-9448-CFD8993D560C}.Release|Any CPU.Build.0 = Release|Any CPU - {434C8C47-022D-4DDD-8413-14FF2BB0115E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {434C8C47-022D-4DDD-8413-14FF2BB0115E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {434C8C47-022D-4DDD-8413-14FF2BB0115E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {434C8C47-022D-4DDD-8413-14FF2BB0115E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LightBuzz.RealSense/LightBuzz.RealSense/DeviceConfiguration.cs b/LightBuzz.RealSense/LightBuzz.RealSense/DeviceConfiguration.cs index 78ad59c..bbabfdc 100644 --- a/LightBuzz.RealSense/LightBuzz.RealSense/DeviceConfiguration.cs +++ b/LightBuzz.RealSense/LightBuzz.RealSense/DeviceConfiguration.cs @@ -4,36 +4,52 @@ namespace LightBuzz.RealSense { [Serializable] - public struct DeviceConfiguration + public class DeviceConfiguration { - public enum Mode + public VideoStreamMode Mode { get; set; } + + public VideoStreamRequest[] Profiles { get; set; } + + public string RequestedSerialNumber { get; set; } + + public string PlaybackFile { get; set; } + + public string RecordPath { get; set; } + + public DeviceConfiguration() { - Live, Playback, Record } - public Mode mode; - public VideoStreamRequest[] Profiles; - public string RequestedSerialNumber; - public string PlaybackFile; - public string RecordPath; + public DeviceConfiguration(VideoStreamRequest[] profiles) + { + Mode = VideoStreamMode.Live; + RequestedSerialNumber = string.Empty; + Profiles = profiles; + } + public DeviceConfiguration(VideoStreamRequest[] profiles, VideoStreamMode mode, string id) + { + Mode = mode; + RequestedSerialNumber = id; + Profiles = profiles; + } public Config ToPipelineConfig() { Config cfg = new Config(); - switch (mode) + switch (Mode) { - case Mode.Live: + case VideoStreamMode.Live: cfg.EnableDevice(RequestedSerialNumber); foreach (var p in Profiles) cfg.EnableStream(p.Stream, p.StreamIndex, p.Width, p.Height, p.Format, p.Framerate); break; - case Mode.Playback: + case VideoStreamMode.Playback: if (string.IsNullOrEmpty(PlaybackFile)) { - mode = Mode.Live; + Mode = VideoStreamMode.Live; } else { @@ -41,7 +57,7 @@ public Config ToPipelineConfig() } break; - case Mode.Record: + case VideoStreamMode.Record: foreach (var p in Profiles) cfg.EnableStream(p.Stream, p.StreamIndex, p.Width, p.Height, p.Format, p.Framerate); if (!string.IsNullOrEmpty(RecordPath)) @@ -52,5 +68,44 @@ public Config ToPipelineConfig() return cfg; } + + public static DeviceConfiguration Default() + { + return new DeviceConfiguration + { + Mode = VideoStreamMode.Live, + RequestedSerialNumber = string.Empty, + Profiles = new VideoStreamRequest[] + { + new VideoStreamRequest + { + Stream = Stream.Depth, + StreamIndex = -1, + Width = 640, + Height = 480, + Format = Format.Z16, + Framerate = 0 + }, + new VideoStreamRequest + { + Stream = Stream.Infrared, + StreamIndex = -1, + Width = 640, + Height = 480, + Format = Format.Y8, + Framerate = 0 + }, + new VideoStreamRequest + { + Stream = Stream.Color, + StreamIndex = -1, + Width = 640, + Height = 480, + Format = Format.Rgb8, + Framerate = 0 + } + } + }; + } } } diff --git a/LightBuzz.RealSense/LightBuzz.RealSense/LightBuzz.RealSense.csproj b/LightBuzz.RealSense/LightBuzz.RealSense/LightBuzz.RealSense.csproj index 2717cf7..4e8df18 100644 --- a/LightBuzz.RealSense/LightBuzz.RealSense/LightBuzz.RealSense.csproj +++ b/LightBuzz.RealSense/LightBuzz.RealSense/LightBuzz.RealSense.csproj @@ -32,7 +32,8 @@ bin\Release\LightBuzz.RealSense.xml - + + False ..\..\Dependencies\Intel.Realsense.dll @@ -46,6 +47,7 @@ + diff --git a/LightBuzz.RealSense/LightBuzz.RealSense/RealSenseDevice.cs b/LightBuzz.RealSense/LightBuzz.RealSense/RealSenseDevice.cs index ecf3033..2bfc0e6 100644 --- a/LightBuzz.RealSense/LightBuzz.RealSense/RealSenseDevice.cs +++ b/LightBuzz.RealSense/LightBuzz.RealSense/RealSenseDevice.cs @@ -10,52 +10,86 @@ namespace LightBuzz.RealSense /// public class RealSenseDevice { + #region Constants + + private readonly AutoResetEvent _stopEvent = new AutoResetEvent(false); + + #endregion + + #region Members + + private Pipeline _pipeline; + private Thread _worker; + + #endregion + + #region Events + /// - /// Notifies upon streaming start + /// Reaised when streaming starts. /// public event Action OnStart; /// - /// Notifies when streaming has stopped + /// Raised when streaming stops. /// public event Action OnStop; /// - /// Fired when a new frame is available + /// Raised when a new frame set is available /// - public event Action OnFrameArrive; + public event Action OnFrameSetArrived; /// - /// Fired when a new frame set is available + /// Raised when a color frame is available. /// - public event Action OnFrameSetArrive; + public event Action OnColorFrameArrived; /// - /// User configuration + /// Raised when a depth frame is available. /// - public DeviceConfiguration DeviceConfiguration = new DeviceConfiguration - { - mode = DeviceConfiguration.Mode.Live, - RequestedSerialNumber = string.Empty, - Profiles = new VideoStreamRequest[] - { - new VideoStreamRequest {Stream = Stream.Depth, StreamIndex = -1, Width = 640, Height = 480, Format = Format.Z16 , Framerate = 0 }, - new VideoStreamRequest {Stream = Stream.Infrared, StreamIndex = -1, Width = 640, Height = 480, Format = Format.Y8 , Framerate = 0 }, - new VideoStreamRequest {Stream = Stream.Color, StreamIndex = -1, Width = 640, Height = 480, Format = Format.Rgb8 , Framerate = 0 } - } - }; + public event Action OnDepthFrameArrived; + + /// + /// Raised when an infrared frame is available. + /// + public event Action OnInfraredFrameArrived; + + #endregion + + #region Properties + /// + /// Determines whether the device is streaming frames. + /// public bool Streaming { get; protected set; } + + /// + /// The active profile of the RealSense camera. + /// public PipelineProfile ActiveProfile { get; protected set; } - private Pipeline _pipeline; - private Thread _worker; - private readonly AutoResetEvent _stopEvent = new AutoResetEvent(false); + /// + /// The current device configuration. + /// + public DeviceConfiguration DeviceConfiguration { get; set; } + + #endregion + #region Public methods + + /// + /// Opens the RealSense device. + /// public void Open() { _pipeline = new Pipeline(); + if (DeviceConfiguration == null) + { + DeviceConfiguration = DeviceConfiguration.Default(); + } + using (var cfg = DeviceConfiguration.ToPipelineConfig()) { ActiveProfile = _pipeline.Start(cfg); @@ -79,10 +113,15 @@ public void Open() OnStart?.Invoke(ActiveProfile); } + /// + /// Closes the sensor and disposes any resources. + /// public void Close() { - OnFrameSetArrive = null; - OnFrameArrive = null; + OnFrameSetArrived = null; + OnColorFrameArrived = null; + OnDepthFrameArrived = null; + OnInfraredFrameArrived = null; if (_worker != null) { @@ -91,7 +130,9 @@ public void Close() } if (Streaming && OnStop != null) + { OnStop(); + } if (_pipeline != null) { @@ -119,22 +160,39 @@ public void Close() _pipeline = null; } - /// - /// Worker Thread for multithreaded operations - /// + #endregion + + #region Private methods + private void WaitForFrames() { while (!_stopEvent.WaitOne(0)) { - //using (var frames = _pipeline.WaitForFrames()) - //using (var frame = frames.AsFrame()) - // OnFrameArrive?.Invoke(frame); - - using (var frames = _pipeline.WaitForFrames()) + using (FrameSet set = _pipeline.WaitForFrames()) { - OnFrameSetArrive?.Invoke(frames); + OnFrameSetArrived?.Invoke(set); + + foreach (Frame frame in set) + { + if (frame.Profile.Stream == Stream.Color) + { + OnColorFrameArrived?.Invoke(frame as VideoFrame); + } + + if (frame.Profile.Stream == Stream.Depth) + { + OnDepthFrameArrived?.Invoke(frame as DepthFrame); + } + + if (frame.Profile.Stream == Stream.Infrared) + { + OnInfraredFrameArrived?.Invoke(frame as VideoFrame); + } + } } } } + + #endregion } } diff --git a/LightBuzz.RealSense/LightBuzz.RealSense/VideoStreamMode.cs b/LightBuzz.RealSense/LightBuzz.RealSense/VideoStreamMode.cs new file mode 100644 index 0000000..8471f8c --- /dev/null +++ b/LightBuzz.RealSense/LightBuzz.RealSense/VideoStreamMode.cs @@ -0,0 +1,9 @@ +namespace LightBuzz.RealSense +{ + public enum VideoStreamMode + { + Live, + Playback, + Record + } +}