1 /*****************************************************************************
2 * audio_video.c: Audio/Video management : volume, snapshot, OSD
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
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 <mediacontrol_internal.h>
26 #include <vlc/mediacontrol.h>
31 #include <vlc_demux.h>
35 #define HAS_SNAPSHOT 1
41 #include <stdlib.h> /* malloc(), free() */
44 #include <errno.h> /* ENOMEM */
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
54 #ifdef HAVE_SYS_TYPES_H
55 # include <sys/types.h>
58 #define RAISE( c, m ) exception->code = c; \
59 exception->message = strdup(m);
61 mediacontrol_RGBPicture *
62 mediacontrol_snapshot( mediacontrol_Instance *self,
63 const mediacontrol_Position * a_position,
64 mediacontrol_Exception *exception )
66 mediacontrol_RGBPicture *retval = NULL;
67 input_thread_t* p_input = self->p_playlist->p_input;
68 vout_thread_t *p_vout = NULL;
73 snapshot_t *p_best_snapshot;
79 exception=mediacontrol_exception_init( exception );
82 if( var_Get( self->p_vlc, "snapshot-id", &val ) == VLC_SUCCESS )
83 p_vout = vlc_object_get( self->p_vlc, val.i_int );
86 /* FIXME: if in p_libvlc, we cannot have multiple video outputs */
87 /* Once corrected, search for snapshot-id to modify all instances */
88 if( var_Get( p_input, "snapshot-id", &val ) != VLC_SUCCESS )
90 RAISE( mediacontrol_InternalException, "No snapshot-id in p_input" );
93 p_vout = vlc_object_get( self->p_vlc, val.i_int );
97 RAISE( mediacontrol_InternalException, "No snapshot module" );
102 /* We test if the vout is a snapshot module. We cannot test
103 pvout_psz_object_name( which is NULL ). But we can check if
104 there are snapshot-specific variables */
105 if( var_Get( p_vout, "snapshot-datasize", &val ) != VLC_SUCCESS )
107 RAISE( mediacontrol_InternalException, "No snapshot module" );
108 vlc_object_release( p_vout );
111 i_datasize = val.i_int;
113 /* Handle the a_position parameter */
114 if( ! ( a_position->origin == mediacontrol_RelativePosition
115 && a_position->value == 0 ) )
117 /* The position is not the current one. Go to it. */
118 mediacontrol_set_media_position( self,
119 ( mediacontrol_Position* ) a_position,
121 if( exception->code )
123 vlc_object_release( p_vout );
128 /* FIXME: We should not go further until we got past the position
129 ( which means that we had the possibility to capture the right
132 vlc_mutex_lock( &p_vout->picture_lock );
134 searched_date = mediacontrol_position2microsecond( p_input,
135 ( mediacontrol_Position * ) a_position );
137 var_Get( p_vout, "snapshot-cache-size", &val );
138 i_cachesize = val.i_int ;
140 var_Get( p_vout, "snapshot-list-pointer", &val );
141 pointer = ( snapshot_t ** )val.p_address;
145 RAISE( mediacontrol_InternalException, "No available snapshot" );
147 vlc_mutex_unlock( &p_vout->picture_lock );
148 vlc_object_release( p_vout );
152 /* Find the more appropriate picture, based on date */
153 p_best_snapshot = pointer[0];
155 for( i_index = 1 ; i_index < i_cachesize ; i_index++ )
157 long l_diff = pointer[i_index]->date - searched_date;
158 if( l_diff > 0 && l_diff < abs( p_best_snapshot->date - searched_date ))
160 /* This one is closer, and _after_ the requested position */
161 p_best_snapshot = pointer[i_index];
165 /* FIXME: add a test for the case that no picture matched the test
166 ( we have p_best_snapshot == pointer[0] */
167 retval = _mediacontrol_createRGBPicture( p_best_snapshot->i_width,
168 p_best_snapshot->i_height,
169 p_vout->output.i_chroma,
170 p_best_snapshot->date,
171 p_best_snapshot->p_data,
174 vlc_mutex_unlock( &p_vout->picture_lock );
175 vlc_object_release( p_vout );
182 mediacontrol_RGBPicture **
183 mediacontrol_all_snapshots( mediacontrol_Instance *self,
184 mediacontrol_Exception *exception )
186 mediacontrol_RGBPicture **retval = NULL;
187 vout_thread_t *p_vout = NULL;
193 snapshot_t **pointer;
196 exception=mediacontrol_exception_init( exception );
198 if( var_Get( self->p_playlist->p_input, "snapshot-id", &val ) == VLC_SUCCESS )
199 p_vout = vlc_object_get( self->p_vlc, val.i_int );
203 RAISE( mediacontrol_InternalException, "No snapshot module" );
207 /* We test if the vout is a snapshot module. We cannot test
208 pvout_psz_object_name( which is NULL ). But we can check if
209 there are snapshot-specific variables */
210 if( var_Get( p_vout, "snapshot-datasize", &val ) != VLC_SUCCESS )
212 RAISE( mediacontrol_InternalException, "No snapshot module" );
213 vlc_object_release( p_vout );
216 i_datasize = val.i_int;
218 vlc_mutex_lock( &p_vout->picture_lock );
220 var_Get( p_vout, "snapshot-cache-size", &val );
221 i_cachesize = val.i_int ;
223 var_Get( p_vout, "snapshot-list-pointer", &val );
224 pointer = ( snapshot_t ** )val.p_address;
228 RAISE( mediacontrol_InternalException, "No available picture" );
230 vlc_mutex_unlock( &p_vout->picture_lock );
231 vlc_object_release( p_vout );
235 retval = ( mediacontrol_RGBPicture** )malloc( (i_cachesize + 1 ) * sizeof( char* ));
237 for( i_index = 0 ; i_index < i_cachesize ; i_index++ )
239 snapshot_t *p_s = pointer[i_index];
240 mediacontrol_RGBPicture *p_rgb;
242 p_rgb = _mediacontrol_createRGBPicture( p_s->i_width,
244 p_vout->output.i_chroma,
249 retval[i_index] = p_rgb;
252 retval[i_cachesize] = NULL;
254 vlc_mutex_unlock( &p_vout->picture_lock );
255 vlc_object_release( p_vout );
262 int mediacontrol_showtext( vout_thread_t *p_vout, int i_channel,
263 char *psz_string, text_style_t *p_style,
264 int i_flags, int i_hmargin, int i_vmargin,
265 mtime_t i_start, mtime_t i_stop )
270 if( !psz_string ) return VLC_EGENERIC;
272 p_spu = spu_CreateSubpicture( p_vout->p_spu );
273 if( !p_spu ) return VLC_EGENERIC;
275 /* Create a new subpicture region */
276 memset( &fmt, 0, sizeof(video_format_t) );
277 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
279 fmt.i_width = fmt.i_height = 0;
280 fmt.i_x_offset = fmt.i_y_offset = 0;
281 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_vout), &fmt );
282 if( !p_spu->p_region )
284 msg_Err( p_vout, "cannot allocate SPU region" );
285 spu_DestroySubpicture( p_vout->p_spu, p_spu );
289 p_spu->p_region->psz_text = strdup( psz_string );
290 p_spu->i_start = i_start;
291 p_spu->i_stop = i_stop;
292 p_spu->b_ephemer = VLC_FALSE;
293 p_spu->b_absolute = VLC_FALSE;
295 p_spu->i_x = i_hmargin;
296 p_spu->i_y = i_vmargin;
297 p_spu->i_flags = i_flags;
298 p_spu->i_channel = i_channel;
300 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
307 mediacontrol_display_text( mediacontrol_Instance *self,
308 const char * message,
309 const mediacontrol_Position * begin,
310 const mediacontrol_Position * end,
311 mediacontrol_Exception *exception )
313 input_thread_t *p_input = NULL;
314 vout_thread_t *p_vout = NULL;
317 psz_message = strdup( message );
320 RAISE( mediacontrol_InternalException, "No more memory" );
324 p_vout = vlc_object_find( self->p_playlist, VLC_OBJECT_VOUT, FIND_CHILD );
327 RAISE( mediacontrol_InternalException, "No video output" );
331 if( begin->origin == mediacontrol_RelativePosition &&
333 end->origin == mediacontrol_RelativePosition )
335 mtime_t i_duration = 0;
336 mtime_t i_now = mdate();
338 i_duration = 1000 * mediacontrol_unit_convert( self->p_playlist->p_input,
340 mediacontrol_MediaTime,
343 mediacontrol_showtext( p_vout, DEFAULT_CHAN, psz_message, NULL,
344 OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
345 i_now, i_now + i_duration );
349 mtime_t i_debut, i_fin, i_now;
351 p_input = self->p_playlist->p_input;
354 RAISE( mediacontrol_InternalException, "No input" );
355 vlc_object_release( p_vout );
360 /* i_now = input_ClockGetTS( p_input, NULL, 0 ); */
363 i_debut = mediacontrol_position2microsecond( p_input,
364 ( mediacontrol_Position* ) begin );
367 i_fin = mediacontrol_position2microsecond( p_input,
368 ( mediacontrol_Position * ) end );
371 vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN, psz_message, NULL,
372 OSD_ALIGN_BOTTOM | OSD_ALIGN_LEFT, 0, 0,
376 vlc_object_release( p_vout );
380 mediacontrol_sound_get_volume( mediacontrol_Instance *self,
381 mediacontrol_Exception *exception )
384 audio_volume_t i_volume;
388 RAISE( mediacontrol_InternalException, "No interface module" );
391 aout_VolumeGet( self->p_intf, &i_volume );
397 mediacontrol_sound_set_volume( mediacontrol_Instance *self,
398 const unsigned short volume,
399 mediacontrol_Exception *exception )
403 RAISE( mediacontrol_InternalException, "No interface module" );
406 aout_VolumeSet( self->p_intf,( audio_volume_t )volume );
409 vlc_bool_t mediacontrol_set_visual( mediacontrol_Instance *self,
410 WINDOWHANDLE visual_id,
411 mediacontrol_Exception *exception )
418 RAISE( mediacontrol_InternalException, "No vlc reference" );
421 value.i_int=visual_id;
422 ret = var_Set(self->p_vlc, "drawable", value);
424 return (ret == VLC_SUCCESS);