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 <vlc/mediacontrol.h>
26 #include <vlc_interface.h>
28 #include <vlc_input.h>
29 #include <vlc_demux.h>
33 #include "mediacontrol_internal.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 /* FIXME: Need to stop accessing private input structures !! */
53 #include "input/input_internal.h"
55 vlc_int64_t private_mediacontrol_unit_convert( input_thread_t *p_input,
56 mediacontrol_PositionKey from,
57 mediacontrol_PositionKey to,
63 /* For all conversions, we need data from p_input */
69 case mediacontrol_MediaTime:
70 if( to == mediacontrol_ByteCount )
74 /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
77 if( to == mediacontrol_SampleCount )
81 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
84 return( value * f_fps / 1000.0 );
87 /* See http://catb.org/~esr/jargon/html/entry/can't-happen.html */
90 case mediacontrol_SampleCount:
94 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) ||
98 if( to == mediacontrol_ByteCount )
102 /* return ( vlc_int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
106 if( to == mediacontrol_MediaTime )
107 return( vlc_int64_t )( value * 1000.0 / ( double )f_fps );
112 case mediacontrol_ByteCount:
117 // if( p_input->stream.i_mux_rate == 0 )
120 // /* Convert an offset into milliseconds. Taken from input_ext-intf.c.
121 // The 50 hardcoded constant comes from the definition of i_mux_rate :
122 // i_mux_rate : the rate we read the stream (in units of 50 bytes/s) ;
124 // if( to == mediacontrol_MediaTime )
125 // return ( vlc_int64_t )( 1000 * value / 50 / p_input->stream.i_mux_rate );
127 // if( to == mediacontrol_SampleCount )
130 // if( demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
133 // return ( vlc_int64_t )( value * f_fps / 50 / p_input->stream.i_mux_rate );
142 /* Converts a mediacontrol_Position into a time in microseconds in
145 private_mediacontrol_position2microsecond( input_thread_t* p_input, const mediacontrol_Position * pos )
147 switch( pos->origin )
149 case mediacontrol_AbsolutePosition:
150 return ( 1000 * private_mediacontrol_unit_convert( p_input,
152 mediacontrol_MediaTime, /* to */
155 case mediacontrol_RelativePosition:
163 var_Get( p_input, "time", &val );
166 l_pos = 1000 * private_mediacontrol_unit_convert( p_input,
168 mediacontrol_MediaTime,
170 return val.i_time + l_pos;
173 case mediacontrol_ModuloPosition:
181 var_Get( p_input, "length", &val );
186 l_pos = ( 1000 * private_mediacontrol_unit_convert( p_input,
188 mediacontrol_MediaTime,
194 return l_pos % val.i_time;
201 mediacontrol_RGBPicture*
202 private_mediacontrol_RGBPicture__alloc( int datasize )
204 mediacontrol_RGBPicture* pic;
206 pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
210 pic->size = datasize;
211 pic->data = ( char* )malloc( datasize * sizeof( char ) );
216 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
225 mediacontrol_PlaylistSeq*
226 private_mediacontrol_PlaylistSeq__alloc( int size )
228 mediacontrol_PlaylistSeq* ps;
230 ps =( mediacontrol_PlaylistSeq* )malloc( sizeof( mediacontrol_PlaylistSeq ) );
235 ps->data = ( char** )malloc( size * sizeof( char* ) );
240 mediacontrol_PlaylistSeq__free( mediacontrol_PlaylistSeq* ps )
245 for( i = 0 ; i < ps->size ; i++ )
253 mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
263 mediacontrol_Exception*
264 mediacontrol_exception_create( void )
266 mediacontrol_Exception* exception;
268 exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
269 mediacontrol_exception_init( exception );
274 mediacontrol_exception_init( mediacontrol_Exception *exception )
279 exception->message = NULL;
284 mediacontrol_exception_free( mediacontrol_Exception *exception )
289 free( exception->message );
293 mediacontrol_RGBPicture*
294 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, vlc_int64_t l_date,
295 char* p_data, int i_datasize )
297 mediacontrol_RGBPicture *retval;
299 retval = private_mediacontrol_RGBPicture__alloc( i_datasize );
302 retval->width = i_width;
303 retval->height = i_height;
304 retval->type = i_chroma;
305 retval->date = l_date;
306 retval->size = i_datasize;
307 memcpy( retval->data, p_data, i_datasize );