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
|
#define AL_LOG_SECTION "cmv"
#include <al/log.h>
#include <nnwt/common.h>
#include <nnwt/event_loop.h>
#include <nnwt/thread.h>
#include "../../libsink/desktop.h"
#include "../../server/common.h"
#ifndef CAMU_SINK_ONLY
#include "../../server/server.h"
#endif
#include "../../util/color_palette.h"
#ifdef CAMU_HAVE_FFMPEG
#include "../../codec/ffmpeg/common.h"
#endif
#ifdef STELA_ICON_AT_RUNTIME
#include "icon.h"
#endif
#ifndef NAUNET_ON_WINDOWS
// https://unix.stackexchange.com/a/16884
#define PID_MAX_STR 7 // 0x400000 (2^22).
static str CMV_UNIX_PATH = al_str_c("/tmp/cmv_sock_");
#endif
struct cmv {
struct nn_event_loop loop;
struct camu_desktop desktop;
#ifndef CAMU_SINK_ONLY
struct camu_server server;
#endif
};
static void exit_callback(void *userdata, struct camu_desktop *desktop)
{
struct cmv *c = (struct cmv *)userdata;
(void)desktop;
#ifndef CAMU_SINK_ONLY
camu_server_close(&c->server);
#else
(void)c;
#endif
}
static nn_thread_result NNWT_THREADCALL event_loop_thread(void *userdata)
{
struct cmv *c = (struct cmv *)userdata;
nn_thread_set_name("event_loop");
nn_event_loop_run(&c->loop);
return 0;
}
static bool decode_addr(str *addr, str *enc)
{
if (al_str_find(enc, '.') != AL_STR_NO_POS) {
al_str_clone(addr, enc);
return true;
}
char value[4]; // max = 255.
for (u32 i = 0; i < 8; i += 2) {
str sub = al_str_substr(enc, i, i + 2);
bool error;
s64 l = al_str_to_long(&sub, 16, &error);
al_assert(l >= 0 && l <= 255);
if (error) return false;
al_snprintf(value, sizeof(value), "%hhu", (u8)l);
al_str_cat(addr, &al_str_cs(value));
if (i != 6) {
al_str_cat(addr, &al_str_c("."));
}
}
return true;
}
// sink-p12_NOTE-976502a7.exe
static bool parse_exe_name(str *exe_name, str *addr)
{
u32 index = al_str_rfind(exe_name, '/');
if (index == AL_STR_NO_POS) {
if ((index = al_str_rfind(exe_name, '\\')) == AL_STR_NO_POS) {
return false;
}
}
str sub = al_str_substr(exe_name, index + 1, exe_name->length);
if (al_str_cmp(&sub, &al_str_c("sink-"), 0, 5) != 0) {
return false;
}
sub = al_str_substr(&sub, 5, sub.length);
// Skip version.
if ((index = al_str_find(&sub, '-')) == AL_STR_NO_POS) {
return false;
}
sub = al_str_substr(&sub, index + 1, sub.length);
if ((index = al_str_find(&sub, '.')) != AL_STR_NO_POS) {
sub = al_str_substr(&sub, 0, index);
}
decode_addr(addr, &sub);
log_debug("Parsed address %.*s from exe filename.", al_str_x(addr));
return true;
}
static struct cmv c = { 0 };
#ifdef STELA_USE_PLATFORM_MAIN
s32 stl_platform_main(u32 argc, str *argv, void *extra)
#else
s32 window_system_main(u32 argc, str *argv, void *extra)
#endif
{
bool local;
#ifdef CAMU_SINK_ONLY
local = argc > 1;
if (local) {
log_warn("Got arguments in a sink-only configuration.");
return EXIT_FAILURE;
}
#else
char *SINK_SERVER = getenv("SINK_SERVER");
if (SINK_SERVER && al_strscmp(SINK_SERVER, "1") == 0) {
local = true;
} else {
local = argc > 1;
}
#endif
#ifdef CAMU_HAVE_FFMPEG
camu_ff_common_init();
#endif
s32 ret = EXIT_SUCCESS;
nn_event_loop_init(&c.loop);
u8 type;
str addr = AL_STR_EMPTY;
if (local) {
#if defined NAUNET_ON_WINDOWS || defined NAUNET_ON_ANDROID
type = NNWT_SOCKET_TCP;
al_str_clone(&addr, &al_str_c("127.0.0.1"));
#else
type = NNWT_SOCKET_UNIX;
al_str_clone(&addr, &CMV_UNIX_PATH);
char pid_str[PID_MAX_STR];
al_snprintf(pid_str, PID_MAX_STR, "%x", getpid());
al_str_cat(&addr, &al_str_cs(pid_str));
#endif
} else if (parse_exe_name(&argv[0], &addr)) {
type = NNWT_SOCKET_TCP;
} else {
type = NNWT_SOCKET_TCP;
char *SINK_IP = getenv("SINK_IP");
if (!SINK_IP || !decode_addr(&addr, &al_str_cr(SINK_IP))) {
local = true; // If no IP, default back to local.
al_str_cat(&addr, &al_str_c("127.0.0.1"));
}
}
struct lia_prefs *prefs = &c.desktop.sink.prefs;
#ifdef CAMU_HAVE_SUBTITLES
prefs->enabled = (1 << CAMU_STREAM_AUDIO) | (1 << CAMU_STREAM_SUBTITLE);
#else
prefs->enabled = (1 << CAMU_STREAM_AUDIO);
#endif
char *SINK_NO_VIDEO = getenv("SINK_NO_VIDEO");
if (SINK_NO_VIDEO && al_strscmp(SINK_NO_VIDEO, "1") == 0) {
} else {
prefs->enabled |= (1 << CAMU_STREAM_VIDEO);
}
prefs->language.audio = CAMU_LANG_JAPANESE;
prefs->language.subtitles = CAMU_LANG_ENGLISH;
#ifndef CAMU_SINK_ONLY
if (local) {
camu_server_init(&c.server, &c.loop);
#ifdef CAMU_DIRECT_MODE
camu_server_bind_direct(&c.server);
#else
if (!camu_server_listen(&c.server, type, &addr, CAMU_PORT)) {
ret = EXIT_FAILURE;
goto out;
}
#endif
}
#endif
camu_colors_default_init();
str window_name;
al_str_from(&window_name, "Sink (");
if (local) {
al_str_cat(&window_name, &al_str_c("cmv)"));
} else {
al_str_cat(&window_name, &addr);
al_str_cat(&window_name, &al_str_c(")"));
}
char *window_name_c = al_str_to_c_str(&window_name);
bool desktop_opened = camu_desktop_open(&c.desktop, window_name_c);
al_free(window_name_c);
al_str_free(&window_name);
if (!desktop_opened) {
ret = EXIT_FAILURE;
goto out;
}
str *name = local ? &al_str_c("local") : &al_str_c("desktop");
if (local) {
c.desktop.exit_callback = exit_callback;
c.desktop.userdata = &c;
}
char *SINK_FORCE_LOCAL = getenv("SINK_FORCE_LOCAL");
if (SINK_FORCE_LOCAL && al_strscmp(SINK_FORCE_LOCAL, "1") == 0) {
c.desktop.sink.local = true;
} else {
c.desktop.sink.local = local;
}
if (!camu_desktop_connect(&c.desktop, name, &c.loop, type, &addr, CAMU_PORT)) {
ret = EXIT_FAILURE;
goto out;
}
if (local) {
for (u32 i = 1; i < argc; i++) {
struct nn_packet *packet = nn_packet_create();
nn_packet_write_str(packet, &argv[i]);
#ifndef CAMU_SINK_ONLY
camu_server_local_add(&c.server, packet);
#endif
nn_packet_free(packet);
}
if (extra) {
u32 start_index = *(u32 *)extra;
if (start_index != 0) {
camu_server_local_set_index(&c.server, start_index);
}
}
}
struct nn_thread thread;
nn_thread_create(&thread, event_loop_thread, &c);
#ifdef STELA_USE_PLATFORM_MAIN
al_str_free(&addr);
return 0;
#endif
while (camu_desktop_tick(&c.desktop)) {}
camu_desktop_stop(&c.desktop);
nn_thread_join(&thread);
out:
al_str_free(&addr);
camu_desktop_free(&c.desktop);
#ifndef CAMU_SINK_ONLY
camu_server_free(&c.server);
#endif
nn_event_loop_destroy(&c.loop);
return ret;
}
#ifndef STELA_USE_PLATFORM_MAIN
static bool sigint_force = false;
static void sigint_handler(s32 signum)
{
(void)signum;
if (sigint_force) {
exit(128 + SIGINT);
} else {
c.desktop.should_quit = 1;
log_warn("Attempting graceful exit. ^C again to force exit.");
sigint_force = true;
}
}
#ifdef NAUNET_ON_WINDOWS
s32 wmain(s32 argc, wchar_t **argv)
#else
s32 main(s32 argc, char *argv[])
#endif
{
if (!nn_common_init("cmv_main")) {
return EXIT_FAILURE;
}
if (!stl_global_init(false)) {
nn_common_close();
return EXIT_FAILURE;
}
#ifdef STELA_ICON_AT_RUNTIME
stl_set_icon_data(icon_256, icon_256_len);
#endif
signal(SIGINT, sigint_handler);
array(str) args;
al_array_init(args);
al_array_reserve(args, argc);
for (s32 i = 0; i < argc; i++) {
str arg;
#ifdef NAUNET_ON_WINDOWS
if (!al_str_from_wstr(&arg, &al_wstr_cr(argv[i]))) { // This is so broken.
log_error("Failed to convert argument #%i from a wide string.", i);
continue;
}
#else
al_str_from(&arg, argv[i]);
#endif
al_array_push(args, arg);
}
s32 ret = stl_swallow_main(args.count, args.data, window_system_main, NULL);
stl_global_close();
str *arg;
al_array_foreach_ptr(args, i, arg) {
al_str_free(arg);
}
al_array_free(args);
nn_common_close();
return ret;
}
#endif
|