MediaPlayerTests.cs 4.84 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1
using System;
Martin Finkel's avatar
Martin Finkel committed
2 3 4
using System.Diagnostics;
using System.IO;
using System.Linq;
Martin Finkel's avatar
Martin Finkel committed
5
using System.Runtime.ExceptionServices;
Martin Finkel's avatar
Martin Finkel committed
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()
        {
Martin Finkel's avatar
Martin Finkel committed
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]
Martin Finkel's avatar
Martin Finkel committed
24
        public void OutputDeviceEnum()
Martin Finkel's avatar
Martin Finkel committed
25
        {
Martin Finkel's avatar
Martin Finkel committed
26
            var mp = new MediaPlayer(_libVLC);
Martin Finkel's avatar
Martin Finkel committed
27 28
            var t = mp.OutputDeviceEnum;
            Debug.WriteLine(t);
Martin Finkel's avatar
Martin Finkel committed
29
        }
Martin Finkel's avatar
Martin Finkel committed
30
        
Martin Finkel's avatar
Martin Finkel committed
31
        [Test]
Martin Finkel's avatar
Martin Finkel committed
32
        public async Task TrackDescription()
Martin Finkel's avatar
Martin Finkel committed
33
        {
Martin Finkel's avatar
Martin Finkel committed
34
            var mp = new MediaPlayer(_libVLC);
35
            var media = new Media(_libVLC, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", FromType.FromLocation);
Martin Finkel's avatar
Martin Finkel committed
36 37
            var tcs = new TaskCompletionSource<bool>();
            
Martin Finkel's avatar
Martin Finkel committed
38
            mp.Media = media;
Martin Finkel's avatar
Martin Finkel committed
39
            mp.Play();
Martin Finkel's avatar
Martin Finkel committed
40
            mp.Playing += (sender, args) =>
Martin Finkel's avatar
Martin Finkel committed
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);
Martin Finkel's avatar
Martin Finkel committed
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);
Martin Finkel's avatar
Martin Finkel committed
55 56
            var mp = new MediaPlayer(media);
            var called = false;
Martin Finkel's avatar
Martin Finkel committed
57
            mp.Playing += (sender, args) =>
Martin Finkel's avatar
Martin Finkel committed
58 59 60 61 62 63
            {
                called = true;
            };
            mp.Play();
            await Task.Delay(5000);
            Assert.True(called);
Martin Finkel's avatar
Martin Finkel committed
64
            //Assert.True(mp.IsPlaying);
Martin Finkel's avatar
Martin Finkel committed
65
        }
Martin Finkel's avatar
Martin Finkel committed
66 67 68 69 70 71 72 73

        int callCountRegisterOne = 0;
        int callCountRegisterTwo = 0;

        [Test]
        public async Task EventFireOnceForeachRegistration()
        {
            try
Martin Finkel's avatar
Martin Finkel committed
74
            {
75
                var media = new Media(_libVLC, "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4", FromType.FromLocation);
Martin Finkel's avatar
Martin Finkel committed
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()
        {
Martin Finkel's avatar
Martin Finkel committed
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()
        {
Martin Finkel's avatar
Martin Finkel committed
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);
        }
Martin Finkel's avatar
Martin Finkel committed
178 179
    }
}