mediacontrol: prefix unit conversion functions with private_
[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 #include "mediacontrol_internal.h"
25
26 #include <vlc/mediacontrol.h>
27 #include <vlc/libvlc.h>
28
29 #include <vlc_playlist.h>
30
31 #include <vlc_aout.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     vlc_object_t* p_cache;
59     vout_thread_t* p_vout;
60     mediacontrol_RGBPicture *p_pic = NULL;
61     char path[256];
62     snapshot_t *p_snapshot;
63
64     mediacontrol_exception_init( exception );
65
66     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
67     if( ! p_vout )
68     {
69         RAISE_NULL( mediacontrol_InternalException, "No video output" );
70     }
71     p_cache = vlc_object_create( self->p_playlist, VLC_OBJECT_GENERIC );
72     if( p_cache == NULL )
73     {
74         vlc_object_release( p_vout );
75         RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
76     }
77     snprintf( path, 255, "object:%d", p_cache->i_object_id );
78     var_SetString( p_vout, "snapshot-path", path );
79     var_SetString( p_vout, "snapshot-format", "png" );
80
81     vlc_mutex_lock( &p_cache->object_lock );
82     vout_Control( p_vout, VOUT_SNAPSHOT );
83     vlc_cond_wait( &p_cache->object_wait, &p_cache->object_lock );
84     vlc_object_release( p_vout );
85
86     p_snapshot = ( snapshot_t* ) p_cache->p_private;
87     vlc_object_destroy( p_cache );
88
89     if( p_snapshot )
90     {
91         p_pic = private_mediacontrol_createRGBPicture( p_snapshot->i_width,
92                                                        p_snapshot->i_height,
93                                                        VLC_FOURCC( 'p','n','g',' ' ),
94                                                        p_snapshot->date,
95                                                        p_snapshot->p_data,
96                                                        p_snapshot->i_datasize );
97         if( !p_pic )
98         {
99             free( p_snapshot->p_data );
100             free( p_snapshot );
101             RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
102         }
103     }
104     else
105     {
106         RAISE_NULL( mediacontrol_InternalException, "Snapshot exception" );
107     }
108     return p_pic;
109 }
110
111 mediacontrol_RGBPicture **
112 mediacontrol_all_snapshots( mediacontrol_Instance *self,
113                             mediacontrol_Exception *exception )
114 {
115     mediacontrol_exception_init( exception );
116
117     RAISE_NULL( mediacontrol_InternalException, "unsupported method" );
118 }
119
120 static
121 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
122                            char *psz_string, text_style_t *p_style,
123                            int i_flags, int i_hmargin, int i_vmargin,
124                            mtime_t i_start, mtime_t i_stop )
125 {
126     subpicture_t *p_spu;
127     video_format_t fmt;
128
129     if( !psz_string ) return VLC_EGENERIC;
130
131     p_spu = spu_CreateSubpicture( p_vout->p_spu );
132     if( !p_spu ) return VLC_EGENERIC;
133
134     /* Create a new subpicture region */
135     memset( &fmt, 0, sizeof(video_format_t) );
136     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
137     fmt.i_aspect = 0;
138     fmt.i_width = fmt.i_height = 0;
139     fmt.i_x_offset = fmt.i_y_offset = 0;
140     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
141     if( !p_spu->p_region )
142     {
143         msg_Err( p_vout, "cannot allocate SPU region" );
144         spu_DestroySubpicture( p_vout->p_spu, p_spu );
145         return VLC_EGENERIC;
146     }
147
148     p_spu->p_region->psz_text = strdup( psz_string );
149     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
150     p_spu->i_start = i_start;
151     p_spu->i_stop = i_stop;
152     p_spu->b_ephemer = VLC_FALSE;
153     p_spu->b_absolute = VLC_FALSE;
154
155     p_spu->i_x = i_hmargin;
156     p_spu->i_y = i_vmargin;
157     p_spu->i_flags = i_flags & ~SUBPICTURE_ALIGN_MASK;
158     p_spu->i_channel = i_channel;
159
160     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
161
162     return VLC_SUCCESS;
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     input_thread_t *p_input = NULL;
174     vout_thread_t *p_vout = NULL;
175     char* psz_message;
176
177     psz_message = strdup( message );
178     if( !psz_message )
179     {
180         RAISE_VOID( mediacontrol_InternalException, "no more memory" );
181     }
182
183     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
184     if( ! p_vout )
185     {
186         RAISE_VOID( mediacontrol_InternalException, "no video output" );
187     }
188
189     if( begin->origin == mediacontrol_RelativePosition &&
190         begin->value == 0 &&
191         end->origin == mediacontrol_RelativePosition )
192     {
193         mtime_t i_duration = 0;
194         mtime_t i_now = mdate();
195
196         i_duration = 1000 * private_mediacontrol_unit_convert(
197                                                 self->p_playlist->p_input,
198                                                 end->key,
199                                                 mediacontrol_MediaTime,
200                                                 end->value );
201
202         mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
203                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
204                                i_now, i_now + i_duration );
205     }
206     else
207     {
208         mtime_t i_debut, i_fin, i_now;
209
210         p_input = self->p_playlist->p_input;
211         if( ! p_input )
212         {
213             vlc_object_release( p_vout );
214             RAISE_VOID( mediacontrol_InternalException, "No input" );
215         }
216
217         /* FIXME */
218         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
219         i_now = mdate();
220
221         i_debut = private_mediacontrol_position2microsecond( p_input,
222                                             ( mediacontrol_Position* ) begin );
223         i_debut += i_now;
224
225         i_fin = private_mediacontrol_position2microsecond( p_input,
226                                           ( mediacontrol_Position * ) end );
227         i_fin += i_now;
228
229         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_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 vlc_bool_t 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_video_set_parent( self->p_instance, visual_id, &ex );
278     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
279     return VLC_TRUE;
280 }
281
282 int
283 mediacontrol_get_rate( mediacontrol_Instance *self,
284                        mediacontrol_Exception *exception )
285 {
286     libvlc_exception_t ex;
287     libvlc_media_instance_t* p_mi;
288     int i_ret;
289
290     mediacontrol_exception_init( exception );
291     libvlc_exception_init( &ex );
292
293     p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex );
294     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
295
296     i_ret = libvlc_media_instance_get_rate( p_mi, &ex );
297     libvlc_media_instance_release( p_mi );
298     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
299
300     return i_ret / 10;
301 }
302
303 void
304 mediacontrol_set_rate( mediacontrol_Instance *self,
305                        const int rate,
306                        mediacontrol_Exception *exception )
307 {
308     libvlc_exception_t ex;
309     libvlc_media_instance_t* p_mi;
310
311     mediacontrol_exception_init( exception );
312     libvlc_exception_init( &ex );
313
314     p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex );
315     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
316
317     libvlc_media_instance_set_rate( p_mi, rate * 10, &ex );
318     libvlc_media_instance_release( p_mi );
319     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
320 }
321
322 int
323 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
324                              mediacontrol_Exception *exception )
325 {
326     libvlc_exception_t ex;
327     libvlc_media_instance_t* p_mi;
328     int i_ret;
329
330     mediacontrol_exception_init( exception );
331     libvlc_exception_init( &ex );
332
333     p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex );
334     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
335
336     i_ret = libvlc_get_fullscreen( p_mi, &ex );
337     libvlc_media_instance_release( p_mi );
338     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
339
340     return i_ret;
341 }
342
343 void
344 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
345                              const int b_fullscreen,
346                              mediacontrol_Exception *exception )
347 {
348     libvlc_exception_t ex;
349     libvlc_media_instance_t* p_mi;
350
351     mediacontrol_exception_init( exception );
352     libvlc_exception_init( &ex );
353
354     p_mi = libvlc_playlist_get_media_instance( self->p_instance, &ex );
355     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
356
357     libvlc_set_fullscreen( p_mi, b_fullscreen, &ex );
358     libvlc_media_instance_release( p_mi );
359     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
360 }