Ejemplo n.º 1
0
static void
handle_controller_client_setting_message (SnraClient * client, GstStructure * s)
{
  gint64 client_id;
  gboolean enabled;
  gsize i;

  if (!client->player_info)
    return;

  if (!snra_json_structure_get_int64 (s, "client-id", &client_id))
    return;

  if (!snra_json_structure_get_boolean (s, "enabled", &enabled))
    return;

  for (i = 0; i < client->player_info->len; i++) {
    SnraPlayerInfo *info;
    info = &g_array_index (client->player_info, SnraPlayerInfo, i);
    if (info->id == (guint) client_id) {
      info->enabled = enabled;
      g_signal_emit (client, signals[SIGNAL_CLIENT_SETTING_CHANGED], 0,
          info->id, info->enabled);
      return;
    }
  }
}
Ejemplo n.º 2
0
static void
handle_set_media_message (SnraClient * client, GstStructure * s)
{
  const gchar *protocol, *path;
  int port;
  GstClockTime base_time;
  gint64 tmp;
  gchar *uri;
  gboolean paused;

  protocol = gst_structure_get_string (s, "resource-protocol");
  path = gst_structure_get_string (s, "resource-path");

  if (protocol == NULL || path == NULL)
    return;                     /* Invalid message */

  if (!snra_json_structure_get_int (s, "resource-port", &port))
    return;

  if (!snra_json_structure_get_int64 (s, "base-time", &tmp))
    return;                     /* Invalid message */

  if (!snra_json_structure_get_boolean (s, "paused", &paused))
    return;

  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);
  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);

  if (client->enabled) {
    if (paused)
      client->state = GST_STATE_PAUSED;
    else
      client->state = GST_STATE_PLAYING;
  } else {
    client->state = DISABLED_STATE;
  }

  gst_element_set_state (client->player, client->state);
}
Ejemplo n.º 3
0
static void
handle_controller_enrol_message (SnraClient * client, GstStructure * s)
{
  if (snra_json_structure_get_double (s, "volume-level", &client->volume))
    g_object_notify (G_OBJECT (client), "volume");

  if (!(client->flags & SNRA_CLIENT_PLAYER)) {
    if (snra_json_structure_get_boolean (s, "paused", &client->paused))
      g_object_notify (G_OBJECT (client), "paused");
  }
}
Ejemplo n.º 4
0
static void
handle_set_client_message (SnraClient * client, GstStructure * s)
{
  if (!snra_json_structure_get_boolean (s, "enabled", &client->enabled))
    return;

  if (client->enabled == FALSE)
    client->state = DISABLED_STATE;
  else if (client->paused)
    client->state = GST_STATE_PAUSED;
  else
    client->state = GST_STATE_PLAYING;

  if (client->player)
    gst_element_set_state (GST_ELEMENT (client->player), client->state);
}
Ejemplo n.º 5
0
static void
handle_player_set_media_message (SnraClient * client, GstStructure * s)
{
  const gchar *protocol, *path, *language;
  int port;
  gint64 tmp;

  protocol = gst_structure_get_string (s, "resource-protocol");
  path = gst_structure_get_string (s, "resource-path");

  if (protocol == NULL || path == NULL)
    return;                     /* Invalid message */

  if (!snra_json_structure_get_int (s, "resource-port", &port))
    return;

  if (!snra_json_structure_get_int64 (s, "base-time", &tmp))
    return;                     /* Invalid message */
  client->base_time = (GstClockTime) (tmp);

  if (!snra_json_structure_get_int64 (s, "position", &tmp))
    return;                     /* Invalid message */
  client->position = (GstClockTime) (tmp);

  if (!snra_json_structure_get_boolean (s, "paused", &client->paused))
    return;

  g_free (client->language);
  language = gst_structure_get_string (s, "language");
  client->language = g_strdup (language ? language : "en");

  g_free (client->uri);
  client->uri = g_strdup_printf ("%s://%s:%d%s", protocol,
      client->connected_server, port, path);

  if (client->enabled)
    set_media (client);

  g_object_notify (G_OBJECT (client), "language");
  g_object_notify (G_OBJECT (client), "media-uri");
}
Ejemplo n.º 6
0
static void
handle_player_set_client_message (SnraClient * client, GstStructure * s)
{
  gboolean enabled;

  if (!snra_json_structure_get_boolean (s, "enabled", &enabled))
    return;

  if (enabled == client->enabled)
    return;
  client->enabled = enabled;

  if (!client->player)
    return;

  if (client->enabled && client->uri)
    set_media (client);
  else
    gst_element_set_state (GST_ELEMENT (client->player), GST_STATE_NULL);

  g_object_notify (G_OBJECT (client), "enabled");
}
Ejemplo n.º 7
0
static void
handle_enrol_message (SnraClient * client, GstStructure * s)
{
  int clock_port;
  gint64 tmp;
  GstClockTime cur_time;
  gchar *server_ip_str = NULL;
  gdouble new_vol;

  if (!snra_json_structure_get_int (s, "clock-port", &clock_port))
    return;                     /* Invalid message */

  if (!snra_json_structure_get_int64 (s, "current-time", &tmp))
    return;                     /* Invalid message */
  cur_time = (GstClockTime) (tmp);

  if (snra_json_structure_get_double (s, "volume-level", &new_vol)) {
    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);
    }
  }

  snra_json_structure_get_boolean (s, "enabled", &client->enabled);
  snra_json_structure_get_boolean (s, "paused", &client->paused);

