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 void PlayerOpenFile(MainWindow *win) {
58 if(pthread_create(&tid, NULL, start_player, win)) {
59 perror("pthread_create");
63 static int prepare_player(Player *player, char *log_arg, char *ipc_arg) {
64 // create tmp directory for IPC
65 char *player_tmp = NULL;
66 char buf[STR_BUFSIZE];
67 snprintf(buf, STR_BUFSIZE, "/tmp/uwplayer-%x", rand());
68 int mkdir_success = 0;
69 for(int t=0;t<5;t++) {
70 if(!mkdir(buf, S_IRWXU)) {
73 } else if(errno != EEXIST) {
77 if(!mkdir_success) return 1;
78 player_tmp = strdup(buf);
79 player->tmp = player_tmp;
81 // prepare log/ipc args and create log fifo
84 if(snprintf(log_arg, STR_BUFSIZE, "--log-file=%s/%s", player_tmp, "log.fifo") >= STR_BUFSIZE) {
87 if(snprintf(ipc_arg, STR_BUFSIZE, "--input-ipc-server=%s/%s", player_tmp, "ipc.socket") >= STR_BUFSIZE) {
91 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player_tmp);
92 if(err || mkfifo(buf, S_IRUSR|S_IWUSR)) {
100 static int start_player_process(Player *player, MainWindow *win) {
101 char log_arg[STR_BUFSIZE];
102 char ipc_arg[STR_BUFSIZE];
104 if(prepare_player(player, log_arg, ipc_arg)) {
108 char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
110 // -wid parameter value for embedding the player in the player_widget
111 Window wid = XtWindow(win->player_widget);
112 char wid_arg[WID_ARG_BUFSIZE];
113 if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
117 // create player arg list
119 args[0] = player_bin;
122 args[3] = "--no-terminal";
128 posix_spawn_file_actions_t actions;
129 posix_spawn_file_actions_init(&actions);
131 //posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
132 //posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
136 if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
137 perror("posix_spawn");
140 posix_spawn_file_actions_destroy(&actions);
142 player->process = player_pid;
143 player->isactive = TRUE;
148 static int wait_for_ipc(Player *player) {
149 char buf[STR_BUFSIZE];
150 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player->tmp); // cannot fail
153 int fd_log = open(buf, O_RDONLY);
155 perror("Cannot open log");
158 player->log = fd_log;
160 // read log until IPC socket is created
161 char *scan_str = "Listening to IPC";
163 int scan_len = strlen(scan_str);
166 while((r = read(fd_log, buf, STR_BUFSIZE)) > 0) {
167 for(int i=0;i<r;i++) {
171 if(scan_pos == scan_len) {
176 if(scan_str[scan_pos] == c) {
182 if(ipc_listen) break;
188 static int connect_to_ipc(Player *player) {
189 // connect to IPC socket
190 int fd_ipc = socket(AF_UNIX, SOCK_STREAM, 0);
192 perror("Cannot create IPC socket");
195 player->ipc = fd_ipc;
197 char buf[STR_BUFSIZE];
198 snprintf(buf, STR_BUFSIZE, "%s/%s", player->tmp, "ipc.socket"); // cannot fail
200 struct sockaddr_un ipc_addr;
201 memset(&ipc_addr, 0, sizeof(struct sockaddr_un));
202 ipc_addr.sun_family = AF_UNIX;
203 memcpy(ipc_addr.sun_path, buf, strlen(buf));
204 if(connect(fd_ipc, (struct sockaddr *)&ipc_addr, sizeof(ipc_addr)) == -1) {
205 perror("Cannot connect to IPC socket");
212 static void* start_player(void *data) {
213 MainWindow *win = data;
215 Player *player = malloc(sizeof(Player));
216 memset(player, 0, sizeof(Player));
219 if(start_player_process(player, win)) {
220 PlayerDestroy(player);
224 // wait until IPC socket is ready
225 if(wait_for_ipc(player)) {
226 PlayerDestroy(player);
230 if(connect_to_ipc(player)) {
231 PlayerDestroy(player);
235 // set player in main window
237 PlayerDestroy(win->player);
239 win->player = player;
247 static void player_io(Player *p) {
248 int flags = fcntl(p->log, F_GETFL, 0);
249 fcntl(p->log, F_SETFL, flags | O_NONBLOCK);
251 struct pollfd fds[2];
253 fds[0].events = POLLIN;
256 fds[1].events = POLLIN;
259 JSONParser *js = json_parser_new();
261 char buf[PLAYER_IN_BUFSIZE];
262 while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
263 if(fds[0].revents == POLLIN) {
265 read(fds[0].fd, buf, PLAYER_IN_BUFSIZE);
268 if(fds[1].revents == POLLIN) {
270 if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
273 json_parser_fill(js, buf, r);
277 while((ret = json_read_value(js, &value)) == 1) {
278 json_print(value, NULL, 0);
279 json_value_free(value);
283 fprintf(stderr, "JSON-RPC error\n");
288 char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n";
289 write(p->ipc, cmd, strlen(cmd));
293 void PlayerDestroy(Player *p) {
306 kill(p->process, SIGTERM);
313 static void json_print(JSONValue *value, char *name, int indent) {
315 printf("%*s%s: ", indent*4, "", name);
317 printf("%*s", indent*4, "");
321 switch(value->type) {
325 for(int i=0;i<value->value.object.size;i++) {
326 JSONObjValue val = value->value.object.values[i];
327 json_print(val.value, val.name, indent+1);
328 if(i+1 < value->value.object.size) {
335 printf("%*s}", indent*4, "");
341 for(int i=0;i<value->value.object.size;i++) {
342 JSONValue *v = value->value.array.array[i];
343 json_print(v, NULL, indent+1);
344 if(i+1 < value->value.array.size) {
351 printf("%*s]", indent*4, "");
355 printf("\"%s\"", value->value.string.string);
359 printf("%i", (int)value->value.integer.value);
363 printf("%f", value->value.number.value);
367 printf("%s\n", "literal\n");