2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "rtsp-session.h"
24 static void gst_rtsp_session_finalize (GObject * obj);
26 G_DEFINE_TYPE (GstRTSPSession, gst_rtsp_session, G_TYPE_OBJECT);
29 gst_rtsp_session_class_init (GstRTSPSessionClass * klass)
31 GObjectClass *gobject_class;
33 gobject_class = G_OBJECT_CLASS (klass);
35 gobject_class->finalize = gst_rtsp_session_finalize;
39 gst_rtsp_session_init (GstRTSPSession * session)
44 gst_rtsp_session_free_stream (GstRTSPSessionStream *stream)
46 if (stream->client_trans)
47 gst_rtsp_transport_free (stream->client_trans);
48 g_free (stream->destination);
49 if (stream->server_trans)
50 gst_rtsp_transport_free (stream->server_trans);
52 if (stream->udpsrc[0])
53 gst_object_unref (stream->udpsrc[0]);
59 gst_rtsp_session_free_media (GstRTSPSessionMedia *media)
63 gst_element_set_state (media->pipeline, GST_STATE_NULL);
66 g_object_unref (media->media);
68 for (walk = media->streams; walk; walk = g_list_next (walk)) {
69 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
71 gst_rtsp_session_free_stream (stream);
74 gst_object_unref (media->pipeline);
75 g_list_free (media->streams);
79 gst_rtsp_session_finalize (GObject * obj)
81 GstRTSPSession *session;
84 session = GST_RTSP_SESSION (obj);
86 g_free (session->sessionid);
88 for (walk = session->medias; walk; walk = g_list_next (walk)) {
89 GstRTSPSessionMedia *media = (GstRTSPSessionMedia *) walk->data;
91 gst_rtsp_session_free_media (media);
93 g_list_free (session->medias);
95 G_OBJECT_CLASS (gst_rtsp_session_parent_class)->finalize (obj);
99 * gst_rtsp_session_get_media:
100 * @sess: a #GstRTSPSession
101 * @media: a #GstRTSPSessionMedia
103 * Get or create the session information for @media.
105 * Returns: the configuration for @media in @sess.
107 GstRTSPSessionMedia *
108 gst_rtsp_session_get_media (GstRTSPSession *sess, GstRTSPMedia *media)
110 GstRTSPSessionMedia *result;
113 for (walk = sess->medias; walk; walk = g_list_next (walk)) {
114 result = (GstRTSPSessionMedia *) walk->data;
116 if (result->media == media)
121 if (result == NULL) {
122 result = g_new0 (GstRTSPSessionMedia, 1);
123 result->media = media;
124 result->pipeline = gst_pipeline_new ("pipeline");
126 /* prepare media into the pipeline */
127 if (!gst_rtsp_media_prepare (media, GST_BIN (result->pipeline)))
130 result->rtpbin = gst_element_factory_make ("gstrtpbin", "rtpbin");
132 /* add stuf to the bin */
133 gst_bin_add (GST_BIN (result->pipeline), result->rtpbin);
135 gst_element_set_state (result->pipeline, GST_STATE_READY);
137 sess->medias = g_list_prepend (sess->medias, result);
144 gst_rtsp_session_free_media (result);
150 * gst_rtsp_session_get_stream:
151 * @media: a #GstRTSPSessionMedia
152 * @idx: the stream index
154 * Get a previously created or create a new #GstRTSPSessionStream at @idx.
156 * Returns: a #GstRTSPSessionStream that is valid until the session of @media
159 GstRTSPSessionStream *
160 gst_rtsp_session_get_stream (GstRTSPSessionMedia *media, guint idx)
162 GstRTSPSessionStream *result;
165 for (walk = media->streams; walk; walk = g_list_next (walk)) {
166 result = (GstRTSPSessionStream *) walk->data;
168 if (result->idx == idx)
173 if (result == NULL) {
174 result = g_new0 (GstRTSPSessionStream, 1);
176 result->media = media;
177 result->media_stream = gst_rtsp_media_get_stream (media->media, idx);
179 media->streams = g_list_prepend (media->streams, result);
185 * gst_rtsp_session_new:
187 * Create a new #GstRTSPSession instance.
190 gst_rtsp_session_new (const gchar *sessionid)
192 GstRTSPSession *result;
194 result = g_object_new (GST_TYPE_RTSP_SESSION, NULL);
195 result->sessionid = g_strdup (sessionid);
201 alloc_udp_ports (GstRTSPSessionStream * stream)
203 GstStateChangeReturn ret;
204 GstElement *udpsrc0, *udpsrc1;
205 GstElement *udpsink0, *udpsink1;
206 gint tmp_rtp, tmp_rtcp;
208 gint rtpport, rtcpport, sockfd;
217 /* Start with random port */
220 /* try to allocate 2 UDP ports, the RTP port should be an even
221 * number and the RTCP port should be the next (uneven) port */
223 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, "udp://0.0.0.0", NULL);
225 goto no_udp_protocol;
226 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
228 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
229 if (ret == GST_STATE_CHANGE_FAILURE) {
235 gst_element_set_state (udpsrc0, GST_STATE_NULL);
236 gst_object_unref (udpsrc0);
240 goto no_udp_protocol;
243 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
245 /* check if port is even */
246 if ((tmp_rtp & 1) != 0) {
247 /* port not even, close and allocate another */
251 gst_element_set_state (udpsrc0, GST_STATE_NULL);
252 gst_object_unref (udpsrc0);
258 /* allocate port+1 for RTCP now */
259 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, "udp://0.0.0.0", NULL);
261 goto no_udp_rtcp_protocol;
264 tmp_rtcp = tmp_rtp + 1;
265 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
267 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
268 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
269 if (ret == GST_STATE_CHANGE_FAILURE) {
274 gst_element_set_state (udpsrc0, GST_STATE_NULL);
275 gst_object_unref (udpsrc0);
277 gst_element_set_state (udpsrc1, GST_STATE_NULL);
278 gst_object_unref (udpsrc1);
284 /* all fine, do port check */
285 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
286 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
288 /* this should not happen... */
289 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
292 name = g_strdup_printf ("udp://%s:%d", stream->destination, stream->client_trans->client_port.min);
293 udpsink0 = gst_element_make_from_uri (GST_URI_SINK, name, NULL);
297 goto no_udp_protocol;
299 g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
300 g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
301 g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
303 name = g_strdup_printf ("udp://%s:%d", stream->destination, stream->client_trans->client_port.max);
304 udpsink1 = gst_element_make_from_uri (GST_URI_SINK, name, NULL);
308 goto no_udp_protocol;
310 g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
311 g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
312 g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
313 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
314 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
317 /* we keep these elements, we configure all in configure_transport when the
318 * server told us to really use the UDP ports. */
319 stream->udpsrc[0] = gst_object_ref (udpsrc0);
320 stream->udpsrc[1] = gst_object_ref (udpsrc1);
321 stream->udpsink[0] = gst_object_ref (udpsink0);
322 stream->udpsink[1] = gst_object_ref (udpsink1);
323 stream->server_trans->server_port.min = rtpport;
324 stream->server_trans->server_port.max = rtcpport;
326 /* they are ours now */
327 gst_object_sink (udpsrc0);
328 gst_object_sink (udpsrc1);
329 gst_object_sink (udpsink0);
330 gst_object_sink (udpsink1);
343 no_udp_rtcp_protocol:
354 gst_element_set_state (udpsrc0, GST_STATE_NULL);
355 gst_object_unref (udpsrc0);
358 gst_element_set_state (udpsrc1, GST_STATE_NULL);
359 gst_object_unref (udpsrc1);
362 gst_element_set_state (udpsink0, GST_STATE_NULL);
363 gst_object_unref (udpsink0);
366 gst_element_set_state (udpsink1, GST_STATE_NULL);
367 gst_object_unref (udpsink1);
375 * gst_rtsp_session_stream_init_udp:
376 * @stream: a #GstRTSPSessionStream
377 * @ct: a client #GstRTSPTransport
379 * Set @ct as the client transport and create and return a matching server
380 * transport. After this call the needed ports and elements will be created and
383 * Returns: a server transport or NULL if something went wrong.
386 gst_rtsp_session_stream_set_transport (GstRTSPSessionStream *stream,
387 const gchar *destination, GstRTSPTransport *ct)
389 GstRTSPTransport *st;
392 GstRTSPSessionMedia *media;
394 media = stream->media;
396 /* prepare the server transport */
397 gst_rtsp_transport_new (&st);
399 st->trans = ct->trans;
400 st->profile = ct->profile;
401 st->lower_transport = ct->lower_transport;
402 st->client_port = ct->client_port;
404 /* keep track of the transports */
405 g_free (stream->destination);
406 stream->destination = g_strdup (destination);
407 if (stream->client_trans)
408 gst_rtsp_transport_free (stream->client_trans);
409 stream->client_trans = ct;
410 if (stream->server_trans)
411 gst_rtsp_transport_free (stream->server_trans);
412 stream->server_trans = st;
414 alloc_udp_ports (stream);
416 gst_bin_add (GST_BIN (media->pipeline), stream->udpsink[0]);
417 gst_bin_add (GST_BIN (media->pipeline), stream->udpsink[1]);
418 gst_bin_add (GST_BIN (media->pipeline), stream->udpsrc[1]);
420 /* hook up the stream to the RTP session elements. */
421 name = g_strdup_printf ("send_rtp_sink_%d", stream->idx);
422 stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
424 name = g_strdup_printf ("send_rtp_src_%d", stream->idx);
425 stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
427 name = g_strdup_printf ("send_rtcp_src_%d", stream->idx);
428 stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
430 name = g_strdup_printf ("recv_rtcp_sink_%d", stream->idx);
431 stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
434 gst_pad_link (stream->media_stream->srcpad, stream->send_rtp_sink);
435 pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
436 gst_pad_link (stream->send_rtp_src, pad);
437 gst_object_unref (pad);
438 pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
439 gst_pad_link (stream->send_rtcp_src, pad);
440 gst_object_unref (pad);
441 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
442 gst_pad_link (pad, stream->recv_rtcp_sink);
443 gst_object_unref (pad);
449 * gst_rtsp_session_media_play:
450 * @media: a #GstRTSPSessionMedia
452 * Tell the media object @media to start playing and streaming to the client.
454 * Returns: a #GstStateChangeReturn
457 gst_rtsp_session_media_play (GstRTSPSessionMedia *media)
459 GstStateChangeReturn ret;
461 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
467 * gst_rtsp_session_media_pause:
468 * @media: a #GstRTSPSessionMedia
470 * Tell the media object @media to pause.
472 * Returns: a #GstStateChangeReturn
475 gst_rtsp_session_media_pause (GstRTSPSessionMedia *media)
477 GstStateChangeReturn ret;
479 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
485 * gst_rtsp_session_media_stop:
486 * @media: a #GstRTSPSessionMedia
488 * Tell the media object @media to stop playing. After this call the media
489 * cannot be played or paused anymore
491 * Returns: a #GstStateChangeReturn
494 gst_rtsp_session_media_stop (GstRTSPSessionMedia *media)
496 GstStateChangeReturn ret;
498 ret = gst_element_set_state (media->pipeline, GST_STATE_NULL);