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);
59 void PlayerOpenFile(MainWindow *win) {
61 if(pthread_create(&tid, NULL, start_player, win)) {
62 perror("pthread_create");
66 static int prepare_player(Player *player, char *log_arg, char *ipc_arg) {
67 // create tmp directory for IPC
68 char *player_tmp = NULL;
69 char buf[STR_BUFSIZE];
70 snprintf(buf, STR_BUFSIZE, "/tmp/uwplayer-%x", rand());
71 int mkdir_success = 0;
72 for(int t=0;t<5;t++) {
73 if(!mkdir(buf, S_IRWXU)) {
76 } else if(errno != EEXIST) {
80 if(!mkdir_success) return 1;
81 player_tmp = strdup(buf);
82 player->tmp = player_tmp;
84 // prepare log/ipc args and create log fifo
87 if(snprintf(log_arg, STR_BUFSIZE, "--log-file=%s/%s", player_tmp, "log.fifo") >= STR_BUFSIZE) {
90 if(snprintf(ipc_arg, STR_BUFSIZE, "--input-ipc-server=%s/%s", player_tmp, "ipc.socket") >= STR_BUFSIZE) {
94 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player_tmp);
95 if(err || mkfifo(buf, S_IRUSR|S_IWUSR)) {
103 static int start_player_process(Player *player, MainWindow *win) {
104 char log_arg[STR_BUFSIZE];
105 char ipc_arg[STR_BUFSIZE];
107 if(prepare_player(player, log_arg, ipc_arg)) {
111 char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
113 // -wid parameter value for embedding the player in the player_widget
114 Window wid = XtWindow(win->player_widget);
115 char wid_arg[WID_ARG_BUFSIZE];
116 if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
120 // create player arg list
122 args[0] = player_bin;
125 args[3] = "--no-terminal";
131 posix_spawn_file_actions_t actions;
132 posix_spawn_file_actions_init(&actions);
134 //posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
135 //posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
139 if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
140 perror("posix_spawn");
143 posix_spawn_file_actions_destroy(&actions);
145 player->process = player_pid;
146 player->isactive = TRUE;
151 static int wait_for_ipc(Player *player) {
152 char buf[STR_BUFSIZE];
153 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player->tmp); // cannot fail
156 int fd_log = open(buf, O_RDONLY);
158 perror("Cannot open log");
161 player->log = fd_log;
163 // read log until IPC socket is created
164 char *scan_str = "Listening to IPC";
166 int scan_len = strlen(scan_str);
169 while((r = read(fd_log, buf, STR_BUFSIZE)) > 0) {
170 for(int i=0;i<r;i++) {
174 if(scan_pos == scan_len) {
179 if(scan_str[scan_pos] == c) {
185 if(ipc_listen) break;
191 static int connect_to_ipc(Player *player) {
192 // connect to IPC socket
193 int fd_ipc = socket(AF_UNIX, SOCK_STREAM, 0);
195 perror("Cannot create IPC socket");
198 player->ipc = fd_ipc;
200 char buf[STR_BUFSIZE];
201 snprintf(buf, STR_BUFSIZE, "%s/%s", player->tmp, "ipc.socket"); // cannot fail
203 struct sockaddr_un ipc_addr;
204 memset(&ipc_addr, 0, sizeof(struct sockaddr_un));
205 ipc_addr.sun_family = AF_UNIX;
206 memcpy(ipc_addr.sun_path, buf, strlen(buf));
207 if(connect(fd_ipc, (struct sockaddr *)&ipc_addr, sizeof(ipc_addr)) == -1) {
208 perror("Cannot connect to IPC socket");
215 static void* start_player(void *data) {
216 MainWindow *win = data;
218 Player *player = malloc(sizeof(Player));
219 memset(player, 0, sizeof(Player));
222 if(start_player_process(player, win)) {
223 PlayerDestroy(player);
227 // wait until IPC socket is ready
228 if(wait_for_ipc(player)) {
229 PlayerDestroy(player);
233 if(connect_to_ipc(player)) {
234 PlayerDestroy(player);
238 // set player in main window
240 PlayerDestroy(win->player);
242 win->player = player;
250 static void player_io(Player *p) {
251 int flags = fcntl(p->log, F_GETFL, 0);
252 fcntl(p->log, F_SETFL, flags | O_NONBLOCK);
254 struct pollfd fds[2];
256 fds[0].events = POLLIN;
259 fds[1].events = POLLIN;
262 JSONParser *js = json_parser_new();
264 char buf[PLAYER_IN_BUFSIZE];
265 while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
266 if(fds[0].revents == POLLIN) {
268 read(fds[0].fd, buf, PLAYER_IN_BUFSIZE);
271 if(fds[1].revents == POLLIN) {
273 if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
276 json_parser_fill(js, buf, r);
280 while((ret = json_read_value(js, &value)) == 1) {
281 handle_json_rpc_msg(p, value);
282 json_value_free(value);
286 fprintf(stderr, "JSON-RPC error\n");
291 char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n";
292 write(p->ipc, cmd, strlen(cmd));
297 static void handle_json_rpc_msg(Player *player, JSONValue *v) {
298 if(v->type != JSON_OBJECT) return;
300 JSONValue *request_id_v = json_obj_get(&v->value.object, "request_id");
301 if(request_id_v && request_id_v->type == JSON_STRING) {
303 if(request_id_v->value.string.length == 2) {
304 request_id = 10 * (request_id_v->value.string.string[0] - '0') + (request_id_v->value.string.string[1] - '0');
305 handle_json_rpc_reqid(player, v, request_id);
310 json_print(v, NULL, 0);
313 static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) {
314 JSONValue *data = json_obj_get(&v->value.object, "data");
318 case REQ_ID_PLAYBACK_TIME_INT: {
319 if(data->type == JSON_NUMBER) {
320 player->playback_time = data->value.number.value;
327 void PlayerDestroy(Player *p) {
340 kill(p->process, SIGTERM);
347 static void json_print(JSONValue *value, char *name, int indent) {
349 printf("%*s%s: ", indent*4, "", name);
351 printf("%*s", indent*4, "");
355 switch(value->type) {
359 for(int i=0;i<value->value.object.size;i++) {
360 JSONObjValue val = value->value.object.values[i];
361 json_print(val.value, val.name, indent+1);
362 if(i+1 < value->value.object.size) {
369 printf("%*s}", indent*4, "");
375 for(int i=0;i<value->value.object.size;i++) {
376 JSONValue *v = value->value.array.array[i];
377 json_print(v, NULL, indent+1);
378 if(i+1 < value->value.array.size) {
385 printf("%*s]", indent*4, "");
389 printf("\"%s\"", value->value.string.string);
393 printf("%i", (int)value->value.integer.value);
397 printf("%f", value->value.number.value);
401 printf("%s\n", "literal\n");