[vlc-devel] [PATCH 6/6] chromecast: Use vlc::threads

Hugo Beauzée-Luyssen hugo at beauzee.fr
Mon Aug 20 14:38:20 CEST 2018


---
 modules/stream_out/chromecast/chromecast.h    |   8 +-
 .../stream_out/chromecast/chromecast_ctrl.cpp | 102 ++++++++----------
 2 files changed, 52 insertions(+), 58 deletions(-)

diff --git a/modules/stream_out/chromecast/chromecast.h b/modules/stream_out/chromecast/chromecast.h
index 4bd292b7c8..22aec5e2f0 100644
--- a/modules/stream_out/chromecast/chromecast.h
+++ b/modules/stream_out/chromecast/chromecast.h
@@ -34,6 +34,8 @@
 #include <vlc_tls.h>
 #include <vlc_interrupt.h>
 #include <vlc_httpd.h>
+#include <vlc_threads.h>
+#include <vlc_cxx_helpers.hpp>
 
 #include <atomic>
 #include <sstream>
@@ -252,9 +254,9 @@ private:
     unsigned m_last_request_id;
     int64_t m_mediaSessionId;
 
-    mutable vlc_mutex_t  m_lock;
-    vlc_cond_t   m_stateChangedCond;
-    vlc_cond_t   m_pace_cond;
+    mutable vlc::threads::mutex  m_lock;
+    vlc::threads::condition_variable m_stateChangedCond;
+    vlc::threads::condition_variable m_pace_cond;
     vlc_thread_t m_chromecastThread;
 
     on_input_event_itf    m_on_input_event;
diff --git a/modules/stream_out/chromecast/chromecast_ctrl.cpp b/modules/stream_out/chromecast/chromecast_ctrl.cpp
index 63e7095306..119446ebef 100644
--- a/modules/stream_out/chromecast/chromecast_ctrl.cpp
+++ b/modules/stream_out/chromecast/chromecast_ctrl.cpp
@@ -126,10 +126,6 @@ intf_sys_t::intf_sys_t(vlc_object_t * const p_this, int port, std::string device
     if( unlikely(m_ctl_thread_interrupt == NULL) )
         throw std::runtime_error( "error creating interrupt context" );
 
-    vlc_mutex_init(&m_lock);
-    vlc_cond_init( &m_stateChangedCond );
-    vlc_cond_init( &m_pace_cond );
-
     std::stringstream ss;
     ss << "http://" << m_communication->getServerIp() << ":" << port;
     m_art_http_ip = ss.str();
@@ -151,8 +147,6 @@ intf_sys_t::intf_sys_t(vlc_object_t * const p_this, int port, std::string device
                   VLC_THREAD_PRIORITY_LOW))
     {
         vlc_interrupt_destroy( m_ctl_thread_interrupt );
-        vlc_cond_destroy( &m_stateChangedCond );
-        vlc_cond_destroy( &m_pace_cond );
         var_SetAddress( m_module->obj.parent->obj.parent, CC_SHARED_VAR_NAME, NULL );
         throw std::runtime_error( "error creating cc thread" );
     }
@@ -162,7 +156,7 @@ intf_sys_t::~intf_sys_t()
 {
     var_Destroy( m_module->obj.parent->obj.parent, CC_SHARED_VAR_NAME );
 
-    vlc_mutex_lock(&m_lock);
+    m_lock.lock();
     if( m_communication )
     {
         switch ( m_state )
@@ -186,14 +180,14 @@ intf_sys_t::~intf_sys_t()
             break;
         }
 
-        vlc_mutex_unlock(&m_lock);
+        m_lock.unlock();
         vlc_interrupt_kill( m_ctl_thread_interrupt );
         vlc_join(m_chromecastThread, NULL);
 
         delete m_communication;
     }
     else
-        vlc_mutex_unlock(&m_lock);
+        m_lock.unlock();
 
     vlc_interrupt_destroy( m_ctl_thread_interrupt );
 
