]> git.sesse.net Git - vlc/blob - src/control/mediacontrol_audio_video.c
9afb08cf9f0105b96d1e833f23660ededb85a0c4
[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 #include "media_player_internal.h"
30
31 #include <vlc/mediacontrol.h>
32 #include <vlc/libvlc.h>
33
34 #include <vlc_vout.h>
35 #include <vlc_input.h>
36 #include <vlc_osd.h>
37 #include <vlc_block.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     (void)a_position;
62     vout_thread_t* p_vout;
63     input_thread_t *p_input;
64     mediacontrol_RGBPicture *p_pic;
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 = input_GetVout( p_input );
77     vlc_object_release( p_input );
78     if( ! p_vout )
79     {
80         RAISE_NULL( mediacontrol_InternalException, "No video output" );
81     }
82
83     block_t *p_image;
84     video_format_t fmt;
85
86     if( vout_GetSnapshot( p_vout, &p_image, NULL, &fmt, "png", 500*1000 ) )
87     {
88         vlc_object_release( p_vout );
89         RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
90         return NULL;
91     }
92
93     /* */
94     char *p_data = malloc( p_image->i_buffer );
95     if( p_data )
96     {
97         memcpy( p_data, p_image->p_buffer, p_image->i_buffer );
98         p_pic = private_mediacontrol_createRGBPicture( fmt.i_width,
99                                                        fmt.i_height,
100                                                        fmt.i_chroma,
101                                                        p_image->i_pts,
102                                                        p_data,
103                                                        p_image->i_buffer );
104     }
105     else
106     {
107         p_pic = NULL;
108     }
109     block_Release( p_image );
110
111     if( !p_pic )
112         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
113
114     vlc_object_release( p_vout );
115     return p_pic;
116 }
117
118 static
119 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
120                            const char *psz_string, text_style_t *p_style,
121                            int i_flags, int i_hmargin, int i_vmargin,
122                            mtime_t i_start, mtime_t i_stop )
123 {
124     return osd_ShowTextAbsolute( vout_GetSpu( p_vout ), i_channel,
125                                  psz_string, p_style,
126                                  i_flags, i_hmargin, i_vmargin,
127                                  i_start, i_stop );
128 }
129
130
131 void
132 mediacontrol_display_text( mediacontrol_Instance *self,
133                            const char * message,
134                            const mediacontrol_Position * begin,
135                            const mediacontrol_Position * end,
136                            mediacontrol_Exception *exception )
137 {
138     vout_thread_t *p_vout = NULL;
139     input_thread_t *p_input;
140     libvlc_exception_t ex;
141
142     libvlc_exception_init( &ex );
143     mediacontrol_exception_init( exception );
144
145     if( !message )
146     {
147         RAISE_VOID( mediacontrol_InternalException, "Empty text" );
148     }
149
150     p_input = libvlc_get_input_thread( self->p_media_player, &ex );
151     if( ! p_input )
152     {
153         RAISE_VOID( mediacontrol_InternalException, "No input" );
154     }
155     p_vout = input_GetVout( p_input );
156     /*FIXME: take care of the next fixme that can use p_input */
157     vlc_object_release( p_input );
158
159     if( ! p_vout )
160     {
161         RAISE_VOID( mediacontrol_InternalException, "No video output" );
162     }
163
164     if( begin->origin == mediacontrol_RelativePosition &&
165         begin->value == 0 &&
166         end->origin == mediacontrol_RelativePosition )
167     {
168         mtime_t i_duration = 0;
169         mtime_t i_now = mdate();
170
171         i_duration = 1000 * private_mediacontrol_unit_convert(
172                                                               self->p_media_player,
173                                                               end->key,
174                                                               mediacontrol_MediaTime,
175                                                               end->value );
176
177         mediacontrol_showtext( p_vout, DEFAULT_CHAN, message, NULL,
178                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
179                                i_now, i_now + i_duration );
180     }
181     else
182     {
183         mtime_t i_debut, i_fin, i_now;
184
185         /* FIXME */
186         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
187         i_now = mdate();
188
189         i_debut = private_mediacontrol_position2microsecond( self->p_media_player,
190                                             ( mediacontrol_Position* ) begin );
191         i_debut += i_now;
192
193         i_fin = private_mediacontrol_position2microsecond( self->p_media_player,
194                                           ( mediacontrol_Position * ) end );
195         i_fin += i_now;
196
197         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, message, NULL,
198                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
199                                i_debut, i_fin );
200     }
201
202     vlc_object_release( p_vout );
203 }
204
205 unsigned short
206 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
207                                mediacontrol_Exception *exception )
208 {
209     libvlc_exception_t ex;
210     int i_ret = 0;
211
212     mediacontrol_exception_init( exception );
213     libvlc_exception_init( &ex );
214
215     i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
216     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
217     /* FIXME: Normalize in [0..100] */
218     return (unsigned short)i_ret;
219 }
220
221 void
222 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
223                                const unsigned short volume,
224                                mediacontrol_Exception *exception )
225 {
226     /* FIXME: Normalize in [0..100] */
227     libvlc_exception_t ex;
228
229     mediacontrol_exception_init( exception );
230     libvlc_exception_init( &ex );
231
232     libvlc_audio_set_volume( self->p_instance, volume, &ex );
233     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
234 }
235
236 int mediacontrol_set_visual( mediacontrol_Instance *self,
237                                     WINDOWHANDLE visual_id,
238                                     mediacontrol_Exception *exception )
239 {
240     libvlc_exception_t ex;
241
242     mediacontrol_exception_init( exception );
243     libvlc_exception_init( &ex );
244 #ifdef WIN32
245     libvlc_media_player_set_hwnd( self->p_media_player, visual_id, &ex );
246 #else
247     libvlc_media_player_set_xwindow( self->p_media_player, visual_id, &ex );
248 #endif
249     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
250     return true;
251 }
252
253 int
254 mediacontrol_get_rate( mediacontrol_Instance *self,
255                mediacontrol_Exception *exception )
256 {
257     libvlc_exception_t ex;
258     int i_ret;
259
260     mediacontrol_exception_init( exception );
261     libvlc_exception_init( &ex );
262
263     i_ret = libvlc_media_player_get_rate( self->p_media_player, &ex );
264     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
265
266     return i_ret / 10;
267 }
268
269 void
270 mediacontrol_set_rate( mediacontrol_Instance *self,
271                const int rate,
272                mediacontrol_Exception *exception )
273 {
274     libvlc_exception_t ex;
275
276     mediacontrol_exception_init( exception );
277     libvlc_exception_init( &ex );
278
279     libvlc_media_player_set_rate( self->p_media_player, rate * 10, &ex );
280     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
281 }
282
283 int
284 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
285                  mediacontrol_Exception *exception )
286 {
287     libvlc_exception_t ex;
288     int i_ret;
289
290     mediacontrol_exception_init( exception );
291     libvlc_exception_init( &ex );
292
293     i_ret = libvlc_get_fullscreen( self->p_media_player, &ex );
294     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
295
296     return i_ret;
297 }
298
299 void
300 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
301                  const int b_fullscreen,
302                  mediacontrol_Exception *exception )
303 {
304     libvlc_exception_t ex;
305
306     mediacontrol_exception_init( exception );
307     libvlc_exception_init( &ex );
308
309     libvlc_set_fullscreen( self->p_media_player, b_fullscreen, &ex );
310     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
311 }