1 /* GStreamer
2  * Copyright (C) <2016> Stian Selnes <stian@pexip.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "gstrtpmeta.h"
25 #include <string.h>
26 
27 /**
28  * SECTION:gstrtpmeta
29  * @title: GstMeta for RTP
30  * @short_description: RTP related GstMeta
31  *
32  */
33 
34 /**
35  * gst_buffer_add_rtp_source_meta:
36  * @buffer: a #GstBuffer
37  * @ssrc: (allow-none) (transfer none): pointer to the SSRC
38  * @csrc: (allow-none) (transfer none): pointer to the CSRCs
39  * @csrc_count: number of elements in @csrc
40  *
41  * Attaches RTP source information to @buffer.
42  *
43  * Returns: (transfer none): the #GstRTPSourceMeta on @buffer.
44  *
45  * Since: 1.16
46  */
47 GstRTPSourceMeta *
gst_buffer_add_rtp_source_meta(GstBuffer * buffer,const guint32 * ssrc,const guint * csrc,guint csrc_count)48 gst_buffer_add_rtp_source_meta (GstBuffer * buffer, const guint32 * ssrc,
49     const guint * csrc, guint csrc_count)
50 {
51   gint i;
52   GstRTPSourceMeta *meta;
53 
54   g_return_val_if_fail (buffer != NULL, NULL);
55   g_return_val_if_fail (csrc_count <= GST_RTP_SOURCE_META_MAX_CSRC_COUNT, NULL);
56   g_return_val_if_fail (csrc_count == 0 || csrc != NULL, NULL);
57 
58   meta = (GstRTPSourceMeta *) gst_buffer_add_meta (buffer,
59       GST_RTP_SOURCE_META_INFO, NULL);
60   if (!meta)
61     return NULL;
62 
63   if (ssrc != NULL) {
64     meta->ssrc = *ssrc;
65     meta->ssrc_valid = TRUE;
66   } else {
67     meta->ssrc_valid = FALSE;
68   }
69 
70   meta->csrc_count = csrc_count;
71   for (i = 0; i < csrc_count; i++) {
72     meta->csrc[i] = csrc[i];
73   }
74 
75   return meta;
76 }
77 
78 /**
79  * gst_buffer_get_rtp_source_meta:
80  * @buffer: a #GstBuffer
81  *
82  * Find the #GstRTPSourceMeta on @buffer.
83  *
84  * Returns: (transfer none): the #GstRTPSourceMeta or %NULL when there
85  * is no such metadata on @buffer.
86  *
87  * Since: 1.16
88  */
89 GstRTPSourceMeta *
gst_buffer_get_rtp_source_meta(GstBuffer * buffer)90 gst_buffer_get_rtp_source_meta (GstBuffer * buffer)
91 {
92   return (GstRTPSourceMeta *) gst_buffer_get_meta (buffer,
93       gst_rtp_source_meta_api_get_type ());
94 }
95 
96 static gboolean
gst_rtp_source_meta_transform(GstBuffer * dst,GstMeta * meta,GstBuffer * src,GQuark type,gpointer data)97 gst_rtp_source_meta_transform (GstBuffer * dst, GstMeta * meta,
98     GstBuffer * src, GQuark type, gpointer data)
99 {
100   if (GST_META_TRANSFORM_IS_COPY (type)) {
101     GstRTPSourceMeta *smeta = (GstRTPSourceMeta *) meta;
102     GstRTPSourceMeta *dmeta;
103     guint32 *ssrc = smeta->ssrc_valid ? &smeta->ssrc : NULL;
104 
105     dmeta = gst_buffer_add_rtp_source_meta (dst, ssrc, smeta->csrc,
106         smeta->csrc_count);
107     if (dmeta == NULL)
108       return FALSE;
109   } else {
110     /* return FALSE, if transform type is not supported */
111     return FALSE;
112   }
113 
114   return TRUE;
115 }
116 
117 /**
118  * gst_rtp_source_meta_get_source_count:
119  * @meta: a #GstRTPSourceMeta
120  *
121  * Count the total number of RTP sources found in @meta, both SSRC and CSRC.
122  *
123  * Returns: The number of RTP sources
124  *
125  * Since: 1.16
126  */
127 guint
gst_rtp_source_meta_get_source_count(const GstRTPSourceMeta * meta)128 gst_rtp_source_meta_get_source_count (const GstRTPSourceMeta * meta)
129 {
130   /* Never return more than a count of 15 so that the returned value
131    * conveniently can be used as argument 'csrc_count' in
132    * gst_rtp_buffer-functions. */
133   guint ssrc_count = meta->ssrc_valid ? 1 : 0;
134   return MIN (meta->csrc_count + ssrc_count, 15);
135 }
136 
137 /**
138  * gst_rtp_source_meta_set_ssrc:
139  * @meta: a #GstRTPSourceMeta
140  * @ssrc: (allow-none) (transfer none): pointer to the SSRC
141  *
142  * Sets @ssrc in @meta. If @ssrc is %NULL the ssrc of @meta will be unset.
143  *
144  * Returns: %TRUE on success, %FALSE otherwise.
145  *
146  * Since: 1.16
147  **/
148 gboolean
gst_rtp_source_meta_set_ssrc(GstRTPSourceMeta * meta,guint32 * ssrc)149 gst_rtp_source_meta_set_ssrc (GstRTPSourceMeta * meta, guint32 * ssrc)
150 {
151   if (ssrc != NULL) {
152     meta->ssrc = *ssrc;
153     meta->ssrc_valid = TRUE;
154   } else {
155     meta->ssrc_valid = FALSE;
156   }
157 
158   return TRUE;
159 }
160 
161 /**
162  * gst_rtp_source_meta_append_csrc:
163  * @meta: a #GstRTPSourceMeta
164  * @csrc: the csrcs to append
165  * @csrc_count: number of elements in @csrc
166  *
167  * Appends @csrc to the list of contributing sources in @meta.
168  *
169  * Returns: %TRUE if all elements in @csrc was added, %FALSE otherwise.
170  *
171  * Since: 1.16
172  **/
173 gboolean
gst_rtp_source_meta_append_csrc(GstRTPSourceMeta * meta,const guint32 * csrc,guint csrc_count)174 gst_rtp_source_meta_append_csrc (GstRTPSourceMeta * meta, const guint32 * csrc,
175     guint csrc_count)
176 {
177   gint i;
178   guint new_csrc_count = meta->csrc_count + csrc_count;
179 
180   if (new_csrc_count > GST_RTP_SOURCE_META_MAX_CSRC_COUNT)
181     return FALSE;
182 
183   for (i = 0; i < csrc_count; i++)
184     meta->csrc[meta->csrc_count + i] = csrc[i];
185   meta->csrc_count = new_csrc_count;
186 
187   return TRUE;
188 }
189 
190 GType
gst_rtp_source_meta_api_get_type(void)191 gst_rtp_source_meta_api_get_type (void)
192 {
193   static volatile GType type = 0;
194   static const gchar *tags[] = { NULL };
195 
196   if (g_once_init_enter (&type)) {
197     GType _type = gst_meta_api_type_register ("GstRTPSourceMetaAPI", tags);
198     g_once_init_leave (&type, _type);
199   }
200   return type;
201 }
202 
203 static gboolean
gst_rtp_source_meta_init(GstMeta * meta,gpointer params,GstBuffer * buffer)204 gst_rtp_source_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
205 {
206   GstRTPSourceMeta *dmeta = (GstRTPSourceMeta *) meta;
207 
208   dmeta->ssrc_valid = FALSE;
209   dmeta->csrc_count = 0;
210 
211   return TRUE;
212 }
213 
214 const GstMetaInfo *
gst_rtp_source_meta_get_info(void)215 gst_rtp_source_meta_get_info (void)
216 {
217   static const GstMetaInfo *rtp_source_meta_info = NULL;
218 
219   if (g_once_init_enter (&rtp_source_meta_info)) {
220     const GstMetaInfo *meta = gst_meta_register (GST_RTP_SOURCE_META_API_TYPE,
221         "GstRTPSourceMeta",
222         sizeof (GstRTPSourceMeta),
223         gst_rtp_source_meta_init,
224         (GstMetaFreeFunction) NULL,
225         gst_rtp_source_meta_transform);
226     g_once_init_leave (&rtp_source_meta_info, meta);
227   }
228   return rtp_source_meta_info;
229 }
230