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() */
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
47 #ifdef HAVE_SYS_TYPES_H
48 # include <sys/types.h>
51 libvlc_time_t private_mediacontrol_unit_convert( libvlc_media_player_t *p_media_player,
52 mediacontrol_PositionKey from,
53 mediacontrol_PositionKey to,
64 case mediacontrol_MediaTime:
65 if( to == mediacontrol_ByteCount )
67 /* FIXME Unsupported */
69 /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
72 if( to == mediacontrol_SampleCount )
75 libvlc_exception_t ex;
76 libvlc_exception_init( &ex );
78 f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
82 return( value * f_fps / 1000.0 );
85 /* See http://catb.org/~esr/jargon/html/entry/can-t-happen.html */
88 case mediacontrol_SampleCount:
91 libvlc_exception_t ex;
92 libvlc_exception_init( &ex );
94 f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
98 if( to == mediacontrol_ByteCount )
102 /* return ( int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
106 if( to == mediacontrol_MediaTime )
107 return( int64_t )( value * 1000.0 / ( double )f_fps );
112 case mediacontrol_ByteCount:
120 /* Converts a mediacontrol_Position into a time in microseconds in
123 private_mediacontrol_position2microsecond( libvlc_media_player_t * p_media_player,
124 const mediacontrol_Position * pos )
126 switch( pos->origin )
128 case mediacontrol_AbsolutePosition:
129 return ( 1000 * private_mediacontrol_unit_convert( p_media_player,
131 mediacontrol_MediaTime, /* to */
134 case mediacontrol_RelativePosition:
136 libvlc_time_t l_time = 0;
137 libvlc_time_t l_pos = 0;
138 libvlc_exception_t ex;
139 libvlc_exception_init( &ex );
141 l_time = libvlc_media_player_get_time( p_media_player, &ex );
142 /* Ignore exception, we will assume a 0 time value */
144 l_pos = private_mediacontrol_unit_convert( p_media_player,
146 mediacontrol_MediaTime,
148 return 1000 * ( l_time + l_pos );
151 case mediacontrol_ModuloPosition:
153 libvlc_time_t l_time = 0;
154 libvlc_time_t l_length = 0;
155 libvlc_time_t l_pos = 0;
156 libvlc_exception_t ex;
157 libvlc_exception_init( &ex );
159 l_length = libvlc_media_player_get_length( p_media_player, &ex );
163 l_time = libvlc_media_player_get_time( p_media_player, &ex );
164 /* Ignore exception, we will assume a 0 time value */
166 l_pos = private_mediacontrol_unit_convert( p_media_player,
168 mediacontrol_MediaTime,
171 return 1000 * ( ( l_time + l_pos ) % l_length );
179 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
189 mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
199 mediacontrol_Exception*
200 mediacontrol_exception_create( void )
202 mediacontrol_Exception* exception;
204 exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
205 mediacontrol_exception_init( exception );
210 mediacontrol_exception_init( mediacontrol_Exception *exception )
215 exception->message = NULL;
220 mediacontrol_exception_cleanup( mediacontrol_Exception *exception )
223 free( exception->message );
227 mediacontrol_exception_free( mediacontrol_Exception *exception )
229 mediacontrol_exception_cleanup( exception );
234 * Allocates and initializes a mediacontrol_RGBPicture object.
236 * @param i_width: picture width
237 * @param i_height: picture width
238 * @param i_chroma: picture chroma
239 * @param l_date: picture timestamp
240 * @param p_data: pointer to the data. The data will be directly used, not copied.
241 * @param i_datasize: data size in bytes
243 * @return the new object, or NULL on error.
245 mediacontrol_RGBPicture*
246 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, int64_t l_date,
247 char* p_data, int i_datasize )
249 mediacontrol_RGBPicture *retval;
251 retval = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
254 retval->width = i_width;
255 retval->height = i_height;
256 retval->type = i_chroma;
257 retval->date = l_date;
258 retval->size = i_datasize;
259 retval->data = p_data;