]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_audio_video.c
Do not leak exception message
[vlc] / src / control / mediacontrol_audio_video.c
1 /*****************************************************************************
2  * audio_video.c: Audio/Video management : volume, snapshot, OSD
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 #include "mediacontrol_internal.h"
28 #include "libvlc_internal.h"
29
30 #include <vlc/mediacontrol.h>
31 #include <vlc/libvlc.h>
32
33 #include <vlc_vout.h>
34 #include <vlc_osd.h>
35 #include <vlc_block.h>
36
37 #include <stdlib.h>                                      /* malloc(), free() */
38 #include <string.h>
39
40 #include <errno.h>                                                 /* ENOMEM */
41 #include <stdio.h>
42 #include <ctype.h>
43 #include "../video_output/vout_control.h"
44
45 #ifdef HAVE_UNISTD_H
46 #    include <unistd.h>
47 #endif
48 #ifdef HAVE_SYS_TIME_H
49 #    include <sys/time.h>
50 #endif
51 #ifdef HAVE_SYS_TYPES_H
52 #    include <sys/types.h>
53 #endif
54
55 mediacontrol_RGBPicture *
56 mediacontrol_snapshot( mediacontrol_Instance *self,
57                        const mediacontrol_Position * a_position,
58                        mediacontrol_Exception *exception )
59 {
60     (void)a_position;
61     vout_thread_t* p_vout;
62     input_thread_t *p_input;
63     mediacontrol_RGBPicture *p_pic;
64     libvlc_exception_t ex;
65
66     libvlc_exception_init( &ex );
67     mediacontrol_exception_init( exception );
68
69     p_input = libvlc_get_input_thread( self->p_media_player, &ex );
70     if( ! p_input )
71     {
72         RAISE_NULL( mediacontrol_InternalException, "No input" );
73     }
74
75     p_vout = input_GetVout( p_input );
76     vlc_object_release( p_input );
77     if( ! p_vout )
78     {
79         RAISE_NULL( mediacontrol_InternalException, "No video output" );
80     }
81
82     block_t *p_image;
83     video_format_t fmt;
84
85     if( vout_GetSnapshot( p_vout, &p_image, NULL, &fmt, "png", 500*1000 ) )
86     {
87         RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
88         return NULL;
89     }
90
91     /* */
92     char *p_data = malloc( p_image->i_buffer );
93     if( p_data )
94     {
95         memcpy( p_data, p_image->p_buffer, p_image->i_buffer );
96         p_pic = private_mediacontrol_createRGBPicture( fmt.i_width,
97                                                        fmt.i_height,
98                                                        fmt.i_chroma,
99                                                        p_image->i_pts,
100                                                        p_data,
101                                                        p_image->i_buffer );
102     }
103     else
104     {
105         p_pic = NULL;
106     }
107     block_Release( p_image );
108
109     if( !p_pic )
110         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
111     return p_pic;
112 }
113
114 static
115 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
116                            const char *psz_string, text_style_t *p_style,
117                            int i_flags, int i_hmargin, int i_vmargin,
118                            mtime_t i_start, mtime_t i_stop )
119 {
120     return osd_ShowTextAbsolute( p_vout->p_spu, i_channel,
121                                  psz_string, p_style,
122                                  i_flags, i_hmargin, i_vmargin,
123                                  i_start, i_stop );
124 }
125
126
127 void
128 mediacontrol_display_text( mediacontrol_Instance *self,
129                            const char * message,
130                            const mediacontrol_Position * begin,
131                            const mediacontrol_Position * end,
132                            mediacontrol_Exception *exception )
133 {
134     vout_thread_t *p_vout = NULL;
135     input_thread_t *p_input;
136     libvlc_exception_t ex;
137
138     libvlc_exception_init( &ex );
139     mediacontrol_exception_init( exception );
140
141     if( !message )
142     {
143         RAISE_VOID( mediacontrol_InternalException, "Empty text" );
144     }
145
146     p_input = libvlc_get_input_thread( self->p_media_player, &ex );
147     if( ! p_input )
148     {
149         RAISE_VOID( mediacontrol_InternalException, "No input" );
150     }
151     p_vout = input_GetVout( p_input );
152     if( ! p_vout )
153     {
154         RAISE_VOID( mediacontrol_InternalException, "No video output" );
155     }
156
157     if( begin->origin == mediacontrol_RelativePosition &&
158         begin->value == 0 &&
159         end->origin == mediacontrol_RelativePosition )
160     {
161         mtime_t i_duration = 0;
162         mtime_t i_now = mdate();
163
164         i_duration = 1000 * private_mediacontrol_unit_convert(
165                                                               self->p_media_player,
166                                                               end->key,
167                                                               mediacontrol_MediaTime,
168                                                               end->value );
169
170         mediacontrol_showtext( p_vout, DEFAULT_CHAN, message, NULL,
171                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
172                                i_now, i_now + i_duration );
173     }
174     else
175     {
176         mtime_t i_debut, i_fin, i_now;
177
178         /* FIXME */
179         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
180         i_now = mdate();
181
182         i_debut = private_mediacontrol_position2microsecond( self->p_media_player,
183                                             ( mediacontrol_Position* ) begin );
184         i_debut += i_now;
185
186         i_fin = private_mediacontrol_position2microsecond( self->p_media_player,
187                                           ( mediacontrol_Position * ) end );
188         i_fin += i_now;
189
190         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, message, NULL,
191                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
192                                i_debut, i_fin );
193     }
194
195     vlc_object_release( p_vout );
196 }
197
198 unsigned short
199 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
200                                mediacontrol_Exception *exception )
201 {
202     libvlc_exception_t ex;
203     int i_ret = 0;
204
205     mediacontrol_exception_init( exception );
206     libvlc_exception_init( &ex );
207
208     i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
209     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
210     /* FIXME: Normalize in [0..100] */
211     return (unsigned short)i_ret;
212 }
213
214 void
215 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
216                                const unsigned short volume,
217                                mediacontrol_Exception *exception )
218 {
219     /* FIXME: Normalize in [0..100] */
220     libvlc_exception_t ex;
221
222     mediacontrol_exception_init( exception );
223     libvlc_exception_init( &ex );
224
225     libvlc_audio_set_volume( self->p_instance, volume, &ex );
226     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
227 }
228
229 int mediacontrol_set_visual( mediacontrol_Instance *self,
230                                     WINDOWHANDLE visual_id,
231                                     mediacontrol_Exception *exception )
232 {
233     libvlc_exception_t ex;
234
235     mediacontrol_exception_init( exception );
236     libvlc_exception_init( &ex );
237
238     libvlc_media_player_set_drawable( self->p_media_player, (libvlc_drawable_t)visual_id, &ex );
239     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
240     return true;
241 }
242
243 int
244 mediacontrol_get_rate( mediacontrol_Instance *self,
245                mediacontrol_Exception *exception )
246 {
247     libvlc_exception_t ex;
248     int i_ret;
249
250     mediacontrol_exception_init( exception );
251     libvlc_exception_init( &ex );
252
253     i_ret = libvlc_media_player_get_rate( self->p_media_player, &ex );
254     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
255
256     return i_ret / 10;
257 }
258
259 void
260 mediacontrol_set_rate( mediacontrol_Instance *self,
261                const int rate,
262                mediacontrol_Exception *exception )
263 {
264     libvlc_exception_t ex;
265
266     mediacontrol_exception_init( exception );
267     libvlc_exception_init( &ex );
268
269     libvlc_media_player_set_rate( self->p_media_player, rate * 10, &ex );
270     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
271 }
272
273 int
274 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
275                  mediacontrol_Exception *exception )
276 {
277     libvlc_exception_t ex;
278     int i_ret;
279
280     mediacontrol_exception_init( exception );
281     libvlc_exception_init( &ex );
282
283     i_ret = libvlc_get_fullscreen( self->p_media_player, &ex );
284     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
285
286     return i_ret;
287 }
288
289 void
290 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
291                  const int b_fullscreen,
292                  mediacontrol_Exception *exception )
293 {
294     libvlc_exception_t ex;
295
296     mediacontrol_exception_init( exception );
297     libvlc_exception_init( &ex );
298
299     libvlc_set_fullscreen( self->p_media_player, b_fullscreen, &ex );
300     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
301 }