Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.17.0)
# marks debug dlls
set(CMAKE_DEBUG_POSTFIX _d)

add_subdirectory(csharp)
add_subdirectory(empty)
add_subdirectory(dummy)
add_subdirectory(websocket)
39 changes: 39 additions & 0 deletions plugins/csharp/CLI/Bridge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <msclr/marshal.h>
#include <msclr/marshal_windows.h>

#include "Bridge.h"

#using MANAGED_PLUGIN_DLL

namespace neurala::dotnet {
void initialize() {
Neurala::Program::Main();
}
}

namespace neurala::dotnet::result_output {
void invokeResultOutput(const char* metadata, const void* imageBytes, int width, int height) {
Neurala::ResultOutput::Invoke(msclr::interop::marshal_as<System::String^>(metadata),
msclr::interop::marshal_as<System::IntPtr>(const_cast<void*>(imageBytes)),
width,
height);
}
}

namespace neurala::dotnet::video_source {
void getMetadata(int& width, int& height) {
Neurala::VideoSource::GetMetadata(width, height);
}

void moveNextFrame(int& status) {
Neurala::VideoSource::MoveNextFrame(status);
}

void getFrame(void* buffer) {
Neurala::VideoSource::GetFrame(msclr::interop::marshal_as<System::IntPtr>(buffer));
}

void execute(const char* action) {
Neurala::VideoSource::Execute(msclr::interop::marshal_as<System::String^>(action));
}
}
17 changes: 17 additions & 0 deletions plugins/csharp/CLI/Bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace neurala::dotnet {
void initialize();
}

namespace neurala::dotnet::result_output {
void invokeResultOutput(const char* metadata, const void* imageBytes, int width, int height);
}

namespace neurala::dotnet::video_source {
void getMetadata(int& width, int& height);

void moveNextFrame(int& status);

void getFrame(void* buffer);

void execute(const char* action);
}
21 changes: 21 additions & 0 deletions plugins/csharp/CLI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
add_library(cppcli SHARED Bridge.cpp)

set_target_properties(cppcli
PROPERTIES
COMMON_LANGUAGE_RUNTIME ""
CXX_STANDARD 17
VS_DOTNET_REFERENCES "System"
WINDOWS_EXPORT_ALL_SYMBOLS ON)

target_compile_definitions(cppcli
PRIVATE
"MANAGED_PLUGIN_DLL=\"${MANAGED_PLUGIN_DLL}\"")

target_link_libraries(cppcli
PRIVATE
csharp_assemblies)

add_custom_command(TARGET cppcli POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${MANAGED_PLUGIN_DLL}"
$<TARGET_FILE_DIR:cppcli>)
9 changes: 9 additions & 0 deletions plugins/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_custom_command(OUTPUT Managed.dll
COMMAND dotnet build "${CMAKE_CURRENT_SOURCE_DIR}/Managed" -c Release)

add_library(csharp_assemblies INTERFACE Managed.dll)

set(MANAGED_PLUGIN_DLL "${CMAKE_CURRENT_SOURCE_DIR}/Managed/bin/x64/Release/net472/Managed.dll")

add_subdirectory(CLI)
add_subdirectory(Native)
12 changes: 12 additions & 0 deletions plugins/csharp/Managed/Managed.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<Platforms>x64</Platforms>
<LangVersion>latest</LangVersion>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions plugins/csharp/Managed/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Neurala {
public static class Program {
public static void Main() {
var imageFiles = Directory.EnumerateFiles(@"C:\Images");

while (true) {
foreach (var imageFile in imageFiles) {
Console.WriteLine(imageFile);

var bitmap = new Bitmap(imageFile);
var result = VideoSource.SendImage(bitmap);
}
}
}
}
}
10 changes: 10 additions & 0 deletions plugins/csharp/Managed/ResultOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Runtime.InteropServices;

