3e6c27136e85a92130b3d337dbe42e8d906b2bfc
[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   n_streams = gst_rtsp_media_n_streams (media);
39
40   gst_sdp_message_new (&sdp);
41
42   /* some standard things first */
43   gst_sdp_message_set_version (sdp, "0");
44   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", "IP4", "127.0.0.1");
45   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
46   gst_sdp_message_set_information (sdp, "rtsp-server");
47   gst_sdp_message_add_time (sdp, "0", "0", NULL);
48   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
49   gst_sdp_message_add_attribute (sdp, "type", "broadcast");
50   rangestr = gst_rtsp_range_to_string (&media->range);
51   gst_sdp_message_add_attribute (sdp, "range", rangestr);
52   g_free (rangestr);
53
54   for (i = 0; i < n_streams; i++) {
55     GstRTSPMediaStream *stream;
56     GstSDPMedia *smedia;
57     GstStructure *s;
58     const gchar *caps_str, *caps_enc, *caps_params;
59     gchar *tmp;
60     gint caps_pt, caps_rate;
61     guint n_fields, j;
62     gboolean first;
63     GString *fmtp;
64
65     stream = gst_rtsp_media_get_stream (media, i);
66     gst_sdp_media_new (&smedia);
67
68     s = gst_caps_get_structure (stream->caps, 0);
69
70     /* get media type and payload for the m= line */
71     caps_str = gst_structure_get_string (s, "media");
72     gst_sdp_media_set_media (smedia, caps_str);
73
74     gst_structure_get_int (s, "payload", &caps_pt);
75     tmp = g_strdup_printf ("%d", caps_pt);
76     gst_sdp_media_add_format (smedia, tmp);
77     g_free (tmp);
78
79     gst_sdp_media_set_port_info (smedia, 0, 1);
80     gst_sdp_media_set_proto (smedia, "RTP/AVP");
81
82     /* for the c= line */
83     gst_sdp_media_add_connection (smedia, "IN", "IP4", "127.0.0.1", 0, 0);
84
85     /* get clock-rate, media type and params for the rtpmap attribute */
86     gst_structure_get_int (s, "clock-rate", &caps_rate);
87     caps_enc = gst_structure_get_string (s, "encoding-name");
88     caps_params = gst_structure_get_string (s, "encoding-params");
89
90     if (caps_enc) {
91       if (caps_params)
92         tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
93                       caps_params);
94       else
95         tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
96
97       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
98       g_free (tmp);
99     }
100
101     /* the config uri */
102     tmp = g_strdup_printf ("stream=%d", i);
103     gst_sdp_media_add_attribute (smedia, "control", tmp);
104     g_free (tmp);
105
106     /* collect all other properties and add them to fmtp */
107     fmtp = g_string_new ("");
108     g_string_append_printf (fmtp, "%d ", caps_pt);
109     first = TRUE;
110     n_fields = gst_structure_n_fields (s);
111     for (j = 0; j < n_fields; j++) {
112       const gchar *fname, *fval;
113
114       fname = gst_structure_nth_field_name (s, j);
115
116       /* filter out standard properties */
117       if (!strcmp (fname, "media")) 
118         continue;
119       if (!strcmp (fname, "payload")) 
120         continue;
121       if (!strcmp (fname, "clock-rate")) 
122         continue;
123       if (!strcmp (fname, "encoding-name")) 
124         continue;
125       if (!strcmp (fname, "encoding-params")) 
126         continue;
127       if (!strcmp (fname, "ssrc")) 
128         continue;
129       if (!strcmp (fname, "clock-base")) 
130         continue;
131       if (!strcmp (fname, "seqnum-base")) 
132         continue;
133
134       if ((fval = gst_structure_get_string (s, fname))) {
135         g_string_append_printf (fmtp, "%s%s=%s", first ? "":";", fname, fval);
136         first = FALSE;
137       }
138     }
139     if (!first) {
140       tmp = g_string_free (fmtp, FALSE);
141       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
142       g_free (tmp);
143     }
144     else {
145       g_string_free (fmtp, TRUE);
146     }
147     gst_sdp_message_add_media (sdp, smedia);
148     gst_sdp_media_free (smedia);
149   }
150
151   return sdp;
152 }