Example #1
0
        private void Record(string source, string directory, string pattern)
        {
            var filename = Path.Combine(directory, string.Format(pattern, DateTime.Now.ToString("yyyyMMddHHmmss")));

            var opts   = $":sout=#std{{access=file,dst='{filename}'}}";
            var player = new VlcMediaPlayer(new DirectoryInfo(ConfigurationManager.AppSettings["VlcDirectory"]), new[] { "--rtsp-tcp" });

            player.SetMedia(source, opts);
            player.EncounteredError += (sender, eventArgs) =>
            {
                player.Dispose();
                Upload(filename);
            };
            player.Play();
            var recordTimer = new Timer((duration + 2) * 1000);

            recordTimer.AutoReset = false;
            recordTimer.Elapsed  += (sender, eventArgs) =>
            {
                player.Stop();
                player.Dispose();
                recordTimer.Dispose();
                Console.WriteLine($"recorded {filename}");
                Upload(filename);
            };
            Console.WriteLine($"start recording to {filename}");
            recordTimer.Start();
        }
Example #2
0
        /// <summary>
        /// Extract a still frame from a video
        /// </summary>
        /// <param name="startAt"></param>
        /// <returns></returns>
        public Bitmap GetFrame(double startAt)
        {
            FileInfo file  = new FileInfo(this.FileName);
            FileInfo file2 = new FileInfo(Path.GetTempPath() + "\\snapshot.png");

            if (File.Exists(file2.FullName))
            {
                File.Delete(file2.FullName);
            }
            VlcMediaPlayer mediaPlayer = new VlcMediaPlayer(VideoTools.GetLibVlcLocation(), VideoTools.GetVlcOptionsHeadless(startAt));

            try
            {
                bool done         = false;
                bool takesnapshot = true;
                mediaPlayer.PositionChanged += (sender, e) =>
                {
                    if (takesnapshot)
                    {
                        takesnapshot = false; // avoid re-entry
                        mediaPlayer.TakeSnapshot(file2);
                        done = true;
                    }
                };
                mediaPlayer.SetMedia(file);
                mediaPlayer.Play();
                DateTime start = DateTime.UtcNow;
                while (!done) // wait for video to start, timeout 3 seconds
                {
                    if ((DateTime.UtcNow - start).TotalMilliseconds >= 3000)
                    {
                        throw (new Exception("Error when starting video!"));
                    }
                }
                mediaPlayer.Dispose();
            }
            catch (Exception exc)
            {
                mediaPlayer.Dispose();
                throw (new Exception("VLC library error:" + exc.Message));
            }

            if (File.Exists(file2.FullName))
            {
                Bitmap pic = new Bitmap(file2.FullName);
                return(pic);
            }
            else
            {
                throw (new Exception("Error while extracting snapshot from video!"));
            }
        }
Example #3
0
        public async Task <VideoAudioInfo> Execute(Stream thumbnailStream)
        {
            ThumbnailStream = thumbnailStream;
            LibVlcManager.LoadLibVlc(AppDomain.CurrentDomain.BaseDirectory + @"..\..\libvlc");
            Vlc vlc = new Vlc(new[] { "-I", "dummy", "--ignore-config", "--no-video-title" });

            Console.WriteLine($"LibVlcManager: {LibVlcManager.GetVersion()}s");
            mediaPlayer = vlc.CreateMediaPlayer();
            mediaPlayer.SetVideoDecodeCallback(LockCB, unLockCB, DisplayCB, IntPtr.Zero);
            mediaPlayer.SetVideoFormatCallback(VideoFormatCallback, VideoCleanupCallback);
            mediaPlayer.Playing += MediaPlayer_Playing;
            mediaPlayer.Media    = vlc.CreateMediaFromLocation(FilePath);

            await Task.Run(() =>
            {
                mediaPlayer.Play();
                mediaPlayer.IsMute = true;
                Stopwatch s        = new Stopwatch();
                s.Start();
                while (waiting)
                {
                    Thread.Sleep(100);
                }
                s.Stop();
                TimeSpan sec = new TimeSpan(s.ElapsedTicks);
                Console.WriteLine(sec.ToString());
                mediaPlayer.Dispose();
                vlc.Dispose();
            });

            _VideoAudioInfo.Resolution = JsonConvert.SerializeObject(_VideoResolution);
            //_VideoAudioInfo.Thumbnail = ThumbnailStream;
            return(_VideoAudioInfo);
        }
Example #4
0
 public void Dispose()
 {
     if (_mediaPlayer != null)
     {
         _mediaPlayer.Dispose();
     }
 }
