MediaTests.cs 5.48 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 media = new Media(_libVLC, Path.GetTempFileName());
Martin Finkel's avatar
Martin Finkel committed
18 19 20 21 22 23 24

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

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

        [Test]
        public void ReleaseMedia()
        {
Martin Finkel's avatar
Martin Finkel committed
32
            var media = new Media(_libVLC, Path.GetTempFileName());
Martin Finkel's avatar
Martin Finkel committed
33 34 35 36 37 38 39 40 41

            media.Dispose();

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

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

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

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

69 70 71
        [Test]
        public void Duplicate()
        {
Martin Finkel's avatar
Martin Finkel committed
72
            var media = new Media(_libVLC, new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
73 74 75 76 77 78 79
            var duplicate = media.Duplicate();
            Assert.AreNotEqual(duplicate.NativeReference, media.NativeReference);
        }

        [Test]
        public void CreateMediaFromFileStream()
        {
Martin Finkel's avatar
Martin Finkel committed
80
            var media = new Media(_libVLC, new FileStream(RealMp3Path, FileMode.Open, FileAccess.Read, FileShare.Read));
81
            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
82 83 84 85 86
        }

        [Test]
        public void SetMetadata()
        {
Martin Finkel's avatar
Martin Finkel committed
87
            var media = new Media(_libVLC, Path.GetTempFileName());
88 89 90 91 92
            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
93 94

        [Test]
Martin Finkel's avatar
Martin Finkel committed
95
        public void GetTracks()
Martin Finkel's avatar
Martin Finkel committed
96
        {
Martin Finkel's avatar
Martin Finkel committed
97
            var media = new Media(_libVLC, RealMp3Path);
Martin Finkel's avatar
Martin Finkel committed
98
            media.Parse();
99 100
            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
101
        }
Martin Finkel's avatar
Martin Finkel committed
102 103 104 105

        [Test]
        public async Task CreateRealMediaSpecialCharacters()
        {
Martin Finkel's avatar
Martin Finkel committed
106 107
            _libVLC.Log += LibVLC_Log;
            using (var media = new Media(_libVLC, RealMp3PathSpecialCharacter, Media.FromType.FromPath))
Martin Finkel's avatar
Martin Finkel committed
108
            {
Martin Finkel's avatar
Martin Finkel committed
109 110 111 112 113 114 115
                Assert.False(media.IsParsed);

                media.Parse();
                await Task.Delay(5000);
                Assert.True(media.IsParsed);
                Assert.AreEqual(Media.MediaParsedStatus.Done, media.ParsedStatus);
                using (var mp = new MediaPlayer(media))
Martin Finkel's avatar
Martin Finkel committed
116
                {
Martin Finkel's avatar
Martin Finkel committed
117 118 119
                    Assert.True(mp.Play());
                    await Task.Delay(10000);
                    mp.Stop();
Martin Finkel's avatar
Martin Finkel committed
120 121
                }
            }
Martin Finkel's avatar
Martin Finkel committed
122
            _libVLC.Log -= LibVLC_Log;
Martin Finkel's avatar
Martin Finkel committed
123 124
        }

125 126 127
        [Test]
        public async Task CreateMediaFromStreamMultiplePlay()
        {
Martin Finkel's avatar
Martin Finkel committed
128
            using(var mp = new MediaPlayer(_libVLC))
129
            {
Martin Finkel's avatar
Martin Finkel committed
130
                var media = new Media(_libVLC, await GetStreamFromUrl("http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"));
131 132 133 134 135 136 137 138 139 140 141
                mp.Play(media);

                await Task.Delay(1000);

                mp.Time = 60000;

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

                mp.Play(media);
            }
        }
142 143 144 145

        [Test]
        public async Task CreateMultipleMediaFromStreamPlay()
        {
Martin Finkel's avatar
Martin Finkel committed
146 147
            var libVLC1 = new LibVLC("--no-audio", "--no-video");
            var libVLC2 = new LibVLC("--no-audio", "--no-video");
148 149 150 151 152 153 154 155 156 157

            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
158
        private async Task<Stream> GetStreamFromUrl(string url)
159 160 161
        {
            byte[] imageData = null;

Martin Finkel's avatar
Martin Finkel committed
162 163
            using (var client = new System.Net.Http.HttpClient())
                imageData = await client.GetByteArrayAsync(url);
164 165 166 167

            return new MemoryStream(imageData);
        }

Martin Finkel's avatar
Martin Finkel committed
168 169 170 171
        private void LibVLC_Log(object sender, LogEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
Martin Finkel's avatar
Martin Finkel committed
172
    }
Martin Finkel's avatar
Martin Finkel committed
173
}