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 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
47 libvlc_time_t private_mediacontrol_unit_convert( libvlc_media_player_t *p_media_player,
48 mediacontrol_PositionKey from,
49 mediacontrol_PositionKey to,
60 case mediacontrol_MediaTime:
61 if( to == mediacontrol_ByteCount )
63 /* FIXME Unsupported */
65 /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
68 if( to == mediacontrol_SampleCount )
71 libvlc_exception_t ex;
72 libvlc_exception_init( &ex );
74 f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
78 return( value * f_fps / 1000.0 );
81 /* See http://catb.org/~esr/jargon/html/entry/can-t-happen.html */
84 case mediacontrol_SampleCount:
87 libvlc_exception_t ex;
88 libvlc_exception_init( &ex );
90 f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
94 if( to == mediacontrol_ByteCount )
98 /* return ( int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
102 if( to == mediacontrol_MediaTime )
103 return( int64_t )( value * 1000.0 / ( double )f_fps );
108 case mediacontrol_ByteCount:
116 /* Converts a mediacontrol_Position into a time in microseconds in
119 private_mediacontrol_position2microsecond( libvlc_media_player_t * p_media_player,
120 const mediacontrol_Position * pos )
122 switch( pos->origin )
124 case mediacontrol_AbsolutePosition:
125 return ( 1000 * private_mediacontrol_unit_convert( p_media_player,
127 mediacontrol_MediaTime, /* to */
130 case mediacontrol_RelativePosition:
132 libvlc_time_t l_time = 0;
133 libvlc_time_t l_pos = 0;
134 libvlc_exception_t ex;
135 libvlc_exception_init( &ex );
137 l_time = libvlc_media_player_get_time( p_media_player, &ex );
138 /* Ignore exception, we will assume a 0 time value */
140 l_pos = private_mediacontrol_unit_convert( p_media_player,
142 mediacontrol_MediaTime,
144 return 1000 * ( l_time + l_pos );
147 case mediacontrol_ModuloPosition:
149 libvlc_time_t l_time = 0;
150 libvlc_time_t l_length = 0;
151 libvlc_time_t l_pos = 0;
152 libvlc_exception_t ex;
153 libvlc_exception_init( &ex );
155 l_length = libvlc_media_player_get_length( p_media_player, &ex );
159 l_time = libvlc_media_player_get_time( p_media_player, &ex );
160 /* Ignore exception, we will assume a 0 time value */
162 l_pos = private_mediacontrol_unit_convert( p_media_player,
164 mediacontrol_MediaTime,
167 return 1000 * ( ( l_time + l_pos ) % l_length );
175 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
185 mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
195 mediacontrol_Exception*
196 mediacontrol_exception_create( void )
198 mediacontrol_Exception* exception;
200 exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
201 mediacontrol_exception_init( exception );
206 mediacontrol_exception_init( mediacontrol_Exception *exception )
211 exception->message = NULL;
216 mediacontrol_exception_cleanup( mediacontrol_Exception *exception )
219 free( exception->message );
223 mediacontrol_exception_free( mediacontrol_Exception *exception )
225 mediacontrol_exception_cleanup( exception );
230 * Allocates and initializes a mediacontrol_RGBPicture object.
232 * @param i_width: picture width
233 * @param i_height: picture width
234 * @param i_chroma: picture chroma
235 * @param l_date: picture timestamp
236 * @param p_data: pointer to the data. The data will be directly used, not copied.
237 * @param i_datasize: data size in bytes
239 * @return the new object, or NULL on error.
241 mediacontrol_RGBPicture*
242 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, int64_t l_date,
243 char* p_data, int i_datasize )
245 mediacontrol_RGBPicture *retval;
247 retval = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
250 retval->width = i_width;
251 retval->height = i_height;
252 retval->type = i_chroma;
253 retval->date = l_date;
254 retval->size = i_datasize;
255 retval->data = p_data;