taglist, plugins: fix compiler warnings with GLib >= 2.76
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / srtp / gstsrtp.c
1 /*
2  * GStreamer - GStreamer SRTP encoder and decoder
3  *
4  * Copyright 2009-2013 Collabora Ltd.
5  *  @author: Gabriel Millaire <gabriel.millaire@collabora.co.uk>
6  *  @author: Olivier Crete <olivier.crete@collabora.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26
27 #include "gstsrtp.h"
28
29 #include <gst/rtp/gstrtcpbuffer.h>
30
31 #include "gstsrtpenc.h"
32 #include "gstsrtpdec.h"
33
34 #ifndef HAVE_SRTP2
35 srtp_err_status_t
36 srtp_set_stream_roc (srtp_t session, guint32 ssrc, guint32 roc)
37 {
38   srtp_stream_t stream;
39
40   stream = srtp_get_stream (session, htonl (ssrc));
41   if (stream == NULL) {
42     return srtp_err_status_bad_param;
43   }
44
45   rdbx_set_roc (&stream->rtp_rdbx, roc);
46   return srtp_err_status_ok;
47 }
48
49 srtp_err_status_t
50 srtp_get_stream_roc (srtp_t session, guint32 ssrc, guint32 * roc)
51 {
52   srtp_stream_t stream;
53
54   stream = srtp_get_stream (session, htonl (ssrc));
55   if (stream == NULL) {
56     return srtp_err_status_bad_param;
57   }
58
59   *roc = stream->rtp_rdbx.index >> 16;
60   return srtp_err_status_ok;
61 }
62 #endif
63
64 static void free_reporter_data (gpointer data);
65
66 GPrivate current_callback = G_PRIVATE_INIT (free_reporter_data);
67
68 struct GstSrtpEventReporterData
69 {
70   gboolean soft_limit_reached;
71 };
72
73 static void
74 free_reporter_data (gpointer data)
75 {
76   g_slice_free (struct GstSrtpEventReporterData, data);
77 }
78
79
80 static void
81 srtp_event_reporter (srtp_event_data_t * data)
82 {
83   struct GstSrtpEventReporterData *dat = g_private_get (&current_callback);
84
85   if (!dat)
86     return;
87
88   switch (data->event) {
89     case event_key_soft_limit:
90       dat->soft_limit_reached = TRUE;
91       break;
92
93     default:
94       break;
95   }
96 }
97
98 void
99 gst_srtp_init_event_reporter (void)
100 {
101   struct GstSrtpEventReporterData *dat = g_private_get (&current_callback);
102
103   if (!dat) {
104     dat = g_slice_new (struct GstSrtpEventReporterData);
105     g_private_set (&current_callback, dat);
106   }
107
108   dat->soft_limit_reached = FALSE;
109
110   srtp_install_event_handler (srtp_event_reporter);
111 }
112
113 const gchar *
114 enum_nick_from_value (GType enum_gtype, gint value)
115 {
116   GEnumClass *enum_class = g_type_class_ref (enum_gtype);
117   GEnumValue *enum_value;
118   const gchar *nick;
119
120   if (!enum_gtype)
121     return NULL;
122
123   enum_value = g_enum_get_value (enum_class, value);
124   if (!enum_value)
125     return NULL;
126   nick = enum_value->value_nick;
127   g_type_class_unref (enum_class);
128
129   return nick;
130 }
131
132
133 gint
134 enum_value_from_nick (GType enum_gtype, const gchar * nick)
135 {
136   GEnumClass *enum_class = g_type_class_ref (enum_gtype);
137   GEnumValue *enum_value;
138   gint value;
139
140   if (!enum_gtype)
141     return -1;
142
143   enum_value = g_enum_get_value_by_nick (enum_class, nick);
144   if (!enum_value)
145     return -1;
146   value = enum_value->value;
147   g_type_class_unref (enum_class);
148
149   return value;
150 }
151
152 gboolean
153 gst_srtp_get_soft_limit_reached (void)
154 {
155   struct GstSrtpEventReporterData *dat = g_private_get (&current_callback);
156
157   if (dat)
158     return dat->soft_limit_reached;
159   return FALSE;
160 }
161
162 /* Get SSRC from RTCP buffer
163  */
164 gboolean
165 rtcp_buffer_get_ssrc (GstBuffer * buf, guint32 * ssrc)
166 {
167   gboolean ret = FALSE;
168   GstRTCPBuffer rtcpbuf = GST_RTCP_BUFFER_INIT;
169   GstRTCPPacket packet;
170
171   /* Get SSRC from RR or SR packet (RTCP) */
172
173   if (!gst_rtcp_buffer_map (buf, GST_MAP_READ, &rtcpbuf))
174     return FALSE;
175
176   if (gst_rtcp_buffer_get_first_packet (&rtcpbuf, &packet)) {
177     GstRTCPType type;
178     do {
179       type = gst_rtcp_packet_get_type (&packet);
180       switch (type) {
181         case GST_RTCP_TYPE_RR:
182           *ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
183           ret = TRUE;
184           break;
185         case GST_RTCP_TYPE_SR:
186           gst_rtcp_packet_sr_get_sender_info (&packet, ssrc, NULL, NULL, NULL,
187               NULL);
188           ret = TRUE;
189           break;
190         case GST_RTCP_TYPE_RTPFB:
191         case GST_RTCP_TYPE_PSFB:
192           *ssrc = gst_rtcp_packet_fb_get_sender_ssrc (&packet);
193           ret = TRUE;
194           break;
195         case GST_RTCP_TYPE_APP:
196           *ssrc = gst_rtcp_packet_app_get_ssrc (&packet);
197           ret = TRUE;
198           break;
199         case GST_RTCP_TYPE_BYE:
200           *ssrc = gst_rtcp_packet_bye_get_nth_ssrc (&packet, 0);
201           ret = TRUE;
202           break;
203         default:
204           break;
205       }
206     } while ((ret == FALSE) && (type != GST_RTCP_TYPE_INVALID) &&
207         gst_rtcp_packet_move_to_next (&packet));
208   }
209
210   gst_rtcp_buffer_unmap (&rtcpbuf);
211
212   return ret;
213 }
214
215 void
216 set_crypto_policy_cipher_auth (GstSrtpCipherType cipher,
217     GstSrtpAuthType auth, srtp_crypto_policy_t * policy)
218 {
219   switch (cipher) {
220     case GST_SRTP_CIPHER_AES_128_ICM:
221       policy->cipher_type = SRTP_AES_ICM_128;
222       break;
223     case GST_SRTP_CIPHER_AES_256_ICM:
224       policy->cipher_type = SRTP_AES_ICM_256;
225       break;
226     case GST_SRTP_CIPHER_AES_128_GCM:
227       policy->cipher_type = SRTP_AES_GCM_128;
228       break;
229     case GST_SRTP_CIPHER_AES_256_GCM:
230       policy->cipher_type = SRTP_AES_GCM_256;
231       break;
232     case GST_SRTP_CIPHER_NULL:
233       policy->cipher_type = SRTP_NULL_CIPHER;
234       break;
235     default:
236       g_assert_not_reached ();
237   }
238
239   policy->cipher_key_len = cipher_key_size (cipher);
240
241   switch (auth) {
242     case GST_SRTP_AUTH_HMAC_SHA1_80:
243       policy->auth_type = SRTP_HMAC_SHA1;
244       policy->auth_key_len = 20;
245       policy->auth_tag_len = 10;
246       break;
247     case GST_SRTP_AUTH_HMAC_SHA1_32:
248       policy->auth_type = SRTP_HMAC_SHA1;
249       policy->auth_key_len = 20;
250       policy->auth_tag_len = 4;
251       break;
252     case GST_SRTP_AUTH_NULL:
253       policy->auth_type = SRTP_NULL_AUTH;
254       policy->auth_key_len = 0;
255       if (cipher == GST_SRTP_CIPHER_AES_128_GCM
256           || cipher == GST_SRTP_CIPHER_AES_256_GCM) {
257         policy->auth_tag_len = 16;
258       } else {
259         policy->auth_tag_len = 0;
260       }
261       break;
262   }
263
264   if (cipher == GST_SRTP_CIPHER_NULL && auth == GST_SRTP_AUTH_NULL)
265     policy->sec_serv = sec_serv_none;
266   else if (cipher == GST_SRTP_CIPHER_NULL)
267     policy->sec_serv = sec_serv_auth;
268   else if (auth == GST_SRTP_AUTH_NULL)
269     policy->sec_serv = sec_serv_conf;
270   else
271     policy->sec_serv = sec_serv_conf_and_auth;
272 }
273
274 guint
275 cipher_key_size (GstSrtpCipherType cipher)
276 {
277   guint size = 0;
278
279   switch (cipher) {
280     case GST_SRTP_CIPHER_AES_128_ICM:
281       size = SRTP_AES_ICM_128_KEY_LEN_WSALT;
282       break;
283     case GST_SRTP_CIPHER_AES_256_ICM:
284       size = SRTP_AES_ICM_256_KEY_LEN_WSALT;
285       break;
286     case GST_SRTP_CIPHER_AES_128_GCM:
287       size = SRTP_AES_GCM_128_KEY_LEN_WSALT;
288       break;
289     case GST_SRTP_CIPHER_AES_256_GCM:
290       size = SRTP_AES_GCM_256_KEY_LEN_WSALT;
291       break;
292     case GST_SRTP_CIPHER_NULL:
293       break;
294     default:
295       g_assert_not_reached ();
296   }
297
298   return size;
299 }