#include <sys/fcntl.h>
#include <spawn.h>
#include <sys/wait.h>
-
+#include <signal.h>
+#include <poll.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/un.h>
#include <pthread.h>
+#include "json.h"
+
extern char **environ;
+#define STR_BUFSIZE 512
#define WID_ARG_BUFSIZE 24
+#define PLAYER_POLL_TIMEOUT 500
+#define PLAYER_IN_BUFSIZE 8192
+
+static void json_print(JSONValue *value, char *name, int indent);
+
static void* start_player(void *data);
+static void player_io(Player *p);
+
+static void handle_json_rpc_msg(Player *player, JSONValue *v);
+static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid);
+static void handle_json_rpc_event(Player *player, JSONValue *v, JSONValue *event);
+
void PlayerOpenFile(MainWindow *win) {
pthread_t tid;
if(pthread_create(&tid, NULL, start_player, win)) {
}
}
-static void* start_player(void *data) {
- MainWindow *win = data;
+static int prepare_player(Player *player, char *log_arg, char *ipc_arg) {
+ // create tmp directory for IPC
+ char *player_tmp = NULL;
+ char buf[STR_BUFSIZE];
+ snprintf(buf, STR_BUFSIZE, "/tmp/uwplayer-%x", rand());
+ int mkdir_success = 0;
+ for(int t=0;t<5;t++) {
+ if(!mkdir(buf, S_IRWXU)) {
+ mkdir_success = 1;
+ break;
+ } else if(errno != EEXIST) {
+ break;
+ }
+ }
+ if(!mkdir_success) return 1;
+ player_tmp = strdup(buf);
+ player->tmp = player_tmp;
+
+ // prepare log/ipc args and create log fifo
+ int err = 0;
+
+ if(snprintf(log_arg, STR_BUFSIZE, "--log-file=%s/%s", player_tmp, "log.fifo") >= STR_BUFSIZE) {
+ err = 1;
+ }
+ if(snprintf(ipc_arg, STR_BUFSIZE, "--input-ipc-server=%s/%s", player_tmp, "ipc.socket") >= STR_BUFSIZE) {
+ err = 1;
+ }
+
+ snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player_tmp);
+ if(err || mkfifo(buf, S_IRUSR|S_IWUSR)) {
+ rmdir(player_tmp);
+ return 1;
+ }
+
+ return 0;
+}
+
+static void* wait_for_process(void *data) {
+ Player *player = data;
+ int status = 0;
+ waitpid(player->process, &status, 0);
+
+ player->isactive = FALSE;
+ player->status = status;
+
+ return NULL;
+}
+
+static int start_player_process(Player *player, MainWindow *win) {
+ char log_arg[STR_BUFSIZE];
+ char ipc_arg[STR_BUFSIZE];
+
+ if(prepare_player(player, log_arg, ipc_arg)) {
+ return 1;
+ }
char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
Window wid = XtWindow(win->player_widget);
char wid_arg[WID_ARG_BUFSIZE];
if(snprintf(wid_arg, WID_ARG_BUFSIZE, "%lu", wid) >= WID_ARG_BUFSIZE) {
- return NULL;
+ return 1;
}
// create player arg list
args[0] = player_bin;
args[1] = "-wid";
args[2] = wid_arg;
- args[3] = win->file;
- args[4] = NULL;
-
- // redirect stdin/stdout
- int pout[2];
- int pin[2];
- if(pipe(pout)) {
- perror("pipe");
- return NULL;
- }
- if(pipe(pin)) {
- perror("pipe");
- return NULL;
- }
+ args[3] = "--no-terminal";
+ args[4] = log_arg;
+ args[5] = ipc_arg;
+ args[6] = win->file;
+ args[7] = NULL;
posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions);
- posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
- posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
+ //posix_spawn_file_actions_adddup2(&actions, pin[0], STDIN_FILENO);
+ //posix_spawn_file_actions_adddup2(&actions, pout[1], STDOUT_FILENO);
// start player
pid_t player_pid;
if(posix_spawn(&player_pid, player_bin, &actions, NULL, args, environ)) {
perror("posix_spawn");
- return NULL;
+ return 1;
}
posix_spawn_file_actions_destroy(&actions);
+ player->process = player_pid;
+ player->isactive = TRUE;
+
+ pthread_t tid;
+ if(pthread_create(&tid, NULL, wait_for_process, player)) {
+ perror("pthread_create");
+ }
+
+ return 0;
+}
+
+static int wait_for_ipc(Player *player) {
+ char buf[STR_BUFSIZE];
+ snprintf(buf, STR_BUFSIZE, "%s/log.fifo", player->tmp); // cannot fail
+
+ // open log
+ int fd_log = open(buf, O_RDONLY);
+ if(fd_log < 0) {
+ perror("Cannot open log");
+ return 1;
+ }
+ player->log = fd_log;
+
+ // read log until IPC socket is created
+ char *scan_str = "Listening to IPC";
+ int scan_pos = 0;
+ int scan_len = strlen(scan_str);
+ int ipc_listen = 0;
+ ssize_t r;
+ while((r = read(fd_log, buf, STR_BUFSIZE)) > 0) {
+ for(int i=0;i<r;i++) {
+ char c = buf[i];
+ char *s_str = buf+i;
+
+ if(scan_pos == scan_len) {
+ ipc_listen = 1;
+ break;
+ }
+
+ if(scan_str[scan_pos] == c) {
+ scan_pos++;
+ } else {
+ scan_pos = 0;
+ }
+ }
+ if(ipc_listen) break;
+ }
+
+ return 0;
+}
+
+static int connect_to_ipc(Player *player) {
+ // connect to IPC socket
+ int fd_ipc = socket(AF_UNIX, SOCK_STREAM, 0);
+ if(fd_ipc < 0) {
+ perror("Cannot create IPC socket");
+ return 1;
+ }
+ player->ipc = fd_ipc;
+
+ char buf[STR_BUFSIZE];
+ snprintf(buf, STR_BUFSIZE, "%s/%s", player->tmp, "ipc.socket"); // cannot fail
+
+ struct sockaddr_un ipc_addr;
+ memset(&ipc_addr, 0, sizeof(struct sockaddr_un));
+ ipc_addr.sun_family = AF_UNIX;
+ memcpy(ipc_addr.sun_path, buf, strlen(buf));
+ if(connect(fd_ipc, (struct sockaddr *)&ipc_addr, sizeof(ipc_addr)) == -1) {
+ perror("Cannot connect to IPC socket");
+ return 1;
+ }
+
+ return 0;
+}
+
+static void* start_player(void *data) {
+ MainWindow *win = data;
+
Player *player = malloc(sizeof(Player));
memset(player, 0, sizeof(Player));
- player->in = pin[1];
- player->out = pout[0];
- close(pin[0]);
- close(pout[1]);
- player->process = player_pid;
+ // start mpv
+ if(start_player_process(player, win)) {
+ PlayerDestroy(player);
+ return NULL;
+ }
+
+ // wait until IPC socket is ready
+ if(wait_for_ipc(player)) {
+ PlayerDestroy(player);
+ return NULL;
+ }
+
+ if(connect_to_ipc(player)) {
+ PlayerDestroy(player);
+ return NULL;
+ }
+ // set player in main window
if(win->player) {
PlayerDestroy(win->player);
}
win->player = player;
+ // IO
+ player_io(player);
+
return NULL;
}
+static void player_io(Player *p) {
+ int flags = fcntl(p->log, F_GETFL, 0);
+ fcntl(p->log, F_SETFL, flags | O_NONBLOCK);
+
+ struct pollfd fds[2];
+ fds[0].fd = p->log;
+ fds[0].events = POLLIN;
+ fds[0].revents = 0;
+ fds[1].fd = p->ipc;
+ fds[1].events = POLLIN;
+ fds[1].revents = 0;
+
+ JSONParser *js = json_parser_new();
+
+ char buf[PLAYER_IN_BUFSIZE];
+ while(p->isactive && poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
+ if(fds[0].revents == POLLIN) {
+ // clean up fifo
+ read(fds[0].fd, buf, PLAYER_IN_BUFSIZE);
+ }
+
+ if(fds[1].revents == POLLIN) {
+ ssize_t r;
+ if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
+ break;
+ }
+ //fwrite(buf, 1, r, stdout);
+ fflush(stdout);
+ json_parser_fill(js, buf, r);
+
+ JSONValue *value;
+ int ret;
+ while((ret = json_read_value(js, &value)) == 1) {
+ handle_json_rpc_msg(p, value);
+ json_value_free(value);
+ }
+
+ if(ret == -1) {
+ fprintf(stderr, "JSON-RPC error\n");
+ break;
+ }
+ }
+
+ char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n";
+ //write(p->ipc, cmd, strlen(cmd));
+ }
+
+ printf("PlayerEnd\n");
+ fflush(stdout);
+}
+
+
+static void handle_json_rpc_msg(Player *player, JSONValue *v) {
+ if(v->type != JSON_OBJECT) return;
+
+ JSONValue *request_id_v = json_obj_get(&v->value.object, "request_id");
+ JSONValue *event = NULL;
+ if(request_id_v && request_id_v->type == JSON_STRING) {
+ int request_id = 0;
+ if(request_id_v->value.string.length == 2) {
+ request_id = 10 * (request_id_v->value.string.string[0] - '0') + (request_id_v->value.string.string[1] - '0');
+ handle_json_rpc_reqid(player, v, request_id);
+ return;
+ }
+ } else if ((event = json_obj_get(&v->value.object, "event")) != NULL) {
+ handle_json_rpc_event(player, v, event);
+ }
+
+ json_print(v, NULL, 0);
+ fflush(stdout);
+}
+
+static void player_set_size(Player *player, int width, int height) {
+ if(width >= 0) {
+ player->width = width;
+ }
+ if(height >= 0) {
+ player->height = height;
+ }
+ if(player->width > 0 && player->height > 0) {
+ printf("TODO: set player size\n");
+ }
+}
+
+static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) {
+ JSONValue *data = json_obj_get(&v->value.object, "data");
+ if(!data) return;
+
+ switch(reqid) {
+ case REQ_ID_PLAYBACK_TIME_INT: {
+ if(data->type == JSON_NUMBER) {
+ player->playback_time = data->value.number.value;
+ }
+ break;
+ }
+ case REQ_ID_WIDTH_INT: {
+ if(data->type == JSON_INTEGER) {
+ player_set_size(player, data->value.integer.value, -1);
+ }
+ break;
+ }
+ case REQ_ID_HEIGHT_INT: {
+ if(data->type == JSON_INTEGER) {
+ player_set_size(player, -1, data->value.integer.value);
+ }
+ break;
+ }
+ }
+}
+
+static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) {
+ if(!json_strcmp(event, "property-change")) {
+ printf("property change\n");
+ } else if(!json_strcmp(event, "playback-restart")) {
+ char *cmd = "{ \"command\": [\"observe_property\", 1, \"playback-time\"] }\n"
+ "{ \"command\": [\"get_property\", \"width\"], request_id=\"" REQ_ID_WIDTH "\" }\n"
+ "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n";
+ write(p->ipc, cmd, strlen(cmd));
+ } else if(!json_strcmp(event, "end-file")) {
+ JSONValue *reason = json_obj_get(&v->value.object, "reason");
+ if(reason && !json_strcmp(reason, "eof")) {
+ PlayerEOF(p);
+ }
+ }
+}
+
void PlayerDestroy(Player *p) {
+ if(p->log >= 0) {
+ close(p->log);
+ }
+ if(p->ipc >= 0) {
+ close(p->ipc);
+ }
+
+ if(p->tmp) {
+ free(p->tmp);
+ }
+
+ if(p->isactive) {
+ kill(p->process, SIGTERM);
+ }
+
free(p);
}
+
+
+static void json_print(JSONValue *value, char *name, int indent) {
+ if(name) {
+ printf("%*s%s: ", indent*4, "", name);
+ } else {
+ printf("%*s", indent*4, "");
+ }
+
+
+ switch(value->type) {
+ case JSON_OBJECT: {
+ printf("{\n");
+
+ for(int i=0;i<value->value.object.size;i++) {
+ JSONObjValue val = value->value.object.values[i];
+ json_print(val.value, val.name, indent+1);
+ if(i+1 < value->value.object.size) {
+ printf(",\n");
+ } else {
+ printf("\n");
+ }
+ }
+
+ printf("%*s}", indent*4, "");
+ break;
+ }
+ case JSON_ARRAY: {
+ printf("[\n");
+
+ for(int i=0;i<value->value.object.size;i++) {
+ JSONValue *v = value->value.array.array[i];
+ json_print(v, NULL, indent+1);
+ if(i+1 < value->value.array.size) {
+ printf(",\n");
+ } else {
+ printf("\n");
+ }
+ }
+
+ printf("%*s]", indent*4, "");
+ break;
+ }
+ case JSON_STRING: {
+ printf("\"%s\"", value->value.string.string);
+ break;
+ }
+ case JSON_INTEGER: {
+ printf("%i", (int)value->value.integer.value);
+ break;
+ }
+ case JSON_NUMBER: {
+ printf("%f", value->value.number.value);
+ break;
+ }
+ case JSON_LITERAL: {
+ printf("%s\n", "literal\n");
+ break;
+ }
+ }
+
+ if(indent == 0) {
+ putchar('\n');
+ }
+}
+
+void PlayerEOF(Player *p) {
+ char *cmd = "{ \"command\": [\"set_property\", \"pause\"], true }\n";
+ //write(p->ipc, cmd, strlen(cmd));
+}