gst/rtpmanager/: Some more ghostpad magic.
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpclient.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:element-rtpclient
22  * @short_description: handle media from one RTP client
23  * @see_also: rtpjitterbuffer, rtpbin, rtpsession
24  *
25  * <refsect2>
26  * <para>
27  * This element handles RTP data from one client. It accepts multiple RTP streams that
28  * should be synchronized together.
29  * </para>
30  * <para>
31  * Normally the SSRCs that map to the same CNAME (as given in the RTCP SDES messages)
32  * should be synchronized.
33  * </para>
34  * <title>Example pipelines</title>
35  * <para>
36  * <programlisting>
37  * </programlisting>
38  * </para>
39  * </refsect2>
40  *
41  * Last reviewed on 2007-04-02 (0.10.6)
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 #include <string.h>
48
49 #include "gstrtpclient.h"
50
51 /* elementfactory information */
52 static const GstElementDetails rtpclient_details =
53 GST_ELEMENT_DETAILS ("RTP Client",
54     "Filter/Editor/Video",
55     "Implement an RTP client",
56     "Wim Taymans <wim@fluendo.com>");
57
58 /* sink pads */
59 static GstStaticPadTemplate rtpclient_rtp_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("rtp_sink_%d",
61     GST_PAD_SINK,
62     GST_PAD_REQUEST,
63     GST_STATIC_CAPS ("application/x-rtp")
64     );
65
66 static GstStaticPadTemplate rtpclient_sync_sink_template =
67 GST_STATIC_PAD_TEMPLATE ("sync_sink_%d",
68     GST_PAD_SINK,
69     GST_PAD_REQUEST,
70     GST_STATIC_CAPS ("application/x-rtcp")
71     );
72
73 /* src pads */
74 static GstStaticPadTemplate rtpclient_rtp_src_template =
75 GST_STATIC_PAD_TEMPLATE ("rtp_src_%d_%d",
76     GST_PAD_SRC,
77     GST_PAD_SOMETIMES,
78     GST_STATIC_CAPS ("application/x-rtp")
79     );
80
81 #define GST_RTP_CLIENT_GET_PRIVATE(obj)  \
82    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_CLIENT, GstRTPClientPrivate))
83
84 struct _GstRTPClientPrivate
85 {
86 };
87
88 /* all the info needed to handle the stream with SSRC */
89 typedef struct
90 {
91   GstRTPClient *client;
92
93   /* the SSRC of this stream */
94   guint32 ssrc;
95
96   /* RTP and RTCP in */
97   GstPad *rtp_sink;
98   GstPad *sync_sink;
99
100   /* the jitterbuffer */
101   GstElement *jitterbuffer;
102   /* the payload demuxer */
103   GstElement *ptdemux;
104   /* the new-pad signal */
105   gulong new_pad_sig;
106 } GstRTPClientStream;
107
108 /* the PT demuxer found a new payload type */
109 static void
110 new_pad (GstElement * element, GstPad * pad, GstRTPClientStream * stream)
111 {
112 }
113
114 /* create a new stream for SSRC.
115  *
116  * We create a jitterbuffer and an payload demuxer for the SSRC. The sinkpad of
117  * the jitterbuffer is ghosted to the bin. We connect a pad-added signal to
118  * rtpptdemux so that we can ghost the payload pads outside.
119  *
120  *       +-----------------+     +---------------+
121  *       | rtpjitterbuffer |     |  rtpptdemux   |
122  *   +- sink              src - sink             |
123  *  /    +-----------------+     +---------------+
124  *
125  */
126 static GstRTPClientStream *
127 create_stream (GstRTPClient * rtpclient, guint32 ssrc)
128 {
129   GstRTPClientStream *stream;
130   gchar *name;
131   GstPad *srcpad, *sinkpad;
132   GstPadLinkReturn res;
133
134   stream = g_new0 (GstRTPClientStream, 1);
135   stream->ssrc = ssrc;
136   stream->client = rtpclient;
137
138   stream->jitterbuffer = gst_element_factory_make ("rtpjitterbuffer", NULL);
139   if (!stream->jitterbuffer)
140     goto no_jitterbuffer;
141
142   stream->ptdemux = gst_element_factory_make ("rtpptdemux", NULL);
143   if (!stream->ptdemux)
144     goto no_ptdemux;
145
146   /* add elements to bin */
147   gst_bin_add (GST_BIN_CAST (rtpclient), stream->jitterbuffer);
148   gst_bin_add (GST_BIN_CAST (rtpclient), stream->ptdemux);
149
150   /* link jitterbuffer and PT demuxer */
151   srcpad = gst_element_get_pad (stream->jitterbuffer, "src");
152   sinkpad = gst_element_get_pad (stream->ptdemux, "sink");
153   res = gst_pad_link (srcpad, sinkpad);
154   gst_object_unref (srcpad);
155   gst_object_unref (sinkpad);
156
157   if (res != GST_PAD_LINK_OK)
158     goto could_not_link;
159
160   /* add stream to list */
161   rtpclient->streams = g_list_prepend (rtpclient->streams, stream);
162
163   /* ghost sinkpad */
164   name = g_strdup_printf ("rtp_sink_%d", ssrc);
165   sinkpad = gst_element_get_pad (stream->jitterbuffer, "sink");
166   stream->rtp_sink = gst_ghost_pad_new (name, sinkpad);
167   gst_object_unref (sinkpad);
168   g_free (name);
169   gst_element_add_pad (GST_ELEMENT_CAST (rtpclient), stream->rtp_sink);
170
171   /* add signal to ptdemuxer */
172   stream->new_pad_sig =
173       g_signal_connect (G_OBJECT (stream->ptdemux), "pad-added",
174       G_CALLBACK (new_pad), stream);
175
176   return stream;
177
178   /* ERRORS */
179 no_jitterbuffer:
180   {
181     g_free (stream);
182     g_warning ("could not create rtpjitterbuffer element");
183     return NULL;
184   }
185 no_ptdemux:
186   {
187     gst_object_unref (stream->jitterbuffer);
188     g_free (stream);
189     g_warning ("could not create rtpptdemux element");
190     return NULL;
191   }
192 could_not_link:
193   {
194     gst_bin_remove (GST_BIN_CAST (rtpclient), stream->jitterbuffer);
195     gst_bin_remove (GST_BIN_CAST (rtpclient), stream->ptdemux);
196     g_free (stream);
197     g_warning ("could not link jitterbuffer and rtpptdemux element");
198     return NULL;
199   }
200 }
201
202 #if 0
203 static void
204 free_stream (GstRTPClientStream * stream)
205 {
206   gst_object_unref (stream->jitterbuffer);
207   g_free (stream);
208 }
209 #endif
210
211 /* find the stream for the given SSRC, return NULL if the stream did not exist
212  */
213 static GstRTPClientStream *
214 find_stream_by_ssrc (GstRTPClient * client, guint32 ssrc)
215 {
216   GstRTPClientStream *stream;
217   GList *walk;
218
219   for (walk = client->streams; walk; walk = g_list_next (walk)) {
220     stream = (GstRTPClientStream *) walk->data;
221     if (stream->ssrc == ssrc)
222       return stream;
223   }
224   return NULL;
225 }
226
227 /* signals and args */
228 enum
229 {
230   /* FILL ME */
231   LAST_SIGNAL
232 };
233
234 enum
235 {
236   PROP_0
237 };
238
239 /* GObject vmethods */
240 static void gst_rtp_client_finalize (GObject * object);
241 static void gst_rtp_client_set_property (GObject * object, guint prop_id,
242     const GValue * value, GParamSpec * pspec);
243 static void gst_rtp_client_get_property (GObject * object, guint prop_id,
244     GValue * value, GParamSpec * pspec);
245
246 /* GstElement vmethods */
247 static GstStateChangeReturn gst_rtp_client_change_state (GstElement * element,
248     GstStateChange transition);
249 static GstPad *gst_rtp_client_request_new_pad (GstElement * element,
250     GstPadTemplate * templ, const gchar * name);
251 static void gst_rtp_client_release_pad (GstElement * element, GstPad * pad);
252
253 /*static guint gst_rtp_client_signals[LAST_SIGNAL] = { 0 }; */
254
255 GST_BOILERPLATE (GstRTPClient, gst_rtp_client, GstBin, GST_TYPE_BIN);
256
257 static void
258 gst_rtp_client_base_init (gpointer klass)
259 {
260   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
261
262   /* sink pads */
263   gst_element_class_add_pad_template (element_class,
264       gst_static_pad_template_get (&rtpclient_rtp_sink_template));
265   gst_element_class_add_pad_template (element_class,
266       gst_static_pad_template_get (&rtpclient_sync_sink_template));
267
268   /* src pads */
269   gst_element_class_add_pad_template (element_class,
270       gst_static_pad_template_get (&rtpclient_rtp_src_template));
271
272   gst_element_class_set_details (element_class, &rtpclient_details);
273 }
274
275 static void
276 gst_rtp_client_class_init (GstRTPClientClass * klass)
277 {
278   GObjectClass *gobject_class;
279   GstElementClass *gstelement_class;
280
281   gobject_class = (GObjectClass *) klass;
282   gstelement_class = (GstElementClass *) klass;
283
284   g_type_class_add_private (klass, sizeof (GstRTPClientPrivate));
285
286   gobject_class->finalize = gst_rtp_client_finalize;
287   gobject_class->set_property = gst_rtp_client_set_property;
288   gobject_class->get_property = gst_rtp_client_get_property;
289
290   gstelement_class->change_state =
291       GST_DEBUG_FUNCPTR (gst_rtp_client_change_state);
292   gstelement_class->request_new_pad =
293       GST_DEBUG_FUNCPTR (gst_rtp_client_request_new_pad);
294   gstelement_class->release_pad =
295       GST_DEBUG_FUNCPTR (gst_rtp_client_release_pad);
296 }
297
298 static void
299 gst_rtp_client_init (GstRTPClient * rtpclient, GstRTPClientClass * klass)
300 {
301   rtpclient->priv = GST_RTP_CLIENT_GET_PRIVATE (rtpclient);
302 }
303
304 static void
305 gst_rtp_client_finalize (GObject * object)
306 {
307   GstRTPClient *rtpclient;
308
309   rtpclient = GST_RTP_CLIENT (object);
310
311   G_OBJECT_CLASS (parent_class)->finalize (object);
312 }
313
314 static void
315 gst_rtp_client_set_property (GObject * object, guint prop_id,
316     const GValue * value, GParamSpec * pspec)
317 {
318   GstRTPClient *rtpclient;
319
320   rtpclient = GST_RTP_CLIENT (object);
321
322   switch (prop_id) {
323     default:
324       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325       break;
326   }
327 }
328
329 static void
330 gst_rtp_client_get_property (GObject * object, guint prop_id,
331     GValue * value, GParamSpec * pspec)
332 {
333   GstRTPClient *rtpclient;
334
335   rtpclient = GST_RTP_CLIENT (object);
336
337   switch (prop_id) {
338     default:
339       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340       break;
341   }
342 }
343
344 static GstStateChangeReturn
345 gst_rtp_client_change_state (GstElement * element, GstStateChange transition)
346 {
347   GstStateChangeReturn res;
348   GstRTPClient *rtpclient;
349
350   rtpclient = GST_RTP_CLIENT (element);
351
352   switch (transition) {
353     case GST_STATE_CHANGE_NULL_TO_READY:
354       break;
355     case GST_STATE_CHANGE_READY_TO_PAUSED:
356       break;
357     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
358       break;
359     default:
360       break;
361   }
362
363   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
364
365   switch (transition) {
366     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
367       break;
368     case GST_STATE_CHANGE_PAUSED_TO_READY:
369       break;
370     case GST_STATE_CHANGE_READY_TO_NULL:
371       break;
372     default:
373       break;
374   }
375   return res;
376 }
377
378 /* We have 2 request pads (rtp_sink_%d and sync_sink_%d), the %d is assumed to
379  * be the SSRC of the stream.
380  *
381  * We require that the rtp pad is requested first for a particular SSRC, then
382  * (optionaly) the sync pad can be requested. If no sync pad is requested, no
383  * sync information can be exchanged for this stream.
384  */
385 static GstPad *
386 gst_rtp_client_request_new_pad (GstElement * element,
387     GstPadTemplate * templ, const gchar * name)
388 {
389   GstRTPClient *rtpclient;
390   GstElementClass *klass;
391   GstPadTemplate *rtp_sink_templ, *sync_sink_templ;
392   guint32 ssrc;
393   GstRTPClientStream *stream;
394   GstPad *result;
395
396   g_return_val_if_fail (templ != NULL, NULL);
397   g_return_val_if_fail (GST_IS_RTP_CLIENT (element), NULL);
398
399   if (templ->direction != GST_PAD_SINK)
400     goto wrong_direction;
401
402   rtpclient = GST_RTP_CLIENT (element);
403   klass = GST_ELEMENT_GET_CLASS (element);
404
405   /* figure out the template */
406   rtp_sink_templ = gst_element_class_get_pad_template (klass, "rtp_sink_%d");
407   sync_sink_templ = gst_element_class_get_pad_template (klass, "sync_sink_%d");
408
409   if (templ != rtp_sink_templ && templ != sync_sink_templ)
410     goto wrong_template;
411
412   if (templ == rtp_sink_templ) {
413     /* create new rtp sink pad. If a stream with the pad number already exists
414      * we have an error, else we create the sinkpad, add a jitterbuffer and
415      * ptdemuxer. */
416     if (name == NULL || strlen (name) < 9)
417       goto no_name;
418
419     ssrc = atoi (&name[9]);
420
421     /* see if a stream with that name exists, if so we have an error. */
422     stream = find_stream_by_ssrc (rtpclient, ssrc);
423     if (stream != NULL)
424       goto stream_exists;
425
426     /* ok, create new stream */
427     stream = create_stream (rtpclient, ssrc);
428     if (stream == NULL)
429       goto stream_not_found;
430
431     result = stream->rtp_sink;
432   } else {
433     /* create new rtp sink pad. We can only do this if the RTP pad was
434      * requested before, meaning the session with the padnumber must exist. */
435     if (name == NULL || strlen (name) < 10)
436       goto no_name;
437
438     ssrc = atoi (&name[10]);
439
440     /* find stream */
441     stream = find_stream_by_ssrc (rtpclient, ssrc);
442     if (stream == NULL)
443       goto stream_not_found;
444
445     stream->sync_sink =
446         gst_pad_new_from_static_template (&rtpclient_sync_sink_template, name);
447     gst_element_add_pad (GST_ELEMENT_CAST (rtpclient), stream->sync_sink);
448
449     result = stream->sync_sink;
450   }
451
452   return result;
453
454   /* ERRORS */
455 wrong_direction:
456   {
457     g_warning ("rtpclient: request pad that is not a SINK pad");
458     return NULL;
459   }
460 wrong_template:
461   {
462     g_warning ("rtpclient: this is not our template");
463     return NULL;
464   }
465 no_name:
466   {
467     g_warning ("rtpclient: no padname was specified");
468     return NULL;
469   }
470 stream_exists:
471   {
472     g_warning ("rtpclient: stream with SSRC %d already registered", ssrc);
473     return NULL;
474   }
475 stream_not_found:
476   {
477     g_warning ("rtpclient: stream with SSRC %d not yet registered", ssrc);
478     return NULL;
479   }
480 }
481
482 static void
483 gst_rtp_client_release_pad (GstElement * element, GstPad * pad)
484 {
485 }