]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_audio_video.c
Merge branch 'master' of git@git.videolan.org:vlc
[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
29 #include <vlc/mediacontrol.h>
30 #include <vlc/libvlc.h>
31
32 #include <vlc_playlist.h>
33
34 #include <vlc_aout.h>
35
36 #include <vlc_vout.h>
37 #include <vlc_osd.h>
38
39 #include <stdlib.h>                                      /* malloc(), free() */
40 #include <string.h>
41
42 #include <errno.h>                                                 /* ENOMEM */
43 #include <stdio.h>
44 #include <ctype.h>
45
46 #ifdef HAVE_UNISTD_H
47 #    include <unistd.h>
48 #endif
49 #ifdef HAVE_SYS_TIME_H
50 #    include <sys/time.h>
51 #endif
52 #ifdef HAVE_SYS_TYPES_H
53 #    include <sys/types.h>
54 #endif
55
56 mediacontrol_RGBPicture *
57 mediacontrol_snapshot( mediacontrol_Instance *self,
58                        const mediacontrol_Position * a_position,
59                        mediacontrol_Exception *exception )
60 {
61     vlc_object_t* p_cache;
62     vout_thread_t* p_vout;
63     mediacontrol_RGBPicture *p_pic = NULL;
64     char path[256];
65     snapshot_t *p_snapshot;
66
67     mediacontrol_exception_init( exception );
68
69     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
70     if( ! p_vout )
71     {
72         RAISE_NULL( mediacontrol_InternalException, "No video output" );
73     }
74     p_cache = vlc_object_create( self->p_playlist, VLC_OBJECT_GENERIC );
75     if( p_cache == NULL )
76     {
77         vlc_object_release( p_vout );
78         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
79     }
80     snprintf( path, 255, "object:%d", p_cache->i_object_id );
81     var_SetString( p_vout, "snapshot-path", path );
82     var_SetString( p_vout, "snapshot-format", "png" );
83
84     vlc_object_lock( p_cache );
85     vout_Control( p_vout, VOUT_SNAPSHOT );
86     vlc_object_wait( p_cache );
87     vlc_object_release( p_vout );
88
89     p_snapshot = ( snapshot_t* ) p_cache->p_private;
90     vlc_object_unlock( p_cache );
91     vlc_object_release( p_cache );
92
93     if( p_snapshot )
94     {
95         p_pic = private_mediacontrol_createRGBPicture( p_snapshot->i_width,
96                                p_snapshot->i_height,
97                                VLC_FOURCC( 'p','n','g',' ' ),
98                                p_snapshot->date,
99                                p_snapshot->p_data,
100                                p_snapshot->i_datasize );
101         if( !p_pic )
102         {
103             free( p_snapshot->p_data );
104             free( p_snapshot );
105             RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
106         }
107     }
108     else
109     {
110         RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
111     }
112     return p_pic;
113 }
114
115 mediacontrol_RGBPicture **
116 mediacontrol_all_snapshots( mediacontrol_Instance *self,
117                             mediacontrol_Exception *exception )
118 {
119     mediacontrol_exception_init( exception );
120
121     RAISE_NULL( mediacontrol_InternalException, "unsupported method" );
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->i_start = i_start;
155     p_spu->i_stop = i_stop;
156     p_spu->b_ephemer = false;
157     p_spu->b_absolute = false;
158
159     p_spu->i_x = i_hmargin;
160     p_spu->i_y = i_vmargin;
161     p_spu->i_flags = i_flags & ~SUBPICTURE_ALIGN_MASK;
162     p_spu->i_channel = i_channel;
163
164     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
165
166     return VLC_SUCCESS;
167 }
168
169
170 void
171 mediacontrol_display_text( mediacontrol_Instance *self,
172                            const char * message,
173                            const mediacontrol_Position * begin,
174                            const mediacontrol_Position * end,
175                            mediacontrol_Exception *exception )
176 {
177     input_thread_t *p_input = NULL;
178     vout_thread_t *p_vout = NULL;
179     char* psz_message;
180
181     psz_message = strdup( message );
182     if( !psz_message )
183     {
184         RAISE_VOID( mediacontrol_InternalException, "no more memory" );
185     }
186
187     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
188     if( ! p_vout )
189     {
190         RAISE_VOID( mediacontrol_InternalException, "no video output" );
191     }
192
193     if( begin->origin == mediacontrol_RelativePosition &&
194         begin->value == 0 &&
195         end->origin == mediacontrol_RelativePosition )
196     {
197         mtime_t i_duration = 0;
198         mtime_t i_now = mdate();
199
200         i_duration = 1000 * private_mediacontrol_unit_convert(
201                                                 self->p_playlist->p_input,
202                                                 end->key,
203                                                 mediacontrol_MediaTime,
204                                                 end->value );
205
206         mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
207                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
208                                i_now, i_now + i_duration );
209     }
210     else
211     {
212         mtime_t i_debut, i_fin, i_now;
213
214         p_input = self->p_playlist->p_input;
215         if( ! p_input )
216         {
217             vlc_object_release( p_vout );
218             RAISE_VOID( mediacontrol_InternalException, "No input" );
219         }
220
221         /* FIXME */
222         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
223         i_now = mdate();
224
225         i_debut = private_mediacontrol_position2microsecond( p_input,
226                                             ( mediacontrol_Position* ) begin );
227         i_debut += i_now;
228
229         i_fin = private_mediacontrol_position2microsecond( p_input,
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_video_set_parent( self->p_instance, 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     libvlc_media_player_t* p_mi;
292     int i_ret;
293
294     mediacontrol_exception_init( exception );
295     libvlc_exception_init( &ex );
296
297     p_mi = libvlc_playlist_get_media_player( self->p_instance, &ex );
298     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
299
300     i_ret = libvlc_media_player_get_rate( p_mi, &ex );
301     libvlc_media_player_release( p_mi );
302     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
303
304     return i_ret / 10;
305 }
306
307 void
308 mediacontrol_set_rate( mediacontrol_Instance *self,
309                const int rate,
310                mediacontrol_Exception *exception )
311 {
312     libvlc_exception_t ex;
313     libvlc_media_player_t* p_mi;
314
315     mediacontrol_exception_init( exception );
316     libvlc_exception_init( &ex );
317
318     p_mi = libvlc_playlist_get_media_player( self->p_instance, &ex );
319     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
320
321     libvlc_media_player_set_rate( p_mi, rate * 10, &ex );
322     libvlc_media_player_release( p_mi );
323     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
324 }
325
326 int
327 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
328                  mediacontrol_Exception *exception )
329 {
330     libvlc_exception_t ex;
331     libvlc_media_player_t* p_mi;
332     int i_ret;
333
334     mediacontrol_exception_init( exception );
335     libvlc_exception_init( &ex );
336
337     p_mi = libvlc_playlist_get_media_player( self->p_instance, &ex );
338     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
339
340     i_ret = libvlc_get_fullscreen( p_mi, &ex );
341     libvlc_media_player_release( p_mi );
342     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
343
344     return i_ret;
345 }
346
347 void
348 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
349                  const int b_fullscreen,
350                  mediacontrol_Exception *exception )
351 {
352     libvlc_exception_t ex;
353     libvlc_media_player_t* p_mi;
354
355     mediacontrol_exception_init( exception );
356     libvlc_exception_init( &ex );
357
358     p_mi = libvlc_playlist_get_media_player( self->p_instance, &ex );
359     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
360
361     libvlc_set_fullscreen( p_mi, b_fullscreen, &ex );
362     libvlc_media_player_release( p_mi );
363     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
364 }