sdp: reindent and check for prepared status
[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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <string.h>
20
21 #include "rtsp-sdp.h"
22
23 /**
24  * gst_rtsp_sdp_from_media:
25  * @media: a #GstRTSPMedia
26  *
27  * Create a new sdp message for @media.
28  *
29  * Returns: a new sdp message for @media. gst_sdp_message_free() after usage.
30  */
31 GstSDPMessage *
32 gst_rtsp_sdp_from_media (GstRTSPMedia * media)
33 {
34   GstSDPMessage *sdp;
35   guint i, n_streams;
36   gchar *rangestr;
37
38   if (media->status != GST_RTSP_MEDIA_STATUS_PREPARED)
39     goto not_prepared;
40
41   n_streams = gst_rtsp_media_n_streams (media);
42
43   gst_sdp_message_new (&sdp);
44
45   /* some standard things first */
46   gst_sdp_message_set_version (sdp, "0");
47
48   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", "IP4",
49       "127.0.0.1");
50   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
51   gst_sdp_message_set_information (sdp, "rtsp-server");
52   gst_sdp_message_add_time (sdp, "0", "0", NULL);
53   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
54   gst_sdp_message_add_attribute (sdp, "type", "broadcast");
55   gst_sdp_message_add_attribute (sdp, "control", "*");
56   rangestr = gst_rtsp_range_to_string (&media->range);
57   gst_sdp_message_add_attribute (sdp, "range", rangestr);
58   g_free (rangestr);
59
60   for (i = 0; i < n_streams; i++) {
61     GstRTSPMediaStream *stream;
62     GstSDPMedia *smedia;
63     GstStructure *s;
64     const gchar *caps_str, *caps_enc, *caps_params;
65     gchar *tmp;
66     gint caps_pt, caps_rate;
67     guint n_fields, j;
68     gboolean first;
69     GString *fmtp;
70
71     stream = gst_rtsp_media_get_stream (media, i);
72
73     if (stream->caps == NULL) {
74       g_warning ("ignoring stream %d without media type", i);
75       continue;
76     }
77
78     s = gst_caps_get_structure (stream->caps, 0);
79     if (s == NULL) {
80       g_warning ("ignoring stream %d without media type", i);
81       continue;
82     }
83
84     gst_sdp_media_new (&smedia);
85
86     /* get media type and payload for the m= line */
87     caps_str = gst_structure_get_string (s, "media");
88     gst_sdp_media_set_media (smedia, caps_str);
89
90     gst_structure_get_int (s, "payload", &caps_pt);
91     tmp = g_strdup_printf ("%d", caps_pt);
92     gst_sdp_media_add_format (smedia, tmp);
93     g_free (tmp);
94
95     gst_sdp_media_set_port_info (smedia, 0, 1);
96     gst_sdp_media_set_proto (smedia, "RTP/AVP");
97
98     /* for the c= line */
99     gst_sdp_media_add_connection (smedia, "IN", "IP4", "127.0.0.1", 0, 0);
100
101     /* get clock-rate, media type and params for the rtpmap attribute */
102     gst_structure_get_int (s, "clock-rate", &caps_rate);
103     caps_enc = gst_structure_get_string (s, "encoding-name");
104     caps_params = gst_structure_get_string (s, "encoding-params");
105
106     if (caps_enc) {
107       if (caps_params)
108         tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
109             caps_params);
110       else
111         tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
112
113       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
114       g_free (tmp);
115     }
116
117     /* the config uri */
118     tmp = g_strdup_printf ("stream=%d", i);
119     gst_sdp_media_add_attribute (smedia, "control", tmp);
120     g_free (tmp);
121
122     /* collect all other properties and add them to fmtp */
123     fmtp = g_string_new ("");
124     g_string_append_printf (fmtp, "%d ", caps_pt);
125     first = TRUE;
126     n_fields = gst_structure_n_fields (s);
127     for (j = 0; j < n_fields; j++) {
128       const gchar *fname, *fval;
129
130       fname = gst_structure_nth_field_name (s, j);
131
132       /* filter out standard properties */
133       if (!strcmp (fname, "media"))
134         continue;
135       if (!strcmp (fname, "payload"))
136         continue;
137       if (!strcmp (fname, "clock-rate"))
138         continue;
139       if (!strcmp (fname, "encoding-name"))
140         continue;
141       if (!strcmp (fname, "encoding-params"))
142         continue;
143       if (!strcmp (fname, "ssrc"))
144         continue;
145       if (!strcmp (fname, "clock-base"))
146         continue;
147       if (!strcmp (fname, "seqnum-base"))
148         continue;
149
150       if ((fval = gst_structure_get_string (s, fname))) {
151         g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
152         first = FALSE;
153       }
154     }
155     if (!first) {
156       tmp = g_string_free (fmtp, FALSE);
157       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
158       g_free (tmp);
159     } else {
160       g_string_free (fmtp, TRUE);
161     }
162     gst_sdp_message_add_media (sdp, smedia);
163     gst_sdp_media_free (smedia);
164   }
165
166   return sdp;
167
168   /* ERRORS */
169 not_prepared:
170   {
171     GST_ERROR ("media %p is not prepared", media);
172     return NULL;
173   }
174 }