• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.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
13  */
14 
15 /**
16  * SECTION:gstrtpbasepayload
17  * @title: GstRTPBasePayload
18  * @short_description: Base class for RTP payloader
19  *
20  * Provides a base class for RTP payloaders
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <string.h>
28 
29 #include <gst/rtp/gstrtpbuffer.h>
30 
31 #include "gstrtpbasepayload.h"
32 #include "gstrtpmeta.h"
33 #include "gstrtphdrext.h"
34 
35 GST_DEBUG_CATEGORY_STATIC (rtpbasepayload_debug);
36 #define GST_CAT_DEFAULT (rtpbasepayload_debug)
37 
38 struct _GstRTPBasePayloadPrivate
39 {
40   gboolean ts_offset_random;
41   gboolean seqnum_offset_random;
42   gboolean ssrc_random;
43   guint16 next_seqnum;
44   gboolean perfect_rtptime;
45   gint notified_first_timestamp;
46 
47   gboolean pt_set;
48 
49   gboolean source_info;
50   GstBuffer *input_meta_buffer;
51 
52   guint64 base_offset;
53   gint64 base_rtime;
54   guint64 base_rtime_hz;
55   guint64 running_time;
56   gboolean scale_rtptime;
57   gboolean auto_hdr_ext;
58 
59   gint64 prop_max_ptime;
60   gint64 caps_max_ptime;
61 
62   gboolean onvif_no_rate_control;
63 
64   gboolean negotiated;
65 
66   /* We need to know whether negotiate was called in order to decide
67    * whether we should store the input buffer as input meta in case
68    * negotiate() gets called from the subclass' handle_buffer() implementation,
69    * as negotiate() is where we instantiate header extensions.
70    */
71   gboolean negotiate_called;
72 
73   gboolean delay_segment;
74   GstEvent *pending_segment;
75 
76   GstCaps *subclass_srccaps;
77   GstCaps *sinkcaps;
78 
79   /* array of GstRTPHeaderExtension's * */
80   GPtrArray *header_exts;
81 };
82 
83 /* RTPBasePayload signals and args */
84 enum
85 {
86   SIGNAL_0,
87   SIGNAL_REQUEST_EXTENSION,
88   SIGNAL_ADD_EXTENSION,
89   SIGNAL_CLEAR_EXTENSIONS,
90   LAST_SIGNAL
91 };
92 
93 static guint gst_rtp_base_payload_signals[LAST_SIGNAL] = { 0 };
94 
95 /* FIXME 0.11, a better default is the Ethernet MTU of
96  * 1500 - sizeof(headers) as pointed out by marcelm in IRC:
97  * So an Ethernet MTU of 1500, minus 60 for the max IP, minus 8 for UDP, gives
98  * 1432 bytes or so.  And that should be adjusted downward further for other
99  * encapsulations like PPPoE, so 1400 at most.
100  */
101 #define DEFAULT_MTU                     1400
102 #define DEFAULT_PT                      96
103 #define DEFAULT_SSRC                    -1
104 #define DEFAULT_TIMESTAMP_OFFSET        -1
105 #define DEFAULT_SEQNUM_OFFSET           -1
106 #define DEFAULT_MAX_PTIME               -1
107 #define DEFAULT_MIN_PTIME               0
108 #define DEFAULT_PERFECT_RTPTIME         TRUE
109 #define DEFAULT_PTIME_MULTIPLE          0
110 #define DEFAULT_RUNNING_TIME            GST_CLOCK_TIME_NONE
111 #define DEFAULT_SOURCE_INFO             FALSE
112 #define DEFAULT_ONVIF_NO_RATE_CONTROL   FALSE
113 #define DEFAULT_SCALE_RTPTIME           TRUE
114 #define DEFAULT_AUTO_HEADER_EXTENSION   TRUE
115 
116 #define RTP_HEADER_EXT_ONE_BYTE_MAX_SIZE 16
117 #define RTP_HEADER_EXT_TWO_BYTE_MAX_SIZE 256
118 #define RTP_HEADER_EXT_ONE_BYTE_MAX_ID 14
119 #define RTP_HEADER_EXT_TWO_BYTE_MAX_ID 255
120 
121 enum
122 {
123   PROP_0,
124   PROP_MTU,
125   PROP_PT,
126   PROP_SSRC,
127   PROP_TIMESTAMP_OFFSET,
128   PROP_SEQNUM_OFFSET,
129   PROP_MAX_PTIME,
130   PROP_MIN_PTIME,
131   PROP_TIMESTAMP,
132   PROP_SEQNUM,
133   PROP_PERFECT_RTPTIME,
134   PROP_PTIME_MULTIPLE,
135   PROP_STATS,
136   PROP_SOURCE_INFO,
137   PROP_ONVIF_NO_RATE_CONTROL,
138   PROP_SCALE_RTPTIME,
139   PROP_AUTO_HEADER_EXTENSION,
140   PROP_LAST
141 };
142 
143 static void gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass);
144 static void gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload,
145     gpointer g_class);
146 static void gst_rtp_base_payload_finalize (GObject * object);
147 
148 static GstCaps *gst_rtp_base_payload_getcaps_default (GstRTPBasePayload *
149     rtpbasepayload, GstPad * pad, GstCaps * filter);
150 
151 static gboolean gst_rtp_base_payload_sink_event_default (GstRTPBasePayload *
152     rtpbasepayload, GstEvent * event);
153 static gboolean gst_rtp_base_payload_sink_event (GstPad * pad,
154     GstObject * parent, GstEvent * event);
155 static gboolean gst_rtp_base_payload_src_event_default (GstRTPBasePayload *
156     rtpbasepayload, GstEvent * event);
157 static gboolean gst_rtp_base_payload_src_event (GstPad * pad,
158     GstObject * parent, GstEvent * event);
159 static gboolean gst_rtp_base_payload_query_default (GstRTPBasePayload *
160     rtpbasepayload, GstPad * pad, GstQuery * query);
161 static gboolean gst_rtp_base_payload_query (GstPad * pad, GstObject * parent,
162     GstQuery * query);
163 static GstFlowReturn gst_rtp_base_payload_chain (GstPad * pad,
164     GstObject * parent, GstBuffer * buffer);
165 
166 static void gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
167     const GValue * value, GParamSpec * pspec);
168 static void gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
169     GValue * value, GParamSpec * pspec);
170 
171 static GstStateChangeReturn gst_rtp_base_payload_change_state (GstElement *
172     element, GstStateChange transition);
173 
174 static gboolean gst_rtp_base_payload_negotiate (GstRTPBasePayload * payload);
175 
176 static void gst_rtp_base_payload_add_extension (GstRTPBasePayload * payload,
177     GstRTPHeaderExtension * ext);
178 static void gst_rtp_base_payload_clear_extensions (GstRTPBasePayload * payload);
179 
180 static GstElementClass *parent_class = NULL;
181 static gint private_offset = 0;
182 
183 GType
gst_rtp_base_payload_get_type(void)184 gst_rtp_base_payload_get_type (void)
185 {
186   static GType rtpbasepayload_type = 0;
187 
188   if (g_once_init_enter ((gsize *) & rtpbasepayload_type)) {
189     static const GTypeInfo rtpbasepayload_info = {
190       sizeof (GstRTPBasePayloadClass),
191       NULL,
192       NULL,
193       (GClassInitFunc) gst_rtp_base_payload_class_init,
194       NULL,
195       NULL,
196       sizeof (GstRTPBasePayload),
197       0,
198       (GInstanceInitFunc) gst_rtp_base_payload_init,
199     };
200     GType _type;
201 
202     _type = g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBasePayload",
203         &rtpbasepayload_info, G_TYPE_FLAG_ABSTRACT);
204 
205     private_offset =
206         g_type_add_instance_private (_type, sizeof (GstRTPBasePayloadPrivate));
207 
208     g_once_init_leave ((gsize *) & rtpbasepayload_type, _type);
209   }
210   return rtpbasepayload_type;
211 }
212 
213 static inline GstRTPBasePayloadPrivate *
gst_rtp_base_payload_get_instance_private(GstRTPBasePayload * self)214 gst_rtp_base_payload_get_instance_private (GstRTPBasePayload * self)
215 {
216   return (G_STRUCT_MEMBER_P (self, private_offset));
217 }
218 
219 static GstRTPHeaderExtension *
gst_rtp_base_payload_request_extension_default(GstRTPBasePayload * payload,guint ext_id,const gchar * uri)220 gst_rtp_base_payload_request_extension_default (GstRTPBasePayload * payload,
221     guint ext_id, const gchar * uri)
222 {
223   GstRTPHeaderExtension *ext = NULL;
224 
225   if (!payload->priv->auto_hdr_ext)
226     return NULL;
227 
228   ext = gst_rtp_header_extension_create_from_uri (uri);
229   if (ext) {
230     GST_DEBUG_OBJECT (payload,
231         "Automatically enabled extension %s for uri \'%s\'",
232         GST_ELEMENT_NAME (ext), uri);
233 
234     gst_rtp_header_extension_set_id (ext, ext_id);
235   } else {
236     GST_DEBUG_OBJECT (payload,
237         "Didn't find any extension implementing uri \'%s\'", uri);
238   }
239 
240   return ext;
241 }
242 
243 static gboolean
extension_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer data)244 extension_accumulator (GSignalInvocationHint * ihint,
245     GValue * return_accu, const GValue * handler_return, gpointer data)
246 {
247   gpointer ext;
248 
249   /* Call default handler if user callback didn't create the extension */
250   ext = g_value_get_object (handler_return);
251   if (!ext)
252     return TRUE;
253 
254   g_value_set_object (return_accu, ext);
255   return FALSE;
256 }
257 
258 static void
gst_rtp_base_payload_class_init(GstRTPBasePayloadClass * klass)259 gst_rtp_base_payload_class_init (GstRTPBasePayloadClass * klass)
260 {
261   GObjectClass *gobject_class;
262   GstElementClass *gstelement_class;
263 
264   gobject_class = (GObjectClass *) klass;
265   gstelement_class = (GstElementClass *) klass;
266 
267   if (private_offset != 0)
268     g_type_class_adjust_private_offset (klass, &private_offset);
269 
270   parent_class = g_type_class_peek_parent (klass);
271 
272   gobject_class->finalize = gst_rtp_base_payload_finalize;
273 
274   gobject_class->set_property = gst_rtp_base_payload_set_property;
275   gobject_class->get_property = gst_rtp_base_payload_get_property;
276 
277   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
278       g_param_spec_uint ("mtu", "MTU",
279           "Maximum size of one packet",
280           28, G_MAXUINT, DEFAULT_MTU,
281           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PT,
283       g_param_spec_uint ("pt", "payload type",
284           "The payload type of the packets", 0, 0x7f, DEFAULT_PT,
285           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
287       g_param_spec_uint ("ssrc", "SSRC",
288           "The SSRC of the packets (default == random)", 0, G_MAXUINT32,
289           DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290   g_object_class_install_property (G_OBJECT_CLASS (klass),
291       PROP_TIMESTAMP_OFFSET, g_param_spec_uint ("timestamp-offset",
292           "Timestamp Offset",
293           "Offset to add to all outgoing timestamps (default = random)", 0,
294           G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
295           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
297       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
298           "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXUINT16,
299           DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_PTIME,
301       g_param_spec_int64 ("max-ptime", "Max packet time",
302           "Maximum duration of the packet data in ns (-1 = unlimited up to MTU)",
303           -1, G_MAXINT64, DEFAULT_MAX_PTIME,
304           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305   /**
306    * GstRTPBasePayload:min-ptime:
307    *
308    * Minimum duration of the packet data in ns (can't go above MTU)
309    **/
310   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MIN_PTIME,
311       g_param_spec_int64 ("min-ptime", "Min packet time",
312           "Minimum duration of the packet data in ns (can't go above MTU)",
313           0, G_MAXINT64, DEFAULT_MIN_PTIME,
314           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315 
316   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMESTAMP,
317       g_param_spec_uint ("timestamp", "Timestamp",
318           "The RTP timestamp of the last processed packet",
319           0, G_MAXUINT32, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
321       g_param_spec_uint ("seqnum", "Sequence number",
322           "The RTP sequence number of the last processed packet",
323           0, G_MAXUINT16, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
324 
325   /**
326    * GstRTPBasePayload:perfect-rtptime:
327    *
328    * Try to use the offset fields to generate perfect RTP timestamps. When this
329    * option is disabled, RTP timestamps are generated from GST_BUFFER_PTS of
330    * each payloaded buffer. The PTSes of buffers may not necessarily increment
331    * with the amount of data in each input buffer, consider e.g. the case where
332    * the buffer arrives from a network which means that the PTS is unrelated to
333    * the amount of data. Because the RTP timestamps are generated from
334    * GST_BUFFER_PTS this can result in RTP timestamps that also don't increment
335    * with the amount of data in the payloaded packet. To circumvent this it is
336    * possible to set the perfect rtptime option enabled. When this option is
337    * enabled the payloader will increment the RTP timestamps based on
338    * GST_BUFFER_OFFSET which relates to the amount of data in each packet
339    * rather than the GST_BUFFER_PTS of each buffer and therefore the RTP
340    * timestamps will more closely correlate with the amount of data in each
341    * buffer. Currently GstRTPBasePayload is limited to handling perfect RTP
342    * timestamps for audio streams.
343    */
344   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PERFECT_RTPTIME,
345       g_param_spec_boolean ("perfect-rtptime", "Perfect RTP Time",
346           "Generate perfect RTP timestamps when possible",
347           DEFAULT_PERFECT_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348   /**
349    * GstRTPBasePayload:ptime-multiple:
350    *
351    * Force buffers to be multiples of this duration in ns (0 disables)
352    **/
353   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PTIME_MULTIPLE,
354       g_param_spec_int64 ("ptime-multiple", "Packet time multiple",
355           "Force buffers to be multiples of this duration in ns (0 disables)",
356           0, G_MAXINT64, DEFAULT_PTIME_MULTIPLE,
357           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 
359   /**
360    * GstRTPBasePayload:stats:
361    *
362    * Various payloader statistics retrieved atomically (and are therefore
363    * synchroized with each other), these can be used e.g. to generate an
364    * RTP-Info header. This property return a GstStructure named
365    * application/x-rtp-payload-stats containing the following fields relating to
366    * the last processed buffer and current state of the stream being payloaded:
367    *
368    *   * `clock-rate` :#G_TYPE_UINT, clock-rate of the stream
369    *   * `running-time` :#G_TYPE_UINT64, running time
370    *   * `seqnum` :#G_TYPE_UINT, sequence number, same as #GstRTPBasePayload:seqnum
371    *   * `timestamp` :#G_TYPE_UINT, RTP timestamp, same as #GstRTPBasePayload:timestamp
372    *   * `ssrc` :#G_TYPE_UINT, The SSRC in use
373    *   * `pt` :#G_TYPE_UINT, The Payload type in use, same as #GstRTPBasePayload:pt
374    *   * `seqnum-offset` :#G_TYPE_UINT, The current offset added to the seqnum
375    *   * `timestamp-offset` :#G_TYPE_UINT, The current offset added to the timestamp
376    **/
377   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_STATS,
378       g_param_spec_boxed ("stats", "Statistics", "Various statistics",
379           GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
380 
381   /**
382    * GstRTPBasePayload:source-info:
383    *
384    * Enable writing the CSRC field in allocated RTP header based on RTP source
385    * information found in the input buffer's #GstRTPSourceMeta.
386    *
387    * Since: 1.16
388    **/
389   g_object_class_install_property (gobject_class, PROP_SOURCE_INFO,
390       g_param_spec_boolean ("source-info", "RTP source information",
391           "Write CSRC based on buffer meta RTP source information",
392           DEFAULT_SOURCE_INFO, G_PARAM_READWRITE));
393 
394   /**
395    * GstRTPBasePayload:onvif-no-rate-control:
396    *
397    * Make the payloader timestamp packets according to the Rate-Control=no
398    * behaviour specified in the ONVIF replay spec.
399    *
400    * Since: 1.16
401    */
402   g_object_class_install_property (G_OBJECT_CLASS (klass),
403       PROP_ONVIF_NO_RATE_CONTROL, g_param_spec_boolean ("onvif-no-rate-control",
404           "ONVIF no rate control",
405           "Enable ONVIF Rate-Control=no timestamping mode",
406           DEFAULT_ONVIF_NO_RATE_CONTROL,
407           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 
409   /**
410    * GstRTPBasePayload:scale-rtptime:
411    *
412    * Make the RTP packets' timestamps be scaled with the segment's rate
413    * (corresponding to RTSP speed parameter). Disabling this property means
414    * the timestamps will not be affected by the set delivery speed (RTSP speed).
415    *
416    * Example: A server wants to allow streaming a recorded video in double
417    * speed but still have the timestamps correspond to the position in the
418    * video. This is achieved by the client setting RTSP Speed to 2 while the
419    * server has this property disabled.
420    *
421    * Since: 1.18
422    */
423   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SCALE_RTPTIME,
424       g_param_spec_boolean ("scale-rtptime", "Scale RTP time",
425           "Whether the RTP timestamp should be scaled with the rate (speed)",
426           DEFAULT_SCALE_RTPTIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 
428   /**
429    * GstRTPBasePayload:auto-header-extension:
430    *
431    * If enabled, the payloader will automatically try to enable all the
432    * RTP header extensions provided in the src caps, saving the application
433    * the need to handle these extensions manually using the
434    * GstRTPBasePayload::request-extension: signal.
435    *
436    * Since: 1.20
437    */
438   g_object_class_install_property (G_OBJECT_CLASS (klass),
439       PROP_AUTO_HEADER_EXTENSION, g_param_spec_boolean ("auto-header-extension",
440           "Automatic RTP header extension",
441           "Whether RTP header extensions should be automatically enabled, if an implementation is available",
442           DEFAULT_AUTO_HEADER_EXTENSION,
443           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 
445   /**
446    * GstRTPBasePayload::add-extension:
447    * @object: the #GstRTPBasePayload
448    * @ext: (transfer full): the #GstRTPHeaderExtension
449    *
450    * Add @ext as an extension for writing part of an RTP header extension onto
451    * outgoing RTP packets.
452    *
453    * Since: 1.20
454    */
455   gst_rtp_base_payload_signals[SIGNAL_ADD_EXTENSION] =
456       g_signal_new_class_handler ("add-extension", G_TYPE_FROM_CLASS (klass),
457       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
458       G_CALLBACK (gst_rtp_base_payload_add_extension), NULL, NULL, NULL,
459       G_TYPE_NONE, 1, GST_TYPE_RTP_HEADER_EXTENSION);
460 
461   /**
462    * GstRTPBasePayload::request-extension:
463    * @object: the #GstRTPBasePayload
464    * @ext_id: the extension id being requested
465    * @ext_uri: the extension URI being requested
466    *
467    * The returned @ext must be configured with the correct @ext_id and with the
468    * necessary attributes as required by the extension implementation.
469    *
470    * Returns: (transfer full): the #GstRTPHeaderExtension for @ext_id, or %NULL
471    *
472    * Since: 1.20
473    */
474   gst_rtp_base_payload_signals[SIGNAL_REQUEST_EXTENSION] =
475       g_signal_new_class_handler ("request-extension",
476       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
477       G_CALLBACK (gst_rtp_base_payload_request_extension_default),
478       extension_accumulator, NULL, NULL,
479       GST_TYPE_RTP_HEADER_EXTENSION, 2, G_TYPE_UINT, G_TYPE_STRING);
480 
481   /**
482    * GstRTPBasePayload::clear-extensions:
483    * @object: the #GstRTPBasePayload
484    *
485    * Clear all RTP header extensions used by this payloader.
486    *
487    * Since: 1.20
488    */
489   gst_rtp_base_payload_signals[SIGNAL_CLEAR_EXTENSIONS] =
490       g_signal_new_class_handler ("clear-extensions", G_TYPE_FROM_CLASS (klass),
491       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
492       G_CALLBACK (gst_rtp_base_payload_clear_extensions), NULL, NULL, NULL,
493       G_TYPE_NONE, 0);
494 
495   gstelement_class->change_state = gst_rtp_base_payload_change_state;
496 
497   klass->get_caps = gst_rtp_base_payload_getcaps_default;
498   klass->sink_event = gst_rtp_base_payload_sink_event_default;
499   klass->src_event = gst_rtp_base_payload_src_event_default;
500   klass->query = gst_rtp_base_payload_query_default;
501 
502   GST_DEBUG_CATEGORY_INIT (rtpbasepayload_debug, "rtpbasepayload", 0,
503       "Base class for RTP Payloaders");
504 }
505 
506 static void
gst_rtp_base_payload_init(GstRTPBasePayload * rtpbasepayload,gpointer g_class)507 gst_rtp_base_payload_init (GstRTPBasePayload * rtpbasepayload, gpointer g_class)
508 {
509   GstPadTemplate *templ;
510   GstRTPBasePayloadPrivate *priv;
511 
512   rtpbasepayload->priv = priv =
513       gst_rtp_base_payload_get_instance_private (rtpbasepayload);
514 
515   templ =
516       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
517   g_return_if_fail (templ != NULL);
518 
519   rtpbasepayload->srcpad = gst_pad_new_from_template (templ, "src");
520   gst_pad_set_event_function (rtpbasepayload->srcpad,
521       gst_rtp_base_payload_src_event);
522   gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->srcpad);
523 
524   templ =
525       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
526   g_return_if_fail (templ != NULL);
527 
528   rtpbasepayload->sinkpad = gst_pad_new_from_template (templ, "sink");
529   gst_pad_set_chain_function (rtpbasepayload->sinkpad,
530       gst_rtp_base_payload_chain);
531   gst_pad_set_event_function (rtpbasepayload->sinkpad,
532       gst_rtp_base_payload_sink_event);
533   gst_pad_set_query_function (rtpbasepayload->sinkpad,
534       gst_rtp_base_payload_query);
535   gst_element_add_pad (GST_ELEMENT (rtpbasepayload), rtpbasepayload->sinkpad);
536 
537   rtpbasepayload->mtu = DEFAULT_MTU;
538   rtpbasepayload->pt = DEFAULT_PT;
539   rtpbasepayload->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
540   rtpbasepayload->ssrc = DEFAULT_SSRC;
541   rtpbasepayload->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
542   priv->running_time = DEFAULT_RUNNING_TIME;
543   priv->seqnum_offset_random = (rtpbasepayload->seqnum_offset == -1);
544   priv->ts_offset_random = (rtpbasepayload->ts_offset == -1);
545   priv->ssrc_random = (rtpbasepayload->ssrc == -1);
546   priv->pt_set = FALSE;
547   priv->source_info = DEFAULT_SOURCE_INFO;
548 
549   rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
550   rtpbasepayload->min_ptime = DEFAULT_MIN_PTIME;
551   rtpbasepayload->priv->perfect_rtptime = DEFAULT_PERFECT_RTPTIME;
552   rtpbasepayload->ptime_multiple = DEFAULT_PTIME_MULTIPLE;
553   rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
554   rtpbasepayload->priv->base_rtime_hz = GST_BUFFER_OFFSET_NONE;
555   rtpbasepayload->priv->onvif_no_rate_control = DEFAULT_ONVIF_NO_RATE_CONTROL;
556   rtpbasepayload->priv->scale_rtptime = DEFAULT_SCALE_RTPTIME;
557   rtpbasepayload->priv->auto_hdr_ext = DEFAULT_AUTO_HEADER_EXTENSION;
558 
559   rtpbasepayload->media = NULL;
560   rtpbasepayload->encoding_name = NULL;
561 
562   rtpbasepayload->clock_rate = 0;
563 
564   rtpbasepayload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
565   rtpbasepayload->priv->prop_max_ptime = DEFAULT_MAX_PTIME;
566   rtpbasepayload->priv->header_exts =
567       g_ptr_array_new_with_free_func ((GDestroyNotify) gst_object_unref);
568 }
569 
570 static void
gst_rtp_base_payload_finalize(GObject * object)571 gst_rtp_base_payload_finalize (GObject * object)
572 {
573   GstRTPBasePayload *rtpbasepayload;
574 
575   rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
576 
577   g_free (rtpbasepayload->media);
578   rtpbasepayload->media = NULL;
579   g_free (rtpbasepayload->encoding_name);
580   rtpbasepayload->encoding_name = NULL;
581 
582   gst_caps_replace (&rtpbasepayload->priv->subclass_srccaps, NULL);
583   gst_caps_replace (&rtpbasepayload->priv->sinkcaps, NULL);
584 
585   g_ptr_array_unref (rtpbasepayload->priv->header_exts);
586   rtpbasepayload->priv->header_exts = NULL;
587 
588   G_OBJECT_CLASS (parent_class)->finalize (object);
589 }
590 
591 static GstCaps *
gst_rtp_base_payload_getcaps_default(GstRTPBasePayload * rtpbasepayload,GstPad * pad,GstCaps * filter)592 gst_rtp_base_payload_getcaps_default (GstRTPBasePayload * rtpbasepayload,
593     GstPad * pad, GstCaps * filter)
594 {
595   GstCaps *caps;
596 
597   caps = GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
598   GST_DEBUG_OBJECT (pad,
599       "using pad template %p with caps %p %" GST_PTR_FORMAT,
600       GST_PAD_PAD_TEMPLATE (pad), caps, caps);
601 
602   if (filter)
603     caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
604   else
605     caps = gst_caps_ref (caps);
606 
607   return caps;
608 }
609 
610 static gboolean
gst_rtp_base_payload_sink_event_default(GstRTPBasePayload * rtpbasepayload,GstEvent * event)611 gst_rtp_base_payload_sink_event_default (GstRTPBasePayload * rtpbasepayload,
612     GstEvent * event)
613 {
614   GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
615   gboolean res = FALSE;
616 
617   switch (GST_EVENT_TYPE (event)) {
618     case GST_EVENT_FLUSH_START:
619       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
620       break;
621     case GST_EVENT_FLUSH_STOP:
622       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
623       gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
624       gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
625       break;
626     case GST_EVENT_CAPS:
627     {
628       GstRTPBasePayloadClass *rtpbasepayload_class;
629       GstCaps *caps;
630 
631       gst_event_parse_caps (event, &caps);
632       GST_DEBUG_OBJECT (rtpbasepayload, "setting caps %" GST_PTR_FORMAT, caps);
633 
634       gst_caps_replace (&rtpbasepayload->priv->sinkcaps, caps);
635 
636       rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
637       if (rtpbasepayload_class->set_caps)
638         res = rtpbasepayload_class->set_caps (rtpbasepayload, caps);
639       else
640         res = gst_rtp_base_payload_negotiate (rtpbasepayload);
641 
642       rtpbasepayload->priv->negotiated = res;
643 
644       gst_event_unref (event);
645       break;
646     }
647     case GST_EVENT_SEGMENT:
648     {
649       GstSegment *segment;
650 
651       segment = &rtpbasepayload->segment;
652       gst_event_copy_segment (event, segment);
653 
654       rtpbasepayload->priv->base_offset = GST_BUFFER_OFFSET_NONE;
655 
656       GST_DEBUG_OBJECT (rtpbasepayload,
657           "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
658       if (rtpbasepayload->priv->delay_segment) {
659         gst_event_replace (&rtpbasepayload->priv->pending_segment, event);
660         gst_event_unref (event);
661         res = TRUE;
662       } else {
663         res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
664       }
665       break;
666     }
667     case GST_EVENT_GAP:
668     {
669       if (G_UNLIKELY (rtpbasepayload->priv->pending_segment)) {
670         gst_pad_push_event (rtpbasepayload->srcpad,
671             rtpbasepayload->priv->pending_segment);
672         rtpbasepayload->priv->pending_segment = FALSE;
673         rtpbasepayload->priv->delay_segment = FALSE;
674       }
675       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
676       break;
677     }
678     default:
679       res = gst_pad_event_default (rtpbasepayload->sinkpad, parent, event);
680       break;
681   }
682   return res;
683 }
684 
685 static gboolean
gst_rtp_base_payload_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)686 gst_rtp_base_payload_sink_event (GstPad * pad, GstObject * parent,
687     GstEvent * event)
688 {
689   GstRTPBasePayload *rtpbasepayload;
690   GstRTPBasePayloadClass *rtpbasepayload_class;
691   gboolean res = FALSE;
692 
693   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
694   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
695 
696   if (rtpbasepayload_class->sink_event)
697     res = rtpbasepayload_class->sink_event (rtpbasepayload, event);
698   else
699     gst_event_unref (event);
700 
701   return res;
702 }
703 
704 static gboolean
gst_rtp_base_payload_src_event_default(GstRTPBasePayload * rtpbasepayload,GstEvent * event)705 gst_rtp_base_payload_src_event_default (GstRTPBasePayload * rtpbasepayload,
706     GstEvent * event)
707 {
708   GstObject *parent = GST_OBJECT_CAST (rtpbasepayload);
709   gboolean res = TRUE, forward = TRUE;
710 
711   switch (GST_EVENT_TYPE (event)) {
712     case GST_EVENT_CUSTOM_UPSTREAM:
713     {
714       const GstStructure *s = gst_event_get_structure (event);
715 
716       if (gst_structure_has_name (s, "GstRTPCollision")) {
717         guint ssrc = 0;
718 
719         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
720           ssrc = -1;
721 
722         GST_DEBUG_OBJECT (rtpbasepayload, "collided ssrc: %" G_GUINT32_FORMAT,
723             ssrc);
724 
725         /* choose another ssrc for our stream */
726         if (ssrc == rtpbasepayload->current_ssrc) {
727           GstCaps *caps;
728           guint suggested_ssrc = 0;
729 
730           if (gst_structure_get_uint (s, "suggested-ssrc", &suggested_ssrc))
731             rtpbasepayload->current_ssrc = suggested_ssrc;
732 
733           while (ssrc == rtpbasepayload->current_ssrc)
734             rtpbasepayload->current_ssrc = g_random_int ();
735 
736           caps = gst_pad_get_current_caps (rtpbasepayload->srcpad);
737           if (caps) {
738             caps = gst_caps_make_writable (caps);
739             gst_caps_set_simple (caps,
740                 "ssrc", G_TYPE_UINT, rtpbasepayload->current_ssrc, NULL);
741             res = gst_pad_set_caps (rtpbasepayload->srcpad, caps);
742             gst_caps_unref (caps);
743           }
744 
745           /* the event was for us */
746           forward = FALSE;
747         }
748       }
749       break;
750     }
751     default:
752       break;
753   }
754 
755   if (forward)
756     res = gst_pad_event_default (rtpbasepayload->srcpad, parent, event);
757   else
758     gst_event_unref (event);
759 
760   return res;
761 }
762 
763 static gboolean
gst_rtp_base_payload_src_event(GstPad * pad,GstObject * parent,GstEvent * event)764 gst_rtp_base_payload_src_event (GstPad * pad, GstObject * parent,
765     GstEvent * event)
766 {
767   GstRTPBasePayload *rtpbasepayload;
768   GstRTPBasePayloadClass *rtpbasepayload_class;
769   gboolean res = FALSE;
770 
771   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
772   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
773 
774   if (rtpbasepayload_class->src_event)
775     res = rtpbasepayload_class->src_event (rtpbasepayload, event);
776   else
777     gst_event_unref (event);
778 
779   return res;
780 }
781 
782 
783 static gboolean
gst_rtp_base_payload_query_default(GstRTPBasePayload * rtpbasepayload,GstPad * pad,GstQuery * query)784 gst_rtp_base_payload_query_default (GstRTPBasePayload * rtpbasepayload,
785     GstPad * pad, GstQuery * query)
786 {
787   gboolean res = FALSE;
788 
789   switch (GST_QUERY_TYPE (query)) {
790     case GST_QUERY_CAPS:
791     {
792       GstRTPBasePayloadClass *rtpbasepayload_class;
793       GstCaps *filter, *caps;
794 
795       gst_query_parse_caps (query, &filter);
796       GST_DEBUG_OBJECT (rtpbasepayload, "getting caps with filter %"
797           GST_PTR_FORMAT, filter);
798 
799       rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
800       if (rtpbasepayload_class->get_caps) {
801         caps = rtpbasepayload_class->get_caps (rtpbasepayload, pad, filter);
802         gst_query_set_caps_result (query, caps);
803         gst_caps_unref (caps);
804         res = TRUE;
805       }
806       break;
807     }
808     default:
809       res =
810           gst_pad_query_default (pad, GST_OBJECT_CAST (rtpbasepayload), query);
811       break;
812   }
813   return res;
814 }
815 
816 static gboolean
gst_rtp_base_payload_query(GstPad * pad,GstObject * parent,GstQuery * query)817 gst_rtp_base_payload_query (GstPad * pad, GstObject * parent, GstQuery * query)
818 {
819   GstRTPBasePayload *rtpbasepayload;
820   GstRTPBasePayloadClass *rtpbasepayload_class;
821   gboolean res = FALSE;
822 
823   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
824   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
825 
826   if (rtpbasepayload_class->query)
827     res = rtpbasepayload_class->query (rtpbasepayload, pad, query);
828 
829   return res;
830 }
831 
832 static GstFlowReturn
gst_rtp_base_payload_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)833 gst_rtp_base_payload_chain (GstPad * pad, GstObject * parent,
834     GstBuffer * buffer)
835 {
836   GstRTPBasePayload *rtpbasepayload;
837   GstRTPBasePayloadClass *rtpbasepayload_class;
838   GstFlowReturn ret;
839 
840   rtpbasepayload = GST_RTP_BASE_PAYLOAD (parent);
841   rtpbasepayload_class = GST_RTP_BASE_PAYLOAD_GET_CLASS (rtpbasepayload);
842 
843   if (!rtpbasepayload_class->handle_buffer)
844     goto no_function;
845 
846   if (!rtpbasepayload->priv->negotiated)
847     goto not_negotiated;
848 
849   if (rtpbasepayload->priv->source_info
850       || rtpbasepayload->priv->header_exts->len > 0
851       || !rtpbasepayload->priv->negotiate_called) {
852     /* Save a copy of meta (instead of taking an extra reference before
853      * handle_buffer) to make the meta available when allocating a output
854      * buffer. */
855     rtpbasepayload->priv->input_meta_buffer = gst_buffer_new ();
856     gst_buffer_copy_into (rtpbasepayload->priv->input_meta_buffer, buffer,
857         GST_BUFFER_COPY_METADATA, 0, -1);
858   }
859 
860   if (gst_pad_check_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (rtpbasepayload))) {
861     if (!gst_rtp_base_payload_negotiate (rtpbasepayload)) {
862       gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (rtpbasepayload));
863       if (GST_PAD_IS_FLUSHING (GST_RTP_BASE_PAYLOAD_SRCPAD (rtpbasepayload))) {
864         goto flushing;
865       } else {
866         goto negotiate_failed;
867       }
868     }
869   }
870 
871   ret = rtpbasepayload_class->handle_buffer (rtpbasepayload, buffer);
872 
873   gst_buffer_replace (&rtpbasepayload->priv->input_meta_buffer, NULL);
874 
875   return ret;
876 
877   /* ERRORS */
878 no_function:
879   {
880     GST_ELEMENT_ERROR (rtpbasepayload, STREAM, NOT_IMPLEMENTED, (NULL),
881         ("subclass did not implement handle_buffer function"));
882     gst_buffer_unref (buffer);
883     return GST_FLOW_ERROR;
884   }
885 not_negotiated:
886   {
887     GST_ELEMENT_ERROR (rtpbasepayload, CORE, NEGOTIATION, (NULL),
888         ("No input format was negotiated, i.e. no caps event was received. "
889             "Perhaps you need a parser or typefind element before the payloader"));
890     gst_buffer_unref (buffer);
891     return GST_FLOW_NOT_NEGOTIATED;
892   }
893 negotiate_failed:
894   {
895     GST_DEBUG_OBJECT (rtpbasepayload, "Not negotiated");
896     gst_buffer_unref (buffer);
897     return GST_FLOW_NOT_NEGOTIATED;
898   }
899 flushing:
900   {
901     GST_DEBUG_OBJECT (rtpbasepayload, "we are flushing");
902     gst_buffer_unref (buffer);
903     return GST_FLOW_FLUSHING;
904   }
905 }
906 
907 /**
908  * gst_rtp_base_payload_set_options:
909  * @payload: a #GstRTPBasePayload
910  * @media: the media type (typically "audio" or "video")
911  * @dynamic: if the payload type is dynamic
912  * @encoding_name: the encoding name
913  * @clock_rate: the clock rate of the media
914  *
915  * Set the rtp options of the payloader. These options will be set in the caps
916  * of the payloader. Subclasses must call this method before calling
917  * gst_rtp_base_payload_push() or gst_rtp_base_payload_set_outcaps().
918  */
919 void
gst_rtp_base_payload_set_options(GstRTPBasePayload * payload,const gchar * media,gboolean dynamic,const gchar * encoding_name,guint32 clock_rate)920 gst_rtp_base_payload_set_options (GstRTPBasePayload * payload,
921     const gchar * media, gboolean dynamic, const gchar * encoding_name,
922     guint32 clock_rate)
923 {
924   g_return_if_fail (payload != NULL);
925   g_return_if_fail (clock_rate != 0);
926 
927   g_free (payload->media);
928   payload->media = g_strdup (media);
929   payload->dynamic = dynamic;
930   g_free (payload->encoding_name);
931   payload->encoding_name = g_strdup (encoding_name);
932   payload->clock_rate = clock_rate;
933 }
934 
935 static gboolean
copy_fixed(GQuark field_id,const GValue * value,GstStructure * dest)936 copy_fixed (GQuark field_id, const GValue * value, GstStructure * dest)
937 {
938   if (gst_value_is_fixed (value)) {
939     gst_structure_id_set_value (dest, field_id, value);
940   }
941   return TRUE;
942 }
943 
944 static void
update_max_ptime(GstRTPBasePayload * rtpbasepayload)945 update_max_ptime (GstRTPBasePayload * rtpbasepayload)
946 {
947   if (rtpbasepayload->priv->caps_max_ptime != -1 &&
948       rtpbasepayload->priv->prop_max_ptime != -1)
949     rtpbasepayload->max_ptime = MIN (rtpbasepayload->priv->caps_max_ptime,
950         rtpbasepayload->priv->prop_max_ptime);
951   else if (rtpbasepayload->priv->caps_max_ptime != -1)
952     rtpbasepayload->max_ptime = rtpbasepayload->priv->caps_max_ptime;
953   else if (rtpbasepayload->priv->prop_max_ptime != -1)
954     rtpbasepayload->max_ptime = rtpbasepayload->priv->prop_max_ptime;
955   else
956     rtpbasepayload->max_ptime = DEFAULT_MAX_PTIME;
957 }
958 
959 static gboolean
_set_caps(GQuark field_id,const GValue * value,GstCaps * caps)960 _set_caps (GQuark field_id, const GValue * value, GstCaps * caps)
961 {
962   gst_caps_set_value (caps, g_quark_to_string (field_id), value);
963 
964   return TRUE;
965 }
966 
967 /**
968  * gst_rtp_base_payload_set_outcaps_structure:
969  * @payload: a #GstRTPBasePayload
970  * @s: (nullable): a #GstStructure with the caps fields
971  *
972  * Configure the output caps with the optional fields.
973  *
974  * Returns: %TRUE if the caps could be set.
975  *
976  * Since: 1.20
977  */
978 gboolean
gst_rtp_base_payload_set_outcaps_structure(GstRTPBasePayload * payload,GstStructure * s)979 gst_rtp_base_payload_set_outcaps_structure (GstRTPBasePayload * payload,
980     GstStructure * s)
981 {
982   GstCaps *srccaps;
983 
984   /* fill in the defaults, their properties cannot be negotiated. */
985   srccaps = gst_caps_new_simple ("application/x-rtp",
986       "media", G_TYPE_STRING, payload->media,
987       "clock-rate", G_TYPE_INT, payload->clock_rate,
988       "encoding-name", G_TYPE_STRING, payload->encoding_name, NULL);
989 
990   GST_DEBUG_OBJECT (payload, "defaults: %" GST_PTR_FORMAT, srccaps);
991 
992   if (s && gst_structure_n_fields (s) > 0) {
993     gst_structure_foreach (s, (GstStructureForeachFunc) _set_caps, srccaps);
994 
995     GST_DEBUG_OBJECT (payload, "custom added: %" GST_PTR_FORMAT, srccaps);
996   }
997 
998   gst_caps_replace (&payload->priv->subclass_srccaps, srccaps);
999   gst_caps_unref (srccaps);
1000 
1001   return gst_rtp_base_payload_negotiate (payload);
1002 }
1003 
1004 /**
1005  * gst_rtp_base_payload_set_outcaps:
1006  * @payload: a #GstRTPBasePayload
1007  * @fieldname: the first field name or %NULL
1008  * @...: field values
1009  *
1010  * Configure the output caps with the optional parameters.
1011  *
1012  * Variable arguments should be in the form field name, field type
1013  * (as a GType), value(s).  The last variable argument should be NULL.
1014  *
1015  * Returns: %TRUE if the caps could be set.
1016  */
1017 gboolean
gst_rtp_base_payload_set_outcaps(GstRTPBasePayload * payload,const gchar * fieldname,...)1018 gst_rtp_base_payload_set_outcaps (GstRTPBasePayload * payload,
1019     const gchar * fieldname, ...)
1020 {
1021   gboolean result;
1022   GstStructure *s = NULL;
1023 
1024   if (fieldname) {
1025     va_list varargs;
1026 
1027     s = gst_structure_new_empty ("unused");
1028 
1029     /* override with custom properties */
1030     va_start (varargs, fieldname);
1031     gst_structure_set_valist (s, fieldname, varargs);
1032     va_end (varargs);
1033   }
1034 
1035   result = gst_rtp_base_payload_set_outcaps_structure (payload, s);
1036 
1037   gst_clear_structure (&s);
1038 
1039   return result;
1040 }
1041 
1042 static void
add_and_ref_item(GstRTPHeaderExtension * ext,GPtrArray * ret)1043 add_and_ref_item (GstRTPHeaderExtension * ext, GPtrArray * ret)
1044 {
1045   g_ptr_array_add (ret, gst_object_ref (ext));
1046 }
1047 
1048 static void
remove_item_from(GstRTPHeaderExtension * ext,GPtrArray * ret)1049 remove_item_from (GstRTPHeaderExtension * ext, GPtrArray * ret)
1050 {
1051   g_ptr_array_remove_fast (ret, ext);
1052 }
1053 
1054 static void
add_item_to(GstRTPHeaderExtension * ext,GPtrArray * ret)1055 add_item_to (GstRTPHeaderExtension * ext, GPtrArray * ret)
1056 {
1057   g_ptr_array_add (ret, ext);
1058 }
1059 
1060 static void
add_header_ext_to_caps(GstRTPHeaderExtension * ext,GstCaps * caps)1061 add_header_ext_to_caps (GstRTPHeaderExtension * ext, GstCaps * caps)
1062 {
1063   if (!gst_rtp_header_extension_set_caps_from_attributes (ext, caps)) {
1064     GST_WARNING ("Failed to set caps from rtp header extension");
1065   }
1066 }
1067 
1068 static gboolean
gst_rtp_base_payload_negotiate(GstRTPBasePayload * payload)1069 gst_rtp_base_payload_negotiate (GstRTPBasePayload * payload)
1070 {
1071   GstCaps *templ, *peercaps, *srccaps;
1072   GstStructure *s, *d;
1073   gboolean res = TRUE;
1074 
1075   payload->priv->caps_max_ptime = DEFAULT_MAX_PTIME;
1076   payload->ptime = 0;
1077 
1078   gst_pad_check_reconfigure (payload->srcpad);
1079 
1080   templ = gst_pad_get_pad_template_caps (payload->srcpad);
1081 
1082   if (payload->priv->subclass_srccaps) {
1083     GstCaps *tmp = gst_caps_intersect (payload->priv->subclass_srccaps,
1084         templ);
1085     gst_caps_unref (templ);
1086     templ = tmp;
1087   }
1088 
1089   peercaps = gst_pad_peer_query_caps (payload->srcpad, templ);
1090 
1091   if (peercaps == NULL) {
1092     /* no peer caps, just add the other properties */
1093 
1094     srccaps = gst_caps_copy (templ);
1095     gst_caps_set_simple (srccaps,
1096         "payload", G_TYPE_INT, GST_RTP_BASE_PAYLOAD_PT (payload),
1097         "ssrc", G_TYPE_UINT, payload->current_ssrc,
1098         "timestamp-offset", G_TYPE_UINT, payload->ts_base,
1099         "seqnum-offset", G_TYPE_UINT, payload->seqnum_base, NULL);
1100 
1101     GST_DEBUG_OBJECT (payload, "no peer caps: %" GST_PTR_FORMAT, srccaps);
1102   } else {
1103     GstCaps *temp;
1104     const GValue *value;
1105     gboolean have_pt = FALSE;
1106     gboolean have_ts_offset = FALSE;
1107     gboolean have_seqnum_offset = FALSE;
1108     guint max_ptime, ptime;
1109 
1110     /* peer provides caps we can use to fixate. They are already intersected
1111      * with our srccaps, just make them writable */
1112     temp = gst_caps_make_writable (peercaps);
1113     peercaps = NULL;
1114 
1115     if (gst_caps_is_empty (temp)) {
1116       gst_caps_unref (temp);
1117       gst_caps_unref (templ);
1118       res = FALSE;
1119       goto out;
1120     }
1121 
1122     /* We prefer the pt, timestamp-offset, seqnum-offset from the
1123      * property (if set), or any previously configured value over what
1124      * downstream prefers. Only if downstream can't accept that, or the
1125      * properties were not set, we fall back to choosing downstream's
1126      * preferred value
1127      *
1128      * For ssrc we prefer any value downstream suggests, otherwise
1129      * the property value or as a last resort a random value.
1130      * This difference for ssrc is implemented for retaining backwards
1131      * compatibility with changing rtpsession's internal-ssrc property.
1132      *
1133      * FIXME 2.0: All these properties should go away and be negotiated
1134      * via caps only!
1135      */
1136 
1137     /* try to use the previously set pt, or the one from the property */
1138     if (payload->priv->pt_set || gst_pad_has_current_caps (payload->srcpad)) {
1139       GstCaps *probe_caps = gst_caps_copy (templ);
1140       GstCaps *intersection;
1141 
1142       gst_caps_set_simple (probe_caps, "payload", G_TYPE_INT,
1143           GST_RTP_BASE_PAYLOAD_PT (payload), NULL);
1144       intersection = gst_caps_intersect (probe_caps, temp);
1145 
1146       if (!gst_caps_is_empty (intersection)) {
1147         GST_LOG_OBJECT (payload, "Using selected pt %d",
1148             GST_RTP_BASE_PAYLOAD_PT (payload));
1149         have_pt = TRUE;
1150         gst_caps_unref (temp);
1151         temp = intersection;
1152       } else {
1153         GST_WARNING_OBJECT (payload, "Can't use selected pt %d",
1154             GST_RTP_BASE_PAYLOAD_PT (payload));
1155         gst_caps_unref (intersection);
1156       }
1157       gst_caps_unref (probe_caps);
1158     }
1159 
1160     /* If we got no pt above, select one now */
1161     if (!have_pt) {
1162       gint pt;
1163 
1164       /* get first structure */
1165       s = gst_caps_get_structure (temp, 0);
1166 
1167       if (gst_structure_get_int (s, "payload", &pt)) {
1168         /* use peer pt */
1169         GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
1170         GST_LOG_OBJECT (payload, "using peer pt %d", pt);
1171       } else {
1172         if (gst_structure_has_field (s, "payload")) {
1173           /* can only fixate if there is a field */
1174           gst_structure_fixate_field_nearest_int (s, "payload",
1175               GST_RTP_BASE_PAYLOAD_PT (payload));
1176           gst_structure_get_int (s, "payload", &pt);
1177           GST_RTP_BASE_PAYLOAD_PT (payload) = pt;
1178           GST_LOG_OBJECT (payload, "using peer pt %d", pt);
1179         } else {
1180           /* no pt field, use the internal pt */
1181           pt = GST_RTP_BASE_PAYLOAD_PT (payload);
1182           gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
1183           GST_LOG_OBJECT (payload, "using internal pt %d", pt);
1184         }
1185       }
1186       s = NULL;
1187     }
1188 
1189     /* If we got no ssrc above, select one now */
1190     /* get first structure */
1191     s = gst_caps_get_structure (temp, 0);
1192 
1193     if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT)) {
1194       value = gst_structure_get_value (s, "ssrc");
1195       payload->current_ssrc = g_value_get_uint (value);
1196       GST_LOG_OBJECT (payload, "using peer ssrc %08x", payload->current_ssrc);
1197     } else {
1198       /* FIXME, fixate_nearest_uint would be even better but we
1199        * don't support uint ranges so how likely is it that anybody
1200        * uses a list of possible ssrcs */
1201       gst_structure_set (s, "ssrc", G_TYPE_UINT, payload->current_ssrc, NULL);
1202       GST_LOG_OBJECT (payload, "using internal ssrc %08x",
1203           payload->current_ssrc);
1204     }
1205     s = NULL;
1206 
1207     /* try to select the previously used timestamp-offset, or the one from the property */
1208     if (!payload->priv->ts_offset_random
1209         || gst_pad_has_current_caps (payload->srcpad)) {
1210       GstCaps *probe_caps = gst_caps_copy (templ);
1211       GstCaps *intersection;
1212 
1213       gst_caps_set_simple (probe_caps, "timestamp-offset", G_TYPE_UINT,
1214           payload->ts_base, NULL);
1215       intersection = gst_caps_intersect (probe_caps, temp);
1216 
1217       if (!gst_caps_is_empty (intersection)) {
1218         GST_LOG_OBJECT (payload, "Using selected timestamp-offset %u",
1219             payload->ts_base);
1220         gst_caps_unref (temp);
1221         temp = intersection;
1222         have_ts_offset = TRUE;
1223       } else {
1224         GST_WARNING_OBJECT (payload, "Can't use selected timestamp-offset %u",
1225             payload->ts_base);
1226         gst_caps_unref (intersection);
1227       }
1228       gst_caps_unref (probe_caps);
1229     }
1230 
1231     /* If we got no timestamp-offset above, select one now */
1232     if (!have_ts_offset) {
1233       /* get first structure */
1234       s = gst_caps_get_structure (temp, 0);
1235 
1236       if (gst_structure_has_field_typed (s, "timestamp-offset", G_TYPE_UINT)) {
1237         value = gst_structure_get_value (s, "timestamp-offset");
1238         payload->ts_base = g_value_get_uint (value);
1239         GST_LOG_OBJECT (payload, "using peer timestamp-offset %u",
1240             payload->ts_base);
1241       } else {
1242         /* FIXME, fixate_nearest_uint would be even better but we
1243          * don't support uint ranges so how likely is it that anybody
1244          * uses a list of possible timestamp-offsets */
1245         gst_structure_set (s, "timestamp-offset", G_TYPE_UINT, payload->ts_base,
1246             NULL);
1247         GST_LOG_OBJECT (payload, "using internal timestamp-offset %u",
1248             payload->ts_base);
1249       }
1250       s = NULL;
1251     }
1252 
1253     /* try to select the previously used seqnum-offset, or the one from the property */
1254     if (!payload->priv->seqnum_offset_random
1255         || gst_pad_has_current_caps (payload->srcpad)) {
1256       GstCaps *probe_caps = gst_caps_copy (templ);
1257       GstCaps *intersection;
1258 
1259       gst_caps_set_simple (probe_caps, "seqnum-offset", G_TYPE_UINT,
1260           payload->seqnum_base, NULL);
1261       intersection = gst_caps_intersect (probe_caps, temp);
1262 
1263       if (!gst_caps_is_empty (intersection)) {
1264         GST_LOG_OBJECT (payload, "Using selected seqnum-offset %u",
1265             payload->seqnum_base);
1266         gst_caps_unref (temp);
1267         temp = intersection;
1268         have_seqnum_offset = TRUE;
1269       } else {
1270         GST_WARNING_OBJECT (payload, "Can't use selected seqnum-offset %u",
1271             payload->seqnum_base);
1272         gst_caps_unref (intersection);
1273       }
1274       gst_caps_unref (probe_caps);
1275     }
1276 
1277     /* If we got no seqnum-offset above, select one now */
1278     if (!have_seqnum_offset) {
1279       /* get first structure */
1280       s = gst_caps_get_structure (temp, 0);
1281 
1282       if (gst_structure_has_field_typed (s, "seqnum-offset", G_TYPE_UINT)) {
1283         value = gst_structure_get_value (s, "seqnum-offset");
1284         payload->seqnum_base = g_value_get_uint (value);
1285         GST_LOG_OBJECT (payload, "using peer seqnum-offset %u",
1286             payload->seqnum_base);
1287         payload->priv->next_seqnum = payload->seqnum_base;
1288         payload->seqnum = payload->seqnum_base;
1289         payload->priv->seqnum_offset_random = FALSE;
1290       } else {
1291         /* FIXME, fixate_nearest_uint would be even better but we
1292          * don't support uint ranges so how likely is it that anybody
1293          * uses a list of possible seqnum-offsets */
1294         gst_structure_set (s, "seqnum-offset", G_TYPE_UINT,
1295             payload->seqnum_base, NULL);
1296         GST_LOG_OBJECT (payload, "using internal seqnum-offset %u",
1297             payload->seqnum_base);
1298       }
1299 
1300       s = NULL;
1301     }
1302 
1303     /* now fixate, start by taking the first caps */
1304     temp = gst_caps_truncate (temp);
1305 
1306     /* get first structure */
1307     s = gst_caps_get_structure (temp, 0);
1308 
1309     if (gst_structure_get_uint (s, "maxptime", &max_ptime))
1310       payload->priv->caps_max_ptime = max_ptime * GST_MSECOND;
1311 
1312     if (gst_structure_get_uint (s, "ptime", &ptime))
1313       payload->ptime = ptime * GST_MSECOND;
1314 
1315     /* make the target caps by copying over all the fixed fields, removing the
1316      * unfixed fields. */
1317     srccaps = gst_caps_new_empty_simple (gst_structure_get_name (s));
1318     d = gst_caps_get_structure (srccaps, 0);
1319 
1320     gst_structure_foreach (s, (GstStructureForeachFunc) copy_fixed, d);
1321 
1322     gst_caps_unref (temp);
1323 
1324     GST_DEBUG_OBJECT (payload, "with peer caps: %" GST_PTR_FORMAT, srccaps);
1325   }
1326 
1327   if (payload->priv->sinkcaps != NULL) {
1328     s = gst_caps_get_structure (payload->priv->sinkcaps, 0);
1329     if (g_str_has_prefix (gst_structure_get_name (s), "video")) {
1330       gboolean has_framerate;
1331       gint num, denom;
1332 
1333       GST_DEBUG_OBJECT (payload, "video caps: %" GST_PTR_FORMAT,
1334           payload->priv->sinkcaps);
1335 
1336       has_framerate = gst_structure_get_fraction (s, "framerate", &num, &denom);
1337       if (has_framerate && num == 0 && denom == 1) {
1338         has_framerate =
1339             gst_structure_get_fraction (s, "max-framerate", &num, &denom);
1340       }
1341 
1342       if (has_framerate) {
1343         gchar str[G_ASCII_DTOSTR_BUF_SIZE];
1344         gdouble framerate;
1345 
1346         gst_util_fraction_to_double (num, denom, &framerate);
1347         g_ascii_dtostr (str, G_ASCII_DTOSTR_BUF_SIZE, framerate);
1348         d = gst_caps_get_structure (srccaps, 0);
1349         gst_structure_set (d, "a-framerate", G_TYPE_STRING, str, NULL);
1350       }
1351 
1352       GST_DEBUG_OBJECT (payload, "with video caps: %" GST_PTR_FORMAT, srccaps);
1353     }
1354   }
1355 
1356   update_max_ptime (payload);
1357 
1358   {
1359     /* try to find header extension implementations for the list in the
1360      * caps */
1361     GstStructure *s = gst_caps_get_structure (srccaps, 0);
1362     guint i, j, n_fields = gst_structure_n_fields (s);
1363     GPtrArray *header_exts = g_ptr_array_new_with_free_func (gst_object_unref);
1364     GPtrArray *to_add = g_ptr_array_new ();
1365     GPtrArray *to_remove = g_ptr_array_new ();
1366 
1367     GST_OBJECT_LOCK (payload);
1368     g_ptr_array_foreach (payload->priv->header_exts,
1369         (GFunc) add_and_ref_item, header_exts);
1370     GST_OBJECT_UNLOCK (payload);
1371 
1372     for (i = 0; i < n_fields; i++) {
1373       const gchar *field_name = gst_structure_nth_field_name (s, i);
1374       if (g_str_has_prefix (field_name, "extmap-")) {
1375         const GValue *val;
1376         const gchar *uri = NULL;
1377         gchar *nptr;
1378         guint ext_id;
1379         GstRTPHeaderExtension *ext = NULL;
1380 
1381         errno = 0;
1382         ext_id = g_ascii_strtoull (&field_name[strlen ("extmap-")], &nptr, 10);
1383         if (errno != 0 || (ext_id == 0 && field_name == nptr)) {
1384           GST_WARNING_OBJECT (payload, "could not parse id from %s",
1385               field_name);
1386           res = FALSE;
1387           goto ext_out;
1388         }
1389 
1390         val = gst_structure_get_value (s, field_name);
1391         if (G_VALUE_HOLDS_STRING (val)) {
1392           uri = g_value_get_string (val);
1393         } else if (GST_VALUE_HOLDS_ARRAY (val)) {
1394           /* the uri is the second value in the array */
1395           const GValue *str = gst_value_array_get_value (val, 1);
1396           if (G_VALUE_HOLDS_STRING (str)) {
1397             uri = g_value_get_string (str);
1398           }
1399         }
1400 
1401         if (!uri) {
1402           GST_WARNING_OBJECT (payload, "could not get extmap uri for "
1403               "field %s", field_name);
1404           res = FALSE;
1405           goto ext_out;
1406         }
1407 
1408         /* try to find if this extension mapping already exists */
1409         for (j = 0; j < header_exts->len; j++) {
1410           ext = g_ptr_array_index (header_exts, j);
1411           if (gst_rtp_header_extension_get_id (ext) == ext_id) {
1412             if (g_strcmp0 (uri, gst_rtp_header_extension_get_uri (ext)) == 0) {
1413               /* still matching, we're good, set attributes from caps in case
1414                * the caps have been updated */
1415               if (!gst_rtp_header_extension_set_attributes_from_caps (ext,
1416                       srccaps)) {
1417                 GST_WARNING_OBJECT (payload,
1418                     "Failed to configure rtp header " "extension %"
1419                     GST_PTR_FORMAT " attributes from caps %" GST_PTR_FORMAT,
1420                     ext, srccaps);
1421                 res = FALSE;
1422                 goto ext_out;
1423               }
1424               break;
1425             } else {
1426               GST_DEBUG_OBJECT (payload, "extension id %u"
1427                   "was replaced with a different extension uri "
1428                   "original:\'%s' vs \'%s\'", ext_id,
1429                   gst_rtp_header_extension_get_uri (ext), uri);
1430               g_ptr_array_add (to_remove, ext);
1431               ext = NULL;
1432               break;
1433             }
1434           } else {
1435             ext = NULL;
1436           }
1437         }
1438 
1439         /* if no extension, attempt to request one */
1440         if (!ext) {
1441           GST_DEBUG_OBJECT (payload, "requesting extension for id %u"
1442               " and uri %s", ext_id, uri);
1443           g_signal_emit (payload,
1444               gst_rtp_base_payload_signals[SIGNAL_REQUEST_EXTENSION], 0,
1445               ext_id, uri, &ext);
1446           GST_DEBUG_OBJECT (payload, "request returned extension %p \'%s\' "
1447               "for id %u and uri %s", ext,
1448               ext ? GST_OBJECT_NAME (ext) : "", ext_id, uri);
1449 
1450           /* We require caller to set the appropriate extension if it's required */
1451           if (ext && gst_rtp_header_extension_get_id (ext) != ext_id) {
1452             g_warning ("\'request-extension\' signal provided an rtp header "
1453                 "extension for uri \'%s\' that does not match the requested "
1454                 "extension id %u", uri, ext_id);
1455             gst_clear_object (&ext);
1456           }
1457 
1458           if (ext && !gst_rtp_header_extension_set_attributes_from_caps (ext,
1459                   srccaps)) {
1460             GST_WARNING_OBJECT (payload,
1461                 "Failed to configure rtp header " "extension %"
1462                 GST_PTR_FORMAT " attributes from caps %" GST_PTR_FORMAT,
1463                 ext, srccaps);
1464             res = FALSE;
1465             g_clear_object (&ext);
1466             goto ext_out;
1467           }
1468 
1469           if (ext) {
1470             g_ptr_array_add (to_add, ext);
1471           }
1472         }
1473       }
1474     }
1475 
1476     GST_OBJECT_LOCK (payload);
1477     g_ptr_array_foreach (to_remove, (GFunc) remove_item_from,
1478         payload->priv->header_exts);
1479     g_ptr_array_foreach (to_add, (GFunc) add_item_to,
1480         payload->priv->header_exts);
1481     /* let extensions update their internal state from sinkcaps */
1482     if (payload->priv->sinkcaps) {
1483       gint i;
1484 
1485       for (i = 0; i < payload->priv->header_exts->len; i++) {
1486         GstRTPHeaderExtension *ext;
1487 
1488         ext = g_ptr_array_index (payload->priv->header_exts, i);
1489         if (!gst_rtp_header_extension_set_non_rtp_sink_caps (ext,
1490                 payload->priv->sinkcaps)) {
1491           GST_WARNING_OBJECT (payload,
1492               "Failed to update rtp header extension (%s) from sink caps",
1493               GST_OBJECT_NAME (ext));
1494           res = FALSE;
1495           GST_OBJECT_UNLOCK (payload);
1496           goto ext_out;
1497         }
1498       }
1499     }
1500     /* add extension information to srccaps */
1501     g_ptr_array_foreach (payload->priv->header_exts,
1502         (GFunc) add_header_ext_to_caps, srccaps);
1503     GST_OBJECT_UNLOCK (payload);
1504 
1505   ext_out:
1506     g_ptr_array_unref (to_add);
1507     g_ptr_array_unref (to_remove);
1508     g_ptr_array_unref (header_exts);
1509   }
1510 
1511   GST_DEBUG_OBJECT (payload, "configuring caps %" GST_PTR_FORMAT, srccaps);
1512 
1513   if (res)
1514     res = gst_pad_set_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), srccaps);
1515   gst_caps_unref (srccaps);
1516   gst_caps_unref (templ);
1517 
1518 out:
1519   payload->priv->negotiate_called = TRUE;
1520 
1521   if (!res)
1522     gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
1523 
1524   return res;
1525 }
1526 
1527 /**
1528  * gst_rtp_base_payload_is_filled:
1529  * @payload: a #GstRTPBasePayload
1530  * @size: the size of the packet
1531  * @duration: the duration of the packet
1532  *
1533  * Check if the packet with @size and @duration would exceed the configured
1534  * maximum size.
1535  *
1536  * Returns: %TRUE if the packet of @size and @duration would exceed the
1537  * configured MTU or max_ptime.
1538  */
1539 gboolean
gst_rtp_base_payload_is_filled(GstRTPBasePayload * payload,guint size,GstClockTime duration)1540 gst_rtp_base_payload_is_filled (GstRTPBasePayload * payload,
1541     guint size, GstClockTime duration)
1542 {
1543   if (size > payload->mtu)
1544     return TRUE;
1545 
1546   if (payload->max_ptime != -1 && duration >= payload->max_ptime)
1547     return TRUE;
1548 
1549   return FALSE;
1550 }
1551 
1552 typedef struct
1553 {
1554   GstRTPBasePayload *payload;
1555   guint32 ssrc;
1556   guint16 seqnum;
1557   guint8 pt;
1558   GstClockTime dts;
1559   GstClockTime pts;
1560   guint64 offset;
1561   guint32 rtptime;
1562 } HeaderData;
1563 
1564 static gboolean
find_timestamp(GstBuffer ** buffer,guint idx,gpointer user_data)1565 find_timestamp (GstBuffer ** buffer, guint idx, gpointer user_data)
1566 {
1567   HeaderData *data = user_data;
1568   data->dts = GST_BUFFER_DTS (*buffer);
1569   data->pts = GST_BUFFER_PTS (*buffer);
1570   data->offset = GST_BUFFER_OFFSET (*buffer);
1571 
1572   /* stop when we find a timestamp. We take whatever offset is associated with
1573    * the timestamp (if any) to do perfect timestamps when we need to. */
1574   if (data->pts != -1)
1575     return FALSE;
1576   else
1577     return TRUE;
1578 }
1579 
1580 static void
gst_rtp_base_payload_add_extension(GstRTPBasePayload * payload,GstRTPHeaderExtension * ext)1581 gst_rtp_base_payload_add_extension (GstRTPBasePayload * payload,
1582     GstRTPHeaderExtension * ext)
1583 {
1584   g_return_if_fail (GST_IS_RTP_HEADER_EXTENSION (ext));
1585   g_return_if_fail (gst_rtp_header_extension_get_id (ext) > 0);
1586 
1587   /* XXX: check for duplicate ids? */
1588   GST_OBJECT_LOCK (payload);
1589   g_ptr_array_add (payload->priv->header_exts, gst_object_ref (ext));
1590   gst_pad_mark_reconfigure (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
1591   GST_OBJECT_UNLOCK (payload);
1592 }
1593 
1594 static void
gst_rtp_base_payload_clear_extensions(GstRTPBasePayload * payload)1595 gst_rtp_base_payload_clear_extensions (GstRTPBasePayload * payload)
1596 {
1597   GST_OBJECT_LOCK (payload);
1598   g_ptr_array_set_size (payload->priv->header_exts, 0);
1599   GST_OBJECT_UNLOCK (payload);
1600 }
1601 
1602 typedef struct
1603 {
1604   GstRTPBasePayload *payload;
1605   GstRTPHeaderExtensionFlags flags;
1606   GstBuffer *output;
1607   guint8 *data;
1608   gsize allocated_size;
1609   gsize written_size;
1610   gsize hdr_unit_size;
1611   gboolean abort;
1612 } HeaderExt;
1613 
1614 static void
determine_header_extension_flags_size(GstRTPHeaderExtension * ext,gpointer user_data)1615 determine_header_extension_flags_size (GstRTPHeaderExtension * ext,
1616     gpointer user_data)
1617 {
1618   HeaderExt *hdr = user_data;
1619   guint ext_id;
1620   gsize max_size;
1621 
1622   hdr->flags &= gst_rtp_header_extension_get_supported_flags (ext);
1623   max_size =
1624       gst_rtp_header_extension_get_max_size (ext,
1625       hdr->payload->priv->input_meta_buffer);
1626 
1627   if (max_size > RTP_HEADER_EXT_ONE_BYTE_MAX_SIZE)
1628     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_ONE_BYTE;
1629   if (max_size > RTP_HEADER_EXT_TWO_BYTE_MAX_SIZE)
1630     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1631 
1632   ext_id = gst_rtp_header_extension_get_id (ext);
1633   if (ext_id > RTP_HEADER_EXT_ONE_BYTE_MAX_ID)
1634     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_ONE_BYTE;
1635   if (ext_id > RTP_HEADER_EXT_TWO_BYTE_MAX_ID)
1636     hdr->flags &= ~GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1637 
1638   hdr->allocated_size += max_size;
1639 }
1640 
1641 static void
write_header_extension(GstRTPHeaderExtension * ext,gpointer user_data)1642 write_header_extension (GstRTPHeaderExtension * ext, gpointer user_data)
1643 {
1644   HeaderExt *hdr = user_data;
1645   gsize remaining =
1646       hdr->allocated_size - hdr->written_size - hdr->hdr_unit_size;
1647   gsize offset = hdr->written_size + hdr->hdr_unit_size;
1648   gssize written;
1649   guint ext_id;
1650 
1651   if (hdr->abort)
1652     return;
1653 
1654   written = gst_rtp_header_extension_write (ext,
1655       hdr->payload->priv->input_meta_buffer, hdr->flags, hdr->output,
1656       &hdr->data[offset], remaining);
1657 
1658   GST_TRACE_OBJECT (hdr->payload, "extension %" GST_PTR_FORMAT " wrote %"
1659       G_GSIZE_FORMAT, ext, written);
1660 
1661   if (written == 0) {
1662     /* extension wrote no data */
1663     return;
1664   } else if (written < 0) {
1665     GST_WARNING_OBJECT (hdr->payload, "%s failed to write extension data",
1666         GST_OBJECT_NAME (ext));
1667     goto error;
1668   } else if (written > remaining) {
1669     /* wrote too much! */
1670     g_error ("Overflow detected writing rtp header extensions. One of the "
1671         "instances likely did not report a large enough maximum size. "
1672         "Memory corruption has occured. Aborting");
1673     goto error;
1674   }
1675 
1676   ext_id = gst_rtp_header_extension_get_id (ext);
1677 
1678   /* move to the beginning of the extension header */
1679   offset -= hdr->hdr_unit_size;
1680 
1681   /* write extension header */
1682   if (hdr->flags & GST_RTP_HEADER_EXTENSION_ONE_BYTE) {
1683     if (written > RTP_HEADER_EXT_ONE_BYTE_MAX_SIZE) {
1684       g_critical ("Amount of data written by %s is larger than allowed with "
1685           "a one byte header.", GST_OBJECT_NAME (ext));
1686       goto error;
1687     }
1688 
1689     hdr->data[offset] = ((ext_id & 0x0F) << 4) | ((written - 1) & 0x0F);
1690   } else if (hdr->flags & GST_RTP_HEADER_EXTENSION_TWO_BYTE) {
1691     if (written > RTP_HEADER_EXT_TWO_BYTE_MAX_SIZE) {
1692       g_critical ("Amount of data written by %s is larger than allowed with "
1693           "a two byte header.", GST_OBJECT_NAME (ext));
1694       goto error;
1695     }
1696 
1697     hdr->data[offset] = ext_id & 0xFF;
1698     hdr->data[offset + 1] = written & 0xFF;
1699   } else {
1700     g_critical ("Don't know how to write extension data with flags 0x%x!",
1701         hdr->flags);
1702     goto error;
1703   }
1704 
1705   hdr->written_size += written + hdr->hdr_unit_size;
1706 
1707   return;
1708 
1709 error:
1710   hdr->abort = TRUE;
1711   return;
1712 }
1713 
1714 static gboolean
set_headers(GstBuffer ** buffer,guint idx,gpointer user_data)1715 set_headers (GstBuffer ** buffer, guint idx, gpointer user_data)
1716 {
1717   HeaderData *data = user_data;
1718   HeaderExt hdrext = { NULL, };
1719   GstRTPBuffer rtp = { NULL, };
1720 
1721   if (!gst_rtp_buffer_map (*buffer, GST_MAP_READWRITE, &rtp))
1722     goto map_failed;
1723 
1724   gst_rtp_buffer_set_ssrc (&rtp, data->ssrc);
1725   gst_rtp_buffer_set_payload_type (&rtp, data->pt);
1726   gst_rtp_buffer_set_seq (&rtp, data->seqnum);
1727   gst_rtp_buffer_set_timestamp (&rtp, data->rtptime);
1728 
1729   GST_OBJECT_LOCK (data->payload);
1730   if (data->payload->priv->header_exts->len > 0
1731       && data->payload->priv->input_meta_buffer) {
1732     guint wordlen;
1733     gsize extlen;
1734     guint16 bit_pattern;
1735 
1736     /* write header extensions */
1737     hdrext.payload = data->payload;
1738     hdrext.output = *buffer;
1739     /* XXX: pre-calculate these flags and sizes? */
1740     hdrext.flags =
1741         GST_RTP_HEADER_EXTENSION_ONE_BYTE | GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1742     g_ptr_array_foreach (data->payload->priv->header_exts,
1743         (GFunc) determine_header_extension_flags_size, &hdrext);
1744     hdrext.hdr_unit_size = 0;
1745     if (hdrext.flags & GST_RTP_HEADER_EXTENSION_ONE_BYTE) {
1746       /* prefer the one byte header */
1747       hdrext.hdr_unit_size = 1;
1748       /* TODO: support mixed size writing modes, i.e. RFC8285 */
1749       hdrext.flags &= ~GST_RTP_HEADER_EXTENSION_TWO_BYTE;
1750       bit_pattern = 0xBEDE;
1751     } else if (hdrext.flags & GST_RTP_HEADER_EXTENSION_TWO_BYTE) {
1752       hdrext.hdr_unit_size = 2;
1753       bit_pattern = 0x1000;
1754     } else {
1755       goto unsupported_flags;
1756     }
1757 
1758     extlen =
1759         hdrext.hdr_unit_size * data->payload->priv->header_exts->len +
1760         hdrext.allocated_size;
1761     wordlen = extlen / 4 + ((extlen % 4) ? 1 : 0);
1762 
1763     /* XXX: do we need to add to any existing extension data instead of
1764      * overwriting everything? */
1765     gst_rtp_buffer_set_extension_data (&rtp, bit_pattern, wordlen);
1766     gst_rtp_buffer_get_extension_data (&rtp, NULL, (gpointer) & hdrext.data,
1767         &wordlen);
1768 
1769     /* from 32-bit words to bytes */
1770     hdrext.allocated_size = wordlen * 4;
1771 
1772     g_ptr_array_foreach (data->payload->priv->header_exts,
1773         (GFunc) write_header_extension, &hdrext);
1774 
1775     if (hdrext.written_size > 0) {
1776       wordlen = hdrext.written_size / 4 + ((hdrext.written_size % 4) ? 1 : 0);
1777 
1778       /* zero-fill the hdrext padding bytes */
1779       memset (&hdrext.data[hdrext.written_size], 0,
1780           wordlen * 4 - hdrext.written_size);
1781 
1782       gst_rtp_buffer_set_extension_data (&rtp, bit_pattern, wordlen);
1783     } else {
1784       gst_rtp_buffer_remove_extension_data (&rtp);
1785     }
1786   }
1787   GST_OBJECT_UNLOCK (data->payload);
1788   gst_rtp_buffer_unmap (&rtp);
1789 
1790   /* increment the seqnum for each buffer */
1791   data->seqnum++;
1792 
1793   return TRUE;
1794   /* ERRORS */
1795 map_failed:
1796   {
1797     GST_ERROR ("failed to map buffer %p", *buffer);
1798     return FALSE;
1799   }
1800 
1801 unsupported_flags:
1802   {
1803     GST_OBJECT_UNLOCK (data->payload);
1804     gst_rtp_buffer_unmap (&rtp);
1805     GST_ERROR ("Cannot add rtp header extensions with mixed header types");
1806     return FALSE;
1807   }
1808 }
1809 
1810 static gboolean
foreach_metadata_drop(GstBuffer * buffer,GstMeta ** meta,gpointer user_data)1811 foreach_metadata_drop (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
1812 {
1813   GType drop_api_type = (GType) user_data;
1814   const GstMetaInfo *info = (*meta)->info;
1815 
1816   if (info->api == drop_api_type)
1817     *meta = NULL;
1818 
1819   return TRUE;
1820 }
1821 
1822 static gboolean
filter_meta(GstBuffer ** buffer,guint idx,gpointer user_data)1823 filter_meta (GstBuffer ** buffer, guint idx, gpointer user_data)
1824 {
1825   return gst_buffer_foreach_meta (*buffer, foreach_metadata_drop,
1826       (gpointer) GST_RTP_SOURCE_META_API_TYPE);
1827 }
1828 
1829 /* Updates the SSRC, payload type, seqnum and timestamp of the RTP buffer
1830  * before the buffer is pushed. */
1831 static GstFlowReturn
gst_rtp_base_payload_prepare_push(GstRTPBasePayload * payload,gpointer obj,gboolean is_list)1832 gst_rtp_base_payload_prepare_push (GstRTPBasePayload * payload,
1833     gpointer obj, gboolean is_list)
1834 {
1835   GstRTPBasePayloadPrivate *priv;
1836   HeaderData data;
1837 
1838   if (payload->clock_rate == 0)
1839     goto no_rate;
1840 
1841   priv = payload->priv;
1842 
1843   /* update first, so that the property is set to the last
1844    * seqnum pushed */
1845   payload->seqnum = priv->next_seqnum;
1846 
1847   /* fill in the fields we want to set on all headers */
1848   data.payload = payload;
1849   data.seqnum = payload->seqnum;
1850   data.ssrc = payload->current_ssrc;
1851   data.pt = payload->pt;
1852 
1853   /* find the first buffer with a timestamp */
1854   if (is_list) {
1855     data.dts = -1;
1856     data.pts = -1;
1857     data.offset = GST_BUFFER_OFFSET_NONE;
1858     gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), find_timestamp, &data);
1859   } else {
1860     data.dts = GST_BUFFER_DTS (GST_BUFFER_CAST (obj));
1861     data.pts = GST_BUFFER_PTS (GST_BUFFER_CAST (obj));
1862     data.offset = GST_BUFFER_OFFSET (GST_BUFFER_CAST (obj));
1863   }
1864 
1865   /* convert to RTP time */
1866   if (priv->perfect_rtptime && data.offset != GST_BUFFER_OFFSET_NONE &&
1867       priv->base_offset != GST_BUFFER_OFFSET_NONE) {
1868     /* generate perfect RTP time by adding together the base timestamp, the
1869      * running time of the first buffer and difference between the offset of the
1870      * first buffer and the offset of the current buffer. */
1871     guint64 offset = data.offset - priv->base_offset;
1872     data.rtptime = payload->ts_base + priv->base_rtime_hz + offset;
1873 
1874     GST_LOG_OBJECT (payload,
1875         "Using offset %" G_GUINT64_FORMAT " for RTP timestamp", data.offset);
1876 
1877     /* store buffer's running time */
1878     GST_LOG_OBJECT (payload,
1879         "setting running-time to %" G_GUINT64_FORMAT,
1880         data.offset - priv->base_offset);
1881     priv->running_time = priv->base_rtime + data.offset - priv->base_offset;
1882   } else if (GST_CLOCK_TIME_IS_VALID (data.pts)) {
1883     guint64 rtime_ns;
1884     guint64 rtime_hz;
1885 
1886     /* no offset, use the gstreamer pts */
1887     if (priv->onvif_no_rate_control || !priv->scale_rtptime)
1888       rtime_ns = gst_segment_to_stream_time (&payload->segment,
1889           GST_FORMAT_TIME, data.pts);
1890     else
1891       rtime_ns =
1892           gst_segment_to_running_time (&payload->segment, GST_FORMAT_TIME,
1893           data.pts);
1894 
1895     if (!GST_CLOCK_TIME_IS_VALID (rtime_ns)) {
1896       GST_LOG_OBJECT (payload, "Clipped pts, using base RTP timestamp");
1897       rtime_hz = 0;
1898     } else {
1899       GST_LOG_OBJECT (payload,
1900           "Using running_time %" GST_TIME_FORMAT " for RTP timestamp",
1901           GST_TIME_ARGS (rtime_ns));
1902       rtime_hz =
1903           gst_util_uint64_scale_int (rtime_ns, payload->clock_rate, GST_SECOND);
1904       priv->base_offset = data.offset;
1905       priv->base_rtime_hz = rtime_hz;
1906     }
1907 
1908     /* add running_time in clock-rate units to the base timestamp */
1909     data.rtptime = payload->ts_base + rtime_hz;
1910 
1911     /* store buffer's running time */
1912     if (priv->perfect_rtptime) {
1913       GST_LOG_OBJECT (payload,
1914           "setting running-time to %" G_GUINT64_FORMAT, rtime_hz);
1915       priv->running_time = rtime_hz;
1916     } else {
1917       GST_LOG_OBJECT (payload,
1918           "setting running-time to %" GST_TIME_FORMAT,
1919           GST_TIME_ARGS (rtime_ns));
1920       priv->running_time = rtime_ns;
1921     }
1922   } else {
1923     GST_LOG_OBJECT (payload,
1924         "Using previous RTP timestamp %" G_GUINT32_FORMAT, payload->timestamp);
1925     /* no timestamp to convert, take previous timestamp */
1926     data.rtptime = payload->timestamp;
1927   }
1928 
1929   /* set ssrc, payload type, seq number, caps and rtptime */
1930   /* remove unwanted meta */
1931   if (is_list) {
1932     gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), set_headers, &data);
1933     gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj), filter_meta, NULL);
1934     /* sequence number has increased more if this was a buffer list */
1935     payload->seqnum = data.seqnum - 1;
1936   } else {
1937     GstBuffer *buf = GST_BUFFER_CAST (obj);
1938     set_headers (&buf, 0, &data);
1939     filter_meta (&buf, 0, NULL);
1940   }
1941 
1942   priv->next_seqnum = data.seqnum;
1943   payload->timestamp = data.rtptime;
1944 
1945   GST_LOG_OBJECT (payload, "Preparing to push %s with size %"
1946       G_GSIZE_FORMAT ", seq=%d, rtptime=%u, pts %" GST_TIME_FORMAT,
1947       (is_list) ? "list" : "packet",
1948       (is_list) ? gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)) :
1949       gst_buffer_get_size (GST_BUFFER (obj)),
1950       payload->seqnum, data.rtptime, GST_TIME_ARGS (data.pts));
1951 
1952   if (g_atomic_int_compare_and_exchange (&payload->priv->
1953           notified_first_timestamp, 1, 0)) {
1954     g_object_notify (G_OBJECT (payload), "timestamp");
1955     g_object_notify (G_OBJECT (payload), "seqnum");
1956   }
1957 
1958   return GST_FLOW_OK;
1959 
1960   /* ERRORS */
1961 no_rate:
1962   {
1963     GST_ELEMENT_ERROR (payload, STREAM, NOT_IMPLEMENTED, (NULL),
1964         ("subclass did not specify clock-rate"));
1965     return GST_FLOW_ERROR;
1966   }
1967 }
1968 
1969 /**
1970  * gst_rtp_base_payload_push_list:
1971  * @payload: a #GstRTPBasePayload
1972  * @list: (transfer full): a #GstBufferList
1973  *
1974  * Push @list to the peer element of the payloader. The SSRC, payload type,
1975  * seqnum and timestamp of the RTP buffer will be updated first.
1976  *
1977  * This function takes ownership of @list.
1978  *
1979  * Returns: a #GstFlowReturn.
1980  */
1981 GstFlowReturn
gst_rtp_base_payload_push_list(GstRTPBasePayload * payload,GstBufferList * list)1982 gst_rtp_base_payload_push_list (GstRTPBasePayload * payload,
1983     GstBufferList * list)
1984 {
1985   GstFlowReturn res;
1986 
1987   res = gst_rtp_base_payload_prepare_push (payload, list, TRUE);
1988 
1989   if (G_LIKELY (res == GST_FLOW_OK)) {
1990     if (G_UNLIKELY (payload->priv->pending_segment)) {
1991       gst_pad_push_event (payload->srcpad, payload->priv->pending_segment);
1992       payload->priv->pending_segment = FALSE;
1993       payload->priv->delay_segment = FALSE;
1994     }
1995     res = gst_pad_push_list (payload->srcpad, list);
1996   } else {
1997     gst_buffer_list_unref (list);
1998   }
1999 
2000   return res;
2001 }
2002 
2003 /**
2004  * gst_rtp_base_payload_push:
2005  * @payload: a #GstRTPBasePayload
2006  * @buffer: (transfer full): a #GstBuffer
2007  *
2008  * Push @buffer to the peer element of the payloader. The SSRC, payload type,
2009  * seqnum and timestamp of the RTP buffer will be updated first.
2010  *
2011  * This function takes ownership of @buffer.
2012  *
2013  * Returns: a #GstFlowReturn.
2014  */
2015 GstFlowReturn
gst_rtp_base_payload_push(GstRTPBasePayload * payload,GstBuffer * buffer)2016 gst_rtp_base_payload_push (GstRTPBasePayload * payload, GstBuffer * buffer)
2017 {
2018   GstFlowReturn res;
2019 
2020   res = gst_rtp_base_payload_prepare_push (payload, buffer, FALSE);
2021 
2022   if (G_LIKELY (res == GST_FLOW_OK)) {
2023     if (G_UNLIKELY (payload->priv->pending_segment)) {
2024       gst_pad_push_event (payload->srcpad, payload->priv->pending_segment);
2025       payload->priv->pending_segment = FALSE;
2026       payload->priv->delay_segment = FALSE;
2027     }
2028     res = gst_pad_push (payload->srcpad, buffer);
2029   } else {
2030     gst_buffer_unref (buffer);
2031   }
2032 
2033   return res;
2034 }
2035 
2036 /**
2037  * gst_rtp_base_payload_allocate_output_buffer:
2038  * @payload: a #GstRTPBasePayload
2039  * @payload_len: the length of the payload
2040  * @pad_len: the amount of padding
2041  * @csrc_count: the minimum number of CSRC entries
2042  *
2043  * Allocate a new #GstBuffer with enough data to hold an RTP packet with
2044  * minimum @csrc_count CSRCs, a payload length of @payload_len and padding of
2045  * @pad_len. If @payload has #GstRTPBasePayload:source-info %TRUE additional
2046  * CSRCs may be allocated and filled with RTP source information.
2047  *
2048  * Returns: A newly allocated buffer that can hold an RTP packet with given
2049  * parameters.
2050  *
2051  * Since: 1.16
2052  */
2053 GstBuffer *
gst_rtp_base_payload_allocate_output_buffer(GstRTPBasePayload * payload,guint payload_len,guint8 pad_len,guint8 csrc_count)2054 gst_rtp_base_payload_allocate_output_buffer (GstRTPBasePayload * payload,
2055     guint payload_len, guint8 pad_len, guint8 csrc_count)
2056 {
2057   GstBuffer *buffer = NULL;
2058 
2059   if (payload->priv->input_meta_buffer != NULL) {
2060     GstRTPSourceMeta *meta =
2061         gst_buffer_get_rtp_source_meta (payload->priv->input_meta_buffer);
2062     if (meta != NULL) {
2063       guint total_csrc_count, idx, i;
2064       GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
2065 
2066       total_csrc_count = csrc_count + meta->csrc_count +
2067           (meta->ssrc_valid ? 1 : 0);
2068       total_csrc_count = MIN (total_csrc_count, 15);
2069       buffer = gst_rtp_buffer_new_allocate (payload_len, pad_len,
2070           total_csrc_count);
2071 
2072       gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtp);
2073 
2074       /* Skip CSRC fields requested by derived class and fill CSRCs from meta.
2075        * Finally append the SSRC as a new CSRC. */
2076       idx = csrc_count;
2077       for (i = 0; i < meta->csrc_count && idx < 15; i++, idx++)
2078         gst_rtp_buffer_set_csrc (&rtp, idx, meta->csrc[i]);
2079       if (meta->ssrc_valid && idx < 15)
2080         gst_rtp_buffer_set_csrc (&rtp, idx, meta->ssrc);
2081 
2082       gst_rtp_buffer_unmap (&rtp);
2083     }
2084   }
2085 
2086   if (buffer == NULL)
2087     buffer = gst_rtp_buffer_new_allocate (payload_len, pad_len, csrc_count);
2088 
2089   return buffer;
2090 }
2091 
2092 static GstStructure *
gst_rtp_base_payload_create_stats(GstRTPBasePayload * rtpbasepayload)2093 gst_rtp_base_payload_create_stats (GstRTPBasePayload * rtpbasepayload)
2094 {
2095   GstRTPBasePayloadPrivate *priv;
2096   GstStructure *s;
2097 
2098   priv = rtpbasepayload->priv;
2099 
2100   s = gst_structure_new ("application/x-rtp-payload-stats",
2101       "clock-rate", G_TYPE_UINT, (guint) rtpbasepayload->clock_rate,
2102       "running-time", G_TYPE_UINT64, priv->running_time,
2103       "seqnum", G_TYPE_UINT, (guint) rtpbasepayload->seqnum,
2104       "timestamp", G_TYPE_UINT, (guint) rtpbasepayload->timestamp,
2105       "ssrc", G_TYPE_UINT, rtpbasepayload->current_ssrc,
2106       "pt", G_TYPE_UINT, rtpbasepayload->pt,
2107       "seqnum-offset", G_TYPE_UINT, (guint) rtpbasepayload->seqnum_base,
2108       "timestamp-offset", G_TYPE_UINT, (guint) rtpbasepayload->ts_base, NULL);
2109 
2110   return s;
2111 }
2112 
2113 static void
gst_rtp_base_payload_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2114 gst_rtp_base_payload_set_property (GObject * object, guint prop_id,
2115     const GValue * value, GParamSpec * pspec)
2116 {
2117   GstRTPBasePayload *rtpbasepayload;
2118   GstRTPBasePayloadPrivate *priv;
2119   gint64 val;
2120 
2121   rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
2122   priv = rtpbasepayload->priv;
2123 
2124   switch (prop_id) {
2125     case PROP_MTU:
2126       rtpbasepayload->mtu = g_value_get_uint (value);
2127       break;
2128     case PROP_PT:
2129       rtpbasepayload->pt = g_value_get_uint (value);
2130       priv->pt_set = TRUE;
2131       break;
2132     case PROP_SSRC:
2133       val = g_value_get_uint (value);
2134       rtpbasepayload->ssrc = val;
2135       priv->ssrc_random = FALSE;
2136       break;
2137     case PROP_TIMESTAMP_OFFSET:
2138       val = g_value_get_uint (value);
2139       rtpbasepayload->ts_offset = val;
2140       priv->ts_offset_random = FALSE;
2141       break;
2142     case PROP_SEQNUM_OFFSET:
2143       val = g_value_get_int (value);
2144       rtpbasepayload->seqnum_offset = val;
2145       priv->seqnum_offset_random = (val == -1);
2146       GST_DEBUG_OBJECT (rtpbasepayload, "seqnum offset 0x%04x, random %d",
2147           rtpbasepayload->seqnum_offset, priv->seqnum_offset_random);
2148       break;
2149     case PROP_MAX_PTIME:
2150       rtpbasepayload->priv->prop_max_ptime = g_value_get_int64 (value);
2151       update_max_ptime (rtpbasepayload);
2152       break;
2153     case PROP_MIN_PTIME:
2154       rtpbasepayload->min_ptime = g_value_get_int64 (value);
2155       break;
2156     case PROP_PERFECT_RTPTIME:
2157       priv->perfect_rtptime = g_value_get_boolean (value);
2158       break;
2159     case PROP_PTIME_MULTIPLE:
2160       rtpbasepayload->ptime_multiple = g_value_get_int64 (value);
2161       break;
2162     case PROP_SOURCE_INFO:
2163       gst_rtp_base_payload_set_source_info_enabled (rtpbasepayload,
2164           g_value_get_boolean (value));
2165       break;
2166     case PROP_ONVIF_NO_RATE_CONTROL:
2167       priv->onvif_no_rate_control = g_value_get_boolean (value);
2168       break;
2169     case PROP_SCALE_RTPTIME:
2170       priv->scale_rtptime = g_value_get_boolean (value);
2171       break;
2172     case PROP_AUTO_HEADER_EXTENSION:
2173       priv->auto_hdr_ext = g_value_get_boolean (value);
2174       break;
2175     default:
2176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2177       break;
2178   }
2179 }
2180 
2181 static void
gst_rtp_base_payload_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2182 gst_rtp_base_payload_get_property (GObject * object, guint prop_id,
2183     GValue * value, GParamSpec * pspec)
2184 {
2185   GstRTPBasePayload *rtpbasepayload;
2186   GstRTPBasePayloadPrivate *priv;
2187 
2188   rtpbasepayload = GST_RTP_BASE_PAYLOAD (object);
2189   priv = rtpbasepayload->priv;
2190 
2191   switch (prop_id) {
2192     case PROP_MTU:
2193       g_value_set_uint (value, rtpbasepayload->mtu);
2194       break;
2195     case PROP_PT:
2196       g_value_set_uint (value, rtpbasepayload->pt);
2197       break;
2198     case PROP_SSRC:
2199       if (priv->ssrc_random)
2200         g_value_set_uint (value, -1);
2201       else
2202         g_value_set_uint (value, rtpbasepayload->ssrc);
2203       break;
2204     case PROP_TIMESTAMP_OFFSET:
2205       if (priv->ts_offset_random)
2206         g_value_set_uint (value, -1);
2207       else
2208         g_value_set_uint (value, (guint32) rtpbasepayload->ts_offset);
2209       break;
2210     case PROP_SEQNUM_OFFSET:
2211       if (priv->seqnum_offset_random)
2212         g_value_set_int (value, -1);
2213       else
2214         g_value_set_int (value, (guint16) rtpbasepayload->seqnum_offset);
2215       break;
2216     case PROP_MAX_PTIME:
2217       g_value_set_int64 (value, rtpbasepayload->max_ptime);
2218       break;
2219     case PROP_MIN_PTIME:
2220       g_value_set_int64 (value, rtpbasepayload->min_ptime);
2221       break;
2222     case PROP_TIMESTAMP:
2223       g_value_set_uint (value, rtpbasepayload->timestamp);
2224       break;
2225     case PROP_SEQNUM:
2226       g_value_set_uint (value, rtpbasepayload->seqnum);
2227       break;
2228     case PROP_PERFECT_RTPTIME:
2229       g_value_set_boolean (value, priv->perfect_rtptime);
2230       break;
2231     case PROP_PTIME_MULTIPLE:
2232       g_value_set_int64 (value, rtpbasepayload->ptime_multiple);
2233       break;
2234     case PROP_STATS:
2235       g_value_take_boxed (value,
2236           gst_rtp_base_payload_create_stats (rtpbasepayload));
2237       break;
2238     case PROP_SOURCE_INFO:
2239       g_value_set_boolean (value,
2240           gst_rtp_base_payload_is_source_info_enabled (rtpbasepayload));
2241       break;
2242     case PROP_ONVIF_NO_RATE_CONTROL:
2243       g_value_set_boolean (value, priv->onvif_no_rate_control);
2244       break;
2245     case PROP_SCALE_RTPTIME:
2246       g_value_set_boolean (value, priv->scale_rtptime);
2247       break;
2248     case PROP_AUTO_HEADER_EXTENSION:
2249       g_value_set_boolean (value, priv->auto_hdr_ext);
2250       break;
2251     default:
2252       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2253       break;
2254   }
2255 }
2256 
2257 static GstStateChangeReturn
gst_rtp_base_payload_change_state(GstElement * element,GstStateChange transition)2258 gst_rtp_base_payload_change_state (GstElement * element,
2259     GstStateChange transition)
2260 {
2261   GstRTPBasePayload *rtpbasepayload;
2262   GstRTPBasePayloadPrivate *priv;
2263   GstStateChangeReturn ret;
2264 
2265   rtpbasepayload = GST_RTP_BASE_PAYLOAD (element);
2266   priv = rtpbasepayload->priv;
2267 
2268   switch (transition) {
2269     case GST_STATE_CHANGE_NULL_TO_READY:
2270       break;
2271     case GST_STATE_CHANGE_READY_TO_PAUSED:
2272       gst_segment_init (&rtpbasepayload->segment, GST_FORMAT_UNDEFINED);
2273       rtpbasepayload->priv->delay_segment = TRUE;
2274       gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
2275 
2276       if (priv->seqnum_offset_random)
2277         rtpbasepayload->seqnum_base = g_random_int_range (0, G_MAXINT16);
2278       else
2279         rtpbasepayload->seqnum_base = rtpbasepayload->seqnum_offset;
2280       priv->next_seqnum = rtpbasepayload->seqnum_base;
2281       rtpbasepayload->seqnum = rtpbasepayload->seqnum_base;
2282 
2283       if (priv->ssrc_random)
2284         rtpbasepayload->current_ssrc = g_random_int ();
2285       else
2286         rtpbasepayload->current_ssrc = rtpbasepayload->ssrc;
2287 
2288       if (priv->ts_offset_random)
2289         rtpbasepayload->ts_base = g_random_int ();
2290       else
2291         rtpbasepayload->ts_base = rtpbasepayload->ts_offset;
2292       rtpbasepayload->timestamp = rtpbasepayload->ts_base;
2293       priv->running_time = DEFAULT_RUNNING_TIME;
2294       g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
2295       priv->base_offset = GST_BUFFER_OFFSET_NONE;
2296       priv->negotiated = FALSE;
2297       priv->negotiate_called = FALSE;
2298       gst_caps_replace (&rtpbasepayload->priv->subclass_srccaps, NULL);
2299       gst_caps_replace (&rtpbasepayload->priv->sinkcaps, NULL);
2300       break;
2301     default:
2302       break;
2303   }
2304 
2305   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2306 
2307   switch (transition) {
2308     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2309       g_atomic_int_set (&rtpbasepayload->priv->notified_first_timestamp, 1);
2310       break;
2311     case GST_STATE_CHANGE_PAUSED_TO_READY:
2312       gst_event_replace (&rtpbasepayload->priv->pending_segment, NULL);
2313       break;
2314     default:
2315       break;
2316   }
2317   return ret;
2318 }
2319 
2320 /**
2321  * gst_rtp_base_payload_set_source_info_enabled:
2322  * @payload: a #GstRTPBasePayload
2323  * @enable: whether to add contributing sources to RTP packets
2324  *
2325  * Enable or disable adding contributing sources to RTP packets from
2326  * #GstRTPSourceMeta.
2327  *
2328  * Since: 1.16
2329  **/
2330 void
gst_rtp_base_payload_set_source_info_enabled(GstRTPBasePayload * payload,gboolean enable)2331 gst_rtp_base_payload_set_source_info_enabled (GstRTPBasePayload * payload,
2332     gboolean enable)
2333 {
2334   payload->priv->source_info = enable;
2335 }
2336 
2337 /**
2338  * gst_rtp_base_payload_is_source_info_enabled:
2339  * @payload: a #GstRTPBasePayload
2340  *
2341  * Queries whether the payloader will add contributing sources (CSRCs) to the
2342  * RTP header from #GstRTPSourceMeta.
2343  *
2344  * Returns: %TRUE if source-info is enabled.
2345  *
2346  * Since: 1.16
2347  **/
2348 gboolean
gst_rtp_base_payload_is_source_info_enabled(GstRTPBasePayload * payload)2349 gst_rtp_base_payload_is_source_info_enabled (GstRTPBasePayload * payload)
2350 {
2351   return payload->priv->source_info;
2352 }
2353 
2354 
2355 /**
2356  * gst_rtp_base_payload_get_source_count:
2357  * @payload: a #GstRTPBasePayload
2358  * @buffer: (transfer none): a #GstBuffer, typically the buffer to payload
2359  *
2360  * Count the total number of RTP sources found in the meta of @buffer, which
2361  * will be automically added by gst_rtp_base_payload_allocate_output_buffer().
2362  * If #GstRTPBasePayload:source-info is %FALSE the count will be 0.
2363  *
2364  * Returns: The number of sources.
2365  *
2366  * Since: 1.16
2367  **/
2368 guint
gst_rtp_base_payload_get_source_count(GstRTPBasePayload * payload,GstBuffer * buffer)2369 gst_rtp_base_payload_get_source_count (GstRTPBasePayload * payload,
2370     GstBuffer * buffer)
2371 {
2372   guint count = 0;
2373 
2374   g_return_val_if_fail (buffer != NULL, 0);
2375 
2376   if (gst_rtp_base_payload_is_source_info_enabled (payload)) {
2377     GstRTPSourceMeta *meta = gst_buffer_get_rtp_source_meta (buffer);
2378     if (meta != NULL)
2379       count = gst_rtp_source_meta_get_source_count (meta);
2380   }
2381 
2382   return count;
2383 }
2384