rtsp: parse SMPTE ranges
[platform/upstream/gstreamer.git] / gst-libs / gst / rtsp / gstrtsprange.c
1 /* GStreamer
2  * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.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 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42
43 /**
44  * SECTION:gstrtsprange
45  * @short_description: dealing with time ranges
46  *  
47  * Provides helper functions to deal with time ranges.
48  *  
49  * Last reviewed on 2007-07-25 (0.10.14)
50  */
51
52
53 #include <stdio.h>
54 #include <string.h>
55
56 #include "gstrtsprange.h"
57
58 static gdouble
59 gst_strtod (const gchar * dstr)
60 {
61   gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
62
63   /* canonicalise floating point string so we can handle float strings
64    * in the form "24.930" or "24,930" irrespective of the current locale.
65    * We should always be getting floats in 24.930 format with a floating point,
66    * but let's accept malformed ones as well, easy mistake to make after all */
67   g_strlcpy (s, dstr, sizeof (s));
68   g_strdelimit (s, ",", '.');
69   return g_ascii_strtod (s, NULL);
70 }
71
72 /* npt-time     =   "now" | npt-sec | npt-hhmmss
73  * npt-sec      =   1*DIGIT [ "." *DIGIT ]
74  * npt-hhmmss   =   npt-hh ":" npt-mm ":" npt-ss [ "." *DIGIT ]
75  * npt-hh       =   1*DIGIT     ; any positive number
76  * npt-mm       =   1*2DIGIT    ; 0-59
77  * npt-ss       =   1*2DIGIT    ; 0-59
78  */
79 static GstRTSPResult
80 parse_npt_time (const gchar * str, GstRTSPTime * time)
81 {
82   if (strncmp (str, "now", 3) == 0) {
83     time->type = GST_RTSP_TIME_NOW;
84   } else if (str[0] == '\0' || str[0] == '-') {
85     time->type = GST_RTSP_TIME_END;
86   } else if (strstr (str, ":")) {
87     gint hours, mins;
88
89     if (sscanf (str, "%2d:%2d:", &hours, &mins) != 2)
90       return GST_RTSP_EINVAL;
91
92     str = strchr (str, ':');
93     str = strchr (str + 1, ':');
94     if (str == NULL)
95       return GST_RTSP_EINVAL;
96
97     time->type = GST_RTSP_TIME_SECONDS;
98     time->seconds = ((hours * 60) + mins) * 60 + gst_strtod (str + 1);
99   } else {
100     time->type = GST_RTSP_TIME_SECONDS;
101     time->seconds = gst_strtod (str);
102   }
103   return GST_RTSP_OK;
104 }
105
106 /* npt-range    =   ( npt-time "-" [ npt-time ] ) | ( "-" npt-time )
107  */
108 static GstRTSPResult
109 parse_npt_range (const gchar * str, GstRTSPTimeRange * range)
110 {
111   GstRTSPResult res;
112   gchar *p;
113
114   range->unit = GST_RTSP_RANGE_NPT;
115
116   /* find '-' separator */
117   p = strstr (str, "-");
118   if (p == NULL)
119     return GST_RTSP_EINVAL;
120
121   if ((res = parse_npt_time (str, &range->min)) != GST_RTSP_OK)
122     goto done;
123
124   res = parse_npt_time (p + 1, &range->max);
125
126   /* a single - is not allowed */
127   if (range->min.type == GST_RTSP_TIME_END
128       && range->max.type == GST_RTSP_TIME_END)
129     return GST_RTSP_EINVAL;
130
131 done:
132   return res;
133 }
134
135
136 static GstRTSPResult
137 parse_clock_range (const gchar * str, GstRTSPTimeRange * range)
138 {
139   return GST_RTSP_ENOTIMPL;
140 }
141
142 /* smpte-time   =   1*2DIGIT ":" 1*2DIGIT ":" 1*2DIGIT [ ":" 1*2DIGIT ]
143  *                     [ "." 1*2DIGIT ]
144  *  hours:minutes:seconds:frames.subframes
145 */
146 static GstRTSPResult
147 parse_smpte_time (const gchar * str, GstRTSPTime * time, const gchar * limit)
148 {
149   gint hours, mins, secs;
150
151   if (str[0] == '\0') {
152     time->type = GST_RTSP_TIME_END;
153     return GST_RTSP_OK;
154   } else {
155     if (sscanf (str, "%02d:%2d:%02d", &hours, &mins, &secs) != 3)
156       return GST_RTSP_EINVAL;
157
158     time->type = GST_RTSP_TIME_FRAMES;
159     time->seconds = ((hours * 60) + mins) * 60 + secs;
160     str = strchr (str, ':');
161     str = strchr (str + 1, ':');
162     str = strchr (str + 1, ':');
163     if (str && (limit == NULL || str < limit))
164       time->frames = gst_strtod (str + 1);
165   }
166   return GST_RTSP_OK;
167 }
168
169 /* smpte-range  =   smpte-type "=" smpte-time "-" [ smpte-time ]
170  */
171 static GstRTSPResult
172 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
173 {
174   GstRTSPResult res;
175   gchar *p;
176
177   range->unit = GST_RTSP_RANGE_SMPTE;
178
179   /* find '-' separator, can't have a single - */
180   p = strstr (str, "-");
181   if (p == NULL || p == str)
182     return GST_RTSP_EINVAL;
183
184   if ((res = parse_smpte_time (str, &range->min, p)) != GST_RTSP_OK)
185     goto done;
186
187   res = parse_smpte_time (p + 1, &range->max, NULL);
188
189 done:
190   return res;
191 }
192
193 /**
194  * gst_rtsp_range_parse:
195  * @rangestr: a range string to parse
196  * @range: location to hold the #GstRTSPTimeRange result
197  *
198  * Parse @rangestr to a #GstRTSPTimeRange.
199  *
200  * Returns: #GST_RTSP_OK on success.
201  */
202 GstRTSPResult
203 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
204 {
205   GstRTSPResult ret;
206   GstRTSPTimeRange *res;
207   gchar *p;
208
209   g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
210   g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
211
212   res = g_new0 (GstRTSPTimeRange, 1);
213
214   p = (gchar *) rangestr;
215   /* first figure out the units of the range */
216   if (g_str_has_prefix (p, "npt=")) {
217     ret = parse_npt_range (p + 4, res);
218   } else if (g_str_has_prefix (p, "clock=")) {
219     ret = parse_clock_range (p + 6, res);
220   } else if (g_str_has_prefix (p, "smpte=")) {
221     res->unit = GST_RTSP_RANGE_SMPTE;
222     ret = parse_smpte_range (p + 6, res);
223   } else if (g_str_has_prefix (p, "smpte-30-drop=")) {
224     res->unit = GST_RTSP_RANGE_SMPTE_30_DROP;
225     ret = parse_smpte_range (p + 14, res);
226   } else if (g_str_has_prefix (p, "smpte-25=")) {
227     res->unit = GST_RTSP_RANGE_SMPTE_25;
228     ret = parse_smpte_range (p + 9, res);
229   } else
230     goto invalid;
231
232   if (ret != GST_RTSP_OK)
233     goto invalid;
234
235   *range = res;
236   return ret;
237
238   /* ERRORS */
239 invalid:
240   {
241     gst_rtsp_range_free (res);
242     return GST_RTSP_EINVAL;
243   }
244 }
245
246 static gboolean
247 npt_time_string (const GstRTSPTime * time, GString * string)
248 {
249   gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
250   gboolean res = TRUE;;
251
252   switch (time->type) {
253     case GST_RTSP_TIME_SECONDS:
254       /* need to format floating point value strings as in C locale */
255       g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, time->seconds);
256       g_string_append (string, dstrbuf);
257       break;
258     case GST_RTSP_TIME_NOW:
259       g_string_append (string, "now");
260       break;
261     case GST_RTSP_TIME_END:
262       break;
263     default:
264       res = FALSE;
265       break;
266   }
267   return res;
268 }
269
270 static gboolean
271 npt_range_string (const GstRTSPTimeRange * range, GString * string)
272 {
273   gboolean res;
274
275   if (!(res = npt_time_string (&range->min, string)))
276     goto done;
277
278   g_string_append (string, "-");
279
280   if (!(res = npt_time_string (&range->max, string)))
281     goto done;
282
283 done:
284   return res;
285 }
286
287 /**
288  * gst_rtsp_range_to_string:
289  * @range: a #GstRTSPTimeRange
290  *
291  * Convert @range into a string representation.
292  *
293  * Returns: The string representation of @range. g_free() after usage.
294  */
295 gchar *
296 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
297 {
298   gchar *result = NULL;
299   GString *string;
300
301   g_return_val_if_fail (range != NULL, NULL);
302
303   string = g_string_new ("");
304
305   switch (range->unit) {
306     case GST_RTSP_RANGE_NPT:
307       g_string_append (string, "npt=");
308       if (!npt_range_string (range, string)) {
309         g_string_free (string, TRUE);
310         string = NULL;
311       }
312       break;
313     case GST_RTSP_RANGE_SMPTE:
314     case GST_RTSP_RANGE_SMPTE_30_DROP:
315     case GST_RTSP_RANGE_SMPTE_25:
316     case GST_RTSP_RANGE_CLOCK:
317     default:
318       g_warning ("time range unit not yet implemented");
319       g_string_free (string, TRUE);
320       string = NULL;
321       break;
322   }
323   if (string)
324     result = g_string_free (string, FALSE);
325
326   return result;
327 }
328
329 /**
330  * gst_rtsp_range_free:
331  * @range: a #GstRTSPTimeRange
332  *
333  * Free the memory allocated by @range.
334  */
335 void
336 gst_rtsp_range_free (GstRTSPTimeRange * range)
337 {
338   g_free (range);
339 }