e2e98bc1c27ddbf6a77dabd90f8ad01fdcd4619c
[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 "rtsp-sdp.h"
30
31 static gboolean
32 get_info_from_tags (GstPad * pad, GstEvent ** event, gpointer user_data)
33 {
34   GstSDPMedia *media = (GstSDPMedia *) user_data;
35
36   if (GST_EVENT_TYPE (*event) == GST_EVENT_TAG) {
37     GstTagList *tags;
38     guint bitrate = 0;
39
40     gst_event_parse_tag (*event, &tags);
41
42     if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
43       return TRUE;
44
45     if (!gst_tag_list_get_uint (tags, GST_TAG_MAXIMUM_BITRATE,
46             &bitrate) || bitrate == 0)
47       if (!gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
48           bitrate == 0)
49         return TRUE;
50
51     /* set bandwidth (kbits/s) */
52     gst_sdp_media_add_bandwidth (media, GST_SDP_BWTYPE_AS, bitrate / 1000);
53
54     return FALSE;
55
56   }
57
58   return TRUE;
59 }
60
61 static void
62 update_sdp_from_tags (GstRTSPStream * stream, GstSDPMedia * stream_media)
63 {
64   GstPad *src_pad;
65
66   src_pad = gst_rtsp_stream_get_srcpad (stream);
67
68   gst_pad_sticky_events_foreach (src_pad, get_info_from_tags, stream_media);
69
70   gst_object_unref (src_pad);
71 }
72
73 /**
74  * gst_rtsp_sdp_from_media:
75  * @sdp: a #GstSDPMessage
76  * @info: info
77  * @media: a #GstRTSPMedia
78  *
79  * Add @media specific info to @sdp. @info is used to configure the connection
80  * information in the SDP.
81  *
82  * Returns: TRUE on success.
83  */
84 gboolean
85 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
86     GstRTSPMedia * media)
87 {
88   guint i, n_streams;
89   gchar *rangestr;
90
91   n_streams = gst_rtsp_media_n_streams (media);
92
93   rangestr = gst_rtsp_media_get_range_string (media, FALSE, GST_RTSP_RANGE_NPT);
94   if (rangestr == NULL)
95     goto not_prepared;
96
97   gst_sdp_message_add_attribute (sdp, "range", rangestr);
98   g_free (rangestr);
99
100   for (i = 0; i < n_streams; i++) {
101     GstRTSPStream *stream;
102     GstSDPMedia *smedia;
103     GstStructure *s;
104     const gchar *caps_str, *caps_enc, *caps_params;
105     gchar *tmp;
106     gint caps_pt, caps_rate;
107     guint n_fields, j;
108     gboolean first;
109     GString *fmtp;
110     GstCaps *caps;
111     GstRTSPLowerTrans ltrans;
112     GSocketFamily family;
113     const gchar *addrtype;
114     gchar *address;
115     guint ttl;
116
117     stream = gst_rtsp_media_get_stream (media, i);
118     caps = gst_rtsp_stream_get_caps (stream);
119
120     if (caps == NULL) {
121       g_warning ("ignoring stream %d without media type", i);
122       continue;
123     }
124
125     s = gst_caps_get_structure (caps, 0);
126     if (s == NULL) {
127       gst_caps_unref (caps);
128       g_warning ("ignoring stream %d without media type", i);
129       continue;
130     }
131
132     gst_sdp_media_new (&smedia);
133
134     /* get media type and payload for the m= line */
135     caps_str = gst_structure_get_string (s, "media");
136     gst_sdp_media_set_media (smedia, caps_str);
137
138     gst_structure_get_int (s, "payload", &caps_pt);
139     tmp = g_strdup_printf ("%d", caps_pt);
140     gst_sdp_media_add_format (smedia, tmp);
141     g_free (tmp);
142
143     gst_sdp_media_set_port_info (smedia, 0, 1);
144     gst_sdp_media_set_proto (smedia, "RTP/AVP");
145
146     if (info->is_ipv6) {
147       addrtype = "IP6";
148       family = G_SOCKET_FAMILY_IPV6;
149     } else {
150       addrtype = "IP4";
151       family = G_SOCKET_FAMILY_IPV4;
152     }
153
154     ltrans = gst_rtsp_stream_get_protocols (stream);
155     if (ltrans == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
156       GstRTSPAddress *addr;
157
158       addr = gst_rtsp_stream_get_multicast_address (stream, family);
159       if (addr == NULL) {
160         gst_sdp_media_free (smedia);
161         gst_caps_unref (caps);
162         g_warning ("ignoring stream %d without multicast address", i);
163         continue;
164       }
165       address = g_strdup (addr->address);
166       ttl = addr->ttl;
167       gst_rtsp_address_free (addr);
168     } else {
169       ttl = 16;
170       if (info->is_ipv6)
171         address = g_strdup ("::");
172       else
173         address = g_strdup ("0.0.0.0");
174     }
175
176     /* for the c= line */
177     gst_sdp_media_add_connection (smedia, "IN", addrtype, address, ttl, 1);
178     g_free (address);
179
180     /* get clock-rate, media type and params for the rtpmap attribute */
181     gst_structure_get_int (s, "clock-rate", &caps_rate);
182     caps_enc = gst_structure_get_string (s, "encoding-name");
183     caps_params = gst_structure_get_string (s, "encoding-params");
184
185     if (caps_enc) {
186       if (caps_params)
187         tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
188             caps_params);
189       else
190         tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
191
192       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
193       g_free (tmp);
194     }
195
196     /* the config uri */
197     tmp = gst_rtsp_stream_get_control (stream);
198     gst_sdp_media_add_attribute (smedia, "control", tmp);
199     g_free (tmp);
200
201     /* collect all other properties and add them to fmtp or attributes */
202     fmtp = g_string_new ("");
203     g_string_append_printf (fmtp, "%d ", caps_pt);
204     first = TRUE;
205     n_fields = gst_structure_n_fields (s);
206     for (j = 0; j < n_fields; j++) {
207       const gchar *fname, *fval;
208
209       fname = gst_structure_nth_field_name (s, j);
210
211       /* filter out standard properties */
212       if (!strcmp (fname, "media"))
213         continue;
214       if (!strcmp (fname, "payload"))
215         continue;
216       if (!strcmp (fname, "clock-rate"))
217         continue;
218       if (!strcmp (fname, "encoding-name"))
219         continue;
220       if (!strcmp (fname, "encoding-params"))
221         continue;
222       if (!strcmp (fname, "ssrc"))
223         continue;
224       if (!strcmp (fname, "clock-base"))
225         continue;
226       if (!strcmp (fname, "seqnum-base"))
227         continue;
228
229       if (g_str_has_prefix (fname, "a-")) {
230         /* attribute */
231         if ((fval = gst_structure_get_string (s, fname)))
232           gst_sdp_media_add_attribute (smedia, fname + 2, fval);
233         continue;
234       }
235       if (g_str_has_prefix (fname, "x-")) {
236         /* attribute */
237         if ((fval = gst_structure_get_string (s, fname)))
238           gst_sdp_media_add_attribute (smedia, fname, fval);
239         continue;
240       }
241
242       if ((fval = gst_structure_get_string (s, fname))) {
243         g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
244         first = FALSE;
245       }
246     }
247     if (!first) {
248       tmp = g_string_free (fmtp, FALSE);
249       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
250       g_free (tmp);
251     } else {
252       g_string_free (fmtp, TRUE);
253     }
254
255     update_sdp_from_tags (stream, smedia);
256
257     gst_sdp_message_add_media (sdp, smedia);
258     gst_sdp_media_free (smedia);
259     gst_caps_unref (caps);
260   }
261
262   {
263     GstNetTimeProvider *provider;
264
265     if ((provider =
266             gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
267       GstClock *clock;
268       gchar *address, *str;
269       gint port;
270
271       g_object_get (provider, "clock", &clock, "address", &address, "port",
272           &port, NULL);
273
274       str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
275           g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
276           gst_clock_get_time (clock));
277
278       gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
279       g_free (str);
280       gst_object_unref (clock);
281       g_free (address);
282       gst_object_unref (provider);
283     }
284   }
285
286   return TRUE;
287
288   /* ERRORS */
289 not_prepared:
290   {
291     GST_ERROR ("media %p is not prepared", media);
292     return FALSE;
293   }
294 }