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)) {
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];
player->process = player_pid;
player->isactive = TRUE;
+ pthread_t tid;
+ if(pthread_create(&tid, NULL, wait_for_process, player)) {
+ perror("pthread_create");
+ }
+
return 0;
}
JSONParser *js = json_parser_new();
char buf[PLAYER_IN_BUFSIZE];
- while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
+ 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((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) {
- json_print(value, NULL, 0);
+ handle_json_rpc_msg(p, value);
json_value_free(value);
}
}
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 PlayerEOF(Player *p) {
+ char *cmd = "{ \"command\": [\"set_property\", \"pause\"], true }\n";
+ //write(p->ipc, cmd, strlen(cmd));
+}