rtsp-stream: obtain stream position from pad
[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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
21
22 /**
23  * SECTION:rtsp-sdp
24  * @short_description: Make SDP messages
25  * @see_also: #GstRTSPMedia
26  *
27  * Last reviewed on 2013-07-11 (1.0.0)
28  */
29
30 #include <string.h>
31
32 #include <gst/net/net.h>
33 #include <gst/sdp/gstmikey.h>
34
35 #include "rtsp-sdp.h"
36
37 static gboolean
38 get_info_from_tags (GstPad * pad, GstEvent ** event, gpointer user_data)
39 {
40   GstSDPMedia *media = (GstSDPMedia *) user_data;
41
42   if (GST_EVENT_TYPE (*event) == GST_EVENT_TAG) {
43     GstTagList *tags;
44     guint bitrate = 0;
45
46     gst_event_parse_tag (*event, &tags);
47
48     if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
49       return TRUE;
50
51     if (!gst_tag_list_get_uint (tags, GST_TAG_MAXIMUM_BITRATE,
52             &bitrate) || bitrate == 0)
53       if (!gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
54           bitrate == 0)
55         return TRUE;
56
57     /* set bandwidth (kbits/s) */
58     gst_sdp_media_add_bandwidth (media, GST_SDP_BWTYPE_AS, bitrate / 1000);
59
60     return FALSE;
61
62   }
63
64   return TRUE;
65 }
66
67 static void
68 update_sdp_from_tags (GstRTSPStream * stream, GstSDPMedia * stream_media)
69 {
70   GstPad *src_pad;
71
72   src_pad = gst_rtsp_stream_get_srcpad (stream);
73
74   gst_pad_sticky_events_foreach (src_pad, get_info_from_tags, stream_media);
75
76   gst_object_unref (src_pad);
77 }
78
79 static guint
80 get_roc_from_stats (GstStructure * stats, guint ssrc)
81 {
82   const GValue *va, *v;
83   guint i, len;
84   /* initialize roc to something different than 0, so if we don't get
85      the proper ROC from the encoder, streaming should fail initially. */
86   guint roc = -1;
87
88   va = gst_structure_get_value (stats, "streams");
89   if (!va || !G_VALUE_HOLDS (va, GST_TYPE_ARRAY)) {
90     GST_WARNING ("stats doesn't have a valid 'streams' field");
91     return 0;
92   }
93
94   len = gst_value_array_get_size (va);
95
96   /* look if there's any SSRC that matches. */
97   for (i = 0; i < len; i++) {
98     GstStructure *stream;
99     v = gst_value_array_get_value (va, i);
100     if (v && (stream = g_value_get_boxed (v))) {
101       guint stream_ssrc;
102       gst_structure_get_uint (stream, "ssrc", &stream_ssrc);
103       if (stream_ssrc == ssrc) {
104         gst_structure_get_uint (stream, "roc", &roc);
105         break;
106       }
107     }
108   }
109
110   return roc;
111 }
112
113 static gboolean
114 mikey_add_crypto_sessions (GstRTSPStream * stream, GstMIKEYMessage * msg)
115 {
116   guint i;
117   GObject *session;
118   GstElement *encoder;
119   GValueArray *sources;
120   gboolean roc_found;
121
122   encoder = gst_rtsp_stream_get_srtp_encoder (stream);
123   if (encoder == NULL) {
124     GST_ERROR ("unable to get SRTP encoder from stream %p", stream);
125     return FALSE;
126   }
127
128   session = gst_rtsp_stream_get_rtpsession (stream);
129   if (session == NULL) {
130     GST_ERROR ("unable to get RTP session from stream %p", stream);
131     gst_object_unref (encoder);
132     return FALSE;
133   }
134
135   roc_found = FALSE;
136   g_object_get (session, "sources", &sources, NULL);
137   for (i = 0; sources && (i < sources->n_values); i++) {
138     GValue *val;
139     GObject *source;
140     guint32 ssrc;
141     gboolean is_sender;
142
143     val = g_value_array_get_nth (sources, i);
144     source = (GObject *) g_value_get_object (val);
145
146     g_object_get (source, "ssrc", &ssrc, "is-sender", &is_sender, NULL);
147
148     if (is_sender) {
149       guint32 roc = -1;
150       GstStructure *stats;
151
152       g_object_get (encoder, "stats", &stats, NULL);
153
154       if (stats) {
155         roc = get_roc_from_stats (stats, ssrc);
156         gst_structure_free (stats);
157       }
158
159       roc_found = ! !(roc != -1);
160       if (!roc_found) {
161         GST_ERROR ("unable to obtain ROC for stream %p with SSRC %u",
162             stream, ssrc);
163         goto cleanup;
164       }
165
166       GST_INFO ("stream %p with SSRC %u has a ROC of %u", stream, ssrc, roc);
167
168       gst_mikey_message_add_cs_srtp (msg, 0, ssrc, roc);
169     }
170   }
171
172 cleanup:
173   {
174     g_value_array_free (sources);
175
176     gst_object_unref (encoder);
177     g_object_unref (session);
178     return roc_found;
179   }
180 }
181
182 static gboolean
183 make_media (GstSDPMessage * sdp, GstSDPInfo * info,
184     GstRTSPStream * stream, GstCaps * caps, GstRTSPProfile profile)
185 {
186   GstSDPMedia *smedia;
187   gchar *tmp;
188   GstRTSPLowerTrans ltrans;
189   GSocketFamily family;
190   const gchar *addrtype, *proto;
191   gchar *address;
192   guint ttl;
193   GstClockTime rtx_time;
194   gchar *base64;
195   GstMIKEYMessage *mikey_msg;
196
197   gst_sdp_media_new (&smedia);
198
199   if (gst_sdp_media_set_media_from_caps (caps, smedia) != GST_SDP_OK) {
200     goto caps_error;
201   }
202
203   gst_sdp_media_set_port_info (smedia, 0, 1);
204
205   switch (profile) {
206     case GST_RTSP_PROFILE_AVP:
207       proto = "RTP/AVP";
208       break;
209     case GST_RTSP_PROFILE_AVPF:
210       proto = "RTP/AVPF";
211       break;
212     case GST_RTSP_PROFILE_SAVP:
213       proto = "RTP/SAVP";
214       break;
215     case GST_RTSP_PROFILE_SAVPF:
216       proto = "RTP/SAVPF";
217       break;
218     default:
219       proto = "udp";
220       break;
221   }
222   gst_sdp_media_set_proto (smedia, proto);
223
224   if (info->is_ipv6) {
225     addrtype = "IP6";
226     family = G_SOCKET_FAMILY_IPV6;
227   } else {
228     addrtype = "IP4";
229     family = G_SOCKET_FAMILY_IPV4;
230   }
231
232   ltrans = gst_rtsp_stream_get_protocols (stream);
233   if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
234     GstRTSPAddress *addr;
235
236     addr = gst_rtsp_stream_get_multicast_address (stream, family);
237     if (addr == NULL)
238       goto no_multicast;
239
240     address = g_strdup (addr->address);
241     ttl = addr->ttl;
242     gst_rtsp_address_free (addr);
243   } else {
244     ttl = 16;
245     if (info->is_ipv6)
246       address = g_strdup ("::");
247     else
248       address = g_strdup ("0.0.0.0");
249   }
250
251   /* for the c= line */
252   gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
253   g_free (address);
254
255   /* the config uri */
256   tmp = gst_rtsp_stream_get_control (stream);
257   gst_sdp_media_add_attribute (smedia, "control", tmp);
258   g_free (tmp);
259
260   /* check for srtp */
261   mikey_msg = gst_mikey_message_new_from_caps (caps);
262   if (mikey_msg) {
263     /* add policy '0' for all sending SSRC */
264     if (!mikey_add_crypto_sessions (stream, mikey_msg))
265       goto crypto_sessions_error;
266
267     base64 = gst_mikey_message_base64_encode (mikey_msg);
268     if (base64) {
269       tmp = g_strdup_printf ("mikey %s", base64);
270       g_free (base64);
271       gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
272       g_free (tmp);
273     }
274
275     gst_mikey_message_unref (mikey_msg);
276   }
277
278   /* RFC 7273 clock signalling */
279   {
280     GstBin *joined_bin = gst_rtsp_stream_get_joined_bin (stream);
281     GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (joined_bin));
282     gchar *ts_refclk = NULL;
283     gchar *mediaclk = NULL;
284     guint rtptime, clock_rate;
285     GstClockTime running_time, base_time, clock_time;
286     GstRTSPPublishClockMode publish_clock_mode =
287         gst_rtsp_stream_get_publish_clock_mode (stream);
288
289     gst_rtsp_stream_get_rtpinfo (stream, &rtptime, NULL, &clock_rate,
290         &running_time);
291     base_time = gst_element_get_base_time (GST_ELEMENT_CAST (joined_bin));
292     g_assert (base_time != GST_CLOCK_TIME_NONE);
293     clock_time = running_time + base_time;
294
295     if (publish_clock_mode != GST_RTSP_PUBLISH_CLOCK_MODE_NONE && clock) {
296       if (GST_IS_NTP_CLOCK (clock) || GST_IS_PTP_CLOCK (clock)) {
297         if (publish_clock_mode == GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK_AND_OFFSET) {
298           guint32 mediaclk_offset;
299
300           /* Calculate RTP time at the clock's epoch. That's the direct offset */
301           clock_time =
302               gst_util_uint64_scale (clock_time, clock_rate, GST_SECOND);
303
304           clock_time &= 0xffffffff;
305           mediaclk_offset =
306               G_GUINT64_CONSTANT (0xffffffff) + rtptime - clock_time;
307           mediaclk = g_strdup_printf ("direct=%u", (guint32) mediaclk_offset);
308         }
309
310         if (GST_IS_NTP_CLOCK (clock)) {
311           gchar *ntp_address;
312           guint ntp_port;
313
314           g_object_get (clock, "address", &ntp_address, "port", &ntp_port,
315               NULL);
316
317           if (ntp_port == 123)
318             ts_refclk = g_strdup_printf ("ntp=%s", ntp_address);
319           else
320             ts_refclk = g_strdup_printf ("ntp=%s:%u", ntp_address, ntp_port);
321
322           g_free (ntp_address);
323         } else {
324           guint64 ptp_clock_id;
325           guint ptp_domain;
326
327           g_object_get (clock, "grandmaster-clock-id", &ptp_clock_id, "domain",
328               &ptp_domain, NULL);
329
330           if (ptp_domain != 0)
331             ts_refclk =
332                 g_strdup_printf
333                 ("ptp=IEEE1588-2008:%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X:%u",
334                 (guint) (ptp_clock_id >> 56) & 0xff,
335                 (guint) (ptp_clock_id >> 48) & 0xff,
336                 (guint) (ptp_clock_id >> 40) & 0xff,
337                 (guint) (ptp_clock_id >> 32) & 0xff,
338                 (guint) (ptp_clock_id >> 24) & 0xff,
339                 (guint) (ptp_clock_id >> 16) & 0xff,
340                 (guint) (ptp_clock_id >> 8) & 0xff,
341                 (guint) (ptp_clock_id >> 0) & 0xff, ptp_domain);
342           else
343             ts_refclk =
344                 g_strdup_printf
345                 ("ptp=IEEE1588-2008:%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
346                 (guint) (ptp_clock_id >> 56) & 0xff,
347                 (guint) (ptp_clock_id >> 48) & 0xff,
348                 (guint) (ptp_clock_id >> 40) & 0xff,
349                 (guint) (ptp_clock_id >> 32) & 0xff,
350                 (guint) (ptp_clock_id >> 24) & 0xff,
351                 (guint) (ptp_clock_id >> 16) & 0xff,
352                 (guint) (ptp_clock_id >> 8) & 0xff,
353                 (guint) (ptp_clock_id >> 0) & 0xff);
354         }
355       }
356     }
357     if (clock)
358       gst_object_unref (clock);
359
360     if (!ts_refclk)
361       ts_refclk = g_strdup ("local");
362     if (!mediaclk)
363       mediaclk = g_strdup ("sender");
364
365     gst_sdp_media_add_attribute (smedia, "ts-refclk", ts_refclk);
366     gst_sdp_media_add_attribute (smedia, "mediaclk", mediaclk);
367     g_free (ts_refclk);
368     g_free (mediaclk);
369     gst_object_unref (joined_bin);
370   }
371
372   update_sdp_from_tags (stream, smedia);
373
374   if ((profile == GST_RTSP_PROFILE_AVPF || profile == GST_RTSP_PROFILE_SAVPF)
375       && (rtx_time = gst_rtsp_stream_get_retransmission_time (stream))) {
376     /* ssrc multiplexed retransmit functionality */
377     guint rtx_pt = gst_rtsp_stream_get_retransmission_pt (stream);
378
379     if (rtx_pt == 0) {
380       g_warning ("failed to find an available dynamic payload type. "
381           "Not adding retransmission");
382     } else {
383       gchar *tmp;
384       GstStructure *s;
385       gint caps_pt, caps_rate;
386
387       s = gst_caps_get_structure (caps, 0);
388       if (s == NULL)
389         goto no_caps_info;
390
391       /* get payload type and clock rate */
392       gst_structure_get_int (s, "payload", &caps_pt);
393       gst_structure_get_int (s, "clock-rate", &caps_rate);
394
395       tmp = g_strdup_printf ("%d", rtx_pt);
396       gst_sdp_media_add_format (smedia, tmp);
397       g_free (tmp);
398
399       tmp = g_strdup_printf ("%d rtx/%d", rtx_pt, caps_rate);
400       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
401       g_free (tmp);
402
403       tmp =
404           g_strdup_printf ("%d apt=%d;rtx-time=%" G_GINT64_FORMAT, rtx_pt,
405           caps_pt, GST_TIME_AS_MSECONDS (rtx_time));
406       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
407       g_free (tmp);
408     }
409   }
410
411   gst_sdp_message_add_media (sdp, smedia);
412   gst_sdp_media_free (smedia);
413
414   return TRUE;
415
416   /* ERRORS */
417 caps_error:
418   {
419     gst_sdp_media_free (smedia);
420     GST_ERROR ("unable to set media from caps for stream %d",
421         gst_rtsp_stream_get_index (stream));
422     return FALSE;
423   }
424 no_multicast:
425   {
426     gst_sdp_media_free (smedia);
427     GST_ERROR ("stream %d has no multicast address",
428         gst_rtsp_stream_get_index (stream));
429     return FALSE;
430   }
431 no_caps_info:
432   {
433     gst_sdp_media_free (smedia);
434     GST_ERROR ("caps for stream %d have no structure",
435         gst_rtsp_stream_get_index (stream));
436     return FALSE;
437   }
438 crypto_sessions_error:
439   {
440     gst_sdp_media_free (smedia);
441     GST_ERROR ("unable to add MIKEY crypto sessions for stream %d",
442         gst_rtsp_stream_get_index (stream));
443     return FALSE;
444   }
445 }
446
447 /**
448  * gst_rtsp_sdp_from_media:
449  * @sdp: a #GstSDPMessage
450  * @info: (transfer none): a #GstSDPInfo
451  * @media: (transfer none): a #GstRTSPMedia
452  *
453  * Add @media specific info to @sdp. @info is used to configure the connection
454  * information in the SDP.
455  *
456  * Returns: TRUE on success.
457  */
458 gboolean
459 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
460     GstRTSPMedia * media)
461 {
462   guint i, n_streams;
463   gchar *rangestr;
464   gboolean res;
465
466   n_streams = gst_rtsp_media_n_streams (media);
467
468   rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
469   if (rangestr == NULL)
470     goto not_prepared;
471
472   gst_sdp_message_add_attribute (sdp, "range", rangestr);
473   g_free (rangestr);
474
475   res = TRUE;
476   for (i = 0; res && (i < n_streams); i++) {
477     GstRTSPStream *stream;
478
479     stream = gst_rtsp_media_get_stream (media, i);
480     res = gst_rtsp_sdp_from_stream (sdp, info, stream);
481     if (!res) {
482       GST_ERROR ("could not get SDP from stream %p", stream);
483       goto sdp_error;
484     }
485   }
486
487   {
488     GstNetTimeProvider *provider;
489
490     if ((provider =
491             gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
492       GstClock *clock;
493       gchar *address, *str;
494       gint port;
495
496       g_object_get (provider, "clock", &clock, "address", &address, "port",
497           &port, NULL);
498
499       str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
500           g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
501           gst_clock_get_time (clock));
502
503       gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
504       g_free (str);
505       gst_object_unref (clock);
506       g_free (address);
507       gst_object_unref (provider);
508     }
509   }
510
511   return res;
512
513   /* ERRORS */
514 not_prepared:
515   {
516     GST_ERROR ("media %p is not prepared", media);
517     return FALSE;
518   }
519 sdp_error:
520   {
521     GST_ERROR ("could not get SDP from media %p", media);
522     return FALSE;
523   }
524 }
525
526 /**
527  * gst_rtsp_sdp_from_stream:
528  * @sdp: a #GstSDPMessage
529  * @info: (transfer none): a #GstSDPInfo
530  * @stream: (transfer none): a #GstRTSPStream
531  *
532  * Add info from @stream to @sdp.
533  *
534  * Returns: TRUE on success.
535  */
536 gboolean
537 gst_rtsp_sdp_from_stream (GstSDPMessage * sdp, GstSDPInfo * info,
538     GstRTSPStream * stream)
539 {
540   GstCaps *caps;
541   GstRTSPProfile profiles;
542   guint mask;
543   gboolean res;
544
545   caps = gst_rtsp_stream_get_caps (stream);
546
547   if (caps == NULL) {
548     GST_ERROR ("stream %p has no caps", stream);
549     return FALSE;
550   }
551
552   /* make a new media for each profile */
553   profiles = gst_rtsp_stream_get_profiles (stream);
554   mask = 1;
555   res = TRUE;
556   while (res && (profiles >= mask)) {
557     GstRTSPProfile prof = profiles & mask;
558
559     if (prof)
560       res = make_media (sdp, info, stream, caps, prof);
561
562     mask <<= 1;
563   }
564   gst_caps_unref (caps);
565
566   return res;
567 }