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