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