[gnome-network-displays/cc-tmp: 47/80] cc: json helper functions update and replace single line comments with multi-line comments




commit d83fd7fb22c244a910b12230a68e7abf1cb2538c
Author: Anupam Kumar <kyteinsky gmail com>
Date:   Sat Sep 3 18:46:32 2022 +0530

    cc: json helper functions update and replace single line comments with multi-line comments

 src/cc/cc-ctrl.c | 125 ++++++++++++++++++++++++-------------------------------
 src/cc/cc-ctrl.h |   3 +-
 2 files changed, 55 insertions(+), 73 deletions(-)
---
diff --git a/src/cc/cc-ctrl.c b/src/cc/cc-ctrl.c
index eaf1ea4..c67a755 100644
--- a/src/cc/cc-ctrl.c
+++ b/src/cc/cc-ctrl.c
@@ -19,7 +19,7 @@
 #include "cc-ctrl.h"
 #include "cc-comm.h"
 
-// WAITING FOR
+/* WAITING FOR */
 
 static void
 cc_ctrl_set_waiting_for (CcCtrl *ctrl, CcWaitingFor waiting_for)
@@ -39,7 +39,7 @@ cc_ctrl_is_waiting_for (CcCtrl *ctrl, CcWaitingFor waiting_for)
   return (ctrl->waiting_for & waiting_for) > CC_RWAIT_TYPE_NONE;
 }
 
-// SEND HELPER FUNCTIONS
+/* SEND HELPER FUNCTIONS */
 
 static gboolean
 cc_ctrl_send_auth (CcCtrl *ctrl, GError **error)
