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