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.
20 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24 * @short_description: Make SDP messages
25 * @see_also: #GstRTSPMedia
27 * Last reviewed on 2013-07-11 (1.0.0)
32 #include <gst/net/net.h>
33 #include <gst/sdp/gstmikey.h>
38 get_info_from_tags (GstPad * pad, GstEvent ** event, gpointer user_data)
40 GstSDPMedia *media = (GstSDPMedia *) user_data;
42 if (GST_EVENT_TYPE (*event) == GST_EVENT_TAG) {
46 gst_event_parse_tag (*event, &tags);
48 if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
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) ||
57 /* set bandwidth (kbits/s) */
58 gst_sdp_media_add_bandwidth (media, GST_SDP_BWTYPE_AS, bitrate / 1000);
68 update_sdp_from_tags (GstRTSPStream * stream, GstSDPMedia * stream_media)
72 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 get_roc_from_stats (GstStructure * stats, guint ssrc)
86 /* initialize roc to something different than 0, so if we don't get
87 the proper ROC from the encoder, streaming should fail initially. */
90 va = gst_structure_get_value (stats, "streams");
91 if (!va || !G_VALUE_HOLDS (va, GST_TYPE_ARRAY)) {
92 GST_WARNING ("stats doesn't have a valid 'streams' field");
96 len = gst_value_array_get_size (va);
98 /* look if there's any SSRC that matches. */
99 for (i = 0; i < len; i++) {
100 GstStructure *stream;
101 v = gst_value_array_get_value (va, i);
102 if (v && (stream = g_value_get_boxed (v))) {
104 gst_structure_get_uint (stream, "ssrc", &stream_ssrc);
105 if (stream_ssrc == ssrc) {
106 gst_structure_get_uint (stream, "roc", &roc);
116 mikey_add_crypto_sessions (GstRTSPStream * stream, GstMIKEYMessage * msg)
121 GValueArray *sources;
124 encoder = gst_rtsp_stream_get_srtp_encoder (stream);
125 if (encoder == NULL) {
126 GST_ERROR ("unable to get SRTP encoder from stream %p", stream);
130 session = gst_rtsp_stream_get_rtpsession (stream);
131 if (session == NULL) {
132 GST_ERROR ("unable to get RTP session from stream %p", stream);
133 gst_object_unref (encoder);
138 g_object_get (session, "sources", &sources, NULL);
139 for (i = 0; sources && (i < sources->n_values); i++) {
145 val = g_value_array_get_nth (sources, i);
146 source = (GObject *) g_value_get_object (val);
148 g_object_get (source, "ssrc", &ssrc, "is-sender", &is_sender, NULL);
154 g_object_get (encoder, "stats", &stats, NULL);
157 roc = get_roc_from_stats (stats, ssrc);
158 gst_structure_free (stats);
161 roc_found = ! !(roc != -1);
163 GST_ERROR ("unable to obtain ROC for stream %p with SSRC %u",
168 GST_INFO ("stream %p with SSRC %u has a ROC of %u", stream, ssrc, roc);
170 gst_mikey_message_add_cs_srtp (msg, 0, ssrc, roc);
176 g_value_array_free (sources);
178 gst_object_unref (encoder);
179 g_object_unref (session);
185 gst_rtsp_sdp_make_media (GstSDPMessage * sdp, GstSDPInfo * info,
186 GstRTSPStream * stream, GstCaps * caps, GstRTSPProfile profile)
190 GstRTSPLowerTrans ltrans;
191 GSocketFamily family;
192 const gchar *addrtype, *proto;
195 GstClockTime rtx_time;
197 GstMIKEYMessage *mikey_msg;
199 gst_sdp_media_new (&smedia);
201 if (gst_sdp_media_set_media_from_caps (caps, smedia) != GST_SDP_OK) {
205 gst_sdp_media_set_port_info (smedia, 0, 1);
208 case GST_RTSP_PROFILE_AVP:
211 case GST_RTSP_PROFILE_AVPF:
214 case GST_RTSP_PROFILE_SAVP:
217 case GST_RTSP_PROFILE_SAVPF:
224 gst_sdp_media_set_proto (smedia, proto);
228 family = G_SOCKET_FAMILY_IPV6;
231 family = G_SOCKET_FAMILY_IPV4;
234 ltrans = gst_rtsp_stream_get_protocols (stream);
235 if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
236 GstRTSPAddress *addr;
238 addr = gst_rtsp_stream_get_multicast_address (stream, family);
242 address = g_strdup (addr->address);
244 gst_rtsp_address_free (addr);
248 address = g_strdup ("::");
250 address = g_strdup ("0.0.0.0");
253 /* for the c= line */
254 gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
258 tmp = gst_rtsp_stream_get_control (stream);
259 gst_sdp_media_add_attribute (smedia, "control", tmp);
263 mikey_msg = gst_mikey_message_new_from_caps (caps);
265 /* add policy '0' for all sending SSRC */
266 if (!mikey_add_crypto_sessions (stream, mikey_msg))
267 goto crypto_sessions_error;
269 base64 = gst_mikey_message_base64_encode (mikey_msg);
271 tmp = g_strdup_printf ("mikey %s", base64);
273 gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
277 gst_mikey_message_unref (mikey_msg);
280 /* RFC 7273 clock signalling */
282 GstBin *joined_bin = gst_rtsp_stream_get_joined_bin (stream);
283 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (joined_bin));
284 gchar *ts_refclk = NULL;
285 gchar *mediaclk = NULL;
286 guint rtptime, clock_rate;
287 GstClockTime running_time, base_time, clock_time;
288 GstRTSPPublishClockMode publish_clock_mode =
289 gst_rtsp_stream_get_publish_clock_mode (stream);
291 gst_rtsp_stream_get_rtpinfo (stream, &rtptime, NULL, &clock_rate,
293 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (joined_bin));
294 g_assert (base_time != GST_CLOCK_TIME_NONE);
295 clock_time = running_time + base_time;
297 if (publish_clock_mode != GST_RTSP_PUBLISH_CLOCK_MODE_NONE && clock) {
298 if (GST_IS_NTP_CLOCK (clock) || GST_IS_PTP_CLOCK (clock)) {
299 if (publish_clock_mode == GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK_AND_OFFSET) {
300 guint32 mediaclk_offset;
302 /* Calculate RTP time at the clock's epoch. That's the direct offset */
304 gst_util_uint64_scale (clock_time, clock_rate, GST_SECOND);
306 clock_time &= 0xffffffff;
308 G_GUINT64_CONSTANT (0xffffffff) + rtptime - clock_time;
309 mediaclk = g_strdup_printf ("direct=%u", (guint32) mediaclk_offset);
312 if (GST_IS_NTP_CLOCK (clock)) {
316 g_object_get (clock, "address", &ntp_address, "port", &ntp_port,
320 ts_refclk = g_strdup_printf ("ntp=%s", ntp_address);
322 ts_refclk = g_strdup_printf ("ntp=%s:%u", ntp_address, ntp_port);
324 g_free (ntp_address);
326 guint64 ptp_clock_id;
329 g_object_get (clock, "grandmaster-clock-id", &ptp_clock_id, "domain",
335 ("ptp=IEEE1588-2008:%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X:%u",
336 (guint) (ptp_clock_id >> 56) & 0xff,
337 (guint) (ptp_clock_id >> 48) & 0xff,
338 (guint) (ptp_clock_id >> 40) & 0xff,
339 (guint) (ptp_clock_id >> 32) & 0xff,
340 (guint) (ptp_clock_id >> 24) & 0xff,
341 (guint) (ptp_clock_id >> 16) & 0xff,
342 (guint) (ptp_clock_id >> 8) & 0xff,
343 (guint) (ptp_clock_id >> 0) & 0xff, ptp_domain);
347 ("ptp=IEEE1588-2008:%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
348 (guint) (ptp_clock_id >> 56) & 0xff,
349 (guint) (ptp_clock_id >> 48) & 0xff,
350 (guint) (ptp_clock_id >> 40) & 0xff,
351 (guint) (ptp_clock_id >> 32) & 0xff,
352 (guint) (ptp_clock_id >> 24) & 0xff,
353 (guint) (ptp_clock_id >> 16) & 0xff,
354 (guint) (ptp_clock_id >> 8) & 0xff,
355 (guint) (ptp_clock_id >> 0) & 0xff);
360 gst_object_unref (clock);
363 ts_refclk = g_strdup ("local");
365 mediaclk = g_strdup ("sender");
367 gst_sdp_media_add_attribute (smedia, "ts-refclk", ts_refclk);
368 gst_sdp_media_add_attribute (smedia, "mediaclk", mediaclk);
371 gst_object_unref (joined_bin);
374 update_sdp_from_tags (stream, smedia);
376 if (profile == GST_RTSP_PROFILE_AVPF || profile == GST_RTSP_PROFILE_SAVPF) {
377 if ((rtx_time = gst_rtsp_stream_get_retransmission_time (stream))) {
378 /* ssrc multiplexed retransmit functionality */
379 guint rtx_pt = gst_rtsp_stream_get_retransmission_pt (stream);
382 g_warning ("failed to find an available dynamic payload type. "
383 "Not adding retransmission");
387 gint caps_pt, caps_rate;
389 s = gst_caps_get_structure (caps, 0);
393 /* get payload type and clock rate */
394 gst_structure_get_int (s, "payload", &caps_pt);
395 gst_structure_get_int (s, "clock-rate", &caps_rate);
397 tmp = g_strdup_printf ("%d", rtx_pt);
398 gst_sdp_media_add_format (smedia, tmp);
401 tmp = g_strdup_printf ("%d rtx/%d", rtx_pt, caps_rate);
402 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
406 g_strdup_printf ("%d apt=%d;rtx-time=%" G_GINT64_FORMAT, rtx_pt,
407 caps_pt, GST_TIME_AS_MSECONDS (rtx_time));
408 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
413 if (gst_rtsp_stream_get_ulpfec_percentage (stream)) {
414 guint ulpfec_pt = gst_rtsp_stream_get_ulpfec_pt (stream);
416 if (ulpfec_pt == 0) {
417 g_warning ("failed to find an available dynamic payload type. "
418 "Not adding ulpfec");
422 gint caps_pt, caps_rate;
424 s = gst_caps_get_structure (caps, 0);
428 /* get payload type and clock rate */
429 gst_structure_get_int (s, "payload", &caps_pt);
430 gst_structure_get_int (s, "clock-rate", &caps_rate);
432 tmp = g_strdup_printf ("%d", ulpfec_pt);
433 gst_sdp_media_add_format (smedia, tmp);
436 tmp = g_strdup_printf ("%d ulpfec/%d", ulpfec_pt, caps_rate);
437 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
441 g_strdup_printf ("%d apt=%d", ulpfec_pt, caps_pt);
442 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
448 gst_sdp_message_add_media (sdp, smedia);
449 gst_sdp_media_free (smedia);
456 gst_sdp_media_free (smedia);
457 GST_ERROR ("unable to set media from caps for stream %d",
458 gst_rtsp_stream_get_index (stream));
463 gst_sdp_media_free (smedia);
464 GST_ERROR ("stream %d has no multicast address",
465 gst_rtsp_stream_get_index (stream));
470 gst_sdp_media_free (smedia);
471 GST_ERROR ("caps for stream %d have no structure",
472 gst_rtsp_stream_get_index (stream));
475 crypto_sessions_error:
477 gst_sdp_media_free (smedia);
478 GST_ERROR ("unable to add MIKEY crypto sessions for stream %d",
479 gst_rtsp_stream_get_index (stream));
485 * gst_rtsp_sdp_from_media:
486 * @sdp: a #GstSDPMessage
487 * @info: (transfer none): a #GstSDPInfo
488 * @media: (transfer none): a #GstRTSPMedia
490 * Add @media specific info to @sdp. @info is used to configure the connection
491 * information in the SDP.
493 * Returns: TRUE on success.
496 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
497 GstRTSPMedia * media)
503 n_streams = gst_rtsp_media_n_streams (media);
505 rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
506 if (rangestr == NULL)
509 gst_sdp_message_add_attribute (sdp, "range", rangestr);
513 for (i = 0; res && (i < n_streams); i++) {
514 GstRTSPStream *stream;
516 stream = gst_rtsp_media_get_stream (media, i);
517 res = gst_rtsp_sdp_from_stream (sdp, info, stream);
519 GST_ERROR ("could not get SDP from stream %p", stream);
525 GstNetTimeProvider *provider;
528 gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
530 gchar *address, *str;
533 g_object_get (provider, "clock", &clock, "address", &address, "port",
536 str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
537 g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
538 gst_clock_get_time (clock));
540 gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
542 gst_object_unref (clock);
544 gst_object_unref (provider);
553 GST_ERROR ("media %p is not prepared", media);
558 GST_ERROR ("could not get SDP from media %p", media);
564 * gst_rtsp_sdp_from_stream:
565 * @sdp: a #GstSDPMessage
566 * @info: (transfer none): a #GstSDPInfo
567 * @stream: (transfer none): a #GstRTSPStream
569 * Add info from @stream to @sdp.
571 * Returns: TRUE on success.
574 gst_rtsp_sdp_from_stream (GstSDPMessage * sdp, GstSDPInfo * info,
575 GstRTSPStream * stream)
578 GstRTSPProfile profiles;
582 caps = gst_rtsp_stream_get_caps (stream);
585 GST_ERROR ("stream %p has no caps", stream);
589 /* make a new media for each profile */
590 profiles = gst_rtsp_stream_get_profiles (stream);
593 while (res && (profiles >= mask)) {
594 GstRTSPProfile prof = profiles & mask;
597 res = gst_rtsp_sdp_make_media (sdp, info, stream, caps, prof);
601 gst_caps_unref (caps);