Skip to content

Commit

Permalink
+NetDevice object; +cs-tutorial-7-net-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Uzbekov authored and Nir-Az committed Sep 29, 2021
1 parent f96c56e commit e8bf11e
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 0 deletions.
1 change: 1 addition & 0 deletions wrappers/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ add_subdirectory(tutorial/software-dev)
add_subdirectory(tutorial/pose)
add_subdirectory(tutorial/d400-occ)
add_subdirectory(tutorial/load-json)
add_subdirectory(tutorial/net-dev)
1 change: 1 addition & 0 deletions wrappers/csharp/Intel.RealSense/Devices/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ target_sources(${LRS_DOTNET_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/UpdatableDevice.cs"
"${CMAKE_CURRENT_LIST_DIR}/UpdateDevice.cs"
"${CMAKE_CURRENT_LIST_DIR}/FirmwareLogsDevice.cs"
"${CMAKE_CURRENT_LIST_DIR}/NetDevice.cs"
)
33 changes: 33 additions & 0 deletions wrappers/csharp/Intel.RealSense/Devices/NetDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

namespace Intel.RealSense
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;


public class NetDevice : Device
{
public NetDevice(string address) : base(init(address))
{
}

public void AddTo(Context ctx)
{
object error;
NativeMethods.rs2_context_add_software_device(ctx.Handle, Handle, out error);
}

private static IntPtr init(string address)
{
object error;
var ApiVersion = NativeMethods.rs2_get_api_version(out error);
var dev = NativeMethods.rs2_create_net_device(ApiVersion, address, out error);

return dev;
}
}
}
7 changes: 7 additions & 0 deletions wrappers/csharp/Intel.RealSense/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ internal static class NativeMethods

#if DEBUG
private const string dllName = "realsense2d";
private const string dllNetName = "realsense2-netd";
#else
private const string dllName = "realsense2";
private const string dllNetName = "realsense2-net";
#endif


Expand Down Expand Up @@ -904,6 +906,11 @@ internal static extern IntPtr rs2_terminal_parse_response(IntPtr terminal_parser
[DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int rs2_config_can_resolve(IntPtr config, IntPtr pipe, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ErrorMarshaler))] out object error);
#endregion
#region rs_net_device
[DllImport(dllNetName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr rs2_create_net_device(int api_version, string address, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ErrorMarshaler))] out object error);
#endregion


#region Error Handling
[DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
Expand Down
28 changes: 28 additions & 0 deletions wrappers/csharp/cs-tutorial-7-net-dev/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required( VERSION 3.8.0 )

project(cs-tutorial-7-net-dev)

add_executable(${PROJECT_NAME}
Program.cs
Window.xaml
Window.xaml.cs

Properties/AssemblyInfo.cs
)

set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v${DOTNET_VERSION_EXAMPLES}")
# set_property(TARGET ${PROJECT_NAME} PROPERTY WIN32_EXECUTABLE TRUE)

add_dependencies(${PROJECT_NAME} Intel.RealSense)

set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_REFERENCES
"System"
"System.Xaml"
"PresentationCore"
"PresentationFramework"
"WindowsBase"
)

set_target_properties (${PROJECT_NAME} PROPERTIES
FOLDER Wrappers/csharp
)
20 changes: 20 additions & 0 deletions wrappers/csharp/cs-tutorial-7-net-dev/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Linq;

