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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
#include "../include/Args.hpp"
#include "../include/Utils.hpp"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
#include <thread>
#include <mutex>
#include <optional>
#include <set>
#include <algorithm>
#include <mpv/client.h>
#include <mpv/stream_cb.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include <json/json.h>
#define COMMAND_BUFFER_MAX_SIZE 2048
static size_t ipc_handler(mpv_handle *mpv_ctx, int fd, char *buffer, size_t buffer_size, bool &disconnected) {
ssize_t bytes_read = read_eintr(fd, buffer, buffer_size);
if(bytes_read < 0) {
fprintf(stderr, "Error: ipc read failed, error: %s\n", strerror(errno));
//exit(3);
disconnected = false;
return 0;
}
mpv_wakeup(mpv_ctx);
disconnected = (bytes_read == 0);
return bytes_read;
}
static Json::Value handle_json_command_time_pos(mpv_handle *mpv_ctx) {
double time_pos = 0.0;
const int res = mpv_get_property(mpv_ctx, "time-pos", MPV_FORMAT_DOUBLE, &time_pos);
Json::Value response_json(Json::objectValue);
if(res < 0) {
response_json["status"] = "error";
response_json["message"] = std::string("time-pos: ") + mpv_error_string(res);
} else {
response_json["status"] = "success";
response_json["data"] = time_pos;
}
return response_json;
}
static Json::Value handle_json_command_duration(mpv_handle *mpv_ctx) {
double duration = 0.0;
const int res = mpv_get_property(mpv_ctx, "duration", MPV_FORMAT_DOUBLE, &duration);
Json::Value response_json(Json::objectValue);
if(res < 0) {
response_json["status"] = "error";
response_json["message"] = std::string("duration: ") + mpv_error_string(res);
} else {
response_json["status"] = "success";
response_json["data"] = duration;
}
return response_json;
}
static Json::Value handle_json_command_sub_add(mpv_handle *mpv_ctx, const Json::Value &json_root) {
Json::Value response_json(Json::objectValue);
const Json::Value &data_json = json_root["data"];
if(!data_json.isObject()) {
response_json["status"] = "error";
response_json["message"] = "expected \"data\" to be an object";
return response_json;
}
const Json::Value &file_json = data_json["file"];
const Json::Value &title_json = data_json["title"];
const Json::Value &language_json = data_json["language"];
if(!file_json.isString()) {
response_json["status"] = "error";
response_json["message"] = "expected \"data.file\" to be a string";
return response_json;
}
if(!title_json.isString() && !title_json.isNull()) {
response_json["status"] = "error";
response_json["message"] = "expected optional field \"data.title\" to be a string or omitted";
return response_json;
}
if(!language_json.isString() && !language_json.isNull()) {
response_json["status"] = "error";
response_json["message"] = "expected optional field \"data.language\" to be a string or omitted";
return response_json;
}
std::vector<const char*> args;
args.push_back("sub-add");
args.push_back(file_json.asCString());
args.push_back("auto");
if(title_json.isString())
args.push_back(title_json.asCString());
if(language_json.isString()) {
if(!title_json.isString())
args.push_back(language_json.asCString());
args.push_back(language_json.asCString());
}
args.push_back(nullptr);
const int res = mpv_command_async(mpv_ctx, 0, args.data());
if(res < 0) {
response_json["status"] = "error";
response_json["message"] = std::string("sub-add: ") + mpv_error_string(res);
} else {
response_json["status"] = "success";
}
return response_json;
}
static Json::Value handle_json_command_cycle_fullscreen(mpv_handle *mpv_ctx) {
const char *args[] = { "cycle", "fullscreen", nullptr };
const int res = mpv_command_async(mpv_ctx, 0, args);
Json::Value response_json(Json::objectValue);
if(res < 0) {
response_json["status"] = "error";
response_json["message"] = std::string("cycle fullscreen: ") + mpv_error_string(res);
} else {
response_json["status"] = "success";
}
return response_json;
}
static void send_error(const std::string &err_msg, std::optional<int64_t> request_id, int fd) {
fprintf(stderr, "Error: %s\n", err_msg.c_str());
Json::Value json_root(Json::objectValue);
json_root["status"] = "error";
json_root["message"] = err_msg;
if(request_id)
json_root["request_id"] = *request_id;
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = "";
const std::string response_str = Json::writeString(builder, json_root) + "\n";
ssize_t bytes_written = write_all(fd, response_str.data(), response_str.size());
if(bytes_written < 0) {
fprintf(stderr, "Error: ipc write failed, error: %s\n", strerror(errno));
//exit(3);
return;
}
}
static void handle_json_command(mpv_handle *mpv_ctx, const Json::Value &json_root, int fd) {
if(!json_root.isObject()) {
send_error("expected command json root to be an object", std::nullopt, fd);
return;
}
const Json::Value &command_json = json_root["command"];
if(!command_json.isString()) {
send_error("command json is missing field \"command\" or it's not a string", std::nullopt, fd);
return;
}
std::optional<int64_t> request_id = std::nullopt;
const Json::Value &request_id_json = json_root["request_id"];
if(!request_id_json.isNull()) {
if(request_id_json.isInt64()) {
request_id = request_id_json.asInt64();
} else {
send_error("request_id was provided but its not an integer", std::nullopt, fd);
return;
}
}
Json::Value response_json;
if(strcmp(command_json.asCString(), "time-pos") == 0) {
response_json = handle_json_command_time_pos(mpv_ctx);
} else if(strcmp(command_json.asCString(), "duration") == 0) {
response_json = handle_json_command_duration(mpv_ctx);
} else if(strcmp(command_json.asCString(), "sub-add") == 0) {
response_json = handle_json_command_sub_add(mpv_ctx, json_root);
} else if(strcmp(command_json.asCString(), "cycle-fullscreen") == 0) {
response_json = handle_json_command_cycle_fullscreen(mpv_ctx);
} else {
response_json = Json::Value(Json::objectValue);
response_json["status"] = "error";
response_json["message"] = "invalid command " + command_json.asString() + ", expected time-pos, duration, sub-add or cycle-fullscreen";
}
if(request_id)
response_json["request_id"] = *request_id;
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = "";
const std::string response_str = Json::writeString(builder, response_json) + "\n";
ssize_t bytes_written = write_all(fd, response_str.data(), response_str.size());
if(bytes_written < 0) {
fprintf(stderr, "Error: ipc write failed, error: %s\n", strerror(errno));
//exit(3);
return;
}
}
static void handle_request_commands_line_by_line(mpv_handle *mpv_ctx, int fd, char *command_buffer, size_t &command_buffer_size, Json::Value &json_root, std::string &json_errors) {
size_t command_offset = 0;
while(command_offset < command_buffer_size) {
const void *space_p = memchr(command_buffer + command_offset, '\n', command_buffer_size - command_offset);
if(!space_p)
space_p = command_buffer + command_buffer_size;
Json::CharReaderBuilder json_builder;
std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
json_errors.clear();
if(json_reader->parse(command_buffer + command_offset, (const char*)space_p, &json_root, &json_errors)) {
handle_json_command(mpv_ctx, json_root, fd);
} else {
send_error("failed to parse command as json, error: " + json_errors, std::nullopt, fd);
}
command_offset = ((const char*)space_p + 1) - command_buffer;
}
command_buffer_size = 0;
}
static void send_event(const char *event_name, int fd, const std::vector<std::string> &args = {}) {
Json::Value json_root(Json::objectValue);
json_root["event"] = event_name;
if(!args.empty()) {
Json::Value args_json(Json::arrayValue);
for(const std::string &arg : args) {
args_json.append(arg);
}
json_root["args"] = std::move(args_json);
}
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = "";
const std::string response_str = Json::writeString(builder, json_root) + "\n";
ssize_t bytes_written = write_all(fd, response_str.data(), response_str.size());
if(bytes_written < 0) {
fprintf(stderr, "Error: ipc write failed, error: %s\n", strerror(errno));
//exit(3);
return;
}
}
static inline void check_error(int status, const char *prefix) {
if (status < 0) {
fprintf(stderr, "Error: %s mpv error: %s\n", prefix, mpv_error_string(status));
exit(2);
}
}
static bool is_num(char c) {
return c >= '0' && c <= '9';
}
static bool string_looks_like_int(const char *str, size_t size) {
for(size_t i = 0; i < size; ++i) {
char c = str[i];
if(!is_num(c) && c != '-')
return false;
}
return true;
}
static bool string_looks_like_double(const char *str, size_t size) {
for(size_t i = 0; i < size; ++i) {
char c = str[i];
if(!is_num(c) && c != '-' && c != '.')
return false;
}
return true;
}
static void mpv_set_before_init_options(mpv_handle *mpv_ctx, const Args &args) {
long value_long = 0;
double value_double = 0;
std::set<std::string> known_string_properties = { "start", "force-media-title" };
for(const MpvProperty &property : args.mpv_properties) {
const bool is_string_property = known_string_properties.find(property.key) != known_string_properties.end();
if(!is_string_property && string_looks_like_int(property.value.c_str(), property.value.size()) && string_to_long(property.value.c_str(), value_long))
check_error(mpv_set_option(mpv_ctx, property.key.c_str(), MPV_FORMAT_INT64, &value_long), property.key.c_str());
else if(!is_string_property && string_looks_like_double(property.value.c_str(), property.value.size()) && string_to_double(property.value.c_str(), value_double))
check_error(mpv_set_option(mpv_ctx, property.key.c_str(), MPV_FORMAT_DOUBLE, &value_double), property.key.c_str());
else
check_error(mpv_set_option_string(mpv_ctx, property.key.c_str(), property.value.c_str()), property.key.c_str());
}
if(args.audio_file) {
mpv_node first_element;
first_element.format = MPV_FORMAT_STRING;
first_element.u.string = (char*)args.audio_file;
mpv_node_list list;
list.num = 1;
list.keys = NULL;
list.values = &first_element;
mpv_node node;
node.format = MPV_FORMAT_NODE_ARRAY;
node.u.list = &list;
check_error(mpv_set_option(mpv_ctx, "audio-files", MPV_FORMAT_NODE, &node), "audio-files");
}
}
#define READ_END 0
#define WRITE_END 1
struct ReadProgram {
pid_t pid = -1;
int read_fd = -1;
int64_t offset = 0;
int64_t content_length = -1;
char *url = nullptr;
};
static int exec_program_pipe(const char **args, ReadProgram *read_program) {
read_program->pid = -1;
read_program->read_fd = -1;
/* 1 arguments */
if(args[0] == NULL)
return -1;
int fd[2];
if(pipe(fd) == -1) {
perror("Failed to open pipe");
return -2;
}
pid_t pid = vfork();
if(pid == -1) {
perror("Failed to vfork");
close(fd[READ_END]);
close(fd[WRITE_END]);
return -3;
} else if(pid == 0) { /* child */
dup2(fd[WRITE_END], STDOUT_FILENO);
close(fd[READ_END]);
close(fd[WRITE_END]);
execvp(args[0], (char* const*)args);
perror("execvp");
_exit(127);
} else { /* parent */
close(fd[WRITE_END]);
read_program->pid = pid;
read_program->read_fd = fd[READ_END];
return 0;
}
}
static char to_upper(char c) {
if(c >= 'a' && c <= 'z')
return c - 32;
else
return c;
}
size_t str_find_case_insensitive(const std::string &str, size_t start_index, const char *substr, size_t substr_len) {
if(substr_len == 0)
return 0;
auto it = std::search(str.begin() + start_index, str.end(), substr, substr + substr_len,
[](char c1, char c2) {
return to_upper(c1) == to_upper(c2);
});
if(it == str.end())
return std::string::npos;
return it - str.begin();
}
static int64_t size_fn(void *cookie) {
ReadProgram *program = (ReadProgram*)cookie;
if(program->content_length != -1)
return program->content_length;
const char *args[] = { "curl",
"-I",
"-H", "Accept-Language: en-US,en;q=0.5", "-H", "Connection: keep-alive",
"--no-buffer",
"-H", "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
"-g", "-s", "-L", "-f",
"--", program->url, nullptr };
ReadProgram header_program;
int64_t res = exec_program_pipe(args, &header_program);
if(res != 0)
return MPV_ERROR_UNSUPPORTED;
std::string buffer;
buffer.resize(8192);
size_t read_offset = 0;
for(;;) {
ssize_t bytes_read = read_eintr(header_program.read_fd, &buffer[read_offset], buffer.size() - read_offset);
if(bytes_read == 0) {
buffer.resize(read_offset);
break;
} else if(bytes_read == -1) {
res = MPV_ERROR_UNSUPPORTED;
break;
}
read_offset += bytes_read;
if(read_offset >= buffer.size()) {
fprintf(stderr, "Error: youtube header too large\n");
res = MPV_ERROR_UNSUPPORTED;
break;
}
}
if(res != 0)
kill(header_program.pid, SIGTERM);
int status = 0;
if(waitpid(header_program.pid, &status, 0) == -1) {
perror("waitpid failed");
res = MPV_ERROR_UNSUPPORTED;
goto cleanup;
}
if(!WIFEXITED(status)) {
res = MPV_ERROR_UNSUPPORTED;
goto cleanup;
}
if(WEXITSTATUS(status) != 0)
res = MPV_ERROR_UNSUPPORTED;
cleanup:
close(header_program.read_fd);
if(res == 0) {
size_t header_start = str_find_case_insensitive(buffer, 0, "200 OK", 6);
if(header_start == std::string::npos) {
res = MPV_ERROR_UNSUPPORTED;
goto end;
}
header_start += 6;
size_t content_length_index = str_find_case_insensitive(buffer, header_start, "content-length:", 15);
if(content_length_index == std::string::npos) {
res = MPV_ERROR_UNSUPPORTED;
goto end;
}
content_length_index += 15;
size_t content_length_end = buffer.find('\r', content_length_index);
if(content_length_end == std::string::npos) {
res = MPV_ERROR_UNSUPPORTED;
goto end;
}
buffer[content_length_end] = '\0';
errno = 0;
char *endptr;
int64_t content_length = strtoll(&buffer[content_length_index], &endptr, 10);
if(endptr == &buffer[content_length_index] || errno != 0) {
res = MPV_ERROR_UNSUPPORTED;
goto end;
}
res = content_length;
}
end:
program->content_length = res;
if(res < 0) {
fprintf(stderr, "Error: video size failed\n");
exit(2);
}
return res;
}
static int64_t seek_fn(void *cookie, int64_t offset) {
ReadProgram *program = (ReadProgram*)cookie;
if(program->pid != -1) {
kill(program->pid, SIGTERM);
int status;
waitpid(program->pid, &status, 0);
program->pid = -1;
}
if(program->read_fd != -1) {
close(program->read_fd);
program->read_fd = -1;
}
program->offset = offset;
char range[64];
snprintf(range, sizeof(range), "%lld-%lld", (long long)program->offset, (long long)program->offset + 5242870LL);
const char *args[] = { "curl",
"-H", "Accept-Language: en-US,en;q=0.5", "-H", "Connection: keep-alive",
"--no-buffer",
"-H", "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
"-g", "-s", "-L", "-f",
"-r", range, "--", program->url, nullptr };
int res = exec_program_pipe(args, program);
if(res != 0) {
fprintf(stderr, "Error: video seek failed\n");
exit(2);
}
return offset;
}
static int64_t read_fn(void *cookie, char *buf, uint64_t nbytes) {
ReadProgram *program = (ReadProgram*)cookie;
ssize_t bytes_read = read_eintr(program->read_fd, buf, nbytes);
if(bytes_read > 0) {
program->offset += bytes_read;
} else if(bytes_read == 0) {
// End of current range, progress to next range
bytes_read = seek_fn(program, program->offset);
if(bytes_read >= 0) {
bytes_read = read_eintr(program->read_fd, buf, nbytes);
if(bytes_read > 0)
program->offset += bytes_read;
}
}
if(bytes_read < 0) {
fprintf(stderr, "Error: video read failed\n");
exit(2);
}
return bytes_read;
}
static void close_fn(void *cookie) {
ReadProgram *program = (ReadProgram*)cookie;
if(program->pid != -1) {
kill(program->pid, SIGTERM);
int status;
waitpid(program->pid, &status, 0);
program->pid = -1;
}
if(program->read_fd != -1) {
close(program->read_fd);
program->read_fd = -1;
}
if(program->url) {
free(program->url);
program->url = nullptr;
}
delete program;
}
static int open_fn(void*, char *uri, mpv_stream_cb_info *info) {
ReadProgram *read_program = new ReadProgram();
read_program->read_fd = -1;
read_program->pid = -1;
read_program->url = strdup((const char*)uri + 8);
read_program->offset = 0;
int64_t res = seek_fn(read_program, read_program->offset);
if(res < 0) {
fprintf(stderr, "Error: video open failed\n");
exit(2);
}
info->cookie = read_program;
info->size_fn = size_fn;
info->read_fn = read_fn;
info->seek_fn = seek_fn;
info->close_fn = close_fn;
return res >= 0 ? 0 : MPV_ERROR_LOADING_FAILED;
}
int main(int argc, char **argv) {
// This is needed for mpv_create or it will fail
setlocale(LC_ALL, "C");
Args args = parse_args(argc, argv);
mpv_handle *mpv_ctx = mpv_create();
if (!mpv_ctx) {
fprintf(stderr, "Error: failed to create mpv context\n");
return 1;
}
mpv_set_before_init_options(mpv_ctx, args);
check_error(mpv_initialize(mpv_ctx), "mpv_initialize");
check_error(mpv_stream_cb_add_ro(mpv_ctx, "qm-yt", nullptr, open_fn), "mpv_stream_cb_add_ro");
check_error(mpv_observe_property(mpv_ctx, 0, "idle-active", MPV_FORMAT_FLAG), "observe idle-active");
check_error(mpv_observe_property(mpv_ctx, 0, "fullscreen", MPV_FORMAT_FLAG), "observe fullscreen");
const char *cmd[] = { "loadfile", args.file_to_play, NULL };
check_error(mpv_command(mpv_ctx, cmd), "loadfile");
char command_buffer[COMMAND_BUFFER_MAX_SIZE];
size_t command_buffer_size = 0;
std::mutex command_mutex;
bool ipc_disconnected = false;
bool running = true;
// TODO: Clean cleanup instead of terminating... To make that possible we need to find a way to wake up the read call that is waiting for data
if(args.ipc_fd) {
std::thread([&]() mutable {
while(running) {
command_buffer_size = ipc_handler(mpv_ctx, args.ipc_fd_num, command_buffer, COMMAND_BUFFER_MAX_SIZE, ipc_disconnected);
std::lock_guard<std::mutex> lock(command_mutex); // Wait until the command has been handled in the main loop in the main thread
}
}).detach();
}
Json::Value json_root;
std::string json_errors;
bool file_started = false;
int exit_code = 0;
while (running) {
mpv_event *event = mpv_wait_event(mpv_ctx, -1.0);
if(args.ipc_fd && event->event_id != MPV_EVENT_NONE)
send_event(mpv_event_name(event->event_id), args.ipc_fd_num);
if(event->event_id == MPV_EVENT_START_FILE && !file_started) {
file_started = true;
} else if(event->event_id == MPV_EVENT_SHUTDOWN) {
running = false;
break;
} else if(event->event_id == MPV_EVENT_END_FILE) {
mpv_event_end_file *end_file_event = (mpv_event_end_file*)event->data;
if(end_file_event->reason == MPV_END_FILE_REASON_ERROR) {
exit_code = 2;
break;
}
} else if(event->event_id == MPV_EVENT_PROPERTY_CHANGE) {
// End of file (idle)
mpv_event_property *property = (mpv_event_property*)event->data;
if(file_started && strcmp(property->name, "idle-active") == 0 && *(int*)property->data == 1) {
running = false;
break;
} else if(args.ipc_fd && strcmp(property->name, "fullscreen") == 0) {
send_event("fullscreen", args.ipc_fd_num, { *(bool*)property->data ? "yes" : "no" });
}
}
if(args.ipc_fd) {
std::lock_guard<std::mutex> lock(command_mutex);
// Other end of the ipc socket has disconnected
if(ipc_disconnected) {
fprintf(stderr, "Warning: the other end of the ipc fd was closed, closing the video player...\n");
running = false;
break;
}
handle_request_commands_line_by_line(mpv_ctx, args.ipc_fd_num, command_buffer, command_buffer_size, json_root, json_errors);
}
}
mpv_terminate_destroy(mpv_ctx);
return exit_code;
}
|