]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_util.c
Merge branch 'master' of git@git.videolan.org:vlc
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc/mediacontrol.h>
29
30 #include <vlc_interface.h>
31 #include <vlc_aout.h>
32 #include <vlc_input.h>
33 #include <vlc_demux.h>
34
35 #include <vlc_vout.h>
36 #include <vlc_osd.h>
37 #include "mediacontrol_internal.h"
38
39 #include <stdlib.h>                                      /* malloc(), free() */
40 #include <string.h>
41
42 #include <errno.h>                                                 /* ENOMEM */
43 #include <stdio.h>
44 #include <ctype.h>
45
46 #ifdef HAVE_UNISTD_H
47 #    include <unistd.h>
48 #endif
49 #ifdef HAVE_SYS_TIME_H
50 #    include <sys/time.h>
51 #endif
52 #ifdef HAVE_SYS_TYPES_H
53 #    include <sys/types.h>
54 #endif
55
56 /* FIXME: Need to stop accessing private input structures !! */
57 #include "input/input_internal.h"
58
59 vlc_int64_t private_mediacontrol_unit_convert( input_thread_t *p_input,
60                                        mediacontrol_PositionKey from,
61                                        mediacontrol_PositionKey to,
62                                        vlc_int64_t value )
63 {
64     if( to == from )
65         return value;
66
67     /* For all conversions, we need data from p_input */
68     if( !p_input )
69         return 0;
70
71     switch( from )
72     {
73     case mediacontrol_MediaTime:
74         if( to == mediacontrol_ByteCount )
75         {
76             /* FIXME */
77             /* vlc < 0.8 API */
78             /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
79             return 0;
80         }
81         if( to == mediacontrol_SampleCount )
82         {
83             double f_fps;
84
85             if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
86                 return 0;
87             else
88                 return( value * f_fps / 1000.0 );
89         }
90         /* Cannot happen */
91         /* See http://catb.org/~esr/jargon/html/entry/can-t-happen.html */
92         break;
93
94     case mediacontrol_SampleCount:
95     {
96         double f_fps;
97
98         if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) ||
99                                 f_fps < 0.1 )
100             return 0;
101
102         if( to == mediacontrol_ByteCount )
103         {
104             /* FIXME */
105             /* vlc < 0.8 API */
106 /*             return ( vlc_int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
107             return 0;
108         }
109
110         if( to == mediacontrol_MediaTime )
111             return( vlc_int64_t )( value * 1000.0 / ( double )f_fps );
112
113         /* Cannot happen */
114         break;
115     }
116     case mediacontrol_ByteCount:
117         /* FIXME */
118         return 0;
119 /* vlc < 0.8 API: */
120
121 //         if( p_input->stream.i_mux_rate == 0 )
122 //             return 0;
123 //
124 //         /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
125 //            The 50 hardcoded constant comes from the definition of i_mux_rate :
126 //            i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
127 //            0 if undef */
128 //         if( to == mediacontrol_MediaTime )
129 //             return ( vlc_int64_t )( 1000 * value / 50 / p_input->stream.i_mux_rate );
130 //
131 //         if( to == mediacontrol_SampleCount )
132 //         {
133 //             double f_fps;
134 //             if( demux_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
135 //                 return 0;
136 //             else
137 //                 return ( vlc_int64_t )( value * f_fps / 50 / p_input->stream.i_mux_rate );
138 //         }
139         /* Cannot happen */
140         break;
141     }
142     /* Cannot happen */
143     return 0;
144 }
145
146 /* Converts a mediacontrol_Position into a time in microseconds in
147    movie clock time */
148 vlc_int64_t
149 private_mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
150 {
151     switch( pos->origin )
152     {
153     case mediacontrol_AbsolutePosition:
154         return ( 1000 * private_mediacontrol_unit_convert( p_input,
155                                                    pos->key, /* from */
156                                                    mediacontrol_MediaTime,  /* to */
157                                                    pos->value ) );
158         break;
159     case mediacontrol_RelativePosition:
160     {
161         vlc_int64_t l_pos;
162         vlc_value_t val;
163
164         val.i_time = 0;
165         if( p_input )
166         {
167             var_Get( p_input, "time", &val );
168         }
169
170         l_pos = 1000 * private_mediacontrol_unit_convert( p_input,
171                                                   pos->key,
172                                                   mediacontrol_MediaTime,
173                                                   pos->value );
174         return val.i_time + l_pos;
175         break;
176     }
177     case mediacontrol_ModuloPosition:
178     {
179         vlc_int64_t l_pos;
180         vlc_value_t val;
181
182         val.i_time = 0;
183         if( p_input )
184         {
185             var_Get( p_input, "length", &val );
186         }
187
188         if( val.i_time > 0)
189         {
190             l_pos = ( 1000 * private_mediacontrol_unit_convert( p_input,
191                                                         pos->key,
192                                                         mediacontrol_MediaTime,
193                                                         pos->value ) );
194         }
195         else
196             l_pos = 0;
197
198         return l_pos % val.i_time;
199         break;
200     }
201     }
202     return 0;
203 }
204
205 mediacontrol_RGBPicture*
206 private_mediacontrol_RGBPicture__alloc( int datasize )
207 {
208     mediacontrol_RGBPicture* pic;
209
210     pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
211     if( ! pic )
212         return NULL;
213
214     pic->size = datasize;
215     pic->data = ( char* )malloc( datasize * sizeof( char ) );
216     return pic;
217 }
218
219 void
220 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
221 {
222     if( pic )
223     {
224         free( pic->data );
225         free( pic );
226     }
227 }
228
229 mediacontrol_PlaylistSeq*
230 private_mediacontrol_PlaylistSeq__alloc( int size )
231 {
232     mediacontrol_PlaylistSeq* ps;
233
234     ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
235     if( ! ps )
236         return NULL;
237
238     ps->size = size;
239     ps->data = ( char** )malloc( size * sizeof( char* ) );
240     return ps;
241 }
242
243 void
244 mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
245 {
246     if( ps )
247     {
248         int i;
249         for( i = 0 ; i < ps->size ; i++ )
250             free( ps->data[i] );
251         free( ps->data );
252         free( ps );
253     }
254 }
255
256 void
257 mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
258 {
259   if( p_si )
260   {
261       free( p_si->url );
262       free( p_si );
263   }
264 }
265
266
267 mediacontrol_Exception*
268 mediacontrol_exception_create( void )
269 {
270     mediacontrol_Exception* exception;
271  
272     exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
273     mediacontrol_exception_init( exception );
274     return exception;
275 }
276
277 void
278 mediacontrol_exception_init( mediacontrol_Exception *exception )
279 {
280     if( exception )
281     {
282         exception->code = 0;
283         exception->message = NULL;
284     }
285 }
286
287 void
288 mediacontrol_exception_cleanup( mediacontrol_Exception *exception )
289 {
290     if( exception )
291         free( exception->message );
292 }
293
294 void
295 mediacontrol_exception_free( mediacontrol_Exception *exception )
296 {
297     mediacontrol_exception_cleanup( exception );
298     free( exception );
299 }
300
301 mediacontrol_RGBPicture*
302 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, vlc_int64_t l_date,
303                                 char* p_data, int i_datasize )
304 {
305     mediacontrol_RGBPicture *retval;
306
307     retval = private_mediacontrol_RGBPicture__alloc( i_datasize );
308     if( retval )
309     {
310         retval->width  = i_width;
311         retval->height = i_height;
312         retval->type   = i_chroma;
313         retval->date   = l_date;
314         retval->size   = i_datasize;
315         memcpy( retval->data, p_data, i_datasize );
316     }
317     return retval;
318 }