2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
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.
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.
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.
21 * @short_description: Make SDP messages
22 * @see_also: #GstRTSPMedia
24 * Last reviewed on 2013-07-11 (1.0.0)
29 #include <gst/sdp/gstmikey.h>
33 #define AES_128_KEY_LEN 16
34 #define AES_256_KEY_LEN 32
36 #define HMAC_32_KEY_LEN 4
37 #define HMAC_80_KEY_LEN 10
40 get_info_from_tags (GstPad * pad, GstEvent ** event, gpointer user_data)
42 GstSDPMedia *media = (GstSDPMedia *) user_data;
44 if (GST_EVENT_TYPE (*event) == GST_EVENT_TAG) {
48 gst_event_parse_tag (*event, &tags);
50 if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
53 if (!gst_tag_list_get_uint (tags, GST_TAG_MAXIMUM_BITRATE,
54 &bitrate) || bitrate == 0)
55 if (!gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
59 /* set bandwidth (kbits/s) */
60 gst_sdp_media_add_bandwidth (media, GST_SDP_BWTYPE_AS, bitrate / 1000);
70 update_sdp_from_tags (GstRTSPStream * stream, GstSDPMedia * stream_media)
74 src_pad = gst_rtsp_stream_get_srcpad (stream);
76 gst_pad_sticky_events_foreach (src_pad, get_info_from_tags, stream_media);
78 gst_object_unref (src_pad);
82 enc_key_length_from_cipher_name (const gchar * cipher)
84 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
85 return AES_128_KEY_LEN;
86 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
87 return AES_256_KEY_LEN;
89 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
95 auth_key_length_from_auth_name (const gchar * auth)
97 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
98 return HMAC_32_KEY_LEN;
99 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
100 return HMAC_80_KEY_LEN;
102 GST_ERROR ("authentication algorithm '%s' not supported", auth);
108 make_media (GstSDPMessage * sdp, GstSDPInfo * info, GstRTSPMedia * media,
109 GstRTSPStream * stream, GstStructure * s, GstRTSPProfile profile)
112 const gchar *caps_str, *caps_enc, *caps_params;
114 gint caps_pt, caps_rate;
118 GstRTSPLowerTrans ltrans;
119 GSocketFamily family;
120 const gchar *addrtype, *proto;
124 gst_sdp_media_new (&smedia);
126 /* get media type and payload for the m= line */
127 caps_str = gst_structure_get_string (s, "media");
128 gst_sdp_media_set_media (smedia, caps_str);
130 gst_structure_get_int (s, "payload", &caps_pt);
131 tmp = g_strdup_printf ("%d", caps_pt);
132 gst_sdp_media_add_format (smedia, tmp);
135 gst_sdp_media_set_port_info (smedia, 0, 1);
138 case GST_RTSP_PROFILE_AVP:
141 case GST_RTSP_PROFILE_AVPF:
144 case GST_RTSP_PROFILE_SAVP:
147 case GST_RTSP_PROFILE_SAVPF:
154 gst_sdp_media_set_proto (smedia, proto);
158 family = G_SOCKET_FAMILY_IPV6;
161 family = G_SOCKET_FAMILY_IPV4;
164 ltrans = gst_rtsp_stream_get_protocols (stream);
165 if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
166 GstRTSPAddress *addr;
168 addr = gst_rtsp_stream_get_multicast_address (stream, family);
172 address = g_strdup (addr->address);
174 gst_rtsp_address_free (addr);
178 address = g_strdup ("::");
180 address = g_strdup ("0.0.0.0");
183 /* for the c= line */
184 gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
187 /* get clock-rate, media type and params for the rtpmap attribute */
188 gst_structure_get_int (s, "clock-rate", &caps_rate);
189 caps_enc = gst_structure_get_string (s, "encoding-name");
190 caps_params = gst_structure_get_string (s, "encoding-params");
194 tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
197 tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
199 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
204 tmp = gst_rtsp_stream_get_control (stream);
205 gst_sdp_media_add_attribute (smedia, "control", tmp);
213 const gchar *srtpcipher, *srtpauth, *srtcpcipher, *srtcpauth;
214 GstMIKEYMessage *msg;
215 GstMIKEYPayload *payload, *pkd;
224 val = gst_structure_get_value (s, "srtp-key");
228 srtpkey = gst_value_get_buffer (val);
232 srtpcipher = gst_structure_get_string (s, "srtp-cipher");
233 srtpauth = gst_structure_get_string (s, "srtp-auth");
234 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
235 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
237 if (srtpcipher == NULL || srtpauth == NULL || srtcpcipher == NULL ||
241 msg = gst_mikey_message_new ();
242 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
243 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
244 FALSE, GST_MIKEY_PRF_MIKEY_1, 0, GST_MIKEY_MAP_TYPE_SRTP);
245 /* add policy '0' for our SSRC */
246 gst_rtsp_stream_get_ssrc (stream, &ssrc);
247 gst_mikey_message_add_cs_srtp (msg, 0, ssrc, 0);
248 /* timestamp is now */
249 gst_mikey_message_add_t_now_ntp_utc (msg);
250 /* add some random data */
251 gst_mikey_message_add_rand_len (msg, 16);
253 /* the policy '0' is SRTP with the above discovered algorithms */
254 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
255 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
257 /* only AES-CM is supported */
259 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1,
261 /* Encryption key length */
262 byte = enc_key_length_from_cipher_name (srtpcipher);
263 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
266 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
268 /* Authentication key length */
269 byte = auth_key_length_from_auth_name (srtpauth);
270 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
272 /* we enable encryption on RTP and RTCP */
273 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
275 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
277 /* we enable authentication on RTP and RTCP */
278 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
280 gst_mikey_message_add_payload (msg, payload);
282 /* make unencrypted KEMAC */
283 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
284 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL,
287 /* add the key in key data */
288 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
289 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
290 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
292 gst_buffer_unmap (srtpkey, &info);
293 /* add key data to KEMAC */
294 gst_mikey_payload_kemac_add_sub (payload, pkd);
295 gst_mikey_message_add_payload (msg, payload);
297 /* now serialize this to bytes */
298 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
299 gst_mikey_message_unref (msg);
300 /* and make it into base64 */
301 data = g_bytes_get_data (bytes, &size);
302 base64 = g_base64_encode (data, size);
303 g_bytes_unref (bytes);
305 tmp = g_strdup_printf ("mikey %s", base64);
308 gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
312 /* collect all other properties and add them to fmtp or attributes */
313 fmtp = g_string_new ("");
314 g_string_append_printf (fmtp, "%d ", caps_pt);
316 n_fields = gst_structure_n_fields (s);
317 for (j = 0; j < n_fields; j++) {
318 const gchar *fname, *fval;
320 fname = gst_structure_nth_field_name (s, j);
322 /* filter out standard properties */
323 if (!strcmp (fname, "media"))
325 if (!strcmp (fname, "payload"))
327 if (!strcmp (fname, "clock-rate"))
329 if (!strcmp (fname, "encoding-name"))
331 if (!strcmp (fname, "encoding-params"))
333 if (!strcmp (fname, "ssrc"))
335 if (!strcmp (fname, "clock-base"))
337 if (!strcmp (fname, "seqnum-base"))
339 if (g_str_has_prefix (fname, "srtp-"))
341 if (g_str_has_prefix (fname, "srtcp-"))
344 if (g_str_has_prefix (fname, "a-")) {
346 if ((fval = gst_structure_get_string (s, fname)))
347 gst_sdp_media_add_attribute (smedia, fname + 2, fval);
350 if (g_str_has_prefix (fname, "x-")) {
352 if ((fval = gst_structure_get_string (s, fname)))
353 gst_sdp_media_add_attribute (smedia, fname, fval);
357 if ((fval = gst_structure_get_string (s, fname))) {
358 g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
363 tmp = g_string_free (fmtp, FALSE);
364 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
367 g_string_free (fmtp, TRUE);
370 update_sdp_from_tags (stream, smedia);
372 gst_sdp_message_add_media (sdp, smedia);
373 gst_sdp_media_free (smedia);
380 gst_sdp_media_free (smedia);
381 g_warning ("ignoring stream %d without multicast address",
382 gst_rtsp_stream_get_index (stream));
388 * gst_rtsp_sdp_from_media:
389 * @sdp: a #GstSDPMessage
390 * @info: (transfer none): a #GstSDPInfo
391 * @media: (transfer none): a #GstRTSPMedia
393 * Add @media specific info to @sdp. @info is used to configure the connection
394 * information in the SDP.
396 * Returns: TRUE on success.
399 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
400 GstRTSPMedia * media)
405 n_streams = gst_rtsp_media_n_streams (media);
407 rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
408 if (rangestr == NULL)
411 gst_sdp_message_add_attribute (sdp, "range", rangestr);
414 for (i = 0; i < n_streams; i++) {
415 GstRTSPStream *stream;
418 GstRTSPProfile profiles;
421 stream = gst_rtsp_media_get_stream (media, i);
422 caps = gst_rtsp_stream_get_caps (stream);
425 g_warning ("ignoring stream %d without media type", i);
429 s = gst_caps_get_structure (caps, 0);
431 gst_caps_unref (caps);
432 g_warning ("ignoring stream %d without media type", i);
436 /* make a new media for each profile */
437 profiles = gst_rtsp_stream_get_profiles (stream);
439 while (profiles >= mask) {
440 GstRTSPProfile prof = profiles & mask;
443 make_media (sdp, info, media, stream, s, prof);
447 gst_caps_unref (caps);
451 GstNetTimeProvider *provider;
454 gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
456 gchar *address, *str;
459 g_object_get (provider, "clock", &clock, "address", &address, "port",
462 str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
463 g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
464 gst_clock_get_time (clock));
466 gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
468 gst_object_unref (clock);
470 gst_object_unref (provider);
479 GST_ERROR ("media %p is not prepared", media);