Commit 90809693 authored by Martin Finkel's avatar Martin Finkel

Interop: Fix 360 viewpoint change

Add Viewpoint unit test and API comments

Close #101

(cherry picked from commit 307abf86e2b752d1af4a79436f89babe0942373b)
parent e9bece6d
......@@ -171,5 +171,26 @@ namespace LibVLCSharp.Tests
Assert.AreEqual(IntPtr.Zero, mp.NativeReference);
}
[Test]
public async Task UpdateViewpoint()
{
var libvlc = new LibVLC();
var mp = new MediaPlayer(libvlc);
mp.Play(new Media(libvlc, "https://streams.videolan.org/streams/360/eagle_360.mp4", Media.FromType.FromLocation));
await Task.Delay(1000);
var result = mp.UpdateViewpoint(yaw: 0, pitch: 90, roll: 0, fov: 0);
Assert.IsTrue(result);
await Task.Delay(1000);
mp.Dispose();
Assert.AreEqual(IntPtr.Zero, mp.NativeReference);
}
}
}
......@@ -39,7 +39,9 @@ namespace LibVLCSharp.Shared.Helpers
#endregion
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "libvlc_free", SetLastError = true)]
public static extern void LibVLCFree(IntPtr ptr);
const string Write = "w";
}
......@@ -321,6 +323,7 @@ namespace LibVLCSharp.Shared.Helpers
}
/// <summary>
/// Marshal a pointer to a struct
/// Helper with netstandard1.1 and net40 support
/// </summary>
/// <typeparam name="T"></typeparam>
......@@ -334,7 +337,7 @@ namespace LibVLCSharp.Shared.Helpers
return Marshal.PtrToStructure<T>(ptr);
#endif
}
/// <summary>
/// Crossplatform dlopen
/// </summary>
......@@ -403,6 +406,20 @@ namespace LibVLCSharp.Shared.Helpers
}
#endif
}
/// <summary>
/// Frees an heap allocation returned by a LibVLC function.
/// If you know you're using the same underlying C run-time as the LibVLC
/// implementation, then you can call ANSI C free() directly instead.
/// </summary>
/// <param name="ptr">the pointer</param>
internal static void LibVLCFree(ref IntPtr ptr)
{
if (ptr == IntPtr.Zero) return;
Native.LibVLCFree(ptr);
ptr = IntPtr.Zero;
}
}
[AttributeUsage(AttributeTargets.Method)]
......
......@@ -596,10 +596,13 @@ namespace LibVLCSharp.Shared
internal static extern int LibVLCMediaPlayerAddSlave(IntPtr mediaPlayer, MediaSlaveType mediaSlaveType,
[MarshalAs(UnmanagedType.LPStr)] string uri, bool selectWhenloaded);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_new_viewpoint")]
internal static extern IntPtr LibVLCVideoNewViewpoint();
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_update_viewpoint")]
internal static extern int LibVLCVideoUpdateViewpoint(IntPtr mediaPlayer, VideoViewpoint viewpoint, bool absolute);
internal static extern int LibVLCVideoUpdateViewpoint(IntPtr mediaPlayer, IntPtr viewpoint, bool absolute);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
......@@ -1560,12 +1563,36 @@ namespace LibVLCSharp.Shared
Native.LibVLCMediaPlayerAddSlave(NativeReference, type, uri, select) == 0;
/// <summary>
/// Current 360 viewpoint of this mediaplayer.
/// <para/>Update with <see cref="UpdateViewpoint"/>
/// </summary>
/// <param name="viewpoint"></param>
/// <param name="absolute"></param>
/// <returns></returns>
public bool UpdateViewpoint(VideoViewpoint viewpoint, bool absolute) =>
Native.LibVLCVideoUpdateViewpoint(NativeReference, viewpoint, absolute) == 0;
public VideoViewpoint Viewpoint { get; private set; }
/// <summary>
/// Update the video viewpoint information.
/// The values are set asynchronously, it will be used by the next frame displayed.
/// It is safe to call this function before the media player is started.
/// LibVLC 3.0.0 and later
/// </summary>
/// <param name="yaw">view point yaw in degrees ]-180;180]</param>
/// <param name="pitch">view point pitch in degrees ]-90;90]</param>
/// <param name="roll">view point roll in degrees ]-180;180]</param>
/// <param name="fov">field of view in degrees ]0;180[ (default 80.)</param>
/// <param name="absolute">if true replace the old viewpoint with the new one. If false, increase/decrease it.</param>
/// <returns>true if successful, false otherwise</returns>
public bool UpdateViewpoint(float yaw, float pitch, float roll, float fov, bool absolute = true)
{
var vpPtr = Native.LibVLCVideoNewViewpoint();
if (vpPtr == IntPtr.Zero) return false;
Viewpoint = new VideoViewpoint { Yaw = yaw, Pitch = pitch, Roll = roll, Fov = fov };
Marshal.StructureToPtr(Viewpoint, vpPtr, false);
var result = Native.LibVLCVideoUpdateViewpoint(NativeReference, vpPtr, absolute) == 0;
MarshalUtils.LibVLCFree(ref vpPtr);
return result;
}
public bool SetRenderer(RendererItem rendererItem) =>
Native.LibVLCMediaPlayerSetRenderer(NativeReference, rendererItem.NativeReference) == 0;
......
......@@ -3,13 +3,27 @@
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 struct VideoViewpoint
{
public readonly float Yaw;
public readonly float Pitch;
public readonly float Roll;
public readonly float Fov;
/// <summary>
/// view point yaw in degrees ]-180;180]
/// </summary>
public float Yaw { get; internal set; }
/// <summary>
/// view point pitch in degrees ]-90;90]
/// </summary>
public float Pitch { get; internal set; }
/// <summary>
/// view point roll in degrees ]-180;180]
/// </summary>
public float Roll { get; internal set; }
/// <summary>
/// field of view in degrees ]0;180[ (default 80.)
/// </summary>
public float Fov { get; internal set; }
}
}
}
\ 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