-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchromecast_communication.cpp
More file actions
473 lines (399 loc) · 15.2 KB
/
Copy pathchromecast_communication.cpp
File metadata and controls
473 lines (399 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*****************************************************************************
* chromecast_communication.cpp: Handle chromecast protocol messages
*****************************************************************************
* Copyright © 2014-2017 VideoLAN
*
* Authors: Adrien Maglo <magsoft@videolan.org>
* Jean-Baptiste Kempf <jb@videolan.org>
* Steve Lhomme <robux4@videolabs.io>
* Hugo Beauzée-Luyssen <hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "chromecast.h"
#ifdef HAVE_POLL
# include <poll.h>
#endif
#include <iomanip>
ChromecastCommunication::ChromecastCommunication( vlc_object_t* p_module,
std::string serverPath, unsigned int serverPort, const char* targetIP, unsigned int devicePort )
: m_module( p_module )
, m_creds( NULL )
, m_tls( NULL )
, m_receiver_requestId( 1 )
, m_requestId( 1 )
, m_serverPath( serverPath )
, m_serverPort( serverPort )
{
if (devicePort == 0)
devicePort = CHROMECAST_CONTROL_PORT;
m_creds = vlc_tls_ClientCreate( m_module->obj.parent );
if (m_creds == NULL)
throw std::runtime_error( "Failed to create TLS client" );
m_tls = vlc_tls_SocketOpenTLS( m_creds, targetIP, devicePort, "tcps",
NULL, NULL );
if (m_tls == NULL)
{
vlc_tls_Delete(m_creds);
throw std::runtime_error( "Failed to create client session" );
}
char psz_localIP[NI_MAXNUMERICHOST];
if (net_GetSockAddress( vlc_tls_GetFD(m_tls), psz_localIP, NULL ))
throw std::runtime_error( "Cannot get local IP address" );
char* host_proxy;
host_proxy = std::getenv ("HOSTIP");
m_serverIp = psz_localIP;
if (host_proxy!=NULL)
m_serverIp= host_proxy;
}
ChromecastCommunication::~ChromecastCommunication()
{
disconnect();
}
void ChromecastCommunication::disconnect()
{
if ( m_tls != NULL )
{
vlc_tls_Close(m_tls);
vlc_tls_Delete(m_creds);
m_tls = NULL;
}
}
/**
* @brief Build a CastMessage to send to the Chromecast
* @param namespace_ the message namespace
* @param payloadType the payload type (CastMessage_PayloadType_STRING or
* CastMessage_PayloadType_BINARY
* @param payload the payload
* @param destinationId the destination idenifier
* @return the generated CastMessage
*/
int ChromecastCommunication::buildMessage(const std::string & namespace_,
const std::string & payload,
const std::string & destinationId,
castchannel::CastMessage_PayloadType payloadType)
{
castchannel::CastMessage msg;
msg.set_protocol_version(castchannel::CastMessage_ProtocolVersion_CASTV2_1_0);
msg.set_namespace_(namespace_);
msg.set_payload_type(payloadType);
msg.set_source_id("sender-vlc");
msg.set_destination_id(destinationId);
if (payloadType == castchannel::CastMessage_PayloadType_STRING)
msg.set_payload_utf8(payload);
else // CastMessage_PayloadType_BINARY
msg.set_payload_binary(payload);
return sendMessage(msg);
}
/**
* @brief Receive a data packet from the Chromecast
* @param p_data the buffer in which to store the data
* @param i_size the size of the buffer
* @param i_timeout maximum time to wait for a packet, in millisecond
* @param pb_timeout Output parameter that will contain true if no packet was received due to a timeout
* @return the number of bytes received of -1 on error
*/
ssize_t ChromecastCommunication::receive( uint8_t *p_data, size_t i_size, int i_timeout, bool *pb_timeout )
{
ssize_t i_received = 0;
struct pollfd ufd[1];
ufd[0].fd = vlc_tls_GetFD( m_tls );
ufd[0].events = POLLIN;
struct iovec iov;
iov.iov_base = p_data;
iov.iov_len = i_size;
/* The Chromecast normally sends a PING command every 5 seconds or so.
* If we do not receive one after 6 seconds, we send a PING.
* If after this PING, we do not receive a PONG, then we consider the
* connection as dead. */
do
{
ssize_t i_ret = m_tls->ops->readv( m_tls, &iov, 1 );
if ( i_ret < 0 )
{
#ifdef _WIN32
if ( WSAGetLastError() != WSAEWOULDBLOCK )
#else
if ( errno != EAGAIN )
#endif
{
return -1;
}
ssize_t val = vlc_poll_i11e(ufd, 1, i_timeout);
if ( val < 0 )
return -1;
else if ( val == 0 )
{
*pb_timeout = true;
return i_received;
}
assert( ufd[0].revents & POLLIN );
continue;
}
else if ( i_ret == 0 )
return -1;
assert( i_size >= (size_t)i_ret );
i_size -= i_ret;
i_received += i_ret;
iov.iov_base = (uint8_t*)iov.iov_base + i_ret;
iov.iov_len = i_size;
} while ( i_size > 0 );
return i_received;
}
/*****************************************************************************
* Message preparation
*****************************************************************************/
unsigned ChromecastCommunication::getNextReceiverRequestId()
{
unsigned id = m_receiver_requestId++;
return likely(id != 0) ? id : m_receiver_requestId++;
}
unsigned ChromecastCommunication::getNextRequestId()
{
unsigned id = m_requestId++;
return likely(id != 0) ? id : m_requestId++;
}
unsigned ChromecastCommunication::msgAuth()
{
castchannel::DeviceAuthMessage authMessage;
authMessage.mutable_challenge();
return buildMessage(NAMESPACE_DEVICEAUTH, authMessage.SerializeAsString(),
DEFAULT_CHOMECAST_RECEIVER, castchannel::CastMessage_PayloadType_BINARY)
== VLC_SUCCESS ? 1 : kInvalidId;
}
unsigned ChromecastCommunication::msgPing()
{
std::string s("{\"type\":\"PING\"}");
return buildMessage( NAMESPACE_HEARTBEAT, s, DEFAULT_CHOMECAST_RECEIVER )
== VLC_SUCCESS ? 1 : kInvalidId;
}
unsigned ChromecastCommunication::msgPong()
{
std::string s("{\"type\":\"PONG\"}");
return buildMessage( NAMESPACE_HEARTBEAT, s, DEFAULT_CHOMECAST_RECEIVER )
== VLC_SUCCESS ? 1 : kInvalidId;
}
unsigned ChromecastCommunication::msgConnect( const std::string& destinationId )
{
std::string s("{\"type\":\"CONNECT\"}");
return buildMessage( NAMESPACE_CONNECTION, s, destinationId )
== VLC_SUCCESS ? 1 : kInvalidId;
}
unsigned ChromecastCommunication::msgReceiverClose( const std::string& destinationId )
{
std::string s("{\"type\":\"CLOSE\"}");
return buildMessage( NAMESPACE_CONNECTION, s, destinationId )
== VLC_SUCCESS ? 1 : kInvalidId;
}
unsigned ChromecastCommunication::msgReceiverGetStatus()
{
unsigned id = getNextReceiverRequestId();
std::stringstream ss;
ss << "{\"type\":\"GET_STATUS\","
<< "\"requestId\":" << id << "}";
return buildMessage( NAMESPACE_RECEIVER, ss.str(), DEFAULT_CHOMECAST_RECEIVER )
== VLC_SUCCESS ? id : kInvalidId;
}
unsigned ChromecastCommunication::msgReceiverLaunchApp()
{
unsigned id = getNextReceiverRequestId();
std::stringstream ss;
ss << "{\"type\":\"LAUNCH\","
<< "\"appId\":\"" << APP_ID << "\","
<< "\"requestId\":" << id << "}";
return buildMessage( NAMESPACE_RECEIVER, ss.str(), DEFAULT_CHOMECAST_RECEIVER )
== VLC_SUCCESS ? id : kInvalidId;
}
unsigned ChromecastCommunication::msgPlayerGetStatus( const std::string& destinationId )
{
unsigned id = getNextRequestId();
std::stringstream ss;
ss << "{\"type\":\"GET_STATUS\","
<< "\"requestId\":" << id
<< "}";
return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : kInvalidId;
}
static std::string escape_json(const std::string &s)
{
/* Control characters ('\x00' to '\x1f'), '"' and '\" must be escaped */
std::ostringstream o;
for (std::string::const_iterator c = s.begin(); c != s.end(); c++)
{
if (*c == '"' || *c == '\\' || ('\x00' <= *c && *c <= '\x1f'))
o << "\\u"
<< std::hex << std::setw(4) << std::setfill('0') << (int)*c;
else
o << *c;
}
return o.str();
}
static std::string meta_get_escaped(const vlc_meta_t *p_meta, vlc_meta_type_t type)
{
const char *psz = vlc_meta_Get(p_meta, type);
if (!psz)
return std::string();
return escape_json(std::string(psz));
}
std::string ChromecastCommunication::GetMedia( const std::string& mime,
const vlc_meta_t *p_meta )
{
std::stringstream ss;
bool b_music = strncmp(mime.c_str(), "audio", strlen("audio")) == 0;
std::string title;
std::string artwork;
std::string artist;
std::string album;
std::string albumartist;
std::string tracknumber;
std::string discnumber;
if( p_meta )
{
title = meta_get_escaped( p_meta, vlc_meta_Title );
artwork = meta_get_escaped( p_meta, vlc_meta_ArtworkURL );
if( b_music && !title.empty() )
{
artist = meta_get_escaped( p_meta, vlc_meta_Artist );
album = meta_get_escaped( p_meta, vlc_meta_Album );
albumartist = meta_get_escaped( p_meta, vlc_meta_AlbumArtist );
tracknumber = meta_get_escaped( p_meta, vlc_meta_TrackNumber );
discnumber = meta_get_escaped( p_meta, vlc_meta_DiscNumber );
}
if( title.empty() )
{
title = meta_get_escaped( p_meta, vlc_meta_NowPlaying );
if( title.empty() )
title = meta_get_escaped( p_meta, vlc_meta_ESNowPlaying );
}
if ( !title.empty() )
{
ss << "\"metadata\":{"
<< " \"metadataType\":" << ( b_music ? "3" : "0" )
<< ",\"title\":\"" << title << "\"";
if( b_music )
{
if( !artist.empty() )
ss << ",\"artist\":\"" << artist << "\"";
if( album.empty() )
ss << ",\"album\":\"" << album << "\"";
if( albumartist.empty() )
ss << ",\"albumArtist\":\"" << albumartist << "\"";
if( tracknumber.empty() )
ss << ",\"trackNumber\":\"" << tracknumber << "\"";
if( discnumber.empty() )
ss << ",\"discNumber\":\"" << discnumber << "\"";
}
if ( !artwork.empty() && !strncmp( artwork.c_str(), "http", 4 ) )
ss << ",\"images\":[{\"url\":\"" << artwork << "\"}]";
ss << "},";
}
}
std::stringstream chromecast_url;
chromecast_url << "http://" << m_serverIp << ":" << m_serverPort << m_serverPath;
msg_Dbg( m_module, "s_chromecast_url_cambio: %s", chromecast_url.str().c_str());
ss << "\"contentId\":\"" << chromecast_url.str() << "\""
<< ",\"streamType\":\"LIVE\""
<< ",\"contentType\":\"" << mime << "\"";
return ss.str();
}
unsigned ChromecastCommunication::msgPlayerLoad( const std::string& destinationId,
const std::string& mime, const vlc_meta_t *p_meta )
{
unsigned id = getNextRequestId();
std::stringstream ss;
ss << "{\"type\":\"LOAD\","
<< "\"media\":{" << GetMedia( mime, p_meta ) << "},"
<< "\"autoplay\":\"false\","
<< "\"requestId\":" << id
<< "}";
return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : kInvalidId;
}
unsigned ChromecastCommunication::msgPlayerPlay( const std::string& destinationId, int64_t mediaSessionId )
{
assert(mediaSessionId != 0);
unsigned id = getNextRequestId();
std::stringstream ss;
ss << "{\"type\":\"PLAY\","
<< "\"mediaSessionId\":" << mediaSessionId << ","
<< "\"requestId\":" << id
<< "}";
return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : kInvalidId;
}
unsigned ChromecastCommunication::msgPlayerStop( const std::string& destinationId, int64_t mediaSessionId )
{
assert(mediaSessionId != 0);
unsigned id = getNextRequestId();
std::stringstream ss;
ss << "{\"type\":\"STOP\","
<< "\"mediaSessionId\":" << mediaSessionId << ","
<< "\"requestId\":" << id
<< "}";
return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : kInvalidId;
}
unsigned ChromecastCommunication::msgPlayerPause( const std::string& destinationId, int64_t mediaSessionId )
{
assert(mediaSessionId != 0);
unsigned id = getNextRequestId();
std::stringstream ss;
ss << "{\"type\":\"PAUSE\","
<< "\"mediaSessionId\":" << mediaSessionId << ","
<< "\"requestId\":" << id
<< "}";
return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : kInvalidId;
}
unsigned ChromecastCommunication::msgPlayerSetVolume( const std::string& destinationId, int64_t mediaSessionId, float f_volume, bool b_mute )
{
assert(mediaSessionId != 0);
unsigned id = getNextRequestId();
if ( f_volume < 0.0 || f_volume > 1.0)
return VLC_EGENERIC;
std::stringstream ss;
ss << "{\"type\":\"SET_VOLUME\","
<< "\"volume\":{\"level\":" << f_volume << ",\"muted\":" << ( b_mute ? "true" : "false" ) << "},"
<< "\"mediaSessionId\":" << mediaSessionId << ","
<< "\"requestId\":" << id
<< "}";
return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : kInvalidId;
}
/**
* @brief Send a message to the Chromecast
* @param msg the CastMessage to send
* @return vlc error code
*/
int ChromecastCommunication::sendMessage( const castchannel::CastMessage &msg )
{
int i_size = msg.ByteSize();
uint8_t *p_data = new(std::nothrow) uint8_t[PACKET_HEADER_LEN + i_size];
if (p_data == NULL)
return VLC_ENOMEM;
#ifndef NDEBUG
msg_Dbg( m_module, "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);
int i_ret = vlc_tls_Write(m_tls, p_data, PACKET_HEADER_LEN + i_size);
delete[] p_data;
if (i_ret == PACKET_HEADER_LEN + i_size)
return VLC_SUCCESS;
msg_Warn( m_module, "failed to send message %s (%s)", msg.payload_utf8().c_str(), strerror( errno ) );
return VLC_EGENERIC;
}
int ChromecastCommunication::pushMediaPlayerMessage( const std::string& destinationId, const std::stringstream & payload )
{
assert(!destinationId.empty());
return buildMessage( NAMESPACE_MEDIA, payload.str(), destinationId );
}