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