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