#if GLIB_CHECK_VERSION(2,22,0)
  {
    GResolver *resolver = g_resolver_get_default ();
    GList *names;

    if (resolver == NULL)
      return;

    names =
        g_resolver_lookup_by_name (resolver, client->connected_server, NULL,
        NULL);
    if (names) {
      server_ip_str = g_inet_address_to_string ((GInetAddress *) (names->data));
      g_resolver_free_addresses (names);
    }
    g_object_unref (resolver);
  }
#else
  {
    struct addrinfo *names = NULL;
    if (getaddrinfo (client->connected_server, NULL, NULL, &names))
      return;
    if (names) {
      char hbuf[NI_MAXHOST];
      if (getnameinfo (names->ai_addr, names->ai_addrlen,
              hbuf, sizeof (hbuf), NULL, 0, NI_NUMERICHOST) == 0) {
        server_ip_str = g_strdup (hbuf);
      }
      freeaddrinfo (names);
    }
  }
#endif
  if (server_ip_str) {
    g_print ("Creating net clock at %s:%d time %" GST_TIME_FORMAT "\n",
        server_ip_str, clock_port, GST_TIME_ARGS (cur_time));
    if (client->net_clock)
      gst_object_unref (client->net_clock);
    client->net_clock = gst_net_client_clock_new ("net_clock", server_ip_str,
        clock_port, cur_time);
    g_free (server_ip_str);
  }
}
Ejemplo n.º 8
0
static void
handle_player_info (G_GNUC_UNUSED SoupSession *session, SoupMessage *msg,
    SnraClient *client)
{
  SoupBuffer *buffer;

  if (msg->status_code < 200 || msg->status_code >= 300)
    return;

  buffer = soup_message_body_flatten (msg->response_body);
  if (json_parser_load_from_data (client->json, buffer->data, buffer->length,
          NULL)) {
    const GValue *v1;
    GArray *player_info = NULL;
    gsize i;
    JsonNode *root = json_parser_get_root (client->json);
    GstStructure *s1 = snra_json_to_gst_structure (root);

    if (s1 == NULL)
      return;                   /* Invalid chunk */

    v1 = gst_structure_get_value (s1, "player-clients");
    if (!GST_VALUE_HOLDS_ARRAY (v1))
      goto failed;

    player_info = g_array_sized_new (TRUE, TRUE,
          sizeof (SnraPlayerInfo), gst_value_array_get_size (v1));

    for (i = 0; i < gst_value_array_get_size (v1); i++) {
      SnraPlayerInfo info;
      const GValue *v2 = gst_value_array_get_value (v1, i);
      const GstStructure *s2;
      gint64 client_id;

      if (!GST_VALUE_HOLDS_STRUCTURE (v2))
        goto failed;

      s2 = gst_value_get_structure (v2);
      if (!snra_json_structure_get_int64 (s2, "client-id", &client_id))
        goto failed;
      info.id = client_id;

      if (!snra_json_structure_get_boolean (s2, "enabled", &info.enabled))
        goto failed;

      if (!snra_json_structure_get_double (s2, "volume", &info.volume))
        goto failed;

      if (!(info.host = g_strdup (gst_structure_get_string (s2, "host"))))
        goto failed;

      g_array_append_val (player_info, info);
    }

    free_player_info (client->player_info);
    client->player_info = player_info;
    player_info = NULL;

    g_signal_emit (client, signals[SIGNAL_PLAYER_INFO_CHANGED], 0);

failed:
    if (player_info)
      free_player_info (player_info);
    gst_structure_free (s1);
  }
}