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