media: disconnect from signal handlers in unprepare()
[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 #if 0
103   if (priv->rtpsource)
104     g_object_set_qdata (priv->rtpsource, ssrc_stream_map_key, NULL);
105 #endif
106
107   G_OBJECT_CLASS (gst_rtsp_stream_transport_parent_class)->finalize (obj);
108 }
109
110 /**
111  * gst_rtsp_stream_transport_new:
112  * @stream: a #GstRTSPStream
113  * @tr: (transfer full): a GstRTSPTransport
114  *
115  * Create a new #GstRTSPStreamTransport that can be used to manage
116  * @stream with transport @tr.
117  *
118  * Returns: a new #GstRTSPStreamTransport
119  */
120 GstRTSPStreamTransport *
121 gst_rtsp_stream_transport_new (GstRTSPStream * stream, GstRTSPTransport * tr)
122 {
123   GstRTSPStreamTransportPrivate *priv;
124   GstRTSPStreamTransport *trans;
125
126   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
127   g_return_val_if_fail (tr != NULL, NULL);
128
129   trans = g_object_new (GST_TYPE_RTSP_STREAM_TRANSPORT, NULL);
130   priv = trans->priv;
131   priv->stream = stream;
132   priv->transport = tr;
133
134   return trans;
135 }
136
137 /**
138  * gst_rtsp_stream_transport_get_stream:
139  * @trans: a #GstRTSPStreamTransport
140  *
141  * Get the #GstRTSPStream used when constructing @trans.
142  *
143  * Returns: (transfer none): the stream used when constructing @trans.
144  */
145 GstRTSPStream *
146 gst_rtsp_stream_transport_get_stream (GstRTSPStreamTransport * trans)
147 {
148   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), NULL);
149
150   return trans->priv->stream;
151 }
152
153 /**
154  * gst_rtsp_stream_transport_set_callbacks:
155  * @trans: a #GstRTSPStreamTransport
156  * @send_rtp: (scope notified): a callback called when RTP should be sent
157  * @send_rtcp: (scope notified): a callback called when RTCP should be sent
158  * @user_data: user data passed to callbacks
159  * @notify: called with the user_data when no longer needed.
160  *
161  * Install callbacks that will be called when data for a stream should be sent
162  * to a client. This is usually used when sending RTP/RTCP over TCP.
163  */
164 void
165 gst_rtsp_stream_transport_set_callbacks (GstRTSPStreamTransport * trans,
166     GstRTSPSendFunc send_rtp, GstRTSPSendFunc send_rtcp,
167     gpointer user_data, GDestroyNotify notify)
168 {
169   GstRTSPStreamTransportPrivate *priv;
170
171   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
172
173   priv = trans->priv;
174
175   priv->send_rtp = send_rtp;
176   priv->send_rtcp = send_rtcp;
177   if (priv->notify)
178     priv->notify (priv->user_data);
179   priv->user_data = user_data;
180   priv->notify = notify;
181 }
182
183 /**
184  * gst_rtsp_stream_transport_set_keepalive:
185  * @trans: a #GstRTSPStreamTransport
186  * @keep_alive: a callback called when the receiver is active
187  * @user_data: user data passed to callback
188  * @notify: called with the user_data when no longer needed.
189  *
190  * Install callbacks that will be called when RTCP packets are received from the
191  * receiver of @trans.
192  */
193 void
194 gst_rtsp_stream_transport_set_keepalive (GstRTSPStreamTransport * trans,
195     GstRTSPKeepAliveFunc keep_alive, gpointer user_data, GDestroyNotify notify)
196 {
197   GstRTSPStreamTransportPrivate *priv;
198
199   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
200
201   priv = trans->priv;
202
203   priv->keep_alive = keep_alive;
204   if (priv->ka_notify)
205     priv->ka_notify (priv->ka_user_data);
206   priv->ka_user_data = user_data;
207   priv->ka_notify = notify;
208 }
209
210
211 /**
212  * gst_rtsp_stream_transport_set_transport:
213  * @trans: a #GstRTSPStreamTransport
214  * @tr: (transfer full): a client #GstRTSPTransport
215  *
216  * Set @tr as the client transport. This function takes ownership of the
217  * passed @tr.
218  */
219 void
220 gst_rtsp_stream_transport_set_transport (GstRTSPStreamTransport * trans,
221     GstRTSPTransport * tr)
222 {
223   GstRTSPStreamTransportPrivate *priv;
224
225   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
226   g_return_if_fail (tr != NULL);
227
228   priv = trans->priv;
229
230   /* keep track of the transports in the stream. */
231   if (priv->transport)
232     gst_rtsp_transport_free (priv->transport);
233   priv->transport = tr;
234 }
235
236 /**
237  * gst_rtsp_stream_transport_get_transport:
238  * @trans: a #GstRTSPStreamTransport
239  *
240  * Get the transport configured in @trans.
241  *
242  * Returns: (transfer none): the transport configured in @trans. It remains
243  *     valid for as long as @trans is valid.
244  */
245 const GstRTSPTransport *
246 gst_rtsp_stream_transport_get_transport (GstRTSPStreamTransport * trans)
247 {
248   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), NULL);
249
250   return trans->priv->transport;
251 }
252
253 /**
254  * gst_rtsp_stream_transport_set_active:
255  * @trans: a #GstRTSPStreamTransport
256  * @active: new state of @trans
257  *
258  * Activate or deactivate datatransfer configured in @trans.
259  *
260  * Returns: %TRUE when the state was changed.
261  */
262 gboolean
263 gst_rtsp_stream_transport_set_active (GstRTSPStreamTransport * trans,
264     gboolean active)
265 {
266   GstRTSPStreamTransportPrivate *priv;
267   gboolean res;
268
269   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
270
271   priv = trans->priv;
272
273   if (priv->active == active)
274     return FALSE;
275
276   if (active)
277     res = gst_rtsp_stream_add_transport (priv->stream, trans);
278   else
279     res = gst_rtsp_stream_remove_transport (priv->stream, trans);
280
281   if (res)
282     priv->active = active;
283
284   return res;
285 }
286
287 /**
288  * gst_rtsp_stream_transport_set_timed_out:
289  * @trans: a #GstRTSPStreamTransport
290  * @timedout: timed out value
291  *
292  * Set the timed out state of @trans to @timedout
293  */
294 void
295 gst_rtsp_stream_transport_set_timed_out (GstRTSPStreamTransport * trans,
296     gboolean timedout)
297 {
298   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
299
300   trans->priv->timed_out = timedout;
301 }
302
303 /**
304  * gst_rtsp_stream_transport_is_timed_out:
305  * @trans: a #GstRTSPStreamTransport
306  *
307  * Check if @trans is timed out.
308  *
309  * Returns: %TRUE if @trans timed out.
310  */
311 gboolean
312 gst_rtsp_stream_transport_is_timed_out (GstRTSPStreamTransport * trans)
313 {
314   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
315
316   return trans->priv->timed_out;
317 }
318
319 /**
320  * gst_rtsp_stream_transport_send_rtp:
321  * @trans: a #GstRTSPStreamTransport
322  * @buffer: a #GstBuffer
323  *
324  * Send @buffer to the installed RTP callback for @trans.
325  *
326  * Returns: %TRUE on success
327  */
328 gboolean
329 gst_rtsp_stream_transport_send_rtp (GstRTSPStreamTransport * trans,
330     GstBuffer * buffer)
331 {
332   GstRTSPStreamTransportPrivate *priv;
333   gboolean res = FALSE;
334
335   priv = trans->priv;
336
337   if (priv->send_rtp)
338     res =
339         priv->send_rtp (buffer, priv->transport->interleaved.min,
340         priv->user_data);
341
342   return res;
343 }
344
345 /**
346  * gst_rtsp_stream_transport_send_rtcp:
347  * @trans: a #GstRTSPStreamTransport
348  * @buffer: a #GstBuffer
349  *
350  * Send @buffer to the installed RTCP callback for @trans.
351  *
352  * Returns: %TRUE on success
353  */
354 gboolean
355 gst_rtsp_stream_transport_send_rtcp (GstRTSPStreamTransport * trans,
356     GstBuffer * buffer)
357 {
358   GstRTSPStreamTransportPrivate *priv;
359   gboolean res = FALSE;
360
361   priv = trans->priv;
362
363   if (priv->send_rtcp)
364     res =
365         priv->send_rtcp (buffer, priv->transport->interleaved.max,
366         priv->user_data);
367
368   return res;
369 }
370
371 /**
372  * gst_rtsp_stream_transport_keep_alive:
373  * @trans: a #GstRTSPStreamTransport
374  *
375  * Signal the installed keep_alive callback for @trans.
376  */
377 void
378 gst_rtsp_stream_transport_keep_alive (GstRTSPStreamTransport * trans)
379 {
380   GstRTSPStreamTransportPrivate *priv;
381
382   priv = trans->priv;
383
384   if (priv->keep_alive)
385     priv->keep_alive (priv->ka_user_data);
386 }