public void SetCallbacks(AudioCallbacks callbacks) { if (callbacks.SoundCallback == null) { throw new ArgumentNullException("Sound playback callback must be set"); } m_callbacks = callbacks; LibVlcMethods.libvlc_audio_set_callbacks(m_hMediaPlayer, m_hSound, m_hPause, m_hResume, m_hFlush, m_hDrain, IntPtr.Zero); LibVlcMethods.libvlc_audio_set_volume_callback(m_hMediaPlayer, m_hVolume); }
protected override void Dispose(bool disposing) { LibVlcMethods.libvlc_audio_set_format_callbacks(_mHMediaPlayer, IntPtr.Zero, IntPtr.Zero); LibVlcMethods.libvlc_audio_set_callbacks(_mHMediaPlayer, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (disposing) { _mFormatSetupCb = null; _mExcHandler = null; _mCallbacks = null; _mCallbacksDelegates.Clear(); } }
public void SetCallbacks(AudioCallbacks callbacks) { if (callbacks.SoundCallback == null) { throw new ArgumentNullException("Sound playback callback must be set"); } _mCallbacks = callbacks; LibVlcMethods.libvlc_audio_set_callbacks(_mHMediaPlayer, _mHSound, _mHPause, _mHResume, _mHFlush, _mHDrain, IntPtr.Zero); LibVlcMethods.libvlc_audio_set_volume_callback(_mHMediaPlayer, _mHVolume); }
/// <summary> /// Start video source. /// </summary> /// /// <remarks>Starts video source and return execution to caller. Video source /// object creates background thread and notifies about new frames with the /// help of <see cref="NewFrame"/> event.</remarks> /// /// <exception cref="ArgumentException">Video source is not specified.</exception> /// public void Start() { if (!VlcHelper.VlcInstalled) return; Isstopping = false; if (!IsRunning && !_starting) { _starting = true; // check source if (string.IsNullOrEmpty(_source)) throw new ArgumentException("Video source is not specified."); DisposePlayer(); _mFactory = new MediaPlayerFactory(false); _mPlayer = _mFactory.CreatePlayer<IVideoPlayer>(); _mPlayer.Events.PlayerPlaying += EventsPlayerPlaying; _mPlayer.Events.PlayerStopped += EventsPlayerStopped; _mPlayer.Events.PlayerEncounteredError += EventsPlayerEncounteredError; _mMedia = _mFactory.CreateMedia<IMedia>(_source, Arguments); _mPlayer.Open(_mMedia); GC.KeepAlive(_mFactory); GC.KeepAlive(_mPlayer); GC.KeepAlive(_mMedia); _needsSetup = true; var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback); _mPlayer.CustomAudioRenderer.SetFormatCallback(fc); var ac = new AudioCallbacks { SoundCallback = SoundCallback }; _mPlayer.CustomAudioRenderer.SetCallbacks(ac); _mPlayer.CustomRenderer.SetFormat(new BitmapFormat(FormatWidth, FormatHeight, ChromaType.RV24)); _mPlayer.CustomRenderer.SetCallback(FrameCallback); _mMedia.Parse(true); _framesReceived = 0; _mPlayer.Play(); } }
/// <summary> /// Start audio source. /// </summary> /// /// <remarks>Starts audio source and return execution to caller. audio source /// object creates background thread and notifies about new frames with the /// help of <see cref="DataAvailable"/> event.</remarks> /// /// <exception cref="ArgumentException">audio source is not specified.</exception> /// public void Start() { if (!VlcHelper.VlcInstalled) return; if (!IsRunning) { // check source if (string.IsNullOrEmpty(_source)) throw new ArgumentException("Audio source is not specified."); DisposePlayer(); _mFactory = new MediaPlayerFactory(false); _mPlayer = _mFactory.CreatePlayer<IVideoPlayer>(); _mPlayer.Events.PlayerPlaying += EventsPlayerPlaying; _mPlayer.Events.PlayerStopped += EventsPlayerStopped; _mPlayer.Events.PlayerEncounteredError += EventsPlayerEncounteredError; string[] args = Arguments.Trim(',').Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); List<String> inargs = args.ToList(); inargs.Add(":sout=#transcode{vcodec=none}:Display"); _mMedia = _mFactory.CreateMedia<IMedia>(_source, inargs.ToArray()); _mPlayer.Open(_mMedia); GC.KeepAlive(_mFactory); GC.KeepAlive(_mPlayer); GC.KeepAlive(_mMedia); _needsSetup = true; var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback); _mPlayer.CustomAudioRenderer.SetFormatCallback(fc); var ac = new AudioCallbacks {SoundCallback = SoundCallback}; _mPlayer.CustomAudioRenderer.SetCallbacks(ac); _mMedia.Events.ParsedChanged += EventsParsedChanged; _mMedia.Parse(true); _mPlayer.Play(); } }
private void WorkerThread() { bool file = false; try { if (File.Exists(_source)) { file = true; } } catch { // ignored } if (_mFactory == null) { var args = new List<string> { "-I", "dumy", "--ignore-config", "--no-osd", "--disable-screensaver", "--plugin-path=./plugins", "--novideo" }; if (file) args.Add("--file-caching=3000"); try { var l2 = args.ToList(); l2.AddRange(_arguments); l2 = l2.Distinct().ToList(); _mFactory = new MediaPlayerFactory(l2.ToArray()); } catch (Exception ex) { Logger.LogExceptionToFile(ex, "VLC Audio"); Logger.LogMessageToFile("VLC arguments are: " + String.Join(",", args.ToArray()), "VLC Audio"); Logger.LogMessageToFile("Using default VLC configuration.", "VLC Audio"); _mFactory = new MediaPlayerFactory(args.ToArray()); } GC.KeepAlive(_mFactory); } _mMedia = file ? _mFactory.CreateMedia<IMediaFromFile>(_source) : _mFactory.CreateMedia<IMedia>(_source); _mMedia.Events.DurationChanged += EventsDurationChanged; _mMedia.Events.StateChanged += EventsStateChanged; try { _mPlayer?.Dispose(); } catch { // ignored } _mPlayer = null; _mPlayer = _mFactory.CreatePlayer<IVideoPlayer>(); _mPlayer.Events.TimeChanged += EventsTimeChanged; var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback); _mPlayer.CustomAudioRenderer.SetFormatCallback(fc); var ac = new AudioCallbacks { SoundCallback = SoundCallback }; _mPlayer.CustomAudioRenderer.SetCallbacks(ac); _mPlayer.CustomAudioRenderer.SetExceptionHandler(Handler); GC.KeepAlive(_mPlayer); _needsSetup = true; _stopping = false; _mPlayer.Open(_mMedia); _mMedia.Parse(true); _mPlayer.Delay = 0; _framesReceived = 0; Duration = Time = 0; LastFrame = DateTime.MinValue; //check if file source (isseekable in _mPlayer is not reliable) Seekable = false; try { var p = Path.GetFullPath(_mMedia.Input); Seekable = !string.IsNullOrEmpty(p); } catch (Exception) { Seekable = false; } _mPlayer.WindowHandle = IntPtr.Zero; _mPlayer.Play(); _stopEvent.WaitOne(); if (!Seekable && !_stopRequested) { AudioFinished?.Invoke(this, new PlayingFinishedEventArgs(ReasonToFinishPlaying.DeviceLost)); } else { AudioFinished?.Invoke(this, new PlayingFinishedEventArgs(ReasonToFinishPlaying.StoppedByUser)); } DisposePlayer(); _stopEvent?.Close(); _stopEvent = null; }
/// <summary> /// Start video source. /// </summary> /// /// <remarks>Starts video source and return execution to caller. Video source /// object creates background thread and notifies about new frames with the /// help of <see cref="NewFrame"/> event.</remarks> /// /// <exception cref="ArgumentException">Video source is not specified.</exception> /// public void Start() { lock (_lock) { if (!VlcHelper.VlcInstalled) return; if (_stoprequested) return; if (!IsRunning && !_starting) { _starting = true; if (string.IsNullOrEmpty(_source)) throw new ArgumentException("Video source is not specified."); bool init = _mFactory == null; bool file = false; try { if (File.Exists(_source)) { file = true; } } catch { } if (init) { var args = new List<string> { "-I", "dumy", "--ignore-config", "--no-osd", "--disable-screensaver", "--plugin-path=./plugins" }; if (file) args.Add("--file-caching=3000"); _mFactory = new MediaPlayerFactory(args.ToArray()); _mPlayer = _mFactory.CreatePlayer<IVideoPlayer>(); _mPlayer.Events.PlayerPlaying += EventsPlayerPlaying; _mPlayer.Events.TimeChanged += EventsTimeChanged; var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback); _mPlayer.CustomAudioRenderer.SetFormatCallback(fc); var ac = new AudioCallbacks { SoundCallback = SoundCallback }; _mPlayer.CustomAudioRenderer.SetCallbacks(ac); _mPlayer.CustomRenderer.SetCallback(FrameCallback); _mPlayer.CustomRenderer.SetExceptionHandler(Handler); _mPlayer.CustomAudioRenderer.SetExceptionHandler(Handler); } if (file) _mMedia = _mFactory.CreateMedia<IMediaFromFile>(_source); else _mMedia = _mFactory.CreateMedia<IMedia>(_source); _mMedia.AddOptions(_arguments); _mMedia.Events.DurationChanged -= EventsDurationChanged; _mMedia.Events.StateChanged -= EventsStateChanged; _mMedia.Events.DurationChanged += EventsDurationChanged; _mMedia.Events.StateChanged += EventsStateChanged; _needsSetup = true; _mPlayer.CustomRenderer.SetFormat(new BitmapFormat(FormatWidth, FormatHeight, ChromaType.RV24)); _mPlayer.Open(_mMedia); _mMedia.Parse(true); _framesReceived = 0; Duration = Time = 0; LastFrame = DateTime.MinValue; _mPlayer.Play(); //check if file source (isseekable in _mPlayer is not reliable) Seekable = false; try { var p = Path.GetFullPath(_mMedia.Input); Seekable = !String.IsNullOrEmpty(p); } catch (Exception) { Seekable = false; } } } }
/// <summary> /// Start video source. /// </summary> /// /// <remarks>Starts video source and return execution to caller. Video source /// object creates background thread and notifies about new frames with the /// help of <see cref="NewFrame"/> event.</remarks> /// /// <exception cref="ArgumentException">Video source is not specified.</exception> /// public void Start() { lock (_lock) { if (!VlcHelper.VlcInstalled) return; if (_stoprequested) return; if (!IsRunning && !_starting) { _starting = true; // check source if (string.IsNullOrEmpty(_source)) throw new ArgumentException("Audio source is not specified."); bool init = _mFactory == null; if (init) { _mFactory = new MediaPlayerFactory(); _mPlayer = _mFactory.CreatePlayer<IVideoPlayer>(); _mPlayer.Events.PlayerPlaying += EventsPlayerPlaying; _mPlayer.Events.TimeChanged += EventsTimeChanged; var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback); _mPlayer.CustomAudioRenderer.SetFormatCallback(fc); var ac = new AudioCallbacks { SoundCallback = SoundCallback }; _mPlayer.CustomAudioRenderer.SetCallbacks(ac); _mPlayer.CustomAudioRenderer.SetExceptionHandler(Handler); } bool file = false; try { if (File.Exists(_source)) { file = true; } } catch { } if (file) _mMedia = _mFactory.CreateMedia<IMediaFromFile>(_source); else _mMedia = _mFactory.CreateMedia<IMedia>(_source); _mMedia.AddOptions(_arguments); _mMedia.Events.DurationChanged -= EventsDurationChanged; _mMedia.Events.StateChanged -= EventsStateChanged; _mMedia.Events.DurationChanged += EventsDurationChanged; _mMedia.Events.StateChanged += EventsStateChanged; _needsSetup = true; _mPlayer.Open(_mMedia); _mMedia.Parse(true); _framesReceived = 0; Duration = Time = 0; LastFrame = DateTime.MinValue; _mPlayer.Play(); //check if file source (isseekable in _mPlayer is not reliable) Seekable = false; try { var p = Path.GetFullPath(_mMedia.Input); Seekable = !String.IsNullOrEmpty(p); } catch (Exception) { Seekable = false; } } } }
protected override void Dispose(bool disposing) { LibVlcMethods.libvlc_audio_set_format_callbacks(m_hMediaPlayer, IntPtr.Zero, IntPtr.Zero); LibVlcMethods.libvlc_audio_set_callbacks(m_hMediaPlayer, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (disposing) { m_formatSetupCB = null; m_callbacks = null; m_callbacksDelegates.Clear(); } }
private void WorkerThread() { bool file = false; try { if (File.Exists(_source)) { file = true; } } catch { } if (_mFactory == null) { var args = new List<string> { "-I", "dumy", "--ignore-config", "--no-osd", "--disable-screensaver", "--plugin-path=./plugins" }; if (file) args.Add("--file-caching=3000"); try { var l2 = args.ToList(); l2.AddRange(_arguments); l2 = l2.Distinct().ToList(); _mFactory = new MediaPlayerFactory(l2.ToArray()); } catch (Exception ex) { MainForm.LogExceptionToFile(ex, "VLC Stream"); MainForm.LogMessageToFile("VLC arguments are: " + String.Join(",", args.ToArray()), "VLC Stream"); MainForm.LogMessageToFile("Using default VLC configuration.", "VLC Stream"); _mFactory = new MediaPlayerFactory(args.ToArray()); } GC.KeepAlive(_mFactory); } if (file) _mMedia = _mFactory.CreateMedia<IMediaFromFile>(_source); else _mMedia = _mFactory.CreateMedia<IMedia>(_source); _mMedia.Events.DurationChanged += EventsDurationChanged; _mMedia.Events.StateChanged += EventsStateChanged; if (_mPlayer != null) { try { _mPlayer.Dispose(); } catch { } _mPlayer = null; } _mPlayer = _mFactory.CreatePlayer<IVideoPlayer>(); _mPlayer.Events.TimeChanged += EventsTimeChanged; var fc = new Func<SoundFormat, SoundFormat>(SoundFormatCallback); _mPlayer.CustomAudioRenderer.SetFormatCallback(fc); var ac = new AudioCallbacks { SoundCallback = SoundCallback }; _mPlayer.CustomAudioRenderer.SetCallbacks(ac); _mPlayer.CustomAudioRenderer.SetExceptionHandler(Handler); _mPlayer.CustomRenderer.SetCallback(FrameCallback); _mPlayer.CustomRenderer.SetExceptionHandler(Handler); GC.KeepAlive(_mPlayer); _needsSetup = true; _stopping = false; _mPlayer.CustomRenderer.SetFormat(new BitmapFormat(FormatWidth, FormatHeight, ChromaType.RV24)); _mPlayer.Open(_mMedia); _mMedia.Parse(true); _mPlayer.Delay = 0; _framesReceived = 0; Duration = Time = 0; LastFrame = DateTime.MinValue; //check if file source (isseekable in _mPlayer is not reliable) Seekable = false; try { var p = Path.GetFullPath(_mMedia.Input); Seekable = !String.IsNullOrEmpty(p); } catch (Exception) { Seekable = false; } _mPlayer.WindowHandle = IntPtr.Zero; _videoQueue = new ConcurrentQueue<Bitmap>(); _audioQueue = new ConcurrentQueue<byte[]>(); _eventing = new Thread(EventManager) { Name = "ffmpeg eventing", IsBackground = true }; _eventing.Start(); _mPlayer.Play(); _stopEvent.WaitOne(); if (_eventing != null && !_eventing.Join(0)) _eventing.Join(); if (!Seekable && !_stopRequested) { if (PlayingFinished != null) PlayingFinished(this, ReasonToFinishPlaying.DeviceLost); if (AudioFinished != null) AudioFinished(this, ReasonToFinishPlaying.DeviceLost); } else { if (PlayingFinished != null) PlayingFinished(this, ReasonToFinishPlaying.StoppedByUser); if (AudioFinished != null) AudioFinished(this, ReasonToFinishPlaying.StoppedByUser); } DisposePlayer(); Free(); }