Commit bae443d4 authored by Martin Finkel's avatar Martin Finkel

remove async parse for now

parent cb23e297
using System.IO;
using System;
using System.IO;
using System.Threading.Tasks;
using LibVLCSharp.Shared;
using NUnit.Framework;
......@@ -8,8 +9,6 @@ namespace LibVLCSharp.Tests
[TestFixture]
public class EventManagerTests : BaseSetup
{
string RealMediaPath => Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "sample.mp3");
[Test]
public void MetaChangedEventSubscribe()
{
......@@ -26,12 +25,9 @@ namespace LibVLCSharp.Tests
Assert.True(eventHandlerCalled);
}
[Test]
var media = new Media(instance, RealMp3Path, Media.FromType.FromPath);
public void DurationChanged()
{
var media = new Media(new Instance(), RealMp3Path, Media.FromType.FromPath);
var media = new Media(new Instance(), RealMp3Path);
var called = false;
long duration = 0;
......@@ -50,7 +46,7 @@ namespace LibVLCSharp.Tests
[Test]
public void FreedMedia()
{
var media = new Media(new Instance(), RealMp3Path, Media.FromType.FromPath);
var media = new Media(new Instance(), RealMp3Path);
var eventCalled = false;
media.EventManager.MediaFreed += (sender, args) =>
{
......@@ -65,7 +61,7 @@ namespace LibVLCSharp.Tests
[Test]
public async Task StateChanged()
{
var media = new Media(new Instance(), RealMp3Path, Media.FromType.FromPath);
var media = new Media(new Instance(), RealMp3Path);
var tcs = new TaskCompletionSource<bool>();
var openingCalled = false;
media.EventManager.StateChanged += (sender, args) =>
......
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LibVLCSharp.Shared;
using NUnit.Framework;
......@@ -16,7 +16,7 @@ namespace LibVLCSharp.Tests
{
var instance = new Instance();
var media = new Media(instance, Path.GetTempFileName(), Media.FromType.FromPath);
var media = new Media(instance, Path.GetTempFileName());
Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
}
......@@ -24,14 +24,14 @@ namespace LibVLCSharp.Tests
[Test]
public void CreateMediaFail()
{
Assert.Throws<ArgumentNullException>(() => new Media(null, Path.GetTempFileName(), Media.FromType.FromPath));
Assert.Throws<ArgumentNullException>(() => new Media(new Instance(), string.Empty, Media.FromType.FromPath));
Assert.Throws<ArgumentNullException>(() => new Media(null, Path.GetTempFileName()));
Assert.Throws<ArgumentNullException>(() => new Media(new Instance(), string.Empty));
}
[Test]
public void ReleaseMedia()
{
var media = new Media(new Instance(), Path.GetTempFileName(), Media.FromType.FromPath);
var media = new Media(new Instance(), Path.GetTempFileName());
media.Dispose();
......@@ -75,7 +75,6 @@ namespace LibVLCSharp.Tests
}
}
string RealMediaPath => Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "sample.mp3");
[Test]
public void Duplicate()
{
......@@ -87,7 +86,7 @@ namespace LibVLCSharp.Tests
[Test]
public void CreateMediaFromFileStream()
{
var media = new Media(new Instance(), new FileStream(RealMediaPath, FileMode.Open, FileAccess.Read, FileShare.Read));
var media = new Media(new Instance(), new FileStream(RealMp3Path, FileMode.Open, FileAccess.Read, FileShare.Read));
media.Parse();
Assert.NotZero(media.Tracks.First().Data.Audio.Channels);
}
......@@ -95,7 +94,7 @@ namespace LibVLCSharp.Tests
[Test]
public void SetMetadata()
{
var media = new Media(new Instance(), Path.GetTempFileName(), Media.FromType.FromPath);
var media = new Media(new Instance(), Path.GetTempFileName());
const string test = "test";
media.SetMeta(Media.MetadataType.ShowName, test);
Assert.True(media.SaveMeta());
......@@ -103,47 +102,11 @@ namespace LibVLCSharp.Tests
}
[Test]
public async Task AsyncParse()
public void GetTracks()
{
var media = new Media(new Instance(), RealMediaPath, Media.FromType.FromPath);
var result = await media.ParseAsyncWithOptions();
Assert.True(result);
}
[Test]
public async Task AsyncParseTimeoutStop()
{
//TODO: fix
Assert.Inconclusive();
var media = new Media(new Instance(), RealMediaPath, Media.FromType.FromPath);
var called = false;
media.EventManager.ParsedChanged += (sender, args) =>
{
Assert.True(args.ParsedStatus == Media.MediaParsedStatus.Timeout);
called = true;
};
var result = await media.ParseAsyncWithOptions(timeout: 1);
Assert.False(result);
Assert.True(called);
}
[Test]
public async Task AsyncParseCancel()
{
//TODO: fix
Assert.Inconclusive();
var media = new Media(new Instance(), RealMediaPath, Media.FromType.FromPath);
var called = false;
media.EventManager.ParsedChanged += (sender, args) =>
{
Assert.True(args.ParsedStatus == Media.MediaParsedStatus.Failed);
called = true;
};
// current code cancels tasks before the parsing even starts so parseStatus is never set to failed.
var result = await media.ParseAsyncWithOptions(cancellationToken: new CancellationToken(canceled: true));
Assert.False(result);
Assert.True(called);
var media = new Media(new Instance(), RealMp3Path);
media.Parse();
Assert.AreEqual(1, media.Tracks);
}
}
}
......@@ -5,9 +5,6 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using VideoLAN.LibVLC.Events;
namespace LibVLCSharp.Shared
{
......@@ -544,124 +541,10 @@ namespace LibVLCSharp.Shared
/// </summary>
public void Parse() => Native.LibVLCMediaParse(NativeReference);
/// <summary>Parse a media.
/// This fetches (local) art, meta data and tracks information.
/// The method is the asynchronous of libvlc_media_parse().
/// To track when this is over you can listen to libvlc_MediaParsedChanged
/// event. However if the media was already parsed you will not receive this
/// event.
/// You can't be sure to receive the libvlc_MediaParsedChanged
/// event (you can wait indefinitely for this event).
/// Use libvlc_media_parse_with_options() instead
/// libvlc_media_parse
/// libvlc_MediaParsedChanged
/// libvlc_media_get_meta
/// libvlc_media_get_tracks_info
/// </summary>
/// TODO: return task by checking libvlc_MediaParsedChanged
public Task ParseAsync()
{
Native.LibVLCMediaParseAsync(NativeReference);
return Task.CompletedTask;
}
/// <summary>Return true is the media descriptor object is parsed</summary>
/// <returns>true if media object has been parsed otherwise it returns false</returns>
public bool IsParsed => Native.LibVLCMediaIsParsed(NativeReference) != 0;
/// <summary>Parse the media asynchronously with options.</summary>
/// <param name="parseOptions">parse options</param>
/// <param name="token">cancellation cancellationToken</param>
/// <param name="timeout">
/// <para>maximum time allowed to preparse the media. If -1, the</para>
/// <para>default &quot;preparse-timeout&quot; option will be used as a timeout. If 0, it will</para>
/// <para>wait indefinitely. If &gt; 0, the timeout will be used (in milliseconds).</para>
/// </param>
/// <returns>-1 in case of error, 0 otherwise</returns>
/// <remarks>
/// <para>This fetches (local or network) art, meta data and/or tracks information.</para>
/// <para>This method is the extended version of libvlc_media_parse_with_options().</para>
/// <para>To track when this is over you can listen to libvlc_MediaParsedChanged</para>
/// <para>event. However if this functions returns an error, you will not receive any</para>
/// <para>events.</para>
/// <para>It uses a flag to specify parse options (see libvlc_media_parse_flag_t). All</para>
/// <para>these flags can be combined. By default, media is parsed if it's a local</para>
/// <para>file.</para>
/// <para>Parsing can be aborted with libvlc_media_parse_stop().</para>
/// <para>libvlc_MediaParsedChanged</para>
/// <para>libvlc_media_get_meta</para>
/// <para>libvlc_media_tracks_get</para>
/// <para>libvlc_media_get_parsed_status</para>
/// <para>libvlc_media_parse_flag_t</para>
/// <para>LibVLC 3.0.0 or later</para>
/// </remarks>
public Task<bool> ParseAsyncWithOptions(MediaParseOptions parseOptions = MediaParseOptions.ParseLocal, CancellationToken cancellationToken = default(CancellationToken),
int timeout = -1)
{
return ParseAsyncWithOptionsInternal(() =>
Native.LibVLCMediaParseWithOptions(NativeReference, parseOptions, 0) != 0, cancellationToken, timeout);
}
TaskCompletionSource<bool> _tcs;
async Task<bool> ParseAsyncWithOptionsInternal(Func<bool> nativeCall, CancellationToken cancellationToken, int timeout)
{
try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();
_tcs = new TaskCompletionSource<bool>();
var timeoutToken = new CancellationTokenSource(timeout);
EventManager.ParsedChanged += OnParsedChanged;
cancellationToken.Register(() =>
{
EventManager.ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
_tcs?.TrySetCanceled();
});
timeoutToken.Token.Register(() =>
{
EventManager.ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
_tcs?.TrySetCanceled();
});
var result = nativeCall();
}
catch (OperationCanceledException)
{
_tcs?.TrySetCanceled();
return false;
}
catch (Exception ex)
{
_tcs?.TrySetException(ex);
return false;
}
finally
{
}
//EventManager.ParsedChanged -= OnParsedChanged;
if (_tcs == null)
return false;
return await _tcs.Task;
}
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
{
if (ParsedStatus == MediaParsedStatus.Done)
_tcs?.TrySetResult(true);
else if (ParsedStatus == MediaParsedStatus.Failed)
_tcs?.TrySetException(new VLCException($"parsing of {this} failed"));
else _tcs?.TrySetResult(false);
EventManager.ParsedChanged -= OnParsedChanged;
}
/// <summary>Get Parsed status for media descriptor object.</summary>
/// <returns>a value of the libvlc_media_parsed_status_t enum</returns>
/// <remarks>
......@@ -680,13 +563,6 @@ namespace LibVLCSharp.Shared
/// </remarks>
public void ParseStop() => Native.LibVLCMediaParseStop(NativeReference);
// TODO: Whats userData?
//public IntPtr UserData
//{
// get => Native.LibVLCMediaGetUserData(NativeReference);
// set => Native.LibVLCMediaSetUserData(NativeReference, IntPtr.Zero);
//}
/// <summary>Get media descriptor's elementary streams description</summary>
/// <para>address to store an allocated array of Elementary Streams</para>
/// <para>descriptions (must be freed with libvlc_media_tracks_release</para>
......@@ -727,15 +603,8 @@ namespace LibVLCSharp.Shared
/// <para>libvlc_media_list_release() to decrement the reference counting.</para>
/// </summary>
/// <returns>list of media descriptor subitems or NULL</returns>
public MediaList SubItems
{
get
{
var ptr = Native.LibVLCMediaSubitems(NativeReference);
return new MediaList(ptr); //should cache MediaList C# object.
}
}
public MediaList SubItems => new MediaList(Native.LibVLCMediaSubitems(NativeReference));
public MediaType Type => Native.LibVLCMediaGetType(NativeReference);
/// <summary>Add a slave to the current media.</summary>
......@@ -1085,7 +954,6 @@ namespace LibVLCSharp.Shared
public uint Rate;
}
public struct VideoTrack
{
public uint Height;
......@@ -1136,7 +1004,6 @@ namespace LibVLCSharp.Shared
CubemapLayoutStandard = 256
}
/// <summary>Viewpoint for video outputs</summary>
/// <remarks>allocate using libvlc_video_new_viewpoint()</remarks>
[StructLayout(LayoutKind.Sequential)]
......
//// ----------------------------------------------------------------------------
//// <auto-generated>
//// This is autogenerated code by CppSharp.
//// Do not edit this file or all your changes will be lost after re-generation.
//// </auto-generated>
//// ----------------------------------------------------------------------------
//using System;
//using System.Runtime.InteropServices;
//using System.Security;
//namespace LibVLCSharp
//{
// [Flags]
// public enum MediaOption
// {
// Trusted = 2,
// Unique = 256
// }
// //public unsafe partial class Media
// //{
// // [StructLayout(LayoutKind.Explicit, Size = 0)]
// // public partial struct __Internal
// // {
// // }
// // public global::System.IntPtr NativeReference { get; protected set; }
// // protected int __PointerAdjustment;
// // internal static readonly global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::VideoLAN.LibVLCSharp.Media> NativeToManagedMap = new global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::VideoLAN.LibVLCSharp.Media>();
// // protected void*[] __OriginalVTables;
// // protected bool __ownsNativeInstance;
// // internal static global::VideoLAN.LibVLCSharp.Media __CreateInstance(global::System.IntPtr native, bool skipVTables = false)
// // {
// // return new global::VideoLAN.LibVLCSharp.Media(native.ToPointer(), skipVTables);
// // }
// // internal static global::VideoLAN.LibVLCSharp.Media __CreateInstance(global::VideoLAN.LibVLCSharp.Media.__Internal native, bool skipVTables = false)
// // {
// // return new global::VideoLAN.LibVLCSharp.Media(native, skipVTables);
// // }
// // private static void* __CopyValue(global::VideoLAN.LibVLCSharp.Media.__Internal native)
// // {
// // var ret = Marshal.AllocHGlobal(sizeof(global::VideoLAN.LibVLCSharp.Media.__Internal));
// // *(global::VideoLAN.LibVLCSharp.Media.__Internal*) ret = native;
// // return ret.ToPointer();
// // }
// // private Media(global::VideoLAN.LibVLCSharp.Media.__Internal native, bool skipVTables = false)
// // : this(__CopyValue(native), skipVTables)
// // {
// // __ownsNativeInstance = true;
// // NativeToManagedMap[NativeReference] = this;
// // }
// // protected Media(void* native, bool skipVTables = false)
// // {
// // if (native == null)
// // return;
// // NativeReference = new global::System.IntPtr(native);
// // }
// //}
// public unsafe partial class MediaTrackInfo : IDisposable
// {
// [StructLayout(LayoutKind.Explicit, Size = 28)]
// public partial struct __Internal
// {
// [FieldOffset(0)]
// internal uint i_codec;
// [FieldOffset(4)]
// internal int i_id;
// [FieldOffset(8)]
// internal global::VideoLAN.LibVLCSharp.TrackType i_type;
// [FieldOffset(12)]
// internal int i_profile;
// [FieldOffset(16)]
// internal int i_level;
// [FieldOffset(20)]
// internal global::VideoLAN.LibVLCSharp.MediaTrackInfo._.__Internal u;
// [SuppressUnmanagedCodeSecurity]
// [DllImport("libvlc", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
// EntryPoint="??0libvlc_media_track_info_t@@QEAA@AEBU0@@Z")]
// internal static extern global::System.IntPtr cctor(global::System.IntPtr instance, global::System.IntPtr _0);
// }
// public unsafe partial struct _
// {
// [StructLayout(LayoutKind.Explicit, Size = 8)]
// public partial struct __Internal
// {
// [FieldOffset(0)]
// internal global::VideoLAN.LibVLCSharp.MediaTrackInfo._.__.__Internal audio;
// [FieldOffset(0)]
// internal global::VideoLAN.LibVLCSharp.MediaTrackInfo._.___.__Internal video;
// }
// public unsafe partial class __
// {
// [StructLayout(LayoutKind.Explicit, Size = 8)]
// public partial struct __Internal
// {
// [FieldOffset(0)]
// internal uint i_channels;
// [FieldOffset(4)]
// internal uint i_rate;
// }
// }
// public unsafe partial class ___
// {
// [StructLayout(LayoutKind.Explicit, Size = 8)]
// public partial struct __Internal
// {
// [FieldOffset(0)]
// internal uint i_height;
// [FieldOffset(4)]
// internal uint i_width;
// }
// }
// }
// public global::System.IntPtr __Instance { get; protected set; }
// protected int __PointerAdjustment;
// internal static readonly global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::VideoLAN.LibVLCSharp.MediaTrackInfo> NativeToManagedMap = new global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::VideoLAN.LibVLCSharp.MediaTrackInfo>();
// protected void*[] __OriginalVTables;
// protected bool __ownsNativeInstance;
// internal static global::VideoLAN.LibVLCSharp.MediaTrackInfo __CreateInstance(global::System.IntPtr native, bool skipVTables = false)
// {
// return new global::VideoLAN.LibVLCSharp.MediaTrackInfo(native.ToPointer(), skipVTables);
// }
// internal static global::VideoLAN.LibVLCSharp.MediaTrackInfo __CreateInstance(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal native, bool skipVTables = false)
// {
// return new global::VideoLAN.LibVLCSharp.MediaTrackInfo(native, skipVTables);
// }
// private static void* __CopyValue(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal native)
// {
// var ret = Marshal.AllocHGlobal(sizeof(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal));
// *(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) ret = native;
// return ret.ToPointer();
// }
// private MediaTrackInfo(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal native, bool skipVTables = false)
// : this(__CopyValue(native), skipVTables)
// {
// __ownsNativeInstance = true;
// NativeToManagedMap[__Instance] = this;
// }
// protected MediaTrackInfo(void* native, bool skipVTables = false)
// {
// if (native == null)
// return;
// __Instance = new global::System.IntPtr(native);
// }
// public MediaTrackInfo()
// {
// __Instance = Marshal.AllocHGlobal(sizeof(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal));
// __ownsNativeInstance = true;
// NativeToManagedMap[__Instance] = this;
// }
// public MediaTrackInfo(global::VideoLAN.LibVLCSharp.MediaTrackInfo _0)
// {
// __Instance = Marshal.AllocHGlobal(sizeof(global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal));
// __ownsNativeInstance = true;
// NativeToManagedMap[__Instance] = this;
// *((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance) = *((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) _0.__Instance);
// }
// public void Dispose()
// {
// Dispose(disposing: true);
// }
// public virtual void Dispose(bool disposing)
// {
// if (__Instance == IntPtr.Zero)
// return;
// global::VideoLAN.LibVLCSharp.MediaTrackInfo __dummy;
// NativeToManagedMap.TryRemove(__Instance, out __dummy);
// if (__ownsNativeInstance)
// Marshal.FreeHGlobal(__Instance);
// __Instance = IntPtr.Zero;
// }
// public uint ICodec
// {
// get
// {
// return ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_codec;
// }
// set
// {
// ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_codec = value;
// }
// }
// public int IId
// {
// get
// {
// return ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_id;
// }
// set
// {
// ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_id = value;
// }
// }
// public global::VideoLAN.LibVLCSharp.TrackType IType
// {
// get
// {
// return ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_type;
// }
// set
// {
// ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_type = value;
// }
// }
// public int IProfile
// {
// get
// {
// return ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_profile;
// }
// set
// {
// ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_profile = value;
// }
// }
// public int ILevel
// {
// get
// {
// return ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_level;
// }
// set
// {
// ((global::VideoLAN.LibVLCSharp.MediaTrackInfo.__Internal*) __Instance)->i_level = value;
// }
// }
// }
//
// public unsafe partial class libvlc_media
// {
// public partial struct __Internal
// {
// [SuppressUnmanagedCodeSecurity]
// [DllImport("libvlc", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
// EntryPoint="libvlc_media_get_codec_description")]
// internal static extern global::System.IntPtr LibvlcMediaGetCodecDescription(global::VideoLAN.LibVLCSharp.TrackType i_type, uint i_codec);
// }
// ///// <summary>
// ///// <para>Retain a reference to a media descriptor object (libvlc_media_t). Use</para>
// ///// <para>libvlc_media_release() to decrement the reference count of a</para>
// ///// <para>media descriptor object.</para>
// ///// </summary>
// ///// <param name="p_md">the media descriptor</param>
// //public static void LibvlcMediaRetain(global::VideoLAN.LibVLCSharp.Media p_md)
// //{
// // var __arg0 = ReferenceEquals(p_md, null) ? global::System.IntPtr.Zero : p_md.NativeReference;
// // __Internal.LibvlcMediaRetain(__arg0);
// //}