[vlc-commits] chromecast: Connect to the chromecast ASAP

Hugo Beauzée-Luyssen git at videolan.org
Tue Feb 21 14:00:55 CET 2017


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Fri Feb 17 15:46:25 2017 +0100| [daa943ba42dedb96e0a259b04575570750d083e9] | committer: Hugo Beauzée-Luyssen

chromecast: Connect to the chromecast ASAP

So that we don't even bother with loading the module if we can't do
anything with it.
This also allows us to drop some member variables, since we don't need
to remember the device IP/port from the intf_sys_t class anymore

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=daa943ba42dedb96e0a259b04575570750d083e9
---

 modules/stream_out/chromecast/cast.cpp               | 14 +++++++++++---
 modules/stream_out/chromecast/chromecast.h           |  5 +----
 .../chromecast/chromecast_communication.cpp          | 20 +++++---------------
 modules/stream_out/chromecast/chromecast_ctrl.cpp    | 17 +++--------------
 4 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/modules/stream_out/chromecast/cast.cpp b/modules/stream_out/chromecast/cast.cpp
index 6ea9eac..fef1bc4 100644
--- a/modules/stream_out/chromecast/cast.cpp
+++ b/modules/stream_out/chromecast/cast.cpp
@@ -445,12 +445,20 @@ static int Open(vlc_object_t *p_this)
     i_device_port = var_InheritInteger(p_stream, SOUT_CFG_PREFIX "port");
     i_local_server_port = var_InheritInteger(p_stream, SOUT_CFG_PREFIX "http-port");
 
-    p_intf = new(std::nothrow) intf_sys_t( p_this, i_local_server_port, psz_ip, i_device_port, p_interrupt );
-    if ( p_intf == NULL)
+    try
+    {
+        p_intf = new intf_sys_t( p_this, i_local_server_port, psz_ip, i_device_port, p_interrupt );
+    }
+    catch (const std::runtime_error& err )
+    {
+        msg_Err( p_this, "cannot load the Chromecast controler (%s)", err.what() );
+        goto error;
+    }
+    catch (const std::bad_alloc& )
     {
-        msg_Err( p_this, "cannot load the Chromecast controler" );
         goto error;
     }
+
     p_interrupt = NULL;
 
     psz_mux = var_GetNonEmptyString(p_stream, SOUT_CFG_PREFIX "mux");