namespace Intel.RealSense
{
class Program
{
[STAThread]
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine($"Please, specify network server IP address as a parameter");
return;
}
var w = new CaptureWindow(args[0]);
w.ShowDialog();
}
}
}
33 changes: 33 additions & 0 deletions wrappers/csharp/cs-tutorial-7-net-dev/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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("Intel(R) RealSense(TM) SDK C# Wrapper Tutorial-7")]
[assembly: AssemblyDescription("Intel(R) RealSense(TM) SDK C# Wrapper Examples")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Intel(R) Corporation")]
[assembly: AssemblyProduct("Intel(R) RealSense(TM) SDK C# Wrapper")]
[assembly: AssemblyCopyright("Copyright © 2021, Intel Corporation. All rights reserved")]
[assembly: AssemblyTrademark("Intel(R) RealSense(TM)")]
[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("71c85ee0-c7c2-4e2e-8020-6af818537a14")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
21 changes: 21 additions & 0 deletions wrappers/csharp/cs-tutorial-7-net-dev/Window.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Window x:Class="Intel.RealSense.CaptureWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Intel.RealSense"
mc:Ignorable="d" WindowStartupLocation="CenterOwner"
Closing="control_Closing"
Title="Intel RealSense Capture .NET Example" Height="700" Width="1200" x:Name="control">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<TextBox x:Name="txtTimeStamp" Grid.Column="0" />

<Image x:Name="imgDepth" Grid.Column="0" />
<Image x:Name="imgColor" Grid.Column="1" />
</Grid>
</Window>
121 changes: 121 additions & 0 deletions wrappers/csharp/cs-tutorial-7-net-dev/Window.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Intel.RealSense
{
/// <summary>
/// Interaction logic for Window.xaml
/// </summary>
public partial class CaptureWindow : Window
{
private Pipeline pipeline;
private Colorizer colorizer;
private CancellationTokenSource tokenSource = new CancellationTokenSource();

static Action<VideoFrame> UpdateImage(Image img)
{
var wbmp = img.Source as WriteableBitmap;
return new Action<VideoFrame>(frame =>
{
var rect = new Int32Rect(0, 0, frame.Width, frame.Height);
wbmp.WritePixels(rect, frame.Data, frame.Stride * frame.Height, frame.Stride);
});
}

public CaptureWindow(string serverIp)
{
InitializeComponent();

try
{
Action<VideoFrame> updateDepth;
Action<VideoFrame> updateColor;

// The colorizer processing block will be used to visualize the depth frames.
colorizer = new Colorizer();


using (var ctx = new Context())
{
Console.WriteLine($"Connecting to server {serverIp}...");
var dev = new NetDevice(serverIp);
dev.AddTo(ctx);

// Create and config the pipeline to strem color and depth frames.
pipeline = new Pipeline(ctx);

// show device
Console.WriteLine($"Using device 0, {dev.Info[CameraInfo.Name]}" +
$"\tSerial number: {dev.Info[CameraInfo.SerialNumber]}");

var pp = pipeline.Start();

SetupWindow(pp, out updateDepth, out updateColor);
}

Task.Factory.StartNew(() =>
{
while (!tokenSource.Token.IsCancellationRequested)
{
// We wait for the next available FrameSet and using it as a releaser object that would track
// all newly allocated .NET frames, and ensure deterministic finalization
// at the end of scope.
using (var frames = pipeline.WaitForFrames())
{
var colorFrame = frames.ColorFrame.DisposeWith(frames);
var depthFrame = frames.DepthFrame.DisposeWith(frames);

// We colorize the depth frame for visualization purposes
var colorizedDepth = colorizer.Process<VideoFrame>(depthFrame).DisposeWith(frames);

// Render the frames.
Dispatcher.Invoke(DispatcherPriority.Render, updateDepth, colorizedDepth);
Dispatcher.Invoke(DispatcherPriority.Render, updateColor, colorFrame);

Dispatcher.Invoke(new Action(() =>
{
String depth_dev_sn = depthFrame.Sensor.Info[CameraInfo.SerialNumber];
txtTimeStamp.Text = depth_dev_sn + " : " + String.Format("{0,-20:0.00}", depthFrame.Timestamp) + "(" + depthFrame.TimestampDomain.ToString() + ")";
}));
}
}
}, tokenSource.Token);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.Current.Shutdown();
}
}

private void control_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
tokenSource.Cancel();
}

private void SetupWindow(PipelineProfile pipelineProfile, out Action<VideoFrame> depth, out Action<VideoFrame> color)
{
using (var p = pipelineProfile.GetStream(Stream.Depth).As<VideoStreamProfile>())
imgDepth.Source = new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Rgb24, null);
depth = UpdateImage(imgDepth);

using (var p = pipelineProfile.GetStream(Stream.Color).As<VideoStreamProfile>())
imgColor.Source = new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Rgb24, null);
color = UpdateImage(imgColor);
}
}
}
3 changes: 3 additions & 0 deletions wrappers/csharp/cs-tutorial-7-net-dev/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

0 comments on commit e8bf11e

Please sign in to comment.