]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_util.c
Remove useless <ctype.h> inclusions
[vlc] / src / control / mediacontrol_util.c
1 /*****************************************************************************
2  * util.c: Utility functions and exceptions management
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "mediacontrol_internal.h"
29 #include <vlc/mediacontrol.h>
30
31 #include <vlc_common.h>
32 #include <vlc_vout.h>
33 #include <vlc_osd.h>
34
35 #include <stdlib.h>                                      /* malloc(), free() */
36 #include <string.h>
37
38 #include <stdio.h>
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43 #ifdef HAVE_SYS_TIME_H
44 #    include <sys/time.h>
45 #endif
46 #ifdef HAVE_SYS_TYPES_H
47 #    include <sys/types.h>
48 #endif
49
50 libvlc_time_t private_mediacontrol_unit_convert( libvlc_media_player_t *p_media_player,
51                                                  mediacontrol_PositionKey from,
52                                                  mediacontrol_PositionKey to,
53                                                  int64_t value )
54 {
55     if( to == from )
56         return value;
57
58     if( !p_media_player )
59         return 0;
60
61     switch( from )
62     {
63     case mediacontrol_MediaTime:
64         if( to == mediacontrol_ByteCount )
65         {
66             /* FIXME Unsupported */
67             /* vlc < 0.8 API */
68             /* return value * 50 * p_input->stream.i_mux_rate / 1000; */
69             return 0;
70         }
71         if( to == mediacontrol_SampleCount )
72         {
73             double f_fps;
74             libvlc_exception_t ex;
75             libvlc_exception_init( &ex );
76
77             f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
78             if( f_fps < 0 )
79                 return 0;
80             else
81                 return( value * f_fps / 1000.0 );
82         }
83         /* Cannot happen */
84         /* See http://catb.org/~esr/jargon/html/entry/can-t-happen.html */
85         break;
86
87     case mediacontrol_SampleCount:
88     {
89         double f_fps;
90         libvlc_exception_t ex;
91         libvlc_exception_init( &ex );
92
93         f_fps = libvlc_media_player_get_rate( p_media_player, &ex );
94         if( f_fps < 0 )
95             return 0;
96
97         if( to == mediacontrol_ByteCount )
98         {
99             /* FIXME */
100             /* vlc < 0.8 API */
101 /*             return ( int64_t )( value * 50 * p_input->stream.i_mux_rate / f_fps ); */
102             return 0;
103         }
104
105         if( to == mediacontrol_MediaTime )
106             return( int64_t )( value * 1000.0 / ( double )f_fps );
107
108         /* Cannot happen */
109         break;
110     }
111     case mediacontrol_ByteCount:
112         /* FIXME */
113         return 0;
114     }
115     /* Cannot happen */
116     return 0;
117 }
118
119 /* Converts a mediacontrol_Position into a time in microseconds in
120    movie clock time */
121 libvlc_time_t
122 private_mediacontrol_position2microsecond( libvlc_media_player_t * p_media_player,
123                                            const mediacontrol_Position * pos )
124 {
125     switch( pos->origin )
126     {
127     case mediacontrol_AbsolutePosition:
128         return ( 1000 * private_mediacontrol_unit_convert( p_media_player,
129                                                    pos->key, /* from */
130                                                    mediacontrol_MediaTime,  /* to */
131                                                    pos->value ) );
132         break;
133     case mediacontrol_RelativePosition:
134     {
135         libvlc_time_t l_time = 0;
136         libvlc_time_t l_pos = 0;
137         libvlc_exception_t ex;
138         libvlc_exception_init( &ex );
139
140         l_time = libvlc_media_player_get_time( p_media_player, &ex );
141         /* Ignore exception, we will assume a 0 time value */
142
143         l_pos = private_mediacontrol_unit_convert( p_media_player,
144                                                    pos->key,
145                                                    mediacontrol_MediaTime,
146                                                    pos->value );
147         return 1000 * ( l_time + l_pos );
148         break;
149     }
150     case mediacontrol_ModuloPosition:
151     {
152         libvlc_time_t l_time = 0;
153         libvlc_time_t l_length = 0;
154         libvlc_time_t l_pos = 0;
155         libvlc_exception_t ex;
156         libvlc_exception_init( &ex );
157
158         l_length = libvlc_media_player_get_length( p_media_player, &ex );
159         if( l_length <= 0 )
160             return 0;
161
162         l_time = libvlc_media_player_get_time( p_media_player, &ex );
163         /* Ignore exception, we will assume a 0 time value */
164
165         l_pos = private_mediacontrol_unit_convert( p_media_player,
166                                                    pos->key,
167                                                    mediacontrol_MediaTime,
168                                                    pos->value );
169
170         return 1000 * ( ( l_time + l_pos ) % l_length );
171         break;
172     }
173     }
174     return 0;
175 }
176
177 void
178 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
179 {
180     if( pic )
181     {
182         free( pic->data );
183         free( pic );
184     }
185 }
186
187 void
188 mediacontrol_StreamInformation__free( mediacontrol_StreamInformation* p_si )
189 {
190   if( p_si )
191   {
192       free( p_si->url );
193       free( p_si );
194   }
195 }
196
197
198 mediacontrol_Exception*
199 mediacontrol_exception_create( void )
200 {
201     mediacontrol_Exception* exception;
202
203     exception = ( mediacontrol_Exception* )malloc( sizeof( mediacontrol_Exception ) );
204     mediacontrol_exception_init( exception );
205     return exception;
206 }
207
208 void
209 mediacontrol_exception_init( mediacontrol_Exception *exception )
210 {
211     if( exception )
212     {
213         exception->code = 0;
214         exception->message = NULL;
215     }
216 }
217
218 void
219 mediacontrol_exception_cleanup( mediacontrol_Exception *exception )
220 {
221     if( exception )
222         free( exception->message );
223 }
224
225 void
226 mediacontrol_exception_free( mediacontrol_Exception *exception )
227 {
228     mediacontrol_exception_cleanup( exception );
229     free( exception );
230 }
231
232 /**
233  * Allocates and initializes a mediacontrol_RGBPicture object.
234  *
235  * @param i_width: picture width
236  * @param i_height: picture width
237  * @param i_chroma: picture chroma
238  * @param l_date: picture timestamp
239  * @param p_data: pointer to the data. The data will be directly used, not copied.
240  * @param i_datasize: data size in bytes
241  *
242  * @return the new object, or NULL on error.
243  */
244 mediacontrol_RGBPicture*
245 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, int64_t l_date,
246                                        char* p_data, int i_datasize )
247 {
248     mediacontrol_RGBPicture *retval;
249
250     retval = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
251     if( retval )
252     {
253         retval->width  = i_width;
254         retval->height = i_height;
255         retval->type   = i_chroma;
256         retval->date   = l_date;
257         retval->size   = i_datasize;
258         retval->data   = p_data;
259     }
260     return retval;
261 }