Kill stupid VOUT_SNAPSHOT control.
[vlc.git] / 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     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
70     p_snapshot = malloc( sizeof( snapshot_t ) );
71     if( ! p_snapshot )
72     {
73         RAISE_NULL( mediacontrol_InternalException, "Cannot allocate snapshot" );
74     }
75
76     p_input = libvlc_get_input_thread( self->p_media_player, &ex );
77     if( ! p_input )
78     {
79         RAISE_NULL( mediacontrol_InternalException, "No input" );
80     }
81
82     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
83     vlc_object_release( p_input );
84     if( ! p_vout )
85     {
86         RAISE_NULL( mediacontrol_InternalException, "No video output" );
87     }
88
89     snprintf( path, 255, "object:%p", p_snapshot );
90     var_SetString( p_vout, "snapshot-path", path );
91     var_SetString( p_vout, "snapshot-format", "png" );
92
93     vlc_mutex_init( &p_snapshot->p_mutex );
94     vlc_cond_init( &p_snapshot->p_condvar );
95
96     vlc_mutex_lock( &p_snapshot->p_mutex );
97     mutex_cleanup_push( &p_snapshot->p_mutex );
98
99     /* Use p_snapshot address as sentinel against spurious vlc_object_wait wakeups.
100
101        If a legitimate wakeup occurs, then p_snapshot->p_data will hold either
102        NULL (in case of error) or a pointer to valid data. */
103     p_snapshot->p_data = ( char* )p_snapshot;
104
105     var_TriggerCallback( p_vout, "video-snapshot" );
106     while ( p_snapshot->p_data == ( char* )p_snapshot )
107     {
108         vlc_cond_wait( &p_snapshot->p_condvar, &p_snapshot->p_mutex );
109     }
110     vlc_cleanup_pop();
111
112     vlc_object_release( p_vout );
113
114     vlc_mutex_unlock( &p_snapshot->p_mutex );
115     vlc_cond_destroy( &p_snapshot->p_condvar );
116     vlc_mutex_destroy( &p_snapshot->p_mutex );
117
118     if( p_snapshot->p_data )
119     {
120         /* Note: p_snapshot->p_data is directly used, not copied. Thus
121            do not free it here. */
122         p_pic = private_mediacontrol_createRGBPicture( p_snapshot->i_width,
123                                                        p_snapshot->i_height,
124                                                        VLC_FOURCC( 'p','n','g',' ' ),
125                                                        p_snapshot->date,
126                                                        p_snapshot->p_data,
127                                                        p_snapshot->i_datasize );
128         if( !p_pic )
129         {
130             free( p_snapshot );
131             RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
132         }
133     }
134     else
135     {
136         RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
137     }
138     return p_pic;
139 }
140
141 static
142 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
143                            const char *psz_string, text_style_t *p_style,
144                            int i_flags, int i_hmargin, int i_vmargin,
145                            mtime_t i_start, mtime_t i_stop )
146 {
147     return osd_ShowTextAbsolute( p_vout->p_spu, i_channel,
148                                  psz_string, p_style,
149                                  i_flags, i_hmargin, i_vmargin,
150                                  i_start, i_stop );
151 }
152
153
154 void
155 mediacontrol_display_text( mediacontrol_Instance *self,
156                            const char * message,
157                            const mediacontrol_Position * begin,
158                            const mediacontrol_Position * end,
159                            mediacontrol_Exception *exception )
160 {
161     vout_thread_t *p_vout = NULL;
162     input_thread_t *p_input;
163     libvlc_exception_t ex;
164
165     libvlc_exception_init( &ex );
166     mediacontrol_exception_init( exception );
167
168     if( !message )
169     {
170         RAISE_VOID( mediacontrol_InternalException, "Empty text" );
171     }
172
173     p_input = libvlc_get_input_thread( self->p_media_player, &ex );
174     if( ! p_input )
175     {
176         RAISE_VOID( mediacontrol_InternalException, "No input" );
177     }
178     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
179     if( ! p_vout )
180     {
181         RAISE_VOID( mediacontrol_InternalException, "No video output" );
182     }
183
184     if( begin->origin == mediacontrol_RelativePosition &&
185         begin->value == 0 &&
186         end->origin == mediacontrol_RelativePosition )
187     {
188         mtime_t i_duration = 0;
189         mtime_t i_now = mdate();
190
191         i_duration = 1000 * private_mediacontrol_unit_convert(
192                                                               self->p_media_player,
193                                                               end->key,
194                                                               mediacontrol_MediaTime,
195                                                               end->value );
196
197         mediacontrol_showtext( p_vout, DEFAULT_CHAN, message, NULL,
198                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
199                                i_now, i_now + i_duration );
200     }
201     else
202     {
203         mtime_t i_debut, i_fin, i_now;
204
205         /* FIXME */
206         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
207         i_now = mdate();
208
209         i_debut = private_mediacontrol_position2microsecond( self->p_media_player,
210                                             ( mediacontrol_Position* ) begin );
211         i_debut += i_now;
212
213         i_fin = private_mediacontrol_position2microsecond( self->p_media_player,
214                                           ( mediacontrol_Position * ) end );
215         i_fin += i_now;
216
217         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, message, NULL,
218                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
219                                i_debut, i_fin );
220     }
221
222     vlc_object_release( p_vout );
223 }
224
225 unsigned short
226 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
227                                mediacontrol_Exception *exception )
228 {
229     libvlc_exception_t ex;
230     int i_ret = 0;
231
232     mediacontrol_exception_init( exception );
233     libvlc_exception_init( &ex );
234
235     i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
236     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
237     /* FIXME: Normalize in [0..100] */
238     return (unsigned short)i_ret;
239 }
240
241 void
242 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
243                                const unsigned short volume,
244                                mediacontrol_Exception *exception )
245 {
246     /* FIXME: Normalize in [0..100] */
247     libvlc_exception_t ex;
248
249     mediacontrol_exception_init( exception );
250     libvlc_exception_init( &ex );
251
252     libvlc_audio_set_volume( self->p_instance, volume, &ex );
253     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
254 }
255
256 int mediacontrol_set_visual( mediacontrol_Instance *self,
257                                     WINDOWHANDLE visual_id,
258                                     mediacontrol_Exception *exception )
259 {
260     libvlc_exception_t ex;
261
262     mediacontrol_exception_init( exception );
263     libvlc_exception_init( &ex );
264
265     libvlc_media_player_set_drawable( self->p_media_player, (libvlc_drawable_t)visual_id, &ex );
266     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
267     return true;
268 }
269
270 int
271 mediacontrol_get_rate( mediacontrol_Instance *self,
272                mediacontrol_Exception *exception )
273 {
274     libvlc_exception_t ex;
275     int i_ret;
276
277     mediacontrol_exception_init( exception );
278     libvlc_exception_init( &ex );
279
280     i_ret = libvlc_media_player_get_rate( self->p_media_player, &ex );
281     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
282
283     return i_ret / 10;
284 }
285
286 void
287 mediacontrol_set_rate( mediacontrol_Instance *self,
288                const int rate,
289                mediacontrol_Exception *exception )
290 {
291     libvlc_exception_t ex;
292
293     mediacontrol_exception_init( exception );
294     libvlc_exception_init( &ex );
295
296     libvlc_media_player_set_rate( self->p_media_player, rate * 10, &ex );
297     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
298 }
299
300 int
301 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
302                  mediacontrol_Exception *exception )
303 {
304     libvlc_exception_t ex;
305     int i_ret;
306
307     mediacontrol_exception_init( exception );
308     libvlc_exception_init( &ex );
309
310     i_ret = libvlc_get_fullscreen( self->p_media_player, &ex );
311     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
312
313     return i_ret;
314 }
315
316 void
317 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
318                  const int b_fullscreen,
319                  mediacontrol_Exception *exception )
320 {
321     libvlc_exception_t ex;
322
323     mediacontrol_exception_init( exception );
324     libvlc_exception_init( &ex );
325
326     libvlc_set_fullscreen( self->p_media_player, b_fullscreen, &ex );
327     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
328 }