[snappy/aurena: 2/5] make snappy an aurena client



commit 311319b5b1e583055ff55245874dc4462d08eec4
Author: Luis de Bethencourt <luis debethencourt com>
Date:   Fri Sep 14 10:21:51 2012 +0100

    make snappy an aurena client

 src/simple-client.c |    1 +
 src/snappy.c        |   75 ++--------------------------
 src/snra-client.c   |  137 ++++++++++++++++++++++----------------------------
 src/snra-client.h   |    5 ++-
 4 files changed, 70 insertions(+), 148 deletions(-)
---
diff --git a/src/simple-client.c b/src/simple-client.c
index eaee412..a284946 100644
--- a/src/simple-client.c
+++ b/src/simple-client.c
@@ -82,6 +82,7 @@ main (int argc, char *argv[])
   const gchar *server = NULL;
 
   gst_init (&argc, &argv);
+  clutter_init (&argc, &argv);
 
   if (argc > 1) {
     /* Connect directly to the requested server, no avahi */
diff --git a/src/snappy.c b/src/snappy.c
index fb80509..1ccbb9a 100644
--- a/src/snappy.c
+++ b/src/snappy.c
@@ -152,13 +152,13 @@ process_args (int argc, char *argv[],
 
 
 /*            snappy's main function             */
-int
-snappy_main (int argc, char *argv[])
+UserInterface *
+snappy_construct ()
 {
-  UserInterface *ui = NULL;
   GstEngine *engine = NULL;
   ClutterActor *video_texture;
   GstElement *sink;
+  UserInterface *ui = NULL;
 
   gboolean ok, blind = FALSE, fullscreen = FALSE, hide = FALSE, loop = TRUE;
   gboolean secret = FALSE, tags = FALSE;
@@ -171,30 +171,11 @@ snappy_main (int argc, char *argv[])
   GOptionContext *context;
   gchar *data_dir;
 
-#ifdef ENABLE_DBUS
-  SnappyMP *mp_obj = NULL;
-#endif
-
-
   /* Try to find the path for our resources in case snappy was relocated */
   data_dir = g_strdup(SNAPPY_DATA_DIR);
   if (!g_file_test(data_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
     gchar *root_dir;
 
-#ifdef G_OS_WIN32
-    root_dir = g_win32_get_package_installation_directory_of_module(NULL);
-#elif !defined(G_OS_UNIX)
-    gchar *exec_path;
-    gchar *bin_dir;
-
-    exec_path = g_file_read_link("/proc/self/exe", NULL);
-    bin_dir = g_path_get_dirname(exec_path);
-    root_dir = g_build_filename(bin_dir, "..", NULL);
-    g_free(exec_path);
-    g_free(bin_dir);
-#else
-    root_dir = NULL;
-#endif
     if (root_dir != NULL) {
       data_dir = g_build_filename(root_dir, "share", "snappy", NULL);
       g_free(root_dir);
@@ -206,12 +187,6 @@ snappy_main (int argc, char *argv[])
 
   context = g_option_context_new ("<media file> - Play movie files");
 
-  /* Process command arguments */
-  uri_list = process_args (argc, argv, &blind, &fullscreen, &hide,
-      &loop, &secret, &suburi, &tags, context);
-  if (uri_list == NULL)
-    goto quit;
-
   /* User Interface */
   ui = g_new (UserInterface, 1);
   ui->uri_list = uri_list;
@@ -223,7 +198,7 @@ snappy_main (int argc, char *argv[])
   interface_init (ui);
   video_texture = clutter_texture_new ();
 
-  clutter_gst_init (&argc, &argv);
+  clutter_gst_init (NULL, NULL);
 
   version_str = gst_version_string ();
   GST_DEBUG_CATEGORY_INIT (_snappy_gst_debug, "snappy", 0,
@@ -241,8 +216,6 @@ snappy_main (int argc, char *argv[])
       NULL);
 
   ok = engine_init (engine, sink);
-  if (!ok)
-    goto quit;
 
   engine->secret = secret;
   engine->loop = loop;
@@ -253,43 +226,5 @@ snappy_main (int argc, char *argv[])
   gst_bus_add_watch (engine->bus, bus_call, ui);
   gst_object_unref (engine->bus);
 
-  /* Get uri to load */
-  uri = g_list_first (uri_list)->data;
-
-  /* Load engine and start interface */
-  engine_load_uri (engine, uri);
-  interface_start (ui, uri);
-
-  /* Load subtitle file if available */
-  if (suburi != NULL) {
-    suburi = clean_uri (suburi);
-    set_subtitle_uri (engine, suburi);
-  }
-
-  /* Start playing */
-  change_state (engine, "Paused");
-  change_state (engine, "Playing");
-
-#ifdef ENABLE_DBUS
-  /* Start MPRIS Dbus object */
-  mp_obj = g_new (SnappyMP, 1);
-  mp_obj->engine = engine;
-  mp_obj->ui = ui;
-  load_dlna (mp_obj);
-#endif
-
-  /* Main loop */
-  clutter_main ();
-
-  /* Close snappy */
-  close_down (ui, engine);
-#ifdef ENABLE_DBUS
-  close_dlna (mp_obj);
-#endif
-
-quit:
-  g_list_free (uri_list);
-  g_option_context_free (context);
-
-  return ret;
+  return ui;
 }
diff --git a/src/snra-client.c b/src/snra-client.c
index df8d51d..edce8ea 100644
--- a/src/snra-client.c
+++ b/src/snra-client.c
@@ -43,6 +43,7 @@
 #include <src/snra-json.h>
 
 #include "snra-client.h"
+#include "user_interface.h"
 
 #define DISABLED_STATE GST_STATE_PAUSED
 
@@ -66,7 +67,6 @@ static void snra_client_dispose (GObject * object);
 static void search_for_server (SnraClient * client);
 static void connect_to_server (SnraClient * client, const gchar * server,
     int port);
-static void construct_player (SnraClient * client);
 
 static gboolean
 try_reconnect (SnraClient * client)
@@ -96,8 +96,7 @@ handle_connection_closed_cb (G_GNUC_UNUSED SoupSession * session,
   }
   client->was_connected = FALSE;
 
-  if (client->player)
-    gst_element_set_state (client->player, GST_STATE_READY);
+  engine_stop (client->ui->engine);
   if (client->timeout == 0) {
     client->timeout =
         g_timeout_add_seconds (1, (GSourceFunc) try_reconnect, client);
@@ -121,14 +120,10 @@ handle_enrol_message (SnraClient * client, GstStructure * s)
   cur_time = (GstClockTime) (tmp);
 
   if (snra_json_structure_get_double (s, "volume-level", &new_vol)) {
-    if (client->player == NULL)
-      construct_player (client);
+    if (client->ui == NULL)
+      client->ui = snappy_construct ();
 
-    if (client->player) {
-      //g_print ("New volume %g\n", new_vol);
-      g_object_set (G_OBJECT (client->player), "volume", new_vol,
-          "mute", (gboolean) (new_vol == 0.0), NULL);
-    }
+    engine_volume (client->ui->engine, new_vol);
   }
 
   snra_json_structure_get_boolean (s, "enabled", &client->enabled);
@@ -209,27 +204,6 @@ on_error_msg (G_GNUC_UNUSED GstBus * bus, GstMessage * msg,
 }
 
 static void
-construct_player (SnraClient * client)
-{
-  GstBus *bus;
-
-  if (GST_CHECK_VERSION (0, 11, 1))
-    client->player = gst_element_factory_make ("playbin", NULL);
-  else
-    client->player = gst_element_factory_make ("playbin2", NULL);
-
-  if (client->player == NULL) {
-    g_warning ("Failed to construct playbin");
-    return;
-  }
-  bus = gst_element_get_bus (GST_ELEMENT (client->player));
-  gst_bus_add_signal_watch (bus);
-  g_signal_connect (bus, "message::eos", (GCallback) (on_eos_msg), client);
-  g_signal_connect (bus, "message::error", (GCallback) (on_error_msg), client);
-  gst_object_unref (bus);
-}
-
-static void
 handle_set_media_message (SnraClient * client, GstStructure * s)
 {
   const gchar *protocol, *path;
@@ -256,36 +230,40 @@ handle_set_media_message (SnraClient * client, GstStructure * s)
 
   base_time = (GstClockTime) (tmp);
 
-  if (client->player == NULL) {
-    construct_player (client);
-    if (client->player == NULL)
-      return;
-  } else {
-    gst_element_set_state (client->player, GST_STATE_NULL);
-  }
-
   uri =
       g_strdup_printf ("%s://%s:%d%s", protocol, client->connected_server, port,
       path);
   g_print ("Playing URI %s base_time %" GST_TIME_FORMAT "\n", uri,
       GST_TIME_ARGS (base_time));
-  g_object_set (client->player, "uri", uri, NULL);
+ 
+  if (client->state == GST_STATE_NULL) {
+    client->ui = snappy_construct ();
+    engine_load_uri (client->ui->engine, uri);
+    interface_start (client->ui, uri);
+    client->state = GST_STATE_READY;
+  } else {
+    engine_stop (client->ui->engine);
+  }
+
+  engine_open_uri (client->ui->engine, uri);
   g_free (uri);
 
-  gst_element_set_start_time (client->player, GST_CLOCK_TIME_NONE);
-  gst_element_set_base_time (client->player, base_time);
-  gst_pipeline_use_clock (GST_PIPELINE (client->player), client->net_clock);
+  gst_element_set_start_time (client->ui->engine->player, GST_CLOCK_TIME_NONE);
+  gst_element_set_base_time (client->ui->engine->player, base_time);
+  gst_pipeline_use_clock (GST_PIPELINE (client->ui->engine->player),
+      client->net_clock);
 
   if (client->enabled) {
-    if (paused)
+    if (paused) {
       client->state = GST_STATE_PAUSED;
-    else
+      engine_stop (client->ui->engine);
+    } else {
       client->state = GST_STATE_PLAYING;
+      engine_play (client->ui->engine);
+    }
   } else {
     client->state = DISABLED_STATE;
   }
-
-  gst_element_set_state (client->player, client->state);
 }
 
 static void
@@ -300,17 +278,20 @@ handle_play_message (SnraClient * client, GstStructure * s)
 
   client->paused = FALSE;
 
-  if (client->player) {
+  if (client->ui) {
     GstClockTime stream_time =
         gst_clock_get_time (client->net_clock) - base_time;
     g_print ("Playing base_time %" GST_TIME_FORMAT " (offset %" GST_TIME_FORMAT
         ")\n", GST_TIME_ARGS (base_time), GST_TIME_ARGS (stream_time));
-    gst_element_set_base_time (GST_ELEMENT (client->player), base_time);
-    if (client->enabled == FALSE)
+    gst_element_set_base_time (GST_ELEMENT (client->ui->engine->player),
+        base_time);
+    if (client->enabled == FALSE) {
       client->state = DISABLED_STATE;
-    else
+      engine_stop (client->ui->engine);
+    } else {
       client->state = GST_STATE_PLAYING;
-    gst_element_set_state (GST_ELEMENT (client->player), client->state);
+      engine_play (client->ui->engine);
+    }
   }
 }
 
@@ -322,13 +303,8 @@ handle_set_volume_message (SnraClient * client, GstStructure * s)
   if (!snra_json_structure_get_double (s, "level", &new_vol))
     return;
 
-  if (client->player == NULL)
-    construct_player (client);
-
-  if (client->player) {
-    // g_print ("New volume %g\n", new_vol);
-    g_object_set (G_OBJECT (client->player), "volume", new_vol,
-        "mute", (gboolean) (new_vol == 0.0), NULL);
+  if (client->ui) {
+    engine_volume (client->ui->engine, new_vol);
   }
 }
 
@@ -340,13 +316,13 @@ handle_set_client_message (SnraClient * client, GstStructure * s)
 
   if (client->enabled == FALSE)
     client->state = DISABLED_STATE;
-  else if (client->paused)
+  else if (client->paused) {
     client->state = GST_STATE_PAUSED;
-  else
+    engine_stop (client->ui->engine);
+  } else {
     client->state = GST_STATE_PLAYING;
-
-  if (client->player)
-    gst_element_set_state (GST_ELEMENT (client->player), client->state);
+    engine_play (client->ui->engine);
+  }
 }
 
 static void
@@ -390,23 +366,30 @@ handle_received_chunk (G_GNUC_UNUSED SoupMessage * msg, SoupBuffer * chunk,
       return;
     }
 
-    if (g_str_equal (msg_type, "enrol"))
+    if (g_str_equal (msg_type, "enrol")) {
+      g_print ("enrol\n");
       handle_enrol_message (client, s);
-    else if (g_str_equal (msg_type, "set-media"))
+    } else if (g_str_equal (msg_type, "set-media")) {
+      g_print ("set-media\n");
       handle_set_media_message (client, s);
-    else if (g_str_equal (msg_type, "play"))
+    } else if (g_str_equal (msg_type, "play")) {
+      g_print ("play\n");
       handle_play_message (client, s);
-    else if (g_str_equal (msg_type, "pause")) {
+    } else if (g_str_equal (msg_type, "pause")) {
+      g_print ("pause\n");
       client->paused = TRUE;
-      if (client->enabled == FALSE)
+      if (client->enabled == FALSE) {
         client->state = DISABLED_STATE;
-      else
+        engine_stop (client->ui->engine);
+      } else {
         client->state = GST_STATE_PAUSED;
-      if (client->player)
-        gst_element_set_state (GST_ELEMENT (client->player), client->state);
+        engine_stop (client->ui->engine);
+      }
     } else if (g_str_equal (msg_type, "volume")) {
+      g_print ("volume\n");
       handle_set_volume_message (client, s);
     } else if (g_str_equal (msg_type, "client-setting")) {
+      g_print ("client-setting\n");
       handle_set_client_message (client, s);
     } else {
       g_print ("Unhandled event of type %s\n", msg_type);
@@ -443,6 +426,7 @@ snra_client_init (SnraClient * client)
   client->soup = soup_session_async_new ();
   client->server_port = 5457;
   client->state = GST_STATE_NULL;
+  client->ui = NULL;
 }
 
 static void
@@ -492,11 +476,10 @@ snra_client_finalize (GObject * object)
     g_object_unref (client->soup);
   if (client->json)
     g_object_unref (client->json);
-  if (client->player) {
-    GstBus *bus = gst_element_get_bus (client->player);
+  if (client->ui) {
+    GstBus *bus = gst_element_get_bus (client->ui->engine->player);
     gst_bus_remove_signal_watch (bus);
     gst_object_unref (bus);
-    gst_object_unref (client->player);
   }
 
   g_free (client->server_host);
@@ -512,8 +495,8 @@ snra_client_dispose (GObject * object)
 
   if (client->soup)
     soup_session_abort (client->soup);
-  if (client->player)
-    gst_element_set_state (client->player, GST_STATE_NULL);
+  if (client->ui)
+    engine_stop (client->ui->engine);
 
   G_OBJECT_CLASS (snra_client_parent_class)->dispose (object);
 }
diff --git a/src/snra-client.h b/src/snra-client.h
index e98f1e2..6dce2f3 100644
--- a/src/snra-client.h
+++ b/src/snra-client.h
@@ -13,6 +13,7 @@
 #include <avahi-glib/glib-watch.h>
 
 #include <src/snra-types.h>
+#include <src/user_interface.h>
 
 G_BEGIN_DECLS
 
@@ -35,7 +36,7 @@ struct _SnraClient
   SoupSession *soup;
   JsonParser *json;
 
-  GstElement *player;
+  // GstElement *player;
 
   guint timeout;
 
@@ -47,6 +48,8 @@ struct _SnraClient
   AvahiGLibPoll *glib_poll;
   AvahiClient *avahi_client;
   AvahiServiceBrowser *avahi_sb;
+
+  UserInterface *ui;
 };
 
 struct _SnraClientClass



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