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  * @title: GstRTSPTimeRange
46  * @short_description: dealing with time ranges
47  *
48  * Provides helper functions to deal with time ranges.
49  */
50 #ifdef HAVE_CONFIG_H
51 #include "config.h"
52 #endif
53 
54 #include <math.h>
55 #include <stdio.h>
56 #include <string.h>
57 
58 #include "gstrtsprange.h"
59 
60 static gdouble
gst_strtod(const gchar * dstr)61 gst_strtod (const gchar * dstr)
62 {
63   gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
64 
65   /* canonicalise floating point string so we can handle float strings
66    * in the form "24.930" or "24,930" irrespective of the current locale.
67    * We should always be getting floats in 24.930 format with a floating point,
68    * but let's accept malformed ones as well, easy mistake to make after all */
69   g_strlcpy (s, dstr, sizeof (s));
70   g_strdelimit (s, ",", '.');
71   return g_ascii_strtod (s, NULL);
72 }
73 
74 /* npt-time     =   "now" | npt-sec | npt-hhmmss
75  * npt-sec      =   1*DIGIT [ "." *DIGIT ]
76  * npt-hhmmss   =   npt-hh ":" npt-mm ":" npt-ss [ "." *DIGIT ]
77  * npt-hh       =   1*DIGIT     ; any positive number
78  * npt-mm       =   1*2DIGIT    ; 0-59
79  * npt-ss       =   1*2DIGIT    ; 0-59
80  */
81 static GstRTSPResult
parse_npt_time(const gchar * str,GstRTSPTime * time)82 parse_npt_time (const gchar * str, GstRTSPTime * time)
83 {
84   if (strncmp (str, "now", 3) == 0) {
85     time->type = GST_RTSP_TIME_NOW;
86   } else if (str[0] == '\0' || str[0] == '-') {
87     time->type = GST_RTSP_TIME_END;
88   } else if (strstr (str, ":")) {
89     gint hours, mins;
90 
91     if (sscanf (str, "%2d:%2d:", &hours, &mins) != 2)
92       return GST_RTSP_EINVAL;
93 
94     str = strchr (str, ':');
95     str = strchr (str + 1, ':');
96     if (str == NULL)
97       return GST_RTSP_EINVAL;
98 
99     time->type = GST_RTSP_TIME_SECONDS;
100     time->seconds = ((hours * 60) + mins) * 60 + gst_strtod (str + 1);
101   } else {
102     time->type = GST_RTSP_TIME_SECONDS;
103     time->seconds = gst_strtod (str);
104   }
105   return GST_RTSP_OK;
106 }
107 
108 /* npt-range    =   ( npt-time "-" [ npt-time ] ) | ( "-" npt-time )
109  */
110 static GstRTSPResult
parse_npt_range(const gchar * str,GstRTSPTimeRange * range)111 parse_npt_range (const gchar * str, GstRTSPTimeRange * range)
112 {
113   GstRTSPResult res;
114   gchar *p;
115 
116   range->unit = GST_RTSP_RANGE_NPT;
117 
118   /* find '-' separator */
119   p = strstr (str, "-");
120   if (p == NULL)
121     return GST_RTSP_EINVAL;
122 
123   if ((res = parse_npt_time (str, &range->min)) != GST_RTSP_OK)
124     goto done;
125 
126   res = parse_npt_time (p + 1, &range->max);
127 
128   /* a single - is not allowed */
129   if (range->min.type == GST_RTSP_TIME_END
130       && range->max.type == GST_RTSP_TIME_END)
131     return GST_RTSP_EINVAL;
132 
133 done:
134   return res;
135 }
136 
137 /*   utc-time     =   utc-date "T" utc-time "Z"
138  *   utc-date     =   8DIGIT                    ; < YYYYMMDD >
139  *   utc-time     =   6DIGIT [ "." fraction ]   ; < HHMMSS.fraction >
140  *
141  *   Example for November 8, 1996 at 14h37 and 20 and a quarter seconds
142  *   UTC:
143  *
144  *   19961108T143720.25Z
145  */
146 static GstRTSPResult
parse_utc_time(const gchar * str,GstRTSPTime * time,GstRTSPTime2 * time2,const gchar * limit)147 parse_utc_time (const gchar * str, GstRTSPTime * time, GstRTSPTime2 * time2,
148     const gchar * limit)
149 {
150 
151   if (str[0] == '\0') {
152     time->type = GST_RTSP_TIME_END;
153     return GST_RTSP_OK;
154   } else {
155     gint year, month, day;
156     gint hours, mins;
157     gdouble secs;
158     gchar *T, *Z;
159 
160     T = strchr (str, 'T');
161     if (T == NULL || T != str + 8)
162       return GST_RTSP_EINVAL;
163 
164     Z = strchr (T + 1, 'Z');
165     if (Z == NULL)
166       return GST_RTSP_EINVAL;
167 
168     time->type = GST_RTSP_TIME_UTC;
169 
170     if (sscanf (str, "%4d%2d%2dT%2d%2d%lfZ", &year, &month, &day, &hours,
171             &mins, &secs) != 6)
172       return GST_RTSP_EINVAL;
173 
174     time2->year = year;
175     time2->month = month;
176     time2->day = day;
177     time->seconds = ((hours * 60) + mins) * 60 + secs;
178   }
179   return GST_RTSP_OK;
180 }
181 
182 /*   utc-range    =   "clock" "=" utc-time "-" [ utc-time ]
183  */
184 static GstRTSPResult
parse_utc_range(const gchar * str,GstRTSPTimeRange * range)185 parse_utc_range (const gchar * str, GstRTSPTimeRange * range)
186 {
187   GstRTSPResult res;
188   gchar *p;
189 
190   range->unit = GST_RTSP_RANGE_CLOCK;
191 
192   /* find '-' separator, can't have a single - */
193   p = strstr (str, "-");
194   if (p == NULL || p == str)
195     return GST_RTSP_EINVAL;
196 
197   if ((res = parse_utc_time (str, &range->min, &range->min2, p)) != GST_RTSP_OK)
198     goto done;
199 
200   res = parse_utc_time (p + 1, &range->max, &range->max2, NULL);
201 
202 done:
203   return res;
204 }
205 
206 /* smpte-time   =   1*2DIGIT ":" 1*2DIGIT ":" 1*2DIGIT [ ":" 1*2DIGIT ]
207  *                     [ "." 1*2DIGIT ]
208  *  hours:minutes:seconds:frames.subframes
209 */
210 static GstRTSPResult
parse_smpte_time(const gchar * str,GstRTSPTime * time,GstRTSPTime2 * time2,const gchar * limit)211 parse_smpte_time (const gchar * str, GstRTSPTime * time, GstRTSPTime2 * time2,
212     const gchar * limit)
213 {
214   gint hours, mins, secs;
215 
216   if (str[0] == '\0') {
217     time->type = GST_RTSP_TIME_END;
218     return GST_RTSP_OK;
219   } else {
220     if (sscanf (str, "%2d:%2d:%2d", &hours, &mins, &secs) != 3)
221       return GST_RTSP_EINVAL;
222 
223     time->type = GST_RTSP_TIME_FRAMES;
224     time->seconds = ((hours * 60) + mins) * 60 + secs;
225     str = strchr (str, ':');
226     str = strchr (str + 1, ':');
227     str = strchr (str + 1, ':');
228     if (str && (limit == NULL || str < limit))
229       time2->frames = gst_strtod (str + 1);
230   }
231   return GST_RTSP_OK;
232 }
233 
234 /* smpte-range  =   smpte-type "=" smpte-time "-" [ smpte-time ]
235  */
236 static GstRTSPResult
parse_smpte_range(const gchar * str,GstRTSPTimeRange * range)237 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
238 {
239   GstRTSPResult res;
240   gchar *p;
241 
242   /* find '-' separator, can't have a single - */
243   p = strstr (str, "-");
244   if (p == NULL || p == str)
245     return GST_RTSP_EINVAL;
246 
247   if ((res =
248           parse_smpte_time (str, &range->min, &range->min2, p)) != GST_RTSP_OK)
249     goto done;
250 
251   res = parse_smpte_time (p + 1, &range->max, &range->max2, NULL);
252 
253 done:
254   return res;
255 }
256 
257 /**
258  * gst_rtsp_range_parse:
259  * @rangestr: a range string to parse
260  * @range: (out): location to hold the #GstRTSPTimeRange result
261  *
262  * Parse @rangestr to a #GstRTSPTimeRange.
263  *
264  * Returns: #GST_RTSP_OK on success.
265  */
266 GstRTSPResult
gst_rtsp_range_parse(const gchar * rangestr,GstRTSPTimeRange ** range)267 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
268 {
269   GstRTSPResult ret;
270   GstRTSPTimeRange *res;
271   gchar *p;
272 
273   g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
274   g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
275 
276   res = g_new0 (GstRTSPTimeRange, 1);
277 
278   p = (gchar *) rangestr;
279   /* first figure out the units of the range */
280   if (g_str_has_prefix (p, "npt=")) {
281     ret = parse_npt_range (p + 4, res);
282   } else if (g_str_has_prefix (p, "clock=")) {
283     ret = parse_utc_range (p + 6, res);
284   } else if (g_str_has_prefix (p, "smpte=")) {
285     res->unit = GST_RTSP_RANGE_SMPTE;
286     ret = parse_smpte_range (p + 6, res);
287   } else if (g_str_has_prefix (p, "smpte-30-drop=")) {
288     res->unit = GST_RTSP_RANGE_SMPTE_30_DROP;
289     ret = parse_smpte_range (p + 14, res);
290   } else if (g_str_has_prefix (p, "smpte-25=")) {
291     res->unit = GST_RTSP_RANGE_SMPTE_25;
292     ret = parse_smpte_range (p + 9, res);
293   } else
294     goto invalid;
295 
296   if (ret != GST_RTSP_OK)
297     goto invalid;
298 
299   *range = res;
300   return ret;
301 
302   /* ERRORS */
303 invalid:
304   {
305     gst_rtsp_range_free (res);
306     return GST_RTSP_EINVAL;
307   }
308 }
309 
310 static void
string_append_dtostr(GString * string,gdouble value,guint precision)311 string_append_dtostr (GString * string, gdouble value, guint precision)
312 {
313   gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
314   gchar *dot;
315   guint len;
316 
317   precision++;
318 
319   if (value != 0.0)
320     value += 4.9 * pow (10.0, precision * -1.0);
321 
322   g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, value);
323 
324   dot = strchr (dstrbuf, '.');
325 
326   if (dot == NULL)
327     goto done;
328 
329   for (; *dot != '.' && *dot != '0'; dot++);
330 
331   if ((dot - dstrbuf) + precision < G_ASCII_DTOSTR_BUF_SIZE)
332     dot[precision] = 0;
333 
334   len = strlen (dstrbuf);
335   while (dstrbuf[len - 1] == '0')
336     dstrbuf[--len] = 0;
337   if (dstrbuf[len - 1] == '.')
338     dstrbuf[--len] = 0;
339 
340 done:
341 
342   g_string_append (string, dstrbuf);
343 }
344 
345 static gboolean
time_to_string(const GstRTSPTime * t1,const GstRTSPTime2 * t2,GString * string)346 time_to_string (const GstRTSPTime * t1, const GstRTSPTime2 * t2,
347     GString * string)
348 {
349   gboolean res = TRUE;
350 
351   switch (t1->type) {
352     case GST_RTSP_TIME_SECONDS:
353       /* need to format floating point value strings as in C locale */
354       string_append_dtostr (string, t1->seconds +
355           (t1->seconds ? 0.00000000005 : 0), 9);
356       break;
357     case GST_RTSP_TIME_NOW:
358       g_string_append (string, "now");
359       break;
360     case GST_RTSP_TIME_END:
361       break;
362     case GST_RTSP_TIME_FRAMES:
363     {
364       gint64 sec = t1->seconds;
365 
366       /* need to format floating point value strings as in C locale */
367       g_string_append_printf (string, "%d:%02d:%02d", (gint) sec / (60 * 60),
368           (gint) (sec % (60 * 60)) / 60, (gint) sec % 60);
369 
370       if (t2->frames > 0.0) {
371         g_string_append_printf (string, ":%s", t2->frames < 10 ? "0" : "");
372         string_append_dtostr (string, t2->frames + 0.005, 2);
373       }
374       break;
375     }
376     case GST_RTSP_TIME_UTC:
377     {
378       gint64 sec = t1->seconds;
379       gint hours, minutes;
380       gdouble seconds;
381 
382       hours = sec / (60 * 60);
383       sec -= hours * 60 * 60;
384       minutes = sec / 60;
385       sec = ((hours * 60) + minutes) * 60;
386       seconds = t1->seconds - sec;
387       if (seconds)
388         seconds += 0.00000000005;
389 
390       g_string_append_printf (string, "%04d%02d%02dT%02d%02d%s",
391           t2->year, t2->month, t2->day, hours, minutes,
392           seconds < 10 ? "0" : "");
393       string_append_dtostr (string, seconds, 9);
394       g_string_append (string, "Z");
395       break;
396     }
397     default:
398       res = FALSE;
399       break;
400   }
401   return res;
402 }
403 
404 static gboolean
range_to_string(const GstRTSPTimeRange * range,GString * string)405 range_to_string (const GstRTSPTimeRange * range, GString * string)
406 {
407   gboolean res;
408 
409   if (!(res = time_to_string (&range->min, &range->min2, string)))
410     goto done;
411 
412   g_string_append (string, "-");
413 
414   if (!(res = time_to_string (&range->max, &range->max2, string)))
415     goto done;
416 
417 done:
418   return res;
419 }
420 
421 /**
422  * gst_rtsp_range_to_string:
423  * @range: a #GstRTSPTimeRange
424  *
425  * Convert @range into a string representation.
426  *
427  * Returns: The string representation of @range. g_free() after usage.
428  */
429 gchar *
gst_rtsp_range_to_string(const GstRTSPTimeRange * range)430 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
431 {
432   GString *string;
433 
434   g_return_val_if_fail (range != NULL, NULL);
435 
436   switch (range->unit) {
437     case GST_RTSP_RANGE_NPT:
438       string = g_string_new ("npt=");
439       break;
440     case GST_RTSP_RANGE_SMPTE:
441     case GST_RTSP_RANGE_SMPTE_30_DROP:
442       string = g_string_new ("smpte=");
443       break;
444     case GST_RTSP_RANGE_SMPTE_25:
445       string = g_string_new ("smpte-25=");
446       break;
447     case GST_RTSP_RANGE_CLOCK:
448       string = g_string_new ("clock=");
449       break;
450     default:
451       goto not_implemented;
452   }
453 
454   if (!range_to_string (range, string))
455     goto format_failed;
456 
457   return g_string_free (string, FALSE);
458 
459   /* ERRORS */
460 not_implemented:
461   {
462     g_warning ("time range unit not yet implemented");
463     return NULL;
464   }
465 format_failed:
466   {
467     g_string_free (string, TRUE);
468     return NULL;
469   }
470 }
471 
472 /**
473  * gst_rtsp_range_free:
474  * @range: a #GstRTSPTimeRange
475  *
476  * Free the memory allocated by @range.
477  */
478 void
gst_rtsp_range_free(GstRTSPTimeRange * range)479 gst_rtsp_range_free (GstRTSPTimeRange * range)
480 {
481   g_return_if_fail (range != NULL);
482 
483   g_free (range);
484 }
485 
486 static GstClockTime
get_seconds(const GstRTSPTime * t)487 get_seconds (const GstRTSPTime * t)
488 {
489   if (t->seconds < G_MAXINT) {
490     gint num, denom;
491     /* Don't do direct multiply with GST_SECOND to avoid rounding
492      * errors.
493      * This only works for "small" numbers, because num is limited to 32-bit
494      */
495     gst_util_double_to_fraction (t->seconds, &num, &denom);
496     return gst_util_uint64_scale_int (GST_SECOND, num, denom);
497   } else {
498     return gst_util_gdouble_to_guint64 (t->seconds * GST_SECOND);
499   }
500 }
501 
502 static GstClockTime
get_frames(const GstRTSPTime2 * t,GstRTSPRangeUnit unit)503 get_frames (const GstRTSPTime2 * t, GstRTSPRangeUnit unit)
504 {
505   gint num, denom;
506 
507   gst_util_double_to_fraction (t->frames, &num, &denom);
508 
509   switch (unit) {
510     case GST_RTSP_RANGE_SMPTE_25:
511       denom *= 25;
512       break;
513     case GST_RTSP_RANGE_SMPTE:
514     case GST_RTSP_RANGE_SMPTE_30_DROP:
515     default:
516       num *= 1001;
517       denom *= 30003;
518       break;
519   }
520   return gst_util_uint64_scale_int (GST_SECOND, num, denom);
521 }
522 
523 static GstClockTime
get_time(GstRTSPRangeUnit unit,const GstRTSPTime * t1,const GstRTSPTime2 * t2)524 get_time (GstRTSPRangeUnit unit, const GstRTSPTime * t1,
525     const GstRTSPTime2 * t2)
526 {
527   GstClockTime res;
528 
529   switch (t1->type) {
530     case GST_RTSP_TIME_SECONDS:
531     {
532       res = get_seconds (t1);
533       break;
534     }
535     case GST_RTSP_TIME_UTC:
536     {
537       GDateTime *dt, *bt;
538       GTimeSpan span;
539 
540       /* make time base, we use 1900 */
541       bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
542       /* convert to GDateTime without the seconds */
543       dt = g_date_time_new_utc (t2->year, t2->month, t2->day, 0, 0, 0.0);
544       /* get amount of microseconds */
545       span = g_date_time_difference (dt, bt);
546       g_date_time_unref (bt);
547       g_date_time_unref (dt);
548       /* add seconds */
549       res = get_seconds (t1) + (span * 1000);
550       break;
551     }
552     case GST_RTSP_TIME_FRAMES:
553       res = get_seconds (t1);
554       res += get_frames (t2, unit);
555       break;
556     default:
557     case GST_RTSP_TIME_NOW:
558     case GST_RTSP_TIME_END:
559       res = GST_CLOCK_TIME_NONE;
560       break;
561   }
562   return res;
563 }
564 
565 /**
566  * gst_rtsp_range_get_times:
567  * @range: a #GstRTSPTimeRange
568  * @min: (out): result minimum #GstClockTime
569  * @max: (out): result maximum #GstClockTime
570  *
571  * Retrieve the minimum and maximum values from @range converted to
572  * #GstClockTime in @min and @max.
573  *
574  * A value of %GST_CLOCK_TIME_NONE will be used to signal #GST_RTSP_TIME_NOW
575  * and #GST_RTSP_TIME_END for @min and @max respectively.
576  *
577  * UTC times will be converted to nanoseconds since 1900.
578  *
579  * Returns: %TRUE on success.
580  *
581  * Since: 1.2
582  */
583 gboolean
gst_rtsp_range_get_times(const GstRTSPTimeRange * range,GstClockTime * min,GstClockTime * max)584 gst_rtsp_range_get_times (const GstRTSPTimeRange * range,
585     GstClockTime * min, GstClockTime * max)
586 {
587   g_return_val_if_fail (range != NULL, FALSE);
588 
589   if (min)
590     *min = get_time (range->unit, &range->min, &range->min2);
591   if (max)
592     *max = get_time (range->unit, &range->max, &range->max2);
593 
594   return TRUE;
595 }
596 
597 static void
set_time(GstRTSPTime * time,GstRTSPTime2 * time2,GstRTSPRangeUnit unit,GstClockTime clock_time)598 set_time (GstRTSPTime * time, GstRTSPTime2 * time2, GstRTSPRangeUnit unit,
599     GstClockTime clock_time)
600 {
601   memset (time, 0, sizeof (GstRTSPTime));
602   memset (time2, 0, sizeof (GstRTSPTime2));
603 
604   if (clock_time == GST_CLOCK_TIME_NONE) {
605     time->type = GST_RTSP_TIME_END;
606     return;
607   }
608 
609   switch (unit) {
610     case GST_RTSP_RANGE_SMPTE:
611     case GST_RTSP_RANGE_SMPTE_30_DROP:
612     {
613       time->seconds = (guint64) (clock_time / GST_SECOND);
614       time2->frames = 30003 * (clock_time % GST_SECOND) /
615           (gdouble) (1001 * GST_SECOND);
616       time->type = GST_RTSP_TIME_FRAMES;
617       g_assert (time2->frames < 30);
618       break;
619     }
620     case GST_RTSP_RANGE_SMPTE_25:
621     {
622       time->seconds = (guint64) (clock_time / GST_SECOND);
623       time2->frames = (25 * (clock_time % GST_SECOND)) / (gdouble) GST_SECOND;
624       time->type = GST_RTSP_TIME_FRAMES;
625       g_assert (time2->frames < 25);
626       break;
627     }
628     case GST_RTSP_RANGE_NPT:
629     {
630       time->seconds = (gdouble) clock_time / (gdouble) GST_SECOND;
631       time->type = GST_RTSP_TIME_SECONDS;
632       break;
633     }
634     case GST_RTSP_RANGE_CLOCK:
635     {
636       GDateTime *bt, *datetime;
637       GstClockTime subsecond = clock_time % GST_SECOND;
638 
639       bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
640       datetime = g_date_time_add_seconds (bt, clock_time / GST_SECOND);
641 
642       time2->year = g_date_time_get_year (datetime);
643       time2->month = g_date_time_get_month (datetime);
644       time2->day = g_date_time_get_day_of_month (datetime);
645 
646       time->seconds = g_date_time_get_hour (datetime) * 60 * 60;
647       time->seconds += g_date_time_get_minute (datetime) * 60;
648       time->seconds += g_date_time_get_seconds (datetime);
649       time->seconds += (gdouble) subsecond / (gdouble) GST_SECOND;
650       time->type = GST_RTSP_TIME_UTC;
651 
652       g_date_time_unref (bt);
653       g_date_time_unref (datetime);
654       break;
655     }
656   }
657 
658   if (time->seconds < 0.000000001)
659     time->seconds = 0;
660   if (time2->frames < 0.000000001)
661     time2->frames = 0;
662 }
663 
664 /**
665  * gst_rtsp_range_convert_units:
666  * @range: a #GstRTSPTimeRange
667  * @unit: the unit to convert the range into
668  *
669  * Converts the range in-place between different types of units.
670  * Ranges containing the special value #GST_RTSP_TIME_NOW can not be
671  * converted as these are only valid for #GST_RTSP_RANGE_NPT.
672  *
673  * Returns: %TRUE if the range could be converted
674  */
675 
676 gboolean
gst_rtsp_range_convert_units(GstRTSPTimeRange * range,GstRTSPRangeUnit unit)677 gst_rtsp_range_convert_units (GstRTSPTimeRange * range, GstRTSPRangeUnit unit)
678 {
679   if (range->unit == unit)
680     return TRUE;
681 
682   if (range->min.type == GST_RTSP_TIME_NOW ||
683       range->max.type == GST_RTSP_TIME_NOW)
684     return FALSE;
685 
686   set_time (&range->min, &range->min2, unit,
687       get_time (range->unit, &range->min, &range->min2));
688   set_time (&range->max, &range->max2, unit,
689       get_time (range->unit, &range->max, &range->max2));
690 
691   range->unit = unit;
692 
693   return TRUE;
694 }
695