diff --git a/modules/stream_out/chromecast/chromecast.h b/modules/stream_out/chromecast/chromecast.h
index 03798a3..4e918c8 100644
--- a/modules/stream_out/chromecast/chromecast.h
+++ b/modules/stream_out/chromecast/chromecast.h
@@ -91,8 +91,7 @@ enum receiver_state {
 class ChromecastCommunication
 {
 public:
-    ChromecastCommunication( vlc_object_t* m_module );
-    bool connect( const char* targetIP, unsigned int devicePort );
+    ChromecastCommunication( vlc_object_t* m_module, const char* targetIP, unsigned int devicePort );
     ~ChromecastCommunication();
     /**
      * @brief disconnect close the connection with the chromecast
@@ -162,8 +161,6 @@ struct intf_sys_t
 private:
     vlc_object_t  * const p_module;
     const int      i_port;
-    const int      i_target_port;
-    std::string    targetIP;
     std::string    mime;
 
     std::string appTransportId;
diff --git a/modules/stream_out/chromecast/chromecast_communication.cpp b/modules/stream_out/chromecast/chromecast_communication.cpp
index 729150a..daa41da 100644
--- a/modules/stream_out/chromecast/chromecast_communication.cpp
+++ b/modules/stream_out/chromecast/chromecast_communication.cpp
@@ -36,7 +36,7 @@
 #define PONG_WAIT_TIME 500
 #define PONG_WAIT_RETRIES 2
 
-ChromecastCommunication::ChromecastCommunication( vlc_object_t* p_module )
+ChromecastCommunication::ChromecastCommunication( vlc_object_t* p_module, const char* targetIP, unsigned int devicePort )
     : m_module( p_module )
     , m_sock_fd( -1 )
     , m_creds( NULL )
@@ -44,42 +44,32 @@ ChromecastCommunication::ChromecastCommunication( vlc_object_t* p_module )
     , m_receiver_requestId( 0 )
     , m_requestId( 0 )
 {
-}
-
-bool ChromecastCommunication::connect( const char* targetIP, unsigned int devicePort )
-{
     if (devicePort == 0)
         devicePort = CHROMECAST_CONTROL_PORT;
     m_sock_fd = net_ConnectTCP( m_module, targetIP, devicePort);
     if (m_sock_fd < 0)
-        return false;
+        throw std::runtime_error( "Failed to connect to the chromecast" );
 
     char psz_localIP[NI_MAXNUMERICHOST];
     if ( net_GetSockAddress( m_sock_fd, psz_localIP, NULL ) )
-    {
-        msg_Err( m_module, "Cannot get local IP address" );
-        return false;
-    }
+        throw std::runtime_error( "Cannot get local IP address" );
     m_serverIp = psz_localIP;
 
     m_creds = vlc_tls_ClientCreate( m_module->obj.parent );
     if (m_creds == NULL)
     {
-        msg_Err( m_module, "Failed to create TLS client" );
         net_Close(m_sock_fd);
-        return false;
+        throw std::runtime_error( "Failed to create TLS client" );
     }
 
     m_tls = vlc_tls_ClientSessionCreateFD( m_creds, m_sock_fd, targetIP, "tcps", NULL, NULL );
 
     if (m_tls == NULL)
     {
-        msg_Err( m_module, "Failed to create client session" );
         net_Close(m_sock_fd);
         vlc_tls_Delete(m_creds);
-        return false;
+        throw std::runtime_error( "Failed to create client session" );
     }
-    return true;
 }
 
 ChromecastCommunication::~ChromecastCommunication()
diff --git a/modules/stream_out/chromecast/chromecast_ctrl.cpp b/modules/stream_out/chromecast/chromecast_ctrl.cpp
index 4b17c58..93322d6 100644
--- a/modules/stream_out/chromecast/chromecast_ctrl.cpp
+++ b/modules/stream_out/chromecast/chromecast_ctrl.cpp
@@ -49,10 +49,8 @@ static const mtime_t SEEK_FORWARD_OFFSET = 1000000;
 intf_sys_t::intf_sys_t(vlc_object_t * const p_this, int port, std::string device_addr, int device_port, vlc_interrupt_t *p_interrupt)
  : p_module(p_this)
  , i_port(port)
- , i_target_port(device_port)
- , targetIP(device_addr)
  , receiverState(RECEIVER_IDLE)
- , m_communication( p_this )
+ , m_communication( p_this, device_addr.c_str(), device_port )
  , requested_stop(false)
  , requested_seek(false)
  , conn_status(CHROMECAST_DISCONNECTED)
@@ -132,8 +130,7 @@ intf_sys_t::~intf_sys_t()
 void intf_sys_t::setHasInput( bool b_has_input, const std::string mime_type )
 {
     vlc_mutex_locker locker(&lock);
-    msg_Dbg( p_module, "setHasInput %s device:%s session:%s",b_has_input ? "true":"false",
-             targetIP.c_str(), mediaSessionId.c_str() );
+    msg_Dbg( p_module, "setHasInput %s session:%s",b_has_input ? "true":"false", mediaSessionId.c_str() );
 
     this->has_input = b_has_input;
     this->mime = mime_type;
@@ -489,17 +486,9 @@ void intf_sys_t::processMessage(const castchannel::CastMessage &msg)
 void* intf_sys_t::ChromecastThread(void* p_data)
 {
     intf_sys_t *p_sys = reinterpret_cast<intf_sys_t*>(p_data);
-    p_sys->setConnectionStatus( CHROMECAST_DISCONNECTED );
-
-    if ( p_sys->m_communication.connect( p_sys->targetIP.c_str(), p_sys->i_target_port ) == false )
-    {
-        msg_Err( p_sys->p_module, "Could not connect the Chromecast" );
-        vlc_mutex_locker locker(&p_sys->lock);
-        p_sys->setConnectionStatus(CHROMECAST_CONNECTION_DEAD);
-        return NULL;
-    }
 
     vlc_interrupt_set( p_sys->p_ctl_thread_interrupt );
+    vlc_interrupt_set( p_sys->p_ctl_thread_interrupt );
 
     vlc_mutex_lock(&p_sys->lock);
     p_sys->setConnectionStatus(CHROMECAST_TLS_CONNECTED);



More information about the vlc-commits mailing list