Initial release including wifi display based on gst-rtsp-server-1.4.1
[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
124   gst_sdp_media_new (&smedia);
125
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);
129
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);
133   g_free (tmp);
134
135   gst_sdp_media_set_port_info (smedia, 0, 1);
136
137   switch (profile) {
138     case GST_RTSP_PROFILE_AVP:
139       proto = "RTP/AVP";
140       break;
141     case GST_RTSP_PROFILE_AVPF:
142       proto = "RTP/AVPF";
143       break;
144     case GST_RTSP_PROFILE_SAVP:
145       proto = "RTP/SAVP";
146       break;
147     case GST_RTSP_PROFILE_SAVPF:
148       proto = "RTP/SAVPF";
149       break;
150     default:
151       proto = "udp";
152       break;
153   }
154   gst_sdp_media_set_proto (smedia, proto);
155
156   if (info->is_ipv6) {
157     addrtype = "IP6";
158     family = G_SOCKET_FAMILY_IPV6;
159   } else {
160     addrtype = "IP4";
161     family = G_SOCKET_FAMILY_IPV4;
162   }
163
164   ltrans = gst_rtsp_stream_get_protocols (stream);
165   if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
166     GstRTSPAddress *addr;
167
168     addr = gst_rtsp_stream_get_multicast_address (stream, family);
169     if (addr == NULL)
170       goto no_multicast;
171
172     address = g_strdup (addr->address);
173     ttl = addr->ttl;
174     gst_rtsp_address_free (addr);
175   } else {
176     ttl = 16;
177     if (info->is_ipv6)
178       address = g_strdup ("::");
179     else
180       address = g_strdup ("0.0.0.0");
181   }
182
183   /* for the c= line */
184   gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
185   g_free (address);
186
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");
191
192   if (caps_enc) {
193     if (caps_params)
194       tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
195           caps_params);
196     else
197       tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
198
199     gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
200     g_free (tmp);
201   }
202
203   /* the config uri */
204   tmp = gst_rtsp_stream_get_control (stream);
205   gst_sdp_media_add_attribute (smedia, "control", tmp);
206   g_free (tmp);
207
208
209   /* check for srtp */
210   do {
211     GstBuffer *srtpkey;
212     const GValue *val;
213     const gchar *srtpcipher, *srtpauth, *srtcpcipher, *srtcpauth;
214     GstMIKEYMessage *msg;
215     GstMIKEYPayload *payload, *pkd;
216     GBytes *bytes;
217     GstMapInfo info;
218     const guint8 *data;
219     gsize size;
220     gchar *base64;
221     guint8 byte;
222     guint32 ssrc;
223
224     val = gst_structure_get_value (s, "srtp-key");
225     if (val == NULL)
226       break;
227
228     srtpkey = gst_value_get_buffer (val);
229     if (srtpkey == NULL)
230       break;
231
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");
236
237     if (srtpcipher == NULL || srtpauth == NULL || srtcpcipher == NULL ||
238         srtcpauth == NULL)
239       break;
240
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);
252
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);
256
257     /* only AES-CM is supported */
258     byte = 1;
259     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1,
260         &byte);
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,
264         &byte);
265     /* only HMAC-SHA1 */
266     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
267         &byte);
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,
271         &byte);
272     /* we enable encryption on RTP and RTCP */
273     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
274         &byte);
275     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
276         &byte);
277     /* we enable authentication on RTP and RTCP */
278     gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
279         &byte);
280     gst_mikey_message_add_payload (msg, payload);
281
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,
285         GST_MIKEY_MAC_NULL);
286
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,
291         info.data);
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);
296
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);
304
305     tmp = g_strdup_printf ("mikey %s", base64);
306     g_free (base64);
307
308     gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
309     g_free (tmp);
310   } while (FALSE);
311
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);
315   first = TRUE;
316   n_fields = gst_structure_n_fields (s);
317   for (j = 0; j < n_fields; j++) {
318     const gchar *fname, *fval;
319
320     fname = gst_structure_nth_field_name (s, j);
321
322     /* filter out standard properties */
323     if (!strcmp (fname, "media"))
324       continue;
325     if (!strcmp (fname, "payload"))
326       continue;
327     if (!strcmp (fname, "clock-rate"))
328       continue;
329     if (!strcmp (fname, "encoding-name"))
330       continue;
331     if (!strcmp (fname, "encoding-params"))
332       continue;
333     if (!strcmp (fname, "ssrc"))
334       continue;
335     if (!strcmp (fname, "clock-base"))
336       continue;
337     if (!strcmp (fname, "seqnum-base"))
338       continue;
339     if (g_str_has_prefix (fname, "srtp-"))
340       continue;
341     if (g_str_has_prefix (fname, "srtcp-"))
342       continue;
343
344     if (g_str_has_prefix (fname, "a-")) {
345       /* attribute */
346       if ((fval = gst_structure_get_string (s, fname)))
347         gst_sdp_media_add_attribute (smedia, fname + 2, fval);
348       continue;
349     }
350     if (g_str_has_prefix (fname, "x-")) {
351       /* attribute */
352       if ((fval = gst_structure_get_string (s, fname)))
353         gst_sdp_media_add_attribute (smedia, fname, fval);
354       continue;
355     }
356
357     if ((fval = gst_structure_get_string (s, fname))) {
358       g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
359       first = FALSE;
360     }
361   }
362   if (!first) {
363     tmp = g_string_free (fmtp, FALSE);
364     gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
365     g_free (tmp);
366   } else {
367     g_string_free (fmtp, TRUE);
368   }
369
370   update_sdp_from_tags (stream, smedia);
371
372   gst_sdp_message_add_media (sdp, smedia);
373   gst_sdp_media_free (smedia);
374
375   return;
376
377   /* ERRORS */
378 no_multicast:
379   {
380     gst_sdp_media_free (smedia);
381     g_warning ("ignoring stream %d without multicast address",
382         gst_rtsp_stream_get_index (stream));
383     return;
384   }
385 }
386
387 /**
388  * gst_rtsp_sdp_from_media:
389  * @sdp: a #GstSDPMessage
390  * @info: (transfer none): a #GstSDPInfo
391  * @media: (transfer none): a #GstRTSPMedia
392  *
393  * Add @media specific info to @sdp. @info is used to configure the connection
394  * information in the SDP.
395  *
396  * Returns: TRUE on success.
397  */
398 gboolean
399 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
400     GstRTSPMedia * media)
401 {
402   guint i, n_streams;
403   gchar *rangestr;
404
405   n_streams = gst_rtsp_media_n_streams (media);
406
407   rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
408   if (rangestr == NULL)
409     goto not_prepared;
410
411   gst_sdp_message_add_attribute (sdp, "range", rangestr);
412   g_free (rangestr);
413
414   for (i = 0; i < n_streams; i++) {
415     GstRTSPStream *stream;
416     GstCaps *caps;
417     GstStructure *s;
418     GstRTSPProfile profiles;
419     guint mask;
420
421     stream = gst_rtsp_media_get_stream (media, i);
422     caps = gst_rtsp_stream_get_caps (stream);
423
424     if (caps == NULL) {
425       g_warning ("ignoring stream %d without media type", i);
426       continue;
427     }
428
429     s = gst_caps_get_structure (caps, 0);
430     if (s == NULL) {
431       gst_caps_unref (caps);
432       g_warning ("ignoring stream %d without media type", i);
433       continue;
434     }
435
436     /* make a new media for each profile */
437     profiles = gst_rtsp_stream_get_profiles (stream);
438     mask = 1;
439     while (profiles >= mask) {
440       GstRTSPProfile prof = profiles & mask;
441
442       if (prof)
443         make_media (sdp, info, media, stream, s, prof);
444
445       mask <<= 1;
446     }
447     gst_caps_unref (caps);
448   }
449
450   {
451     GstNetTimeProvider *provider;
452
453     if ((provider =
454             gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
455       GstClock *clock;
456       gchar *address, *str;
457       gint port;
458
459       g_object_get (provider, "clock", &clock, "address", &address, "port",
460           &port, NULL);
461
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));
465
466       gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
467       g_free (str);
468       gst_object_unref (clock);
469       g_free (address);
470       gst_object_unref (provider);
471     }
472   }
473
474   return TRUE;
475
476   /* ERRORS */
477 not_prepared:
478   {
479     GST_ERROR ("media %p is not prepared", media);
480     return FALSE;
481   }
482 }