rtsp-sdp: Only add RTX to the SDP when using a feedback profile
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-sdp.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 /**
20  * SECTION:rtsp-sdp
21  * @short_description: Make SDP messages
22  * @see_also: #GstRTSPMedia
23  *
24  * Last reviewed on 2013-07-11 (1.0.0)
25  */
26
27 #include <string.h>
28
29 #include <gst/sdp/gstmikey.h>
30
31 #include "rtsp-sdp.h"
32
33 #define AES_128_KEY_LEN 16
34 #define AES_256_KEY_LEN 32
35
36 #define HMAC_32_KEY_LEN 4
37 #define HMAC_80_KEY_LEN 10
38
39 static gboolean
40 get_info_from_tags (GstPad * pad, GstEvent ** event, gpointer user_data)
41 {
42   GstSDPMedia *media = (GstSDPMedia *) user_data;
43
44   if (GST_EVENT_TYPE (*event) == GST_EVENT_TAG) {
45     GstTagList *tags;
46     guint bitrate = 0;
47
48     gst_event_parse_tag (*event, &tags);
49
50     if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
51       return TRUE;
52
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) ||
56           bitrate == 0)
57         return TRUE;
58
59     /* set bandwidth (kbits/s) */
60     gst_sdp_media_add_bandwidth (media, GST_SDP_BWTYPE_AS, bitrate / 1000);
61
62     return FALSE;
63
64   }
65
66   return TRUE;
67 }
68
69 static void
70 update_sdp_from_tags (GstRTSPStream * stream, GstSDPMedia * stream_media)
71 {
72   GstPad *src_pad;
73
74   src_pad = gst_rtsp_stream_get_srcpad (stream);
75
76   gst_pad_sticky_events_foreach (src_pad, get_info_from_tags, stream_media);
77
78   gst_object_unref (src_pad);
79 }
80
81 static guint8
82 enc_key_length_from_cipher_name (const gchar * cipher)
83 {
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;
88   else {
89     GST_ERROR ("encryption algorithm '%s' not supported", cipher);
90     return 0;
91   }
92 }
93
94 static guint8
95 auth_key_length_from_auth_name (const gchar * auth)
96 {
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;
101   else {
102     GST_ERROR ("authentication algorithm '%s' not supported", auth);
103     return 0;
104   }
105 }
106
107 static void
108 make_media (GstSDPMessage * sdp, GstSDPInfo * info, GstRTSPMedia * media,
109     GstRTSPStream * stream, GstStructure * s, GstRTSPProfile profile)
110 {
111   GstSDPMedia *smedia;
112   const gchar *caps_str, *caps_enc, *caps_params;
113   gchar *tmp;
114   gint caps_pt, caps_rate;
115   guint n_fields, j;
116   gboolean first;
117   GString *fmtp;
118   GstRTSPLowerTrans ltrans;
119   GSocketFamily family;
120   const gchar *addrtype, *proto;
121   gchar *address;
122   guint ttl;
123   GstClockTime rtx_time;
124
125   gst_sdp_media_new (&smedia);
126
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);
130
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);
134   g_free (tmp);
135
136   gst_sdp_media_set_port_info (smedia, 0, 1);
137
138   switch (profile) {
139     case GST_RTSP_PROFILE_AVP:
140       proto = "RTP/AVP";
141       break;
142     case GST_RTSP_PROFILE_AVPF:
143       proto = "RTP/AVPF";
144       break;
145     case GST_RTSP_PROFILE_SAVP:
146       proto = "RTP/SAVP";
147       break;
148     case GST_RTSP_PROFILE_SAVPF:
149       proto = "RTP/SAVPF";
150       break;
151     default:
152       proto = "udp";
153       break;
154   }
155   gst_sdp_media_set_proto (smedia, proto);
156
157   if (info->is_ipv6) {
158     addrtype = "IP6";
159     family = G_SOCKET_FAMILY_IPV6;
160   } else {
161     addrtype = "IP4";
162     family = G_SOCKET_FAMILY_IPV4;
163   }
164
165   ltrans = gst_rtsp_stream_get_protocols (stream);
166   if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
167     GstRTSPAddress *addr;
168
169     addr = gst_rtsp_stream_get_multicast_address (stream, family);
170     if (addr == NULL)
171       goto no_multicast;
172
173     address = g_strdup (addr->address);
174     ttl = addr->ttl;
175     gst_rtsp_address_free (addr);
176   } else {
177     ttl = 16;
178     if (info->is_ipv6)
179       address = g_strdup ("::");
180     else
181       address = g_strdup ("0.0.0.0");
182   }
183
184   /* for the c= line */
185   gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
186   g_free (address);
187
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");
192
193   if (caps_enc) {
194     if (caps_params)
195       tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
196           caps_params);
197     else
198       tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
199
200     gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
201     g_free (tmp);
202   }
203
204   /* the config uri */
205   tmp = gst_rtsp_stream_get_control (stream);
206   gst_sdp_media_add_attribute (smedia, "control", tmp);
207   g_free (tmp);
208
209
210   /* check for srtp */
211   do {
212     GstBuffer *srtpkey;
213     const GValue *val;
214     const gchar *srtpcipher, *srtpauth, *srtcpcipher, *srtcpauth;
215     GstMIKEYMessage *msg;
216     GstMIKEYPayload *payload, *pkd;
217     GBytes *bytes;
218     GstMapInfo info;
219     const guint8 *data;
220     gsize size;
221     gchar *base64;
222     guint8 byte;
223     guint32 ssrc;
224
225     val = gst_structure_get_value (s, "srtp-key");
226     if (val == NULL)
227       break;
228
229     srtpkey = gst_value_get_buffer (val);
230     if (srtpkey == NULL)
231       break;
232
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");
237
238     if (srtpcipher == NULL || srtpauth == NULL || srtcpcipher == NULL ||
239         srtcpauth == NULL)
240       break;
241
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);
253
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);
257
258     /* only AES-CM is supported */
259     byte = 1;
260     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1,
261         &byte);
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,
265         &byte);
266     /* only HMAC-SHA1 */
267     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
268         &byte);
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,
272         &byte);
273     /* we enable encryption on RTP and RTCP */
274     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
275         &byte);
276     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
277         &byte);
278     /* we enable authentication on RTP and RTCP */
279     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
280         &byte);
281     gst_mikey_message_add_payload (msg, payload);
282
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,
286         GST_MIKEY_MAC_NULL);
287
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,
292         info.data);
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);
297
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);
305
306     tmp = g_strdup_printf ("mikey %s", base64);
307     g_free (base64);
308
309     gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
310     g_free (tmp);
311   } while (FALSE);
312
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);
316   first = TRUE;
317   n_fields = gst_structure_n_fields (s);
318   for (j = 0; j < n_fields; j++) {
319     const gchar *fname, *fval;
320
321     fname = gst_structure_nth_field_name (s, j);
322
323     /* filter out standard properties */
324     if (!strcmp (fname, "media"))
325       continue;
326     if (!strcmp (fname, "payload"))
327       continue;
328     if (!strcmp (fname, "clock-rate"))
329       continue;
330     if (!strcmp (fname, "encoding-name"))
331       continue;
332     if (!strcmp (fname, "encoding-params"))
333       continue;
334     if (!strcmp (fname, "ssrc"))
335       continue;
336     if (!strcmp (fname, "timestamp-offset"))
337       continue;
338     if (!strcmp (fname, "seqnum-offset"))
339       continue;
340     if (g_str_has_prefix (fname, "srtp-"))
341       continue;
342     if (g_str_has_prefix (fname, "srtcp-"))
343       continue;
344     /* handled later */
345     if (g_str_has_prefix (fname, "x-gst-rtsp-server-rtx-time"))
346       continue;
347
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);
353         g_free (tmp);
354       }
355       continue;
356     }
357
358     if (g_str_has_prefix (fname, "a-")) {
359       /* attribute */
360       if ((fval = gst_structure_get_string (s, fname)))
361         gst_sdp_media_add_attribute (smedia, fname + 2, fval);
362       continue;
363     }
364     if (g_str_has_prefix (fname, "x-")) {
365       /* attribute */
366       if ((fval = gst_structure_get_string (s, fname)))
367         gst_sdp_media_add_attribute (smedia, fname, fval);
368       continue;
369     }
370
371     if ((fval = gst_structure_get_string (s, fname))) {
372       g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
373       first = FALSE;
374     }
375   }
376
377   if (!first) {
378     tmp = g_string_free (fmtp, FALSE);
379     gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
380     g_free (tmp);
381   } else {
382     g_string_free (fmtp, TRUE);
383   }
384
385   update_sdp_from_tags (stream, smedia);
386
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);
391
392     if (rtx_pt == 0) {
393       g_warning ("failed to find an available dynamic payload type. "
394           "Not adding retransmission");
395     } else {
396       gchar *tmp;
397
398       tmp = g_strdup_printf ("%d", rtx_pt);
399       gst_sdp_media_add_format (smedia, tmp);
400       g_free (tmp);
401
402       tmp = g_strdup_printf ("%d rtx/%d", rtx_pt, caps_rate);
403       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
404       g_free (tmp);
405
406       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);
410       g_free (tmp);
411     }
412   }
413
414   gst_sdp_message_add_media (sdp, smedia);
415   gst_sdp_media_free (smedia);
416
417   return;
418
419   /* ERRORS */
420 no_multicast:
421   {
422     gst_sdp_media_free (smedia);
423     g_warning ("ignoring stream %d without multicast address",
424         gst_rtsp_stream_get_index (stream));
425     return;
426   }
427 }
428
429 /**
430  * gst_rtsp_sdp_from_media:
431  * @sdp: a #GstSDPMessage
432  * @info: (transfer none): a #GstSDPInfo
433  * @media: (transfer none): a #GstRTSPMedia
434  *
435  * Add @media specific info to @sdp. @info is used to configure the connection
436  * information in the SDP.
437  *
438  * Returns: TRUE on success.
439  */
440 gboolean
441 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
442     GstRTSPMedia * media)
443 {
444   guint i, n_streams;
445   gchar *rangestr;
446
447   n_streams = gst_rtsp_media_n_streams (media);
448
449   rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
450   if (rangestr == NULL)
451     goto not_prepared;
452
453   gst_sdp_message_add_attribute (sdp, "range", rangestr);
454   g_free (rangestr);
455
456   for (i = 0; i < n_streams; i++) {
457     GstRTSPStream *stream;
458     GstCaps *caps;
459     GstStructure *s;
460     GstRTSPProfile profiles;
461     guint mask;
462
463     stream = gst_rtsp_media_get_stream (media, i);
464     caps = gst_rtsp_stream_get_caps (stream);
465
466     if (caps == NULL) {
467       g_warning ("ignoring stream %d without media type", i);
468       continue;
469     }
470
471     s = gst_caps_get_structure (caps, 0);
472     if (s == NULL) {
473       gst_caps_unref (caps);
474       g_warning ("ignoring stream %d without media type", i);
475       continue;
476     }
477
478     /* make a new media for each profile */
479     profiles = gst_rtsp_stream_get_profiles (stream);
480     mask = 1;
481     while (profiles >= mask) {
482       GstRTSPProfile prof = profiles & mask;
483
484       if (prof)
485         make_media (sdp, info, media, stream, s, prof);
486
487       mask <<= 1;
488     }
489     gst_caps_unref (caps);
490   }
491
492   {
493     GstNetTimeProvider *provider;
494
495     if ((provider =
496             gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
497       GstClock *clock;
498       gchar *address, *str;
499       gint port;
500
501       g_object_get (provider, "clock", &clock, "address", &address, "port",
502           &port, NULL);
503
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));
507
508       gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
509       g_free (str);
510       gst_object_unref (clock);
511       g_free (address);
512       gst_object_unref (provider);
513     }
514   }
515
516   return TRUE;
517
518   /* ERRORS */
519 not_prepared:
520   {
521     GST_ERROR ("media %p is not prepared", media);
522     return FALSE;
523   }
524 }