rtsprange: add method to convert ranges to GstClockTime
[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 /*   utc-time     =   utc-date "T" utc-time "Z"
136  *   utc-date     =   8DIGIT                    ; < YYYYMMDD >
137  *   utc-time     =   6DIGIT [ "." fraction ]   ; < HHMMSS.fraction >
138  *
139  *   Example for November 8, 1996 at 14h37 and 20 and a quarter seconds
140  *   UTC:
141  *
142  *   19961108T143720.25Z
143  */
144 static GstRTSPResult
145 parse_utc_time (const gchar * str, GstRTSPTime * time, GstRTSPTime2 * time2,
146     const gchar * limit)
147 {
148
149   if (str[0] == '\0') {
150     time->type = GST_RTSP_TIME_END;
151     return GST_RTSP_OK;
152   } else {
153     gint year, month, day;
154     gint hours, mins;
155     gdouble secs;
156     gchar *T, *Z;
157
158     T = strchr (str, 'T');
159     if (T == NULL || T != str + 8)
160       return GST_RTSP_EINVAL;
161
162     Z = strchr (T + 1, 'Z');
163     if (Z == NULL)
164       return GST_RTSP_EINVAL;
165
166     time->type = GST_RTSP_TIME_UTC;
167
168     if (sscanf (str, "%4d%2d%2dT%2d%2d%lfZ", &year, &month, &day, &hours,
169             &mins, &secs) != 6)
170       return GST_RTSP_EINVAL;
171
172     time2->year = year;
173     time2->month = month;
174     time2->day = day;
175     time->seconds = ((hours * 60) + mins) * 60 + secs;
176   }
177   return GST_RTSP_OK;
178 }
179
180 /*   utc-range    =   "clock" "=" utc-time "-" [ utc-time ]
181  */
182 static GstRTSPResult
183 parse_utc_range (const gchar * str, GstRTSPTimeRange * range)
184 {
185   GstRTSPResult res;
186   gchar *p;
187
188   range->unit = GST_RTSP_RANGE_CLOCK;
189
190   /* find '-' separator, can't have a single - */
191   p = strstr (str, "-");
192   if (p == NULL || p == str)
193     return GST_RTSP_EINVAL;
194
195   if ((res = parse_utc_time (str, &range->min, &range->min2, p)) != GST_RTSP_OK)
196     goto done;
197
198   res = parse_utc_time (p + 1, &range->max, &range->max2, NULL);
199
200 done:
201   return res;
202 }
203
204 /* smpte-time   =   1*2DIGIT ":" 1*2DIGIT ":" 1*2DIGIT [ ":" 1*2DIGIT ]
205  *                     [ "." 1*2DIGIT ]
206  *  hours:minutes:seconds:frames.subframes
207 */
208 static GstRTSPResult
209 parse_smpte_time (const gchar * str, GstRTSPTime * time, GstRTSPTime2 * time2,
210     const gchar * limit)
211 {
212   gint hours, mins, secs;
213
214   if (str[0] == '\0') {
215     time->type = GST_RTSP_TIME_END;
216     return GST_RTSP_OK;
217   } else {
218     if (sscanf (str, "%2d:%2d:%2d", &hours, &mins, &secs) != 3)
219       return GST_RTSP_EINVAL;
220
221     time->type = GST_RTSP_TIME_FRAMES;
222     time->seconds = ((hours * 60) + mins) * 60 + secs;
223     str = strchr (str, ':');
224     str = strchr (str + 1, ':');
225     str = strchr (str + 1, ':');
226     if (str && (limit == NULL || str < limit))
227       time2->frames = gst_strtod (str + 1);
228   }
229   return GST_RTSP_OK;
230 }
231
232 /* smpte-range  =   smpte-type "=" smpte-time "-" [ smpte-time ]
233  */
234 static GstRTSPResult
235 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
236 {
237   GstRTSPResult res;
238   gchar *p;
239
240   /* find '-' separator, can't have a single - */
241   p = strstr (str, "-");
242   if (p == NULL || p == str)
243     return GST_RTSP_EINVAL;
244
245   if ((res =
246           parse_smpte_time (str, &range->min, &range->min2, p)) != GST_RTSP_OK)
247     goto done;
248
249   res = parse_smpte_time (p + 1, &range->max, &range->max2, NULL);
250
251 done:
252   return res;
253 }
254
255 /**
256  * gst_rtsp_range_parse:
257  * @rangestr: a range string to parse
258  * @range: location to hold the #GstRTSPTimeRange result
259  *
260  * Parse @rangestr to a #GstRTSPTimeRange.
261  *
262  * Returns: #GST_RTSP_OK on success.
263  */
264 GstRTSPResult
265 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
266 {
267   GstRTSPResult ret;
268   GstRTSPTimeRange *res;
269   gchar *p;
270
271   g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
272   g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
273
274   res = g_new0 (GstRTSPTimeRange, 1);
275
276   p = (gchar *) rangestr;
277   /* first figure out the units of the range */
278   if (g_str_has_prefix (p, "npt=")) {
279     ret = parse_npt_range (p + 4, res);
280   } else if (g_str_has_prefix (p, "clock=")) {
281     ret = parse_utc_range (p + 6, res);
282   } else if (g_str_has_prefix (p, "smpte=")) {
283     res->unit = GST_RTSP_RANGE_SMPTE;
284     ret = parse_smpte_range (p + 6, res);
285   } else if (g_str_has_prefix (p, "smpte-30-drop=")) {
286     res->unit = GST_RTSP_RANGE_SMPTE_30_DROP;
287     ret = parse_smpte_range (p + 14, res);
288   } else if (g_str_has_prefix (p, "smpte-25=")) {
289     res->unit = GST_RTSP_RANGE_SMPTE_25;
290     ret = parse_smpte_range (p + 9, res);
291   } else
292     goto invalid;
293
294   if (ret != GST_RTSP_OK)
295     goto invalid;
296
297   *range = res;
298   return ret;
299
300   /* ERRORS */
301 invalid:
302   {
303     gst_rtsp_range_free (res);
304     return GST_RTSP_EINVAL;
305   }
306 }
307
308 static gboolean
309 npt_time_string (const GstRTSPTime * time, GString * string)
310 {
311   gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
312   gboolean res = TRUE;;
313
314   switch (time->type) {
315     case GST_RTSP_TIME_SECONDS:
316       /* need to format floating point value strings as in C locale */
317       g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, time->seconds);
318       g_string_append (string, dstrbuf);
319       break;
320     case GST_RTSP_TIME_NOW:
321       g_string_append (string, "now");
322       break;
323     case GST_RTSP_TIME_END:
324       break;
325     default:
326       res = FALSE;
327       break;
328   }
329   return res;
330 }
331
332 static gboolean
333 npt_range_string (const GstRTSPTimeRange * range, GString * string)
334 {
335   gboolean res;
336
337   if (!(res = npt_time_string (&range->min, string)))
338     goto done;
339
340   g_string_append (string, "-");
341
342   if (!(res = npt_time_string (&range->max, string)))
343     goto done;
344
345 done:
346   return res;
347 }
348
349 /**
350  * gst_rtsp_range_to_string:
351  * @range: a #GstRTSPTimeRange
352  *
353  * Convert @range into a string representation.
354  *
355  * Returns: The string representation of @range. g_free() after usage.
356  */
357 gchar *
358 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
359 {
360   gchar *result = NULL;
361   GString *string;
362
363   g_return_val_if_fail (range != NULL, NULL);
364
365   string = g_string_new ("");
366
367   switch (range->unit) {
368     case GST_RTSP_RANGE_NPT:
369       g_string_append (string, "npt=");
370       if (!npt_range_string (range, string)) {
371         g_string_free (string, TRUE);
372         string = NULL;
373       }
374       break;
375     case GST_RTSP_RANGE_SMPTE:
376     case GST_RTSP_RANGE_SMPTE_30_DROP:
377     case GST_RTSP_RANGE_SMPTE_25:
378     case GST_RTSP_RANGE_CLOCK:
379     default:
380       g_warning ("time range unit not yet implemented");
381       g_string_free (string, TRUE);
382       string = NULL;
383       break;
384   }
385   if (string)
386     result = g_string_free (string, FALSE);
387
388   return result;
389 }
390
391 /**
392  * gst_rtsp_range_free:
393  * @range: a #GstRTSPTimeRange
394  *
395  * Free the memory allocated by @range.
396  */
397 void
398 gst_rtsp_range_free (GstRTSPTimeRange * range)
399 {
400   g_return_if_fail (range != NULL);
401
402   g_free (range);
403 }
404
405 static GstClockTime
406 get_seconds (const GstRTSPTime * t)
407 {
408   gint num, denom;
409   /* don't do direct multiply with GST_SECOND to avoid rounding
410    * errors */
411   gst_util_double_to_fraction (t->seconds, &num, &denom);
412   return gst_util_uint64_scale_int (GST_SECOND, num, denom);
413 }
414
415 static GstClockTime
416 get_frames (const GstRTSPTime2 * t, GstRTSPRangeUnit unit)
417 {
418   gint num, denom;
419
420   gst_util_double_to_fraction (t->frames, &num, &denom);
421
422   switch (unit) {
423     case GST_RTSP_RANGE_SMPTE_25:
424       denom *= 25;
425       break;
426     case GST_RTSP_RANGE_SMPTE:
427     case GST_RTSP_RANGE_SMPTE_30_DROP:
428     default:
429       num *= 1001;
430       denom *= 30003;
431       break;
432   }
433   return gst_util_uint64_scale_int (GST_SECOND, num, denom);
434 }
435
436 static GstClockTime
437 get_time (GstRTSPRangeUnit unit, const GstRTSPTime * t1,
438     const GstRTSPTime2 * t2)
439 {
440   GstClockTime res;
441
442   switch (t1->type) {
443     case GST_RTSP_TIME_SECONDS:
444     {
445       res = get_seconds (t1);
446       break;
447     }
448     case GST_RTSP_TIME_UTC:
449     {
450       GDateTime *dt, *bt;
451       GTimeSpan span;
452
453       /* make time base, we use 1900 */
454       bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
455       /* convert to GDateTime without the seconds */
456       dt = g_date_time_new_utc (t2->year, t2->month, t2->day, 0, 0, 0.0);
457       /* get amount of microseconds */
458       span = g_date_time_difference (bt, dt);
459       g_date_time_unref (bt);
460       g_date_time_unref (dt);
461       /* add seconds */
462       res = get_seconds (t1) + (span * 1000);
463       break;
464     }
465     case GST_RTSP_TIME_FRAMES:
466       res = get_seconds (t1);
467       res += get_frames (t2, unit);
468       break;
469     default:
470     case GST_RTSP_TIME_NOW:
471     case GST_RTSP_TIME_END:
472       res = GST_CLOCK_TIME_NONE;
473       break;
474   }
475   return res;
476 }
477
478 /**
479  * gst_rtsp_range_get_times:
480  * @range: a #GstRTSPTimeRange
481  * @min: result minimum #GstClockTime
482  * @max: result maximum #GstClockTime
483  *
484  * Retrieve the minimum and maximum values from @range converted to
485  * #GstClockTime in @min and @max.
486  *
487  * A value of %GST_CLOCK_TIME_NONE will be used to signal #GST_RTSP_TIME_NOW
488  * and #GST_RTSP_TIME_END for @min and @max respectively.
489  *
490  * UTC times will be converted to nanoseconds since 1900.
491  *
492  * Returns: %TRUE on success.
493  *
494  * Since: 1.1.1
495  */
496 gboolean
497 gst_rtsp_range_get_times (const GstRTSPTimeRange * range,
498     GstClockTime * min, GstClockTime * max)
499 {
500   g_return_val_if_fail (range != NULL, FALSE);
501
502   if (min)
503     *min = get_time (range->unit, &range->min, &range->min2);
504   if (max)
505     *max = get_time (range->unit, &range->max, &range->max2);
506
507   return TRUE;
508 }