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
|
/*
* Argyll Color Correction System
* ChromCast test harness
*
* Author: Graeme W. Gill
* Date: 28/8/2014
*
* Copyright 2014 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License2.txt file for licencing details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
#include "copyright.h"
#include "aconfig.h"
#ifndef SALONEINSTLIB
#include "numlib.h"
#else
#include "numsup.h"
#endif
#include "ccmdns.h"
#include "ccpacket.h"
#include "ccmes.h"
#include "yajl.h"
/* Check if JSON is invalid */
/* Exits if invalid */
static void check_json(char *mesbuf) {
yajl_val node;
char errbuf[1024];
if ((node = yajl_tree_parse(mesbuf, errbuf, sizeof(errbuf))) == NULL) {
printf("yajl_tree_parse of send message failed with '%s'\n",errbuf);
printf("JSON = '%s'\n",mesbuf);
exit(1);
}
yajl_tree_free(node);
}
/* Return nz on error */
static int get_a_reply(ccmessv *mes, ORD8 **pdata) {
ccmessv_err merr;
char *source_id;
char *destination_id;
char *namespace;
int binary; /* Flag */
ORD8 *data; /* String or binary */
ORD32 bin_len; /* Binary data length */
if ((merr = mes->receive(mes, &source_id, &destination_id, &namespace,
&binary, &data, &bin_len)) != ccmessv_OK) {
printf("mes->receive failed with '%s'\n",ccmessv_emes(merr));
return -1;
} else {
printf("Got message:\n");
printf(" source_id = '%s'\n",source_id);
printf(" destination_id = '%s'\n",destination_id);
printf(" namespace = '%s'\n",namespace);
printf(" binary = %d\n",binary);
if (binary) {
printf(" payload =\n");
adump_bytes(g_log, " ", data, 0, bin_len);
} else {
printf(" payload = '%s'\n",data);
}
if (pdata != NULL)
*pdata = data;
else
free(data);
}
return 0;
}
/* Get replies until we get one with the matching Id, then return */
static int get_a_reply_id(ccmessv *mes, int tid, ORD8 **pdata) {
ORD8 *data;
int id, rv;
yajl_val node, v;
char errbuf[1024];
//printf("~~~ looking for id %d\n",tid);
for (;;) {
int id = -1;
if ((rv = get_a_reply(mes, &data)) != 0)
return rv;
// printf("\nGot JSON data to parse: '%s'\n",data);
if ((node = yajl_tree_parse(data, errbuf, sizeof(errbuf))) == NULL)
error("yajl_tree_parse failed with '%s'",errbuf);
if ((v = yajl_tree_get_first(node, "requestId", yajl_t_number)) != NULL) {
printf("~~~~ Got id %d\n",id);
id = YAJL_GET_INTEGER(v);
}
yajl_tree_free(node);
if (id == tid) {
//printf("~~~ got our message\n");
break;
}
free(data);
//printf("~~~ round we go again\n");
}
if (pdata != NULL)
*pdata = data;
else
free(data);
return 0;
}
/* Do the authentication sequence */
static void authenticate(ccmessv *mes) {
/* (Not needed) */
}
int
main(
int argc,
char *argv[]
) {
ccast_id **ids;
int i;
ccpacket *pk;
ccpacket_err perr;
ccmessv *mes;
ccmessv_err merr;
char *sessionId = NULL;
char *transportId = NULL;
char mesbuf[1024];
if ((ids = get_ccids()) == NULL) {
error("get_ccids() failed");
}
if (ids[0] == NULL) {
error("Found no ChromCasts");
}
for (i = 0; ids[i] != NULL; i++) {
printf("Entry %d:\n",i);
printf(" Name: %s\n",ids[i]->name);
printf(" IP: %s\n",ids[i]->ip);
}
if ((pk = new_ccpacket()) == NULL) {
error("new_ccpacket() failed");
}
if ((perr = pk->connect(pk, ids[0]->ip, 8009)) != ccpacket_OK) {
error("ccpacket connect failed with '%s",ccpacket_emes(perr));
}
printf("Got TLS connection to '%s\n'",ids[0]->name);
if ((mes = new_ccmessv(pk)) == NULL) {
error("new_ccmessv() failed");
}
/* Attempt a connection */
if ((merr = mes->send(mes, "sender-0", "receiver-0",
"urn:x-cast:com.google.cast.tp.connection", 0,
"{ \"type\": \"CONNECT\" }", 0)) != ccmessv_OK) {
error("mes->send CONNECT failed with '%s'",ccmessv_emes(merr));
}
/* Send a ping */
if ((merr = mes->send(mes, "sender-0", "receiver-0",
"urn:x-cast:com.google.cast.tp.heartbeat", 0,
"{ \"type\": \"PING\" }", 0)) != ccmessv_OK) {
error("mes->send PING failed with '%s'",ccmessv_emes(merr));
}
/* Wait for a PONG */
get_a_reply(mes, NULL);
authenticate(mes);
/* Launch the default application */
/* (Presumably we would use the com.google.cast.receiver channel */
/* for monitoring and controlling the reciever) */
if ((merr = mes->send(mes, "sender-0", "receiver-0",
"urn:x-cast:com.google.cast.receiver", 0,
"{ \"requestId\": 1, \"type\": \"LAUNCH\", \"appId\": \"CC1AD845\" }", 0)) != ccmessv_OK) {
error("mes->send LAUNCH failed with '%s'",ccmessv_emes(merr));
}
/* Receive the RECEIVER_STATUS status messages until it is ready to cast */
/* and get the sessionId and transportId */
/* We get periodic notification messages (requestId=0) as well as */
/* a response messages (requestId=1) */
for (;sessionId == NULL || transportId == NULL;) {
ORD8 *data;
yajl_val node, v;
char errbuf[1024];
get_a_reply(mes, &data);
// printf("\nGot JSON data to parse: '%s'\n",data);
if ((node = yajl_tree_parse(data, errbuf, sizeof(errbuf))) == NULL)
error("yajl_tree_parse failed with '%s'",errbuf);
if ((v = yajl_tree_get_first(node, "sessionId", yajl_t_string)) != NULL) {
sessionId = strdup(YAJL_GET_STRING(v));
// printf("~1 got sessionId = '%s'\n",sessionId);
}
if ((v = yajl_tree_get_first(node, "transportId", yajl_t_string)) != NULL) {
transportId = strdup(YAJL_GET_STRING(v));
// printf("~1 got transportId = '%s'\n",transportId);
}
free(data);
yajl_tree_free(node);
}
printf("\ngot sessionId = '%s', transportId = '%s'\n",sessionId, transportId);
printf("\nAbout to send URL\n");
/* Connect up to the reciever media channels */
if ((merr = mes->send(mes, "med_send", transportId,
"urn:x-cast:com.google.cast.tp.connection", 0,
"{ \"type\": \"CONNECT\" }", 0)) != ccmessv_OK) {
error("mes->send CONNECT failed with '%s'",ccmessv_emes(merr));
}
// "urn:x-cast:com.google.cast.player.message"
// "urn:x-cast:com.google.cast.media"}
sprintf(mesbuf, "{ \"requestId\": 2, \"type\": \"LOAD\", \"media\": "
"{ \"contentId\": \"http://www.argyllcms.com/ArgyllCMSLogo.gif\",\"streamType\": \"LIVE\","
"\"contentType\": \"image/gif\" } }");
// "\"contentType\": \"image/gif\", \"duration\" : 0.1 }, \"autplay\": \"true\" }");
check_json(mesbuf);
/* Send the LOAD command */
if ((merr = mes->send(mes, "med_send", transportId,
"urn:x-cast:com.google.cast.media", 0,
mesbuf, 0
)) != ccmessv_OK) {
error("mes->send LOAD failed with '%s'",ccmessv_emes(merr));
}
if (get_a_reply_id(mes, 2, NULL) != 0)
exit(1);
msec_sleep(2000);
#ifdef NEVER
/* Check the media status */
if ((merr = mes->send(mes, "med_send", transportId,
"urn:x-cast:com.google.cast.media", 0,
"{ \"requestId\": 3, \"type\": \"GET_STATUS\" }", 0)) != ccmessv_OK) {
error("mes->send GET_STATUS failed with '%s'",ccmessv_emes(merr));
}
if (get_a_reply_id(mes, 3, NULL) != 0)
exit(1);
#endif
sprintf(mesbuf, "{ \"requestId\": 4, \"type\": \"LOAD\", \"media\": { \"contentId\": \"http://www.argyllcms.com/testing.png\",\"streamType\": \"LIVE\",\"contentType\": \"image/png\" } }");
// sprintf(mesbuf, "{ \"requestId\": 4, \"type\": \"LOAD\", \"media\": { \"contentId\": \"http://www.argyllcms.com/testing.tif\",\"streamType\": \"LIVE\",\"contentType\": \"image/tiff\" } }");
// sprintf(mesbuf, "{ \"requestId\": 4, \"type\": \"LOAD\", \"media\": { \"contentId\": \"http://www.argyllcms.com/doc/sl.jpg\",\"streamType\": \"LIVE\",\"contentType\": \"image/jpeg\" } }");
check_json(mesbuf);
/* Send the LOAD command */
if ((merr = mes->send(mes, "med_send", transportId,
"urn:x-cast:com.google.cast.media", 0,
mesbuf, 0
)) != ccmessv_OK) {
error("mes->send LOAD failed with '%s'",ccmessv_emes(merr));
}
if (get_a_reply_id(mes, 4, NULL) != 0)
exit(1);
msec_sleep(2000);
#ifdef NEVER
/* Check the media status */
if ((merr = mes->send(mes, "med_send", transportId,
"urn:x-cast:com.google.cast.media", 0,
"{ \"requestId\": 5, \"type\": \"GET_STATUS\" }", 0)) != ccmessv_OK) {
error("mes->send GET_STATUS failed with '%s'",ccmessv_emes(merr));
}
if (get_a_reply_id(mes, 5, NULL) != 0)
exit(1);
#endif
#ifdef NEVER
/* Try and send it an image URL */
if ((merr = mes->send(mes, "med_send", transportId,
"urn:x-cast:com.google.cast.media", 0,
// "{ \"imageUrl\": \"http://www.argyllcms.com/ArgyllCMSLogo.gif\", \"requestId\": 6 }", 0)) != ccmessv_OK) {
"{ \"url\": \"http://www.argyllcms.com/ArgyllCMSLogo.gif\", \"requestId\": 2 }", 0)) != ccmessv_OK) {
error("mes->send imageUrl failed with '%s'",ccmessv_emes(merr));
}
#endif
/* Wait for any reply */
for (;;) {
if (get_a_reply(mes, NULL) != 0)
break;
}
// ~~999
mes->del(mes);
pk->del(pk);
printf("Disconnected from '%s'\n",ids[0]->name);
free_ccids(ids);
return 0;
}
|