namespace Neurala {
public static class ResultOutput {
public static void Invoke(string metadata, IntPtr imageData, int width, int height) {
VideoSource.PushResult(metadata);
}
}
}
99 changes: 99 additions & 0 deletions plugins/csharp/Managed/VideoSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace Neurala {
public static class VideoSource {
private static Object Lock;
private static Bitmap CurrentBitmap;
private static String Result;
private static Int32 Timeout;

public static int FPS {
get {
return 1000 / Timeout;
}
set {
Timeout = 1000 / value;
}
}

static VideoSource() {
Lock = typeof(VideoSource);
Timeout = 1000 / 60;
}

public static string SendImage(Bitmap bitmap) {
lock (Lock) {
while (CurrentBitmap != null)
Monitor.Wait(Lock);

CurrentBitmap = bitmap;
Monitor.Pulse(Lock);

if (Result == null)
Monitor.Wait(Lock, Timeout);

var result = Result ?? "";
Result = null;
return result;
}
}

internal static void PushResult(string result) {
lock (Lock) {
Result = result;
Monitor.Pulse(Lock);
}
}

private static Bitmap WaitForImage() {
lock (Lock) {
while (CurrentBitmap == null)
Monitor.Wait(Lock);
return CurrentBitmap;
}
}

// Return (width, height) of current image.
public static void GetMetadata(out int width, out int height) {
var image = WaitForImage();

width = image.Width;
height = image.Height;
}

public static void MoveNextFrame(out int status) {
lock (Lock) {
CurrentBitmap = null;
Monitor.Pulse(Lock);
}

status = 0;
}

// Get current image frame data and copy to buffer.
public static void GetFrame(IntPtr buffer) {
var image = WaitForImage();
var bits = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly,
image.PixelFormat);

CopyMemory(buffer, bits.Scan0, image.Width * image.Height * 3);

image.UnlockBits(bits);
}

// Execute output action.
public static void Execute(string action) {
Console.WriteLine($"Execute(\"{action}\")");
}

[DllImport("kernel32.dll")]
private static extern void CopyMemory(IntPtr destination, IntPtr source, int length);
}
}
35 changes: 35 additions & 0 deletions plugins/csharp/Native/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.17.0)

set(CMAKE_CXX_STANDARD 17)

add_library(dotnetCamera
SHARED
src/Plugin.cpp
src/ResultOutput.cpp
src/VideoSource.cpp)

target_compile_definitions(dotnetCamera PRIVATE NEURALA_EXPORT_PLUGIN)

add_executable(dotnetCameraTest
src/Main.cpp)

if(WIN32)
set_target_properties(dotnetCamera
PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

target_include_directories(dotnetCamera
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/../CLI")

target_link_libraries(dotnetCamera
PUBLIC
cppcli
stub)

target_link_libraries(dotnetCameraTest
PRIVATE
${CMAKE_DL_LIBS}
dotnetCamera)
42 changes: 42 additions & 0 deletions plugins/csharp/Native/include/ResultOutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Neurala Inc. 2013-2021
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: The above copyright notice and this
* permission notice (including the next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef NEURALA_CSHARP_RESULTS_OUTPUT_H
#define NEURALA_CSHARP_RESULTS_OUTPUT_H

#include "neurala/plugin/PluginArguments.h"
#include "neurala/plugin/PluginBindings.h"
#include "neurala/plugin/PluginErrorCallback.h"
#include "neurala/plugin/PluginManager.h"
#include "neurala/plugin/PluginStatus.h"
#include "neurala/utils/Version.h"
#include "neurala/utils/ResultsOutput.h"

namespace neurala
{
class PLUGIN_API CSharpResultOutput : public ResultsOutput
{
public:
virtual void operator()(const std::string& metadata, const dto::ImageView* image) noexcept override;

static void* create(PluginArguments&, PluginErrorCallback&);
static void destroy(void* p);
};
} // namespace neurala

#endif // NEURALA_CSHARP_RESULTS_OUTPUT_H
67 changes: 67 additions & 0 deletions plugins/csharp/Native/include/VideoSource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) Neurala Inc. 2013-2021
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: The above copyright notice and this
* permission notice (including the next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef NEURALA_CSHARP_VIDEO_SOURCE_H
#define NEURALA_CSHARP_VIDEO_SOURCE_H

#include <vector>

#include "neurala/plugin/PluginArguments.h"
#include "neurala/plugin/PluginBindings.h"
#include "neurala/plugin/PluginErrorCallback.h"
#include "neurala/plugin/PluginManager.h"
#include "neurala/plugin/PluginStatus.h"
#include "neurala/utils/Version.h"
#include "neurala/video/dto/CameraInfo.h"
#include "neurala/video/CameraDiscoverer.h"
#include "neurala/video/VideoSource.h"

namespace neurala
{
class PLUGIN_API CSharpCameraDiscoverer : public CameraDiscoverer
{
public:
[[nodiscard]] std::vector<dto::CameraInfo> operator()() const noexcept override;

static void* create(PluginArguments&, PluginErrorCallback&);
static void destroy(void* p);
};

class PLUGIN_API CSharpVideoSource : public VideoSource
{
private:
mutable std::vector<std::byte> imageBytes;
mutable dto::ImageView currentFrame;

public:
[[nodiscard]] virtual dto::ImageMetadata metadata() const noexcept override;

[[nodiscard]] virtual std::error_code nextFrame() noexcept override;

[[nodiscard]] virtual dto::ImageView frame() const noexcept override;

[[nodiscard]] virtual dto::ImageView frame(std::byte* data, std::size_t capacity) const noexcept override;

[[nodiscard]] virtual std::error_code execute(const std::string& action) noexcept override;

static void* create(PluginArguments&, PluginErrorCallback&);
static void destroy(void*);
};
} // namespace neurala

#endif // NEURALA_CSHARP_VIDEO_SOURCE_H
Loading