X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gst%2Frtsp-server%2Frtsp-client.c;h=5b5affe2b77fd12feaf12132da87787fbc62650e;hb=bba7c4042d1d0384affa0f94ae0cff7c1a2c9908;hp=8e9b026d722c9db945c65ba8a1bb2b39965d91fe;hpb=4a4a15077b67cc2374ab1df5b7e385949685cffb;p=platform%2Fupstream%2Fgstreamer.git diff --git a/gst/rtsp-server/rtsp-client.c b/gst/rtsp-server/rtsp-client.c index 8e9b026..5b5affe 100644 --- a/gst/rtsp-server/rtsp-client.c +++ b/gst/rtsp-server/rtsp-client.c @@ -13,46 +13,78 @@ * * 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 -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "rtsp-client.h" #include "rtsp-sdp.h" #include "rtsp-params.h" -/* temporary multicast address until it's configurable somewhere */ -#define MCAST_ADDRESS "224.2.0.1" +#define GST_RTSP_CLIENT_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate)) -static GMutex *tunnels_lock; -static GHashTable *tunnels; +/* locking order: + * send_lock, lock, tunnels_lock + */ + +struct _GstRTSPClientPrivate +{ + GMutex lock; /* protects everything else */ + GMutex send_lock; + GstRTSPConnection *connection; + GstRTSPWatch *watch; + guint close_seq; + gchar *server_ip; + gboolean is_ipv6; + gboolean use_client_settings; + + GstRTSPClientSendFunc send_func; /* protected by send_lock */ + gpointer send_data; /* protected by send_lock */ + GDestroyNotify send_notify; /* protected by send_lock */ + + GstRTSPSessionPool *session_pool; + GstRTSPMountPoints *mount_points; + GstRTSPAuth *auth; + + GstRTSPUrl *uri; + GstRTSPMedia *media; + + GList *transports; + GList *sessions; +}; + +static GMutex tunnels_lock; +static GHashTable *tunnels; /* protected by tunnels_lock */ + +#define DEFAULT_SESSION_POOL NULL +#define DEFAULT_MOUNT_POINTS NULL +#define DEFAULT_USE_CLIENT_SETTINGS FALSE enum { PROP_0, PROP_SESSION_POOL, - PROP_MEDIA_MAPPING, + PROP_MOUNT_POINTS, + PROP_USE_CLIENT_SETTINGS, PROP_LAST }; enum { SIGNAL_CLOSED, + SIGNAL_NEW_SESSION, + SIGNAL_OPTIONS_REQUEST, + SIGNAL_DESCRIBE_REQUEST, + SIGNAL_SETUP_REQUEST, + SIGNAL_PLAY_REQUEST, + SIGNAL_PAUSE_REQUEST, + SIGNAL_TEARDOWN_REQUEST, + SIGNAL_SET_PARAMETER_REQUEST, + SIGNAL_GET_PARAMETER_REQUEST, SIGNAL_LAST }; @@ -67,9 +99,10 @@ static void gst_rtsp_client_set_property (GObject * object, guint propid, const GValue * value, GParamSpec * pspec); static void gst_rtsp_client_finalize (GObject * obj); +static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media); static void client_session_finalized (GstRTSPClient * client, GstRTSPSession * session); -static void unlink_session_streams (GstRTSPClient * client, +static void unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session, GstRTSPSessionMedia * media); G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT); @@ -79,22 +112,32 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass) { GObjectClass *gobject_class; + g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate)); + gobject_class = G_OBJECT_CLASS (klass); gobject_class->get_property = gst_rtsp_client_get_property; gobject_class->set_property = gst_rtsp_client_set_property; gobject_class->finalize = gst_rtsp_client_finalize; + klass->create_sdp = create_sdp; + g_object_class_install_property (gobject_class, PROP_SESSION_POOL, g_param_spec_object ("session-pool", "Session Pool", "The session pool to use for client session", GST_TYPE_RTSP_SESSION_POOL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); - g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING, - g_param_spec_object ("media-mapping", "Media Mapping", - "The media mapping to use for client session", - GST_TYPE_RTSP_MEDIA_MAPPING, + g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS, + g_param_spec_object ("mount-points", "Mount Points", + "The mount points to use for client session", + GST_TYPE_RTSP_MOUNT_POINTS, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS, + g_param_spec_boolean ("use-client-settings", "Use Client Settings", + "Use client settings for ttl and destination in multicast", + DEFAULT_USE_CLIENT_SETTINGS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); gst_rtsp_client_signals[SIGNAL_CLOSED] = @@ -102,9 +145,62 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass) G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE); + gst_rtsp_client_signals[SIGNAL_NEW_SESSION] = + g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION); + + gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] = + g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, + G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] = + g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, + G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] = + g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, + G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] = + g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, + G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] = + g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, + G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] = + g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, + G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] = + g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, + set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + + gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] = + g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, + get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + tunnels = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); - tunnels_lock = g_mutex_new (); + g_mutex_init (&tunnels_lock); GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient"); } @@ -112,34 +208,51 @@ gst_rtsp_client_class_init (GstRTSPClientClass * klass) static void gst_rtsp_client_init (GstRTSPClient * client) { + GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client); + + client->priv = priv; + + g_mutex_init (&priv->lock); + g_mutex_init (&priv->send_lock); + priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS; + priv->close_seq = 0; +} + +static GstRTSPFilterResult +filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * media, + gpointer user_data) +{ + GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + + gst_rtsp_session_media_set_state (media, GST_STATE_NULL); + unlink_session_transports (client, sess, media); + + /* unmanage the media in the session */ + return GST_RTSP_FILTER_REMOVE; } static void client_unlink_session (GstRTSPClient * client, GstRTSPSession * session) { - GList *medias; - /* unlink all media managed in this session */ - for (medias = session->medias; medias; medias = g_list_next (medias)) { - unlink_session_streams (client, session, - (GstRTSPSessionMedia *) medias->data); - } + gst_rtsp_session_filter (session, filter_session, client); } static void client_cleanup_sessions (GstRTSPClient * client) { + GstRTSPClientPrivate *priv = client->priv; GList *sessions; /* remove weak-ref from sessions */ - for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) { + for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) { GstRTSPSession *session = (GstRTSPSession *) sessions->data; g_object_weak_unref (G_OBJECT (session), (GWeakNotify) client_session_finalized, client); client_unlink_session (client, session); } - g_list_free (client->sessions); - client->sessions = NULL; + g_list_free (priv->sessions); + priv->sessions = NULL; } /* A client is finalized when the connection is broken */ @@ -147,25 +260,36 @@ static void gst_rtsp_client_finalize (GObject * obj) { GstRTSPClient *client = GST_RTSP_CLIENT (obj); + GstRTSPClientPrivate *priv = client->priv; GST_INFO ("finalize client %p", client); + gst_rtsp_client_set_send_func (client, NULL, NULL, NULL); + + if (priv->watch) + g_source_destroy ((GSource *) priv->watch); + client_cleanup_sessions (client); - gst_rtsp_connection_free (client->connection); - if (client->session_pool) - g_object_unref (client->session_pool); - if (client->media_mapping) - g_object_unref (client->media_mapping); - if (client->auth) - g_object_unref (client->auth); + if (priv->connection) + gst_rtsp_connection_free (priv->connection); + if (priv->session_pool) + g_object_unref (priv->session_pool); + if (priv->mount_points) + g_object_unref (priv->mount_points); + if (priv->auth) + g_object_unref (priv->auth); - if (client->uri) - gst_rtsp_url_free (client->uri); - if (client->media) - g_object_unref (client->media); + if (priv->uri) + gst_rtsp_url_free (priv->uri); + if (priv->media) { + gst_rtsp_media_unprepare (priv->media); + g_object_unref (priv->media); + } - g_free (client->server_ip); + g_free (priv->server_ip); + g_mutex_clear (&priv->lock); + g_mutex_clear (&priv->send_lock); G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj); } @@ -180,8 +304,12 @@ gst_rtsp_client_get_property (GObject * object, guint propid, case PROP_SESSION_POOL: g_value_take_object (value, gst_rtsp_client_get_session_pool (client)); break; - case PROP_MEDIA_MAPPING: - g_value_take_object (value, gst_rtsp_client_get_media_mapping (client)); + case PROP_MOUNT_POINTS: + g_value_take_object (value, gst_rtsp_client_get_mount_points (client)); + break; + case PROP_USE_CLIENT_SETTINGS: + g_value_set_boolean (value, + gst_rtsp_client_get_use_client_settings (client)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec); @@ -198,8 +326,12 @@ gst_rtsp_client_set_property (GObject * object, guint propid, case PROP_SESSION_POOL: gst_rtsp_client_set_session_pool (client, g_value_get_object (value)); break; - case PROP_MEDIA_MAPPING: - gst_rtsp_client_set_media_mapping (client, g_value_get_object (value)); + case PROP_MOUNT_POINTS: + gst_rtsp_client_set_mount_points (client, g_value_get_object (value)); + break; + case PROP_USE_CLIENT_SETTINGS: + gst_rtsp_client_set_use_client_settings (client, + g_value_get_boolean (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec); @@ -225,8 +357,10 @@ gst_rtsp_client_new (void) static void send_response (GstRTSPClient * client, GstRTSPSession * session, - GstRTSPMessage * response) + GstRTSPMessage * response, gboolean close) { + GstRTSPClientPrivate *priv = client->priv; + gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER, "GStreamer RTSP server"); @@ -235,23 +369,22 @@ 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); + if (close) + gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close"); + + g_mutex_lock (&priv->send_lock); + if (priv->send_func) + priv->send_func (client, response, close, priv->send_data); + g_mutex_unlock (&priv->send_lock); + gst_rtsp_message_unset (response); } @@ -259,31 +392,25 @@ static void send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code, GstRTSPClientState * state) { - GstRTSPMessage response = { 0 }; - - gst_rtsp_message_init_response (&response, code, + gst_rtsp_message_init_response (state->response, code, gst_rtsp_status_as_text (code), state->request); - send_response (client, NULL, &response); + send_response (client, NULL, state->response, FALSE); } static void handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth, GstRTSPClientState * state) { - GstRTSPMessage response = { 0 }; - - gst_rtsp_message_init_response (&response, GST_RTSP_STS_UNAUTHORIZED, + gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED, gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request); - state->response = &response; - if (auth) { /* and let the authentication manager setup the auth tokens */ gst_rtsp_auth_setup_auth (auth, client, 0, state); } - send_response (client, state->session, &response); + send_response (client, state->session, state->response, FALSE); } @@ -305,36 +432,40 @@ compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2) static GstRTSPMedia * find_media (GstRTSPClient * client, GstRTSPClientState * state) { + GstRTSPClientPrivate *priv = client->priv; GstRTSPMediaFactory *factory; GstRTSPMedia *media; GstRTSPAuth *auth; - if (!compare_uri (client->uri, state->uri)) { + if (!compare_uri (priv->uri, state->uri)) { /* remove any previously cached values before we try to construct a new * media for uri */ - if (client->uri) - gst_rtsp_url_free (client->uri); - client->uri = NULL; - if (client->media) - g_object_unref (client->media); - client->media = NULL; + if (priv->uri) + gst_rtsp_url_free (priv->uri); + priv->uri = NULL; + if (priv->media) { + gst_rtsp_media_unprepare (priv->media); + g_object_unref (priv->media); + } + priv->media = NULL; - if (!client->media_mapping) - goto no_mapping; + if (!priv->mount_points) + goto no_mount_points; /* find the factory for the uri first */ if (!(factory = - gst_rtsp_media_mapping_find_factory (client->media_mapping, + gst_rtsp_mount_points_find_factory (priv->mount_points, state->uri))) goto no_factory; - state->factory = factory; - /* check if we have access to the factory */ if ((auth = gst_rtsp_media_factory_get_auth (factory))) { + state->factory = factory; + if (!gst_rtsp_auth_check (auth, client, 0, state)) goto not_allowed; + state->factory = NULL; g_object_unref (auth); } @@ -343,21 +474,19 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state) goto no_media; g_object_unref (factory); - - /* set ipv6 on the media before preparing */ - media->is_ipv6 = client->is_ipv6; - state->media = media; + factory = NULL; /* prepare the media */ if (!(gst_rtsp_media_prepare (media))) goto no_prepare; /* now keep track of the uri and the media */ - client->uri = gst_rtsp_url_copy (state->uri); - client->media = media; + priv->uri = gst_rtsp_url_copy (state->uri); + priv->media = media; + state->media = media; } else { /* we have seen this uri before, used cached media */ - media = client->media; + media = priv->media; state->media = media; GST_INFO ("reusing cached media %p", media); } @@ -368,34 +497,39 @@ find_media (GstRTSPClient * client, GstRTSPClientState * state) return media; /* ERRORS */ -no_mapping: +no_mount_points: { + GST_ERROR ("client %p: no mount points configured", client); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); return NULL; } no_factory: { + GST_ERROR ("client %p: no factory for uri", client); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); return NULL; } not_allowed: { + GST_ERROR ("client %p: unauthorized request", client); handle_unauthorized_request (client, auth, state); g_object_unref (factory); + state->factory = NULL; g_object_unref (auth); return NULL; } no_media: { + GST_ERROR ("client %p: can't create media", client); send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state); g_object_unref (factory); return NULL; } no_prepare: { + GST_ERROR ("client %p: can't prepare media", client); send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state); g_object_unref (media); - g_object_unref (factory); return NULL; } } @@ -403,91 +537,89 @@ no_prepare: static gboolean do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client) { + GstRTSPClientPrivate *priv = client->priv; GstRTSPMessage message = { 0 }; + GstMapInfo map_info; guint8 *data; - guint size; + guint usize; gst_rtsp_message_init_data (&message, channel); - data = GST_BUFFER_DATA (buffer); - size = GST_BUFFER_SIZE (buffer); - gst_rtsp_message_take_body (&message, data, size); + /* FIXME, need some sort of iovec RTSPMessage here */ + if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ)) + return FALSE; + + gst_rtsp_message_take_body (&message, map_info.data, map_info.size); - /* FIXME, client->watch could have been finalized here, we need to keep an - * extra refcount to the watch. */ - gst_rtsp_watch_send_message (client->watch, &message, NULL); + g_mutex_lock (&priv->send_lock); + if (priv->send_func) + priv->send_func (client, &message, FALSE, priv->send_data); + g_mutex_unlock (&priv->send_lock); + + gst_rtsp_message_steal_body (&message, &data, &usize); + gst_buffer_unmap (buffer, &map_info); - gst_rtsp_message_steal_body (&message, &data, &size); gst_rtsp_message_unset (&message); return TRUE; } -static gboolean -do_send_data_list (GstBufferList * blist, guint8 channel, - GstRTSPClient * client) +static void +link_transport (GstRTSPClient * client, GstRTSPSession * session, + GstRTSPStreamTransport * trans) { - GstBufferListIterator *it; + GstRTSPClientPrivate *priv = client->priv; - it = gst_buffer_list_iterate (blist); - while (gst_buffer_list_iterator_next_group (it)) { - GstBuffer *group = gst_buffer_list_iterator_merge_group (it); + GST_DEBUG ("client %p: linking transport %p", client, trans); - if (group == NULL) - continue; + gst_rtsp_stream_transport_set_callbacks (trans, + (GstRTSPSendFunc) do_send_data, + (GstRTSPSendFunc) do_send_data, client, NULL); - do_send_data (group, channel, client); - } - gst_buffer_list_iterator_free (it); + priv->transports = g_list_prepend (priv->transports, trans); - return TRUE; -} - -static void -link_stream (GstRTSPClient * client, GstRTSPSession * session, - GstRTSPSessionStream * stream) -{ - GST_DEBUG ("client %p: linking stream %p", client, stream); - gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data, - (GstRTSPSendFunc) do_send_data, (GstRTSPSendListFunc) do_send_data_list, - (GstRTSPSendListFunc) do_send_data_list, client, NULL); - client->streams = g_list_prepend (client->streams, stream); /* make sure our session can't expire */ gst_rtsp_session_prevent_expire (session); } static void -unlink_stream (GstRTSPClient * client, GstRTSPSession * session, - GstRTSPSessionStream * stream) +unlink_transport (GstRTSPClient * client, GstRTSPSession * session, + GstRTSPStreamTransport * trans) { - GST_DEBUG ("client %p: unlinking stream %p", client, stream); - gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL, NULL, - NULL); - client->streams = g_list_remove (client->streams, stream); + GstRTSPClientPrivate *priv = client->priv; + + GST_DEBUG ("client %p: unlinking transport %p", client, trans); + + gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL); + + priv->transports = g_list_remove (priv->transports, trans); + /* our session can now expire */ gst_rtsp_session_allow_expire (session); } static void -unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session, +unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session, GstRTSPSessionMedia * media) { guint n_streams, i; - n_streams = gst_rtsp_media_n_streams (media->media); + n_streams = + gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media)); for (i = 0; i < n_streams; i++) { - GstRTSPSessionStream *sstream; - GstRTSPTransport *tr; + GstRTSPStreamTransport *trans; + const GstRTSPTransport *tr; - /* get the stream as configured in the session */ - sstream = gst_rtsp_session_media_get_stream (media, i); /* get the transport, if there is no transport configured, skip this stream */ - if (!(tr = sstream->trans.transport)) + trans = gst_rtsp_session_media_get_transport (media, i); + if (trans == NULL) continue; + tr = gst_rtsp_stream_transport_get_transport (trans); + if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) { /* for TCP, unlink the stream from the TCP connection of the client */ - unlink_stream (client, session, sstream); + unlink_transport (client, session, trans); } } } @@ -495,31 +627,27 @@ unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session, static void close_connection (GstRTSPClient * client) { + GstRTSPClientPrivate *priv = client->priv; const gchar *tunnelid; GST_DEBUG ("client %p: closing connection", client); - if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) { - g_mutex_lock (tunnels_lock); + if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) { + g_mutex_lock (&tunnels_lock); /* remove from tunnelids */ g_hash_table_remove (tunnels, tunnelid); - g_mutex_unlock (tunnels_lock); + g_mutex_unlock (&tunnels_lock); } - gst_rtsp_connection_close (client->connection); - if (client->watchid) { - g_source_destroy ((GSource *) client->watch); - client->watchid = 0; - client->watch = NULL; - } + gst_rtsp_connection_close (priv->connection); } static gboolean handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state) { + GstRTSPClientPrivate *priv = client->priv; GstRTSPSession *session; GstRTSPSessionMedia *media; - GstRTSPMessage response = { 0 }; GstRTSPStatusCode code; if (!state->session) @@ -534,13 +662,17 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state) state->sessmedia = media; + /* we emit the signal before closing the connection */ + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST], + 0, state); + /* unlink the all TCP callbacks */ - unlink_session_streams (client, session, media); + unlink_session_transports (client, session, media); /* remove the session from the watched sessions */ g_object_weak_unref (G_OBJECT (session), (GWeakNotify) client_session_finalized, client); - client->sessions = g_list_remove (client->sessions, session); + priv->sessions = g_list_remove (priv->sessions, session); gst_rtsp_session_media_set_state (media, GST_STATE_NULL); @@ -548,29 +680,27 @@ handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state) * are torn down. */ if (!gst_rtsp_session_release_media (session, media)) { /* remove the session */ - gst_rtsp_session_pool_remove (client->session_pool, session); + gst_rtsp_session_pool_remove (priv->session_pool, session); } /* construct the response now */ code = GST_RTSP_STS_OK; - gst_rtsp_message_init_response (&response, code, + gst_rtsp_message_init_response (state->response, code, gst_rtsp_status_as_text (code), state->request); - gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONNECTION, "close"); - - send_response (client, session, &response); - - close_connection (client); + send_response (client, session, state->response, TRUE); return TRUE; /* ERRORS */ no_session: { + GST_ERROR ("client %p: no session", client); send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state); return FALSE; } not_found: { + GST_ERROR ("client %p: no media for uri", client); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); return FALSE; } @@ -591,23 +721,23 @@ handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state) /* no body, keep-alive request */ send_generic_response (client, GST_RTSP_STS_OK, state); } else { - /* there is a body */ - GstRTSPMessage response = { 0 }; - - state->response = &response; - /* there is a body, handle the params */ res = gst_rtsp_params_get (client, state); if (res != GST_RTSP_OK) goto bad_request; - send_response (client, state->session, &response); + send_response (client, state->session, state->response, FALSE); } + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST], + 0, state); + return TRUE; /* ERRORS */ bad_request: { + GST_ERROR ("client %p: bad request", client); send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state); return FALSE; } @@ -628,22 +758,23 @@ handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state) /* no body, keep-alive request */ send_generic_response (client, GST_RTSP_STS_OK, state); } else { - GstRTSPMessage response = { 0 }; - - state->response = &response; - /* there is a body, handle the params */ res = gst_rtsp_params_set (client, state); if (res != GST_RTSP_OK) goto bad_request; - send_response (client, state->session, &response); + send_response (client, state->session, state->response, FALSE); } + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST], + 0, state); + return TRUE; /* ERRORS */ bad_request: { + GST_ERROR ("client %p: bad request", client); send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state); return FALSE; } @@ -654,8 +785,8 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state) { GstRTSPSession *session; GstRTSPSessionMedia *media; - GstRTSPMessage response = { 0 }; GstRTSPStatusCode code; + GstRTSPState rtspstate; if (!(session = state->session)) goto no_session; @@ -667,42 +798,49 @@ handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state) state->sessmedia = media; + rtspstate = gst_rtsp_session_media_get_rtsp_state (media); /* the session state must be playing or recording */ - if (media->state != GST_RTSP_STATE_PLAYING && - media->state != GST_RTSP_STATE_RECORDING) + if (rtspstate != GST_RTSP_STATE_PLAYING && + rtspstate != GST_RTSP_STATE_RECORDING) goto invalid_state; /* unlink the all TCP callbacks */ - unlink_session_streams (client, session, media); + unlink_session_transports (client, session, media); /* then pause sending */ gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED); /* construct the response now */ code = GST_RTSP_STS_OK; - gst_rtsp_message_init_response (&response, code, + gst_rtsp_message_init_response (state->response, code, gst_rtsp_status_as_text (code), state->request); - send_response (client, session, &response); + send_response (client, session, state->response, FALSE); /* the state is now READY */ - media->state = GST_RTSP_STATE_READY; + gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_READY); + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], + 0, state); return TRUE; /* ERRORS */ no_session: { + GST_ERROR ("client %p: no seesion", client); send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state); return FALSE; } not_found: { + GST_ERROR ("client %p: no media for uri", client); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); return FALSE; } invalid_state: { + GST_ERROR ("client %p: not PLAYING or RECORDING", client); send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE, state); return FALSE; @@ -714,14 +852,14 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state) { GstRTSPSession *session; GstRTSPSessionMedia *media; - GstRTSPMessage response = { 0 }; GstRTSPStatusCode code; GString *rtpinfo; guint n_streams, i, infocount; - guint timestamp, seqnum; gchar *str; GstRTSPTimeRange *range; GstRTSPResult res; + GstRTSPState rtspstate; + GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT; if (!(session = state->session)) goto no_session; @@ -734,8 +872,8 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state) state->sessmedia = media; /* the session state must be playing or ready */ - if (media->state != GST_RTSP_STATE_PLAYING && - media->state != GST_RTSP_STATE_READY) + rtspstate = gst_rtsp_session_media_get_rtsp_state (media); + if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY) goto invalid_state; /* parse the range header if we have one */ @@ -744,7 +882,8 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state) if (res == GST_RTSP_OK) { if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) { /* we have a range, seek to the position */ - gst_rtsp_media_seek (media->media, range); + gst_rtsp_media_seek (gst_rtsp_session_media_get_media (media), range); + unit = range->unit; gst_rtsp_range_free (range); } } @@ -752,46 +891,36 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state) /* grab RTPInfo from the payloaders now */ rtpinfo = g_string_new (""); - n_streams = gst_rtsp_media_n_streams (media->media); + n_streams = + gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media)); for (i = 0, infocount = 0; i < n_streams; i++) { - GstRTSPSessionStream *sstream; - GstRTSPMediaStream *stream; - GstRTSPTransport *tr; - GObjectClass *payobjclass; + GstRTSPStreamTransport *trans; + GstRTSPStream *stream; + const GstRTSPTransport *tr; gchar *uristr; + guint rtptime, seq; - /* get the stream as configured in the session */ - sstream = gst_rtsp_session_media_get_stream (media, i); /* get the transport, if there is no transport configured, skip this stream */ - if (!(tr = sstream->trans.transport)) { + trans = gst_rtsp_session_media_get_transport (media, i); + if (trans == NULL) { GST_INFO ("stream %d is not configured", i); continue; } + tr = gst_rtsp_stream_transport_get_transport (trans); if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) { /* for TCP, link the stream to the TCP connection of the client */ - link_stream (client, session, sstream); + link_transport (client, session, trans); } - stream = sstream->media_stream; - - payobjclass = G_OBJECT_GET_CLASS (stream->payloader); - - if (g_object_class_find_property (payobjclass, "seqnum") && - g_object_class_find_property (payobjclass, "timestamp")) { - GObject *payobj; - - payobj = G_OBJECT (stream->payloader); - - /* only add RTP-Info for streams with seqnum and timestamp */ - g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL); - + stream = gst_rtsp_stream_transport_get_stream (trans); + if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) { if (infocount > 0) g_string_append (rtpinfo, ", "); uristr = gst_rtsp_url_get_request_uri (state->uri); g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u", - uristr, i, seqnum, timestamp); + uristr, i, seq, rtptime); g_free (uristr); infocount++; @@ -802,43 +931,51 @@ handle_play_request (GstRTSPClient * client, GstRTSPClientState * state) /* construct the response now */ code = GST_RTSP_STS_OK; - gst_rtsp_message_init_response (&response, code, + gst_rtsp_message_init_response (state->response, code, gst_rtsp_status_as_text (code), state->request); /* add the RTP-Info header */ if (infocount > 0) { str = g_string_free (rtpinfo, FALSE); - gst_rtsp_message_take_header (&response, GST_RTSP_HDR_RTP_INFO, str); + gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str); } else { g_string_free (rtpinfo, TRUE); } /* add the range */ - str = gst_rtsp_media_get_range_string (media->media, TRUE); - gst_rtsp_message_take_header (&response, GST_RTSP_HDR_RANGE, str); + str = + gst_rtsp_media_get_range_string (gst_rtsp_session_media_get_media (media), + TRUE, unit); + gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str); - send_response (client, session, &response); + send_response (client, session, state->response, FALSE); /* start playing after sending the request */ gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING); - media->state = GST_RTSP_STATE_PLAYING; + gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_PLAYING); + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], + 0, state); return TRUE; /* ERRORS */ no_session: { + GST_ERROR ("client %p: no session", client); send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state); return FALSE; } not_found: { + GST_ERROR ("client %p: media not found", client); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); return FALSE; } invalid_state: { + GST_ERROR ("client %p: not PLAYING or READY", client); send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE, state); return FALSE; @@ -852,30 +989,202 @@ do_keepalive (GstRTSPSession * session) 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, GstRTSPStream * stream, + GstRTSPMessage * request) +{ + gchar *blocksize_str; + gboolean ret = TRUE; + + if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE, + &blocksize_str, 0) == GST_RTSP_OK) { + guint64 blocksize; + 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); + } + } + return ret; +} + +static gboolean +configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state, + GstRTSPTransport * ct) +{ + GstRTSPClientPrivate *priv = client->priv; + + /* 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 && priv->use_client_settings) { + GstRTSPAddress *addr; + + addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination, + ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl); + + if (addr == NULL) + goto no_address; + + gst_rtsp_address_free (addr); + } else { + 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; + + gst_rtsp_address_free (addr); + } + } else { + GstRTSPUrl *url; + + url = gst_rtsp_connection_get_url (priv->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; + gst_rtsp_stream_get_server_port (state->stream, &st->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; + } + + gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc); + + return st; +} + static gboolean handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state) { + GstRTSPClientPrivate *priv = client->priv; GstRTSPResult res; GstRTSPUrl *uri; gchar *transport; - gchar **transports; - gboolean have_transport; GstRTSPTransport *ct, *st; - gint i; GstRTSPLowerTrans supported; - GstRTSPMessage response = { 0 }; GstRTSPStatusCode code; GstRTSPSession *session; - GstRTSPSessionStream *stream; + GstRTSPStreamTransport *trans; gchar *trans_str, *pos; guint streamid; - GstRTSPSessionMedia *media; - GstRTSPUrl *url; + GstRTSPSessionMedia *sessmedia; + GstRTSPMedia *media; + GstRTSPStream *stream; + GstRTSPState rtspstate; 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="))) { @@ -883,7 +1192,7 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state) goto bad_request; } - /* we can mofify the parse uri in place */ + /* we can mofify the parsed uri in place */ *pos = '\0'; pos += strlen ("/stream="); @@ -897,184 +1206,169 @@ handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state) 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; - if (client->session_pool == NULL) + /* we create the session after parsing stuff so that we don't make + * a session for malformed requests */ + if (priv->session_pool == NULL) goto no_pool; - /* we have a valid transport now, set the destination of the client. */ - g_free (ct->destination); - if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) { - ct->destination = g_strdup (MCAST_ADDRESS); - } else { - url = gst_rtsp_connection_get_url (client->connection); - ct->destination = g_strdup (url->host); - } - session = state->session; if (session) { 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. */ - if (!(session = gst_rtsp_session_pool_create (client->session_pool))) + if (!(session = gst_rtsp_session_pool_create (priv->session_pool))) goto service_unavailable; 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; + state->sessmedia = sessmedia; + state->media = media = gst_rtsp_session_media_get_media (sessmedia); - /* fix the transports */ - 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); - } - } + /* now get the stream */ + stream = gst_rtsp_media_get_stream (media, streamid); + if (stream == NULL) + goto not_found; + + state->stream = stream; - /* get a handle to the stream in the media */ - if (!(stream = gst_rtsp_session_media_get_stream (media, streamid))) - goto no_stream; + /* set blocksize on this stream */ + if (!handle_blocksize (media, stream, state->request)) + goto invalid_blocksize; - st = gst_rtsp_session_stream_set_transport (stream, ct); + /* update the client transport */ + 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); /* configure keepalive for this transport */ - gst_rtsp_session_stream_set_keepalive (stream, + 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); /* construct the response now */ code = GST_RTSP_STS_OK; - gst_rtsp_message_init_response (&response, code, + gst_rtsp_message_init_response (state->response, code, gst_rtsp_status_as_text (code), state->request); - gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str); + gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT, + trans_str); g_free (trans_str); - send_response (client, session, &response); + send_response (client, session, state->response, FALSE); /* update the state */ - switch (media->state) { + rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia); + switch (rtspstate) { 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; + gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY); break; } g_object_unref (session); + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], + 0, state); + return TRUE; /* ERRORS */ bad_request: { + GST_ERROR ("client %p: bad request", client); send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state); return FALSE; } not_found: { + GST_ERROR ("client %p: media not found", client); send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); g_object_unref (session); + gst_rtsp_transport_free (ct); return FALSE; } -no_stream: +invalid_blocksize: { - send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state); - g_object_unref (media); + GST_ERROR ("client %p: invalid blocksize", client); + send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state); + g_object_unref (session); + gst_rtsp_transport_free (ct); + return FALSE; + } +unsupported_client_transport: + { + GST_ERROR ("client %p: unsupported client transport", client); + send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state); g_object_unref (session); + gst_rtsp_transport_free (ct); return FALSE; } no_transport: { + GST_ERROR ("client %p: no transport", client); send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state); return FALSE; } unsupported_transports: { + GST_ERROR ("client %p: unsupported transports", client); send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state); gst_rtsp_transport_free (ct); return FALSE; } no_pool: { - send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state); + GST_ERROR ("client %p: no session pool configured", client); + send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state); + gst_rtsp_transport_free (ct); return FALSE; } service_unavailable: { + GST_ERROR ("client %p: can't create session", client); send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state); + gst_rtsp_transport_free (ct); return FALSE; } } @@ -1082,6 +1376,7 @@ service_unavailable: static GstSDPMessage * create_sdp (GstRTSPClient * client, GstRTSPMedia * media) { + GstRTSPClientPrivate *priv = client->priv; GstSDPMessage *sdp; GstSDPInfo info; const gchar *proto; @@ -1091,13 +1386,13 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media) /* some standard things first */ gst_sdp_message_set_version (sdp, "0"); - if (client->is_ipv6) + if (priv->is_ipv6) proto = "IP6"; else proto = "IP4"; gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto, - client->server_ip); + priv->server_ip); gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer"); gst_sdp_message_set_information (sdp, "rtsp-server"); @@ -1107,20 +1402,21 @@ create_sdp (GstRTSPClient * client, GstRTSPMedia * media) gst_sdp_message_add_attribute (sdp, "control", "*"); info.server_proto = proto; - if (media->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) - info.server_ip = MCAST_ADDRESS; - else - info.server_ip = client->server_ip; + info.server_ip = g_strdup (priv->server_ip); /* create an SDP for the media object */ if (!gst_rtsp_sdp_from_media (sdp, &info, media)) goto no_sdp; + g_free (info.server_ip); + return sdp; /* ERRORS */ no_sdp: { + GST_ERROR ("client %p: could not create SDP", client); + g_free (info.server_ip); gst_sdp_message_free (sdp); return NULL; } @@ -1130,12 +1426,14 @@ no_sdp: static gboolean handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state) { - GstRTSPMessage response = { 0 }; GstRTSPResult res; GstSDPMessage *sdp; guint i, str_len; gchar *str, *content_base; GstRTSPMedia *media; + GstRTSPClientClass *klass; + + klass = GST_RTSP_CLIENT_GET_CLASS (client); /* check what kind of format is accepted, we don't really do anything with it * and always return SDP for now. */ @@ -1157,15 +1455,15 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state) goto no_media; /* create an SDP for the media object on this client */ - if (!(sdp = create_sdp (client, media))) + if (!(sdp = klass->create_sdp (client, media))) goto no_sdp; g_object_unref (media); - gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, + gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK, gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request); - gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONTENT_TYPE, + gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE, "application/sdp"); /* content base for some clients that might screw up creating the setup uri */ @@ -1185,27 +1483,32 @@ handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state) GST_INFO ("adding content-base: %s", content_base); - gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONTENT_BASE, + gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE, content_base); g_free (content_base); /* add SDP to the response body */ str = gst_sdp_message_as_text (sdp); - gst_rtsp_message_take_body (&response, (guint8 *) str, strlen (str)); + gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str)); gst_sdp_message_free (sdp); - send_response (client, state->session, &response); + send_response (client, state->session, state->response, FALSE); + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST], + 0, state); return TRUE; /* ERRORS */ no_media: { + GST_ERROR ("client %p: no media", client); /* error reply is already sent */ return FALSE; } no_sdp: { + GST_ERROR ("client %p: can't create SDP", client); send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state); g_object_unref (media); return FALSE; @@ -1215,7 +1518,6 @@ no_sdp: static gboolean handle_options_request (GstRTSPClient * client, GstRTSPClientState * state) { - GstRTSPMessage response = { 0 }; GstRTSPMethod options; gchar *str; @@ -1228,13 +1530,16 @@ handle_options_request (GstRTSPClient * client, GstRTSPClientState * state) str = gst_rtsp_options_as_text (options); - gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, + gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK, gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request); - gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str); + gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str); g_free (str); - send_response (client, state->session, &response); + send_response (client, state->session, state->response, FALSE); + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST], + 0, state); return TRUE; } @@ -1269,13 +1574,15 @@ sanitize_uri (GstRTSPUrl * uri) static void client_session_finalized (GstRTSPClient * client, GstRTSPSession * session) { + GstRTSPClientPrivate *priv = client->priv; + GST_INFO ("client %p: session %p finished", client, session); /* unlink all media managed in this session */ client_unlink_session (client, session); /* remove the session */ - if (!(client->sessions = g_list_remove (client->sessions, session))) { + if (!(priv->sessions = g_list_remove (priv->sessions, session))) { GST_INFO ("client %p: all sessions finalized, close the connection", client); close_connection (client); @@ -1285,9 +1592,10 @@ client_session_finalized (GstRTSPClient * client, GstRTSPSession * session) static void client_watch_session (GstRTSPClient * client, GstRTSPSession * session) { + GstRTSPClientPrivate *priv = client->priv; GList *walk; - for (walk = client->sessions; walk; walk = g_list_next (walk)) { + for (walk = priv->sessions; walk; walk = g_list_next (walk)) { GstRTSPSession *msession = (GstRTSPSession *) walk->data; /* we already know about this session */ @@ -1299,22 +1607,28 @@ client_watch_session (GstRTSPClient * client, GstRTSPSession * session) g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized, client); - client->sessions = g_list_prepend (client->sessions, session); + priv->sessions = g_list_prepend (priv->sessions, session); + + g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0, + session); } static void handle_request (GstRTSPClient * client, GstRTSPMessage * request) { + GstRTSPClientPrivate *priv = client->priv; GstRTSPMethod method; const gchar *uristr; - GstRTSPUrl *uri; + GstRTSPUrl *uri = NULL; GstRTSPVersion version; GstRTSPResult res; - GstRTSPSession *session; + GstRTSPSession *session = NULL; GstRTSPClientState state = { NULL }; + GstRTSPMessage response = { 0 }; gchar *sessid; state.request = request; + state.response = &response; if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) { gst_rtsp_message_dump (request); @@ -1324,45 +1638,39 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request) gst_rtsp_message_parse_request (request, &method, &uristr, &version); - if (version != GST_RTSP_VERSION_1_0) { - /* we can only handle 1.0 requests */ - send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED, - &state); - return; - } + /* we can only handle 1.0 requests */ + if (version != GST_RTSP_VERSION_1_0) + goto not_supported; + state.method = method; /* we always try to parse the url first */ - if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) { - send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state); - return; - } - - /* sanitize the uri */ - sanitize_uri (uri); - state.uri = uri; + if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) + goto bad_request; /* get the session if there is any */ res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0); if (res == GST_RTSP_OK) { - if (client->session_pool == NULL) + if (priv->session_pool == NULL) goto no_pool; /* we had a session in the request, find it again */ - if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid))) + if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid))) goto session_not_found; /* we add the session to the client list of watched sessions. When a session * disappears because it times out, we will be notified. If all sessions are * gone, we will close the connection */ client_watch_session (client, session); - } else - session = NULL; + } + /* sanitize the uri */ + sanitize_uri (uri); + state.uri = uri; state.session = session; - if (client->auth) { - if (!gst_rtsp_auth_check (client->auth, client, &state)) + if (priv->auth) { + if (!gst_rtsp_auth_check (priv->auth, client, 0, &state)) goto not_authorized; } @@ -1395,40 +1703,63 @@ handle_request (GstRTSPClient * client, GstRTSPMessage * request) case GST_RTSP_ANNOUNCE: case GST_RTSP_RECORD: case GST_RTSP_REDIRECT: - send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state); - break; + goto not_implemented; case GST_RTSP_INVALID: default: - send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state); - break; + goto bad_request; } + +done: if (session) g_object_unref (session); - - gst_rtsp_url_free (uri); + if (uri) + gst_rtsp_url_free (uri); return; /* ERRORS */ +not_supported: + { + GST_ERROR ("client %p: version %d not supported", client, version); + send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED, + &state); + goto done; + } +bad_request: + { + GST_ERROR ("client %p: bad request", client); + send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state); + goto done; + } no_pool: { - send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state); - return; + GST_ERROR ("client %p: no pool configured", client); + send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state); + goto done; } session_not_found: { + GST_ERROR ("client %p: session not found", client); send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state); - return; + goto done; } not_authorized: { - handle_unauthorized_request (client, client->auth, &state); - return; + GST_ERROR ("client %p: not allowed", client); + handle_unauthorized_request (client, priv->auth, &state); + goto done; + } +not_implemented: + { + GST_ERROR ("client %p: method %d not implemented", client, method); + send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state); + goto done; } } static void handle_data (GstRTSPClient * client, GstRTSPMessage * message) { + GstRTSPClientPrivate *priv = client->priv; GstRTSPResult res; guint8 channel; GList *walk; @@ -1444,34 +1775,28 @@ handle_data (GstRTSPClient * client, GstRTSPMessage * message) gst_rtsp_message_steal_body (message, &data, &size); - buffer = gst_buffer_new (); - GST_BUFFER_DATA (buffer) = data; - GST_BUFFER_MALLOCDATA (buffer) = data; - GST_BUFFER_SIZE (buffer) = size; + buffer = gst_buffer_new_wrapped (data, size); handled = FALSE; - for (walk = client->streams; walk; walk = g_list_next (walk)) { - GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data; - GstRTSPMediaStream *mstream; - GstRTSPTransport *tr; + for (walk = priv->transports; walk; walk = g_list_next (walk)) { + GstRTSPStreamTransport *trans; + GstRTSPStream *stream; + const GstRTSPTransport *tr; - /* get the transport, if there is no transport configured, skip this stream */ - if (!(tr = stream->trans.transport)) - continue; + trans = walk->data; - /* we also need a media stream */ - if (!(mstream = stream->media_stream)) - continue; + tr = gst_rtsp_stream_transport_get_transport (trans); + stream = gst_rtsp_stream_transport_get_stream (trans); /* check for TCP transport */ if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) { /* dispatch to the stream based on the channel number */ if (tr->interleaved.min == channel) { - gst_rtsp_media_stream_rtp (mstream, buffer); + gst_rtsp_stream_recv_rtp (stream, buffer); handled = TRUE; break; } else if (tr->interleaved.max == channel) { - gst_rtsp_media_stream_rtcp (mstream, buffer); + gst_rtsp_stream_recv_rtcp (stream, buffer); handled = TRUE; break; } @@ -1495,15 +1820,22 @@ gst_rtsp_client_set_session_pool (GstRTSPClient * client, GstRTSPSessionPool * pool) { GstRTSPSessionPool *old; + GstRTSPClientPrivate *priv; - old = client->session_pool; - if (old != pool) { - if (pool) - g_object_ref (pool); - client->session_pool = pool; - if (old) - g_object_unref (old); - } + g_return_if_fail (GST_IS_RTSP_CLIENT (client)); + + priv = client->priv; + + if (pool) + g_object_ref (pool); + + g_mutex_lock (&priv->lock); + old = priv->session_pool; + priv->session_pool = pool; + g_mutex_unlock (&priv->lock); + + if (old) + g_object_unref (old); } /** @@ -1512,103 +1844,130 @@ gst_rtsp_client_set_session_pool (GstRTSPClient * client, * * Get the #GstRTSPSessionPool object that @client uses to manage its sessions. * - * Returns: a #GstRTSPSessionPool, unref after usage. + * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage. */ GstRTSPSessionPool * gst_rtsp_client_get_session_pool (GstRTSPClient * client) { + GstRTSPClientPrivate *priv; GstRTSPSessionPool *result; - if ((result = client->session_pool)) + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL); + + priv = client->priv; + + g_mutex_lock (&priv->lock); + if ((result = priv->session_pool)) g_object_ref (result); + g_mutex_unlock (&priv->lock); return result; } /** - * gst_rtsp_client_set_server: + * gst_rtsp_client_set_mount_points: * @client: a #GstRTSPClient - * @server: a #GstRTSPServer + * @mounts: a #GstRTSPMountPoints * - * Set @server as the server that created @client. + * Set @mounts as the mount points for @client which it will use to map urls + * to media streams. These mount points are usually inherited from the server that + * created the client but can be overriden later. */ void -gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server) +gst_rtsp_client_set_mount_points (GstRTSPClient * client, + GstRTSPMountPoints * mounts) { - GstRTSPServer *old; + GstRTSPClientPrivate *priv; + GstRTSPMountPoints *old; - old = client->server; - if (old != server) { - if (server) - g_object_ref (server); - client->server = server; - if (old) - g_object_unref (old); - } + g_return_if_fail (GST_IS_RTSP_CLIENT (client)); + + priv = client->priv; + + if (mounts) + g_object_ref (mounts); + + g_mutex_lock (&priv->lock); + old = priv->mount_points; + priv->mount_points = mounts; + g_mutex_unlock (&priv->lock); + + if (old) + g_object_unref (old); } /** - * gst_rtsp_client_get_server: + * gst_rtsp_client_get_mount_points: * @client: a #GstRTSPClient * - * Get the #GstRTSPServer object that @client was created from. + * Get the #GstRTSPMountPoints object that @client uses to manage its sessions. * - * Returns: a #GstRTSPServer, unref after usage. + * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage. */ -GstRTSPServer * -gst_rtsp_client_get_server (GstRTSPClient * client) +GstRTSPMountPoints * +gst_rtsp_client_get_mount_points (GstRTSPClient * client) { - GstRTSPServer *result; + GstRTSPClientPrivate *priv; + GstRTSPMountPoints *result; + + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL); - if ((result = client->server)) + priv = client->priv; + + g_mutex_lock (&priv->lock); + if ((result = priv->mount_points)) g_object_ref (result); + g_mutex_unlock (&priv->lock); return result; } /** - * gst_rtsp_client_set_media_mapping: + * gst_rtsp_client_set_use_client_settings: * @client: a #GstRTSPClient - * @mapping: a #GstRTSPMediaMapping + * @use_client_settings: whether to use client settings for multicast * - * Set @mapping as the media mapping for @client which it will use to map urls - * to media streams. These mapping is usually inherited from the server that - * created the client but can be overriden later. + * Use client transport settings (destination and ttl) for multicast. + * When @use_client_settings is %FALSE, the server settings will be + * used. */ void -gst_rtsp_client_set_media_mapping (GstRTSPClient * client, - GstRTSPMediaMapping * mapping) +gst_rtsp_client_set_use_client_settings (GstRTSPClient * client, + gboolean use_client_settings) { - GstRTSPMediaMapping *old; + GstRTSPClientPrivate *priv; - old = client->media_mapping; + g_return_if_fail (GST_IS_RTSP_CLIENT (client)); - if (old != mapping) { - if (mapping) - g_object_ref (mapping); - client->media_mapping = mapping; - if (old) - g_object_unref (old); - } + priv = client->priv; + + g_mutex_lock (&priv->lock); + priv->use_client_settings = use_client_settings; + g_mutex_unlock (&priv->lock); } /** - * gst_rtsp_client_get_media_mapping: + * gst_rtsp_client_get_use_client_settings: * @client: a #GstRTSPClient * - * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions. - * - * Returns: a #GstRTSPMediaMapping, unref after usage. + * Check if client transport settings (destination and ttl) for multicast + * will be used. */ -GstRTSPMediaMapping * -gst_rtsp_client_get_media_mapping (GstRTSPClient * client) +gboolean +gst_rtsp_client_get_use_client_settings (GstRTSPClient * client) { - GstRTSPMediaMapping *result; + GstRTSPClientPrivate *priv; + gboolean res; - if ((result = client->media_mapping)) - g_object_ref (result); + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE); - return result; + priv = client->priv; + + g_mutex_lock (&priv->lock); + res = priv->use_client_settings; + g_mutex_unlock (&priv->lock); + + return res; } /** @@ -1621,19 +1980,23 @@ gst_rtsp_client_get_media_mapping (GstRTSPClient * client) void gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth) { + GstRTSPClientPrivate *priv; GstRTSPAuth *old; g_return_if_fail (GST_IS_RTSP_CLIENT (client)); - old = client->auth; + priv = client->priv; - if (old != auth) { - if (auth) - g_object_ref (auth); - client->auth = auth; - if (old) - g_object_unref (old); - } + if (auth) + g_object_ref (auth); + + g_mutex_lock (&priv->lock); + old = priv->auth; + priv->auth = auth; + g_mutex_unlock (&priv->lock); + + if (old) + g_object_unref (old); } @@ -1643,27 +2006,121 @@ gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth) * * Get the #GstRTSPAuth used as the authentication manager of @client. * - * Returns: the #GstRTSPAuth of @client. g_object_unref() after + * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after * usage. */ GstRTSPAuth * gst_rtsp_client_get_auth (GstRTSPClient * client) { + GstRTSPClientPrivate *priv; GstRTSPAuth *result; g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL); - if ((result = client->auth)) + priv = client->priv; + + g_mutex_lock (&priv->lock); + if ((result = priv->auth)) g_object_ref (result); + g_mutex_unlock (&priv->lock); return result; } -static GstRTSPResult -message_received (GstRTSPWatch * watch, GstRTSPMessage * message, - gpointer user_data) +/** + * gst_rtsp_client_get_uri: + * @client: a #GstRTSPClient + * + * Get the #GstRTSPUrl of @client. + * + * Returns: (transfer full): the #GstRTSPUrl of @client. Free with + * gst_rtsp_url_free () after usage. + */ +GstRTSPUrl * +gst_rtsp_client_get_uri (GstRTSPClient * client) { - GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + GstRTSPClientPrivate *priv; + GstRTSPUrl *result = NULL; + + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL); + + priv = client->priv; + + g_mutex_lock (&priv->lock); + if (priv->uri != NULL) + result = gst_rtsp_url_copy (priv->uri); + g_mutex_unlock (&priv->lock); + + return result; +} + +/** + * gst_rtsp_client_get_connection: + * @client: a #GstRTSPClient + * + * Get the #GstRTSPConnection of @client. + * + * Returns: (transfer none): the #GstRTSPConnection of @client. + * The connection object returned remains valid until the client is freed. + */ +GstRTSPConnection * +gst_rtsp_client_get_connection (GstRTSPClient * client) +{ + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL); + + return client->priv->connection; +} + +/** + * gst_rtsp_client_set_send_func: + * @client: a #GstRTSPClient + * @func: a #GstRTSPClientSendFunc + * @user_data: user data passed to @func + * @notify: called when @user_data is no longer in use + * + * Set @func as the callback that will be called when a new message needs to be + * sent to the client. @user_data is passed to @func and @notify is called when + * @user_data is no longer in use. + */ +void +gst_rtsp_client_set_send_func (GstRTSPClient * client, + GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify) +{ + GstRTSPClientPrivate *priv; + GDestroyNotify old_notify; + gpointer old_data; + + g_return_if_fail (GST_IS_RTSP_CLIENT (client)); + + priv = client->priv; + + g_mutex_lock (&priv->send_lock); + priv->send_func = func; + old_notify = priv->send_notify; + old_data = priv->send_data; + priv->send_notify = notify; + priv->send_data = user_data; + g_mutex_unlock (&priv->send_lock); + + if (old_notify) + old_notify (old_data); +} + +/** + * gst_rtsp_client_handle_message: + * @client: a #GstRTSPClient + * @message: an #GstRTSPMessage + * + * Let the client handle @message. + * + * Returns: a #GstRTSPResult. + */ +GstRTSPResult +gst_rtsp_client_handle_message (GstRTSPClient * client, + GstRTSPMessage * message) +{ + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL); + g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL); switch (message->type) { case GST_RTSP_MESSAGE_REQUEST: @@ -1681,13 +2138,34 @@ message_received (GstRTSPWatch * watch, GstRTSPMessage * message, } static GstRTSPResult -message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data) +do_send_message (GstRTSPClient * client, GstRTSPMessage * message, + gboolean close, gpointer user_data) { - GstRTSPClient *client; + GstRTSPClientPrivate *priv = client->priv; + + /* send the response and store the seq number so we can wait until it's + * written to the client to close the connection */ + return gst_rtsp_watch_send_message (priv->watch, message, close ? + &priv->close_seq : NULL); +} - client = GST_RTSP_CLIENT (user_data); +static GstRTSPResult +message_received (GstRTSPWatch * watch, GstRTSPMessage * message, + gpointer user_data) +{ + return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message); +} + +static GstRTSPResult +message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data) +{ + GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + GstRTSPClientPrivate *priv = client->priv; - /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */ + if (priv->close_seq && priv->close_seq == cseq) { + priv->close_seq = 0; + close_connection (client); + } return GST_RTSP_OK; } @@ -1696,17 +2174,20 @@ static GstRTSPResult closed (GstRTSPWatch * watch, gpointer user_data) { GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + GstRTSPClientPrivate *priv = client->priv; const gchar *tunnelid; GST_INFO ("client %p: connection closed", client); - if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) { - g_mutex_lock (tunnels_lock); + if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) { + g_mutex_lock (&tunnels_lock); /* remove from tunnelids */ g_hash_table_remove (tunnels, tunnelid); - g_mutex_unlock (tunnels_lock); + g_mutex_unlock (&tunnels_lock); } + gst_rtsp_client_set_send_func (client, NULL, NULL, NULL); + return GST_RTSP_OK; } @@ -1742,22 +2223,23 @@ error_full (GstRTSPWatch * watch, GstRTSPResult result, static gboolean remember_tunnel (GstRTSPClient * client) { + GstRTSPClientPrivate *priv = client->priv; const gchar *tunnelid; /* store client in the pending tunnels */ - tunnelid = gst_rtsp_connection_get_tunnelid (client->connection); + tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection); if (tunnelid == NULL) goto no_tunnelid; GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid); /* we can't have two clients connecting with the same tunnelid */ - g_mutex_lock (tunnels_lock); + g_mutex_lock (&tunnels_lock); if (g_hash_table_lookup (tunnels, tunnelid)) goto tunnel_existed; g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client)); - g_mutex_unlock (tunnels_lock); + g_mutex_unlock (&tunnels_lock); return TRUE; @@ -1769,7 +2251,7 @@ no_tunnelid: } tunnel_existed: { - g_mutex_unlock (tunnels_lock); + g_mutex_unlock (&tunnels_lock); GST_ERROR ("client %p: tunnel session %s already existed", client, tunnelid); return FALSE; @@ -1779,12 +2261,11 @@ tunnel_existed: static GstRTSPStatusCode tunnel_start (GstRTSPWatch * watch, gpointer user_data) { - GstRTSPClient *client; - - client = GST_RTSP_CLIENT (user_data); + GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + GstRTSPClientPrivate *priv = client->priv; GST_INFO ("client %p: tunnel start (connection %p)", client, - client->connection); + priv->connection); if (!remember_tunnel (client)) goto tunnel_error; @@ -1802,12 +2283,11 @@ tunnel_error: static GstRTSPResult tunnel_lost (GstRTSPWatch * watch, gpointer user_data) { - GstRTSPClient *client; - - client = GST_RTSP_CLIENT (user_data); + GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + GstRTSPClientPrivate *priv = client->priv; - GST_INFO ("client %p: tunnel lost (connection %p)", client, - client->connection); + GST_WARNING ("client %p: tunnel lost (connection %p)", client, + priv->connection); /* ignore error, it'll only be a problem when the client does a POST again */ remember_tunnel (client); @@ -1820,16 +2300,18 @@ tunnel_complete (GstRTSPWatch * watch, gpointer user_data) { const gchar *tunnelid; GstRTSPClient *client = GST_RTSP_CLIENT (user_data); + GstRTSPClientPrivate *priv = client->priv; GstRTSPClient *oclient; + GstRTSPClientPrivate *opriv; GST_INFO ("client %p: tunnel complete", client); /* find previous tunnel */ - tunnelid = gst_rtsp_connection_get_tunnelid (client->connection); + tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection); if (tunnelid == NULL) goto no_tunnelid; - g_mutex_lock (tunnels_lock); + g_mutex_lock (&tunnels_lock); if (!(oclient = g_hash_table_lookup (tunnels, tunnelid))) goto no_tunnel; @@ -1838,43 +2320,40 @@ tunnel_complete (GstRTSPWatch * watch, gpointer user_data) g_object_ref (oclient); g_hash_table_remove (tunnels, tunnelid); - if (oclient->watch == NULL) + opriv = oclient->priv; + + if (opriv->watch == NULL) goto tunnel_closed; - g_mutex_unlock (tunnels_lock); + g_mutex_unlock (&tunnels_lock); GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient, - oclient->connection, client->connection); + opriv->connection, priv->connection); /* merge the tunnels into the first client */ - gst_rtsp_connection_do_tunnel (oclient->connection, client->connection); - gst_rtsp_watch_reset (oclient->watch); + gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection); + gst_rtsp_watch_reset (opriv->watch); g_object_unref (oclient); - /* we don't need this watch anymore */ - g_source_destroy ((GSource *) client->watch); - client->watchid = 0; - client->watch = NULL; - return GST_RTSP_OK; /* ERRORS */ no_tunnelid: { - GST_INFO ("client %p: no tunnelid provided", client); - return GST_RTSP_STS_SERVICE_UNAVAILABLE; + GST_ERROR ("client %p: no tunnelid provided", client); + return GST_RTSP_ERROR; } no_tunnel: { - g_mutex_unlock (tunnels_lock); - GST_INFO ("client %p: tunnel session %s not found", client, tunnelid); - return GST_RTSP_STS_SERVICE_UNAVAILABLE; + g_mutex_unlock (&tunnels_lock); + GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid); + return GST_RTSP_ERROR; } tunnel_closed: { - g_mutex_unlock (tunnels_lock); - GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid); + g_mutex_unlock (&tunnels_lock); + GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid); g_object_unref (oclient); - return GST_RTSP_STS_SERVICE_UNAVAILABLE; + return GST_RTSP_ERROR; } } @@ -1892,101 +2371,175 @@ static GstRTSPWatchFuncs watch_funcs = { static void client_watch_notify (GstRTSPClient * client) { + GstRTSPClientPrivate *priv = client->priv; + GST_INFO ("client %p: watch destroyed", client); - client->watchid = 0; - client->watch = NULL; + priv->watch = NULL; g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL); g_object_unref (client); } -/** - * gst_rtsp_client_attach: - * @client: a #GstRTSPClient - * @channel: a #GIOChannel - * - * Accept a new connection for @client on the socket in @channel. - * - * 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_client_accept (GstRTSPClient * client, GIOChannel * channel) +static gboolean +setup_client (GstRTSPClient * client, GSocket * socket, + GstRTSPConnection * conn, GError ** error) { - int sock, fd; - GstRTSPConnection *conn; - GstRTSPResult res; - GSource *source; - GMainContext *context; + GstRTSPClientPrivate *priv = client->priv; + GSocket *read_socket; + GSocketAddress *address; GstRTSPUrl *url; - struct sockaddr_storage addr; - socklen_t addrlen; - gchar ip[INET6_ADDRSTRLEN]; - - /* a new client connected. */ - sock = g_io_channel_unix_get_fd (channel); - - GST_RTSP_CHECK (gst_rtsp_connection_accept (sock, &conn), accept_failed); - fd = gst_rtsp_connection_get_readfd (conn); + read_socket = gst_rtsp_connection_get_read_socket (conn); + priv->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6; - addrlen = sizeof (addr); - if (getsockname (fd, (struct sockaddr *) &addr, &addrlen) < 0) - goto getpeername_failed; + if (!(address = g_socket_get_remote_address (read_socket, error))) + goto no_address; - client->is_ipv6 = addr.ss_family == AF_INET6; + g_free (priv->server_ip); + /* keep the original ip that the client connected to */ + if (G_IS_INET_SOCKET_ADDRESS (address)) { + GInetAddress *iaddr; - if (getnameinfo ((struct sockaddr *) &addr, addrlen, ip, sizeof (ip), NULL, 0, - NI_NUMERICHOST) != 0) - goto getnameinfo_failed; + iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address)); - /* keep the original ip that the client connected to */ - g_free (client->server_ip); - client->server_ip = g_strndup (ip, sizeof (ip)); + priv->server_ip = g_inet_address_to_string (iaddr); + g_object_unref (address); + } else { + priv->server_ip = g_strdup ("unknown"); + } GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client, - client->server_ip, client->is_ipv6); + priv->server_ip, priv->is_ipv6); url = gst_rtsp_connection_get_url (conn); GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port); - client->connection = conn; + priv->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); + return TRUE; - /* find the context to add the watch */ - if ((source = g_main_current_source ())) - context = g_source_get_context (source); - else - context = NULL; + /* ERRORS */ +no_address: + { + GST_ERROR ("could not get remote address %s", (*error)->message); + return FALSE; + } +} - GST_INFO ("attaching to context %p", context); +/** + * 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 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_use_socket (GstRTSPClient * client, GSocket * socket, + const gchar * ip, gint port, const gchar * initial_buffer, GError ** error) +{ + GstRTSPConnection *conn; + GstRTSPResult res; - client->watchid = gst_rtsp_watch_attach (client->watch, context); - gst_rtsp_watch_unref (client->watch); + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE); + g_return_val_if_fail (G_IS_SOCKET (socket), FALSE); - return TRUE; + GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port, + initial_buffer, &conn), no_connection); + + return setup_client (client, socket, conn, error); /* ERRORS */ -accept_failed: +no_connection: { gchar *str = gst_rtsp_strresult (res); - GST_ERROR ("Could not accept client on server socket %d: %s", sock, str); + GST_ERROR ("could not create connection from socket %p: %s", socket, str); g_free (str); return FALSE; } -getpeername_failed: - { - GST_ERROR ("getpeername failed: %s", g_strerror (errno)); - return FALSE; - } -getnameinfo_failed: +} + +/** + * 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. + * + * Returns: %TRUE if the client could be accepted. + */ +gboolean +gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket, + GCancellable * cancellable, GError ** error) +{ + GstRTSPConnection *conn; + GstRTSPResult res; + + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE); + g_return_val_if_fail (G_IS_SOCKET (socket), FALSE); + + /* a new client connected. */ + GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable), + accept_failed); + + return setup_client (client, socket, conn, error); + + /* ERRORS */ +accept_failed: { - GST_ERROR ("getnameinfo failed: %s", g_strerror (errno)); + gchar *str = gst_rtsp_strresult (res); + + GST_ERROR ("Could not accept client on server socket %p: %s", socket, str); + g_free (str); 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) +{ + GstRTSPClientPrivate *priv; + guint res; + + g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0); + priv = client->priv; + g_return_val_if_fail (priv->watch == NULL, 0); + + /* create watch for the connection and attach */ + priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs, + g_object_ref (client), (GDestroyNotify) client_watch_notify); + gst_rtsp_client_set_send_func (client, do_send_message, priv->watch, + (GDestroyNotify) gst_rtsp_watch_unref); + + /* FIXME make this configurable. We don't want to do this yet because it will + * be superceeded by a cache object later */ + gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100); + + GST_INFO ("attaching to context %p", context); + res = gst_rtsp_watch_attach (priv->watch, context); + + return res; +}