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