2 * GStreamer - GStreamer SRTP encoder and decoder
4 * Copyright 2009-2013 Collabora Ltd.
5 * @author: Gabriel Millaire <gabriel.millaire@collabora.co.uk>
6 * @author: Olivier Crete <olivier.crete@collabora.com>
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.
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.
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.
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 #include <gst/rtp/gstrtcpbuffer.h>
31 #include "gstsrtpenc.h"
32 #include "gstsrtpdec.h"
36 srtp_set_stream_roc (srtp_t session, guint32 ssrc, guint32 roc)
40 stream = srtp_get_stream (session, htonl (ssrc));
42 return srtp_err_status_bad_param;
45 rdbx_set_roc (&stream->rtp_rdbx, roc);
46 return srtp_err_status_ok;
50 srtp_get_stream_roc (srtp_t session, guint32 ssrc, guint32 * roc)
54 stream = srtp_get_stream (session, htonl (ssrc));
56 return srtp_err_status_bad_param;
59 *roc = stream->rtp_rdbx.index >> 16;
60 return srtp_err_status_ok;
64 static void free_reporter_data (gpointer data);
66 GPrivate current_callback = G_PRIVATE_INIT (free_reporter_data);
68 struct GstSrtpEventReporterData
70 gboolean soft_limit_reached;
74 free_reporter_data (gpointer data)
76 g_slice_free (struct GstSrtpEventReporterData, data);
81 srtp_event_reporter (srtp_event_data_t * data)
83 struct GstSrtpEventReporterData *dat = g_private_get (¤t_callback);
88 switch (data->event) {
89 case event_key_soft_limit:
90 dat->soft_limit_reached = TRUE;
99 gst_srtp_init_event_reporter (void)
101 struct GstSrtpEventReporterData *dat = g_private_get (¤t_callback);
104 dat = g_slice_new (struct GstSrtpEventReporterData);
105 g_private_set (¤t_callback, dat);
108 dat->soft_limit_reached = FALSE;
110 srtp_install_event_handler (srtp_event_reporter);
114 enum_nick_from_value (GType enum_gtype, gint value)
116 GEnumClass *enum_class = g_type_class_ref (enum_gtype);
117 GEnumValue *enum_value;
123 enum_value = g_enum_get_value (enum_class, value);
126 nick = enum_value->value_nick;
127 g_type_class_unref (enum_class);
134 enum_value_from_nick (GType enum_gtype, const gchar * nick)
136 GEnumClass *enum_class = g_type_class_ref (enum_gtype);
137 GEnumValue *enum_value;
143 enum_value = g_enum_get_value_by_nick (enum_class, nick);
146 value = enum_value->value;
147 g_type_class_unref (enum_class);
153 gst_srtp_get_soft_limit_reached (void)
155 struct GstSrtpEventReporterData *dat = g_private_get (¤t_callback);
158 return dat->soft_limit_reached;
162 /* Get SSRC from RTCP buffer
165 rtcp_buffer_get_ssrc (GstBuffer * buf, guint32 * ssrc)
167 gboolean ret = FALSE;
168 GstRTCPBuffer rtcpbuf = GST_RTCP_BUFFER_INIT;
169 GstRTCPPacket packet;
171 /* Get SSRC from RR or SR packet (RTCP) */
173 if (!gst_rtcp_buffer_map (buf, GST_MAP_READ, &rtcpbuf))
176 if (gst_rtcp_buffer_get_first_packet (&rtcpbuf, &packet)) {
179 type = gst_rtcp_packet_get_type (&packet);
181 case GST_RTCP_TYPE_RR:
182 *ssrc = gst_rtcp_packet_rr_get_ssrc (&packet);
185 case GST_RTCP_TYPE_SR:
186 gst_rtcp_packet_sr_get_sender_info (&packet, ssrc, NULL, NULL, NULL,
190 case GST_RTCP_TYPE_RTPFB:
191 case GST_RTCP_TYPE_PSFB:
192 *ssrc = gst_rtcp_packet_fb_get_sender_ssrc (&packet);
195 case GST_RTCP_TYPE_APP:
196 *ssrc = gst_rtcp_packet_app_get_ssrc (&packet);
199 case GST_RTCP_TYPE_BYE:
200 *ssrc = gst_rtcp_packet_bye_get_nth_ssrc (&packet, 0);
206 } while ((ret == FALSE) && (type != GST_RTCP_TYPE_INVALID) &&
207 gst_rtcp_packet_move_to_next (&packet));
210 gst_rtcp_buffer_unmap (&rtcpbuf);
216 set_crypto_policy_cipher_auth (GstSrtpCipherType cipher,
217 GstSrtpAuthType auth, srtp_crypto_policy_t * policy)
220 case GST_SRTP_CIPHER_AES_128_ICM:
221 policy->cipher_type = SRTP_AES_ICM_128;
223 case GST_SRTP_CIPHER_AES_256_ICM:
224 policy->cipher_type = SRTP_AES_ICM_256;
226 case GST_SRTP_CIPHER_AES_128_GCM:
227 policy->cipher_type = SRTP_AES_GCM_128;
229 case GST_SRTP_CIPHER_AES_256_GCM:
230 policy->cipher_type = SRTP_AES_GCM_256;
232 case GST_SRTP_CIPHER_NULL:
233 policy->cipher_type = SRTP_NULL_CIPHER;
236 g_assert_not_reached ();
239 policy->cipher_key_len = cipher_key_size (cipher);
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;
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;
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;
259 policy->auth_tag_len = 0;
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;
271 policy->sec_serv = sec_serv_conf_and_auth;
275 cipher_key_size (GstSrtpCipherType cipher)
280 case GST_SRTP_CIPHER_AES_128_ICM:
281 size = SRTP_AES_ICM_128_KEY_LEN_WSALT;
283 case GST_SRTP_CIPHER_AES_256_ICM:
284 size = SRTP_AES_ICM_256_KEY_LEN_WSALT;
286 case GST_SRTP_CIPHER_AES_128_GCM:
287 size = SRTP_AES_GCM_128_KEY_LEN_WSALT;
289 case GST_SRTP_CIPHER_AES_256_GCM:
290 size = SRTP_AES_GCM_256_KEY_LEN_WSALT;
292 case GST_SRTP_CIPHER_NULL:
295 g_assert_not_reached ();