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

10
namespace LibVLCSharp.Tests
Martin Finkel's avatar
Martin Finkel committed
11 12
{
    [TestFixture]
13
    public class MediaPlayerTests : BaseSetup
Martin Finkel's avatar
Martin Finkel committed
14 15 16 17
    {
        [Test]
        public void CreateAndDestroy()
        {
18
            var mp = new MediaPlayer(_libVLC);
Martin Finkel's avatar
Martin Finkel committed
19 20 21 22 23
            mp.Dispose();
            Assert.AreEqual(IntPtr.Zero, mp.NativeReference);
        }

        [Test]
24
        public void OutputDeviceEnum()
Martin Finkel's avatar
Martin Finkel committed
25
        {
26
            var mp = new MediaPlayer(_libVLC);
Martin Finkel's avatar
Martin Finkel committed
27
            var t = mp.AudioOutputDeviceEnum;
28
            Debug.WriteLine(t);
Martin Finkel's avatar
Martin Finkel committed
29
        }
Martin Finkel's avatar
Martin Finkel committed
30
        
31
        [Test]
32
        public async Task TrackDescription()
33
        {
34
            var mp = new MediaPlayer(_libVLC);
35
            var media = new Media(_libVLC, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", FromType.FromLocation);
36 37
            var tcs = new TaskCompletionSource<bool>();
            
38
            mp.Media = media;
39
            mp.Play();
Martin Finkel's avatar
Martin Finkel committed
40
            mp.Playing += (sender, args) =>
41 42 43 44 45 46 47 48
            {
                var description = mp.AudioTrackDescription;
                Assert.True(mp.SetAudioTrack(description.First().Id));
                Assert.IsNotEmpty(description);
                tcs.SetResult(true);
            };
            await tcs.Task;
            Assert.True(tcs.Task.Result);
49 50 51 52 53
        }

        [Test]
        public async Task Play()
        {
54
            var media = new Media(_libVLC, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", FromType.FromLocation);
55 56
            var mp = new MediaPlayer(media);
            var called = false;
Martin Finkel's avatar
Martin Finkel committed
57
            mp.Playing += (sender, args) =>
58 59 60 61 62 63
            {
                called = true;
            };
            mp.Play();
            await Task.Delay(5000);
            Assert.True(called);
64
            //Assert.True(mp.IsPlaying);
65
        }
66 67 68 69 70 71 72 73

        int callCountRegisterOne = 0;
        int callCountRegisterTwo = 0;

        [Test]
        public async Task EventFireOnceForeachRegistration()
        {
            try
74
            {
75
                var media = new Media(_libVLC, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", FromType.FromLocation);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                var mp = new MediaPlayer(media);
                

                mp.Playing += Mp_Playing;

                mp.Playing += Mp_Playing1;

                Debug.WriteLine("first play");

                mp.Play();
                await Task.Delay(2000);
                Assert.AreEqual(callCountRegisterOne, 1);
                Assert.AreEqual(callCountRegisterTwo, 1);
            
                callCountRegisterOne = 0;
                callCountRegisterTwo = 0;

                mp.Stop();

                mp.Playing -= Mp_Playing;

            
                Debug.WriteLine("second play");

                mp.Play();
                await Task.Delay(2000);

                Assert.AreEqual(callCountRegisterOne, 0);
                Assert.AreEqual(callCountRegisterTwo, 1);

              //  mp.Stop();

                mp.Playing -= Mp_Playing1; // native crash in detach?



                callCountRegisterOne = 0;
                callCountRegisterTwo = 0;


                Debug.WriteLine("third play");

                mp.Play();
                await Task.Delay(500);

                Assert.AreEqual(callCountRegisterOne, 0);
                Assert.AreEqual(callCountRegisterTwo, 0);

                
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        void Mp_Playing1(object sender, EventArgs e)
        {
            callCountRegisterTwo++;
            Debug.WriteLine($"Mp_Playing1 called with {callCountRegisterTwo}");

        }

        void Mp_Playing(object sender, EventArgs e)
        {
            callCountRegisterOne++;
            Debug.WriteLine($"Mp_Playing called with {callCountRegisterOne}");
        }
144 145 146 147

        [Test]
        public async Task DisposeMediaPlayer()
        {
148
            var mp = new MediaPlayer(_libVLC);
149

150
            mp.Play(new Media(_libVLC, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", FromType.FromLocation));
151 152 153 154 155 156 157

            await Task.Delay(1000);

            mp.Dispose();

            Assert.AreEqual(IntPtr.Zero, mp.NativeReference);
        }
158 159 160 161

        [Test]
        public async Task UpdateViewpoint()
        {
162
            var mp = new MediaPlayer(_libVLC);
163

164
            mp.Play(new Media(_libVLC, "https://streams.videolan.org/streams/360/eagle_360.mp4", FromType.FromLocation));
165 166 167 168 169 170 171 172 173 174 175 176 177

            await Task.Delay(1000);

            var result = mp.UpdateViewpoint(yaw: 0, pitch: 90, roll: 0, fov: 0);

            Assert.IsTrue(result);

            await Task.Delay(1000);
            
            mp.Dispose();

            Assert.AreEqual(IntPtr.Zero, mp.NativeReference);
        }
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

        [Test]
        public void GetMediaPlayerRole()
        {
            var mp = new MediaPlayer(_libVLC);
            Assert.AreEqual(MediaPlayerRole.None, mp.Role);
        }

        [Test]
        public void SetMediaPlayerRole()
        {
            var mp = new MediaPlayer(_libVLC);
            Assert.AreEqual(MediaPlayerRole.None, mp.Role);

            Assert.True(mp.SetRole(MediaPlayerRole.Video));
            Assert.AreEqual(MediaPlayerRole.Video, mp.Role);
        }
Martin Finkel's avatar
Martin Finkel committed
195 196
    }
}