client: wait until the TEARDOWN response is sent to close the connection
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
index b0a6d90..799b42e 100644 (file)
@@ -174,6 +174,7 @@ static void
 gst_rtsp_client_init (GstRTSPClient * client)
 {
   client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
+  client->teardown_response_seq = 0;
 }
 
 static void
@@ -214,7 +215,7 @@ gst_rtsp_client_finalize (GObject * obj)
 
   GST_INFO ("finalize client %p", client);
 
-  if (client->watchid)
+  if (client->watch)
     g_source_destroy ((GSource *) client->watch);
 
   client_cleanup_sessions (client);
@@ -300,7 +301,7 @@ gst_rtsp_client_new (void)
 
 static void
 send_response (GstRTSPClient * client, GstRTSPSession * session,
-    GstRTSPMessage * response)
+    GstRTSPMessage * response, guint * id)
 {
   gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
       "GStreamer RTSP server");
@@ -310,23 +311,15 @@ send_response (GstRTSPClient * client, GstRTSPSession * session,
 
   /* add the new session header for new session ids */
   if (session) {
-    gchar *str;
-
-    if (session->timeout != 60)
-      str =
-          g_strdup_printf ("%s; timeout=%d", session->sessionid,
-          session->timeout);
-    else
-      str = g_strdup (session->sessionid);
-
-    gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
+    gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
+        gst_rtsp_session_get_header (session));
   }
 
   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
     gst_rtsp_message_dump (response);
   }
 
-  gst_rtsp_watch_send_message (client->watch, response, NULL);
+  gst_rtsp_watch_send_message (client->watch, response, id);
   gst_rtsp_message_unset (response);
 }
 
@@ -337,7 +330,7 @@ send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
   gst_rtsp_message_init_response (state->response, code,
       gst_rtsp_status_as_text (code), state->request);
 
-  send_response (client, NULL, state->response);
+  send_response (client, NULL, state->response, NULL);
 }
 
 static void
@@ -352,7 +345,7 @@ handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
     gst_rtsp_auth_setup_auth (auth, client, 0, state);
   }
 
-  send_response (client, state->session, state->response);
+  send_response (client, state->session, state->response, NULL);
 }
 
 
@@ -611,14 +604,15 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
       "close");
 
-  send_response (client, session, state->response);
+  /* send the response and store the seq number so we can wait until it's
+   * written to the client to close the connection */
+  send_response (client, session, state->response,
+      &client->teardown_response_seq);
 
   /* we emit the signal before closing the connection */
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
       0, state);
 
-  close_connection (client);
-
   return TRUE;
 
   /* ERRORS */
@@ -654,7 +648,7 @@ handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
     if (res != GST_RTSP_OK)
       goto bad_request;
 
-    send_response (client, state->session, state->response);
+    send_response (client, state->session, state->response, NULL);
   }
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
@@ -690,7 +684,7 @@ handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
     if (res != GST_RTSP_OK)
       goto bad_request;
 
-    send_response (client, state->session, state->response);
+    send_response (client, state->session, state->response, NULL);
   }
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
@@ -739,7 +733,7 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
   gst_rtsp_message_init_response (state->response, code,
       gst_rtsp_status_as_text (code), state->request);
 
-  send_response (client, session, state->response);
+  send_response (client, session, state->response, NULL);
 
   /* the state is now READY */
   media->state = GST_RTSP_STATE_READY;
@@ -861,7 +855,7 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
   str = gst_rtsp_media_get_range_string (media->media, TRUE);
   gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
 
-  send_response (client, session, state->response);
+  send_response (client, session, state->response, NULL);
 
   /* start playing after sending the request */
   gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
@@ -913,7 +907,7 @@ parse_transport (const char *transport, GstRTSPLowerTrans supported,
   res = FALSE;
   gst_rtsp_transport_init (tr);
 
-  GST_WARNING ("parsing transports %s", transport);
+  GST_DEBUG ("parsing transports %s", transport);
 
   transports = g_strsplit (transport, ",", 0);
 
@@ -951,7 +945,8 @@ parse_transport (const char *transport, GstRTSPLowerTrans supported,
 }
 
 static gboolean
-handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
+handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
+    GstRTSPMessage * request)
 {
   gchar *blocksize_str;
   gboolean ret = TRUE;
@@ -973,7 +968,7 @@ handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
 
       if (blocksize > G_MAXUINT)
         blocksize = G_MAXUINT;
-      gst_rtsp_media_set_mtu (media, blocksize);
+      gst_rtsp_stream_set_mtu (stream, blocksize);
     }
   }
   return ret;