Example #5
0
        /// <summary>
        /// Class describing a video file, manipulated using VLC libraries
        /// </summary>
        /// <param name="fileName"></param>
        public Video(string fileName)
        {
            FileInfo file        = new FileInfo(fileName);
            bool     initialized = false;

            if (!VideoTools.checkVlcLib())
            {
                throw new Exception("Cannot find the VLC library... Please make sure you're running this application from inside your PinUp folder...");
            }
            try
            {
                VlcMediaPlayer mediaPlayer = new VlcMediaPlayer(VideoTools.GetLibVlcLocation(), VideoTools.GetVlcOptionsHeadless(0));
                initialized = true;
                mediaPlayer.SetMedia(file);
                mediaPlayer.GetMedia().Parse();
                this.Duration = mediaPlayer.GetMedia().Duration.TotalSeconds;
                this.Width    = (int)mediaPlayer.GetMedia().TracksInformations[0].Video.Width;
                this.Height   = (int)mediaPlayer.GetMedia().TracksInformations[0].Video.Height;
                this.FileName = fileName;
                mediaPlayer.Dispose();
            }
            catch (Exception exc)
            {
                if (!initialized)
                {
                    throw (new Exception("Cannot initialize VLC player library: " + exc.Message));
                }
                else
                {
                    throw (new Exception("VLC library error: " + exc.Message));
                }
            }
        }
Example #6
0
        /// <summary>
        ///     Cleanup the player used resource.
        /// </summary>
        /// <param name="disposing"></param>
        protected void Dispose(bool disposing)
        {
            if (_disposed || _disposing)
            {
                return;
            }

            _disposing = true;

            Stop();

            if (VlcMediaPlayer != null)
            {
                VlcMediaPlayer.Media?.Dispose();
                VlcMediaPlayer.Dispose();
                VlcMediaPlayer = null;
            }

            _lockCallbackHandle.Free();
            _unlockCallbackHandle.Free();
            _displayCallbackHandle.Free();
            _formatCallbackHandle.Free();
            _cleanupCallbackHandle.Free();
            //_audioSetupCallbackHandle.Free();
            //_audioCleanupCallbackHandle.Free();
            //_audioPlayCallbackHandle.Free();
            //_audioPauseCallbackHandle.Free();
            //_audioResumeCallbackHandle.Free();
            //_audioFlushCallbackHandle.Free();
            //_audioDrainCallbackHandle.Free();
            //_audioSetVolumeCallbackHandle.Free();

            _disposed  = true;
            _disposing = false;
        }
Example #7
0
        /// <summary>
        ///     Cleanup the player used resource.
        /// </summary>
        /// <param name="disposing"></param>
        protected void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            BeginStop(() =>
            {
                if (VlcMediaPlayer != null)
                {
                    if (VlcMediaPlayer.Media != null)
                    {
                        VlcMediaPlayer.Media.Dispose();
                    }
                    VlcMediaPlayer.Dispose();
                }

                _lockCallbackHandle.Free();
                _unlockCallbackHandle.Free();
                _displayCallbackHandle.Free();
                _formatCallbackHandle.Free();
                _cleanupCallbackHandle.Free();
                //_audioSetupCallbackHandle.Free();
                //_audioCleanupCallbackHandle.Free();
                //_audioPlayCallbackHandle.Free();
                //_audioPauseCallbackHandle.Free();
                //_audioResumeCallbackHandle.Free();
                //_audioFlushCallbackHandle.Free();
                //_audioDrainCallbackHandle.Free();
                //_audioSetVolumeCallbackHandle.Free();

                _disposed = true;
            });
        }
Example #8
0
 public void Dispose()
 {
     if (Player != null)
     {
         Stop();
         Player.Dispose();
         Player = null;
     }
 }
Example #9
0
 protected override void Dispose(bool disposing)
 {
     if (_vlcMediaPlayer != null)
     {
         UnregisterEvents();
         _vlcMediaPlayer.Dispose();
         base.Dispose(disposing);
         GC.SuppressFinalize(this);
     }
 }
Example #10
0
        public void AspectRatioTest()
        {
            VlcInstance    instance = new VlcInstance(VlcInstanceArgs);
            VlcMediaPlayer player   = instance.CreatePlayer(BaseTestFilePath + "n900_extremely_short.avi");

            player.Play();
            Assert.AreEqual("1.66", player.Media.AspectRatio);
            player.Stop();
            player.Dispose();
            instance.Dispose();
        }
