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