-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanswermethod.c
More file actions
564 lines (499 loc) · 14.8 KB
/
answermethod.c
File metadata and controls
564 lines (499 loc) · 14.8 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <signal.h>
#include <sys/wait.h>
#include "debug.h"
int debug_level = 1;
int debug_syslog = 0;
int debug_console = 1;
struct point {
int x;
int y;
};
int width = 640, height = 480, imrate = 25, bitrate =
200, qscale = 6, ok = 0, pid;
char *clip = "127.0.0.1";
int status = 1;
int pfderr[2], pfdout[2];
char bufferr[BUFSIZ + 1], buffout[BUFSIZ + 1];
void send_ok(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
DBusMessageIter args;
dbus_uint32_t serial = 0;
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &ok)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void start(DBusMessage * msg, DBusConnection * conn)
{
//debug(2, "%d", conn.generation);
char im_rate[16];
char b_rate[16];
char qscl[16];
char size[16];
char sclip[25];
char *parmList[] =
{ "/usr/bin/ffmpeg", "-f", "video4linux2", "-s", "", "-r", "", "-b",
"", "-qscale", "", "-i", "/dev/video0", "-f", "mjpeg", "", NULL
};
if (status == 0) {
debug(1, "%s", "already started \n");
send_ok(msg, conn);
return;
}
// create the pipe
if ((pipe(pfderr) == -1) || (pipe(pfdout) == -1)) {
panic("%s", "pipe failed\n");
return;
}
status = 0;
debug(1, "%s", "video started \n");
/* create the child */
if ((pid = fork()) < 0) {
panic("%s", "fork failed\n");
return;
}
if (pid == 0) {
/* child */
close(pfderr[0]); /* close the unused read side */
close(pfdout[0]);
dup2(pfderr[1], 2); /* connect the write side with stderr */
dup2(pfdout[1], 1); /* connect the write side with stdout */
close(pfderr[1]); /* close the write side */
close(pfdout[1]);
if (snprintf(size, sizeof(size), "%dx%d", width, height)
>= sizeof(size)) {
panic("error: unable to snprintf(): size=%zu", sizeof(size));
exit(EXIT_FAILURE);
}
parmList[4] = size;
if (snprintf(im_rate, sizeof(im_rate), "%d", imrate)
>= sizeof(im_rate)) {
panic("error: unable to snprintf(): size=%zu", sizeof(im_rate));
exit(EXIT_FAILURE);
}
parmList[6] = im_rate;
if (snprintf(b_rate,sizeof(b_rate), "%d", bitrate)
>= sizeof(b_rate)) {
panic("error: unable to snprintf(): size=%zu", sizeof(b_rate));
exit(EXIT_FAILURE);
}
parmList[8] = b_rate;
if (snprintf(qscl, sizeof(qscl), "%d", qscale)
>= sizeof(qscl)) {
panic("error: unable to snprintf(): size=%zu", sizeof(qscl));
exit(EXIT_FAILURE);
}
parmList[10] = qscl;
if(snprintf(sclip,sizeof(sclip),"udp:%s:1234",clip)
>= sizeof(sclip)) {
panic("error: unable to snprintf(): size=%zu", sizeof(sclip));
exit(EXIT_FAILURE);
}
parmList[15]=sclip;
execvp("ffmpeg", parmList);
panic("%s",
"Return not expected. Must be an execvp() error.\\n");
return;
} else {
/* parent */
send_ok(msg, conn);
return;
}
}
void stop(DBusMessage * msg, DBusConnection * conn)
{
if (status == 0) {
kill(pid, SIGKILL);
debug(1, "%s", "it's killed \n");
}
// create a reply from the message
send_ok(msg, conn);
status = 1;
}
void image_size_set(DBusMessage * msg, DBusConnection * conn)
{
DBusMessageIter args;
// read the arguments
if (!dbus_message_iter_init(msg, &args)) {
panic("%s", "Message has no arguments!\n");
} else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args))
panic("%s", "Argument is not integer!\n");
dbus_message_iter_get_basic(&args, &width);
if (!dbus_message_iter_next(&args)) {
panic("%s", "Message has too few arguments!\n");
} else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args)) {
panic("%s", "Argument is not integer!\n");
} else
dbus_message_iter_get_basic(&args, &height);
debug(1, "%d", width);
debug(1, "%d", height);
send_ok(msg, conn);
}
void image_size_get(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
DBusMessageIter args;
dbus_uint32_t serial = 0;
// create a reply from the message
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &width)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &height)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void image_rate_set(DBusMessage * msg, DBusConnection * conn)
{
DBusMessageIter args;
// read the arguments
if (!dbus_message_iter_init(msg, &args)) {
panic("%s", "Message has no arguments!\n");
} else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args))
panic("%s", "Argument is not integer!\n");
dbus_message_iter_get_basic(&args, &imrate);
debug(1, "%d", imrate);
send_ok(msg, conn);
}
void image_rate_get(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
DBusMessageIter args;
dbus_uint32_t serial = 0;
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &imrate)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void bit_rate_set(DBusMessage * msg, DBusConnection * conn)
{
DBusMessageIter args;
// read the arguments
if (!dbus_message_iter_init(msg, &args)) {
panic("%s", "Message has no arguments!\n");
} else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args)) {
panic("%s", "Argument is not integer!\n");
}
dbus_message_iter_get_basic(&args, &bitrate);
debug(1, "%d", bitrate);
send_ok(msg, conn);
}
void bit_rate_get(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
DBusMessageIter args;
dbus_uint32_t serial = 0;
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &bitrate)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void qscale_set(DBusMessage * msg, DBusConnection * conn)
{
DBusMessageIter args;
// read the arguments
if (!dbus_message_iter_init(msg, &args)) {
panic("%s", "Message has no arguments!\n");
} else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args))
panic("%s", "Argument is not integer!\n");
dbus_message_iter_get_basic(&args, &qscale);
debug(1, "%d", qscale);
send_ok(msg, conn);
}
void qscale_get(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
DBusMessageIter args;
dbus_uint32_t serial = 0;
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &qscale)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void client_ip_set(DBusMessage * msg, DBusConnection * conn)
{
DBusMessageIter args;
// read the arguments
if (!dbus_message_iter_init(msg, &args)) {
panic("%s", "Message has no arguments!\n");
} else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
panic("%s", "Argument is not string!\n");
char *text;
dbus_message_iter_get_basic(&args, &text);
if (clip) {
free(clip);
}
clip = strdup(text);
debug(1, "%s", clip);
send_ok(msg, conn);
}
void client_ip_get(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
DBusMessageIter args;
dbus_uint32_t serial = 0;
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &clip)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
void do_introspect(DBusMessage * msg, DBusConnection * conn)
{
DBusMessage *reply;
dbus_uint32_t serial = 0;
if (dbus_message_is_method_call
(msg, "org.freedesktop.DBus.Introspectable", "Introspect")) {
debug(2, "%s", "must do introspection");
static const char *introspect_xml =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\"ch.cett.misse.ffmpeg\">\n"
" <method name=\"start\">\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"stop\">\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"image_size_set\">\n"
" <arg name=\"size\" direction=\"in\" type=\"ii\" /> \n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"image_size_get\">\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"image_rate_set\">\n"
" <arg name=\"size\" direction=\"in\" type=\"i\" /> \n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"bit_rate_set\">\n"
" <arg name=\"size\" direction=\"in\" type=\"i\" /> \n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"image_rate_get\">\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"bit_rate_get\">\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"qscale_set\">\n"
" <arg name=\"size\" direction=\"in\" type=\"i\" /> \n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"qscale_get\">\n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"client_ip_set\">\n"
" <arg name=\"size\" direction=\"in\" type=\"s\" /> \n"
" <arg name=\"port\" direction=\"out\" type=\"i\"/>\n"
" </method>\n"
" <method name=\"client_ip_get\">\n"
" <arg name=\"port\" direction=\"out\" type=\"s\"/>\n"
" </method>\n" " </interface>\n" "</node>\n";
reply = dbus_message_new_method_return(msg);
if (!dbus_message_append_args(reply,
DBUS_TYPE_STRING, &introspect_xml,
DBUS_TYPE_INVALID)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
if (!dbus_connection_send(conn, reply, &serial)) {
panic("%s", "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
dbus_message_unref(reply);
}
}
void test_message(DBusMessage * msg, DBusConnection * conn)
{
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "image_rate_get")) {
image_rate_get(msg, conn);
}
if (dbus_message_is_method_call(msg, "ch.cett.misse.ffmpeg", "start")) {
start(msg, conn);
}
if (dbus_message_is_method_call(msg, "ch.cett.misse.ffmpeg", "stop")) {
stop(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "image_size_set")) {
image_size_set(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "image_size_get")) {
image_size_get(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "image_rate_set")) {
image_rate_set(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "bit_rate_set")) {
bit_rate_set(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "bit_rate_get")) {
bit_rate_get(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "qscale_set")) {
qscale_set(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "qscale_get")) {
qscale_get(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "client_ip_set")) {
client_ip_set(msg, conn);
}
if (dbus_message_is_method_call
(msg, "ch.cett.misse.ffmpeg", "client_ip_get")) {
client_ip_get(msg, conn);
}
return;
}
void listen()
{
DBusMessage *msg;
DBusConnection *conn;
DBusError err;
int ret;
debug(1, "%s", "Listening for method calls\n");
// initialise the error
dbus_error_init(&err);
// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
error("%s", "Connection Error");
error("%s", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
panic("%s", "Connection Null\n");
exit(1);
}
// request our name on the bus and check for errors
ret = dbus_bus_request_name(conn, "ch.cett.misse.ffmpeg",
DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
if (dbus_error_is_set(&err)) {
error("%s", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
panic("%s", "Not Primary Owner");
panic("%d", ret);
exit(1);
}
// loop, testing for new messages
while (true) {
if (status == 0) {
close(pfderr[1]);
close(pfdout[1]);
if (((read(pfderr[0], bufferr, BUFSIZ) != 0)
|| (read(pfdout[0], buffout, BUFSIZ) != 0))) {
if (bufferr != NULL) {
debug(1, "%s", bufferr);
} else {
debug(1, "%s", buffout);
}
}
}
// non blocking read of the next available message
waitpid(pid, &status, 1);
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
// loop again if we haven't got a message
if (NULL == msg) {
sleep(1);
continue;
}
// check this is a method call for the right interface & method
do_introspect(msg, conn);
test_message(msg, conn);
dbus_message_unref(msg);
}
}
int main(int argc, char **argv)
{
clip = strdup("127.0.0.1");
listen();
return 0;
}