b46dc554646a7bc603f17fe622de1fb911f4412d
[platform/upstream/gstreamer.git] / gst-libs / gst / rtsp / gstrtsprange.c
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  * @short_description: dealing with time ranges
46  *  
47  * Provides helper functions to deal with time ranges.
48  *  
49  * Last reviewed on 2007-07-25 (0.10.14)
50  */
51
52
53 #include <stdio.h>
54 #include <string.h>
55
56 #include "gstrtsprange.h"
57
58 static gdouble
59 gst_strtod (const gchar * dstr)
60 {
61   gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
62
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);
70 }
71
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
78  */
79 static GstRTSPResult
80 parse_npt_time (const gchar * str, GstRTSPTime * time)
81 {
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, ":")) {
87     gint hours, mins;
88
89     sscanf (str, "%2d:%2d:", &hours, &mins);
90     str = strchr (str, ':') + 1;
91     str = strchr (str, ':') + 1;
92     time->type = GST_RTSP_TIME_SECONDS;
93     time->seconds = ((hours * 60) + mins) * 60 + gst_strtod (str);
94   } else {
95     time->type = GST_RTSP_TIME_SECONDS;
96     time->seconds = gst_strtod (str);
97   }
98   return GST_RTSP_OK;
99 }
100
101 /* npt-range    =   ( npt-time "-" [ npt-time ] ) | ( "-" npt-time )
102  */
103 static GstRTSPResult
104 parse_npt_range (const gchar * str, GstRTSPTimeRange * range)
105 {
106   GstRTSPResult res;
107   gchar *p;
108
109   range->unit = GST_RTSP_RANGE_NPT;
110
111   /* find '-' separator */
112   p = strstr (str, "-");
113   if (p == NULL)
114     return GST_RTSP_EINVAL;
115
116   if ((res = parse_npt_time (str, &range->min)) != GST_RTSP_OK)
117     goto done;
118
119   res = parse_npt_time (p + 1, &range->max);
120
121 done:
122   return res;
123 }
124
125 static GstRTSPResult
126 parse_clock_range (const gchar * str, GstRTSPTimeRange * range)
127 {
128   return GST_RTSP_ENOTIMPL;
129 }
130
131 static GstRTSPResult
132 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
133 {
134   return GST_RTSP_ENOTIMPL;
135 }
136
137 /**
138  * gst_rtsp_range_parse:
139  * @rangestr: a range string to parse
140  * @range: location to hold the #GstRTSPTimeRange result
141  *
142  * Parse @rangestr to a #GstRTSPTimeRange.
143  *
144  * Returns: #GST_RTSP_OK on success.
145  */
146 GstRTSPResult
147 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
148 {
149   GstRTSPResult ret;
150   GstRTSPTimeRange *res;
151   gchar *p;
152
153   g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
154   g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
155
156   res = g_new0 (GstRTSPTimeRange, 1);
157
158   p = (gchar *) rangestr;
159   /* first figure out the units of the range */
160   if (g_str_has_prefix (p, "npt=")) {
161     ret = parse_npt_range (p + 4, res);
162   } else if (g_str_has_prefix (p, "clock=")) {
163     ret = parse_clock_range (p + 6, res);
164   } else if (g_str_has_prefix (p, "smpte=")) {
165     res->unit = GST_RTSP_RANGE_SMPTE;
166     ret = parse_smpte_range (p + 6, res);
167   } else if (g_str_has_prefix (p, "smpte-30-drop=")) {
168     res->unit = GST_RTSP_RANGE_SMPTE_30_DROP;
169     ret = parse_smpte_range (p + 14, res);
170   } else if (g_str_has_prefix (p, "smpte-25=")) {
171     res->unit = GST_RTSP_RANGE_SMPTE_25;
172     ret = parse_smpte_range (p + 9, res);
173   } else
174     goto invalid;
175
176   if (ret != GST_RTSP_OK)
177     goto invalid;
178
179   *range = res;
180   return ret;
181
182   /* ERRORS */
183 invalid:
184   {
185     gst_rtsp_range_free (res);
186     return GST_RTSP_EINVAL;
187   }
188 }
189
190 static gboolean
191 npt_time_string (const GstRTSPTime * time, GString * string)
192 {
193   gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
194   gboolean res = TRUE;;
195
196   switch (time->type) {
197     case GST_RTSP_TIME_SECONDS:
198       /* need to format floating point value strings as in C locale */
199       g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, time->seconds);
200       g_string_append (string, dstrbuf);
201       break;
202     case GST_RTSP_TIME_NOW:
203       g_string_append (string, "now");
204       break;
205     case GST_RTSP_TIME_END:
206       break;
207     default:
208       res = FALSE;
209       break;
210   }
211   return res;
212 }
213
214 static gboolean
215 npt_range_string (const GstRTSPTimeRange * range, GString * string)
216 {
217   gboolean res;
218
219   if (!(res = npt_time_string (&range->min, string)))
220     goto done;
221
222   g_string_append (string, "-");
223
224   if (!(res = npt_time_string (&range->max, string)))
225     goto done;
226
227 done:
228   return res;
229 }
230
231 /**
232  * gst_rtsp_range_to_string:
233  * @range: a #GstRTSPTimeRange
234  *
235  * Convert @range into a string representation.
236  *
237  * Returns: The string representation of @range. g_free() after usage.
238  */
239 gchar *
240 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
241 {
242   gchar *result = NULL;
243   GString *string;
244
245   g_return_val_if_fail (range != NULL, NULL);
246
247   string = g_string_new ("");
248
249   switch (range->unit) {
250     case GST_RTSP_RANGE_NPT:
251       g_string_append (string, "npt=");
252       if (!npt_range_string (range, string)) {
253         g_string_free (string, TRUE);
254         string = NULL;
255       }
256       break;
257     case GST_RTSP_RANGE_SMPTE:
258     case GST_RTSP_RANGE_SMPTE_30_DROP:
259     case GST_RTSP_RANGE_SMPTE_25:
260     case GST_RTSP_RANGE_CLOCK:
261     default:
262       g_warning ("time range unit not yet implemented");
263       g_string_free (string, TRUE);
264       string = NULL;
265       break;
266   }
267   if (string)
268     result = g_string_free (string, FALSE);
269
270   return result;
271 }
272
273 /**
274  * gst_rtsp_range_free:
275  * @range: a #GstRTSPTimeRange
276  *
277  * Free the memory allocated by @range.
278  */
279 void
280 gst_rtsp_range_free (GstRTSPTimeRange * range)
281 {
282   g_free (range);
283 }