]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_util.c
String fixes in src (Refs:#438)
[vlc] / src / control / mediacontrol_util.c
1 /*****************************************************************************
2  * util.c: Utility functions and exceptions management
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <mediacontrol_internal.h>
25 #include <vlc/mediacontrol.h>
26
27 #include <vlc/intf.h>
28 #include <vlc/vout.h>
29 #include <vlc/aout.h>
30 #include <vlc_demux.h>
31
32 #include <vlc_osd.h>
33
34 #define HAS_SNAPSHOT 1
35
36 #ifdef HAS_SNAPSHOT
37 #include <snapshot.h>
38 #endif
39
40 #include <stdlib.h>                                      /* malloc(), free() */
41 #include <string.h>
42
43 #include <errno.h>                                                 /* ENOMEM */
44 #include <stdio.h>
45 #include <ctype.h>
46
47 #ifdef HAVE_UNISTD_H
48 #    include <unistd.h>
49 #endif
50 #ifdef HAVE_SYS_TIME_H
51 #    include <sys/time.h>
52 #endif
53 #ifdef HAVE_SYS_TYPES_H
54 #    include <sys/types.h>
55 #endif
56
57 #define RAISE( c, m )  exception->code = c; \
58                        exception->message = strdup(m);
59
60 vlc_int64_t mediacontrol_unit_convert( input_thread_t *p_input,
61                                        mediacontrol_PositionKey from,
62                                        mediacontrol_PositionKey to,
63                                        vlc_int64_t value )
64 {
65     if( to == from )
66         return value;
67
68     /* For all conversions, we need data from p_input */
69     if( !p_input )
70         return 0;
71
72     switch( from )
73     {
74     case mediacontrol_MediaTime:
75         if( to == mediacontrol_ByteCount )
76         {
77             /* FIXME */
78             /* vlc < 0.8 API */
79             /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
80             return 0;
81         }
82         if( to == mediacontrol_SampleCount )
83         {
84             double f_fps;
85
86             if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
87                 return 0;
88             else
89                 return( value * f_fps / 1000.0 );
90         }
91         /* Cannot happen */
92         /* See http://catb.org/~esr/jargon/html/entry/can't-happen.html */
93         break;
94
95     case mediacontrol_SampleCount:
96     {
97         double f_fps;
98
99         if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) ||
100                                 f_fps < 0.1 )
101             return 0;
102
103         if( to == mediacontrol_ByteCount )
104         {
105             /* FIXME */
106             /* vlc < 0.8 API */
107 /*             return ( vlc_int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
108             return 0;
109         }
110
111         if( to == mediacontrol_MediaTime )
112             return( vlc_int64_t )( value * 1000.0 / ( double )f_fps );
113
114         /* Cannot happen */
115         break;
116     }
117     case mediacontrol_ByteCount:
118         /* FIXME */
119         return 0;
120 /* vlc < 0.8 API: */
121
122 //         if( p_input->stream.i_mux_rate == 0 )
123 //             return 0;
124 // 
125 //         /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
126 //            The 50 hardcoded constant comes from the definition of i_mux_rate :
127 //            i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
128 //            0 if undef */
129 //         if( to == mediacontrol_MediaTime )
130 //             return ( vlc_int64_t )( 1000 * value / 50 / p_input->stream.i_mux_rate );
131 // 
132 //         if( to == mediacontrol_SampleCount )
133 //         {
134 //             double f_fps;
135 //             if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
136 //                 return 0;
137 //             else
138 //                 return ( vlc_int64_t )( value * f_fps / 50 / p_input->stream.i_mux_rate );
139 //         }
140         /* Cannot happen */
141         break;
142     }
143     /* Cannot happen */
144     return 0;
145 }
146
147 /* Converts a mediacontrol_Position into a time in microseconds in
148    movie clock time */
149 vlc_int64_t
150 mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
151 {
152     switch( pos->origin )
153     {
154     case mediacontrol_AbsolutePosition:
155         return ( 1000 * mediacontrol_unit_convert( p_input,
156                                                    pos->key, /* from */
157                                                    mediacontrol_MediaTime,  /* to */
158                                                    pos->value ) );
159         break;
160     case mediacontrol_RelativePosition:
161     {
162         vlc_int64_t l_pos;
163         vlc_value_t val;
164
165         val.i_time = 0;
166         if( p_input )
167         {
168             var_Get( p_input, "time", &val );
169         }
170
171         l_pos = 1000 * mediacontrol_unit_convert( p_input,
172                                                   pos->key,
173                                                   mediacontrol_MediaTime,
174                                                   pos->value );
175         return val.i_time + l_pos;
176         break;
177     }
178     case mediacontrol_ModuloPosition:
179     {
180         vlc_int64_t l_pos;
181         vlc_value_t val;
182
183         val.i_time = 0;
184         if( p_input )
185         {
186             var_Get( p_input, "length", &val );
187         }
188
189         if( val.i_time > 0)
190         {
191             l_pos = ( 1000 * mediacontrol_unit_convert( p_input,
192                                                         pos->key,
193                                                         mediacontrol_MediaTime,
194                                                         pos->value ) );
195         }
196         else
197             l_pos = 0;
198
199         return l_pos % val.i_time;
200         break;
201     }
202     }
203     return 0;
204 }
205
206 mediacontrol_RGBPicture*
207 mediacontrol_RGBPicture__alloc( int datasize )
208 {
209     mediacontrol_RGBPicture* pic;
210
211     pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
212     if( ! pic )
213         return NULL;
214
215     pic->size = datasize;
216     pic->data = ( char* )malloc( datasize * sizeof( char ) );
217     return pic;
218 }
219
220 void
221 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
222 {
223     if( pic )
224         free( pic->data );
225     free( pic );
226 }
227
228 mediacontrol_PlaylistSeq*
229 mediacontrol_PlaylistSeq__alloc( int size )
230 {
231     mediacontrol_PlaylistSeq* ps;
232
233     ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
234     if( ! ps )
235         return NULL;
236
237     ps->size = size;
238     ps->data = ( char** )malloc( size * sizeof( char* ) );
239     return ps;
240 }
241
242 void
243 mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
244 {
245     if( ps )
246     {
247         int i;
248         for( i = 0 ; i < ps->size ; i++ )
249             free( ps->data[i] );
250     }
251     free( ps->data );
252     free( ps );
253 }
254
255 mediacontrol_Exception*
256 mediacontrol_exception_init( mediacontrol_Exception *exception )
257 {
258     if( exception == NULL )
259     {
260         exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
261     }
262
263     exception->code = 0;
264     exception->message = NULL;
265     return exception;
266 }
267
268 void
269 mediacontrol_exception_free( mediacontrol_Exception *exception )
270 {
271     if( ! exception )
272         return;
273
274     free( exception->message );
275     free( exception );
276 }
277
278 mediacontrol_RGBPicture*
279 _mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, vlc_int64_t l_date,
280                                 char* p_data, int i_datasize )
281 {
282     mediacontrol_RGBPicture *retval;
283
284     retval = mediacontrol_RGBPicture__alloc( i_datasize );
285     if( retval )
286     {
287         retval->width  = i_width;
288         retval->height = i_height;
289         retval->type   = i_chroma;
290         retval->date   = l_date;
291         retval->size   = i_datasize;
292         memcpy( retval->data, p_data, i_datasize );
293     }
294     return retval;
295 }