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* start_player(void *data);
52 static void player_io(Player *p);
54 void PlayerOpenFile(MainWindow *win) {
56 if(pthread_create(&tid, NULL, start_player, win)) {
57 perror("pthread_create");
61 static int prepare_player(Player *player, char *log_arg, char *ipc_arg) {
62 // create tmp directory for IPC
63 char *player_tmp = NULL;
64 char buf[STR_BUFSIZE];
65 snprintf(buf, STR_BUFSIZE, "/tmp/uwplayer-%x", rand());
66 int mkdir_success = 0;
67 for(int t=0;t<5;t++) {
68 if(!mkdir(buf, S_IRWXU)) {
71 } else if(errno != EEXIST) {
75 if(!mkdir_success) return 1;
76 player_tmp = strdup(buf);
77 player->tmp = player_tmp;
79 // prepare log/ipc args and create log fifo
82 if(snprintf(log_arg, STR_BUFSIZE, "--log-file=%s/%s", player_tmp, "log.fifo") >= STR_BUFSIZE) {
85 if(snprintf(ipc_arg, STR_BUFSIZE, "--input-ipc-server=%s/%s", player_tmp, "ipc.socket") >= STR_BUFSIZE) {
89 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player_tmp);
90 if(err || mkfifo(buf, S_IRUSR|S_IWUSR)) {
98 static int start_player_process(Player *player, MainWindow *win) {
99 char log_arg[STR_BUFSIZE];
100 char ipc_arg[STR_BUFSIZE];
102 if(prepare_player(player, log_arg, ipc_arg)) {
106 char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
108 // -wid parameter value for embedding the player in the player_widget
109 Window wid = XtWindow(win->player_widget);
110 char wid_arg[WID_ARG_BUFSIZE];
111 if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
115 // create player arg list
117 args[0] = player_bin;
120 args[3] = "--no-terminal";
126 posix_spawn_file_actions_t actions;
127 posix_spawn_file_actions_init(&actions);
129 //posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
130 //posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
134 if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
135 perror("posix_spawn");
138 posix_spawn_file_actions_destroy(&actions);
140 player->process = player_pid;
141 player->isactive = TRUE;
146 static int wait_for_ipc(Player *player) {
147 char buf[STR_BUFSIZE];
148 snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player->tmp); // cannot fail
151 int fd_log = open(buf, O_RDONLY);
153 perror("Cannot open log");
156 player->log = fd_log;
158 // read log until IPC socket is created
159 char *scan_str = "Listening to IPC";
161 int scan_len = strlen(scan_str);
164 while((r = read(fd_log, buf, STR_BUFSIZE)) > 0) {
165 for(int i=0;i<r;i++) {
169 if(scan_pos == scan_len) {
174 if(scan_str[scan_pos] == c) {
180 if(ipc_listen) break;
186 static int connect_to_ipc(Player *player) {
187 // connect to IPC socket
188 int fd_ipc = socket(AF_UNIX, SOCK_STREAM, 0);
190 perror("Cannot create IPC socket");
193 player->ipc = fd_ipc;
195 char buf[STR_BUFSIZE];
196 snprintf(buf, STR_BUFSIZE, "%s/%s", player->tmp, "ipc.socket"); // cannot fail
198 struct sockaddr_un ipc_addr;
199 memset(&ipc_addr, 0, sizeof(struct sockaddr_un));
200 ipc_addr.sun_family = AF_UNIX;
201 memcpy(ipc_addr.sun_path, buf, strlen(buf));
202 if(connect(fd_ipc, (struct sockaddr *)&ipc_addr, sizeof(ipc_addr)) == -1) {
203 perror("Cannot connect to IPC socket");
210 static void* start_player(void *data) {
211 MainWindow *win = data;
213 Player *player = malloc(sizeof(Player));
214 memset(player, 0, sizeof(Player));
217 if(start_player_process(player, win)) {
218 PlayerDestroy(player);
222 // wait until IPC socket is ready
223 if(wait_for_ipc(player)) {
224 PlayerDestroy(player);
228 if(connect_to_ipc(player)) {
229 PlayerDestroy(player);
233 // set player in main window
235 PlayerDestroy(win->player);
237 win->player = player;
245 static void player_io(Player *p) {
246 int flags = fcntl(p->log, F_GETFL, 0);
247 fcntl(p->log, F_SETFL, flags | O_NONBLOCK);
249 struct pollfd fds[2];
251 fds[0].events = POLLIN;
254 fds[1].events = POLLIN;
257 char buf[PLAYER_IN_BUFSIZE];
258 while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
259 if(fds[0].revents == POLLIN) {
261 read(fds[0].fd, buf, PLAYER_IN_BUFSIZE);
264 if(fds[1].revents == POLLIN) {
266 if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
274 char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"] }\n";
275 write(p->ipc, cmd, strlen(cmd));
279 void PlayerDestroy(Player *p) {
292 kill(p->process, SIGTERM);