rtsprange: add string conversion for new formats
[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 time_to_string (const GstRTSPTime * t1, const GstRTSPTime2 * t2,
310     GString * string)
311 {
312   gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
313   gboolean res = TRUE;;
314
315   switch (t1->type) {
316     case GST_RTSP_TIME_SECONDS:
317       /* need to format floating point value strings as in C locale */
318       g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t1->seconds);
319       g_string_append (string, dstrbuf);
320       break;
321     case GST_RTSP_TIME_NOW:
322       g_string_append (string, "now");
323       break;
324     case GST_RTSP_TIME_END:
325       break;
326     case GST_RTSP_TIME_FRAMES:
327     {
328       gint64 sec = t1->seconds;
329
330       /* need to format floating point value strings as in C locale */
331       g_string_append_printf (string, "%d:%02d:%02d:", (gint) sec / 60 * 60,
332           (gint) sec / 60, (gint) sec % 60);
333
334       if (t2->frames > 0.0) {
335         g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t2->frames);
336         g_string_append (string, dstrbuf);
337       }
338       break;
339     }
340     case GST_RTSP_TIME_UTC:
341     {
342       gint64 sec = t1->seconds;
343
344       g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t1->seconds - sec);
345       g_string_append_printf (string, "%04d%02d%02dT%02d%02d%02d%sZ",
346           t2->year, t2->month, t2->day, (gint) sec / 60 * 60,
347           (gint) sec / 60, (gint) sec % 60, dstrbuf);
348       break;
349     }
350     default:
351       res = FALSE;
352       break;
353   }
354   return res;
355 }
356
357 static gboolean
358 range_to_string (const GstRTSPTimeRange * range, GString * string)
359 {
360   gboolean res;
361
362   if (!(res = time_to_string (&range->min, &range->min2, string)))
363     goto done;
364
365   g_string_append (string, "-");
366
367   if (!(res = time_to_string (&range->max, &range->max2, string)))
368     goto done;
369
370 done:
371   return res;
372 }
373
374 /**
375  * gst_rtsp_range_to_string:
376  * @range: a #GstRTSPTimeRange
377  *
378  * Convert @range into a string representation.
379  *
380  * Returns: The string representation of @range. g_free() after usage.
381  */
382 gchar *
383 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
384 {
385   GString *string;
386
387   g_return_val_if_fail (range != NULL, NULL);
388
389   switch (range->unit) {
390     case GST_RTSP_RANGE_NPT:
391       string = g_string_new ("npt=");
392       break;
393     case GST_RTSP_RANGE_SMPTE:
394     case GST_RTSP_RANGE_SMPTE_30_DROP:
395       string = g_string_new ("smpte=");
396       break;
397     case GST_RTSP_RANGE_SMPTE_25:
398       string = g_string_new ("smpte-25=");
399       break;
400     case GST_RTSP_RANGE_CLOCK:
401       string = g_string_new ("clock=");
402       break;
403     default:
404       goto not_implemented;
405   }
406
407   if (!range_to_string (range, string))
408     goto format_failed;
409
410   return g_string_free (string, FALSE);
411
412   /* ERRORS */
413 not_implemented:
414   {
415     g_warning ("time range unit not yet implemented");
416     return NULL;
417   }
418 format_failed:
419   {
420     g_string_free (string, TRUE);
421     return NULL;
422   }
423 }
424
425 /**
426  * gst_rtsp_range_free:
427  * @range: a #GstRTSPTimeRange
428  *
429  * Free the memory allocated by @range.
430  */
431 void
432 gst_rtsp_range_free (GstRTSPTimeRange * range)
433 {
434   g_return_if_fail (range != NULL);
435
436   g_free (range);
437 }
438
439 static GstClockTime
440 get_seconds (const GstRTSPTime * t)
441 {
442   gint num, denom;
443   /* don't do direct multiply with GST_SECOND to avoid rounding
444    * errors */
445   gst_util_double_to_fraction (t->seconds, &num, &denom);
446   return gst_util_uint64_scale_int (GST_SECOND, num, denom);
447 }
448
449 static GstClockTime
450 get_frames (const GstRTSPTime2 * t, GstRTSPRangeUnit unit)
451 {
452   gint num, denom;
453
454   gst_util_double_to_fraction (t->frames, &num, &denom);
455
456   switch (unit) {
457     case GST_RTSP_RANGE_SMPTE_25:
458       denom *= 25;
459       break;
460     case GST_RTSP_RANGE_SMPTE:
461     case GST_RTSP_RANGE_SMPTE_30_DROP:
462     default:
463       num *= 1001;
464       denom *= 30003;
465       break;
466   }
467   return gst_util_uint64_scale_int (GST_SECOND, num, denom);
468 }
469
470 static GstClockTime
471 get_time (GstRTSPRangeUnit unit, const GstRTSPTime * t1,
472     const GstRTSPTime2 * t2)
473 {
474   GstClockTime res;
475
476   switch (t1->type) {
477     case GST_RTSP_TIME_SECONDS:
478     {
479       res = get_seconds (t1);
480       break;
481     }
482     case GST_RTSP_TIME_UTC:
483     {
484       GDateTime *dt, *bt;
485       GTimeSpan span;
486
487       /* make time base, we use 1900 */
488       bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
489       /* convert to GDateTime without the seconds */
490       dt = g_date_time_new_utc (t2->year, t2->month, t2->day, 0, 0, 0.0);
491       /* get amount of microseconds */
492       span = g_date_time_difference (bt, dt);
493       g_date_time_unref (bt);
494       g_date_time_unref (dt);
495       /* add seconds */
496       res = get_seconds (t1) + (span * 1000);
497       break;
498     }
499     case GST_RTSP_TIME_FRAMES:
500       res = get_seconds (t1);
501       res += get_frames (t2, unit);
502       break;
503     default:
504     case GST_RTSP_TIME_NOW:
505     case GST_RTSP_TIME_END:
506       res = GST_CLOCK_TIME_NONE;
507       break;
508   }
509   return res;
510 }
511
512 /**
513  * gst_rtsp_range_get_times:
514  * @range: a #GstRTSPTimeRange
515  * @min: result minimum #GstClockTime
516  * @max: result maximum #GstClockTime
517  *
518  * Retrieve the minimum and maximum values from @range converted to
519  * #GstClockTime in @min and @max.
520  *
521  * A value of %GST_CLOCK_TIME_NONE will be used to signal #GST_RTSP_TIME_NOW
522  * and #GST_RTSP_TIME_END for @min and @max respectively.
523  *
524  * UTC times will be converted to nanoseconds since 1900.
525  *
526  * Returns: %TRUE on success.
527  *
528  * Since: 1.1.1
529  */
530 gboolean
531 gst_rtsp_range_get_times (const GstRTSPTimeRange * range,
532     GstClockTime * min, GstClockTime * max)
533 {
534   g_return_val_if_fail (range != NULL, FALSE);
535
536   if (min)
537     *min = get_time (range->unit, &range->min, &range->min2);
538   if (max)
539     *max = get_time (range->unit, &range->max, &range->max2);
540
541   return TRUE;
542 }