add player EOF detection
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Wed, 12 Jan 2022 18:37:26 +0000 (19:37 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Wed, 12 Jan 2022 18:37:26 +0000 (19:37 +0100)
application/json.c
application/player.c

index ea167e5..9bdea02 100644 (file)
@@ -615,7 +615,7 @@ int json_reader_isnull(JSONParser *p) {
 JSONLiteralType json_reader_literal(JSONParser *p) {
     const char *l = p->reader_token.content;
     size_t token_len = p->reader_token.length;
-    if(token_len == 4 && !memcmp(l, "true", 5)) {
+    if(token_len == 4 && !memcmp(l, "true", 4)) {
         return JSON_TRUE;
     } else if(token_len == 5 && !memcmp(l, "false", 5)) {
         return JSON_FALSE;
index e07b946..dbaca58 100644 (file)
@@ -106,7 +106,7 @@ static void* wait_for_process(void *data) {
     int status = 0;
     waitpid(player->process, &status, 0);
 
-    player->isactive = FALSE;
+    //player->isactive = FALSE;
     player->status = status;
     
     return NULL;
@@ -246,6 +246,7 @@ static void* start_player(void *data) {
         PlayerDestroy(player);
         return NULL;
     }
+    close(player->log);
      
     if(connect_to_ipc(player)) {
         PlayerDestroy(player);
@@ -265,29 +266,17 @@ static void* start_player(void *data) {
 }
 
 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].fd = p->ipc;
     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) {
+            if((r = read(fds[0].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
                 break;
             }
             //fwrite(buf, 1, r, stdout);
@@ -311,7 +300,8 @@ static void player_io(Player *p) {
         //write(p->ipc, cmd, strlen(cmd));
     }
     
-    printf("PlayerEnd\n");
+    
+    printf("PlayerEnd: %s\n", strerror(errno));
     fflush(stdout);
 }
 
@@ -376,17 +366,20 @@ static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) {
 
 static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) {
     if(!json_strcmp(event, "property-change")) {
-        printf("property change\n");
+        JSONValue *name = json_obj_get(&v->value.object, "name");
+        JSONValue *data = json_obj_get(&v->value.object, "data");
+        if(!json_strcmp(name, "eof-reached")) {
+            if(data && data->type == JSON_LITERAL && data->value.literal.literal == JSON_TRUE) {
+                PlayerEOF(p);
+            }
+        }
     } else if(!json_strcmp(event, "playback-restart")) {
         char *cmd = "{ \"command\": [\"observe_property\", 1, \"playback-time\"] }\n"
+                    "{ \"command\": [\"observe_property\", 1, \"eof-reached\"] }\n"
                     "{ \"command\": [\"get_property\", \"width\"], request_id=\"" REQ_ID_WIDTH "\" }\n"
-                    "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n";
+                    "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n"
+                    "{ \"command\": [\"set_property\", \"keep-open\", true] }\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);
-        }
     }
 }
 
@@ -464,7 +457,13 @@ static void json_print(JSONValue *value, char *name, int indent) {
             break;
         }
         case JSON_LITERAL: {
-            printf("%s\n", "literal\n");
+            char *lit = "NULL";
+            switch(value->value.literal.literal) {
+                case JSON_NULL: break;
+                case JSON_TRUE: lit = "true"; break;
+                case JSON_FALSE: lit = "false"; break;
+            }
+            printf("%s\n", lit);
             break;
         }
     }
@@ -475,6 +474,6 @@ static void json_print(JSONValue *value, char *name, int indent) {
 }
 
 void PlayerEOF(Player *p) {
-    char *cmd = "{ \"command\": [\"set_property\", \"pause\"], true }\n";
-    //write(p->ipc, cmd, strlen(cmd));
+    char *cmd = "{ \"command\": [\"set_property\", \"playback-time\", 0] }\n";
+    write(p->ipc, cmd, strlen(cmd));
 }