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 *****************************************************************************/
28 #include "mediacontrol_internal.h"
29 #include <vlc/mediacontrol.h>
31 #include <vlc_common.h>
35 #include <stdlib.h> /* malloc(), free() */
43 #include <sys/types.h>
45 libvlc_time_t private_mediacontrol_unit_convert( libvlc_media_player_t *p_media_player,
46 mediacontrol_PositionKey from,
47 mediacontrol_PositionKey to,
58 case mediacontrol_MediaTime:
59 if( to == mediacontrol_ByteCount )
61 /* FIXME Unsupported */
63 /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
66 if( to == mediacontrol_SampleCount )
69 libvlc_exception_t ex;
70 libvlc_exception_init( &ex );
72 f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
76 return( value * f_fps / 1000.0 );
79 /* See http://catb.org/~esr/jargon/html/entry/can-t-happen.html */
82 case mediacontrol_SampleCount:
85 libvlc_exception_t ex;
86 libvlc_exception_init( &ex );
88 f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
92 if( to == mediacontrol_ByteCount )
96 /* return ( int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
100 if( to == mediacontrol_MediaTime )
101 return( int64_t )( value * 1000.0 / ( double )f_fps );
106 case mediacontrol_ByteCount:
114 /* Converts a mediacontrol_Position into a time in microseconds in
117 private_mediacontrol_position2microsecond( libvlc_media_player_t * p_media_player,
118 const mediacontrol_Position * pos )
120 switch( pos->origin )
122 case mediacontrol_AbsolutePosition:
123 return ( 1000 * private_mediacontrol_unit_convert( p_media_player,
125 mediacontrol_MediaTime, /* to */
128 case mediacontrol_RelativePosition:
130 libvlc_time_t l_time = 0;
131 libvlc_time_t l_pos = 0;
132 libvlc_exception_t ex;
133 libvlc_exception_init( &ex );
135 l_time = libvlc_media_player_get_time( p_media_player, &ex );
136 /* Ignore exception, we will assume a 0 time value */
138 l_pos = private_mediacontrol_unit_convert( p_media_player,
140 mediacontrol_MediaTime,
142 return 1000 * ( l_time + l_pos );
145 case mediacontrol_ModuloPosition:
147 libvlc_time_t l_time = 0;
148 libvlc_time_t l_length = 0;
149 libvlc_time_t l_pos = 0;
150 libvlc_exception_t ex;
151 libvlc_exception_init( &ex );
153 l_length = libvlc_media_player_get_length( p_media_player, &ex );
157 l_time = libvlc_media_player_get_time( p_media_player, &ex );
158 /* Ignore exception, we will assume a 0 time value */
160 l_pos = private_mediacontrol_unit_convert( p_media_player,
162 mediacontrol_MediaTime,
165 return 1000 * ( ( l_time + l_pos ) % l_length );
173 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
183 mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
193 mediacontrol_Exception*
194 mediacontrol_exception_create( void )
196 mediacontrol_Exception* exception;
198 exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
199 mediacontrol_exception_init( exception );
204 mediacontrol_exception_init( mediacontrol_Exception *exception )
209 exception->message = NULL;
214 mediacontrol_exception_cleanup( mediacontrol_Exception *exception )
217 free( exception->message );
221 mediacontrol_exception_free( mediacontrol_Exception *exception )
223 mediacontrol_exception_cleanup( exception );
228 * Allocates and initializes a mediacontrol_RGBPicture object.
230 * @param i_width: picture width
231 * @param i_height: picture width
232 * @param i_chroma: picture chroma
233 * @param l_date: picture timestamp
234 * @param p_data: pointer to the data. The data will be directly used, not copied.
235 * @param i_datasize: data size in bytes
237 * @return the new object, or NULL on error.
239 mediacontrol_RGBPicture*
240 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, int64_t l_date,
241 char* p_data, int i_datasize )
243 mediacontrol_RGBPicture *retval;
245 retval = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
248 retval->width = i_width;
249 retval->height = i_height;
250 retval->type = i_chroma;
251 retval->date = l_date;
252 retval->size = i_datasize;
253 retval->data = p_data;