Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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  * 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 /* 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
64  */
65 static GstRTSPResult
66 parse_npt_time (const gchar * str, GstRTSPTime * time)
67 {
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, ":")) {
73     gfloat seconds;
74     gint hours, mins;
75
76     sscanf (str, "%2d:%2d:%f", &hours, &mins, &seconds);
77
78     time->type = GST_RTSP_TIME_SECONDS;
79     time->seconds = ((hours * 60) + mins) * 60 + seconds;
80   } else {
81     gfloat seconds;
82
83     sscanf (str, "%f", &seconds);
84
85     time->type = GST_RTSP_TIME_SECONDS;
86     time->seconds = seconds;
87   }
88   return GST_RTSP_OK;
89 }
90
91 /* npt-range    =   ( npt-time "-" [ npt-time ] ) | ( "-" npt-time )
92  */
93 static GstRTSPResult
94 parse_npt_range (const gchar * str, GstRTSPTimeRange * range)
95 {
96   GstRTSPResult res;
97   gchar *p;
98
99   range->unit = GST_RTSP_RANGE_NPT;
100
101   /* find '-' separator */
102   p = strstr (str, "-");
103   if (p == NULL)
104     return GST_RTSP_EINVAL;
105
106   if ((res = parse_npt_time (str, &range->min)) != GST_RTSP_OK)
107     goto done;
108
109   res = parse_npt_time (p + 1, &range->max);
110
111 done:
112   return res;
113 }
114
115 static GstRTSPResult
116 parse_clock_range (const gchar * str, GstRTSPTimeRange * range)
117 {
118   return GST_RTSP_ENOTIMPL;
119 }
120
121 static GstRTSPResult
122 parse_smpte_range (const gchar * str, GstRTSPTimeRange * range)
123 {
124   return GST_RTSP_ENOTIMPL;
125 }
126
127 /**
128  * gst_rtsp_range_parse:
129  * @rangestr: a range string to parse
130  * @range: location to hold the #GstRTSPTimeRange result
131  *
132  * Parse @rangestr to a #GstRTSPTimeRange.
133  *
134  * Returns: #GST_RTSP_OK on success.
135  */
136 GstRTSPResult
137 gst_rtsp_range_parse (const gchar * rangestr, GstRTSPTimeRange ** range)
138 {
139   GstRTSPResult ret;
140   GstRTSPTimeRange *res;
141   gchar *p;
142
143   g_return_val_if_fail (rangestr != NULL, GST_RTSP_EINVAL);
144   g_return_val_if_fail (range != NULL, GST_RTSP_EINVAL);
145
146   res = g_new0 (GstRTSPTimeRange, 1);
147
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);
163   } else
164     goto invalid;
165
166   if (ret != GST_RTSP_OK)
167     goto invalid;
168
169   *range = res;
170   return ret;
171
172   /* ERRORS */
173 invalid:
174   {
175     gst_rtsp_range_free (res);
176     return GST_RTSP_EINVAL;
177   }
178 }
179
180 static gboolean
181 npt_time_string (const GstRTSPTime * time, GString * string)
182 {
183   gboolean res = TRUE;;
184
185   switch (time->type) {
186     case GST_RTSP_TIME_SECONDS:
187       g_string_append_printf (string, "%f", time->seconds);
188       break;
189     case GST_RTSP_TIME_NOW:
190       g_string_append (string, "now");
191       break;
192     case GST_RTSP_TIME_END:
193       break;
194     default:
195       res = FALSE;
196       break;
197   }
198   return res;
199 }
200
201 static gboolean
202 npt_range_string (const GstRTSPTimeRange * range, GString * string)
203 {
204   gboolean res;
205
206   if (!(res = npt_time_string (&range->min, string)))
207     goto done;
208
209   g_string_append (string, "-");
210
211   if (!(res = npt_time_string (&range->max, string)))
212     goto done;
213
214 done:
215   return res;
216 }
217
218 /**
219  * gst_rtsp_range_to_string:
220  * @range: a #GstRTSPTimeRange
221  *
222  * Convert @range into a string representation.
223  *
224  * Returns: The string representation of @range. g_free() after usage.
225  *
226  * Since: 0.10.23
227  */
228 gchar *
229 gst_rtsp_range_to_string (const GstRTSPTimeRange * range)
230 {
231   gchar *result = NULL;
232   GString *string;
233
234   g_return_val_if_fail (range != NULL, NULL);
235
236   string = g_string_new ("");
237
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);
243         string = NULL;
244       }
245       break;
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:
250     default:
251       g_warning ("time range unit not yet implemented");
252       g_string_free (string, TRUE);
253       string = NULL;
254       break;
255   }
256   if (string)
257     result = g_string_free (string, FALSE);
258
259   return result;
260 }
261
262 /**
263  * gst_rtsp_range_free:
264  * @range: a #GstRTSPTimeRange
265  *
266  * Free the memory alocated by @range.
267  */
268 void
269 gst_rtsp_range_free (GstRTSPTimeRange * range)
270 {
271   g_free (range);
272 }