MediaTests.cs 3.47 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1 2
using System;
using System.IO;
3
using System.Linq;
Martin Finkel's avatar
Martin Finkel committed
4
using System.Threading;
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
    {
        [Test]
        public void CreateMedia()
        {
            var instance = new Instance();

Martin Finkel's avatar
Martin Finkel committed
19
            var media = new Media(instance, Path.GetTempFileName());
Martin Finkel's avatar
Martin Finkel committed
20 21 22 23 24 25 26

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

        [Test]
        public void CreateMediaFail()
        {
Martin Finkel's avatar
Martin Finkel committed
27 28
            Assert.Throws<ArgumentNullException>(() => new Media(null, Path.GetTempFileName()));
            Assert.Throws<ArgumentNullException>(() => new Media(new Instance(), string.Empty));
Martin Finkel's avatar
Martin Finkel committed
29 30 31 32 33
        }

        [Test]
        public void ReleaseMedia()
        {
Martin Finkel's avatar
Martin Finkel committed
34
            var media = new Media(new Instance(), Path.GetTempFileName());
Martin Finkel's avatar
Martin Finkel committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

            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 68 69
                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))
                    {
                        Assert.True(mp.Play());
Martin Finkel's avatar
cleanup  
Martin Finkel committed
70
                        await Task.Delay(4000); // have to wait a bit for statistics to populate
71 72 73 74
                        Assert.Greater(media.Statistics.DemuxBitrate, 0);
                        mp.Stop();
                    }
                }
75 76
            }
        }
77
        
78 79 80 81 82 83 84 85 86 87 88
        [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()
        {
Martin Finkel's avatar
Martin Finkel committed
89
            var media = new Media(new Instance(), new FileStream(RealMp3Path, FileMode.Open, FileAccess.Read, FileShare.Read));
90 91
            media.Parse();
            Assert.NotZero(media.Tracks.First().Data.Audio.Channels);
92 93 94 95 96
        }

        [Test]
        public void SetMetadata()
        {
Martin Finkel's avatar
Martin Finkel committed
97
            var media = new Media(new Instance(), Path.GetTempFileName());
98 99 100 101 102
            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
103 104

        [Test]
Martin Finkel's avatar
Martin Finkel committed
105
        public void GetTracks()
Martin Finkel's avatar
Martin Finkel committed
106
        {
Martin Finkel's avatar
Martin Finkel committed
107 108 109
            var media = new Media(new Instance(), RealMp3Path);
            media.Parse();
            Assert.AreEqual(1, media.Tracks);
Martin Finkel's avatar
Martin Finkel committed
110
        }
Martin Finkel's avatar
Martin Finkel committed
111 112
    }
}