]> git.sesse.net Git - vlc/blob - src/control/audio_video.c
2b097d96cabf544759cb8500cfe0919d6c3646b8
[vlc] / src / control / audio_video.c
1 /*****************************************************************************
2  * audio_video.c: Audio/Video management : volume, snapshot, OSD
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id: vlc.c 10786 2005-04-23 23:19:17Z zorglub $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <vlc/control.h>
25
26 #include <vlc/intf.h>
27 #include <vlc/vout.h>
28 #include <vlc/aout.h>
29 #include <vlc_demux.h>
30
31 #include <osd.h>
32
33 #define HAS_SNAPSHOT 1
34
35 #ifdef HAS_SNAPSHOT
36 #include <snapshot.h>
37 #endif
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
50 #ifdef HAVE_SYS_TIME_H
51 #    include <sys/time.h>
52 #endif
53 #include <sys/types.h>
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     mediacontrol_RGBPicture *retval = NULL;
64     input_thread_t* p_input = self->p_playlist->p_input;
65     vout_thread_t *p_vout = NULL;
66     int i_datasize;
67     snapshot_t **pointer;
68     vlc_value_t val;
69     int i_index;
70     snapshot_t *p_best_snapshot;
71     long searched_date;
72 #ifdef HAS_SNAPSHOT
73     int i_cachesize;
74 #endif
75
76     exception=mediacontrol_exception_init( exception );
77
78     /*
79        if( var_Get( self->p_vlc, "snapshot-id", &val ) == VLC_SUCCESS )
80        p_vout = vlc_object_get( self->p_vlc, val.i_int );
81     */
82
83     /* FIXME: if in p_libvlc, we cannot have multiple video outputs */
84     /* Once corrected, search for snapshot-id to modify all instances */
85     if( var_Get( p_input, "snapshot-id", &val ) != VLC_SUCCESS )
86     {
87         RAISE( mediacontrol_InternalException, "No snapshot-id in p_input" );
88         return NULL;
89     }
90     p_vout = vlc_object_get( self->p_vlc, val.i_int );
91
92     if( ! p_vout )
93     {
94         RAISE( mediacontrol_InternalException, "No snapshot module" );
95         return NULL;
96     }
97
98 #ifdef HAS_SNAPSHOT
99     /* We test if the vout is a snapshot module. We cannot test
100        pvout_psz_object_name( which is NULL ). But we can check if
101        there are snapshot-specific variables */
102     if( var_Get( p_vout, "snapshot-datasize", &val ) != VLC_SUCCESS )
103     {
104         RAISE( mediacontrol_InternalException, "No snapshot module" );
105         vlc_object_release( p_vout );
106         return NULL;
107     }
108     i_datasize = val.i_int;
109
110     /* Handle the a_position parameter */
111     if( ! ( a_position->origin == mediacontrol_RelativePosition
112             && a_position->value == 0 ) )
113     {
114         /* The position is not the current one. Go to it. */
115         mediacontrol_set_media_position( self,
116                                          ( mediacontrol_Position* ) a_position,
117                                          exception );
118         if( exception->code )
119         {
120             vlc_object_release( p_vout );
121             return NULL;
122         }
123     }
124
125     /* FIXME: We should not go further until we got past the position
126        ( which means that we had the possibility to capture the right
127        picture ). */
128
129     vlc_mutex_lock( &p_vout->picture_lock );
130
131     searched_date = mediacontrol_position2microsecond( p_input,
132                                                        ( mediacontrol_Position * ) a_position );
133
134     var_Get( p_vout, "snapshot-cache-size", &val );
135     i_cachesize = val.i_int  ;
136
137     var_Get( p_vout, "snapshot-list-pointer", &val );
138     pointer = ( snapshot_t ** )val.p_address;
139
140     if( ! pointer )
141     {
142         RAISE( mediacontrol_InternalException, "No available snapshot" );
143
144         vlc_mutex_unlock( &p_vout->picture_lock );
145         vlc_object_release( p_vout );
146         return NULL;
147     }
148
149     /* Find the more appropriate picture, based on date */
150     p_best_snapshot = pointer[0];
151
152     for( i_index = 1 ; i_index < i_cachesize ; i_index++ )
153     {
154         long l_diff = pointer[i_index]->date - searched_date;
155         if( l_diff > 0 && l_diff < abs( p_best_snapshot->date - searched_date ))
156         {
157             /* This one is closer, and _after_ the requested position */
158             p_best_snapshot = pointer[i_index];
159         }
160     }
161
162     /* FIXME: add a test for the case that no picture matched the test
163        ( we have p_best_snapshot == pointer[0] */
164     retval = _mediacontrol_createRGBPicture( p_best_snapshot->i_width,
165                                              p_best_snapshot->i_height,
166                                              p_vout->output.i_chroma,
167                                              p_best_snapshot->date,
168                                              p_best_snapshot->p_data,
169                                              i_datasize );
170
171     vlc_mutex_unlock( &p_vout->picture_lock );
172     vlc_object_release( p_vout );
173
174 #endif
175
176     return retval;
177 }
178
179 mediacontrol_RGBPicture **
180 mediacontrol_all_snapshots( mediacontrol_Instance *self,
181                             mediacontrol_Exception *exception )
182 {
183     mediacontrol_RGBPicture **retval = NULL;
184     vout_thread_t *p_vout = NULL;
185     int i_datasize;
186     int i_cachesize;
187     vlc_value_t val;
188     int i_index;
189 #ifdef HAS_SNAPSHOT
190     snapshot_t **pointer;
191 #endif
192
193     exception=mediacontrol_exception_init( exception );
194
195     if( var_Get( self->p_playlist->p_input, "snapshot-id", &val ) == VLC_SUCCESS )
196         p_vout = vlc_object_get( self->p_vlc, val.i_int );
197
198     if( ! p_vout )
199     {
200         RAISE( mediacontrol_InternalException, "No snapshot module" );
201         return NULL;
202     }
203 #ifdef HAS_SNAPSHOT
204     /* We test if the vout is a snapshot module. We cannot test
205        pvout_psz_object_name( which is NULL ). But we can check if
206        there are snapshot-specific variables */
207     if( var_Get( p_vout, "snapshot-datasize", &val ) != VLC_SUCCESS )
208     {
209         RAISE( mediacontrol_InternalException, "No snapshot module" );
210         vlc_object_release( p_vout );
211         return NULL;
212     }
213     i_datasize = val.i_int;
214
215     vlc_mutex_lock( &p_vout->picture_lock );
216
217     var_Get( p_vout, "snapshot-cache-size", &val );
218     i_cachesize = val.i_int  ;
219
220     var_Get( p_vout, "snapshot-list-pointer", &val );
221     pointer = ( snapshot_t ** )val.p_address;
222
223     if( ! pointer )
224     {
225         RAISE( mediacontrol_InternalException, "No available picture" );
226
227         vlc_mutex_unlock( &p_vout->picture_lock );
228         vlc_object_release( p_vout );
229         return NULL;
230     }
231
232     retval = ( mediacontrol_RGBPicture** )malloc( (i_cachesize + 1 ) * sizeof( char* ));
233
234     for( i_index = 0 ; i_index < i_cachesize ; i_index++ )
235     {
236         snapshot_t *p_s = pointer[i_index];
237         mediacontrol_RGBPicture *p_rgb;
238
239         p_rgb = _mediacontrol_createRGBPicture( p_s->i_width,
240                                                 p_s->i_height,
241                                                 p_vout->output.i_chroma,
242                                                 p_s->date,
243                                                 p_s->p_data,
244                                                 i_datasize );
245
246         retval[i_index] = p_rgb;
247     }
248
249     retval[i_cachesize] = NULL;
250
251     vlc_mutex_unlock( &p_vout->picture_lock );
252     vlc_object_release( p_vout );
253
254 #endif
255
256     return retval;
257 }
258
259 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
260                            char *psz_string, text_style_t *p_style,
261                            int i_flags, int i_hmargin, int i_vmargin,
262                            mtime_t i_start, mtime_t i_stop )
263 {
264     subpicture_t *p_spu;
265     video_format_t fmt;
266
267     if( !psz_string ) return VLC_EGENERIC;
268
269     p_spu = spu_CreateSubpicture( p_vout->p_spu );
270     if( !p_spu ) return VLC_EGENERIC;
271
272     /* Create a new subpicture region */
273     memset( &fmt, 0, sizeof(video_format_t) );
274     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
275     fmt.i_aspect = 0;
276     fmt.i_width = fmt.i_height = 0;
277     fmt.i_x_offset = fmt.i_y_offset = 0;
278     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
279     if( !p_spu->p_region )
280     {
281         msg_Err( p_vout, "cannot allocate SPU region" );
282         spu_DestroySubpicture( p_vout->p_spu, p_spu );
283         return VLC_EGENERIC;
284     }
285
286     p_spu->p_region->psz_text = strdup( psz_string );
287     p_spu->i_start = i_start;
288     p_spu->i_stop = i_stop;
289     p_spu->b_ephemer = VLC_FALSE;
290     p_spu->b_absolute = VLC_FALSE;
291
292     p_spu->i_x = i_hmargin;
293     p_spu->i_y = i_vmargin;
294     p_spu->i_flags = i_flags;
295     p_spu->i_channel = i_channel;
296
297     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
298
299     return VLC_SUCCESS;
300 }
301
302
303 void
304 mediacontrol_display_text( mediacontrol_Instance *self,
305                            const char * message,
306                            const mediacontrol_Position * begin,
307                            const mediacontrol_Position * end,
308                            mediacontrol_Exception *exception )
309 {
310     input_thread_t *p_input = NULL;
311     vout_thread_t *p_vout = NULL;
312     char* psz_message;
313
314     psz_message = strdup( message );
315     if( !psz_message )
316     {
317         RAISE( mediacontrol_InternalException, "No more memory" );
318         return;
319     }
320
321     p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
322     if( ! p_vout )
323     {
324         RAISE( mediacontrol_InternalException, "No video output" );
325         return;
326     }
327
328     if( begin->origin == mediacontrol_RelativePosition &&
329         begin->value == 0 &&
330         end->origin == mediacontrol_RelativePosition )
331     {
332         mtime_t i_duration = 0;
333         mtime_t i_now = mdate();
334
335         i_duration = 1000 * mediacontrol_unit_convert( self->p_playlist->p_input,
336                                                        end->key,
337                                                        mediacontrol_MediaTime,
338                                                        end->value );
339
340         mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
341                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
342                                i_now, i_now + i_duration );
343     }
344     else
345     {
346         mtime_t i_debut, i_fin, i_now;
347
348         p_input = self->p_playlist->p_input;
349         if( ! p_input )
350         {
351             RAISE( mediacontrol_InternalException, "No input" );
352             vlc_object_release( p_vout );
353             return;
354         }
355
356         /* FIXME */
357         /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
358         i_now = mdate();
359         
360         i_debut = mediacontrol_position2microsecond( p_input,
361                                                      ( mediacontrol_Position* ) begin );
362         i_debut += i_now;
363
364         i_fin = mediacontrol_position2microsecond( p_input,
365                                                    ( mediacontrol_Position * ) end );
366         i_fin += i_now;
367
368         vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
369                                OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
370                                i_debut, i_fin );
371     }
372
373     vlc_object_release( p_vout );
374 }
375
376 unsigned short
377 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
378                                mediacontrol_Exception *exception )
379 {
380     short retval;
381     audio_volume_t i_volume;
382
383     if( !self->p_intf )
384     {
385         RAISE( mediacontrol_InternalException, "No interface module" );
386         return 0;
387     }
388     aout_VolumeGet( self->p_intf, &i_volume );
389     retval = i_volume;
390     return retval;
391 }
392
393 void
394 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
395                                const unsigned short volume,
396                                mediacontrol_Exception *exception )
397 {
398     if( !self->p_intf )
399     {
400         RAISE( mediacontrol_InternalException, "No interface module" );
401         return;
402     }
403     aout_VolumeSet( self->p_intf,( audio_volume_t )volume );
404 }
405
406 bool mediacontrol_set_visual(mediacontrol_Instance* self,
407                              WINDOWHANDLE visual_id,
408                              mediacontrol_Exception *exception)
409 {
410     vlc_value_t value;
411     int ret;
412
413     if( !self->p_vlc )
414     {
415         RAISE( mediacontrol_InternalException, "No vlc reference" );
416         return 0;
417     }
418     value.i_int=visual_id;
419     ret = var_Set(self->p_vlc, "drawable", value);
420     
421     return (ret == VLC_SUCCESS);
422 }