@@ -58,10 +58,8 @@ cc_ctrl_send_connect (CcCtrl *ctrl, gchar *destination_id, GError **error)
 {
   g_debug ("CcCtrl: Sending CONNECT");
 
-  JsonNode *senderInfo;
-  JsonNode *origin;
-  cc_json_helper_build_node (&origin, NULL);
-  cc_json_helper_build_node (&senderInfo,
+  JsonNode *origin = cc_json_helper_build_node (NULL);
+  JsonNode *senderInfo = cc_json_helper_build_node (
     "sdkType", CC_JSON_TYPE_INT, 2,
     "version", CC_JSON_TYPE_STRING, "X11; Linux x86_64",
     "browserVersion", CC_JSON_TYPE_STRING, "X11; Linux x86_64",
@@ -69,8 +67,7 @@ cc_ctrl_send_connect (CcCtrl *ctrl, gchar *destination_id, GError **error)
     "connectionType", CC_JSON_TYPE_INT, 1,
     NULL);
 
-  gchar *json;
-  cc_json_helper_build_string (&json,
+  gchar *json = cc_json_helper_build_string (
     "type", CC_JSON_TYPE_STRING, "CONNECT",
     "userAgent", CC_JSON_TYPE_STRING, "GND/0.90.5  (X11; Linux x86_64)",
     "connType", CC_JSON_TYPE_INT, 0,
@@ -90,8 +87,7 @@ cc_ctrl_send_disconnect (CcCtrl *ctrl, gchar *destination_id, GError **error)
 {
   g_debug ("CcCtrl: Sending CLOSE");
 
-  gchar *json;
-  cc_json_helper_build_string (&json,
+  gchar *json = cc_json_helper_build_string (
     "type", CC_JSON_TYPE_STRING, "CLOSE",
     NULL);
 
@@ -107,8 +103,7 @@ cc_ctrl_send_get_status (CcCtrl *ctrl, gchar *destination_id, GError **error)
 {
   g_debug ("CcCtrl: Sending GET_STATUS");
 
-  gchar *json;
-  cc_json_helper_build_string (&json,
+  gchar *json = cc_json_helper_build_string (
     "type", CC_JSON_TYPE_STRING, "GET_STATUS",
     "requestId", CC_JSON_TYPE_INT, ctrl->request_id++,
     NULL);
@@ -132,8 +127,7 @@ cc_ctrl_send_get_app_availability (CcCtrl *ctrl, gchar *destination_id, gchar *a
   g_autoptr (GArray) appIds = g_array_new (FALSE, FALSE, sizeof (gchar *));
   g_array_append_val (appIds, CC_MIRRORING_APP_ID);
 
-  gchar *json;
-  cc_json_helper_build_string (&json,
+  gchar *json = cc_json_helper_build_string (
     "type", CC_JSON_TYPE_STRING, "GET_APP_AVAILABILITY",
     "appId", CC_JSON_TYPE_ARRAY_STRING, appIds,
     "requestId", CC_JSON_TYPE_INT, ctrl->request_id++,
@@ -155,8 +149,7 @@ cc_ctrl_send_launch_app (CcCtrl *ctrl, gchar *destination_id, gchar *appId, GErr
 {
   g_debug ("CcCtrl: Sending LAUNCH");
 
-  gchar *json;
-  cc_json_helper_build_string (&json,
+  gchar *json = cc_json_helper_build_string (
     "type", CC_JSON_TYPE_STRING, "LAUNCH",
     "launguage", CC_JSON_TYPE_STRING, "en-US",
     "appId", CC_JSON_TYPE_STRING, appId,
@@ -179,8 +172,7 @@ cc_ctrl_send_close_app (CcCtrl *ctrl, gchar *sessionId, GError **error)
 {
   g_debug ("CcCtrl: Sending STOP");
 
-  gchar *json;
-  cc_json_helper_build_string (&json,
+  gchar *json = cc_json_helper_build_string (
     "type", CC_JSON_TYPE_STRING, "STOP",
     "sessionId", CC_JSON_TYPE_STRING, sessionId,
     "requestId", CC_JSON_TYPE_INT, ctrl->request_id++,
@@ -197,14 +189,12 @@ cc_ctrl_send_close_app (CcCtrl *ctrl, gchar *sessionId, GError **error)
   return send_ok;
 }
 
-// OFFER MESSAGE
+/* OFFER MESSAGE */
 
 JsonNode *
 build_audio_source (AudioStream *audio_stream)
 {
-  JsonNode *node;
-
-  cc_json_helper_build_node (&node,
+  JsonNode *node = cc_json_helper_build_node (
     "aesIvMask", CC_JSON_TYPE_STRING, audio_stream->stream.aes_iv_mask,
     "aesKey", CC_JSON_TYPE_STRING, audio_stream->stream.aes_key,
     "bitRate", CC_JSON_TYPE_INT, audio_stream->bit_rate,
@@ -227,17 +217,15 @@ build_audio_source (AudioStream *audio_stream)
 JsonNode *
 build_video_source (VideoStream *video_stream)
 {
-  JsonNode *node;
-  JsonNode *resolution;
   g_autoptr (GArray) resolutions = g_array_new (FALSE, FALSE, sizeof (JsonNode *));
 
-  cc_json_helper_build_node (&resolution,
+  JsonNode *resolution = cc_json_helper_build_node (
     "height", CC_JSON_TYPE_INT, 1080,
     "width", CC_JSON_TYPE_INT, 1920,
     NULL);
   g_array_append_val (resolutions, resolution);
 
-  cc_json_helper_build_node (&node,
+  JsonNode *node = cc_json_helper_build_node (
     "aesIvMask", CC_JSON_TYPE_STRING, video_stream->stream.aes_iv_mask,
     "aesKey", CC_JSON_TYPE_STRING, video_stream->stream.aes_key,
     "codecName", CC_JSON_TYPE_STRING, video_stream->codec,
@@ -274,27 +262,22 @@ cc_ctrl_send_offer (CcCtrl *ctrl, gchar *destination_id, GError **error)
   g_array_append_val (streams, audio_source_node);
   g_array_append_val (streams, video_source_node);
 
-  JsonNode *offer_key;
-  cc_json_helper_build_node (&offer_key,
+  JsonNode *offer_key = cc_json_helper_build_node (
     "castMode", CC_JSON_TYPE_STRING, offer->cast_mode,
     "receiverGetStatus", CC_JSON_TYPE_BOOLEAN, offer->receiver_get_status,
     "supportedStreams", CC_JSON_TYPE_ARRAY_OBJECT, streams,
     NULL);
 
-  JsonNode *root;
-  cc_json_helper_build_node (&root,
+  JsonNode *root = cc_json_helper_build_node (
     "offer", CC_JSON_TYPE_OBJECT, offer_key,
     "seqNum", CC_JSON_TYPE_INT, offer->seq_num,
     "type", CC_JSON_TYPE_STRING, "OFFER",
     NULL);
-  
-  gchar *json;
-  cc_json_helper_node_to_string (&json, root);
 
   gboolean send_ok = cc_comm_send_request (&ctrl->comm,
                                destination_id,
                                CC_MESSAGE_TYPE_WEBRTC,
-                               json,
+                               cc_json_helper_node_to_string (root),
                                error);
 
   if (send_ok)
@@ -302,9 +285,9 @@ cc_ctrl_send_offer (CcCtrl *ctrl, gchar *destination_id, GError **error)
   return send_ok;
 }
 
-// INTERVAL FUNCTIONS
+/* INTERVAL FUNCTIONS */
 
-// if we are waiting for something for longer than said interval, then our connection has some problem
+/* if we are waiting for something for longer than said interval, then our connection has some problem */
 static gboolean
 cc_ctrl_check_waiting_for (CcCtrl *ctrl)
 {
@@ -323,7 +306,7 @@ cc_ctrl_send_ping (CcCtrl *ctrl)
   g_debug ("CcCtrl: Sending PING");
   g_autoptr(GError) error = NULL;
 
-  // if this errors out, we cancel the periodic ping by returning FALSE
+  /* if this errors out, we cancel the periodic ping by returning FALSE */
   if (!cc_comm_send_request (&ctrl->comm,
                              CC_DEFAULT_RECEIVER_ID,
                              CC_MESSAGE_TYPE_PING,
@@ -362,11 +345,11 @@ cc_ctrl_mirroring_app_init (CcCtrl *ctrl, GError **error)
       return;
     }
 
-  // send offer message after 1 second
+  /* send offer message after 1 second */
   g_timeout_add_seconds (1, G_SOURCE_FUNC (cc_ctrl_send_offer_cb), ctrl);
 }
 
-// HANDLE MESSAGE
+/* HANDLE MESSAGE */
 
 static void
 cc_ctrl_handle_get_app_availability (CcCtrl *ctrl, JsonReader *reader)
@@ -380,7 +363,7 @@ cc_ctrl_handle_get_app_availability (CcCtrl *ctrl, JsonReader *reader)
           const gchar *available = json_reader_get_string_value (reader);
           if (g_strcmp0 (available, "APP_AVAILABLE"))
             {
-              // launch the app now
+              /* launch the app now */
               if (!cc_ctrl_send_launch_app (ctrl, CC_DEFAULT_RECEIVER_ID, CC_MIRRORING_APP_ID, &error))
                 {
                   g_warning ("CcCtrl: Failed to launch the app: %s", error->message);
@@ -388,20 +371,21 @@ cc_ctrl_handle_get_app_availability (CcCtrl *ctrl, JsonReader *reader)
                 }
             }
           
-          // since the app is not available, stop attempts
+          /* since the app is not available, stop attempts */
           g_warning ("CcCtrl: %s app is not available, quiting", CC_MIRRORING_APP_ID);
           ctrl->closure->end_stream (ctrl->closure);
         }
     }
 }
 
-// handler messages for received messages
+/* handler messages for received messages */
 static void
 cc_ctrl_handle_receiver_status (CcCtrl *ctrl, JsonParser *parser)
 {
-  // reports all the open apps (the relevant stuff)
-  // if the app is open, it has a sessionId: opened by this app or not, it is hijackable
-  // connect to it, and then propose an offer
+  /* reports all the open apps (the relevant stuff)
+   * if the app is open, it has a sessionId: opened by this app or not, it is hijackable
+   * connect to it, and then propose an offer
+   */
 
   g_autoptr (GError) error = NULL;
        g_autoptr (JsonNode) app_status = NULL;
@@ -414,13 +398,13 @@ cc_ctrl_handle_receiver_status (CcCtrl *ctrl, JsonParser *parser)
   gsize size;
        json_generator_to_data(generator, &size);
 
-  if (size == 2) // empty array []
+  if (size == 2) /* empty array [] */
     {
       g_debug ("CcCtrl: No apps open");
       if (ctrl->state == CC_CTRL_STATE_LAUNCH_SENT)
         return;
 
-      if (ctrl->state >= CC_CTRL_STATE_APP_OPEN) // app closed unexpectedly
+      if (ctrl->state >= CC_CTRL_STATE_APP_OPEN) /* app closed unexpectedly */
         g_debug ("CcCtrl: App closed unexpectedly");
 
       if (!cc_ctrl_send_launch_app (ctrl, CC_DEFAULT_RECEIVER_ID, CC_MIRRORING_APP_ID, &error))
@@ -433,7 +417,7 @@ cc_ctrl_handle_receiver_status (CcCtrl *ctrl, JsonParser *parser)
       return;
     }
 
-  // one or more apps is/are open
+  /* one or more apps is/are open */
   g_autoptr (JsonReader) reader = json_reader_new(app_status);
 
   if (json_reader_read_element (reader, 0))
@@ -451,7 +435,7 @@ cc_ctrl_handle_receiver_status (CcCtrl *ctrl, JsonParser *parser)
 
               if (g_strcmp0 (appId, CC_MIRRORING_APP_ID) == 0)
                 {
-                  // takeover the session, doesn't matter which sender opened it
+                  /* takeover the session, doesn't matter which sender opened it */
                   g_debug ("CcCtrl: Mirroring app is open");
                   ctrl->state = CC_CTRL_STATE_APP_OPEN;
                   g_clear_pointer (&ctrl->session_id, g_free);
@@ -461,7 +445,7 @@ cc_ctrl_handle_receiver_status (CcCtrl *ctrl, JsonParser *parser)
                   return;
                 }
 
-              // some other app is open, check if `CC_MIRRORING_APP_ID` is available
+              /* some other app is open, check if `CC_MIRRORING_APP_ID` is available */
               if (!cc_ctrl_send_get_app_availability (ctrl, CC_MIRRORING_APP_ID, CC_DEFAULT_RECEIVER_ID, 
&error))
                 {
                   g_warning ("CcCtrl: Failed to send GET_APP_AVAILABILITY: %s", error->message);
@@ -475,11 +459,12 @@ cc_ctrl_handle_receiver_status (CcCtrl *ctrl, JsonParser *parser)
 static void
 cc_ctrl_handle_media_status (CcCtrl *ctrl, Cast__Channel__CastMessage *message, JsonReader *reader)
 {
-  // since answer and media_status are received one after another, we discard this
-  // for the mirroring app
-  // and since this stream is LIVE, we won't need any of it
-  // if (g_strcmp0 (message->source_id, ctrl->session_id))
-  //   return;
+  /* since answer and media_status are received one after another, we discard this
+   * for the mirroring app
+   * and since this stream is LIVE, we won't need any of it
+   * if (g_strcmp0 (message->source_id, ctrl->session_id))
+   *   return;
+   */
 }
 
 static void
@@ -494,7 +479,7 @@ cc_ctrl_handle_close (CcCtrl *ctrl, Cast__Channel__CastMessage *message)
       return;
     }
 
-  // the app closed
+  /* the app closed */
   g_debug ("CcCtrl: App sent a close message, launching again");
   if (!cc_ctrl_send_launch_app (ctrl, CC_DEFAULT_RECEIVER_ID, CC_MIRRORING_APP_ID, &error))
     {
@@ -545,7 +530,7 @@ cc_ctrl_handle_received_msg (CcCommClosure *closure,
       break;
     case CC_RWAIT_TYPE_ANSWER:
       cc_ctrl_unset_waiting_for (ctrl, CC_RWAIT_TYPE_ANSWER);
-      // cc_ctrl_handle_answer (ctrl, reader);
+      /* cc_ctrl_handle_answer (ctrl, reader); */
       break;
     case CC_RWAIT_TYPE_MEDIA_STATUS:
       cc_ctrl_unset_waiting_for (ctrl, CC_RWAIT_TYPE_MEDIA_STATUS);
@@ -571,7 +556,7 @@ cc_ctrl_handle_received_msg (CcCommClosure *closure,
 void
 cc_ctrl_fatal_error (CcCommClosure *closure, GError **error)
 {
-  // XXX: add error arg in end_stream and display an error message to user
+  /* XXX: add error arg in end_stream and display an error message to user */
   CcCtrl *ctrl = (CcCtrl *) closure->userdata;
   ctrl->closure->end_stream (ctrl->closure);
 }
@@ -594,7 +579,7 @@ cc_ctrl_connection_init (CcCtrl *ctrl, gchar *remote_address)
   ctrl->state = CC_CTRL_STATE_DISCONNECTED;
   ctrl->comm.cancellable = ctrl->cancellable;
 
-  // register all the callbacks
+  /* register all the callbacks */
   ctrl->comm.closure = cc_ctrl_get_callback_closure (ctrl);
 
   if (!cc_comm_make_connection (&ctrl->comm, remote_address, &error))
@@ -615,18 +600,18 @@ cc_ctrl_connection_init (CcCtrl *ctrl, gchar *remote_address)
       return FALSE;
     }
   
-  // since tls_send is a synchronous call
+  /* since tls_send is a synchronous call */
   ctrl->state = CC_CTRL_STATE_CONNECTED;
   
-  // send pings to device every 5 seconds
+  /* send pings to device every 5 seconds */
   ctrl->ping_timeout_handle = g_timeout_add_seconds (5, G_SOURCE_FUNC (cc_ctrl_send_ping), ctrl);
 
-  // check waiting for every 15 seconds
+  /* check waiting for every 15 seconds */
   ctrl->waiting_check_timeout_handle = g_timeout_add_seconds (CC_MAX_MESSAGE_TIMEOUT,
                                                               G_SOURCE_FUNC (cc_ctrl_check_waiting_for),
                                                               ctrl);
 
-  // we can skip some message interchange if the mirroring app is already up
+  /* we can skip some message interchange if the mirroring app is already up */
   if (!cc_ctrl_send_get_status (ctrl, CC_DEFAULT_RECEIVER_ID, &error))
     {
       g_warning ("CcCtrl: Failed to send get status: %s", error->message);
@@ -641,11 +626,10 @@ cc_ctrl_finish (CcCtrl *ctrl, GError **r_error)
 {
   g_autoptr(GError) err = NULL;
 
-  // stop both the ping and the waiting check timeout
   g_clear_handle_id (&ctrl->ping_timeout_handle, g_source_remove);
   g_clear_handle_id (&ctrl->waiting_check_timeout_handle, g_source_remove);
 
-  // close app if open
+  /* close app if open */
   if (ctrl->state >= CC_CTRL_STATE_APP_OPEN)
     {
       if (!cc_ctrl_send_disconnect (ctrl, CC_DEFAULT_RECEIVER_ID, &err))
@@ -661,20 +645,19 @@ cc_ctrl_finish (CcCtrl *ctrl, GError **r_error)
       g_clear_pointer (&ctrl->session_id, g_free);
     }
 
-  // close the virtual connection
+  /* close the virtual connection */
   if (!cc_ctrl_send_disconnect (ctrl, CC_DEFAULT_RECEIVER_ID, NULL))
     g_warning ("CcCtrl: Error closing virtual connection: %s", err->message);
 
-  // free up the resources?
+  /* free up the resources */
   g_clear_pointer (&ctrl->comm.closure, g_free);
 
-  // safe to call multiple times
   g_cancellable_cancel (ctrl->cancellable);
   g_clear_object (&ctrl->cancellable);
 
-  // close the socket connection
+  /* close the socket connection */
   cc_comm_close_connection (&ctrl->comm);
 }
 
-// TODO: make the code less coupled with the mirroring app
-// TODO: use waiting_for for error messages
+/* TODO: make the code less coupled with the mirroring app */
+/* TODO: use waiting_for for error messages */
diff --git a/src/cc/cc-ctrl.h b/src/cc/cc-ctrl.h
index 9e5adc5..bc9ef7f 100644
--- a/src/cc/cc-ctrl.h
+++ b/src/cc/cc-ctrl.h
@@ -39,7 +39,6 @@ typedef enum {
 struct _CcCtrlClosure
 {
   gpointer userdata;
-  // TODO
   Offer * (*get_offer_message) (struct _CcCtrlClosure *closure);
   void (*start_stream) (struct _CcCtrlClosure *closure);
   void (*end_stream) (struct _CcCtrlClosure *closure);
@@ -65,7 +64,7 @@ struct _CcCtrl
 
 typedef struct _CcCtrl CcCtrl;
 
-// public functions
+/* public functions */
 gboolean cc_ctrl_connection_init (CcCtrl *ctrl, gchar *remote_address);
 void cc_ctrl_finish (CcCtrl *ctrl, GError **error);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]