@@ -204,10 +198,6 @@ intf_sys_t::~intf_sys_t()
         httpd_FileDelete( m_httpd_file );
 
     free( m_art_url );
-
-    vlc_cond_destroy(&m_stateChangedCond);
-    vlc_cond_destroy(&m_pace_cond);
-    vlc_mutex_destroy(&m_lock);
 }
 
 void intf_sys_t::reinit()
@@ -248,14 +238,15 @@ int intf_sys_t::httpd_file_fill( uint8_t *psz_request, uint8_t **pp_data, int *p
 {
     (void) psz_request;
 
-    vlc_mutex_lock( &m_lock );
+    char *psz_art;
+    {
+    vlc::threads::mutex_locker lock( m_lock );
     if( !m_art_url )
     {
-        vlc_mutex_unlock( &m_lock );
         return VLC_EGENERIC;
     }
-    char *psz_art = strdup( m_art_url );
-    vlc_mutex_unlock( &m_lock );
+    psz_art = strdup( m_art_url );
+    }
 
     stream_t *s = vlc_stream_NewURL( m_module, psz_art );
     free( psz_art );
@@ -323,7 +314,7 @@ void intf_sys_t::prepareHttpArtwork()
         ss_art_idx << getHttpArtRoot() << "/" << m_art_idx;
         m_art_idx++;
 
-        vlc_mutex_unlock( &m_lock );
+        m_lock.unlock();
 
         if( m_httpd_file )
             httpd_FileDelete( m_httpd_file );
@@ -331,7 +322,7 @@ void intf_sys_t::prepareHttpArtwork()
                                       "application/octet-stream", NULL, NULL,
                                       httpd_file_fill_cb, (httpd_file_sys_t *) this );
 
-        vlc_mutex_lock( &m_lock );
+        m_lock.lock();
         if( !m_httpd_file )
             return;
 
@@ -381,13 +372,13 @@ void intf_sys_t::tryLoad()
 
 void intf_sys_t::setRetryOnFail( bool enabled )
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker locker(m_lock);
     m_retry_on_fail = enabled;
 }
 
 void intf_sys_t::setHasInput( const std::string mime_type )
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker locker(m_lock);
     msg_Dbg( m_module, "Loading content" );
 
     if( m_state == Dead )
@@ -414,7 +405,7 @@ void intf_sys_t::setHasInput( const std::string mime_type )
 
     tryLoad();
 
-    vlc_cond_signal( &m_stateChangedCond );
+    m_stateChangedCond.signal();
 }
 
 bool intf_sys_t::isStateError() const
@@ -463,15 +454,13 @@ bool intf_sys_t::isStateReady() const
 
 void intf_sys_t::setPacing(bool do_pace)
 {
-    vlc_mutex_lock( &m_lock );
-    if( m_pace == do_pace )
     {
-        vlc_mutex_unlock( &m_lock );
+    vlc::threads::mutex_locker locker( m_lock );
+    if( m_pace == do_pace )
         return;
-    }
     m_pace = do_pace;
-    vlc_mutex_unlock( &m_lock );
-    vlc_cond_signal( &m_pace_cond );
+    }
+    m_pace_cond.signal();
 }
 
 static void interrupt_wake_up_cb( void *data )
@@ -482,14 +471,14 @@ static void interrupt_wake_up_cb( void *data )
 
 void intf_sys_t::interrupt_wake_up()
 {
-    vlc_mutex_locker locker( &m_lock );
+    vlc::threads::mutex_locker locker( m_lock );
     m_interrupted = true;
-    vlc_cond_signal( &m_pace_cond );
+    m_pace_cond.signal();
 }
 
 int intf_sys_t::pace()
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker locker( m_lock );
 
     m_interrupted = false;
     vlc_interrupt_register( interrupt_wake_up_cb, this );
@@ -500,7 +489,7 @@ int intf_sys_t::pace()
      * CC to finish. In case the demux filter is EOF, we always wait for
      * 500msec (unless interrupted from the input thread). */
     while( !isFinishedPlaying() && ( m_pace || m_input_eof ) && !m_interrupted && ret == 0 )
