docs: improve docs
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-stream-transport.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:rtsp-stream-transport
21  * @short_description: A media stream transport configuration
22  * @see_also: #GstRTSPStream, #GstRTSPSessionMedia
23  *
24  * The #GstRTSPStreamTransport configures the transport used by a
25  * #GstRTSPStream. It is usually manages by a #GstRTSPSessionMedia object.
26  *
27  * With gst_rtsp_stream_transport_set_callbacks(), callbacks can be configured
28  * to handle the RTP and RTCP packets from the stream, for example when they
29  * need to be sent over TCP.
30  *
31  * With  gst_rtsp_stream_transport_set_active() the transports are added and
32  * removed from the stream.
33  *
34  * A #GstRTSPStream will call gst_rtsp_stream_transport_keep_alive() when RTCP
35  * is received from the client. It will also call
36  * gst_rtsp_stream_transport_set_timed_out() when a receiver has timed out.
37  *
38  * Last reviewed on 2013-07-16 (1.0.0)
39  */
40
41 #include <string.h>
42 #include <stdlib.h>
43
44 #include "rtsp-stream-transport.h"
45
46 #define GST_RTSP_STREAM_TRANSPORT_GET_PRIVATE(obj)  \
47        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM_TRANSPORT, GstRTSPStreamTransportPrivate))
48
49 struct _GstRTSPStreamTransportPrivate
50 {
51   GstRTSPStream *stream;
52
53   GstRTSPSendFunc send_rtp;
54   GstRTSPSendFunc send_rtcp;
55   gpointer user_data;
56   GDestroyNotify notify;
57
58   GstRTSPKeepAliveFunc keep_alive;
59   gpointer ka_user_data;
60   GDestroyNotify ka_notify;
61   gboolean active;
62   gboolean timed_out;
63
64   GstRTSPTransport *transport;
65
66   GObject *rtpsource;
67 };
68
69 enum
70 {
71   PROP_0,
72   PROP_LAST
73 };
74
75 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_transport_debug);
76 #define GST_CAT_DEFAULT rtsp_stream_transport_debug
77
78 static void gst_rtsp_stream_transport_finalize (GObject * obj);
79
80 G_DEFINE_TYPE (GstRTSPStreamTransport, gst_rtsp_stream_transport,
81     G_TYPE_OBJECT);
82
83 static void
84 gst_rtsp_stream_transport_class_init (GstRTSPStreamTransportClass * klass)
85 {
86   GObjectClass *gobject_class;
87
88   g_type_class_add_private (klass, sizeof (GstRTSPStreamTransportPrivate));
89
90   gobject_class = G_OBJECT_CLASS (klass);
91
92   gobject_class->finalize = gst_rtsp_stream_transport_finalize;
93
94   GST_DEBUG_CATEGORY_INIT (rtsp_stream_transport_debug, "rtspmediatransport",
95       0, "GstRTSPStreamTransport");
96 }
97
98 static void
99 gst_rtsp_stream_transport_init (GstRTSPStreamTransport * trans)
100 {
101   GstRTSPStreamTransportPrivate *priv =
102       GST_RTSP_STREAM_TRANSPORT_GET_PRIVATE (trans);
103
104   trans->priv = priv;
105 }
106
107 static void
108 gst_rtsp_stream_transport_finalize (GObject * obj)
109 {
110   GstRTSPStreamTransportPrivate *priv;
111   GstRTSPStreamTransport *trans;
112
113   trans = GST_RTSP_STREAM_TRANSPORT (obj);
114   priv = trans->priv;
115
116   /* remove callbacks now */
117   gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
118   gst_rtsp_stream_transport_set_keepalive (trans, NULL, NULL, NULL);
119
120   if (priv->transport)
121     gst_rtsp_transport_free (priv->transport);
122
123   G_OBJECT_CLASS (gst_rtsp_stream_transport_parent_class)->finalize (obj);
124 }
125
126 /**
127  * gst_rtsp_stream_transport_new:
128  * @stream: a #GstRTSPStream
129  * @tr: (transfer full): a GstRTSPTransport
130  *
131  * Create a new #GstRTSPStreamTransport that can be used to manage
132  * @stream with transport @tr.
133  *
134  * Returns: a new #GstRTSPStreamTransport
135  */
136 GstRTSPStreamTransport *
137 gst_rtsp_stream_transport_new (GstRTSPStream * stream, GstRTSPTransport * tr)
138 {
139   GstRTSPStreamTransportPrivate *priv;
140   GstRTSPStreamTransport *trans;
141
142   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
143   g_return_val_if_fail (tr != NULL, NULL);
144
145   trans = g_object_new (GST_TYPE_RTSP_STREAM_TRANSPORT, NULL);
146   priv = trans->priv;
147   priv->stream = stream;
148   priv->transport = tr;
149
150   return trans;
151 }
152
153 /**
154  * gst_rtsp_stream_transport_get_stream:
155  * @trans: a #GstRTSPStreamTransport
156  *
157  * Get the #GstRTSPStream used when constructing @trans.
158  *
159  * Returns: (transfer none): the stream used when constructing @trans.
160  */
161 GstRTSPStream *
162 gst_rtsp_stream_transport_get_stream (GstRTSPStreamTransport * trans)
163 {
164   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), NULL);
165
166   return trans->priv->stream;
167 }
168
169 /**
170  * gst_rtsp_stream_transport_set_callbacks:
171  * @trans: a #GstRTSPStreamTransport
172  * @send_rtp: (scope notified): a callback called when RTP should be sent
173  * @send_rtcp: (scope notified): a callback called when RTCP should be sent
174  * @user_data: user data passed to callbacks
175  * @notify: called with the user_data when no longer needed.
176  *
177  * Install callbacks that will be called when data for a stream should be sent
178  * to a client. This is usually used when sending RTP/RTCP over TCP.
179  */
180 void
181 gst_rtsp_stream_transport_set_callbacks (GstRTSPStreamTransport * trans,
182     GstRTSPSendFunc send_rtp, GstRTSPSendFunc send_rtcp,
183     gpointer user_data, GDestroyNotify notify)
184 {
185   GstRTSPStreamTransportPrivate *priv;
186
187   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
188
189   priv = trans->priv;
190
191   priv->send_rtp = send_rtp;
192   priv->send_rtcp = send_rtcp;
193   if (priv->notify)
194     priv->notify (priv->user_data);
195   priv->user_data = user_data;
196   priv->notify = notify;
197 }
198
199 /**
200  * gst_rtsp_stream_transport_set_keepalive:
201  * @trans: a #GstRTSPStreamTransport
202  * @keep_alive: a callback called when the receiver is active
203  * @user_data: user data passed to callback
204  * @notify: called with the user_data when no longer needed.
205  *
206  * Install callbacks that will be called when RTCP packets are received from the
207  * receiver of @trans.
208  */
209 void
210 gst_rtsp_stream_transport_set_keepalive (GstRTSPStreamTransport * trans,
211     GstRTSPKeepAliveFunc keep_alive, gpointer user_data, GDestroyNotify notify)
212 {
213   GstRTSPStreamTransportPrivate *priv;
214
215   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
216
217   priv = trans->priv;
218
219   priv->keep_alive = keep_alive;
220   if (priv->ka_notify)
221     priv->ka_notify (priv->ka_user_data);
222   priv->ka_user_data = user_data;
223   priv->ka_notify = notify;
224 }
225
226
227 /**
228  * gst_rtsp_stream_transport_set_transport:
229  * @trans: a #GstRTSPStreamTransport
230  * @tr: (transfer full): a client #GstRTSPTransport
231  *
232  * Set @tr as the client transport. This function takes ownership of the
233  * passed @tr.
234  */
235 void
236 gst_rtsp_stream_transport_set_transport (GstRTSPStreamTransport * trans,
237     GstRTSPTransport * tr)
238 {
239   GstRTSPStreamTransportPrivate *priv;
240
241   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
242   g_return_if_fail (tr != NULL);
243
244   priv = trans->priv;
245
246   /* keep track of the transports in the stream. */
247   if (priv->transport)
248     gst_rtsp_transport_free (priv->transport);
249   priv->transport = tr;
250 }
251
252 /**
253  * gst_rtsp_stream_transport_get_transport:
254  * @trans: a #GstRTSPStreamTransport
255  *
256  * Get the transport configured in @trans.
257  *
258  * Returns: (transfer none): the transport configured in @trans. It remains
259  *     valid for as long as @trans is valid.
260  */
261 const GstRTSPTransport *
262 gst_rtsp_stream_transport_get_transport (GstRTSPStreamTransport * trans)
263 {
264   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), NULL);
265
266   return trans->priv->transport;
267 }
268
269 /**
270  * gst_rtsp_stream_transport_set_active:
271  * @trans: a #GstRTSPStreamTransport
272  * @active: new state of @trans
273  *
274  * Activate or deactivate datatransfer configured in @trans.
275  *
276  * Returns: %TRUE when the state was changed.
277  */
278 gboolean
279 gst_rtsp_stream_transport_set_active (GstRTSPStreamTransport * trans,
280     gboolean active)
281 {
282   GstRTSPStreamTransportPrivate *priv;
283   gboolean res;
284
285   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
286
287   priv = trans->priv;
288
289   if (priv->active == active)
290     return FALSE;
291
292   if (active)
293     res = gst_rtsp_stream_add_transport (priv->stream, trans);
294   else
295     res = gst_rtsp_stream_remove_transport (priv->stream, trans);
296
297   if (res)
298     priv->active = active;
299
300   return res;
301 }
302
303 /**
304  * gst_rtsp_stream_transport_set_timed_out:
305  * @trans: a #GstRTSPStreamTransport
306  * @timedout: timed out value
307  *
308  * Set the timed out state of @trans to @timedout
309  */
310 void
311 gst_rtsp_stream_transport_set_timed_out (GstRTSPStreamTransport * trans,
312     gboolean timedout)
313 {
314   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
315
316   trans->priv->timed_out = timedout;
317 }
318
319 /**
320  * gst_rtsp_stream_transport_is_timed_out:
321  * @trans: a #GstRTSPStreamTransport
322  *
323  * Check if @trans is timed out.
324  *
325  * Returns: %TRUE if @trans timed out.
326  */
327 gboolean
328 gst_rtsp_stream_transport_is_timed_out (GstRTSPStreamTransport * trans)
329 {
330   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
331
332   return trans->priv->timed_out;
333 }
334
335 /**
336  * gst_rtsp_stream_transport_send_rtp:
337  * @trans: a #GstRTSPStreamTransport
338  * @buffer: a #GstBuffer
339  *
340  * Send @buffer to the installed RTP callback for @trans.
341  *
342  * Returns: %TRUE on success
343  */
344 gboolean
345 gst_rtsp_stream_transport_send_rtp (GstRTSPStreamTransport * trans,
346     GstBuffer * buffer)
347 {
348   GstRTSPStreamTransportPrivate *priv;
349   gboolean res = FALSE;
350
351   priv = trans->priv;
352
353   if (priv->send_rtp)
354     res =
355         priv->send_rtp (buffer, priv->transport->interleaved.min,
356         priv->user_data);
357
358   return res;
359 }
360
361 /**
362  * gst_rtsp_stream_transport_send_rtcp:
363  * @trans: a #GstRTSPStreamTransport
364  * @buffer: a #GstBuffer
365  *
366  * Send @buffer to the installed RTCP callback for @trans.
367  *
368  * Returns: %TRUE on success
369  */
370 gboolean
371 gst_rtsp_stream_transport_send_rtcp (GstRTSPStreamTransport * trans,
372     GstBuffer * buffer)
373 {
374   GstRTSPStreamTransportPrivate *priv;
375   gboolean res = FALSE;
376
377   priv = trans->priv;
378
379   if (priv->send_rtcp)
380     res =
381         priv->send_rtcp (buffer, priv->transport->interleaved.max,
382         priv->user_data);
383
384   return res;
385 }
386
387 /**
388  * gst_rtsp_stream_transport_keep_alive:
389  * @trans: a #GstRTSPStreamTransport
390  *
391  * Signal the installed keep_alive callback for @trans.
392  */
393 void
394 gst_rtsp_stream_transport_keep_alive (GstRTSPStreamTransport * trans)
395 {
396   GstRTSPStreamTransportPrivate *priv;
397
398   priv = trans->priv;
399
400   if (priv->keep_alive)
401     priv->keep_alive (priv->ka_user_data);
402 }