]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_audio_video.c
Fix a warning.
[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
36 #include <stdlib.h>                                      /* malloc(), free() */
37 #include <string.h>
38
39 #include <errno.h>                                                 /* ENOMEM */
40 #include <stdio.h>
41 #include <ctype.h>
42
43 #ifdef HAVE_UNISTD_H
44 #    include <unistd.h>
45 #endif
46 #ifdef HAVE_SYS_TIME_H
47 #    include <sys/time.h>
48 #endif
49 #ifdef HAVE_SYS_TYPES_H
50 #    include <sys/types.h>
51 #endif
52
53 mediacontrol_RGBPicture *
54 mediacontrol_snapshot( mediacontrol_Instance *self,
55                        const mediacontrol_Position * a_position,
56                        mediacontrol_Exception *exception )
57 {
58     vlc_object_t* p_cache;
59     vout_thread_t* p_vout;
60     input_thread_t *p_input;
61     mediacontrol_RGBPicture *p_pic = NULL;
62     char path[256];
63     snapshot_t *p_snapshot;
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     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
75     if( ! p_vout )
76     {
77         RAISE_NULL( mediacontrol_InternalException, "No video output" );
78     }
79     p_cache = vlc_object_create( p_input, VLC_OBJECT_GENERIC );
80     if( p_cache == NULL )
81     {
82         vlc_object_release( p_vout );
83         vlc_object_release( p_input );
84         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
85     }
86     snprintf( path, 255, "object:%d", p_cache->i_object_id );
87     var_SetString( p_vout, "snapshot-path", path );
88     var_SetString( p_vout, "snapshot-format", "png" );
89
90     vlc_object_lock( p_cache );
91     vout_Control( p_vout, VOUT_SNAPSHOT );
92     vlc_object_wait( p_cache );
93     vlc_object_release( p_vout );
94
95     p_snapshot = ( snapshot_t* ) p_cache->p_private;
96     vlc_object_unlock( p_cache );
97     vlc_object_release( p_cache );
98     vlc_object_release( p_input );
99
100     if( p_snapshot )
101     {
102         p_pic = private_mediacontrol_createRGBPicture( p_snapshot->i_width,
103                                p_snapshot->i_height,
104                                VLC_FOURCC( 'p','n','g',' ' ),
105                                p_snapshot->date,
106                                p_snapshot->p_data,
107                                p_snapshot->i_datasize );
108         if( !p_pic )
109         {
110             free( p_snapshot->p_data );
111             free( p_snapshot );
112             RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
113         }
114     }
115     else
116     {
117         RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
118     }
119     return p_pic;
120 }
121
122 static
123 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
124                            char *psz_string, text_style_t *p_style,
125                            int i_flags, int i_hmargin, int i_vmargin,
126                            mtime_t i_start, mtime_t i_stop )
127 {
128     subpicture_t *p_spu;
129     video_format_t fmt;
130
131     if( !psz_string ) return VLC_EGENERIC;
132
133     p_spu = spu_CreateSubpicture( p_vout->p_spu );
134     if( !p_spu ) return VLC_EGENERIC;
135
136     /* Create a new subpicture region */
137     memset( &fmt, 0, sizeof(video_format_t) );
138     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
139     fmt.i_aspect = 0;
140     fmt.i_width = fmt.i_height = 0;
141     fmt.i_x_offset = fmt.i_y_offset = 0;
142     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
143     if( !p_spu->p_region )
144     {
145         msg_Err( p_vout, "cannot allocate SPU region" );
146         spu_DestroySubpicture( p_vout->p_spu, p_spu );
147         return VLC_EGENERIC;
148     }
149
150     p_spu->p_region->psz_text = strdup( psz_string );
151     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
152     p_spu->i_start = i_start;
153     p_spu->i_stop = i_stop;
154     p_spu->b_ephemer = false;
155     p_spu->b_absolute = false;
156
157     p_spu->i_x = i_hmargin;
158     p_spu->i_y = i_vmargin;
159     p_spu->i_flags = i_flags & ~SUBPICTURE_ALIGN_MASK;
160     p_spu->i_channel = i_channel;
161
162     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
163
164     return VLC_SUCCESS;
165 }
166
167
168 void
169 mediacontrol_display_text( mediacontrol_Instance *self,
170                            const char * message,
171                            const mediacontrol_Position * begin,
172                            const mediacontrol_Position * end,
173                            mediacontrol_Exception *exception )
174 {
175     vout_thread_t *p_vout = NULL;
176     char* psz_message;
177     input_thread_t *p_input;
178     libvlc_exception_t ex;
179
180     libvlc_exception_init( &ex );
181     mediacontrol_exception_init( exception );
182
183     p_input = libvlc_get_input_thread( self->p_media_player, &ex );
184     if( ! p_input )
185     {
186         RAISE_VOID( mediacontrol_InternalException, "No input" );
187     }
188     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
189     if( ! p_vout )
190     {
191         RAISE_VOID( mediacontrol_InternalException, "No video output" );
192     }
193
194     psz_message = strdup( message );
195     if( !psz_message )
196     {
197         RAISE_VOID( mediacontrol_InternalException, "no more memory" );
198     }
199
200     if( begin->origin == mediacontrol_RelativePosition &&
201         begin->value == 0 &&
202         end->origin == mediacontrol_RelativePosition )
203     {
204         mtime_t i_duration = 0;
205         mtime_t i_now = mdate();
206
207         i_duration = 1000 * private_mediacontrol_unit_convert(
208                                                               self->p_media_player,
209                                                               end->key,
210                                                               mediacontrol_MediaTime,
211                                                               end->value );
212
213         mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
214                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
215                                i_now, i_now + i_duration );
216     }
217     else
218     {
219         mtime_t i_debut, i_fin, i_now;
220
221         /* FIXME */
222         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
223         i_now = mdate();
224
225         i_debut = private_mediacontrol_position2microsecond( self->p_media_player,
226                                             ( mediacontrol_Position* ) begin );
227         i_debut += i_now;
228
229         i_fin = private_mediacontrol_position2microsecond( self->p_media_player,
230                                           ( mediacontrol_Position * ) end );
231         i_fin += i_now;
232
233         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
234                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
235                                i_debut, i_fin );
236     }
237
238     vlc_object_release( p_vout );
239 }
240
241 unsigned short
242 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
243                                mediacontrol_Exception *exception )
244 {
245     libvlc_exception_t ex;
246     int i_ret = 0;
247
248     mediacontrol_exception_init( exception );
249     libvlc_exception_init( &ex );
250
251     i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
252     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
253     /* FIXME: Normalize in [0..100] */
254     return (unsigned short)i_ret;
255 }
256
257 void
258 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
259                                const unsigned short volume,
260                                mediacontrol_Exception *exception )
261 {
262     /* FIXME: Normalize in [0..100] */
263     libvlc_exception_t ex;
264
265     mediacontrol_exception_init( exception );
266     libvlc_exception_init( &ex );
267
268     libvlc_audio_set_volume( self->p_instance, volume, &ex );
269     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
270 }
271
272 bool mediacontrol_set_visual( mediacontrol_Instance *self,
273                                     WINDOWHANDLE visual_id,
274                                     mediacontrol_Exception *exception )
275 {
276     libvlc_exception_t ex;
277
278     mediacontrol_exception_init( exception );
279     libvlc_exception_init( &ex );
280
281     libvlc_media_player_set_drawable( self->p_media_player, (libvlc_drawable_t)visual_id, &ex );
282     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
283     return true;
284 }
285
286 int
287 mediacontrol_get_rate( mediacontrol_Instance *self,
288                mediacontrol_Exception *exception )
289 {
290     libvlc_exception_t ex;
291     int i_ret;
292
293     mediacontrol_exception_init( exception );
294     libvlc_exception_init( &ex );
295
296     i_ret = libvlc_media_player_get_rate( self->p_media_player, &ex );
297     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
298
299     return i_ret / 10;
300 }
301
302 void
303 mediacontrol_set_rate( mediacontrol_Instance *self,
304                const int rate,
305                mediacontrol_Exception *exception )
306 {
307     libvlc_exception_t ex;
308
309     mediacontrol_exception_init( exception );
310     libvlc_exception_init( &ex );
311
312     libvlc_media_player_set_rate( self->p_media_player, rate * 10, &ex );
313     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
314 }
315
316 int
317 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
318                  mediacontrol_Exception *exception )
319 {
320     libvlc_exception_t ex;
321     int i_ret;
322
323     mediacontrol_exception_init( exception );
324     libvlc_exception_init( &ex );
325
326     i_ret = libvlc_get_fullscreen( self->p_media_player, &ex );
327     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
328
329     return i_ret;
330 }
331
332 void
333 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
334                  const int b_fullscreen,
335                  mediacontrol_Exception *exception )
336 {
337     libvlc_exception_t ex;
338
339     mediacontrol_exception_init( exception );
340     libvlc_exception_init( &ex );
341
342     libvlc_set_fullscreen( self->p_media_player, b_fullscreen, &ex );
343     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
344 }