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