2 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
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.
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.
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.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
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:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
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
44 * SECTION:gstrtsprange
45 * @short_description: dealing with time ranges
47 * Provides helper functions to deal with time ranges.
49 * Last reviewed on 2007-07-25 (0.10.14)
56 #include "gstrtsprange.h"
59 gst_strtod (const gchar * dstr)
61 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
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);
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
80 parse_npt_time (const gchar * str, GstRTSPTime * time)
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, ":")) {
89 if (sscanf (str, "%2d:%2d:", &hours, &mins) != 2)
90 return GST_RTSP_EINVAL;
92 str = strchr (str, ':');
93 str = strchr (str + 1, ':');
95 return GST_RTSP_EINVAL;
97 time->type = GST_RTSP_TIME_SECONDS;
98 time->seconds = ((hours * 60) + mins) * 60 + gst_strtod (str + 1);
100 time->type = GST_RTSP_TIME_SECONDS;
101 time->seconds = gst_strtod (str);
106 /* npt-range = ( npt-time "-" [ npt-time ] ) | ( "-" npt-time )
109 parse_npt_range (const gchar * str, GstRTSPTimeRange * range)
114 range->unit = GST_RTSP_RANGE_NPT;
116 /* find '-' separator */
117 p = strstr (str, "-");
119 return GST_RTSP_EINVAL;
121 if ((res = parse_npt_time (str, &range->min)) != GST_RTSP_OK)
124 res = parse_npt_time (p + 1, &range->max);
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;
135 /* utc-time = utc-date "T" utc-time "Z"
136 * utc-date = 8DIGIT ; < YYYYMMDD >
137 * utc-time = 6DIGIT [ "." fraction ] ; < HHMMSS.fraction >
139 * Example for November 8, 1996 at 14h37 and 20 and a quarter seconds
142 * 19961108T143720.25Z
145 parse_utc_time (const gchar * str, GstRTSPTime * time, GstRTSPTime2 * time2,
149 if (str[0] == '\0') {
150 time->type = GST_RTSP_TIME_END;
153 gint year, month, day;
158 T = strchr (str, 'T');
159 if (T == NULL || T != str + 8)
160 return GST_RTSP_EINVAL;
162 Z = strchr (T + 1, 'Z');
164 return GST_RTSP_EINVAL;
166 time->type = GST_RTSP_TIME_UTC;
168 if (sscanf (str, "%4d%2d%2dT%2d%2d%lfZ", &year, &month, &day, &hours,
170 return GST_RTSP_EINVAL;
173 time2->month = month;
175 time->seconds = ((hours * 60) + mins) * 60 + secs;
180 /* utc-range = "clock" "=" utc-time "-" [ utc-time ]
183 parse_utc_range (const gchar * str, GstRTSPTimeRange * range)
188 range->unit = GST_RTSP_RANGE_CLOCK;
190 /* find '-' separator, can't have a single - */
191 p = strstr (str, "-");
192 if (p == NULL || p == str)
193 return GST_RTSP_EINVAL;
195 if ((res = parse_utc_time (str, &range->min, &range->min2, p)) != GST_RTSP_OK)
198 res = parse_utc_time (p + 1, &range->max, &range->max2, NULL);
204 /* smpte-time = 1*2DIGIT ":" 1*2DIGIT ":" 1*2DIGIT [ ":" 1*2DIGIT ]
206 * hours:minutes:seconds:frames.subframes
209 parse_smpte_time (const gchar * str, GstRTSPTime * time, GstRTSPTime2 * time2,
212 gint hours, mins, secs;
214 if (str[0] == '\0') {
215 time->type = GST_RTSP_TIME_END;
218 if (sscanf (str, "%2d:%2d:%2d", &hours, &mins, &secs) != 3)
219 return GST_RTSP_EINVAL;
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);
232 /* smpte-range = smpte-type "=" smpte-time "-" [ smpte-time ]
235 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
240 /* find '-' separator, can't have a single - */
241 p = strstr (str, "-");
242 if (p == NULL || p == str)
243 return GST_RTSP_EINVAL;
246 parse_smpte_time (str, &range->min, &range->min2, p)) != GST_RTSP_OK)
249 res = parse_smpte_time (p + 1, &range->max, &range->max2, NULL);
256 * gst_rtsp_range_parse:
257 * @rangestr: a range string to parse
258 * @range: location to hold the #GstRTSPTimeRange result
260 * Parse @rangestr to a #GstRTSPTimeRange.
262 * Returns: #GST_RTSP_OK on success.
265 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
268 GstRTSPTimeRange *res;
271 g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
272 g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
274 res = g_new0 (GstRTSPTimeRange, 1);
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);
294 if (ret != GST_RTSP_OK)
303 gst_rtsp_range_free (res);
304 return GST_RTSP_EINVAL;
309 string_append_dtostr (GString * string, gdouble value, guint precision)
311 gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
318 value += 4.9 * pow (10.0, precision * -1.0);
320 g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, value);
322 dot = strchr (dstrbuf, '.');
327 for (; *dot != '.' && *dot != '0'; dot++);
329 if ((dot - dstrbuf) + precision < G_ASCII_DTOSTR_BUF_SIZE)
332 len = strlen (dstrbuf);
333 while (dstrbuf[len - 1] == '0')
335 if (dstrbuf[len - 1] == '.')
340 g_string_append (string, dstrbuf);
344 time_to_string (const GstRTSPTime * t1, const GstRTSPTime2 * t2,
347 gboolean res = TRUE;;
350 case GST_RTSP_TIME_SECONDS:
351 /* need to format floating point value strings as in C locale */
352 string_append_dtostr (string, t1->seconds +
353 (t1->seconds ? 0.00000000005 : 0), 9);
355 case GST_RTSP_TIME_NOW:
356 g_string_append (string, "now");
358 case GST_RTSP_TIME_END:
360 case GST_RTSP_TIME_FRAMES:
362 gint64 sec = t1->seconds;
364 /* need to format floating point value strings as in C locale */
365 g_string_append_printf (string, "%d:%02d:%02d", (gint) sec / (60 * 60),
366 (gint) (sec % (60 * 60)) / 60, (gint) sec % 60);
368 if (t2->frames > 0.0) {
369 g_string_append_printf (string, ":%s", t2->frames < 10 ? "0" : "");
370 string_append_dtostr (string, t2->frames + 0.005, 2);
374 case GST_RTSP_TIME_UTC:
376 gint64 sec = t1->seconds;
380 hours = sec / (60 * 60);
381 sec -= hours * 60 * 60;
383 sec = ((hours * 60) + minutes) * 60;
384 seconds = t1->seconds - sec;
386 seconds += 0.00000000005;
388 g_string_append_printf (string, "%04d%02d%02dT%02d%02d%s",
389 t2->year, t2->month, t2->day, hours, minutes,
390 seconds < 10 ? "0" : "");
391 string_append_dtostr (string, seconds, 9);
392 g_string_append (string, "Z");
403 range_to_string (const GstRTSPTimeRange * range, GString * string)
407 if (!(res = time_to_string (&range->min, &range->min2, string)))
410 g_string_append (string, "-");
412 if (!(res = time_to_string (&range->max, &range->max2, string)))
420 * gst_rtsp_range_to_string:
421 * @range: a #GstRTSPTimeRange
423 * Convert @range into a string representation.
425 * Returns: The string representation of @range. g_free() after usage.
428 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
432 g_return_val_if_fail (range != NULL, NULL);
434 switch (range->unit) {
435 case GST_RTSP_RANGE_NPT:
436 string = g_string_new ("npt=");
438 case GST_RTSP_RANGE_SMPTE:
439 case GST_RTSP_RANGE_SMPTE_30_DROP:
440 string = g_string_new ("smpte=");
442 case GST_RTSP_RANGE_SMPTE_25:
443 string = g_string_new ("smpte-25=");
445 case GST_RTSP_RANGE_CLOCK:
446 string = g_string_new ("clock=");
449 goto not_implemented;
452 if (!range_to_string (range, string))
455 return g_string_free (string, FALSE);
460 g_warning ("time range unit not yet implemented");
465 g_string_free (string, TRUE);
471 * gst_rtsp_range_free:
472 * @range: a #GstRTSPTimeRange
474 * Free the memory allocated by @range.
477 gst_rtsp_range_free (GstRTSPTimeRange * range)
479 g_return_if_fail (range != NULL);
485 get_seconds (const GstRTSPTime * t)
487 if (t->seconds < G_MAXINT) {
489 /* Don't do direct multiply with GST_SECOND to avoid rounding
491 * This only works for "small" numbers, because num is limited to 32-bit
493 gst_util_double_to_fraction (t->seconds, &num, &denom);
494 return gst_util_uint64_scale_int (GST_SECOND, num, denom);
496 return t->seconds * GST_SECOND;
501 get_frames (const GstRTSPTime2 * t, GstRTSPRangeUnit unit)
505 gst_util_double_to_fraction (t->frames, &num, &denom);
508 case GST_RTSP_RANGE_SMPTE_25:
511 case GST_RTSP_RANGE_SMPTE:
512 case GST_RTSP_RANGE_SMPTE_30_DROP:
518 return gst_util_uint64_scale_int (GST_SECOND, num, denom);
522 get_time (GstRTSPRangeUnit unit, const GstRTSPTime * t1,
523 const GstRTSPTime2 * t2)
528 case GST_RTSP_TIME_SECONDS:
530 res = get_seconds (t1);
533 case GST_RTSP_TIME_UTC:
538 /* make time base, we use 1900 */
539 bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
540 /* convert to GDateTime without the seconds */
541 dt = g_date_time_new_utc (t2->year, t2->month, t2->day, 0, 0, 0.0);
542 /* get amount of microseconds */
543 span = g_date_time_difference (dt, bt);
544 g_date_time_unref (bt);
545 g_date_time_unref (dt);
547 res = get_seconds (t1) + (span * 1000);
550 case GST_RTSP_TIME_FRAMES:
551 res = get_seconds (t1);
552 res += get_frames (t2, unit);
555 case GST_RTSP_TIME_NOW:
556 case GST_RTSP_TIME_END:
557 res = GST_CLOCK_TIME_NONE;
564 * gst_rtsp_range_get_times:
565 * @range: a #GstRTSPTimeRange
566 * @min: result minimum #GstClockTime
567 * @max: result maximum #GstClockTime
569 * Retrieve the minimum and maximum values from @range converted to
570 * #GstClockTime in @min and @max.
572 * A value of %GST_CLOCK_TIME_NONE will be used to signal #GST_RTSP_TIME_NOW
573 * and #GST_RTSP_TIME_END for @min and @max respectively.
575 * UTC times will be converted to nanoseconds since 1900.
577 * Returns: %TRUE on success.
582 gst_rtsp_range_get_times (const GstRTSPTimeRange * range,
583 GstClockTime * min, GstClockTime * max)
585 g_return_val_if_fail (range != NULL, FALSE);
588 *min = get_time (range->unit, &range->min, &range->min2);
590 *max = get_time (range->unit, &range->max, &range->max2);
596 set_time (GstRTSPTime * time, GstRTSPTime2 * time2, GstRTSPRangeUnit unit,
597 GstClockTime clock_time)
599 memset (time, 0, sizeof (GstRTSPTime));
600 memset (time2, 0, sizeof (GstRTSPTime2));
602 if (clock_time == GST_CLOCK_TIME_NONE) {
603 time->type = GST_RTSP_TIME_END;
608 case GST_RTSP_RANGE_SMPTE:
609 case GST_RTSP_RANGE_SMPTE_30_DROP:
611 time->seconds = (guint64) (clock_time / GST_SECOND);
612 time2->frames = 30003 * (clock_time % GST_SECOND) /
613 (gdouble) (1001 * GST_SECOND);
614 time->type = GST_RTSP_TIME_FRAMES;
615 g_assert (time2->frames < 30);
618 case GST_RTSP_RANGE_SMPTE_25:
620 time->seconds = (guint64) (clock_time / GST_SECOND);
621 time2->frames = (25 * (clock_time % GST_SECOND)) / (gdouble) GST_SECOND;
622 time->type = GST_RTSP_TIME_FRAMES;
623 g_assert (time2->frames < 25);
626 case GST_RTSP_RANGE_NPT:
628 time->seconds = (gdouble) clock_time / (gdouble) GST_SECOND;
629 time->type = GST_RTSP_TIME_SECONDS;
632 case GST_RTSP_RANGE_CLOCK:
634 GDateTime *bt, *datetime;
635 GstClockTime subsecond = clock_time % GST_SECOND;
637 bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
638 datetime = g_date_time_add_seconds (bt, clock_time / GST_SECOND);
640 time2->year = g_date_time_get_year (datetime);
641 time2->month = g_date_time_get_month (datetime);
642 time2->day = g_date_time_get_day_of_month (datetime);
644 time->seconds = g_date_time_get_hour (datetime) * 60 * 60;
645 time->seconds += g_date_time_get_minute (datetime) * 60;
646 time->seconds += g_date_time_get_seconds (datetime);
647 time->seconds += (gdouble) subsecond / (gdouble) GST_SECOND;
648 time->type = GST_RTSP_TIME_UTC;
650 g_date_time_unref (bt);
651 g_date_time_unref (datetime);
656 if (time->seconds < 0.000000001)
658 if (time2->frames < 0.000000001)
663 * gst_rtsp_range_convert_units:
664 * @range: a #GstRTSPTimeRange
665 * @unit: the unit to convert the range into
667 * Converts the range in-place between different types of units.
668 * Ranges containing the special value #GST_RTSP_TIME_NOW can not be
669 * converted as these are only valid for #GST_RTSP_RANGE_NPT.
671 * Returns: %TRUE if the range could be converted
675 gst_rtsp_range_convert_units (GstRTSPTimeRange * range, GstRTSPRangeUnit unit)
677 if (range->unit == unit)
680 if (range->min.type == GST_RTSP_TIME_NOW ||
681 range->max.type == GST_RTSP_TIME_NOW)
684 set_time (&range->min, &range->min2, unit,
685 get_time (range->unit, &range->min, &range->min2));
686 set_time (&range->max, &range->max2, unit,
687 get_time (range->unit, &range->max, &range->max2));