MediaTests.cs 5.85 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
    {
        [Test]
        public void CreateMedia()
        {
Martin Finkel's avatar
Martin Finkel committed
17
            var libVLC = new LibVLC();
Martin Finkel's avatar
Martin Finkel committed
18

Martin Finkel's avatar
Martin Finkel committed
19
            var media = new Media(libVLC, 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
            Assert.Throws<ArgumentNullException>(() => new Media(null, Path.GetTempFileName()));
Martin Finkel's avatar
Martin Finkel committed
28
            Assert.Throws<ArgumentNullException>(() => new Media(new LibVLC(), 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 LibVLC(), Path.GetTempFileName());
Martin Finkel's avatar
Martin Finkel committed
35 36 37 38 39 40 41 42 43

            media.Dispose();

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

        [Test]
        public void CreateMediaFromStream()
        {
Martin Finkel's avatar
Martin Finkel committed
44
            var media = new Media(new LibVLC(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
Martin Finkel's avatar
Martin Finkel committed
45 46 47 48 49 50
            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
        }

        [Test]
        public void AddOption()
        {
Martin Finkel's avatar
Martin Finkel committed
51
            var media = new Media(new LibVLC(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
Martin Finkel's avatar
Martin Finkel committed
52 53
            media.AddOption("-sout-all");
        }
54 55

        [Test]
56
        public async Task CreateRealMedia()
57
        {
Martin Finkel's avatar
Martin Finkel committed
58
            using (var libVLC = new LibVLC())
59
            {
Martin Finkel's avatar
Martin Finkel committed
60
                using (var media = new Media(libVLC, RealStreamMediaPath, Media.FromType.FromLocation))
61 62 63 64 65
                {
                    Assert.NotZero(media.Duration);
                    using (var mp = new MediaPlayer(media))
                    {
                        Assert.True(mp.Play());
Martin Finkel's avatar
cleanup  
Martin Finkel committed
66
                        await Task.Delay(4000); // have to wait a bit for statistics to populate
67 68 69 70
                        Assert.Greater(media.Statistics.DemuxBitrate, 0);
                        mp.Stop();
                    }
                }
71 72
            }
        }
73

74 75 76
        [Test]
        public void Duplicate()
        {
Martin Finkel's avatar
Martin Finkel committed
77
            var media = new Media(new LibVLC(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
78 79 80 81 82 83 84
            var duplicate = media.Duplicate();
            Assert.AreNotEqual(duplicate.NativeReference, media.NativeReference);
        }

        [Test]
        public void CreateMediaFromFileStream()
        {
Martin Finkel's avatar
Martin Finkel committed
85
            var media = new Media(new LibVLC(), new FileStream(RealMp3Path, FileMode.Open, FileAccess.Read, FileShare.Read));
86
            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
87 88 89 90 91
        }

        [Test]
        public void SetMetadata()
        {
Martin Finkel's avatar
Martin Finkel committed
92
            var media = new Media(new LibVLC(), Path.GetTempFileName());
93 94 95 96 97
            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
98 99

        [Test]
Martin Finkel's avatar
Martin Finkel committed
100
        public void GetTracks()
Martin Finkel's avatar
Martin Finkel committed
101
        {
Martin Finkel's avatar
Martin Finkel committed
102
            var media = new Media(new LibVLC(), RealMp3Path);
Martin Finkel's avatar
Martin Finkel committed
103
            media.Parse();
104 105
            Assert.AreEqual(media.Tracks.Single().Data.Audio.Channels, 2);
            Assert.AreEqual(media.Tracks.Single().Data.Audio.Rate, 44100);
Martin Finkel's avatar
Martin Finkel committed
106
        }
Martin Finkel's avatar
Martin Finkel committed
107 108 109 110 111 112 113 114 115 116

        [Test]
        public async Task CreateRealMediaSpecialCharacters()
        {
            using (var libVLC = new LibVLC())
            {
                libVLC.Log += LibVLC_Log;
                using (var media = new Media(libVLC, RealMp3PathSpecialCharacter, Media.FromType.FromPath))
                {
                    Assert.False(media.IsParsed);
117

Martin Finkel's avatar
Martin Finkel committed
118 119 120 121 122 123 124
                    media.Parse();
                    await Task.Delay(5000);
                    Assert.True(media.IsParsed);
                    Assert.AreEqual(Media.MediaParsedStatus.Done, media.ParsedStatus);
                    using (var mp = new MediaPlayer(media))
                    {
                        Assert.True(mp.Play());
125
                        await Task.Delay(10000);
Martin Finkel's avatar
Martin Finkel committed
126 127 128 129 130 131 132
                        mp.Stop();
                    }
                }
                libVLC.Log -= LibVLC_Log;
            }
        }

133 134 135 136 137 138
        [Test]
        public async Task CreateMediaFromStreamMultiplePlay()
        {
            using (var libVLC = new LibVLC())
            using(var mp = new MediaPlayer(libVLC))
            {
Martin Finkel's avatar
Martin Finkel committed
139
                var media = new Media(libVLC, await GetStreamFromUrl("http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"));
140 141 142 143 144 145 146 147 148 149 150
                mp.Play(media);

                await Task.Delay(1000);

                mp.Time = 60000;

                await Task.Delay(10000); // end reached, rewind stream

                mp.Play(media);
            }
        }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

        [Test]
        public async Task CreateMultipleMediaFromStreamPlay()
        {
            var libVLC1 = new LibVLC(new[] { "--no-audio" });
            var libVLC2 = new LibVLC(new[] { "--no-sout-display" });

            var mp1 = new MediaPlayer(libVLC1);
            var mp2 = new MediaPlayer(libVLC2);

            mp1.Play(new Media(libVLC1, await GetStreamFromUrl("http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4")));
            mp2.Play(new Media(libVLC2, await GetStreamFromUrl("https://streams.videolan.org/streams/mp3/05-Mr.%20Zebra.mp3")));

            await Task.Delay(10000);
        }

Martin Finkel's avatar
Martin Finkel committed
167
        private async Task<Stream> GetStreamFromUrl(string url)
168 169 170
        {
            byte[] imageData = null;

Martin Finkel's avatar
Martin Finkel committed
171 172
            using (var client = new System.Net.Http.HttpClient())
                imageData = await client.GetByteArrayAsync(url);
173 174 175 176

            return new MemoryStream(imageData);
        }

Martin Finkel's avatar
Martin Finkel committed
177 178 179 180
        private void LibVLC_Log(object sender, LogEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
Martin Finkel's avatar
Martin Finkel committed
181
    }
Martin Finkel's avatar
Martin Finkel committed
182
}