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);
74 gst_pad_sticky_events_foreach (src_pad, get_info_from_tags, stream_media);
76 gst_object_unref (src_pad);
80 get_roc_from_stats (GstStructure * stats, guint ssrc)
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. */
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");
94 len = gst_value_array_get_size (va);
96 /* look if there's any SSRC that matches. */
97 for (i = 0; i < len; i++) {
99 v = gst_value_array_get_value (va, i);
100 if (v && (stream = g_value_get_boxed (v))) {
102 gst_structure_get_uint (stream, "ssrc", &stream_ssrc);
103 if (stream_ssrc == ssrc) {
104 gst_structure_get_uint (stream, "roc", &roc);
114 mikey_add_crypto_sessions (GstRTSPStream * stream, GstMIKEYMessage * msg)
119 GValueArray *sources;
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);
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);
136 g_object_get (session, "sources", &sources, NULL);
137 for (i = 0; sources && (i < sources->n_values); i++) {
143 val = g_value_array_get_nth (sources, i);
144 source = (GObject *) g_value_get_object (val);
146 g_object_get (source, "ssrc", &ssrc, "is-sender", &is_sender, NULL);
152 g_object_get (encoder, "stats", &stats, NULL);
155 roc = get_roc_from_stats (stats, ssrc);
156 gst_structure_free (stats);
159 roc_found = ! !(roc != -1);
161 GST_ERROR ("unable to obtain ROC for stream %p with SSRC %u",
166 GST_INFO ("stream %p with SSRC %u has a ROC of %u", stream, ssrc, roc);
168 gst_mikey_message_add_cs_srtp (msg, 0, ssrc, roc);
174 g_value_array_free (sources);
176 gst_object_unref (encoder);
177 g_object_unref (session);
183 make_media (GstSDPMessage * sdp, GstSDPInfo * info,
184 GstRTSPStream * stream, GstCaps * caps, GstRTSPProfile profile)
188 GstRTSPLowerTrans ltrans;
189 GSocketFamily family;
190 const gchar *addrtype, *proto;
193 GstClockTime rtx_time;
195 GstMIKEYMessage *mikey_msg;
197 gst_sdp_media_new (&smedia);
199 if (gst_sdp_media_set_media_from_caps (caps, smedia) != GST_SDP_OK) {
203 gst_sdp_media_set_port_info (smedia, 0, 1);
206 case GST_RTSP_PROFILE_AVP:
209 case GST_RTSP_PROFILE_AVPF:
212 case GST_RTSP_PROFILE_SAVP:
215 case GST_RTSP_PROFILE_SAVPF:
222 gst_sdp_media_set_proto (smedia, proto);
226 family = G_SOCKET_FAMILY_IPV6;
229 family = G_SOCKET_FAMILY_IPV4;
232 ltrans = gst_rtsp_stream_get_protocols (stream);
233 if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
234 GstRTSPAddress *addr;
236 addr = gst_rtsp_stream_get_multicast_address (stream, family);
240 address = g_strdup (addr->address);
242 gst_rtsp_address_free (addr);
246 address = g_strdup ("::");
248 address = g_strdup ("0.0.0.0");
251 /* for the c= line */
252 gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
256 tmp = gst_rtsp_stream_get_control (stream);
257 gst_sdp_media_add_attribute (smedia, "control", tmp);
261 mikey_msg = gst_mikey_message_new_from_caps (caps);
263 /* add policy '0' for all sending SSRC */
264 if (!mikey_add_crypto_sessions (stream, mikey_msg))
265 goto crypto_sessions_error;
267 base64 = gst_mikey_message_base64_encode (mikey_msg);
269 tmp = g_strdup_printf ("mikey %s", base64);
271 gst_sdp_media_add_attribute (smedia, "key-mgmt", tmp);
275 gst_mikey_message_unref (mikey_msg);
278 /* RFC 7273 clock signalling */
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);
289 gst_rtsp_stream_get_rtpinfo (stream, &rtptime, NULL, &clock_rate,
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;
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;
300 /* Calculate RTP time at the clock's epoch. That's the direct offset */
302 gst_util_uint64_scale (clock_time, clock_rate, GST_SECOND);
304 clock_time &= 0xffffffff;
306 G_GUINT64_CONSTANT (0xffffffff) + rtptime - clock_time;
307 mediaclk = g_strdup_printf ("direct=%u", (guint32) mediaclk_offset);
310 if (GST_IS_NTP_CLOCK (clock)) {
314 g_object_get (clock, "address", &ntp_address, "port", &ntp_port,
318 ts_refclk = g_strdup_printf ("ntp=%s", ntp_address);
320 ts_refclk = g_strdup_printf ("ntp=%s:%u", ntp_address, ntp_port);
322 g_free (ntp_address);
324 guint64 ptp_clock_id;
327 g_object_get (clock, "grandmaster-clock-id", &ptp_clock_id, "domain",
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);
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);
358 gst_object_unref (clock);
361 ts_refclk = g_strdup ("local");
363 mediaclk = g_strdup ("sender");
365 gst_sdp_media_add_attribute (smedia, "ts-refclk", ts_refclk);
366 gst_sdp_media_add_attribute (smedia, "mediaclk", mediaclk);
369 gst_object_unref (joined_bin);
372 update_sdp_from_tags (stream, smedia);
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);
380 g_warning ("failed to find an available dynamic payload type. "
381 "Not adding retransmission");
385 gint caps_pt, caps_rate;
387 s = gst_caps_get_structure (caps, 0);
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);
395 tmp = g_strdup_printf ("%d", rtx_pt);
396 gst_sdp_media_add_format (smedia, tmp);
399 tmp = g_strdup_printf ("%d rtx/%d", rtx_pt, caps_rate);
400 gst_sdp_media_add_attribute (smedia, "rtpmap", 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);
411 gst_sdp_message_add_media (sdp, smedia);
412 gst_sdp_media_free (smedia);
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));
426 gst_sdp_media_free (smedia);
427 GST_ERROR ("stream %d has no multicast address",
428 gst_rtsp_stream_get_index (stream));
433 gst_sdp_media_free (smedia);
434 GST_ERROR ("caps for stream %d have no structure",
435 gst_rtsp_stream_get_index (stream));
438 crypto_sessions_error:
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));
448 * gst_rtsp_sdp_from_media:
449 * @sdp: a #GstSDPMessage
450 * @info: (transfer none): a #GstSDPInfo
451 * @media: (transfer none): a #GstRTSPMedia
453 * Add @media specific info to @sdp. @info is used to configure the connection
454 * information in the SDP.
456 * Returns: TRUE on success.
459 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
460 GstRTSPMedia * media)
466 n_streams = gst_rtsp_media_n_streams (media);
468 rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
469 if (rangestr == NULL)
472 gst_sdp_message_add_attribute (sdp, "range", rangestr);
476 for (i = 0; res && (i < n_streams); i++) {
477 GstRTSPStream *stream;
479 stream = gst_rtsp_media_get_stream (media, i);
480 res = gst_rtsp_sdp_from_stream (sdp, info, stream);
482 GST_ERROR ("could not get SDP from stream %p", stream);
488 GstNetTimeProvider *provider;
491 gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
493 gchar *address, *str;
496 g_object_get (provider, "clock", &clock, "address", &address, "port",
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));
503 gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
505 gst_object_unref (clock);
507 gst_object_unref (provider);
516 GST_ERROR ("media %p is not prepared", media);
521 GST_ERROR ("could not get SDP from media %p", media);
527 * gst_rtsp_sdp_from_stream:
528 * @sdp: a #GstSDPMessage
529 * @info: (transfer none): a #GstSDPInfo
530 * @stream: (transfer none): a #GstRTSPStream
532 * Add info from @stream to @sdp.
534 * Returns: TRUE on success.
537 gst_rtsp_sdp_from_stream (GstSDPMessage * sdp, GstSDPInfo * info,
538 GstRTSPStream * stream)
541 GstRTSPProfile profiles;
545 caps = gst_rtsp_stream_get_caps (stream);
548 GST_ERROR ("stream %p has no caps", stream);
552 /* make a new media for each profile */
553 profiles = gst_rtsp_stream_get_profiles (stream);
556 while (res && (profiles >= mask)) {
557 GstRTSPProfile prof = profiles & mask;
560 res = make_media (sdp, info, stream, caps, prof);
564 gst_caps_unref (caps);