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>
34 get_info_from_tags (GstPad * pad, GstEvent ** event, gpointer user_data)
36 GstSDPMedia *media = (GstSDPMedia *) user_data;
38 if (GST_EVENT_TYPE (*event) == GST_EVENT_TAG) {
42 gst_event_parse_tag (*event, &tags);
44 if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
47 if (!gst_tag_list_get_uint (tags, GST_TAG_MAXIMUM_BITRATE,
48 &bitrate) || bitrate == 0)
49 if (!gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
53 /* set bandwidth (kbits/s) */
54 gst_sdp_media_add_bandwidth (media, GST_SDP_BWTYPE_AS, bitrate / 1000);
64 update_sdp_from_tags (GstRTSPStream * stream, GstSDPMedia * stream_media)
68 src_pad = gst_rtsp_stream_get_srcpad (stream);
70 gst_pad_sticky_events_foreach (src_pad, get_info_from_tags, stream_media);
72 gst_object_unref (src_pad);
76 make_media (GstSDPMessage * sdp, GstSDPInfo * info, GstRTSPMedia * media,
77 GstRTSPStream * stream, GstStructure * s, GstRTSPProfile profile)
80 const gchar *caps_str, *caps_enc, *caps_params;
82 gint caps_pt, caps_rate;
86 GstRTSPLowerTrans ltrans;
88 const gchar *addrtype, *proto;
92 gst_sdp_media_new (&smedia);
94 /* get media type and payload for the m= line */
95 caps_str = gst_structure_get_string (s, "media");
96 gst_sdp_media_set_media (smedia, caps_str);
98 gst_structure_get_int (s, "payload", &caps_pt);
99 tmp = g_strdup_printf ("%d", caps_pt);
100 gst_sdp_media_add_format (smedia, tmp);
103 gst_sdp_media_set_port_info (smedia, 0, 1);
106 case GST_RTSP_PROFILE_AVP:
109 case GST_RTSP_PROFILE_AVPF:
112 case GST_RTSP_PROFILE_SAVP:
115 case GST_RTSP_PROFILE_SAVPF:
122 gst_sdp_media_set_proto (smedia, proto);
126 family = G_SOCKET_FAMILY_IPV6;
129 family = G_SOCKET_FAMILY_IPV4;
132 ltrans = gst_rtsp_stream_get_protocols (stream);
133 if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
134 GstRTSPAddress *addr;
136 addr = gst_rtsp_stream_get_multicast_address (stream, family);
140 address = g_strdup (addr->address);
142 gst_rtsp_address_free (addr);
146 address = g_strdup ("::");
148 address = g_strdup ("0.0.0.0");
151 /* for the c= line */
152 gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
155 /* get clock-rate, media type and params for the rtpmap attribute */
156 gst_structure_get_int (s, "clock-rate", &caps_rate);
157 caps_enc = gst_structure_get_string (s, "encoding-name");
158 caps_params = gst_structure_get_string (s, "encoding-params");
162 tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
165 tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
167 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
172 tmp = gst_rtsp_stream_get_control (stream);
173 gst_sdp_media_add_attribute (smedia, "control", tmp);
181 const gchar *srtpcipher, *srtpauth, *srtcpcipher, *srtcpauth;
182 GstMIKEYMessage *msg;
183 GstMIKEYPayload *payload, *pkd;
192 val = gst_structure_get_value (s, "srtp-key");
196 srtpkey = gst_value_get_buffer (val);
200 srtpcipher = gst_structure_get_string (s, "srtp-cipher");
201 srtpauth = gst_structure_get_string (s, "srtp-auth");
202 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
203 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
205 if (srtpcipher == NULL || srtpauth == NULL || srtcpcipher == NULL ||
209 msg = gst_mikey_message_new ();
210 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
211 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
212 FALSE, GST_MIKEY_PRF_MIKEY_1, 0, GST_MIKEY_MAP_TYPE_SRTP);
213 /* add policy '0' for our SSRC */
214 gst_rtsp_stream_get_ssrc (stream, &ssrc);
215 gst_mikey_message_add_cs_srtp (msg, 0, ssrc, 0);
216 /* timestamp is now */
217 gst_mikey_message_add_t_now_ntp_utc (msg);
218 /* add some random data */
219 gst_mikey_message_add_rand_len (msg, 16);
221 /* the policy '0' is SRTP with the above discovered algorithms */
222 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
223 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
225 /* only AES-CM is supported */
227 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1,
230 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
232 /* we enable encryption on RTP and RTCP */
233 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
235 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
237 /* we enable authentication on RTP and RTCP */
238 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
240 gst_mikey_message_add_payload (msg, payload);
242 /* make unencrypted KEMAC */
243 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
244 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL,
247 /* add the key in key data */
248 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
249 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
250 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
252 gst_buffer_unmap (srtpkey, &info);
253 /* add key data to KEMAC */
254 gst_mikey_payload_kemac_add_sub (payload, pkd);
255 gst_mikey_message_add_payload (msg, payload);
257 /* now serialize this to bytes */
258 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
259 gst_mikey_message_free (msg);
260 /* and make it into base64 */
261 data = g_bytes_get_data (bytes, &size);
262 base64 = g_base64_encode (data, size);
263 g_bytes_unref (bytes);
265 tmp = g_strdup_printf ("mikey %s", base64);
268 gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
272 /* collect all other properties and add them to fmtp or attributes */
273 fmtp = g_string_new ("");
274 g_string_append_printf (fmtp, "%d ", caps_pt);
276 n_fields = gst_structure_n_fields (s);
277 for (j = 0; j < n_fields; j++) {
278 const gchar *fname, *fval;
280 fname = gst_structure_nth_field_name (s, j);
282 /* filter out standard properties */
283 if (!strcmp (fname, "media"))
285 if (!strcmp (fname, "payload"))
287 if (!strcmp (fname, "clock-rate"))
289 if (!strcmp (fname, "encoding-name"))
291 if (!strcmp (fname, "encoding-params"))
293 if (!strcmp (fname, "ssrc"))
295 if (!strcmp (fname, "clock-base"))
297 if (!strcmp (fname, "seqnum-base"))
299 if (g_str_has_prefix (fname, "srtp-"))
301 if (g_str_has_prefix (fname, "srtcp-"))
304 if (g_str_has_prefix (fname, "a-")) {
306 if ((fval = gst_structure_get_string (s, fname)))
307 gst_sdp_media_add_attribute (smedia, fname + 2, fval);
310 if (g_str_has_prefix (fname, "x-")) {
312 if ((fval = gst_structure_get_string (s, fname)))
313 gst_sdp_media_add_attribute (smedia, fname, fval);
317 if ((fval = gst_structure_get_string (s, fname))) {
318 g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
323 tmp = g_string_free (fmtp, FALSE);
324 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
327 g_string_free (fmtp, TRUE);
330 update_sdp_from_tags (stream, smedia);
332 gst_sdp_message_add_media (sdp, smedia);
333 gst_sdp_media_free (smedia);
340 gst_sdp_media_free (smedia);
341 g_warning ("ignoring stream %d without multicast address",
342 gst_rtsp_stream_get_index (stream));
348 * gst_rtsp_sdp_from_media:
349 * @sdp: a #GstSDPMessage
350 * @info: (transfer none): a #GstSDPInfo
351 * @media: (transfer none): a #GstRTSPMedia
353 * Add @media specific info to @sdp. @info is used to configure the connection
354 * information in the SDP.
356 * Returns: TRUE on success.
359 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
360 GstRTSPMedia * media)
365 n_streams = gst_rtsp_media_n_streams (media);
367 rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
368 if (rangestr == NULL)
371 gst_sdp_message_add_attribute (sdp, "range", rangestr);
374 for (i = 0; i < n_streams; i++) {
375 GstRTSPStream *stream;
378 GstRTSPProfile profiles;
381 stream = gst_rtsp_media_get_stream (media, i);
382 caps = gst_rtsp_stream_get_caps (stream);
385 g_warning ("ignoring stream %d without media type", i);
389 s = gst_caps_get_structure (caps, 0);
391 gst_caps_unref (caps);
392 g_warning ("ignoring stream %d without media type", i);
396 /* make a new media for each profile */
397 profiles = gst_rtsp_stream_get_profiles (stream);
399 while (profiles >= mask) {
400 GstRTSPProfile prof = profiles & mask;
403 make_media (sdp, info, media, stream, s, prof);
407 gst_caps_unref (caps);
411 GstNetTimeProvider *provider;
414 gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
416 gchar *address, *str;
419 g_object_get (provider, "clock", &clock, "address", &address, "port",
422 str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
423 g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
424 gst_clock_get_time (clock));
426 gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
428 gst_object_unref (clock);
430 gst_object_unref (provider);
439 GST_ERROR ("media %p is not prepared", media);