media: use segment stop in collect_media_stats
[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
104     stream = gst_rtsp_media_get_stream (media, i);
105     caps = gst_rtsp_stream_get_caps (stream);
106
107     if (caps == NULL) {
108       g_warning ("ignoring stream %d without media type", i);
109       continue;
110     }
111
112     s = gst_caps_get_structure (caps, 0);
113     if (s == NULL) {
114       gst_caps_unref (caps);
115       g_warning ("ignoring stream %d without media type", i);
116       continue;
117     }
118
119     gst_sdp_media_new (&smedia);
120
121     /* get media type and payload for the m= line */
122     caps_str = gst_structure_get_string (s, "media");
123     gst_sdp_media_set_media (smedia, caps_str);
124
125     gst_structure_get_int (s, "payload", &caps_pt);
126     tmp = g_strdup_printf ("%d", caps_pt);
127     gst_sdp_media_add_format (smedia, tmp);
128     g_free (tmp);
129
130     gst_sdp_media_set_port_info (smedia, 0, 1);
131     gst_sdp_media_set_proto (smedia, "RTP/AVP");
132
133     /* for the c= line */
134     if (info->is_ipv6) {
135       gst_sdp_media_add_connection (smedia, "IN", "IP6", "::", 16, 0);
136     } else {
137       gst_sdp_media_add_connection (smedia, "IN", "IP4", "0.0.0.0", 16, 0);
138     }
139
140     /* get clock-rate, media type and params for the rtpmap attribute */
141     gst_structure_get_int (s, "clock-rate", &caps_rate);
142     caps_enc = gst_structure_get_string (s, "encoding-name");
143     caps_params = gst_structure_get_string (s, "encoding-params");
144
145     if (caps_enc) {
146       if (caps_params)
147         tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
148             caps_params);
149       else
150         tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
151
152       gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
153       g_free (tmp);
154     }
155
156     /* the config uri */
157     tmp = g_strdup_printf ("stream=%d", i);
158     gst_sdp_media_add_attribute (smedia, "control", tmp);
159     g_free (tmp);
160
161     /* collect all other properties and add them to fmtp or attributes */
162     fmtp = g_string_new ("");
163     g_string_append_printf (fmtp, "%d ", caps_pt);
164     first = TRUE;
165     n_fields = gst_structure_n_fields (s);
166     for (j = 0; j < n_fields; j++) {
167       const gchar *fname, *fval;
168
169       fname = gst_structure_nth_field_name (s, j);
170
171       /* filter out standard properties */
172       if (!strcmp (fname, "media"))
173         continue;
174       if (!strcmp (fname, "payload"))
175         continue;
176       if (!strcmp (fname, "clock-rate"))
177         continue;
178       if (!strcmp (fname, "encoding-name"))
179         continue;
180       if (!strcmp (fname, "encoding-params"))
181         continue;
182       if (!strcmp (fname, "ssrc"))
183         continue;
184       if (!strcmp (fname, "clock-base"))
185         continue;
186       if (!strcmp (fname, "seqnum-base"))
187         continue;
188
189       if (g_str_has_prefix (fname, "a-")) {
190         /* attribute */
191         if ((fval = gst_structure_get_string (s, fname)))
192           gst_sdp_media_add_attribute (smedia, fname + 2, fval);
193         continue;
194       }
195       if (g_str_has_prefix (fname, "x-")) {
196         /* attribute */
197         if ((fval = gst_structure_get_string (s, fname)))
198           gst_sdp_media_add_attribute (smedia, fname, fval);
199         continue;
200       }
201
202       if ((fval = gst_structure_get_string (s, fname))) {
203         g_string_append_printf (fmtp, "%s%s=%s", first ? "" : ";", fname, fval);
204         first = FALSE;
205       }
206     }
207     if (!first) {
208       tmp = g_string_free (fmtp, FALSE);
209       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
210       g_free (tmp);
211     } else {
212       g_string_free (fmtp, TRUE);
213     }
214
215     update_sdp_from_tags (stream, smedia);
216
217     gst_sdp_message_add_media (sdp, smedia);
218     gst_sdp_media_free (smedia);
219     gst_caps_unref (caps);
220   }
221
222   {
223     GstNetTimeProvider *provider;
224
225     if ((provider =
226             gst_rtsp_media_get_time_provider (media, info->server_ip, 0))) {
227       GstClock *clock;
228       gchar *address, *str;
229       gint port;
230
231       g_object_get (provider, "clock", &clock, "address", &address, "port",
232           &port, NULL);
233
234       str = g_strdup_printf ("GstNetTimeProvider %s %s:%d %" G_GUINT64_FORMAT,
235           g_type_name (G_TYPE_FROM_INSTANCE (clock)), address, port,
236           gst_clock_get_time (clock));
237
238       gst_sdp_message_add_attribute (sdp, "x-gst-clock", str);
239       g_free (str);
240       gst_object_unref (clock);
241       g_free (address);
242       gst_object_unref (provider);
243     }
244   }
245
246   return TRUE;
247
248   /* ERRORS */
249 not_prepared:
250   {
251     GST_ERROR ("media %p is not prepared", media);
252     return FALSE;
253   }
254 }