Пример #1
0
        private void Init()
        {
            _mediaPlayer?.Dispose();

            _videoFormat = VideoFormat;
            _lockCB      = LockVideo;
            _unlockCB    = UnlockVideo;
            _displayCB   = DisplayVideo;
            //_cleanupVideoCB = CleanupVideo;

            _audioSetup   = AudioSetup;
            _processAudio = ProcessAudio;
            _cleanupAudio = CleanupAudio;
            _pauseAudio   = PauseAudio;
            _resumeAudio  = ResumeAudio;
            _flushAudio   = FlushAudio;
            _drainAudio   = DrainAudio;
            string overrideURL = null;

            if (_camera != null)
            {
                switch (_camera.Camobject.settings.sourceindex)
                {
                case 9:
                    var od = _camera.ONVIFDevice;
                    if (od != null)
                    {
                        var ep = od.StreamEndpoint;
                        if (ep != null)
                        {
                            var u = ep.Uri.Uri;
                            overrideURL = u;
                        }
                    }
                    break;
                }
            }

            FromType ftype = FromType.FromLocation;

            Seekable = false;


            var murl = overrideURL ?? Source;

            if (string.IsNullOrEmpty(murl))
            {
                throw new Exception("Video source is empty");
            }

            try
            {
                var p = Path.GetFullPath(overrideURL ?? Source);
                Seekable = !string.IsNullOrEmpty(p);
                if (Seekable)
                {
                    ftype = FromType.FromPath;
                }
            }
            catch (Exception)
            {
                Seekable = false;
            }


            using (var media = new Media(LibVLC, murl, ftype))
            {
                Duration = Time = 0;

                foreach (var opt in _options)
                {
                    media.AddOption(opt);
                }

                _mediaPlayer = new MediaPlayer(media);
                _mediaPlayer.SetVideoFormatCallbacks(_videoFormat, null);//  _cleanupVideoCB);
                _mediaPlayer.SetVideoCallbacks(_lockCB, _unlockCB, _displayCB);
                _mediaPlayer.TimeChanged           += _mediaPlayer_TimeChanged;
                _mediaPlayer.EnableHardwareDecoding = false;


                _mediaPlayer.SetAudioFormatCallback(_audioSetup, _cleanupAudio);
                _mediaPlayer.SetAudioCallbacks(_processAudio, _pauseAudio, _resumeAudio, _flushAudio, _drainAudio);


                _mediaPlayer.EncounteredError += (sender, e) =>
                {
                    ErrorHandler?.Invoke("VLC Error");
                    _res = ReasonToFinishPlaying.VideoSourceError;
                };

                _mediaPlayer.EndReached += (sender, e) =>
                {
                    _res = ReasonToFinishPlaying.VideoSourceError;
                };

                _mediaPlayer.Stopped += (sender, e) =>
                {
                    Logger.LogMessage("VLC stopped");
                    Task.Run(() =>
                    {
                        Debug.WriteLine("CLEANUP");
                        IsRunning = false;
                        _stopping = false;


                        _stopped?.Set();

                        PlayingFinished?.Invoke(this, new PlayingFinishedEventArgs(_res));
                        AudioFinished?.Invoke(this, new PlayingFinishedEventArgs(_res));
                    });
                };
            }
            _lastTimeUpdate = DateTime.UtcNow;
            _mediaPlayer.Play();
        }
Пример #2
0
        private void Init()
        {
            _mediaPlayer?.Dispose();

            _videoFormat    = VideoFormat;
            _lockCB         = LockVideo;
            _unlockCB       = UnlockVideo;
            _displayCB      = DisplayVideo;
            _cleanupVideoCB = CleanupVideo;

            _audioSetup   = AudioSetup;
            _processAudio = ProcessAudio;
            _cleanupAudio = CleanupAudio;
            _pauseAudio   = PauseAudio;
            _resumeAudio  = ResumeAudio;
            _flushAudio   = FlushAudio;
            _drainAudio   = DrainAudio;
            string overrideURL = null;

            if (_camera != null)
            {
                switch (_camera.Camobject.settings.sourceindex)
                {
                case 9:
                    var od = _camera.ONVIFDevice;
                    if (od != null)
                    {
                        var ep = od.StreamEndpoint;
                        if (ep != null)
                        {
                            var u = ep.Uri.Uri;
                            overrideURL = u;
                        }
                    }
                    break;
                }
            }

            FromType ftype = FromType.FromLocation;

            Seekable = false;
            try
            {
                var p = Path.GetFullPath(overrideURL ?? Source);
                Seekable = !string.IsNullOrEmpty(p);
                if (Seekable)
                {
                    ftype = FromType.FromPath;
                }
            }
            catch (Exception)
            {
                Seekable = false;
            }
            using (var media = new Media(LibVLC, overrideURL ?? Source, ftype))
            {
                Duration = Time = 0;

                foreach (var opt in _options)
                {
                    media.AddOption(opt);
                }

                _mediaPlayer = new MediaPlayer(media);
                _mediaPlayer.SetVideoFormatCallbacks(_videoFormat, _cleanupVideoCB);
                _mediaPlayer.SetVideoCallbacks(_lockCB, _unlockCB, _displayCB);

                if (!_ignoreAudio)
                {
                    _mediaPlayer.SetAudioFormatCallback(_audioSetup, _cleanupAudio);
                    _mediaPlayer.SetAudioCallbacks(_processAudio, _pauseAudio, _resumeAudio, _flushAudio, _drainAudio);
                }

                _mediaPlayer.EncounteredError += (sender, e) =>
                {
                    ErrorHandler?.Invoke("VLC Error");
                    _res  = ReasonToFinishPlaying.DeviceLost;
                    _quit = true;
                };

                _mediaPlayer.EndReached += (sender, e) =>
                {
                    _res  = ReasonToFinishPlaying.DeviceLost;
                    _quit = true;
                };

                _mediaPlayer.Stopped += (sender, e) =>
                {
                    _quit = true;
                };
            }
            _mediaPlayer.Play();
        }