Add support for session keepalive

Get and update the session timeout for all requests. get the session as early as
possible.
This commit is contained in:
Wim Taymans 2009-02-13 19:52:05 +01:00
parent cd29e2a454
commit 308ad6f6d0

View file

@ -289,7 +289,7 @@ no_prepare:
/* Get the session or NULL when there was no session */ /* Get the session or NULL when there was no session */
static GstRTSPSession * static GstRTSPSession *
ensure_session (GstRTSPClient *client, GstRTSPMessage *request) find_session (GstRTSPClient *client, GstRTSPMessage *request)
{ {
GstRTSPResult res; GstRTSPResult res;
GstRTSPSession *session; GstRTSPSession *session;
@ -314,30 +314,26 @@ ensure_session (GstRTSPClient *client, GstRTSPMessage *request)
/* ERRORS */ /* ERRORS */
no_pool: no_pool:
{ {
send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
return NULL; return NULL;
} }
session_not_found: session_not_found:
{ {
send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
return NULL; return NULL;
} }
service_unavailable: service_unavailable:
{ {
send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
return NULL; return NULL;
} }
} }
static gboolean static gboolean
handle_teardown_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *request) handle_teardown_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPSession *session, GstRTSPMessage *request)
{ {
GstRTSPSessionMedia *media; GstRTSPSessionMedia *media;
GstRTSPSession *session;
GstRTSPMessage response = { 0 }; GstRTSPMessage response = { 0 };
GstRTSPStatusCode code; GstRTSPStatusCode code;
if (!(session = ensure_session (client, request))) if (!session)
goto no_session; goto no_session;
/* get a handle to the configuration of the media in the session */ /* get a handle to the configuration of the media in the session */
@ -359,14 +355,12 @@ handle_teardown_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage
send_response (client, session, &response); send_response (client, session, &response);
g_object_unref (session);
return FALSE; return FALSE;
/* ERRORS */ /* ERRORS */
no_session: no_session:
{ {
/* error was sent already */ send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
return FALSE; return FALSE;
} }
not_found: not_found:
@ -377,14 +371,13 @@ not_found:
} }
static gboolean static gboolean
handle_pause_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *request) handle_pause_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPSession *session, GstRTSPMessage *request)
{ {
GstRTSPSessionMedia *media; GstRTSPSessionMedia *media;
GstRTSPSession *session;
GstRTSPMessage response = { 0 }; GstRTSPMessage response = { 0 };
GstRTSPStatusCode code; GstRTSPStatusCode code;
if (!(session = ensure_session (client, request))) if (!session)
goto no_session; goto no_session;
/* get a handle to the configuration of the media in the session */ /* get a handle to the configuration of the media in the session */
@ -407,34 +400,31 @@ handle_pause_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *re
/* the state is now READY */ /* the state is now READY */
media->state = GST_RTSP_STATE_READY; media->state = GST_RTSP_STATE_READY;
g_object_unref (session);
return FALSE; return FALSE;
/* ERRORS */ /* ERRORS */
no_session: no_session:
{ {
send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
return FALSE; return FALSE;
} }
not_found: not_found:
{ {
send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
g_object_unref (session);
return FALSE; return FALSE;
} }
invalid_state: invalid_state:
{ {
send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE, request); send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE, request);
g_object_unref (session);
return FALSE; return FALSE;
} }
} }
static gboolean static gboolean
handle_play_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *request) handle_play_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPSession *session, GstRTSPMessage *request)
{ {
GstRTSPSessionMedia *media; GstRTSPSessionMedia *media;
GstRTSPSession *session;
GstRTSPMessage response = { 0 }; GstRTSPMessage response = { 0 };
GstRTSPStatusCode code; GstRTSPStatusCode code;
GString *rtpinfo; GString *rtpinfo;
@ -442,7 +432,7 @@ handle_play_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *req
guint timestamp, seqnum; guint timestamp, seqnum;
gchar *str; gchar *str;
if (!(session = ensure_session (client, request))) if (!session)
goto no_session; goto no_session;
/* get a handle to the configuration of the media in the session */ /* get a handle to the configuration of the media in the session */
@ -494,7 +484,6 @@ handle_play_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *req
gst_rtsp_session_media_play (media); gst_rtsp_session_media_play (media);
media->state = GST_RTSP_STATE_PLAYING; media->state = GST_RTSP_STATE_PLAYING;
g_object_unref (session);
return FALSE; return FALSE;
@ -507,27 +496,23 @@ no_session:
not_found: not_found:
{ {
send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
g_object_unref (session);
return FALSE; return FALSE;
} }
invalid_state: invalid_state:
{ {
send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE, request); send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE, request);
g_object_unref (session);
return FALSE; return FALSE;
} }
} }
static gboolean static gboolean
handle_setup_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *request) handle_setup_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPSession *session, GstRTSPMessage *request)
{ {
GstRTSPResult res; GstRTSPResult res;
gchar *sessid;
gchar *transport; gchar *transport;
gchar **transports; gchar **transports;
gboolean have_transport; gboolean have_transport;
GstRTSPTransport *ct, *st; GstRTSPTransport *ct, *st;
GstRTSPSession *session;
gint i; gint i;
GstRTSPLowerTrans supported; GstRTSPLowerTrans supported;
GstRTSPMessage response = { 0 }; GstRTSPMessage response = { 0 };
@ -597,14 +582,8 @@ handle_setup_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *re
g_free (ct->destination); g_free (ct->destination);
ct->destination = g_strdup (inet_ntoa (client->address.sin_addr)); ct->destination = g_strdup (inet_ntoa (client->address.sin_addr));
/* a setup request creates a session for a client, check if the client already if (session) {
* sent a session id to us */ g_object_ref (session);
res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
if (res == GST_RTSP_OK) {
/* we had a session in the request, find it again */
if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
goto session_not_found;
/* get a handle to the configuration of the media in the session, this can /* 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. */ * return NULL if this is a new url to manage in this session. */
media = gst_rtsp_session_get_media (session, uri); media = gst_rtsp_session_get_media (session, uri);
@ -669,7 +648,6 @@ handle_setup_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *re
media->state = GST_RTSP_STATE_READY; media->state = GST_RTSP_STATE_READY;
break; break;
} }
g_object_unref (session); g_object_unref (session);
return TRUE; return TRUE;
@ -683,17 +661,14 @@ bad_request:
not_found: not_found:
{ {
send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
g_object_unref (session);
return FALSE; return FALSE;
} }
no_stream: no_stream:
{ {
send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
g_object_unref (media); g_object_unref (media);
return FALSE; g_object_unref (session);
}
session_not_found:
{
send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
return FALSE; return FALSE;
} }
no_transport: no_transport:
@ -721,7 +696,7 @@ service_unavailable:
/* for the describe we must generate an SDP */ /* for the describe we must generate an SDP */
static gboolean static gboolean
handle_describe_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *request) handle_describe_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPSession *session, GstRTSPMessage *request)
{ {
GstRTSPMessage response = { 0 }; GstRTSPMessage response = { 0 };
GstRTSPResult res; GstRTSPResult res;
@ -787,7 +762,7 @@ no_sdp:
} }
static void static void
handle_options_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPMessage *request) handle_options_request (GstRTSPClient *client, GstRTSPUrl *uri, GstRTSPSession *session, GstRTSPMessage *request)
{ {
GstRTSPMessage response = { 0 }; GstRTSPMessage response = { 0 };
GstRTSPMethod options; GstRTSPMethod options;
@ -852,6 +827,7 @@ handle_client (GstRTSPClient *client)
while (TRUE) { while (TRUE) {
GTimeVal timeout; GTimeVal timeout;
GstRTSPSession *session;
timeout.tv_sec = client->timeout; timeout.tv_sec = client->timeout;
timeout.tv_usec = 0; timeout.tv_usec = 0;
@ -886,25 +862,28 @@ handle_client (GstRTSPClient *client)
/* sanitize the uri */ /* sanitize the uri */
santize_uri (uri); santize_uri (uri);
/* get the session if there is any */
session = find_session (client, &request);
/* now see what is asked and dispatch to a dedicated handler */ /* now see what is asked and dispatch to a dedicated handler */
switch (method) { switch (method) {
case GST_RTSP_OPTIONS: case GST_RTSP_OPTIONS:
handle_options_request (client, uri, &request); handle_options_request (client, uri, session, &request);
break; break;
case GST_RTSP_DESCRIBE: case GST_RTSP_DESCRIBE:
handle_describe_request (client, uri, &request); handle_describe_request (client, uri, session, &request);
break; break;
case GST_RTSP_SETUP: case GST_RTSP_SETUP:
handle_setup_request (client, uri, &request); handle_setup_request (client, uri, session, &request);
break; break;
case GST_RTSP_PLAY: case GST_RTSP_PLAY:
handle_play_request (client, uri, &request); handle_play_request (client, uri, session, &request);
break; break;
case GST_RTSP_PAUSE: case GST_RTSP_PAUSE:
handle_pause_request (client, uri, &request); handle_pause_request (client, uri, session, &request);
break; break;
case GST_RTSP_TEARDOWN: case GST_RTSP_TEARDOWN:
handle_teardown_request (client, uri, &request); handle_teardown_request (client, uri, session, &request);
break; break;
case GST_RTSP_ANNOUNCE: case GST_RTSP_ANNOUNCE:
case GST_RTSP_GET_PARAMETER: case GST_RTSP_GET_PARAMETER:
@ -918,6 +897,8 @@ handle_client (GstRTSPClient *client)
send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &request); send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &request);
break; break;
} }
if (session)
g_object_unref (session);
gst_rtsp_url_free (uri); gst_rtsp_url_free (uri);
} }
g_object_unref (client); g_object_unref (client);