@@ -986,13 +981,17 @@ configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
   /* we have a valid transport now, set the destination of the client. */
   if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
     if (ct->destination == NULL || !client->use_client_settings) {
+      GstRTSPAddress *addr;
+
+      addr = gst_rtsp_stream_get_address (state->stream);
+      if (addr == NULL)
+        goto no_address;
+
       g_free (ct->destination);
-      ct->destination = gst_rtsp_media_get_multicast_group (state->media);
-    }
-    /* reset ttl and port if client settings are not allowed */
-    if (!client->use_client_settings) {
-      ct->port = state->stream->server_port;
-      ct->ttl = 0;
+      ct->destination = g_strdup (addr->address);
+      ct->port.min = addr->port;
+      ct->port.max = addr->port + addr->n_ports - 1;
+      ct->ttl = addr->ttl;
     }
   } else {
     GstRTSPUrl *url;
@@ -1010,6 +1009,13 @@ configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
     }
   }
   return TRUE;
+
+  /* ERRORS */
+no_address:
+  {
+    GST_ERROR_OBJECT (client, "failed to acquire address for stream");
+    return FALSE;
+  }
 }
 
 static GstRTSPTransport *
@@ -1146,12 +1152,13 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
 
   state->stream = stream;
 
-  /* FIXME set only on this stream */
-  if (!handle_blocksize (media, state->request))
+  /* set blocksize on this stream */
+  if (!handle_blocksize (media, stream, state->request))
     goto invalid_blocksize;
 
   /* update the client transport */
-  configure_client_transport (client, state, ct);
+  if (!configure_client_transport (client, state, ct))
+    goto unsupported_client_transport;
 
   /* set in the session media transport */
   trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
@@ -1174,7 +1181,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
       trans_str);
   g_free (trans_str);
 
-  send_response (client, session, state->response);
+  send_response (client, session, state->response, NULL);
 
   /* update the state */
   switch (sessmedia->state) {
@@ -1214,6 +1221,13 @@ invalid_blocksize:
     gst_rtsp_transport_free (ct);
     return FALSE;
   }
+unsupported_client_transport:
+  {
+    send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
+    g_object_unref (session);
+    gst_rtsp_transport_free (ct);
+    return FALSE;
+  }
 no_transport:
   {
     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
@@ -1245,7 +1259,6 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   GstSDPMessage *sdp;
   GstSDPInfo info;
   const gchar *proto;
-  GstRTSPLowerTrans protocols;
 
   gst_sdp_message_new (&sdp);
 
@@ -1268,11 +1281,7 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
   gst_sdp_message_add_attribute (sdp, "control", "*");
 
   info.server_proto = proto;
-  protocols = gst_rtsp_media_get_protocols (media);
-  if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
-    info.server_ip = gst_rtsp_media_get_multicast_group (media);
-  else
-    info.server_ip = g_strdup (client->server_ip);
+  info.server_ip = g_strdup (client->server_ip);
 
   /* create an SDP for the media object */
   if (!gst_rtsp_sdp_from_media (sdp, &info, media))
@@ -1361,7 +1370,7 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
   gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
   gst_sdp_message_free (sdp);
 
-  send_response (client, state->session, state->response);
+  send_response (client, state->session, state->response, NULL);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
       0, state);
@@ -1403,7 +1412,7 @@ handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
   g_free (str);
 
-  send_response (client, state->session, state->response);
+  send_response (client, state->session, state->response, NULL);
 
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
       0, state);
@@ -1884,11 +1893,13 @@ message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
 static GstRTSPResult
 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
 {
-  /* GstRTSPClient *client; */
-
-  /* client = GST_RTSP_CLIENT (user_data); */
+  GstRTSPClient *client;
 
-  /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
+  client = GST_RTSP_CLIENT (user_data);
+  if (client->teardown_response_seq && client->teardown_response_seq == cseq) {
+    client->teardown_response_seq = 0;
+    close_connection (client);
+  }
 
   return GST_RTSP_OK;
 }
