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