MediaTests.cs 5.16 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1
using System;
2
using System.Diagnostics;
Martin Finkel's avatar
Martin Finkel committed
3
using System.IO;
4
using System.Linq;
5
using System.Threading.Tasks;
Martin Finkel's avatar
Martin Finkel committed
6
using LibVLCSharp.Shared;
Martin Finkel's avatar
Martin Finkel committed
7 8
using NUnit.Framework;

9
namespace LibVLCSharp.Tests
Martin Finkel's avatar
Martin Finkel committed
10 11
{
    [TestFixture]
12
    public class MediaTests : BaseSetup
Martin Finkel's avatar
Martin Finkel committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    {
        [Test]
        public void CreateMedia()
        {
            var instance = new Instance();

            var media = new Media(instance, Path.GetTempFileName(), Media.FromType.FromPath);

            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
        }

        [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));
        }

        [Test]
        public void ReleaseMedia()
        {
            var media = new Media(new Instance(), Path.GetTempFileName(), Media.FromType.FromPath);

            media.Dispose();

            Assert.AreEqual(IntPtr.Zero, media.NativeReference);
        }

        [Test]
        public void CreateMediaFromStream()
        {
            var media = new Media(new Instance(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
        }

        [Test]
        public void AddOption()
        {
            var media = new Media(new Instance(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
            media.AddOption("-sout-all");
        }
54 55

        [Test]
56
        public async Task CreateRealMedia()
57
        {
58
            using (var instance = new Instance())
59
            {
60 61 62 63 64 65 66 67
                using (var media = new Media(instance, RealStreamMediaPath, Media.FromType.FromLocation))
                {
                    Assert.False(media.IsParsed);
                    media.Parse();

                    Assert.True(media.IsParsed);
                    Assert.NotZero(media.Duration);
                    using (var mp = new MediaPlayer(media))
68 69 70
        /// <summary>
        /// add media file for tests in \bin\x64\Debug\net47
        /// </summary>
71 72 73 74 75 76 77
                    {
                        Assert.True(mp.Play());
                        await Task.Delay(3000); // have to wait a bit for statistics to populate
                        Assert.Greater(media.Statistics.DemuxBitrate, 0);
                        mp.Stop();
                    }
                }
78 79
            }
        }
80
        
81 82 83 84 85 86 87 88 89 90 91
        [Test]
        public void Duplicate()
        {
            var media = new Media(new Instance(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
            var duplicate = media.Duplicate();
            Assert.AreNotEqual(duplicate.NativeReference, media.NativeReference);
        }

        [Test]
        public void CreateMediaFromFileStream()
        {
92
            Assert.Fail();
93
            // TODO: fix this.
94
            var media = new Media(new Instance(), new FileStream("realFileUri", FileMode.Open, FileAccess.Read, FileShare.Read));
95 96
            media.Parse();
            Assert.NotZero(media.Tracks.First().Data.Audio.Channels);
97 98 99 100 101 102 103 104 105 106 107
        }

        [Test]
        public void SetMetadata()
        {
            var media = new Media(new Instance(), Path.GetTempFileName(), Media.FromType.FromPath);
            const string test = "test";
            media.SetMeta(Media.MetadataType.ShowName, test);
            Assert.True(media.SaveMeta());
            Assert.AreEqual(test, media.Meta(Media.MetadataType.ShowName));
        }
Martin Finkel's avatar
Martin Finkel committed
108 109

        [Test]
110
        public async Task AsyncParse()
Martin Finkel's avatar
Martin Finkel committed
111
        {
112 113 114 115 116 117 118 119 120 121 122 123
          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;
Martin Finkel's avatar
Martin Finkel committed
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            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);
Martin Finkel's avatar
Martin Finkel committed
151
        }
Martin Finkel's avatar
Martin Finkel committed
152 153
    }
}