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