Commit ef72455b authored by Martin Finkel's avatar Martin Finkel

MediaList: Implement IEnumerable/IEnumerator

parent 061c916c
using System; using System;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using LibVLCSharp.Shared; using LibVLCSharp.Shared;
using NUnit.Framework; using NUnit.Framework;
...@@ -20,13 +22,18 @@ namespace LibVLCSharp.Tests ...@@ -20,13 +22,18 @@ namespace LibVLCSharp.Tests
} }
[Test] [Test]
public void DisposeMediaDiscoverer() public async Task DisposeMediaDiscoverer()
{ {
var mds = _libVLC.MediaDiscoverers(MediaDiscovererCategory.Lan); var mds = _libVLC.MediaDiscoverers(MediaDiscovererCategory.Lan);
var md = new MediaDiscoverer(_libVLC, mds.First().Name); var md = new MediaDiscoverer(_libVLC, mds.First().Name);
Assert.True(md.Start()); Assert.True(md.Start());
Assert.True(md.IsRunning); Assert.True(md.IsRunning);
Assert.NotNull(md.MediaList); Assert.NotNull(md.MediaList);
await Task.Delay(1000);
foreach(var media in md.MediaList)
{
Debug.WriteLine(media.Mrl);
}
md.Dispose(); md.Dispose();
Assert.IsNull(md.MediaList); Assert.IsNull(md.MediaList);
Assert.False(md.IsRunning); Assert.False(md.IsRunning);
......
...@@ -17,7 +17,6 @@ namespace LibVLCSharp.Tests ...@@ -17,7 +17,6 @@ namespace LibVLCSharp.Tests
var itemDeleted = false; var itemDeleted = false;
mediaList.ItemAdded += (sender, args) => itemAdded = true; mediaList.ItemAdded += (sender, args) => itemAdded = true;
mediaList.ItemDeleted += (sender, args) => itemDeleted = true; mediaList.ItemDeleted += (sender, args) => itemDeleted = true;
mediaList.Lock();
mediaList.AddMedia(media); mediaList.AddMedia(media);
Assert.AreEqual(media, mediaList[0]); Assert.AreEqual(media, mediaList[0]);
Assert.AreEqual(1, mediaList.Count); Assert.AreEqual(1, mediaList.Count);
...@@ -26,24 +25,6 @@ namespace LibVLCSharp.Tests ...@@ -26,24 +25,6 @@ namespace LibVLCSharp.Tests
mediaList.RemoveIndex(0); mediaList.RemoveIndex(0);
Assert.Zero(mediaList.Count); Assert.Zero(mediaList.Count);
Assert.True(itemDeleted); Assert.True(itemDeleted);
mediaList.Unlock();
}
[Test]
public void AcquireLockTwiceThrows()
{
var mediaList = new MediaList(_libVLC);
mediaList.Lock();
Assert.Throws<InvalidOperationException>(() => mediaList.Lock(), "already locked");
}
[Test]
public void ReleaseLockTwiceThrows()
{
var mediaList = new MediaList(_libVLC);
mediaList.Lock();
mediaList.Unlock();
Assert.Throws<InvalidOperationException>(() => mediaList.Unlock(), "not locked");
} }
[Test] [Test]
......
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace LibVLCSharp.Shared namespace LibVLCSharp.Shared
{ {
public class MediaList : Internal public class MediaList : Internal, IEnumerable
{ {
MediaListEventManager _eventManager; MediaListEventManager _eventManager;
readonly object _syncLock = new object(); readonly object _syncLock = new object();
...@@ -145,7 +147,11 @@ namespace LibVLCSharp.Shared ...@@ -145,7 +147,11 @@ namespace LibVLCSharp.Shared
} }
finally finally
{ {
Unlock(); lock (_syncLock)
{
if (_nativeLock)
Unlock();
}
} }
} }
...@@ -286,5 +292,40 @@ namespace LibVLCSharp.Shared ...@@ -286,5 +292,40 @@ namespace LibVLCSharp.Shared
base.Dispose(disposing); base.Dispose(disposing);
} }
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public MediaListEnumerator GetEnumerator() => new MediaListEnumerator(this);
public class MediaListEnumerator : IEnumerator<Media>
{
int position = -1;
MediaList _mediaList;
internal MediaListEnumerator(MediaList mediaList)
{
_mediaList = mediaList;
}
public bool MoveNext()
{
position++;
return position < _mediaList.Count;
}
void IEnumerator.Reset()
{
position = -1;
}
public void Dispose()
{
position = -1;
_mediaList = default;
}
object IEnumerator.Current => Current;
public Media Current => _mediaList[position];
}
} }
} }
\ 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