1 /*****************************************************************************
2 * audio_video.c: Audio/Video management : volume, snapshot, OSD
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * $Id: vlc.c 10786 2005-04-23 23:19:17Z zorglub $
7 * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
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.
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.
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 *****************************************************************************/
24 #include <vlc/control.h>
29 #include <vlc_demux.h>
33 #define HAS_SNAPSHOT 1
39 #include <stdlib.h> /* malloc(), free() */
42 #include <errno.h> /* ENOMEM */
50 #ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
53 #include <sys/types.h>
55 #define RAISE( c, m ) exception->code = c; \
56 exception->message = strdup(m);
58 mediacontrol_RGBPicture *
59 mediacontrol_snapshot( mediacontrol_Instance *self,
60 const mediacontrol_Position * a_position,
61 mediacontrol_Exception *exception )
63 mediacontrol_RGBPicture *retval = NULL;
64 input_thread_t* p_input = self->p_playlist->p_input;
65 vout_thread_t *p_vout = NULL;
70 snapshot_t *p_best_snapshot;
76 exception=mediacontrol_exception_init( exception );
79 if( var_Get( self->p_vlc, "snapshot-id", &val ) == VLC_SUCCESS )
80 p_vout = vlc_object_get( self->p_vlc, val.i_int );
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 )
87 RAISE( mediacontrol_InternalException, "No snapshot-id in p_input" );
90 p_vout = vlc_object_get( self->p_vlc, val.i_int );
94 RAISE( mediacontrol_InternalException, "No snapshot module" );
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 )
104 RAISE( mediacontrol_InternalException, "No snapshot module" );
105 vlc_object_release( p_vout );
108 i_datasize = val.i_int;
110 /* Handle the a_position parameter */
111 if( ! ( a_position->origin == mediacontrol_RelativePosition
112 && a_position->value == 0 ) )
114 /* The position is not the current one. Go to it. */
115 mediacontrol_set_media_position( self,
116 ( mediacontrol_Position* ) a_position,
118 if( exception->code )
120 vlc_object_release( p_vout );
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
129 vlc_mutex_lock( &p_vout->picture_lock );
131 searched_date = mediacontrol_position2microsecond( p_input,
132 ( mediacontrol_Position * ) a_position );
134 var_Get( p_vout, "snapshot-cache-size", &val );
135 i_cachesize = val.i_int ;
137 var_Get( p_vout, "snapshot-list-pointer", &val );
138 pointer = ( snapshot_t ** )val.p_address;
142 RAISE( mediacontrol_InternalException, "No available snapshot" );
144 vlc_mutex_unlock( &p_vout->picture_lock );
145 vlc_object_release( p_vout );
149 /* Find the more appropriate picture, based on date */
150 p_best_snapshot = pointer[0];
152 for( i_index = 1 ; i_index < i_cachesize ; i_index++ )
154 long l_diff = pointer[i_index]->date - searched_date;
155 if( l_diff > 0 && l_diff < abs( p_best_snapshot->date - searched_date ))
157 /* This one is closer, and _after_ the requested position */
158 p_best_snapshot = pointer[i_index];
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,
171 vlc_mutex_unlock( &p_vout->picture_lock );
172 vlc_object_release( p_vout );
179 mediacontrol_RGBPicture **
180 mediacontrol_all_snapshots( mediacontrol_Instance *self,
181 mediacontrol_Exception *exception )
183 mediacontrol_RGBPicture **retval = NULL;
184 vout_thread_t *p_vout = NULL;
190 snapshot_t **pointer;
193 exception=mediacontrol_exception_init( exception );
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 );
200 RAISE( mediacontrol_InternalException, "No snapshot module" );
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 )
209 RAISE( mediacontrol_InternalException, "No snapshot module" );
210 vlc_object_release( p_vout );
213 i_datasize = val.i_int;
215 vlc_mutex_lock( &p_vout->picture_lock );
217 var_Get( p_vout, "snapshot-cache-size", &val );
218 i_cachesize = val.i_int ;
220 var_Get( p_vout, "snapshot-list-pointer", &val );
221 pointer = ( snapshot_t ** )val.p_address;
225 RAISE( mediacontrol_InternalException, "No available picture" );
227 vlc_mutex_unlock( &p_vout->picture_lock );
228 vlc_object_release( p_vout );
232 retval = ( mediacontrol_RGBPicture** )malloc( (i_cachesize + 1 ) * sizeof( char* ));
234 for( i_index = 0 ; i_index < i_cachesize ; i_index++ )
236 snapshot_t *p_s = pointer[i_index];
237 mediacontrol_RGBPicture *p_rgb;
239 p_rgb = _mediacontrol_createRGBPicture( p_s->i_width,
241 p_vout->output.i_chroma,
246 retval[i_index] = p_rgb;
249 retval[i_cachesize] = NULL;
251 vlc_mutex_unlock( &p_vout->picture_lock );
252 vlc_object_release( p_vout );
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 )
267 if( !psz_string ) return VLC_EGENERIC;
269 p_spu = spu_CreateSubpicture( p_vout->p_spu );
270 if( !p_spu ) return VLC_EGENERIC;
272 /* Create a new subpicture region */
273 memset( &fmt, 0, sizeof(video_format_t) );
274 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
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 )
281 msg_Err( p_vout, "cannot allocate SPU region" );
282 spu_DestroySubpicture( p_vout->p_spu, p_spu );
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;
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;
297 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
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 )
310 input_thread_t *p_input = NULL;
311 vout_thread_t *p_vout = NULL;
314 psz_message = strdup( message );
317 RAISE( mediacontrol_InternalException, "No more memory" );
321 p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
324 RAISE( mediacontrol_InternalException, "No video output" );
328 if( begin->origin == mediacontrol_RelativePosition &&
330 end->origin == mediacontrol_RelativePosition )
332 mtime_t i_duration = 0;
333 mtime_t i_now = mdate();
335 i_duration = 1000 * mediacontrol_unit_convert( self->p_playlist->p_input,
337 mediacontrol_MediaTime,
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 );
346 mtime_t i_debut, i_fin, i_now;
348 p_input = self->p_playlist->p_input;
351 RAISE( mediacontrol_InternalException, "No input" );
352 vlc_object_release( p_vout );
357 /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
360 i_debut = mediacontrol_position2microsecond( p_input,
361 ( mediacontrol_Position* ) begin );
364 i_fin = mediacontrol_position2microsecond( p_input,
365 ( mediacontrol_Position * ) end );
368 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
369 OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
373 vlc_object_release( p_vout );
377 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
378 mediacontrol_Exception *exception )
381 audio_volume_t i_volume;
385 RAISE( mediacontrol_InternalException, "No interface module" );
388 aout_VolumeGet( self->p_intf, &i_volume );
394 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
395 const unsigned short volume,
396 mediacontrol_Exception *exception )
400 RAISE( mediacontrol_InternalException, "No interface module" );
403 aout_VolumeSet( self->p_intf,( audio_volume_t )volume );
406 bool mediacontrol_set_visual(mediacontrol_Instance* self,
407 WINDOWHANDLE visual_id,
408 mediacontrol_Exception *exception)
415 RAISE( mediacontrol_InternalException, "No vlc reference" );
418 value.i_int=visual_id;
419 ret = var_Set(self->p_vlc, "drawable", value);
421 return (ret == VLC_SUCCESS);