Commit f8604516 authored by Martin Finkel's avatar Martin Finkel

More readonly structs, moving struct to Structures folder and marshalling logic refactoring

parent 9f3bdca7
namespace LibVLCSharp.Shared
{
public readonly struct AudioTrack
{
public readonly uint Channels;
public readonly uint Rate;
}
}
......@@ -40,5 +40,13 @@ namespace LibVLCSharp.Shared.Helpers
/// <returns>public TrackDescription to be consumed by the user</returns>
internal static TrackDescription Build(this TrackDescriptionStructure s) =>
new TrackDescription(s.Id, Utf8StringMarshaler.GetInstance().MarshalNativeToManaged(s.Name) as string);
/// <summary>
/// Helper method that creates a managed type from the internal interop structure.
/// </summary>
/// <param name="s">TrackDescriptionStructure from interop</param>
/// <returns>public TrackDescription to be consumed by the user</returns>
internal static MediaSlave Build(this MediaSlaveStructure s) =>
new MediaSlave(Utf8StringMarshaler.GetInstance().MarshalNativeToManaged(s.Uri) as string, s.Type, s.Priority);
}
}
\ No newline at end of file
......@@ -98,6 +98,38 @@ namespace LibVLCSharp.Shared.Helpers
return resultList.ToArray();
}
// ulong or uint?
internal static TU[] Retrieve<T, TU>(IntPtr nativeRef, Func<IntPtr, IntPtr, uint> getRef, Func<IntPtr, T> retrieve,
Func<T, TU> create, Action<IntPtr, uint> releaseRef)
{
var arrayPtr = IntPtr.Zero;
var count = getRef(nativeRef, arrayPtr);
if(count == 0)
{
#if NETSTANDARD1_1 || NET40
return new TU[0];
#else
return Array.Empty<TU>();
#endif
}
var resultList = new List<TU>();
T structure;
for (var i = 0; i < count; i++)
{
var ptr = Marshal.ReadIntPtr(arrayPtr, i * IntPtr.Size);
structure = retrieve(ptr);
var managedStruct = create(structure);
resultList.Add(managedStruct);
}
releaseRef(arrayPtr, count);
arrayPtr = IntPtr.Zero;
return resultList.ToArray();
}
/// <summary>
/// Turns an array of UTF16 C# strings to an array of pointer to UTF8 strings
/// </summary>
......
......@@ -628,25 +628,10 @@ namespace LibVLCSharp.Shared
/// <para>LibVLC 3.0.0 and later.</para>
/// <para>libvlc_media_slaves_add</para>
/// </remarks>
public IEnumerable<MediaSlave> Slaves
{
get
{
var slaveArrayPtr = IntPtr.Zero;
var count = Native.LibVLCMediaGetSlaves(NativeReference, ref slaveArrayPtr);
if (count == 0) return Enumerable.Empty<MediaSlave>();
var slaves = new List<MediaSlave>();
for (var i = 0; i < count; i++)
{
var ptr = Marshal.ReadIntPtr(slaveArrayPtr, i * IntPtr.Size);
var managedStruct = MarshalUtils.PtrToStructure<MediaSlave>(ptr);
slaves.Add(managedStruct);
}
Native.LibVLCMediaReleaseSlaves(slaveArrayPtr, count);
return slaves;
}
}
public IEnumerable<MediaSlave> Slaves => MarshalUtils.Retrieve(NativeReference, (nativeRef, arrayPtr) => Native.LibVLCMediaGetSlaves(nativeRef, ref arrayPtr),
MarshalUtils.PtrToStructure<MediaSlaveStructure>,
s => s.Build(),
Native.LibVLCMediaReleaseSlaves);
public override bool Equals(object obj)
{
......@@ -924,59 +909,7 @@ namespace LibVLCSharp.Shared
#endregion
#region Structs
[StructLayout(LayoutKind.Sequential)]
public struct MediaStats
{
/* Input */
public int ReadBytes;
public float InputBitrate;
/* Demux */
public int DemuxReadBytes;
public float DemuxBitrate;
public int DemuxCorrupted;
public int DemuxDiscontinuity;
/* Decoders */
public int DecodedVideo;
public int DecodedAudio;
/* Video Output */
public int DisplayedPictures;
public int LostPictures;
/* Audio output */
public int PlayedAudioBuffers;
public int LostAudioBuffers;
/* Stream output */
public int SentPackets;
public int SentBytes;
public float SendBitrate;
}
public struct MediaTrack
{
public uint Codec;
public uint OriginalFourcc;
public int Id;
public TrackType TrackType;
public int Profile;
public int Level;
public MediaTrackData Data;
public uint Bitrate;
public IntPtr Language;
public IntPtr Description;
}
public struct MediaTrackData
{
public AudioTrack Audio;
public VideoTrack Video;
public SubtitleTrack Subtitle;
}
#region enums
/// <summary>Note the order of libvlc_state_t enum must match exactly the order of</summary>
/// <remarks>
......@@ -1007,33 +940,6 @@ namespace LibVLCSharp.Shared
Text = 2
}
public struct AudioTrack
{
public uint Channels;
public uint Rate;
}
public struct VideoTrack
{
public uint Height;
public uint Width;
public uint SarNum;
public uint SarDen;
public uint FrameRateNum;
public uint FrameRateDen;
public VideoOrientation Orientation;
public VideoProjection Projection;
public VideoViewpoint Pose;
}
public enum VideoOrientation
{
/// <summary>Normal. Top line represents top, left column left.</summary>
......@@ -1063,37 +969,19 @@ namespace LibVLCSharp.Shared
CubemapLayoutStandard = 256
}
/// <summary>Viewpoint for video outputs</summary>
/// <remarks>allocate using libvlc_video_new_viewpoint()</remarks>
[StructLayout(LayoutKind.Sequential)]
public struct VideoViewpoint
{
public float Yaw;
public float Pitch;
public float Roll;
public float Fov;
}
public struct SubtitleTrack
{
public IntPtr Encoding;
}
/// <summary>Type of a media slave: subtitle or audio.</summary>
public enum MediaSlaveType
{
/// <summary>
/// Subtitle
/// </summary>
Subtitle = 0,
/// <summary>
/// Audio
/// </summary>
Audio = 1
}
/// <summary>A slave of a libvlc_media_t</summary>
/// <remarks>libvlc_media_slaves_get</remarks>
public struct MediaSlave
{
public IntPtr Uri;
public MediaSlaveType Type;
public uint Priority;
}
#endregion
public class MediaConfiguration
......
......@@ -12,7 +12,7 @@ namespace LibVLCSharp.Shared
MediaDiscovererEventManager _eventManager;
MediaList _mediaList;
struct Native
readonly struct Native
{
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
......@@ -69,9 +69,9 @@ namespace LibVLCSharp.Shared
Localdirs = 3
}
public struct Description
public readonly struct Description
{
public Description(string name, string longName, Category category)
internal Description(string name, string longName, Category category)
{
Name = name;
LongName = longName;
......
......@@ -10,7 +10,7 @@ namespace LibVLCSharp.Shared
readonly object _syncLock = new object();
bool _nativeLock;
struct Native
readonly struct Native
{
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
......
......@@ -8,7 +8,7 @@ namespace LibVLCSharp.Shared
{
public class MediaPlayer : Internal
{
struct Native
readonly struct Native
{
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
......
......@@ -9,7 +9,7 @@ namespace LibVLCSharp.Shared
{
RendererDiscovererEventManager _eventManager;
struct Native
readonly struct Native
{
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
......@@ -92,7 +92,7 @@ namespace LibVLCSharp.Shared
readonly Utf8StringMarshaler _utf8Marshaler = Utf8StringMarshaler.GetInstance();
struct Native
readonly struct Native
{
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_renderer_item_name")]
......
using System;
namespace LibVLCSharp.Shared
{
internal readonly struct MediaSlaveStructure
{
internal readonly IntPtr Uri;
internal readonly MediaSlaveType Type;
internal readonly uint Priority;
}
/// <summary>A slave of a libvlc_media_t</summary>
/// <remarks>libvlc_media_slaves_get</remarks>
public readonly struct MediaSlave
{
internal MediaSlave(string uri, MediaSlaveType type, uint priority)
{
Uri = uri;
Type = type;
Priority = priority;
}
/// <summary>
/// Media slave URI
/// </summary>
public readonly string Uri;
/// <summary>
/// Media slave type
/// </summary>
public readonly MediaSlaveType Type;
/// <summary>
/// Media slave priority
/// </summary>
public readonly uint Priority;
}
}
using System.Runtime.InteropServices;
namespace LibVLCSharp.Shared
{
#endregion
#region Structs
[StructLayout(LayoutKind.Sequential)]
public struct MediaStats
{
/* Input */
public int ReadBytes;
public float InputBitrate;
/* Demux */
public int DemuxReadBytes;
public float DemuxBitrate;
public int DemuxCorrupted;
public int DemuxDiscontinuity;
/* Decoders */
public int DecodedVideo;
public int DecodedAudio;
/* Video Output */
public int DisplayedPictures;
public int LostPictures;
/* Audio output */
public int PlayedAudioBuffers;
public int LostAudioBuffers;
/* Stream output */
public int SentPackets;
public int SentBytes;
public float SendBitrate;
}
}
using System;
namespace LibVLCSharp.Shared
{
#endregion
#region Structs
public struct MediaTrack
{
public uint Codec;
public uint OriginalFourcc;
public int Id;
public TrackType TrackType;
public int Profile;
public int Level;
public MediaTrackData Data;
public uint Bitrate;
public IntPtr Language;
public IntPtr Description;
}
}
namespace LibVLCSharp.Shared
{
#endregion
#region Structs
public struct MediaTrackData
{
public AudioTrack Audio;
public VideoTrack Video;
public SubtitleTrack Subtitle;
}
}
using System;
namespace LibVLCSharp.Shared
{
public readonly struct SubtitleTrack
{
public readonly IntPtr Encoding;
}
}
namespace LibVLCSharp.Shared
{
public readonly struct VideoTrack
{
public readonly uint Height;
public readonly uint Width;
public readonly uint SarNum;
public readonly uint SarDen;
public readonly uint FrameRateNum;
public readonly uint FrameRateDen;
public readonly VideoOrientation Orientation;
public readonly VideoProjection Projection;
public VideoViewpoint Pose;
}
}
using System.Runtime.InteropServices;
namespace LibVLCSharp.Shared
{
/// <summary>Viewpoint for video outputs</summary>
/// <remarks>allocate using libvlc_video_new_viewpoint()</remarks>
[StructLayout(LayoutKind.Sequential)]
public readonly struct VideoViewpoint
{
public readonly float Yaw;
public readonly float Pitch;
public readonly float Roll;
public readonly float Fov;
}
}
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