MediaTests.cs 6.98 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1 2
using System;
using System.IO;
3
using System.Linq;
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()
        {
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()
        {
25
            Assert.Throws<ArgumentNullException>(() => new Media(null, Path.GetTempFileName()));
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()
        {
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()
        {
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()
        {
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
        {
56
            using (var media = new Media(_libVLC, RealStreamMediaPath, FromType.FromLocation))
57
            {
58 59
                Assert.NotZero(media.Duration);
                using (var mp = new MediaPlayer(media))
60
                {
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()
        {
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()
        {
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()
        {
87
            var media = new Media(_libVLC, Path.GetTempFileName());
88
            const string test = "test";
89
            media.SetMeta(MetadataType.ShowName, test);
90
            Assert.True(media.SaveMeta());
91
            Assert.AreEqual(test, media.Meta(MetadataType.ShowName));
92
        }
Martin Finkel's avatar
Martin Finkel committed
93 94

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

        [Test]
        public async Task CreateRealMediaSpecialCharacters()
        {
106
            _libVLC.Log += LibVLC_Log;
107
            using (var media = new Media(_libVLC, RealMp3PathSpecialCharacter, FromType.FromPath))
108
            {
109 110
                Assert.False(media.IsParsed);

111
                await media.Parse();
112 113
                await Task.Delay(5000);
                Assert.True(media.IsParsed);
114
                Assert.AreEqual(MediaParsedStatus.Done, media.ParsedStatus);
115
                using (var mp = new MediaPlayer(media))
116
                {
117 118 119
                    Assert.True(mp.Play());
                    await Task.Delay(10000);
                    mp.Stop();
120 121
                }
            }
122
            _libVLC.Log -= LibVLC_Log;
123 124
        }

125 126 127
        [Test]
        public async Task CreateMediaFromStreamMultiplePlay()
        {
128
            using(var mp = new MediaPlayer(_libVLC))
129
            {
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()
        {
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);
        }

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        [Test]
        public void ParseShouldThrowIfCancelledOperation()
        {
            var media = new Media(_libVLC, RealMp3Path);
            var cancellationToken = new CancellationToken(canceled: true);
            Assert.ThrowsAsync<TaskCanceledException>(async () => await media.Parse(cancellationToken: cancellationToken));
        }

        [Test]
        public async Task ParseShouldTimeoutWith1MillisecondLimit()
        {
            var media = new Media(_libVLC, RealMp3Path);
            var parseResult = await media.Parse(timeout: 1);
            Assert.AreEqual(MediaParsedStatus.Timeout, parseResult);
        }

        [Test]
        public async Task ParseShouldSucceed()
        {
            var media = new Media(_libVLC, RealMp3Path);
            var parseResult = await media.Parse();
            Assert.AreEqual(MediaParsedStatus.Done, parseResult);
        }

        [Test]
        public async Task ParseShouldFailIfNotMediaFile()
        {
            var media = new Media(_libVLC, Path.GetTempFileName());
            var parseResult = await media.Parse();
            Assert.AreEqual(MediaParsedStatus.Failed, parseResult);
        }

190 191 192 193 194 195 196 197
        [Test]
        public async Task ParseShouldBeSkippedIfLocalParseSpecifiedAndRemoteUrlProvided()
        {
            var media = new Media(_libVLC, RealStreamMediaPath, FromType.FromLocation);
            var parseResult = await media.Parse(MediaParseOptions.ParseLocal);
            Assert.AreEqual(MediaParsedStatus.Skipped, parseResult);
        }

Martin Finkel's avatar
Martin Finkel committed
198
        private async Task<Stream> GetStreamFromUrl(string url)
199 200 201
        {
            byte[] imageData = null;

Martin Finkel's avatar
Martin Finkel committed
202 203
            using (var client = new System.Net.Http.HttpClient())
                imageData = await client.GetByteArrayAsync(url);
204 205 206 207

            return new MemoryStream(imageData);
        }

208 209 210 211
        private void LibVLC_Log(object sender, LogEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
Martin Finkel's avatar
Martin Finkel committed
212
    }
213
}