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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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"
58 /* npt-time = "now" | npt-sec | npt-hhmmss
59 * npt-sec = 1*DIGIT [ "." *DIGIT ]
60 * npt-hhmmss = npt-hh ":" npt-mm ":" npt-ss [ "." *DIGIT ]
61 * npt-hh = 1*DIGIT ; any positive number
62 * npt-mm = 1*2DIGIT ; 0-59
63 * npt-ss = 1*2DIGIT ; 0-59
66 parse_npt_time (const gchar * str, GstRTSPTime * time)
68 if (strncmp (str, "now", 3) == 0) {
69 time->type = GST_RTSP_TIME_NOW;
70 } else if (str[0] == '\0') {
71 time->type = GST_RTSP_TIME_END;
72 } else if (strstr (str, ":")) {
76 sscanf (str, "%2d:%2d:%f", &hours, &mins, &seconds);
78 time->type = GST_RTSP_TIME_SECONDS;
79 time->seconds = ((hours * 60) + mins) * 60 + seconds;
83 sscanf (str, "%f", &seconds);
85 time->type = GST_RTSP_TIME_SECONDS;
86 time->seconds = seconds;
91 /* npt-range = ( npt-time "-" [ npt-time ] ) | ( "-" npt-time )
94 parse_npt_range (const gchar * str, GstRTSPTimeRange * range)
99 range->unit = GST_RTSP_RANGE_NPT;
101 /* find '-' separator */
102 p = strstr (str, "-");
104 return GST_RTSP_EINVAL;
106 if ((res = parse_npt_time (str, &range->min)) != GST_RTSP_OK)
109 res = parse_npt_time (p + 1, &range->max);
116 parse_clock_range (const gchar * str, GstRTSPTimeRange * range)
118 return GST_RTSP_ENOTIMPL;
122 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
124 return GST_RTSP_ENOTIMPL;
128 * gst_rtsp_range_parse:
129 * @rangestr: a range string to parse
130 * @range: location to hold the #GstRTSPTimeRange result
132 * Parse @rangestr to a #GstRTSPTimeRange.
134 * Returns: #GST_RTSP_OK on success.
137 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
140 GstRTSPTimeRange *res;
143 g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
144 g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
146 res = g_new0 (GstRTSPTimeRange, 1);
148 p = (gchar *) rangestr;
149 /* first figure out the units of the range */
150 if (g_str_has_prefix (p, "npt=")) {
151 ret = parse_npt_range (p + 4, res);
152 } else if (g_str_has_prefix (p, "clock=")) {
153 ret = parse_clock_range (p + 6, res);
154 } else if (g_str_has_prefix (p, "smpte=")) {
155 res->unit = GST_RTSP_RANGE_SMPTE;
156 ret = parse_smpte_range (p + 6, res);
157 } else if (g_str_has_prefix (p, "smpte-30-drop=")) {
158 res->unit = GST_RTSP_RANGE_SMPTE_30_DROP;
159 ret = parse_smpte_range (p + 14, res);
160 } else if (g_str_has_prefix (p, "smpte-25=")) {
161 res->unit = GST_RTSP_RANGE_SMPTE_25;
162 ret = parse_smpte_range (p + 9, res);
166 if (ret != GST_RTSP_OK)
175 gst_rtsp_range_free (res);
176 return GST_RTSP_EINVAL;
181 npt_time_string (const GstRTSPTime * time, GString * string)
183 gboolean res = TRUE;;
185 switch (time->type) {
186 case GST_RTSP_TIME_SECONDS:
187 g_string_append_printf (string, "%f", time->seconds);
189 case GST_RTSP_TIME_NOW:
190 g_string_append (string, "now");
192 case GST_RTSP_TIME_END:
202 npt_range_string (const GstRTSPTimeRange * range, GString * string)
206 if (!(res = npt_time_string (&range->min, string)))
209 g_string_append (string, "-");
211 if (!(res = npt_time_string (&range->max, string)))
219 * gst_rtsp_range_to_string:
220 * @range: a #GstRTSPTimeRange
222 * Convert @range into a string representation.
224 * Returns: The string representation of @range. g_free() after usage.
229 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
231 gchar *result = NULL;
234 g_return_val_if_fail (range != NULL, NULL);
236 string = g_string_new ("");
238 switch (range->unit) {
239 case GST_RTSP_RANGE_NPT:
240 g_string_append (string, "npt=");
241 if (!npt_range_string (range, string)) {
242 g_string_free (string, TRUE);
246 case GST_RTSP_RANGE_SMPTE:
247 case GST_RTSP_RANGE_SMPTE_30_DROP:
248 case GST_RTSP_RANGE_SMPTE_25:
249 case GST_RTSP_RANGE_CLOCK:
251 g_warning ("time range unit not yet implemented");
252 g_string_free (string, TRUE);
257 result = g_string_free (string, FALSE);
263 * gst_rtsp_range_free:
264 * @range: a #GstRTSPTimeRange
266 * Free the memory allocated by @range.
269 gst_rtsp_range_free (GstRTSPTimeRange * range)