2 * Copyright 2022 Olaf Wintermann
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
28 #include <sys/fcntl.h>
36 #include <sys/socket.h>
42 extern char **environ;
44 #define STR_BUFSIZE 512
45 #define WID_ARG_BUFSIZE 24
47 #define PLAYER_POLL_TIMEOUT 500
48 #define PLAYER_IN_BUFSIZE 8192
50 static void json_print(JSONValue *value, char *name, int indent);
52 static void* start_player(void *data);
54 static void player_io(Player *p);
56 static void handle_json_rpc_msg(Player *player, JSONValue *v);
57 static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid);
58 static void handle_json_rpc_event(Player *player, JSONValue *v, JSONValue *event);
60 void PlayerOpenFile(MainWindow *win) {
62 if(pthread_create(&tid, NULL, start_player, win)) {
63 perror("pthread_create");
67 static int prepare_player(Player *player, char *log_arg, char *ipc_arg) {
68 // create tmp directory for IPC
69 char *player_tmp = NULL;
70 char buf[STR_BUFSIZE];
71 snprintf(buf, STR_BUFSIZE, "/tmp/uwplayer-%x", rand());
72 int mkdir_success = 0;
73 for(int t=0;t<5;t++) {
74 if(!mkdir(buf, S_IRWXU)) {
77 } else if(errno != EEXIST) {
81 if(!mkdir_success) return 1;
82 player_tmp = strdup(buf);
83 player->tmp = player_tmp;
85 // prepare log/ipc args and create log fifo
88 if(snprintf(log_arg, STR_BUFSIZE, "--log-file=%s/%s", player_tmp, "log.fifo") >= STR_BUFSIZE) {
91 if(snprintf(ipc_arg, STR_BUFSIZE, "--input-ipc-server=%s/%s", player_tmp, "ipc.socket") >= STR_BUFSIZE) {
95 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player_tmp);
96 if(err || mkfifo(buf, S_IRUSR|S_IWUSR)) {
104 static int start_player_process(Player *player, MainWindow *win) {
105 char log_arg[STR_BUFSIZE];
106 char ipc_arg[STR_BUFSIZE];
108 if(prepare_player(player, log_arg, ipc_arg)) {
112 char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
114 // -wid parameter value for embedding the player in the player_widget
115 Window wid = XtWindow(win->player_widget);
116 char wid_arg[WID_ARG_BUFSIZE];
117 if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
121 // create player arg list
123 args[0] = player_bin;
126 args[3] = "--no-terminal";
132 posix_spawn_file_actions_t actions;
133 posix_spawn_file_actions_init(&actions);
135 //posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
136 //posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
140 if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
141 perror("posix_spawn");
144 posix_spawn_file_actions_destroy(&actions);
146 player->process = player_pid;
147 player->isactive = TRUE;
152 static int wait_for_ipc(Player *player) {
153 char buf[STR_BUFSIZE];
154 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player->tmp); // cannot fail
157 int fd_log = open(buf, O_RDONLY);
159 perror("Cannot open log");
162 player->log = fd_log;
164 // read log until IPC socket is created
165 char *scan_str = "Listening to IPC";
167 int scan_len = strlen(scan_str);
170 while((r = read(fd_log, buf, STR_BUFSIZE)) > 0) {
171 for(int i=0;i<r;i++) {
175 if(scan_pos == scan_len) {
180 if(scan_str[scan_pos] == c) {
186 if(ipc_listen) break;
192 static int connect_to_ipc(Player *player) {
193 // connect to IPC socket
194 int fd_ipc = socket(AF_UNIX, SOCK_STREAM, 0);
196 perror("Cannot create IPC socket");
199 player->ipc = fd_ipc;
201 char buf[STR_BUFSIZE];
202 snprintf(buf, STR_BUFSIZE, "%s/%s", player->tmp, "ipc.socket"); // cannot fail
204 struct sockaddr_un ipc_addr;
205 memset(&ipc_addr, 0, sizeof(struct sockaddr_un));
206 ipc_addr.sun_family = AF_UNIX;
207 memcpy(ipc_addr.sun_path, buf, strlen(buf));
208 if(connect(fd_ipc, (struct sockaddr *)&ipc_addr, sizeof(ipc_addr)) == -1) {
209 perror("Cannot connect to IPC socket");
216 static void* start_player(void *data) {
217 MainWindow *win = data;
219 Player *player = malloc(sizeof(Player));
220 memset(player, 0, sizeof(Player));
223 if(start_player_process(player, win)) {
224 PlayerDestroy(player);
228 // wait until IPC socket is ready
229 if(wait_for_ipc(player)) {
230 PlayerDestroy(player);
234 if(connect_to_ipc(player)) {
235 PlayerDestroy(player);
239 // set player in main window
241 PlayerDestroy(win->player);
243 win->player = player;
251 static void player_io(Player *p) {
252 int flags = fcntl(p->log, F_GETFL, 0);
253 fcntl(p->log, F_SETFL, flags | O_NONBLOCK);
255 struct pollfd fds[2];
257 fds[0].events = POLLIN;
260 fds[1].events = POLLIN;
263 JSONParser *js = json_parser_new();
265 char buf[PLAYER_IN_BUFSIZE];
266 while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
267 if(fds[0].revents == POLLIN) {
269 read(fds[0].fd, buf, PLAYER_IN_BUFSIZE);
272 if(fds[1].revents == POLLIN) {
274 if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
277 //fwrite(buf, 1, r, stdout);
279 json_parser_fill(js, buf, r);
283 while((ret = json_read_value(js, &value)) == 1) {
284 handle_json_rpc_msg(p, value);
285 json_value_free(value);
289 fprintf(stderr, "JSON-RPC error\n");
294 char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n";
295 //write(p->ipc, cmd, strlen(cmd));
300 static void handle_json_rpc_msg(Player *player, JSONValue *v) {
301 if(v->type != JSON_OBJECT) return;
303 JSONValue *request_id_v = json_obj_get(&v->value.object, "request_id");
304 JSONValue *event = NULL;
305 if(request_id_v && request_id_v->type == JSON_STRING) {
307 if(request_id_v->value.string.length == 2) {
308 request_id = 10 * (request_id_v->value.string.string[0] - '0') + (request_id_v->value.string.string[1] - '0');
309 handle_json_rpc_reqid(player, v, request_id);
312 } else if ((event = json_obj_get(&v->value.object, "event")) != NULL) {
313 handle_json_rpc_event(player, v, event);
316 json_print(v, NULL, 0);
320 static void player_set_size(Player *player, int width, int height) {
322 player->width = width;
325 player->height = height;
327 if(player->width > 0 && player->height > 0) {
328 printf("TODO: set player size\n");
332 static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) {
333 JSONValue *data = json_obj_get(&v->value.object, "data");
337 case REQ_ID_PLAYBACK_TIME_INT: {
338 if(data->type == JSON_NUMBER) {
339 player->playback_time = data->value.number.value;
343 case REQ_ID_WIDTH_INT: {
344 if(data->type == JSON_INTEGER) {
345 player_set_size(player, data->value.integer.value, -1);
349 case REQ_ID_HEIGHT_INT: {
350 if(data->type == JSON_INTEGER) {
351 player_set_size(player, -1, data->value.integer.value);
358 static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) {
359 if(!json_strcmp(event, "property-change")) {
360 printf("property change\n");
361 } else if(!json_strcmp(event, "playback-restart")) {
362 char *cmd = "{ \"command\": [\"observe_property\", 1, \"playback-time\"] }\n"
363 "{ \"command\": [\"get_property\", \"width\"], request_id=\"" REQ_ID_WIDTH "\" }\n"
364 "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n";
365 write(p->ipc, cmd, strlen(cmd));
369 void PlayerDestroy(Player *p) {
382 kill(p->process, SIGTERM);
389 static void json_print(JSONValue *value, char *name, int indent) {
391 printf("%*s%s: ", indent*4, "", name);
393 printf("%*s", indent*4, "");
397 switch(value->type) {
401 for(int i=0;i<value->value.object.size;i++) {
402 JSONObjValue val = value->value.object.values[i];
403 json_print(val.value, val.name, indent+1);
404 if(i+1 < value->value.object.size) {
411 printf("%*s}", indent*4, "");
417 for(int i=0;i<value->value.object.size;i++) {
418 JSONValue *v = value->value.array.array[i];
419 json_print(v, NULL, indent+1);
420 if(i+1 < value->value.array.size) {
427 printf("%*s]", indent*4, "");
431 printf("\"%s\"", value->value.string.string);
435 printf("%i", (int)value->value.integer.value);
439 printf("%f", value->value.number.value);
443 printf("%s\n", "literal\n");