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 time_to_string (const GstRTSPTime * t1, const GstRTSPTime2 * t2,
312 gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
313 gboolean res = TRUE;;
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);
321 case GST_RTSP_TIME_NOW:
322 g_string_append (string, "now");
324 case GST_RTSP_TIME_END:
326 case GST_RTSP_TIME_FRAMES:
328 gint64 sec = t1->seconds;
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);
334 if (t2->frames > 0.0) {
335 g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t2->frames);
336 g_string_append (string, dstrbuf);
340 case GST_RTSP_TIME_UTC:
342 gint64 sec = t1->seconds;
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);
358 range_to_string (const GstRTSPTimeRange * range, GString * string)
362 if (!(res = time_to_string (&range->min, &range->min2, string)))
365 g_string_append (string, "-");
367 if (!(res = time_to_string (&range->max, &range->max2, string)))
375 * gst_rtsp_range_to_string:
376 * @range: a #GstRTSPTimeRange
378 * Convert @range into a string representation.
380 * Returns: The string representation of @range. g_free() after usage.
383 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
387 g_return_val_if_fail (range != NULL, NULL);
389 switch (range->unit) {
390 case GST_RTSP_RANGE_NPT:
391 string = g_string_new ("npt=");
393 case GST_RTSP_RANGE_SMPTE:
394 case GST_RTSP_RANGE_SMPTE_30_DROP:
395 string = g_string_new ("smpte=");
397 case GST_RTSP_RANGE_SMPTE_25:
398 string = g_string_new ("smpte-25=");
400 case GST_RTSP_RANGE_CLOCK:
401 string = g_string_new ("clock=");
404 goto not_implemented;
407 if (!range_to_string (range, string))
410 return g_string_free (string, FALSE);
415 g_warning ("time range unit not yet implemented");
420 g_string_free (string, TRUE);
426 * gst_rtsp_range_free:
427 * @range: a #GstRTSPTimeRange
429 * Free the memory allocated by @range.
432 gst_rtsp_range_free (GstRTSPTimeRange * range)
434 g_return_if_fail (range != NULL);
440 get_seconds (const GstRTSPTime * t)
443 /* don't do direct multiply with GST_SECOND to avoid rounding
445 gst_util_double_to_fraction (t->seconds, &num, &denom);
446 return gst_util_uint64_scale_int (GST_SECOND, num, denom);
450 get_frames (const GstRTSPTime2 * t, GstRTSPRangeUnit unit)
454 gst_util_double_to_fraction (t->frames, &num, &denom);
457 case GST_RTSP_RANGE_SMPTE_25:
460 case GST_RTSP_RANGE_SMPTE:
461 case GST_RTSP_RANGE_SMPTE_30_DROP:
467 return gst_util_uint64_scale_int (GST_SECOND, num, denom);
471 get_time (GstRTSPRangeUnit unit, const GstRTSPTime * t1,
472 const GstRTSPTime2 * t2)
477 case GST_RTSP_TIME_SECONDS:
479 res = get_seconds (t1);
482 case GST_RTSP_TIME_UTC:
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);
496 res = get_seconds (t1) + (span * 1000);
499 case GST_RTSP_TIME_FRAMES:
500 res = get_seconds (t1);
501 res += get_frames (t2, unit);
504 case GST_RTSP_TIME_NOW:
505 case GST_RTSP_TIME_END:
506 res = GST_CLOCK_TIME_NONE;
513 * gst_rtsp_range_get_times:
514 * @range: a #GstRTSPTimeRange
515 * @min: result minimum #GstClockTime
516 * @max: result maximum #GstClockTime
518 * Retrieve the minimum and maximum values from @range converted to
519 * #GstClockTime in @min and @max.
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.
524 * UTC times will be converted to nanoseconds since 1900.
526 * Returns: %TRUE on success.
531 gst_rtsp_range_get_times (const GstRTSPTimeRange * range,
532 GstClockTime * min, GstClockTime * max)
534 g_return_val_if_fail (range != NULL, FALSE);
537 *min = get_time (range->unit, &range->min, &range->min2);
539 *max = get_time (range->unit, &range->max, &range->max2);