client: fix compilation
[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   if (media->status != GST_RTSP_MEDIA_STATUS_PREPARED)
42     goto not_prepared;
43
44   n_streams = gst_rtsp_media_n_streams (media);
45
46   rangestr = gst_rtsp_media_get_range_string (media, FALSE);
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
61     stream = gst_rtsp_media_get_stream (media, i);
62
63     if (stream->caps == NULL) {
64       g_warning ("ignoring stream %d without media type", i);
65       continue;
66     }
67
68     s = gst_caps_get_structure (stream->caps, 0);
69     if (s == NULL) {
70       g_warning ("ignoring stream %d without media type", i);
71       continue;
72     }
73
74     gst_sdp_media_new (&smedia);
75
76     /* get media type and payload for the m= line */
77     caps_str = gst_structure_get_string (s, "media");
78     gst_sdp_media_set_media (smedia, caps_str);
79
80     gst_structure_get_int (s, "payload", &caps_pt);
81     tmp = g_strdup_printf ("%d", caps_pt);
82     gst_sdp_media_add_format (smedia, tmp);
83     g_free (tmp);
84
85     gst_sdp_media_set_port_info (smedia, 0, 1);
86     gst_sdp_media_set_proto (smedia, "RTP/AVP");
87
88     /* for the c= line */
89     gst_sdp_media_add_connection (smedia, "IN", info->server_proto,
90         info->server_ip, 16, 0);
91
92     /* get clock-rate, media type and params for the rtpmap attribute */
93     gst_structure_get_int (s, "clock-rate", &caps_rate);
94     caps_enc = gst_structure_get_string (s, "encoding-name");
95     caps_params = gst_structure_get_string (s, "encoding-params");
96
97     if (caps_enc) {
98       if (caps_params)
99         tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
100             caps_params);
101       else
102         tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
103
104       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
105       g_free (tmp);
106     }
107
108     /* the config uri */
109     tmp = g_strdup_printf ("stream=%d", i);
110     gst_sdp_media_add_attribute (smedia, "control", tmp);
111     g_free (tmp);
112
113     /* collect all other properties and add them to fmtp */
114     fmtp = g_string_new ("");
115     g_string_append_printf (fmtp, "%d ", caps_pt);
116     first = TRUE;
117     n_fields = gst_structure_n_fields (s);
118     for (j = 0; j < n_fields; j++) {
119       const gchar *fname, *fval;
120
121       fname = gst_structure_nth_field_name (s, j);
122
123       /* filter out standard properties */
124       if (!strcmp (fname, "media"))
125         continue;
126       if (!strcmp (fname, "payload"))
127         continue;
128       if (!strcmp (fname, "clock-rate"))
129         continue;
130       if (!strcmp (fname, "encoding-name"))
131         continue;
132       if (!strcmp (fname, "encoding-params"))
133         continue;
134       if (!strcmp (fname, "ssrc"))
135         continue;
136       if (!strcmp (fname, "clock-base"))
137         continue;
138       if (!strcmp (fname, "seqnum-base"))
139         continue;
140
141       if ((fval = gst_structure_get_string (s, fname))) {
142         g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
143         first = FALSE;
144       }
145     }
146     if (!first) {
147       tmp = g_string_free (fmtp, FALSE);
148       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
149       g_free (tmp);
150     } else {
151       g_string_free (fmtp, TRUE);
152     }
153     gst_sdp_message_add_media (sdp, smedia);
154     gst_sdp_media_free (smedia);
155   }
156
157   return TRUE;
158
159   /* ERRORS */
160 not_prepared:
161   {
162     GST_ERROR ("media %p is not prepared", media);
163     return FALSE;
164   }
165 }