-
Notifications
You must be signed in to change notification settings - Fork 1
/
screencast.c
224 lines (189 loc) · 4.45 KB
/
screencast.c
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
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2022 Codethink
*/
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "cmd/cmd.h"
#include "msg/msg.h"
#include "cmd/private.h"
#include "util/cli.h"
#include "util/log.h"
#include "util/file.h"
#include "util/util.h"
#include "util/base64.h"
static struct cmd_screencast_ctx {
const char *display;
const char *format;
uint64_t max_size;
} cmd_screencast_g = {
.format = "jpeg",
};
static const struct cli_table_entry cli_entries[] = {
CMD_CLI_COMMON("screencast"),
{
.s = 's',
.l = "max-size",
.t = CLI_UINT,
.v.u = &cmd_screencast_g.max_size,
.d = "Maximum x/y dimension in px."
},
};
static const struct cli_table cli = {
.entries = cli_entries,
.count = (sizeof(cli_entries))/(sizeof(*cli_entries)),
.min_positional = 2,
};
static bool cmd_screencast_init(int argc, const char **argv,
struct cmd_options *options, void **pw_out)
{
int id;
if (!cmd_cli_parse(argc, argv, &cli, options)) {
return false;
}
cmd_screencast_g.display = options->display;
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_START_SCREENCAST,
.data = {
.start_screencast = {
.max_width = (int)cmd_screencast_g.max_size,
.max_height = (int)cmd_screencast_g.max_size,
.format = cmd_screencast_g.format,
},
},
}, &id);
*pw_out = &cmd_screencast_g;
return true;
}
static void cmd_screencast_msg(void *pw, int id, const char *msg, size_t len)
{
(void)(pw);
cdt_log(CDT_LOG_NOTICE, "Received response with id %i: %.*s",
id, (int)len, msg);
}
struct scan_ctx {
const struct msg_scan_spec *spec;
uint32_t found;
int session_id;
size_t data_len;
const char *data;
double timestamp;
};
static bool cmd_screencast_msg_scan_cb(
void *pw,
const struct msg_scan_spec *key,
const union msg_scan_data *value)
{
struct scan_ctx *scan = pw;
assert(key != NULL);
assert(value != NULL);
scan->found |= 1 << (key - scan->spec);
if (strcmp(key->key, "sessionId") == 0) {
scan->session_id = (int)value->integer;
} else if (strcmp(key->key, "data") == 0) {
scan->data_len = value->string.len;
scan->data = value->string.str;
} else if (strcmp(key->key, "timestamp") == 0) {
scan->timestamp = value->floating_point;
}
return false;
}
static void cmd_screencast_evt(void *pw, const char *method, size_t method_len,
const char *msg, size_t len)
{
struct cmd_screencast_ctx *ctx = pw;
cdt_log(CDT_LOG_NOTICE, "Received event with method: %.*s",
(int)method_len, method);
if (strncmp(method, "Page.screencastFrame", method_len) == 0) {
static const struct msg_scan_spec spec[] = {
{
.key = "timestamp",
.type = MSG_SCAN_TYPE_FLOATING_POINT,
.depth = 3,
},
{
.key = "sessionId",
.type = MSG_SCAN_TYPE_INTEGER,
.depth = 2,
},
{
.key = "data",
.type = MSG_SCAN_TYPE_STRING,
.depth = 2,
},
};
enum {
FOUND_MASK = UINT32_MAX >> (32 - CDT_ARRAY_COUNT(spec)),
};
struct scan_ctx scan = {
.spec = spec,
.found = 0,
};
size_t scr_len;
uint8_t *scr;
int id;
if (!msg_str_scan(msg, len,
spec, CDT_ARRAY_COUNT(spec),
cmd_screencast_msg_scan_cb, &scan)) {
cdt_log(CDT_LOG_ERROR,
"%s: Failed to scan message: %*s",
__func__, (int)method_len, method);
return;
}
if (scan.found != FOUND_MASK) {
cdt_log(CDT_LOG_ERROR,
"%s: Message missing components: %*s",
__func__, (int)method_len, method);
return;
}
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_SCREENCAST_FRAME_ACK,
.data = {
.screencast_frame_ack = {
.session_id = scan.session_id,
},
},
}, &id);
if (!base64_decode(
scan.data,
scan.data_len,
&scr, &scr_len)) {
cdt_log(CDT_LOG_ERROR, "%s: Base64 decode failed",
__func__);
return;
}
file_write(scr, scr_len,
"screenshot-%s-%.6f.%s",
str_get_leaf(ctx->display),
scan.timestamp,
ctx->format);
free(scr);
}
}
static bool cmd_screencast_tick(void *pw)
{
(void)(pw);
usleep(1000 * 10);
return true;
}
static void cmd_screencast_help(int argc, const char **argv);
const struct cmd_table cmd_screencast = {
.cmd = "screencast",
.init = cmd_screencast_init,
.help = cmd_screencast_help,
.msg = cmd_screencast_msg,
.evt = cmd_screencast_evt,
.tick = cmd_screencast_tick,
};
static void cmd_screencast_help(int argc, const char **argv)
{
cli_help(&cli, (argc > 0) ? argv[0] : "cdt");
}