171561289ac5b3d0bb958ac07bdd0d7a1beee009
[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/intf.h>
30 #include <vlc/vout.h>
31 #include <vlc/aout.h>
32 #include <vlc_demux.h>
33
34 #include <vlc_osd.h>
35
36 #include <snapshot.h>
37
38 #include <stdlib.h>                                      /* malloc(), free() */
39 #include <string.h>
40
41 #include <errno.h>                                                 /* ENOMEM */
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 #define RAISE( c, m )  exception->code = c; \
56                        exception->message = strdup(m);
57
58 mediacontrol_RGBPicture *
59 mediacontrol_snapshot( mediacontrol_Instance *self,
60                        const mediacontrol_Position * a_position,
61                        mediacontrol_Exception *exception )
62 {
63     vlc_object_t* p_cache;
64     vout_thread_t* p_vout;
65     mediacontrol_RGBPicture *p_pic = NULL;
66     char path[256];
67     snapshot_t *p_snapshot;
68
69     exception=mediacontrol_exception_init( exception );
70
71     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
72     if( ! p_vout )
73     {
74         RAISE( mediacontrol_InternalException, "No video output" );
75         return NULL;
76     }
77     p_cache = vlc_object_create( self->p_playlist, VLC_OBJECT_GENERIC );
78     if( p_cache == NULL )
79     {
80         vlc_object_release( p_vout );
81         msg_Err( self->p_playlist, "out of memory" );
82         RAISE( mediacontrol_InternalException, "Out of memory" );
83         return NULL;
84     }
85     snprintf( path, 255, "object:%d", p_cache->i_object_id );
86     var_SetString( p_vout, "snapshot-path", path );
87     var_SetString( p_vout, "snapshot-format", "png" );
88
89     vlc_mutex_lock( &p_cache->object_lock );
90     vout_Control( p_vout, VOUT_SNAPSHOT );
91     vlc_cond_wait( &p_cache->object_wait, &p_cache->object_lock );
92     vlc_object_release( p_vout );
93
94     p_snapshot = ( snapshot_t* ) p_cache->p_private;
95     vlc_object_destroy( p_cache );
96
97     if( p_snapshot )
98     {
99         p_pic = _mediacontrol_createRGBPicture( p_snapshot->i_width,
100                                                 p_snapshot->i_height,
101                                                 VLC_FOURCC( 'p','n','g',' ' ),
102                                                 p_snapshot->date,
103                                                 p_snapshot->p_data,
104                                                 p_snapshot->i_datasize );
105         if( !p_pic )
106             RAISE( mediacontrol_InternalException, "out of memory" );
107         free( p_snapshot->p_data );
108         free( p_snapshot );
109     }
110     else
111     {
112         RAISE( mediacontrol_InternalException, "Snapshot exception" );
113     }
114     return p_pic;
115 }
116
117 mediacontrol_RGBPicture **
118 mediacontrol_all_snapshots( mediacontrol_Instance *self,
119                             mediacontrol_Exception *exception )
120 {
121     exception=mediacontrol_exception_init( exception );
122
123     RAISE( mediacontrol_InternalException, "unsupported method" );
124     return NULL;
125 }
126
127 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
128                            char *psz_string, text_style_t *p_style,
129                            int i_flags, int i_hmargin, int i_vmargin,
130                            mtime_t i_start, mtime_t i_stop )
131 {
132     subpicture_t *p_spu;
133     video_format_t fmt;
134
135     if( !psz_string ) return VLC_EGENERIC;
136
137     p_spu = spu_CreateSubpicture( p_vout->p_spu );
138     if( !p_spu ) return VLC_EGENERIC;
139
140     /* Create a new subpicture region */
141     memset( &fmt, 0, sizeof(video_format_t) );
142     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
143     fmt.i_aspect = 0;
144     fmt.i_width = fmt.i_height = 0;
145     fmt.i_x_offset = fmt.i_y_offset = 0;
146     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
147     if( !p_spu->p_region )
148     {
149         msg_Err( p_vout, "cannot allocate SPU region" );
150         spu_DestroySubpicture( p_vout->p_spu, p_spu );
151         return VLC_EGENERIC;
152     }
153
154     p_spu->p_region->psz_text = strdup( psz_string );
155     p_spu->i_start = i_start;
156     p_spu->i_stop = i_stop;
157     p_spu->b_ephemer = VLC_FALSE;
158     p_spu->b_absolute = VLC_FALSE;
159
160     p_spu->i_x = i_hmargin;
161     p_spu->i_y = i_vmargin;
162     p_spu->i_flags = i_flags;
163     p_spu->i_channel = i_channel;
164
165     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
166
167     return VLC_SUCCESS;
168 }
169
170
171 void
172 mediacontrol_display_text( mediacontrol_Instance *self,
173                            const char * message,
174                            const mediacontrol_Position * begin,
175                            const mediacontrol_Position * end,
176                            mediacontrol_Exception *exception )
177 {
178     input_thread_t *p_input = NULL;
179     vout_thread_t *p_vout = NULL;
180     char* psz_message;
181
182     psz_message = strdup( message );
183     if( !psz_message )
184     {
185         RAISE( mediacontrol_InternalException, "no more memory" );
186         return;
187     }
188
189     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
190     if( ! p_vout )
191     {
192         RAISE( mediacontrol_InternalException, "no video output" );
193         return;
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 * mediacontrol_unit_convert(
204                                                 self->p_playlist->p_input,
205                                                 end->key,
206                                                 mediacontrol_MediaTime,
207                                                 end->value );
208
209         mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_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         p_input = self->p_playlist->p_input;
218         if( ! p_input )
219         {
220             RAISE( mediacontrol_InternalException, "No input" );
221             vlc_object_release( p_vout );
222             return;
223         }
224
225         /* FIXME */
226         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
227         i_now = mdate();
228
229         i_debut = mediacontrol_position2microsecond( p_input,
230                                             ( mediacontrol_Position* ) begin );
231         i_debut += i_now;
232
233         i_fin = mediacontrol_position2microsecond( p_input,
234                                           ( mediacontrol_Position * ) end );
235         i_fin += i_now;
236
237         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
238                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
239                                i_debut, i_fin );
240     }
241
242     vlc_object_release( p_vout );
243 }
244
245 unsigned short
246 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
247                                mediacontrol_Exception *exception )
248 {
249     libvlc_exception_t ex;
250     int i_ret = 0;
251
252     mediacontrol_exception_init( exception );
253     libvlc_exception_init( &ex );
254
255     i_ret = libvlc_audio_get_volume( self->p_instance, &ex );
256     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
257     /* FIXME: Normalize in [0..100] */
258     return (unsigned short)i_ret;
259 }
260
261 void
262 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
263                                const unsigned short volume,
264                                mediacontrol_Exception *exception )
265 {
266     /* FIXME: Normalize in [0..100] */
267     libvlc_exception_t ex;
268
269     mediacontrol_exception_init( exception );
270     libvlc_exception_init( &ex );
271
272     libvlc_audio_set_volume( self->p_instance, volume, &ex );
273     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
274 }
275
276 vlc_bool_t mediacontrol_set_visual( mediacontrol_Instance *self,
277                                     WINDOWHANDLE visual_id,
278                                     mediacontrol_Exception *exception )
279 {
280     libvlc_exception_t ex;
281
282     mediacontrol_exception_init( exception );
283     libvlc_exception_init( &ex );
284
285     libvlc_video_set_parent( self->p_instance, visual_id, &ex );
286     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
287     return VLC_TRUE;
288 }
289
290 int
291 mediacontrol_get_rate( mediacontrol_Instance *self,
292                        mediacontrol_Exception *exception )
293 {
294     libvlc_exception_t ex;
295     libvlc_input_t* p_input;
296     int i_ret;
297
298     mediacontrol_exception_init( exception );
299     libvlc_exception_init( &ex );
300
301     p_input = libvlc_playlist_get_input( self->p_instance, &ex );
302     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
303
304     i_ret = libvlc_input_get_rate( p_input, &ex );
305     libvlc_input_free( p_input );
306     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
307
308     return i_ret / 10;
309 }
310
311 void
312 mediacontrol_set_rate( mediacontrol_Instance *self,
313                        const int rate,
314                        mediacontrol_Exception *exception )
315 {
316     libvlc_exception_t ex;
317     libvlc_input_t* p_input;
318
319     mediacontrol_exception_init( exception );
320     libvlc_exception_init( &ex );
321
322     p_input = libvlc_playlist_get_input( self->p_instance, &ex );
323     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
324
325     libvlc_input_set_rate( p_input, rate * 10, &ex );
326     libvlc_input_free( p_input );
327     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
328 }
329
330 int
331 mediacontrol_get_fullscreen( mediacontrol_Instance *self,
332                              mediacontrol_Exception *exception )
333 {
334     libvlc_exception_t ex;
335     libvlc_input_t* p_input;
336     int i_ret;
337
338     mediacontrol_exception_init( exception );
339     libvlc_exception_init( &ex );
340
341     p_input = libvlc_playlist_get_input( self->p_instance, &ex );
342     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
343
344     i_ret = libvlc_get_fullscreen( p_input, &ex );
345     libvlc_input_free( p_input );
346     HANDLE_LIBVLC_EXCEPTION_ZERO( &ex );
347
348     return i_ret;
349 }
350
351 void
352 mediacontrol_set_fullscreen( mediacontrol_Instance *self,
353                              const int b_fullscreen,
354                              mediacontrol_Exception *exception )
355 {
356     libvlc_exception_t ex;
357     libvlc_input_t* p_input;
358
359     mediacontrol_exception_init( exception );
360     libvlc_exception_init( &ex );
361
362     p_input = libvlc_playlist_get_input( self->p_instance, &ex );
363     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
364
365     libvlc_set_fullscreen( p_input, b_fullscreen, &ex );
366     libvlc_input_free( p_input );
367     HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
368 }