diff options
author | Olivier CrĂȘte <olivier.crete@collabora.com> | 2013-02-22 13:26:03 -0500 |
---|---|---|
committer | Wim Taymans <wim.taymans@collabora.co.uk> | 2013-03-11 10:41:25 +0100 |
commit | 0353e608f8768a4982895c661d8d9367435cb07d (patch) | |
tree | 6df70b5edbf9b0922bad568233d5134cc5b56fda | |
parent | 3cfec4de73be0825410498f9538552cd5a2ffc0a (diff) |
rtsprange: Make _to_string() be more in line with RFC 2326
Fix various nits to make it more in line with the RFC, also add unit tests.
-rw-r--r-- | gst-libs/gst/rtsp/gstrtsprange.c | 71 | ||||
-rw-r--r-- | tests/check/libs/rtsp.c | 36 |
2 files changed, 94 insertions, 13 deletions
diff --git a/gst-libs/gst/rtsp/gstrtsprange.c b/gst-libs/gst/rtsp/gstrtsprange.c index 94f3c016f..6bda1419d 100644 --- a/gst-libs/gst/rtsp/gstrtsprange.c +++ b/gst-libs/gst/rtsp/gstrtsprange.c @@ -49,7 +49,7 @@ * Last reviewed on 2007-07-25 (0.10.14) */ - +#include <math.h> #include <stdio.h> #include <string.h> @@ -305,18 +305,52 @@ invalid: } } +static void +string_append_dtostr (GString * string, gdouble value, guint precision) +{ + gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, }; + gchar *dot; + guint len; + + precision++; + + if (value != 0.0) + value += 4.9 * pow (10.0, precision * -1.0); + + g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, value); + + dot = strchr (dstrbuf, '.'); + + if (dot == NULL) + goto done; + + for (; *dot != '.' && *dot != '0'; dot++); + + if ((dot - dstrbuf) + precision < G_ASCII_DTOSTR_BUF_SIZE) + dot[precision] = 0; + + len = strlen (dstrbuf); + while (dstrbuf[len - 1] == '0') + dstrbuf[--len] = 0; + if (dstrbuf[len - 1] == '.') + dstrbuf[--len] = 0; + +done: + + g_string_append (string, dstrbuf); +} + static gboolean time_to_string (const GstRTSPTime * t1, const GstRTSPTime2 * t2, GString * string) { - gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, }; gboolean res = TRUE;; switch (t1->type) { case GST_RTSP_TIME_SECONDS: /* need to format floating point value strings as in C locale */ - g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t1->seconds); - g_string_append (string, dstrbuf); + string_append_dtostr (string, t1->seconds + + (t1->seconds ? 0.00000000005 : 0), 9); break; case GST_RTSP_TIME_NOW: g_string_append (string, "now"); @@ -328,23 +362,34 @@ time_to_string (const GstRTSPTime * t1, const GstRTSPTime2 * t2, gint64 sec = t1->seconds; /* need to format floating point value strings as in C locale */ - g_string_append_printf (string, "%d:%02d:%02d:", (gint) sec / 60 * 60, - (gint) sec / 60, (gint) sec % 60); + g_string_append_printf (string, "%d:%02d:%02d", (gint) sec / (60 * 60), + (gint) (sec % (60 * 60)) / 60, (gint) sec % 60); if (t2->frames > 0.0) { - g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t2->frames); - g_string_append (string, dstrbuf); + g_string_append_printf (string, ":%s", t2->frames < 10 ? "0" : ""); + string_append_dtostr (string, t2->frames + 0.005, 2); } break; } case GST_RTSP_TIME_UTC: { gint64 sec = t1->seconds; - - g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, t1->seconds - sec); - g_string_append_printf (string, "%04d%02d%02dT%02d%02d%02d%sZ", - t2->year, t2->month, t2->day, (gint) sec / 60 * 60, - (gint) sec / 60, (gint) sec % 60, dstrbuf); + gint hours, minutes; + gdouble seconds; + + hours = sec / (60 * 60); + sec -= hours * 60 * 60; + minutes = sec / 60; + sec = ((hours * 60) + minutes) * 60; + seconds = t1->seconds - sec; + if (seconds) + seconds += 0.00000000005; + + g_string_append_printf (string, "%04d%02d%02dT%02d%02d%s", + t2->year, t2->month, t2->day, hours, minutes, + seconds < 10 ? "0" : ""); + string_append_dtostr (string, seconds, 9); + g_string_append (string, "Z"); break; } default: diff --git a/tests/check/libs/rtsp.c b/tests/check/libs/rtsp.c index dc60ab71e..50a55a86f 100644 --- a/tests/check/libs/rtsp.c +++ b/tests/check/libs/rtsp.c @@ -140,6 +140,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == GST_CLOCK_TIME_NONE); fail_unless (max == GST_CLOCK_TIME_NONE); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=-now", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -149,6 +150,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (range->min.type == GST_RTSP_TIME_NOW); fail_unless (range->max.type == GST_RTSP_TIME_NOW); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=now-now", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -158,6 +160,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (range->min.type == GST_RTSP_TIME_NOW); fail_unless (range->max.type == GST_RTSP_TIME_END); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=now-", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -171,6 +174,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == GST_CLOCK_TIME_NONE); fail_unless (max == 34120000000); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=now-34.12", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -184,6 +188,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == 23890000000); fail_unless (max == GST_CLOCK_TIME_NONE); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=23.89-now", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -197,6 +202,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == GST_CLOCK_TIME_NONE); fail_unless (max == 12090000000); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=-12.09", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -210,6 +216,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == 0); fail_unless (max == GST_CLOCK_TIME_NONE); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=0-", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -224,6 +231,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == 1123000000); fail_unless (max == GST_CLOCK_TIME_NONE); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=1.123-", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -238,6 +246,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == 10200000000); fail_unless (max == 20100000000); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=10.2-20.1", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -252,6 +261,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == 500000000000); fail_unless (max == 15001000000); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=500-15.001", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -273,6 +283,7 @@ GST_START_TEST (test_rtsp_range_npt) fail_unless (min == 72754230000000); fail_unless (max == 78300010000000); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("npt=72754.23-78300.01", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -305,6 +316,7 @@ GST_START_TEST (test_rtsp_range_smpte) fail_unless (min == 0); fail_unless (max == GST_CLOCK_TIME_NONE); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("smpte=0:00:00-", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -323,6 +335,7 @@ GST_START_TEST (test_rtsp_range_smpte) /* 20.89 * GST_SECOND * 1001 / 30003 */ fail_unless (max == 72729000000000 + 696959970); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("smpte=10:34:23-20:12:09:20.89", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -343,6 +356,27 @@ GST_START_TEST (test_rtsp_range_smpte) fail_unless (max == 72729000000000 + 835600000); str = gst_rtsp_range_to_string (range); GST_DEBUG ("%s", str); + fail_unless_equals_string ("smpte-25=10:34:23-20:12:09:20.89", str); + g_free (str); + gst_rtsp_range_free (range); + + fail_unless (gst_rtsp_range_parse ("smpte-25=0:00:00:00.01-9:59:59:24.99", + &range) == GST_RTSP_OK); + fail_unless (range->unit == GST_RTSP_RANGE_SMPTE_25); + fail_unless (range->min.type == GST_RTSP_TIME_FRAMES); + fail_unless (range->min.seconds == 0); + fail_unless (range->min2.frames == 0.01); + fail_unless (range->max.type == GST_RTSP_TIME_FRAMES); + fail_unless (range->max.seconds == 35999); + fail_unless (range->max2.frames == 24.99); + fail_unless (gst_rtsp_range_get_times (range, &min, &max)); + fail_unless (min == 400000); + GST_DEBUG ("%" GST_TIME_FORMAT, GST_TIME_ARGS (max)); + /* 35999 + (24.99/25) */ + fail_unless (max == 35999999600000); + str = gst_rtsp_range_to_string (range); + GST_DEBUG ("%s", str); + fail_unless_equals_string ("smpte-25=0:00:00:00.01-9:59:59:24.99", str); g_free (str); gst_rtsp_range_free (range); } @@ -371,6 +405,7 @@ GST_START_TEST (test_rtsp_range_clock) fail_unless (range->min.seconds == 44625.0); fail_unless (range->max.type == GST_RTSP_TIME_END); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("clock=20001010T122345Z-", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); @@ -389,6 +424,7 @@ GST_START_TEST (test_rtsp_range_clock) fail_unless (range->max2.day == 30); fail_unless (range->max.seconds == 72729.89); str = gst_rtsp_range_to_string (range); + fail_unless_equals_string ("clock=19700101T103423Z-30001230T201209.89Z", str); GST_DEBUG ("%s", str); g_free (str); gst_rtsp_range_free (range); |