*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
gst_rtsp_client_init (GstRTSPClient * client)
{
client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
+ client->teardown_response_seq = 0;
}
static void
client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
{
/* unlink all media managed in this session */
- while (g_list_length (session->medias) > 0) {
- GstRTSPSessionMedia *media = g_list_first (session->medias)->data;
+ while (session->medias) {
+ GstRTSPSessionMedia *media = session->medias->data;
gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
unlink_session_transports (client, session, media);
GST_INFO ("finalize client %p", client);
- if (client->watchid)
+ if (client->watch)
g_source_destroy ((GSource *) client->watch);
client_cleanup_sessions (client);
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");
/* 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);
}
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
gst_rtsp_auth_setup_auth (auth, client, 0, state);
}
- send_response (client, state->session, state->response);
+ send_response (client, state->session, state->response, NULL);
}
gst_rtsp_stream_transport_set_callbacks (trans,
(GstRTSPSendFunc) do_send_data,
(GstRTSPSendFunc) do_send_data, client, NULL);
+
client->transports = g_list_prepend (client->transports, trans);
+
/* make sure our session can't expire */
gst_rtsp_session_prevent_expire (session);
}
{
GST_DEBUG ("client %p: unlinking transport %p", client, trans);
gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
+
client->transports = g_list_remove (client->transports, trans);
+
/* our session can now expire */
gst_rtsp_session_allow_expire (session);
}
GstRTSPStreamTransport *trans;
GstRTSPTransport *tr;
- /* get the stream as configured in the session */
- trans = gst_rtsp_session_media_get_transport (media, i);
/* get the transport, if there is no transport configured, skip this stream */
- if (!(tr = trans->transport))
+ trans = gst_rtsp_session_media_get_transport (media, i);
+ if (trans == NULL)
continue;
+ tr = trans->transport;
+
if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
/* for TCP, unlink the stream from the TCP connection of the client */
unlink_transport (client, session, trans);
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 */
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],
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],
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;
gchar *uristr;
guint rtptime, seq;
- /* get the stream as configured in the session */
- trans = gst_rtsp_session_media_get_transport (media, i);
/* get the transport, if there is no transport configured, skip this stream */
- if (!(tr = trans->transport)) {
+ trans = gst_rtsp_session_media_get_transport (media, i);
+ if (trans == NULL) {
GST_INFO ("stream %d is not configured", i);
continue;
}
+ tr = trans->transport;
if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
/* for TCP, link the stream to the TCP connection of the client */
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);
gst_rtsp_session_touch (session);
}
+/* parse @transport and return a valid transport in @tr. only transports
+ * from @supported are returned. Returns FALSE if no valid transport
+ * was found. */
+static gboolean
+parse_transport (const char *transport, GstRTSPLowerTrans supported,
+ GstRTSPTransport * tr)
+{
+ gint i;
+ gboolean res;
+ gchar **transports;
+
+ res = FALSE;
+ gst_rtsp_transport_init (tr);
+
+ GST_DEBUG ("parsing transports %s", transport);
+
+ transports = g_strsplit (transport, ",", 0);
+
+ /* loop through the transports, try to parse */
+ for (i = 0; transports[i]; i++) {
+ res = gst_rtsp_transport_parse (transports[i], tr);
+ if (res != GST_RTSP_OK) {
+ /* no valid transport, search some more */
+ GST_WARNING ("could not parse transport %s", transports[i]);
+ goto next;
+ }
+
+ /* we have a transport, see if it's RTP/AVP */
+ if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
+ GST_WARNING ("invalid transport %s", transports[i]);
+ goto next;
+ }
+
+ if (!(tr->lower_transport & supported)) {
+ GST_WARNING ("unsupported transport %s", transports[i]);
+ goto next;
+ }
+
+ /* we have a valid transport */
+ GST_INFO ("found valid transport %s", transports[i]);
+ res = TRUE;
+ break;
+
+ next:
+ gst_rtsp_transport_init (tr);
+ }
+ g_strfreev (transports);
+
+ return res;
+}
+
static gboolean
-handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
+handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
+ GstRTSPMessage * request)
{
gchar *blocksize_str;
gboolean ret = TRUE;
if (blocksize > G_MAXUINT)
blocksize = G_MAXUINT;
- gst_rtsp_media_set_mtu (media, blocksize);
+ gst_rtsp_stream_set_mtu (stream, blocksize);
}
}
-
return ret;
}
static gboolean
+configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
+ GstRTSPTransport * ct)
+{
+ /* 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 = 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;
+
+ url = gst_rtsp_connection_get_url (client->connection);
+ g_free (ct->destination);
+ ct->destination = g_strdup (url->host);
+
+ if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
+ /* check if the client selected channels for TCP */
+ if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
+ gst_rtsp_session_media_alloc_channels (state->sessmedia,
+ &ct->interleaved);
+ }
+ }
+ }
+ return TRUE;
+
+ /* ERRORS */
+no_address:
+ {
+ GST_ERROR_OBJECT (client, "failed to acquire address for stream");
+ return FALSE;
+ }
+}
+
+static GstRTSPTransport *
+make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
+ GstRTSPTransport * ct)
+{
+ GstRTSPTransport *st;
+
+ /* prepare the server transport */
+ gst_rtsp_transport_new (&st);
+
+ st->trans = ct->trans;
+ st->profile = ct->profile;
+ st->lower_transport = ct->lower_transport;
+
+ switch (st->lower_transport) {
+ case GST_RTSP_LOWER_TRANS_UDP:
+ st->client_port = ct->client_port;
+ st->server_port = state->stream->server_port;
+ break;
+ case GST_RTSP_LOWER_TRANS_UDP_MCAST:
+ st->port = ct->port;
+ st->destination = g_strdup (ct->destination);
+ st->ttl = ct->ttl;
+ break;
+ case GST_RTSP_LOWER_TRANS_TCP:
+ st->interleaved = ct->interleaved;
+ default:
+ break;
+ }
+
+ if (state->stream->session)
+ g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
+
+ return st;
+}
+
+static gboolean
handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
{
GstRTSPResult res;
GstRTSPUrl *uri;
gchar *transport;
- gchar **transports;
- gboolean have_transport;
GstRTSPTransport *ct, *st;
- gint i;
GstRTSPLowerTrans supported;
GstRTSPStatusCode code;
GstRTSPSession *session;
GstRTSPStreamTransport *trans;
gchar *trans_str, *pos;
guint streamid;
- GstRTSPSessionMedia *media;
+ GstRTSPSessionMedia *sessmedia;
+ GstRTSPMedia *media;
+ GstRTSPStream *stream;
uri = state->uri;
/* the uri contains the stream number we added in the SDP config, which is
- * always /stream=%d so we need to strip that off
+ * always /stream=%d so we need to strip that off
* parse the stream we need to configure, look for the stream in the abspath
* first and then in the query. */
if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
goto bad_request;
}
- /* we can mofify the parse uri in place */
+ /* we can mofify the parsed uri in place */
*pos = '\0';
pos += strlen ("/stream=");
if (res != GST_RTSP_OK)
goto no_transport;
- transports = g_strsplit (transport, ",", 0);
gst_rtsp_transport_new (&ct);
- /* init transports */
- have_transport = FALSE;
- gst_rtsp_transport_init (ct);
-
/* our supported transports */
supported = GST_RTSP_LOWER_TRANS_UDP |
GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
- /* loop through the transports, try to parse */
- for (i = 0; transports[i]; i++) {
- res = gst_rtsp_transport_parse (transports[i], ct);
- if (res != GST_RTSP_OK) {
- /* no valid transport, search some more */
- GST_WARNING ("could not parse transport %s", transports[i]);
- goto next;
- }
-
- /* we have a transport, see if it's RTP/AVP */
- if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
- GST_WARNING ("invalid transport %s", transports[i]);
- goto next;
- }
-
- if (!(ct->lower_transport & supported)) {
- GST_WARNING ("unsupported transport %s", transports[i]);
- goto next;
- }
-
- /* we have a valid transport */
- GST_INFO ("found valid transport %s", transports[i]);
- have_transport = TRUE;
- break;
-
- next:
- gst_rtsp_transport_init (ct);
- }
- g_strfreev (transports);
-
- /* we have not found anything usable, error out */
- if (!have_transport)
+ /* parse and find a usable supported transport */
+ if (!parse_transport (transport, supported, ct))
goto unsupported_transports;
+ /* we create the session after parsing stuff so that we don't make
+ * a session for malformed requests */
if (client->session_pool == NULL)
goto no_pool;
g_object_ref (session);
/* get a handle to the configuration of the media in the session, this can
* return NULL if this is a new url to manage in this session. */
- media = gst_rtsp_session_get_media (session, uri);
+ sessmedia = gst_rtsp_session_get_media (session, uri);
} else {
/* create a session if this fails we probably reached our session limit or
* something. */
state->session = session;
/* we need a new media configuration in this session */
- media = NULL;
+ sessmedia = NULL;
}
/* we have no media, find one and manage it */
- if (media == NULL) {
- GstRTSPMedia *m;
-
+ if (sessmedia == NULL) {
/* get a handle to the configuration of the media in the session */
- if ((m = find_media (client, state))) {
+ if ((media = find_media (client, state))) {
/* manage the media in our session now */
- media = gst_rtsp_session_manage_media (session, uri, m);
+ sessmedia = gst_rtsp_session_manage_media (session, uri, media);
}
}
/* if we stil have no media, error */
- if (media == NULL)
+ if (sessmedia == NULL)
goto not_found;
- state->sessmedia = media;
-
- if (!handle_blocksize (media->media, state->request))
- goto invalid_blocksize;
+ state->sessmedia = sessmedia;
+ state->media = media = sessmedia->media;
- /* 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) {
- g_free (ct->destination);
- ct->destination = gst_rtsp_media_get_multicast_group (media->media);
- }
- /* reset ttl if client settings are not allowed */
- if (!client->use_client_settings) {
- ct->ttl = 0;
- }
- } else {
- GstRTSPUrl *url;
+ /* now get the stream */
+ stream = gst_rtsp_media_get_stream (media, streamid);
+ if (stream == NULL)
+ goto not_found;
- url = gst_rtsp_connection_get_url (client->connection);
- g_free (ct->destination);
- ct->destination = g_strdup (url->host);
+ state->stream = stream;
- if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
- /* check if the client selected channels for TCP */
- if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
- gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
- }
- }
- }
+ /* set blocksize on this stream */
+ if (!handle_blocksize (media, stream, state->request))
+ goto invalid_blocksize;
- /* get a handle to the transport of the media in this session */
- if (!(trans = gst_rtsp_session_media_get_transport (media, streamid)))
- goto no_stream_transport;
+ /* update the client transport */
+ if (!configure_client_transport (client, state, ct))
+ goto unsupported_client_transport;
- st = gst_rtsp_stream_transport_set_transport (trans, ct);
+ /* set in the session media transport */
+ trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
/* configure keepalive for this transport */
gst_rtsp_stream_transport_set_keepalive (trans,
(GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
- /* serialize the server transport */
+ /* create and serialize the server transport */
+ st = make_server_transport (client, state, ct);
trans_str = gst_rtsp_transport_as_text (st);
gst_rtsp_transport_free (st);
trans_str);
g_free (trans_str);
- send_response (client, session, state->response);
+ send_response (client, session, state->response, NULL);
/* update the state */
- switch (media->state) {
+ switch (sessmedia->state) {
case GST_RTSP_STATE_PLAYING:
case GST_RTSP_STATE_RECORDING:
case GST_RTSP_STATE_READY:
/* no state change */
break;
default:
- media->state = GST_RTSP_STATE_READY;
+ sessmedia->state = GST_RTSP_STATE_READY;
break;
}
g_object_unref (session);
gst_rtsp_transport_free (ct);
return FALSE;
}
-no_stream_transport:
+unsupported_client_transport:
{
- send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
+ send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
g_object_unref (session);
gst_rtsp_transport_free (ct);
return FALSE;
GstSDPMessage *sdp;
GstSDPInfo info;
const gchar *proto;
- GstRTSPLowerTrans protocols;
gst_sdp_message_new (&sdp);
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))
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);
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);
handled = FALSE;
for (walk = client->transports; walk; walk = g_list_next (walk)) {
- GstRTSPStreamTransport *trans = (GstRTSPStreamTransport *) walk->data;
+ GstRTSPStreamTransport *trans;
GstRTSPStream *stream;
GstRTSPTransport *tr;
- /* get the transport, if there is no transport configured, skip this stream */
- if (!(tr = trans->transport))
- continue;
+ trans = walk->data;
- /* we also need a media stream */
- if (!(stream = trans->stream))
- continue;
+ /* we only add clients with a transport to the list */
+ tr = trans->transport;
+ stream = trans->stream;
/* check for TCP transport */
if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
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;
}
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);
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 */
}
/**
- * 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.
* 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;
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:
* 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
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:
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;
+}