rtsp: make address-pool return an address object
[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 <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
25
26 #include "rtsp-stream-transport.h"
27
28 enum
29 {
30   PROP_0,
31   PROP_LAST
32 };
33
34 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_transport_debug);
35 #define GST_CAT_DEFAULT rtsp_stream_transport_debug
36
37 static void gst_rtsp_stream_transport_finalize (GObject * obj);
38
39 G_DEFINE_TYPE (GstRTSPStreamTransport, gst_rtsp_stream_transport,
40     G_TYPE_OBJECT);
41
42 static void
43 gst_rtsp_stream_transport_class_init (GstRTSPStreamTransportClass * klass)
44 {
45   GObjectClass *gobject_class;
46
47   gobject_class = G_OBJECT_CLASS (klass);
48
49   gobject_class->finalize = gst_rtsp_stream_transport_finalize;
50
51   GST_DEBUG_CATEGORY_INIT (rtsp_stream_transport_debug, "rtspmediatransport",
52       0, "GstRTSPStreamTransport");
53 }
54
55 static void
56 gst_rtsp_stream_transport_init (GstRTSPStreamTransport * trans)
57 {
58 }
59
60 static void
61 gst_rtsp_stream_transport_finalize (GObject * obj)
62 {
63   GstRTSPStreamTransport *trans;
64
65   trans = GST_RTSP_STREAM_TRANSPORT (obj);
66
67   /* remove callbacks now */
68   gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
69   gst_rtsp_stream_transport_set_keepalive (trans, NULL, NULL, NULL);
70
71   if (trans->transport)
72     gst_rtsp_transport_free (trans->transport);
73   if (trans->addr)
74     gst_rtsp_address_free (trans->addr);
75
76 #if 0
77   if (trans->rtpsource)
78     g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
79 #endif
80
81   G_OBJECT_CLASS (gst_rtsp_stream_transport_parent_class)->finalize (obj);
82 }
83
84 /**
85  * gst_rtsp_stream_transport_new:
86  * @stream: a #GstRTSPStream
87  * @tr: (transfer full): a GstRTSPTransport
88  * @addr: (transfer full) (allow none): an optional GstRTSPAddress
89  *
90  * Create a new #GstRTSPStreamTransport that can be used to manage
91  * @stream with transport @tr.
92  *
93  * Returns: a new #GstRTSPStreamTransport
94  */
95 GstRTSPStreamTransport *
96 gst_rtsp_stream_transport_new (GstRTSPStream * stream, GstRTSPTransport * tr,
97     GstRTSPAddress * addr)
98 {
99   GstRTSPStreamTransport *trans;
100
101   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
102   g_return_val_if_fail (tr != NULL, NULL);
103
104   trans = g_object_new (GST_TYPE_RTSP_STREAM_TRANSPORT, NULL);
105   trans->stream = stream;
106   trans->transport = tr;
107   trans->addr = addr;
108
109   return trans;
110 }
111
112 /**
113  * gst_rtsp_stream_transport_set_callbacks:
114  * @trans: a #GstRTSPStreamTransport
115  * @send_rtp: (scope notified): a callback called when RTP should be sent
116  * @send_rtcp: (scope notified): a callback called when RTCP should be sent
117  * @user_data: user data passed to callbacks
118  * @notify: called with the user_data when no longer needed.
119  *
120  * Install callbacks that will be called when data for a stream should be sent
121  * to a client. This is usually used when sending RTP/RTCP over TCP.
122  */
123 void
124 gst_rtsp_stream_transport_set_callbacks (GstRTSPStreamTransport * trans,
125     GstRTSPSendFunc send_rtp, GstRTSPSendFunc send_rtcp,
126     gpointer user_data, GDestroyNotify notify)
127 {
128   trans->send_rtp = send_rtp;
129   trans->send_rtcp = send_rtcp;
130   if (trans->notify)
131     trans->notify (trans->user_data);
132   trans->user_data = user_data;
133   trans->notify = notify;
134 }
135
136 /**
137  * gst_rtsp_stream_transport_set_keepalive:
138  * @trans: a #GstRTSPStreamTransport
139  * @keep_alive: a callback called when the receiver is active
140  * @user_data: user data passed to callback
141  * @notify: called with the user_data when no longer needed.
142  *
143  * Install callbacks that will be called when RTCP packets are received from the
144  * receiver of @trans.
145  */
146 void
147 gst_rtsp_stream_transport_set_keepalive (GstRTSPStreamTransport * trans,
148     GstRTSPKeepAliveFunc keep_alive, gpointer user_data, GDestroyNotify notify)
149 {
150   trans->keep_alive = keep_alive;
151   if (trans->ka_notify)
152     trans->ka_notify (trans->ka_user_data);
153   trans->ka_user_data = user_data;
154   trans->ka_notify = notify;
155 }
156
157
158 /**
159  * gst_rtsp_stream_transport_set_transport:
160  * @trans: a #GstRTSPStreamTransport
161  * @tr: (transfer full): a client #GstRTSPTransport
162  * @addr: (transfer full) (allow none): a ##GstRTSPAddress
163  *
164  * Set @tr and the optional @addr as the client transport. This function
165  * takes ownership of the passed @tr and @addr.
166  */
167 void
168 gst_rtsp_stream_transport_set_transport (GstRTSPStreamTransport * trans,
169     GstRTSPTransport * tr, GstRTSPAddress * addr)
170 {
171   g_return_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans));
172   g_return_if_fail (tr != NULL);
173
174   /* keep track of the transports in the stream. */
175   if (trans->transport)
176     gst_rtsp_transport_free (trans->transport);
177   trans->transport = tr;
178   if (trans->addr)
179     gst_rtsp_address_free (trans->addr);
180   trans->addr = addr;
181 }
182
183 /**
184  * gst_rtsp_stream_transport_send_rtp:
185  * @trans: a #GstRTSPStreamTransport
186  * @buffer: a #GstBuffer
187  *
188  * Send @buffer to the installed RTP callback for @trans.
189  *
190  * Returns: %TRUE on success
191  */
192 gboolean
193 gst_rtsp_stream_transport_send_rtp (GstRTSPStreamTransport * trans,
194     GstBuffer * buffer)
195 {
196   gboolean res = FALSE;
197
198   if (trans->send_rtp)
199     res =
200         trans->send_rtp (buffer, trans->transport->interleaved.min,
201         trans->user_data);
202
203   return res;
204 }
205
206 /**
207  * gst_rtsp_stream_transport_send_rtcp:
208  * @trans: a #GstRTSPStreamTransport
209  * @buffer: a #GstBuffer
210  *
211  * Send @buffer to the installed RTCP callback for @trans.
212  *
213  * Returns: %TRUE on success
214  */
215 gboolean
216 gst_rtsp_stream_transport_send_rtcp (GstRTSPStreamTransport * trans,
217     GstBuffer * buffer)
218 {
219   gboolean res = FALSE;
220
221   if (trans->send_rtcp)
222     res =
223         trans->send_rtcp (buffer, trans->transport->interleaved.max,
224         trans->user_data);
225
226   return res;
227 }
228
229 /**
230  * gst_rtsp_stream_transport_keep_alive:
231  * @trans: a #GstRTSPStreamTransport
232  *
233  * Signal the installed keep_alive callback for @trans.
234  */
235 void
236 gst_rtsp_stream_transport_keep_alive (GstRTSPStreamTransport * trans)
237 {
238   if (trans->keep_alive)
239     trans->keep_alive (trans->ka_user_data);
240 }