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