client: add vmethod to configure media and streams
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
index 76f8c8c..0822727 100644 (file)
@@ -127,6 +127,8 @@ static void client_session_finalized (GstRTSPClient * client,
     GstRTSPSession * session);
 static void unlink_session_transports (GstRTSPClient * client,
     GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
+static gboolean default_configure_client_media (GstRTSPClient * client,
+    GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
 static gboolean default_configure_client_transport (GstRTSPClient * client,
     GstRTSPContext * ctx, GstRTSPTransport * ct);
 static GstRTSPResult default_params_set (GstRTSPClient * client,
@@ -152,6 +154,7 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass)
   gobject_class->finalize = gst_rtsp_client_finalize;
 
   klass->create_sdp = create_sdp;
+  klass->configure_client_media = default_configure_client_media;
   klass->configure_client_transport = default_configure_client_transport;
   klass->params_set = default_params_set;
   klass->params_get = default_params_get;
@@ -1041,7 +1044,7 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
   GstRTSPUrl *uri;
   GString *rtpinfo;
   guint n_streams, i, infocount;
-  gchar *str, *base_url;
+  gchar *str;
   GstRTSPTimeRange *range;
   GstRTSPResult res;
   GstRTSPState rtspstate;
@@ -1074,6 +1077,10 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
     goto invalid_state;
 
+  /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
+  if (!gst_rtsp_media_unsuspend (media))
+    goto unsuspend_failed;
+
   /* parse the range header if we have one */
   res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
   if (res == GST_RTSP_OK) {
@@ -1088,8 +1095,6 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
   /* grab RTPInfo from the payloaders now */
   rtpinfo = g_string_new ("");
 
-  base_url = make_base_url (client, uri, path);
-
   n_streams = gst_rtsp_media_n_streams (media);
   for (i = 0, infocount = 0; i < n_streams; i++) {
     GstRTSPStreamTransport *trans;
@@ -1112,22 +1117,23 @@ handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
 
     stream = gst_rtsp_stream_transport_get_stream (trans);
     if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
-      gchar *control;
+      const GstRTSPUrl *url;
+      gchar *url_str;
 
       if (infocount > 0)
         g_string_append (rtpinfo, ", ");
 
-      control = gst_rtsp_stream_get_control (stream);
-      g_string_append_printf (rtpinfo, "url=%s%s;seq=%u;rtptime=%u",
-          base_url, control, seq, rtptime);
-      g_free (control);
+      url = gst_rtsp_stream_transport_get_url (trans);
+      url_str = gst_rtsp_url_get_request_uri (url);
+      g_string_append_printf (rtpinfo, "url=%s;seq=%u;rtptime=%u",
+          url_str, seq, rtptime);
+      g_free (url_str);
 
       infocount++;
     } else {
       GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
     }
   }
-  g_free (base_url);
   g_free (path);
 
   /* construct the response now */
@@ -1194,6 +1200,13 @@ invalid_state:
     g_free (path);
     return FALSE;
   }
+unsuspend_failed:
+  {
+    GST_ERROR ("client %p: unsuspend failed", client);
+    send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
+    g_free (path);
+    return FALSE;
+  }
 }
 
 static void
@@ -1255,11 +1268,11 @@ parse_transport (const char *transport, GstRTSPLowerTrans supported,
 }
 
 static gboolean
-handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
-    GstRTSPMessage * request)
+default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
+    GstRTSPStream * stream, GstRTSPContext * ctx)
 {
+  GstRTSPMessage *request = ctx->request;
   gchar *blocksize_str;
-  gboolean ret = TRUE;
 
   if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
           &blocksize_str, 0) == GST_RTSP_OK) {
@@ -1267,21 +1280,29 @@ handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
     gchar *end;
 
     blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
-    if (end == blocksize_str) {
-      GST_ERROR ("failed to parse blocksize");
-      ret = FALSE;
-    } else {
-      /* we don't want to change the mtu when this media
-       * can be shared because it impacts other clients */
-      if (gst_rtsp_media_is_shared (media))
-        return TRUE;
-
-      if (blocksize > G_MAXUINT)
-        blocksize = G_MAXUINT;
-      gst_rtsp_stream_set_mtu (stream, blocksize);
-    }
+    if (end == blocksize_str)
+      goto parse_failed;
+
+    /* we don't want to change the mtu when this media
+     * can be shared because it impacts other clients */
+    if (gst_rtsp_media_is_shared (media))
+      goto done;
+
+    if (blocksize > G_MAXUINT)
+      blocksize = G_MAXUINT;
+
+    gst_rtsp_stream_set_mtu (stream, blocksize);
+  }
+done:
+  return TRUE;
+
+  /* ERRORS */
+parse_failed:
+  {
+    GST_ERROR_OBJECT (client, "failed to parse blocksize");
+    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    return FALSE;
   }
-  return ret;
 }
 
 static gboolean
@@ -1466,7 +1487,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (path[matched] == '\0')
     goto control_not_found;
 
-  /* path is what matched. We can modify the parsed uri in place */
+  /* path is what matched. */
   path[matched] = '\0';
   /* control is remainder */
   control = &path[matched + 1];
@@ -1508,9 +1529,8 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
 
   ctx->sessmedia = sessmedia;
 
-  /* set blocksize on this stream */
-  if (!handle_blocksize (media, stream, ctx->request))
-    goto invalid_blocksize;
+  if (!klass->configure_client_media (client, media, stream, ctx))
+    goto configure_media_failed_no_reply;
 
   gst_rtsp_transport_new (&ct);
 
@@ -1528,6 +1548,10 @@ handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
   /* set in the session media transport */
   trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
 
+  /* configure the url used to set this transport, this we will use when
+   * generating the response for the PLAY request */
+  gst_rtsp_stream_transport_set_url (trans, uri);
+
   /* configure keepalive for this transport */
   gst_rtsp_stream_transport_set_keepalive (trans,
       (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
@@ -1635,12 +1659,12 @@ sessmedia_unavailable:
     g_free (path);
     return FALSE;
   }
-invalid_blocksize:
+configure_media_failed_no_reply:
   {
-    GST_ERROR ("client %p: invalid blocksize", client);
-    send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
+    GST_ERROR ("client %p: configure_media failed", client);
     g_object_unref (session);
     g_free (path);
+    /* error reply is already sent */
     return FALSE;
   }
 unsupported_transports:
@@ -1755,6 +1779,8 @@ handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
   if (!(sdp = klass->create_sdp (client, media)))
     goto no_sdp;
 
+  /* we suspend after the describe */
+  gst_rtsp_media_suspend (media);
   g_object_unref (media);
 
   gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
@@ -1921,10 +1947,12 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request)
     gst_rtsp_message_dump (request);
   }
 
-  GST_INFO ("client %p: received a request", client);
-
   gst_rtsp_message_parse_request (request, &method, &uristr, &version);
 
+  GST_INFO ("client %p: received a request %s %s %s", client,
+      gst_rtsp_method_as_text (method), uristr,
+      gst_rtsp_version_as_text (version));
+
   /* we can only handle 1.0 requests */
   if (version != GST_RTSP_VERSION_1_0)
     goto not_supported;