1 /*****************************************************************************
2 * util.c: Utility functions and exceptions management
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
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.
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.
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 *****************************************************************************/
24 #include "mediacontrol_internal.h"
25 #include <vlc/mediacontrol.h>
27 #include <vlc_interface.h>
29 #include <vlc_input.h>
30 #include <vlc_demux.h>
35 #include <stdlib.h> /* malloc(), free() */
38 #include <errno.h> /* ENOMEM */
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
52 #define RAISE( c, m ) exception->code = c; \
53 exception->message = strdup(m);
55 /* FIXME: Need to stop accessing private input structures !! */
56 #include "input/input_internal.h"
58 vlc_int64_t mediacontrol_unit_convert( input_thread_t *p_input,
59 mediacontrol_PositionKey from,
60 mediacontrol_PositionKey to,
66 /* For all conversions, we need data from p_input */
72 case mediacontrol_MediaTime:
73 if( to == mediacontrol_ByteCount )
77 /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
80 if( to == mediacontrol_SampleCount )
84 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
87 return( value * f_fps / 1000.0 );
90 /* See http://catb.org/~esr/jargon/html/entry/can't-happen.html */
93 case mediacontrol_SampleCount:
97 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) ||
101 if( to == mediacontrol_ByteCount )
105 /* return ( vlc_int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
109 if( to == mediacontrol_MediaTime )
110 return( vlc_int64_t )( value * 1000.0 / ( double )f_fps );
115 case mediacontrol_ByteCount:
120 // if( p_input->stream.i_mux_rate == 0 )
123 // /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
124 // The 50 hardcoded constant comes from the definition of i_mux_rate :
125 // i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
127 // if( to == mediacontrol_MediaTime )
128 // return ( vlc_int64_t )( 1000 * value / 50 / p_input->stream.i_mux_rate );
130 // if( to == mediacontrol_SampleCount )
133 // if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
136 // return ( vlc_int64_t )( value * f_fps / 50 / p_input->stream.i_mux_rate );
145 /* Converts a mediacontrol_Position into a time in microseconds in
148 mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
150 switch( pos->origin )
152 case mediacontrol_AbsolutePosition:
153 return ( 1000 * mediacontrol_unit_convert( p_input,
155 mediacontrol_MediaTime, /* to */
158 case mediacontrol_RelativePosition:
166 var_Get( p_input, "time", &val );
169 l_pos = 1000 * mediacontrol_unit_convert( p_input,
171 mediacontrol_MediaTime,
173 return val.i_time + l_pos;
176 case mediacontrol_ModuloPosition:
184 var_Get( p_input, "length", &val );
189 l_pos = ( 1000 * mediacontrol_unit_convert( p_input,
191 mediacontrol_MediaTime,
197 return l_pos % val.i_time;
204 mediacontrol_RGBPicture*
205 mediacontrol_RGBPicture__alloc( int datasize )
207 mediacontrol_RGBPicture* pic;
209 pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
213 pic->size = datasize;
214 pic->data = ( char* )malloc( datasize * sizeof( char ) );
219 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
226 mediacontrol_PlaylistSeq*
227 mediacontrol_PlaylistSeq__alloc( int size )
229 mediacontrol_PlaylistSeq* ps;
231 ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
236 ps->data = ( char** )malloc( size * sizeof( char* ) );
241 mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
246 for( i = 0 ; i < ps->size ; i++ )
254 * Initialize a VLC exception.
256 * If given a NULL pointer, it will instanciate a new exception, which
257 * should be later freed by the caller. If given an existing
258 * exception, it will reset its code and message fields. In both
259 * cases, the exception pointer is returned.
261 * \param exception a pointer on a mediacontrol_Exception
262 * \return a pointer on a mediacontrol_Exception.
264 mediacontrol_Exception*
265 mediacontrol_exception_init( mediacontrol_Exception *exception )
269 exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
276 exception->message = NULL;
281 mediacontrol_exception_free( mediacontrol_Exception *exception )
286 free( exception->message );
290 mediacontrol_RGBPicture*
291 _mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, vlc_int64_t l_date,
292 char* p_data, int i_datasize )
294 mediacontrol_RGBPicture *retval;
296 retval = mediacontrol_RGBPicture__alloc( i_datasize );
299 retval->width = i_width;
300 retval->height = i_height;
301 retval->type = i_chroma;
302 retval->date = l_date;
303 retval->size = i_datasize;
304 memcpy( retval->data, p_data, i_datasize );