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