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;
123 GstClockTime rtx_time;
125 gst_sdp_media_new (&smedia);
127 /* get media type and payload for the m= line */
128 caps_str = gst_structure_get_string (s, "media");
129 gst_sdp_media_set_media (smedia, caps_str);
131 gst_structure_get_int (s, "payload", &caps_pt);
132 tmp = g_strdup_printf ("%d", caps_pt);
133 gst_sdp_media_add_format (smedia, tmp);
136 gst_sdp_media_set_port_info (smedia, 0, 1);
139 case GST_RTSP_PROFILE_AVP:
142 case GST_RTSP_PROFILE_AVPF:
145 case GST_RTSP_PROFILE_SAVP:
148 case GST_RTSP_PROFILE_SAVPF:
155 gst_sdp_media_set_proto (smedia, proto);
159 family = G_SOCKET_FAMILY_IPV6;
162 family = G_SOCKET_FAMILY_IPV4;
165 ltrans = gst_rtsp_stream_get_protocols (stream);
166 if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
167 GstRTSPAddress *addr;
169 addr = gst_rtsp_stream_get_multicast_address (stream, family);
173 address = g_strdup (addr->address);
175 gst_rtsp_address_free (addr);
179 address = g_strdup ("::");
181 address = g_strdup ("0.0.0.0");
184 /* for the c= line */
185 gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
188 /* get clock-rate, media type and params for the rtpmap attribute */
189 gst_structure_get_int (s, "clock-rate", &caps_rate);
190 caps_enc = gst_structure_get_string (s, "encoding-name");
191 caps_params = gst_structure_get_string (s, "encoding-params");
195 tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
198 tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
200 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
205 tmp = gst_rtsp_stream_get_control (stream);
206 gst_sdp_media_add_attribute (smedia, "control", tmp);
214 const gchar *srtpcipher, *srtpauth, *srtcpcipher, *srtcpauth;
215 GstMIKEYMessage *msg;
216 GstMIKEYPayload *payload, *pkd;
225 val = gst_structure_get_value (s, "srtp-key");
229 srtpkey = gst_value_get_buffer (val);
233 srtpcipher = gst_structure_get_string (s, "srtp-cipher");
234 srtpauth = gst_structure_get_string (s, "srtp-auth");
235 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
236 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
238 if (srtpcipher == NULL || srtpauth == NULL || srtcpcipher == NULL ||
242 msg = gst_mikey_message_new ();
243 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
244 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
245 FALSE, GST_MIKEY_PRF_MIKEY_1, 0, GST_MIKEY_MAP_TYPE_SRTP);
246 /* add policy '0' for our SSRC */
247 gst_rtsp_stream_get_ssrc (stream, &ssrc);
248 gst_mikey_message_add_cs_srtp (msg, 0, ssrc, 0);
249 /* timestamp is now */
250 gst_mikey_message_add_t_now_ntp_utc (msg);
251 /* add some random data */
252 gst_mikey_message_add_rand_len (msg, 16);
254 /* the policy '0' is SRTP with the above discovered algorithms */
255 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
256 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
258 /* only AES-CM is supported */
260 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1,
262 /* Encryption key length */
263 byte = enc_key_length_from_cipher_name (srtpcipher);
264 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
267 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
269 /* Authentication key length */
270 byte = auth_key_length_from_auth_name (srtpauth);
271 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
273 /* we enable encryption on RTP and RTCP */
274 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
276 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
278 /* we enable authentication on RTP and RTCP */
279 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
281 gst_mikey_message_add_payload (msg, payload);
283 /* make unencrypted KEMAC */
284 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
285 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL,
288 /* add the key in key data */
289 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
290 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
291 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
293 gst_buffer_unmap (srtpkey, &info);
294 /* add key data to KEMAC */
295 gst_mikey_payload_kemac_add_sub (payload, pkd);
296 gst_mikey_message_add_payload (msg, payload);
298 /* now serialize this to bytes */
299 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
300 gst_mikey_message_unref (msg);
301 /* and make it into base64 */
302 data = g_bytes_get_data (bytes, &size);
303 base64 = g_base64_encode (data, size);
304 g_bytes_unref (bytes);
306 tmp = g_strdup_printf ("mikey %s", base64);
309 gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
313 /* collect all other properties and add them to fmtp or attributes */
314 fmtp = g_string_new ("");
315 g_string_append_printf (fmtp, "%d ", caps_pt);
317 n_fields = gst_structure_n_fields (s);
318 for (j = 0; j < n_fields; j++) {
319 const gchar *fname, *fval;
321 fname = gst_structure_nth_field_name (s, j);
323 /* filter out standard properties */
324 if (!strcmp (fname, "media"))
326 if (!strcmp (fname, "payload"))
328 if (!strcmp (fname, "clock-rate"))
330 if (!strcmp (fname, "encoding-name"))
332 if (!strcmp (fname, "encoding-params"))
334 if (!strcmp (fname, "ssrc"))
336 if (!strcmp (fname, "timestamp-offset"))
338 if (!strcmp (fname, "seqnum-offset"))
340 if (g_str_has_prefix (fname, "srtp-"))
342 if (g_str_has_prefix (fname, "srtcp-"))
345 if (g_str_has_prefix (fname, "x-gst-rtsp-server-rtx-time"))
348 if (!strcmp (fname, "a-framesize")) {
349 /* a-framesize attribute */
350 if ((fval = gst_structure_get_string (s, fname))) {
351 tmp = g_strdup_printf ("%d %s", caps_pt, fval);
352 gst_sdp_media_add_attribute (smedia, fname + 2, tmp);
358 if (g_str_has_prefix (fname, "a-")) {
360 if ((fval = gst_structure_get_string (s, fname)))
361 gst_sdp_media_add_attribute (smedia, fname + 2, fval);
364 if (g_str_has_prefix (fname, "x-")) {
366 if ((fval = gst_structure_get_string (s, fname)))
367 gst_sdp_media_add_attribute (smedia, fname, fval);
371 if ((fval = gst_structure_get_string (s, fname))) {
372 g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
378 tmp = g_string_free (fmtp, FALSE);
379 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
382 g_string_free (fmtp, TRUE);
385 update_sdp_from_tags (stream, smedia);
387 if ((profile == GST_RTSP_PROFILE_AVPF || profile == GST_RTSP_PROFILE_SAVPF)
388 && (rtx_time = gst_rtsp_stream_get_retransmission_time (stream))) {
389 /* ssrc multiplexed retransmit functionality */
390 guint rtx_pt = gst_rtsp_stream_get_retransmission_pt (stream);
393 g_warning ("failed to find an available dynamic payload type. "
394 "Not adding retransmission");
398 tmp = g_strdup_printf ("%d", rtx_pt);
399 gst_sdp_media_add_format (smedia, tmp);
402 tmp = g_strdup_printf ("%d rtx/%d", rtx_pt, caps_rate);
403 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
407 g_strdup_printf ("%d apt=%d;rtx-time=%" G_GINT64_FORMAT, rtx_pt,
408 caps_pt, GST_TIME_AS_MSECONDS (rtx_time));
409 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
414 gst_sdp_message_add_media (sdp, smedia);
415 gst_sdp_media_free (smedia);
422 gst_sdp_media_free (smedia);
423 g_warning ("ignoring stream %d without multicast address",
424 gst_rtsp_stream_get_index (stream));
430 * gst_rtsp_sdp_from_media:
431 * @sdp: a #GstSDPMessage
432 * @info: (transfer none): a #GstSDPInfo
433 * @media: (transfer none): a #GstRTSPMedia
435 * Add @media specific info to @sdp. @info is used to configure the connection
436 * information in the SDP.
438 * Returns: TRUE on success.
441 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
442 GstRTSPMedia * media)
447 n_streams = gst_rtsp_media_n_streams (media);
449 rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
450 if (rangestr == NULL)
453 gst_sdp_message_add_attribute (sdp, "range", rangestr);
456 for (i = 0; i < n_streams; i++) {
457 GstRTSPStream *stream;
460 GstRTSPProfile profiles;
463 stream = gst_rtsp_media_get_stream (media, i);
464 caps = gst_rtsp_stream_get_caps (stream);
467 g_warning ("ignoring stream %d without media type", i);
471 s = gst_caps_get_structure (caps, 0);
473 gst_caps_unref (caps);
474 g_warning ("ignoring stream %d without media type", i);
478 /* make a new media for each profile */
479 profiles = gst_rtsp_stream_get_profiles (stream);
481 while (profiles >= mask) {
482 GstRTSPProfile prof = profiles & mask;
485 make_media (sdp, info, media, stream, s, prof);
489 gst_caps_unref (caps);
493 GstNetTimeProvider *provider;
496 gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
498 gchar *address, *str;
501 g_object_get (provider, "clock", &clock, "address", &address, "port",
504 str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
505 g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
506 gst_clock_get_time (clock));
508 gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
510 gst_object_unref (clock);
512 gst_object_unref (provider);
521 GST_ERROR ("media %p is not prepared", media);