-        ret = vlc_cond_timedwait( &m_pace_cond, &m_lock, deadline );
+        ret = m_pace_cond.timedwait( m_lock, deadline );
 
     vlc_interrupt_unregister();
 
@@ -521,9 +510,12 @@ int intf_sys_t::pace()
 
 void intf_sys_t::sendInputEvent(enum cc_input_event event, union cc_input_arg arg)
 {
-    vlc_mutex_lock(&m_lock);
-    on_input_event_itf on_input_event = m_on_input_event;
-    void *data = m_on_input_event_data;
+    on_input_event_itf on_input_event;
+    void *data;
+    {
+    vlc::threads::mutex_locker locker( m_lock );
+    on_input_event = m_on_input_event;
+    data = m_on_input_event_data;
 
     switch (event)
     {
@@ -540,7 +532,7 @@ void intf_sys_t::sendInputEvent(enum cc_input_event event, union cc_input_arg ar
         default:
             break;
     }
-    vlc_mutex_unlock(&m_lock);
+    }
 
     if (on_input_event)
         on_input_event(data, event, arg);
@@ -637,7 +629,7 @@ void intf_sys_t::mainLoop()
         // Reset the interrupt state to avoid commands not being sent (since
         // the context is still flagged as interrupted)
         vlc_interrupt_unregister();
-        vlc_mutex_locker lock( &m_lock );
+        vlc::threads::mutex_locker lock( m_lock );
         while ( m_msgQueue.empty() == false )
         {
             QueueableMessages msg = m_msgQueue.front();
@@ -671,7 +663,7 @@ void intf_sys_t::processAuthMessage( const castchannel::CastMessage& msg )
     }
     else
     {
-        vlc_mutex_locker locker(&m_lock);
+        vlc::threads::mutex_locker lock( m_lock );
         setState( Connecting );
         m_communication->msgConnect(DEFAULT_CHOMECAST_RECEIVER);
         m_communication->msgReceiverGetStatus();
@@ -724,7 +716,7 @@ bool intf_sys_t::processReceiverMessage( const castchannel::CastMessage& msg )
             }
         }
 
-        vlc_mutex_locker locker(&m_lock);
+        vlc::threads::mutex_locker lock( m_lock );
 
         switch ( m_state )
         {
@@ -788,7 +780,7 @@ bool intf_sys_t::processReceiverMessage( const castchannel::CastMessage& msg )
         json_value reason = (*p_data)["reason"];
         msg_Err( m_module, "Failed to start the MediaPlayer: %s",
                 (const char *)reason);
-        vlc_mutex_locker locker(&m_lock);
+        vlc::threads::mutex_locker lock( m_lock );
         m_appTransportId = "";
         m_mediaSessionId = 0;
         setState( Dead );
@@ -810,7 +802,7 @@ void intf_sys_t::processMediaMessage( const castchannel::CastMessage& msg )
     std::string type((*p_data)["type"]);
     int64_t requestId = (json_int_t) (*p_data)["requestId"];
 
-    vlc_mutex_locker locker( &m_lock );
+    vlc::threads::mutex_locker lock( m_lock );
 
     if ((m_last_request_id != 0 && requestId != m_last_request_id))
     {
@@ -959,7 +951,7 @@ void intf_sys_t::processConnectionMessage( const castchannel::CastMessage& msg )
     {
         // Close message indicates an application is being closed, not the connection.
         // From this point on, we need to relaunch the media receiver app
-        vlc_mutex_locker locker(&m_lock);
+        vlc::threads::mutex_locker lock( m_lock );
         m_appTransportId = "";
         m_mediaSessionId = 0;
         setState( Connected );
@@ -998,14 +990,14 @@ bool intf_sys_t::handleMessages()
                 return true;
             // An error occured, we give up
             msg_Err( m_module, "The connection to the Chromecast died (receiving).");
-            vlc_mutex_locker locker(&m_lock);
+            vlc::threads::mutex_locker lock( m_lock );
             setState( Dead );
             return false;
         }
         else if ( b_timeout == true )
         {
             // If no commands were queued to be sent, we timed out. Let's ping the chromecast
-            vlc_mutex_locker locker(&m_lock);
+            vlc::threads::mutex_locker lock( m_lock );
             if ( m_pingRetriesLeft == 0 )
             {
                 m_state = Dead;
@@ -1025,7 +1017,7 @@ bool intf_sys_t::handleMessages()
             if ( i_payloadSize > PACKET_MAX_LEN - PACKET_HEADER_LEN )
             {
                 msg_Err( m_module, "Payload size is too long: dropping connection" );
-                vlc_mutex_locker locker(&m_lock);
+                vlc::threads::mutex_locker lock( m_lock );
                 m_state = Dead;
                 return false;
             }
@@ -1057,7 +1049,7 @@ void intf_sys_t::doStop()
 
 void intf_sys_t::requestPlayerStop()
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker lock( m_lock );
 
     std::queue<QueueableMessages> empty;
     std::swap(m_msgQueue, empty);
@@ -1077,7 +1069,7 @@ void intf_sys_t::requestPlayerStop()
 
 States intf_sys_t::state() const
 {
-    vlc_mutex_locker locker( &m_lock );
+    vlc::threads::mutex_locker lock( m_lock );
     return m_state;
 }
 
@@ -1097,7 +1089,7 @@ std::string intf_sys_t::timeVLCToCC(vlc_tick_t time)
 void intf_sys_t::setOnInputEventCb(on_input_event_itf on_input_event,
                                    void *on_input_event_data)
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker lock( m_lock );
     m_on_input_event = on_input_event;
     m_on_input_event_data = on_input_event_data;
 }
@@ -1106,7 +1098,7 @@ void intf_sys_t::setDemuxEnabled(bool enabled,
                                  on_paused_changed_itf on_paused_changed,
                                  void *on_paused_changed_data)
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker lock( m_lock );
     m_on_paused_changed = on_paused_changed;
     m_on_paused_changed_data = on_paused_changed_data;
 
@@ -1119,7 +1111,7 @@ void intf_sys_t::setDemuxEnabled(bool enabled,
 
 void intf_sys_t::setPauseState(bool paused, vlc_tick_t delay)
 {
-    vlc_mutex_locker locker( &m_lock );
+    vlc::threads::mutex_locker lock( m_lock );
     if ( m_mediaSessionId == 0 || paused == m_paused || !m_communication )
         return;
 
@@ -1138,7 +1130,7 @@ void intf_sys_t::setPauseState(bool paused, vlc_tick_t delay)
 
 vlc_tick_t intf_sys_t::getPauseDelay()
 {
-    vlc_mutex_locker locker( &m_lock );
+    vlc::threads::mutex_locker lock( m_lock );
     return m_pause_delay;
 }
 
@@ -1164,7 +1156,7 @@ bool intf_sys_t::isFinishedPlaying()
 
 void intf_sys_t::setMeta(vlc_meta_t *p_meta)
 {
-    vlc_mutex_locker locker(&m_lock);
+    vlc::threads::mutex_locker lock( m_lock );
     if (m_meta != NULL)
         vlc_meta_Delete(m_meta);
     m_meta = p_meta;
@@ -1172,7 +1164,7 @@ void intf_sys_t::setMeta(vlc_meta_t *p_meta)
 
 vlc_tick_t intf_sys_t::getPlaybackTimestamp()
 {
-    vlc_mutex_locker locker( &m_lock );
+    vlc::threads::mutex_locker lock( m_lock );
     switch( m_state )
     {
         case Buffering:
@@ -1225,8 +1217,8 @@ void intf_sys_t::setState( States state )
             default:
                 break;
         }
-        vlc_cond_signal( &m_stateChangedCond );
-        vlc_cond_signal( &m_pace_cond );
+        m_stateChangedCond.signal();
+        m_pace_cond.signal();
     }
 }
 
-- 
2.18.0



More information about the vlc-devel mailing list