Commit daa943ba authored by Hugo Beauzée-Luyssen's avatar 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
parent 5acdb8d5
......@@ -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");
......
......@@ -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;
......
......@@ -36,50 +36,40 @@
#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 )
, m_tls( NULL )
, 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()
......
......@@ -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,16 +486,8 @@ 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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment