handle playback-time messages
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 8 Jan 2022 17:13:28 +0000 (18:13 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 8 Jan 2022 17:13:28 +0000 (18:13 +0100)
application/player.c
application/player.h
application/window.h

index 38b3a0c..83a5d4f 100644 (file)
@@ -53,6 +53,9 @@ 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);
+
 void PlayerOpenFile(MainWindow *win) {
     pthread_t tid;
     if(pthread_create(&tid, NULL, start_player, win)) {
@@ -275,7 +278,7 @@ static void player_io(Player *p) {
             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);
             }
             
@@ -290,6 +293,37 @@ static void player_io(Player *p) {
     }
 }
 
+
+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");
+    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;
+        }
+    }
+    
+    json_print(v, NULL, 0);
+}
+
+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;
+        }
+    }
+}
+
 void PlayerDestroy(Player *p) {
     if(p->log >= 0) {
         close(p->log);
index dc60827..26980a6 100644 (file)
@@ -30,7 +30,8 @@
 extern "C" {
 #endif
 
-#define REQ_ID_PLAYBACK_TIME "11"
+#define REQ_ID_PLAYBACK_TIME     "11"
+#define REQ_ID_PLAYBACK_TIME_INT 11
     
 void PlayerOpenFile(MainWindow *win);
 
index f63add5..c90dfe5 100644 (file)
@@ -38,6 +38,8 @@ typedef struct Player {
     int log;
     int ipc;
     bool isactive;
+    
+    double playback_time;
 } Player;
     
 typedef struct MainWindow {