@@ -2089,20 +2100,17 @@ static void
 client_watch_notify (GstRTSPClient * client)
 {
   GST_INFO ("client %p: watch destroyed", client);
-  client->watchid = 0;
   client->watch = NULL;
   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
   g_object_unref (client);
 }
 
 static gboolean
-attach_client (GstRTSPClient * client, GSocket * socket,
+setup_client (GstRTSPClient * client, GSocket * socket,
     GstRTSPConnection * conn, GError ** error)
 {
   GSocket *read_socket;
   GSocketAddress *address;
-  GSource *source;
-  GMainContext *context;
   GstRTSPUrl *url;
 
   read_socket = gst_rtsp_connection_get_read_socket (conn);
@@ -2132,21 +2140,6 @@ attach_client (GstRTSPClient * client, GSocket * socket,
 
   client->connection = conn;
 
-  /* create watch for the connection and attach */
-  client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
-      g_object_ref (client), (GDestroyNotify) client_watch_notify);
-
-  /* find the context to add the watch */
-  if ((source = g_main_current_source ()))
-    context = g_source_get_context (source);
-  else
-    context = NULL;
-
-  GST_INFO ("attaching to context %p", context);
-
-  client->watchid = gst_rtsp_watch_attach (client->watch, context);
-  gst_rtsp_watch_unref (client->watch);
-
   return TRUE;
 
   /* ERRORS */
@@ -2158,12 +2151,13 @@ no_address:
 }
 
 /**
- * gst_rtsp_client_create_from_socket:
+ * gst_rtsp_client_use_socket:
  * @client: a #GstRTSPClient
  * @socket: a #GSocket
  * @ip: the IP address of the remote client
  * @port: the port used by the other end
- * @initial_buffer: any initial data that was already read from the socket
+ * @initial_buffer: any zero terminated initial data that was already read from
+ *     the socket
  * @error: a #GError
  *
  * Take an existing network socket and use it for an RTSP connection.
@@ -2171,7 +2165,7 @@ no_address:
  * Returns: %TRUE on success.
  */
 gboolean
-gst_rtsp_client_create_from_socket (GstRTSPClient * client, GSocket * socket,
+gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
     const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
 {
   GstRTSPConnection *conn;
@@ -2180,7 +2174,7 @@ gst_rtsp_client_create_from_socket (GstRTSPClient * client, GSocket * socket,
   GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
           initial_buffer, &conn), no_connection);
 
-  return attach_client (client, socket, conn, error);
+  return setup_client (client, socket, conn, error);
 
   /* ERRORS */
 no_connection:
@@ -2197,14 +2191,12 @@ no_connection:
  * gst_rtsp_client_accept:
  * @client: a #GstRTSPClient
  * @socket: a #GSocket
+ * @context: the context to run in
  * @cancellable: a #GCancellable
  * @error: a #GError
  *
  * Accept a new connection for @client on @socket.
  *
- * This function should be called when the client properties and urls are fully
- * configured and the client is ready to start.
- *
  * Returns: %TRUE if the client could be accepted.
  */
 gboolean
@@ -2218,7 +2210,7 @@ gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
   GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
       accept_failed);
 
-  return attach_client (client, socket, conn, error);
+  return setup_client (client, socket, conn, error);
 
   /* ERRORS */
 accept_failed:
@@ -2230,3 +2222,36 @@ accept_failed:
     return FALSE;
   }
 }
+
+/**
+ * gst_rtsp_client_attach:
+ * @client: a #GstRTSPClient
+ * @context: (allow-none): a #GMainContext
+ *
+ * Attaches @client to @context. When the mainloop for @context is run, the
+ * client will be dispatched. When @context is NULL, the default context will be
+ * used).
+ *
+ * This function should be called when the client properties and urls are fully
+ * configured and the client is ready to start.
+ *
+ * Returns: the ID (greater than 0) for the source within the GMainContext.
+ */
+guint
+gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
+{
+  guint res;
+
+  g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
+  g_return_val_if_fail (client->watch == NULL, 0);
+
+  /* create watch for the connection and attach */
+  client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
+      g_object_ref (client), (GDestroyNotify) client_watch_notify);
+
+  GST_INFO ("attaching to context %p", context);
+  res = gst_rtsp_watch_attach (client->watch, context);
+  gst_rtsp_watch_unref (client->watch);
+
+  return res;
+}