Commit 49e3d4aa authored by Martin Finkel's avatar Martin Finkel

Add renderer discoverer bindings

parent f2bd0714
using System.Threading.Tasks;
using NUnit.Framework;
using VideoLAN.LibVLC;
using static System.Diagnostics.Debug;
namespace LibVLCSharp.Tests
{
[TestFixture]
public class RendererDiscovererTests
{
[Test]
public async Task T()
{
var instance = new Instance();
var rendererDiscoverer = new RendererDiscoverer(instance, "microdns");
var em = rendererDiscoverer.EventManager;
em.ItemAdded += (sender, args) =>
{
WriteLine($"New item discovered: {args.RendererItem.Name} of type {args.RendererItem.Type}");
if(args.RendererItem.CanRenderVideo)
WriteLine("Can render video");
if(args.RendererItem.CanRenderAudio)
WriteLine("Can render audio");
};
if(!rendererDiscoverer.Start())
NUnit.Framework.Assert.Fail();
await Task.Delay(1000);
}
}
}
......@@ -1369,6 +1369,72 @@ namespace VideoLAN.LibVLC
}
}
public class RendererDiscovererEventManager : EventManager
{
readonly object _lock = new object();
EventHandler<RendererDiscovererItemAddedEventArgs> _itemAdded;
EventHandler<RendererDiscovererItemDeletedEventArgs> _itemDeleted;
public RendererDiscovererEventManager(IntPtr ptr) : base(ptr)
{
}
// v3
public event EventHandler<RendererDiscovererItemAddedEventArgs> ItemAdded
{
add
{
lock (_lock)
{
_itemAdded += value;
AttachEvent(EventType.RendererDiscovererItemAdded, OnItemAdded);
}
}
remove
{
lock (_lock)
{
_itemAdded -= value;
DetachEvent(EventType.RendererDiscovererItemAdded, OnItemAdded);
}
}
}
// v3
public event EventHandler<RendererDiscovererItemDeletedEventArgs> ItemDeleted
{
add
{
lock (_lock)
{
_itemDeleted += value;
AttachEvent(EventType.RendererDiscovererItemDeleted, OnItemDeleted);
}
}
remove
{
lock (_lock)
{
_itemDeleted -= value;
DetachEvent(EventType.RendererDiscovererItemDeleted, OnItemDeleted);
}
}
}
void OnItemDeleted(IntPtr args)
{
var rendererItem = RetrieveEvent(args).Union.RendererDiscovererItemDeleted.Item;
_itemDeleted?.Invoke(this, new RendererDiscovererItemDeletedEventArgs(new RendererItem(rendererItem)));
}
void OnItemAdded(IntPtr args)
{
var rendererItem = RetrieveEvent(args).Union.RendererDiscovererItemAdded.Item;
_itemAdded?.Invoke(this, new RendererDiscovererItemAddedEventArgs(new RendererItem(rendererItem)));
}
}
public class VLMEventManager : EventManager
{
readonly object _lock = new object();
......
......@@ -696,4 +696,28 @@ namespace VideoLAN.LibVLC.Events
}
#endregion
#region RendererDiscoverer events
public class RendererDiscovererItemAddedEventArgs : EventArgs
{
public RendererDiscovererItemAddedEventArgs(RendererItem rendererItem)
{
RendererItem = rendererItem;
}
public RendererItem RendererItem { get; }
}
public class RendererDiscovererItemDeletedEventArgs : EventArgs
{
public RendererDiscovererItemDeletedEventArgs(RendererItem rendererItem)
{
RendererItem = rendererItem;
}
public RendererItem RendererItem { get; }
}
#endregion
}
\ No newline at end of file
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace VideoLAN.LibVLC
{
public class RendererDiscoverer : Internal
{
struct Native
{
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_new")]
internal static extern IntPtr LibVLCRendererDiscovererNew(IntPtr instance, string name);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_release")]
internal static extern void LibVLCRendererDiscovererRelease(IntPtr rendererDiscoverer);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_start")]
internal static extern int LibVLCRendererDiscovererStart(IntPtr rendererDiscoverer);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_stop")]
internal static extern void LibVLCRendererDiscovererStop(IntPtr rendererDiscoverer);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_event_manager")]
internal static extern IntPtr LibVLCRendererDiscovererEventManager(IntPtr rendererDiscoverer);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_list_get")]
internal static extern UIntPtr LibVLCRendererDiscovererGetList(IntPtr instance, out IntPtr discovererList);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_discoverer_list_release")]
internal static extern void LibVLCRendererDiscovererReleaseList(IntPtr discovererList, UIntPtr count);
}
public RendererDiscoverer(Instance instance, string name)
: base(() => Native.LibVLCRendererDiscovererNew(instance.NativeReference, name), Native.LibVLCRendererDiscovererRelease)
{
}
RendererDiscovererEventManager _eventManager;
public RendererDiscovererEventManager EventManager
{
get
{
if (_eventManager == null)
{
var eventManagerPtr = Native.LibVLCRendererDiscovererEventManager(NativeReference);
_eventManager = new RendererDiscovererEventManager(eventManagerPtr);
}
return _eventManager;
}
}
public bool Start() => Native.LibVLCRendererDiscovererStart(NativeReference) == 0;
public void Stop() => Native.LibVLCRendererDiscovererStop(NativeReference);
}
public class RendererItem : Internal
{
const int VideoRenderer = 0x0002;
const int AudioRenderer = 0x0001;
struct Native
{
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_name")]
internal static extern string LibVLCRendererItemName(IntPtr rendererItem);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_release")]
internal static extern void LibVLCRendererItemRelease(IntPtr rendererItem);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_hold")]
internal static extern IntPtr LibVLCRendererItemHold(IntPtr rendererItem);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_type")]
internal static extern string LibVLCRendererItemType(IntPtr rendererItem);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_icon_uri")]
internal static extern string LibVLCRendererItemIconUri(IntPtr rendererItem);
[SuppressUnmanagedCodeSecurity]
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_icon_uri")]
internal static extern int LibVLCRendererItemFlags(IntPtr rendererItem);
}
public RendererItem(IntPtr reference) : base(() => reference, Native.LibVLCRendererItemRelease)
{
Native.LibVLCRendererItemHold(reference);
}
public string Name => Native.LibVLCRendererItemName(NativeReference);
public string Type => Native.LibVLCRendererItemType(NativeReference);
public string IconUri => Native.LibVLCRendererItemIconUri(NativeReference);
public bool CanRenderVideo => (Native.LibVLCRendererItemFlags(NativeReference) & VideoRenderer) != 0;
public bool CanRenderAudio => (Native.LibVLCRendererItemFlags(NativeReference) & AudioRenderer) != 0;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment