Commit 7f575521 authored by Steve Lhomme's avatar Steve Lhomme Committed by Jean-Baptiste Kempf

chromecast_ctrl: send messages right away

The Chromecast protocol is not a client server, all requests don't get an
answer and we can receive unexpected broadcasts.
Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 5b526dae
......@@ -286,8 +286,6 @@ static void Close(vlc_object_t *p_this)
// ft
case CHROMECAST_AUTHENTICATED:
p_sys->p_intf->msgReceiverClose(DEFAULT_CHOMECAST_RECEIVER);
// Send the just added close messages.
p_sys->p_intf->sendMessages();
// ft
default:
break;
......@@ -328,7 +326,6 @@ static void* chromecastThread(void* p_data)
sout_stream_sys_t* p_sys = p_stream->p_sys;
p_sys->p_intf->msgAuth();
p_sys->p_intf->sendMessages();
vlc_restorecancel(canc);
while (1)
......
......@@ -34,8 +34,6 @@
#include <vlc_sout.h>
#include <vlc_tls.h>
#include <queue>
#include "cast_channel.pb.h"
#define PACKET_HEADER_LEN 4
......@@ -77,7 +75,6 @@ struct intf_sys_t
void msgReceiverClose(std::string destinationId);
void handleMessages();
int sendMessages();
connection_status getConnectionStatus() const
{
......@@ -108,12 +105,10 @@ struct intf_sys_t
void msgPlayerLoad();
std::queue<castchannel::CastMessage> messagesToSend;
void processMessage(const castchannel::CastMessage &msg);
private:
int sendMessage(castchannel::CastMessage &msg);
int sendMessage(const castchannel::CastMessage &msg);
void buildMessage(const std::string & namespace_,
const std::string & payload,
......
......@@ -88,7 +88,7 @@ void intf_sys_t::buildMessage(const std::string & namespace_,
else // CastMessage_PayloadType_BINARY
msg.set_payload_binary(payload);
messagesToSend.push(msg);
sendMessage(msg);
}
intf_sys_t::intf_sys_t(sout_stream_t * const p_this)
......@@ -529,46 +529,29 @@ void intf_sys_t::msgPlayerLoad()
/**
* @brief Send a message to the Chromecast
* @param msg the CastMessage to send
* @return the number of bytes sent or -1 on error
* @return vlc error code
*/
int intf_sys_t::sendMessage(castchannel::CastMessage &msg)
int intf_sys_t::sendMessage(const castchannel::CastMessage &msg)
{
uint32_t i_size = msg.ByteSize();
uint32_t i_sizeNetwork = hton32(i_size);
char *p_data = new(std::nothrow) char[PACKET_HEADER_LEN + i_size];
int i_size = msg.ByteSize();
uint8_t *p_data = new(std::nothrow) uint8_t[PACKET_HEADER_LEN + i_size];
if (p_data == NULL)
return -1;
return VLC_ENOMEM;
memcpy(p_data, &i_sizeNetwork, PACKET_HEADER_LEN);
msg.SerializeWithCachedSizesToArray((uint8_t *)(p_data + PACKET_HEADER_LEN));
#ifndef NDEBUG
msg_Dbg(p_stream, "sendMessage: %s->%s %s", msg.namespace_().c_str(), msg.destination_id().c_str(), msg.payload_utf8().c_str());
#endif
SetDWBE(p_data, i_size);
msg.SerializeWithCachedSizesToArray(p_data + PACKET_HEADER_LEN);
vlc_mutex_locker locker(&lock);
int i_ret = tls_Send(p_tls, p_data, PACKET_HEADER_LEN + i_size);
delete[] p_data;
if (i_ret == PACKET_HEADER_LEN + i_size)
return VLC_SUCCESS;
return i_ret;
}
/**
* @brief Send all the messages in the pending queue to the Chromecast
* @param msg the CastMessage to send
* @return the number of bytes sent or -1 on error
*/
int intf_sys_t::sendMessages()
{
int i_ret = 0;
while (!messagesToSend.empty())
{
unsigned i_retSend = sendMessage(messagesToSend.front());
if (i_retSend <= 0)
return i_retSend;
messagesToSend.pop();
i_ret += i_retSend;
}
return i_ret;
return VLC_EGENERIC;
}
void intf_sys_t::handleMessages()
......@@ -615,21 +598,5 @@ void intf_sys_t::handleMessages()
processMessage(msg);
}
// Send the answer messages if there is any.
if (!messagesToSend.empty())
{
i_ret = sendMessages();
#if defined(_WIN32)
if ((i_ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) || (i_ret == 0))
#else
if ((i_ret < 0 && errno != EAGAIN) || i_ret == 0)
#endif
{
msg_Err(p_stream, "The connection to the Chromecast died (sending).");
vlc_mutex_locker locker(&lock);
setConnectionStatus(CHROMECAST_CONNECTION_DEAD);
}
}
vlc_restorecancel(canc);
}
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