small cleanup
[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 /**
24  * gst_rtsp_sdp_from_media:
25  * @sdp: a #GstSDPMessage
26  * @info: info
27  * @media: a #GstRTSPMedia
28  *
29  * Add @media specific info to @sdp. @info is used to configure the connection
30  * information in the SDP.
31  *
32  * Returns: TRUE on success.
33  */
34 gboolean
35 gst_rtsp_sdp_from_media (GstSDPMessage * sdp, GstSDPInfo * info,
36     GstRTSPMedia * media)
37 {
38   guint i, n_streams;
39   gchar *rangestr;
40
41   n_streams = gst_rtsp_media_n_streams (media);
42
43   rangestr = gst_rtsp_media_get_range_string (media, FALSE);
44   if (rangestr == NULL)
45     goto not_prepared;
46
47   gst_sdp_message_add_attribute (sdp, "range", rangestr);
48   g_free (rangestr);
49
50   for (i = 0; i < n_streams; i++) {
51     GstRTSPStream *stream;
52     GstSDPMedia *smedia;
53     GstStructure *s;
54     const gchar *caps_str, *caps_enc, *caps_params;
55     gchar *tmp;
56     gint caps_pt, caps_rate;
57     guint n_fields, j;
58     gboolean first;
59     GString *fmtp;
60     GstCaps *caps;
61
62     stream = gst_rtsp_media_get_stream (media, i);
63     caps = gst_rtsp_stream_get_caps (stream);
64
65     if (caps == NULL) {
66       g_warning ("ignoring stream %d without media type", i);
67       continue;
68     }
69
70     s = gst_caps_get_structure (caps, 0);
71     if (s == NULL) {
72       gst_caps_unref (caps);
73       g_warning ("ignoring stream %d without media type", i);
74       continue;
75     }
76
77     gst_sdp_media_new (&smedia);
78
79     /* get media type and payload for the m= line */
80     caps_str = gst_structure_get_string (s, "media");
81     gst_sdp_media_set_media (smedia, caps_str);
82
83     gst_structure_get_int (s, "payload", &caps_pt);
84     tmp = g_strdup_printf ("%d", caps_pt);
85     gst_sdp_media_add_format (smedia, tmp);
86     g_free (tmp);
87
88     gst_sdp_media_set_port_info (smedia, 0, 1);
89     gst_sdp_media_set_proto (smedia, "RTP/AVP");
90
91     /* for the c= line */
92     gst_sdp_media_add_connection (smedia, "IN", info->server_proto,
93         info->server_ip, 16, 0);
94
95     /* get clock-rate, media type and params for the rtpmap attribute */
96     gst_structure_get_int (s, "clock-rate", &caps_rate);
97     caps_enc = gst_structure_get_string (s, "encoding-name");
98     caps_params = gst_structure_get_string (s, "encoding-params");
99
100     if (caps_enc) {
101       if (caps_params)
102         tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
103             caps_params);
104       else
105         tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
106
107       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
108       g_free (tmp);
109     }
110
111     /* the config uri */
112     tmp = g_strdup_printf ("stream=%d", i);
113     gst_sdp_media_add_attribute (smedia, "control", tmp);
114     g_free (tmp);
115
116     /* collect all other properties and add them to fmtp */
117     fmtp = g_string_new ("");
118     g_string_append_printf (fmtp, "%d ", caps_pt);
119     first = TRUE;
120     n_fields = gst_structure_n_fields (s);
121     for (j = 0; j < n_fields; j++) {
122       const gchar *fname, *fval;
123
124       fname = gst_structure_nth_field_name (s, j);
125
126       /* filter out standard properties */
127       if (!strcmp (fname, "media"))
128         continue;
129       if (!strcmp (fname, "payload"))
130         continue;
131       if (!strcmp (fname, "clock-rate"))
132         continue;
133       if (!strcmp (fname, "encoding-name"))
134         continue;
135       if (!strcmp (fname, "encoding-params"))
136         continue;
137       if (!strcmp (fname, "ssrc"))
138         continue;
139       if (!strcmp (fname, "clock-base"))
140         continue;
141       if (!strcmp (fname, "seqnum-base"))
142         continue;
143
144       if ((fval = gst_structure_get_string (s, fname))) {
145         g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
146         first = FALSE;
147       }
148     }
149     if (!first) {
150       tmp = g_string_free (fmtp, FALSE);
151       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
152       g_free (tmp);
153     } else {
154       g_string_free (fmtp, TRUE);
155     }
156     gst_sdp_message_add_media (sdp, smedia);
157     gst_sdp_media_free (smedia);
158     gst_caps_unref (caps);
159   }
160
161   return TRUE;
162
163   /* ERRORS */
164 not_prepared:
165   {
166     GST_ERROR ("media %p is not prepared", media);
167     return FALSE;
168   }
169 }