Example #11
0
        public void SubtitleTracksTest()
        {
            VlcInstance    instance = null;
            VlcMediaPlayer player   = null;

            instance = new VlcInstance(VlcInstanceArgs);
            player   = instance.CreatePlayer(BaseTestFilePath + "mewmew-vorbis-ssa.mkv");
            Assert.IsInstanceOfType(player, typeof(VlcMediaPlayer));
            player.Play();
            Assert.AreEqual(player.Media.Length, 58183);
            ArrayList subtitles = player.Media.SubtitleTracks;

            Assert.AreEqual(subtitles.Count, 17);

            Assert.AreEqual("Disable", ((VLCLibrary.libvlc_track_description_t)subtitles[0]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(0));
            Assert.AreEqual("Track 1 - [English]", ((VLCLibrary.libvlc_track_description_t)subtitles[1]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(1));
            Assert.AreEqual("Track 2 - [Nederlands]", ((VLCLibrary.libvlc_track_description_t)subtitles[2]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(2));
            Assert.AreEqual("Track 3 - [English]", ((VLCLibrary.libvlc_track_description_t)subtitles[3]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(3));
            Assert.AreEqual("Track 4 - [Suomi]", ((VLCLibrary.libvlc_track_description_t)subtitles[4]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(4));
            Assert.AreEqual("Track 5 - [Français]", ((VLCLibrary.libvlc_track_description_t)subtitles[5]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(5));
            Assert.AreEqual("Track 6 - [Deutsch]", ((VLCLibrary.libvlc_track_description_t)subtitles[6]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(6));
            Assert.AreEqual("Track 7 - [עברית]", ((VLCLibrary.libvlc_track_description_t)subtitles[7]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(7));
            Assert.AreEqual("Track 8 - [Magyar]", ((VLCLibrary.libvlc_track_description_t)subtitles[8]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(8));
            Assert.AreEqual("Track 9 - [Italiano]", ((VLCLibrary.libvlc_track_description_t)subtitles[9]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(9));
            Assert.AreEqual("Track 10 - [日本語]", ((VLCLibrary.libvlc_track_description_t)subtitles[10]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(10));
            Assert.AreEqual("Track 11 - [Norsk]", ((VLCLibrary.libvlc_track_description_t)subtitles[11]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(11));
            Assert.AreEqual("Track 12 - [Polski]", ((VLCLibrary.libvlc_track_description_t)subtitles[12]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(12));
            Assert.AreEqual("Track 13 - [Português]", ((VLCLibrary.libvlc_track_description_t)subtitles[13]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(13));
            Assert.AreEqual("Track 14 - [Русский]", ((VLCLibrary.libvlc_track_description_t)subtitles[14]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(14));
            Assert.AreEqual("Track 15 - [Español]", ((VLCLibrary.libvlc_track_description_t)subtitles[15]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(15));
            Assert.AreEqual("Track 16 - [Svenska]", ((VLCLibrary.libvlc_track_description_t)subtitles[16]).psz_name);
            Assert.IsTrue(player.ChangeSubtitleTrack(16));

            player.Stop();
            player.Dispose();

            instance.Dispose();
        }
Example #12
0
        public void VlcMediaConstructorTest()
        {
            VlcInstance    instance = null;
            VlcMediaPlayer player   = null;

            instance = new VlcInstance(VlcInstanceArgs);
            player   = instance.CreatePlayer(BaseTestFilePath + "n900_extremely_short.avi");
            Assert.IsInstanceOfType(player, typeof(VlcMediaPlayer));
            player.Play();
            Assert.AreEqual(player.Media.Length, 5000);
            player.Stop();
            player.Dispose();
        }
Example #13
0
 /// <summary>
 /// Resetting player is necessary as stopping and starting may cause issues when the volume has been changed.
 /// https://github.com/ZeBobo5/Vlc.DotNet/issues/130
 /// https://github.com/ZeBobo5/Vlc.DotNet/issues/381
 /// </summary>
 public void ResetPlayer()
 {
     if (VlcMediaPlayer != null)
     {
         VlcMediaPlayer.Dispose();
     }
     VlcMediaPlayer              = new VlcMediaPlayer(LibVlcDirectory, new string[] { "--aout=directsound" });
     VlcMediaPlayer.Stopped     += VlcMediaPlayer_Stopped;
     VlcMediaPlayer.TimeChanged += VlcMediaPlayer_TimeChanged;
     VlcMediaPlayer.Playing     += VlcMediaPlayer_Playing;
     VlcMediaPlayer.SetMedia(new Uri(Urls[0]));
     VlcMediaPlayer.Audio.Volume = Volume;
     VlcMediaPlayer.Audio.IsMute = IsMute;
 }
Example #14
0
        private void OnStopClick(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnStop.Enabled  = false;

            try
            {
                _mediaPlayer.Stop();
                _mediaPlayer.Dispose();
            }
            catch (Exception exception)
            {
                Debug.WriteLine($"Error: {exception}");
            }
        }
Example #15
0
        public void LengthTest()
        {
            VlcInstance    instance = null;
            VlcMediaPlayer player   = null;

            instance = new VlcInstance(VlcInstanceArgs);
            player   = instance.CreatePlayer(BaseTestFilePath + "mewmew-vorbis-ssa.mkv");
            Assert.IsInstanceOfType(player, typeof(VlcMediaPlayer));
            player.Play();
            Assert.AreEqual(player.Media.Length, 58183);

            player.Stop();
            player.Dispose();

            instance.Dispose();
        }
Example #16
0
 private void frmUNIcastPlayer_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (multicastClient != null)
     {
         multicastClient.EndReceive();
     }
     isExiting = true;
     if (media != null)
     {
         media.Dispose();
     }
     if (player != null)
     {
         player.Dispose();
     }
     instance.Dispose();
 }
Example #17
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (myVlcMediaPlayer != null)
         {
             UnregisterEvents();
             if (IsPlaying)
             {
                 Stop();
             }
             myVlcMediaPlayer.Dispose();
         }
         base.Dispose(disposing);
         GC.SuppressFinalize(this);
     }
 }
Example #18
0
 protected override void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             if (myVlcMediaPlayer != null)
             {
                 UnregisterEvents();
                 myVlcMediaPlayer.Dispose();
             }
             myVlcMediaPlayer = null;
         }
         disposed = true;
     }
     base.Dispose(disposing);
 }
        public void MyTestCleanup()
        {
            if (player != null)
            {
                if (!player.IsStopped)
                {
                    player.Stop();
                }

                player.Dispose();
            }

            if (instance != null)
            {
                instance.Dispose();
            }
        }
Example #20
0
        private void StopCapturing()
        {
            // Close connection with the service on Raspberry.
            myVideoChannel.CloseConnection();

            // Close the video pipe.
            if (myVideoPipe != null)
            {
                myVideoPipe.Close();
                myVideoPipe = null;
            }

            // Stop VLC.
            if (myPlayer != null)
            {
                myPlayer.Dispose();
                myPlayer = null;
            }
        }
Example #21
0
        private void AttachPlayer(VlcMediaPlayer mediaPlayer)
        {
            if (VlcMediaPlayer != null)
            {
                VlcMediaPlayer.PositionChanged -= VlcMediaPlayerPositionChanged;
                VlcMediaPlayer.TimeChanged     -= VlcMediaPlayerTimeChanged;
                VlcMediaPlayer.EndReached      -= VlcMediaPlayerEndReached;
                VlcMediaPlayer.SeekableChanged -= VlcMediaPlayerSeekableChanged;
                VlcMediaPlayer.LengthChanged   -= VlcMediaPlayerLengthChanged;
                VlcMediaPlayer.SubItemChanged  -= VlcMediaPlayer_SubItemChanged;
                VlcMediaPlayer.MediaChanged    -= VlcMediaPlayerMediaChanged;

                VlcMediaPlayer.SetVideoDecodeCallback(null, null, null, IntPtr.Zero);
                VlcMediaPlayer.SetVideoFormatCallback(null, null);
                //VlcMediaPlayer.SetAudioCallback(null, null, null, null, null);
                //VlcMediaPlayer.SetAudioFormatCallback(null, null);
                //VlcMediaPlayer.SetAudioVolumeCallback(null);

                VlcMediaPlayer.Dispose();
            }

            VlcMediaPlayer = mediaPlayer;

            if (VlcMediaPlayer != null)
            {
                VlcMediaPlayer.PositionChanged += VlcMediaPlayerPositionChanged;
                VlcMediaPlayer.TimeChanged     += VlcMediaPlayerTimeChanged;
                VlcMediaPlayer.EndReached      += VlcMediaPlayerEndReached;
                VlcMediaPlayer.SeekableChanged += VlcMediaPlayerSeekableChanged;
                VlcMediaPlayer.LengthChanged   += VlcMediaPlayerLengthChanged;
                VlcMediaPlayer.MediaChanged    += VlcMediaPlayerMediaChanged;
                VlcMediaPlayer.SubItemChanged  += VlcMediaPlayer_SubItemChanged;

                VlcMediaPlayer.SetVideoDecodeCallback(_lockCallback, _unlockCallback, _displayCallback, IntPtr.Zero);
                VlcMediaPlayer.SetVideoFormatCallback(_formatCallback, _cleanupCallback);
                //VlcMediaPlayer.SetAudioCallback(_audioPlayCallback, _audioPauseCallback, _audioResumeCallback, _audioFlushCallback, _audioDrainCallback);
                //VlcMediaPlayer.SetAudioFormatCallback(_audioSetupCallback, _audioCleanupCallback);
                //VlcMediaPlayer.SetAudioVolumeCallback(_audioSetVolumeCallback);
            }
        }
Example #22
0
 void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
     //throw new NotImplementedException();
     player.Dispose();
     Pbase.Dispose();
 }