1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C)  2015 Kurento (http://kurento.org/)
4  *   @author: Miguel París <mparisdiaz@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifndef __RTP_SOURCE_H__
23 #define __RTP_SOURCE_H__
24 
25 #include <gst/gst.h>
26 #include <gst/rtp/rtp.h>
27 #include <gst/net/gstnetaddressmeta.h>
28 #include <gio/gio.h>
29 
30 #include "rtpstats.h"
31 
32 /* the default number of consecutive RTP packets we need to receive before the
33  * source is considered valid */
34 #define RTP_NO_PROBATION        0
35 #define RTP_DEFAULT_PROBATION   2
36 
37 #define RTP_SEQ_MOD          (1 << 16)
38 
39 typedef struct _RTPSource RTPSource;
40 typedef struct _RTPSourceClass RTPSourceClass;
41 
42 #define RTP_TYPE_SOURCE             (rtp_source_get_type())
43 #define RTP_SOURCE(src)             (G_TYPE_CHECK_INSTANCE_CAST((src),RTP_TYPE_SOURCE,RTPSource))
44 #define RTP_SOURCE_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),RTP_TYPE_SOURCE,RTPSourceClass))
45 #define RTP_IS_SOURCE(src)          (G_TYPE_CHECK_INSTANCE_TYPE((src),RTP_TYPE_SOURCE))
46 #define RTP_IS_SOURCE_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),RTP_TYPE_SOURCE))
47 #define RTP_SOURCE_CAST(src)        ((RTPSource *)(src))
48 
49 /**
50  * RTP_SOURCE_IS_ACTIVE:
51  * @src: an #RTPSource
52  *
53  * Check if @src is active. A source is active when it has been validated
54  * and has not yet received a BYE packet.
55  */
56 #define RTP_SOURCE_IS_ACTIVE(src)  (src->validated && !src->marked_bye)
57 
58 /**
59  * RTP_SOURCE_IS_SENDER:
60  * @src: an #RTPSource
61  *
62  * Check if @src is a sender.
63  */
64 #define RTP_SOURCE_IS_SENDER(src)  (src->is_sender)
65 /**
66  * RTP_SOURCE_IS_MARKED_BYE:
67  * @src: an #RTPSource
68  *
69  * Check if @src is a marked as BYE.
70  */
71 #define RTP_SOURCE_IS_MARKED_BYE(src)  (src->marked_bye)
72 
73 
74 /**
75  * RTPSourcePushRTP:
76  * @src: an #RTPSource
77  * @data: the RTP buffer or buffer list ready for processing
78  * @user_data: user data specified when registering
79  *
80  * This callback will be called when @src has @buffer ready for further
81  * processing.
82  *
83  * Returns: a #GstFlowReturn.
84  */
85 typedef GstFlowReturn (*RTPSourcePushRTP) (RTPSource *src, gpointer data,
86 	gpointer user_data);
87 
88 /**
89  * RTPSourceClockRate:
90  * @src: an #RTPSource
91  * @payload: a payload type
92  * @user_data: user data specified when registering
93  *
94  * This callback will be called when @src needs the clock-rate of the
95  * @payload.
96  *
97  * Returns: a clock-rate for @payload.
98  */
99 typedef gint (*RTPSourceClockRate) (RTPSource *src, guint8 payload, gpointer user_data);
100 
101 /**
102  * RTPSourceCallbacks:
103  * @push_rtp: a packet becomes available for handling
104  * @clock_rate: a clock-rate is requested
105  * @get_time: the current clock time is requested
106  *
107  * Callbacks performed by #RTPSource when actions need to be performed.
108  */
109 typedef struct {
110   RTPSourcePushRTP     push_rtp;
111   RTPSourceClockRate   clock_rate;
112 } RTPSourceCallbacks;
113 
114 /**
115  * RTPConflictingAddress:
116  * @address: #GSocketAddress which conflicted
117  * @last_conflict_time: time when the last conflict was seen
118  *
119  * This structure is used to account for addresses that have conflicted to find
120  * loops.
121  */
122 typedef struct {
123   GSocketAddress *address;
124   GstClockTime time;
125 } RTPConflictingAddress;
126 
127 /**
128  * RTPSource:
129  *
130  * A source in the #RTPSession
131  *
132  * @conflicting_addresses: GList of conflicting addresses
133  */
134 struct _RTPSource {
135   GObject       object;
136 
137   /*< private >*/
138   guint32       ssrc;
139 
140   guint16       generation;
141   GHashTable    *reported_in_sr_of;     /* set of SSRCs */
142 
143   guint         probation;
144   guint         curr_probation;
145   gboolean      validated;
146   gboolean      internal;
147   gboolean      is_csrc;
148   gboolean      is_sender;
149   gboolean      closing;
150 
151   GstStructure  *sdes;
152 
153   gboolean      marked_bye;
154   gchar        *bye_reason;
155   gboolean      sent_bye;
156 
157   GSocketAddress *rtp_from;
158   GSocketAddress *rtcp_from;
159 
160   gint          payload;
161   GstCaps      *caps;
162   gint          clock_rate;
163   gint32        seqnum_offset;
164 
165   GstClockTime  bye_time;
166   GstClockTime  last_activity;
167   GstClockTime  last_rtp_activity;
168 
169   GstClockTime  last_rtime;
170   GstClockTime  last_rtptime;
171 
172   /* for bitrate estimation */
173   guint64       bitrate;
174   GstClockTime  prev_rtime;
175   guint64       bytes_sent;
176   guint64       bytes_received;
177 
178   GQueue       *packets;
179   RTPPacketRateCtx packet_rate_ctx;
180   guint32       max_dropout_time;
181   guint32       max_misorder_time;
182 
183   RTPSourceCallbacks callbacks;
184   gpointer           user_data;
185 
186   RTPSourceStats stats;
187   RTPReceiverReport last_rr;
188 
189   GList         *conflicting_addresses;
190 
191   GQueue        *retained_feedback;
192 
193   gboolean     send_pli;
194   gboolean     send_fir;
195   guint8       current_send_fir_seqnum;
196   gint         last_fir_count;
197   GstClockTime last_keyframe_request;
198 
199   gboolean     send_nack;
200   GArray      *nacks;
201   GArray      *nack_deadlines;
202 
203   gboolean      pt_set;
204   guint8        pt;
205 
206   gboolean      disable_rtcp;
207 };
208 
209 struct _RTPSourceClass {
210   GObjectClass   parent_class;
211 };
212 
213 GType rtp_source_get_type (void);
214 
215 /* managing lifetime of sources */
216 RTPSource*      rtp_source_new                 (guint32 ssrc);
217 void            rtp_source_set_callbacks       (RTPSource *src, RTPSourceCallbacks *cb, gpointer data);
218 
219 /* properties */
220 guint32         rtp_source_get_ssrc            (RTPSource *src);
221 
222 void            rtp_source_set_as_csrc         (RTPSource *src);
223 gboolean        rtp_source_is_as_csrc          (RTPSource *src);
224 
225 gboolean        rtp_source_is_active           (RTPSource *src);
226 gboolean        rtp_source_is_validated        (RTPSource *src);
227 gboolean        rtp_source_is_sender           (RTPSource *src);
228 
229 void            rtp_source_mark_bye            (RTPSource *src, const gchar *reason);
230 gboolean        rtp_source_is_marked_bye       (RTPSource *src);
231 gchar *         rtp_source_get_bye_reason      (RTPSource *src);
232 
233 void            rtp_source_update_caps         (RTPSource *src, GstCaps *caps);
234 
235 /* SDES info */
236 const GstStructure *
237                 rtp_source_get_sdes_struct     (RTPSource * src);
238 gboolean        rtp_source_set_sdes_struct     (RTPSource * src, GstStructure *sdes);
239 
240 /* handling network address */
241 void            rtp_source_set_rtp_from        (RTPSource *src, GSocketAddress *address);
242 void            rtp_source_set_rtcp_from       (RTPSource *src, GSocketAddress *address);
243 
244 /* handling RTP */
245 GstFlowReturn   rtp_source_process_rtp         (RTPSource *src, RTPPacketInfo *pinfo);
246 
247 GstFlowReturn   rtp_source_send_rtp            (RTPSource *src, RTPPacketInfo *pinfo);
248 
249 /* RTCP messages */
250 void            rtp_source_process_sr          (RTPSource *src, GstClockTime time, guint64 ntptime,
251                                                 guint32 rtptime, guint32 packet_count, guint32 octet_count);
252 void            rtp_source_process_rb          (RTPSource *src, guint64 ntpnstime, guint8 fractionlost,
253                                                 gint32 packetslost, guint32 exthighestseq, guint32 jitter,
254                                                 guint32 lsr, guint32 dlsr);
255 
256 gboolean        rtp_source_get_new_sr          (RTPSource *src, guint64 ntpnstime, GstClockTime running_time,
257                                                 guint64 *ntptime, guint32 *rtptime, guint32 *packet_count,
258 						guint32 *octet_count);
259 gboolean        rtp_source_get_new_rb          (RTPSource *src, GstClockTime time, guint8 *fractionlost,
260                                                 gint32 *packetslost, guint32 *exthighestseq, guint32 *jitter,
261                                                 guint32 *lsr, guint32 *dlsr);
262 
263 gboolean        rtp_source_get_last_sr         (RTPSource *src, GstClockTime *time, guint64 *ntptime,
264                                                 guint32 *rtptime, guint32 *packet_count,
265 						guint32 *octet_count);
266 gboolean        rtp_source_get_last_rb         (RTPSource *src, guint8 *fractionlost, gint32 *packetslost,
267                                                 guint32 *exthighestseq, guint32 *jitter,
268                                                 guint32 *lsr, guint32 *dlsr, guint32 *round_trip);
269 
270 void            rtp_source_reset               (RTPSource * src);
271 
272 gboolean        rtp_source_find_conflicting_address (RTPSource * src,
273                                                 GSocketAddress *address,
274                                                 GstClockTime time);
275 
276 void            rtp_source_add_conflicting_address (RTPSource * src,
277                                                 GSocketAddress *address,
278                                                 GstClockTime time);
279 
280 gboolean        find_conflicting_address       (GList * conflicting_address,
281                                                 GSocketAddress * address,
282                                                 GstClockTime time);
283 
284 GList *         add_conflicting_address        (GList * conflicting_addresses,
285                                                 GSocketAddress * address,
286                                                 GstClockTime time);
287 GList *         timeout_conflicting_addresses  (GList * conflicting_addresses,
288                                                 GstClockTime current_time);
289 
290 void            rtp_conflicting_address_free   (RTPConflictingAddress * addr);
291 
292 void            rtp_source_timeout             (RTPSource * src,
293                                                 GstClockTime current_time,
294                                                 GstClockTime running_time,
295                                                 GstClockTime feedback_retention_window);
296 
297 void            rtp_source_retain_rtcp_packet  (RTPSource * src,
298                                                 GstRTCPPacket *pkt,
299                                                 GstClockTime running_time);
300 gboolean        rtp_source_has_retained        (RTPSource * src,
301                                                 GCompareFunc func,
302                                                 gconstpointer data);
303 
304 void            rtp_source_register_nack       (RTPSource * src,
305                                                 guint16 seqnum,
306                                                 GstClockTime deadline);
307 guint16 *       rtp_source_get_nacks           (RTPSource * src, guint *n_nacks);
308 GstClockTime *  rtp_source_get_nack_deadlines  (RTPSource * src, guint *n_nacks);
309 void            rtp_source_clear_nacks         (RTPSource * src, guint n_nacks);
310 
311 #endif /* __RTP_SOURCE_H__ */
312