From 8aecdcc938777864c7ab0d66ff6e05a99fe67b21 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 17 Mar 2015 21:31:09 +0100 Subject: Add VLC exit handler --- src/VLCInput.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- src/VLCInput.h | 36 ++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/VLCInput.cpp b/src/VLCInput.cpp index 6b7a4bb..54d9250 100644 --- a/src/VLCInput.cpp +++ b/src/VLCInput.cpp @@ -39,7 +39,7 @@ void prepareRender( { VLCInput* in = (VLCInput*)p_audio_data; - in->preRender(pp_pcm_buffer, size); + in->preRender_cb(pp_pcm_buffer, size); } @@ -60,7 +60,13 @@ void handleStream( assert(rate == in->getRate()); assert(bits_per_sample == 8*BYTES_PER_SAMPLE); - in->postRender(p_pcm_buffer, size); + in->postRender_cb(p_pcm_buffer, size); +} + +// VLC Exit callback +void handleVLCExit(void* opaque) +{ + ((VLCInput*)opaque)->exit_cb(); } int VLCInput::prepare() @@ -96,6 +102,8 @@ int VLCInput::prepare() // Launch VLC m_vlc = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); + libvlc_set_exit_handler(m_vlc, handleVLCExit, this); + // Load the media libvlc_media_t *m; m = libvlc_media_new_location(m_vlc, m_uri.c_str()); @@ -109,7 +117,7 @@ int VLCInput::prepare() return 0; } -void VLCInput::preRender(uint8_t** pp_pcm_buffer, size_t size) +void VLCInput::preRender_cb(uint8_t** pp_pcm_buffer, size_t size) { const size_t max_length = 20 * size; @@ -127,7 +135,34 @@ void VLCInput::preRender(uint8_t** pp_pcm_buffer, size_t size) } } -void VLCInput::postRender(uint8_t* p_pcm_buffer, size_t size) +void VLCInput::exit_cb() +{ + boost::mutex::scoped_lock lock(m_queue_mutex); + + fprintf(stderr, "VLC exit, restarting...\n"); + + cleanup(); + m_current_buf.empty(); + prepare(); +} + +void VLCInput::cleanup() +{ + if (m_mp) { + /* Stop playing */ + libvlc_media_player_stop(m_mp); + + /* Free the media_player */ + libvlc_media_player_release(m_mp); + } + + if (m_vlc) { + libvlc_release(m_vlc); + m_vlc = NULL; + } +} + +void VLCInput::postRender_cb(uint8_t* p_pcm_buffer, size_t size) { boost::mutex::scoped_lock lock(m_queue_mutex); diff --git a/src/VLCInput.h b/src/VLCInput.h index 13b0695..e387044 100644 --- a/src/VLCInput.h +++ b/src/VLCInput.h @@ -48,39 +48,43 @@ class VLCInput m_rate(rate), m_vlc(NULL) { } - ~VLCInput() { - if (m_mp) { - /* Stop playing */ - libvlc_media_player_stop(m_mp); - - /* Free the media_player */ - libvlc_media_player_release(m_mp); - } - - if (m_vlc) { - libvlc_release(m_vlc); - m_vlc = NULL; - } - } + ~VLCInput() { cleanup(); } /* Prepare the audio input */ int prepare(); + /* Read exactly length bytes into buf. + * Blocks if not enough data is available, + * or returns zero if EOF reached. + * + * Returns the number of bytes written into + * the buffer. + */ ssize_t read(uint8_t* buf, size_t length); // Callbacks for VLC - void preRender( + + /* Notification of VLC exit */ + void exit_cb(void); + + /* Prepare a buffer for VLC */ + void preRender_cb( uint8_t** pp_pcm_buffer, size_t size); - void postRender( + /* Receive a buffer with audio samples + * from VLC + */ + void postRender_cb( uint8_t* p_pcm_buffer, size_t size); int getRate() { return m_rate; } protected: + void cleanup(void); + ssize_t m_read(uint8_t* buf, size_t length); std::vector m_current_buf; -- cgit v1.2.3