1 /*****************************************************************************
2 * vout_intf.c : video output interface
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
34 #include <stdlib.h> /* free() */
35 #include <sys/types.h> /* opendir() */
37 #include <dirent.h> /* opendir() */
39 #include <time.h> /* strftime */
41 #include <vlc_interface.h>
42 #include <vlc_block.h>
43 #include <vlc_playlist.h>
46 #include <vlc_window.h>
47 #include <vlc_image.h>
49 #include <vlc_charset.h>
51 #include <vlc_strings.h>
52 #include <vlc_charset.h>
53 #include "../libvlc.h"
54 #include "vout_internal.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
61 /* Object variables callbacks */
62 static int ZoomCallback( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
64 static int CropCallback( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
66 static int AspectCallback( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
68 static int ScalingCallback( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
70 static int OnTopCallback( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
72 static int FullscreenCallback( vlc_object_t *, char const *,
73 vlc_value_t, vlc_value_t, void * );
74 static int SnapshotCallback( vlc_object_t *, char const *,
75 vlc_value_t, vlc_value_t, void * );
77 static int TitleShowCallback( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
79 static int TitleTimeoutCallback( vlc_object_t *, char const *,
80 vlc_value_t, vlc_value_t, void * );
81 static int TitlePositionCallback( vlc_object_t *, char const *,
82 vlc_value_t, vlc_value_t, void * );
85 * Creates a video output window.
86 * On Unix systems, this is an X11 drawable (handle).
87 * On Windows, this is a Win32 window (handle).
88 * Video output plugins are supposed to called this function and display the
89 * video within the resulting window, while in windowed mode.
91 * @param p_vout video output thread to create a window for
92 * @param psz_cap VLC module capability (window system type)
93 * @param pi_x_hint pointer to store the recommended horizontal position [OUT]
94 * @param pi_y_hint pointer to store the recommended vertical position [OUT]
95 * @param pi_width_hint pointer to store the recommended width [OUT]
96 * @param pi_height_hint pointer to store the recommended height [OUT]
98 * @return a vout_window_t object, or NULL in case of failure.
99 * The window is released with vout_ReleaseWindow().
101 vout_window_t *vout_RequestWindow( vout_thread_t *p_vout, const char *psz_cap,
102 int *pi_x_hint, int *pi_y_hint,
103 unsigned int *pi_width_hint,
104 unsigned int *pi_height_hint )
106 /* Get requested coordinates */
107 *pi_x_hint = var_GetInteger( p_vout, "video-x" );
108 *pi_y_hint = var_GetInteger( p_vout, "video-y" );
110 *pi_width_hint = p_vout->i_window_width;
111 *pi_height_hint = p_vout->i_window_height;
113 vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
114 VLC_OBJECT_GENERIC, "window");
119 wnd->width = *pi_width_hint;
120 wnd->height = *pi_height_hint;
121 wnd->pos_x = *pi_x_hint;
122 wnd->pos_y = *pi_y_hint;
123 vlc_object_attach (wnd, p_vout);
125 wnd->module = module_need (wnd, psz_cap, NULL, false);
126 if (wnd->module == NULL)
128 msg_Dbg (wnd, "no \"%s\" window provider available", psz_cap);
129 vlc_object_release (wnd);
132 *pi_width_hint = wnd->width;
133 *pi_height_hint = wnd->height;
134 *pi_x_hint = wnd->pos_x;
135 *pi_y_hint = wnd->pos_y;
140 * Releases a window handle obtained with vout_RequestWindow().
141 * @param p_vout video output thread that allocated the window
142 * (if this is NULL; this fnction is a no-op).
144 void vout_ReleaseWindow( vout_window_t *wnd )
149 assert (wnd->module);
150 module_unneed (wnd, wnd->module);
152 vlc_object_release (wnd);
155 int vout_ControlWindow( vout_window_t *wnd, int i_query, va_list args )
160 assert (wnd->control);
161 return wnd->control (wnd, i_query, args);
164 /*****************************************************************************
165 * vout_IntfInit: called during the vout creation to initialise misc things.
166 *****************************************************************************/
170 const char *psz_label;
171 } p_zoom_values[] = {
172 { 0.25, N_("1:4 Quarter") },
173 { 0.5, N_("1:2 Half") },
174 { 1, N_("1:1 Original") },
175 { 2, N_("2:1 Double") },
180 const char *psz_value;
181 const char *psz_label;
182 } p_crop_values[] = {
183 { "", N_("Default") },
184 { "16:10", "16:10" },
186 { "185:100", "1.85:1" },
187 { "221:100", "2.21:1" },
188 { "235:100", "2.35:1" },
189 { "239:100", "2.39:1" },
198 const char *psz_value;
199 const char *psz_label;
200 } p_aspect_ratio_values[] = {
201 { "", N_("Default") },
205 { "16:10", "16:10" },
206 { "221:100", "2.21:1" },
210 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
213 if( psz_list && *psz_list )
215 char *psz_cur = psz_list;
217 while( psz_cur && *psz_cur )
219 vlc_value_t val, text;
220 psz_next = strchr( psz_cur, ',' );
226 val.psz_string = psz_cur;
227 text.psz_string = psz_cur;
228 var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
234 void vout_IntfInit( vout_thread_t *p_vout )
236 vlc_value_t val, text, old_val;
237 bool b_force_par = false;
241 /* Create a few object variables we'll need later on */
242 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
243 var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
244 var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
245 var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
246 var_Create( p_vout, "snapshot-sequential",
247 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248 var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
249 var_SetInteger( p_vout, "snapshot-num", 1 );
250 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
251 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
253 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
254 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
255 p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
257 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
260 var_Create( p_vout, "mouse-hide-timeout",
261 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
263 p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
264 p_vout->p->i_title_timeout =
265 (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
266 p_vout->p->i_title_position =
267 var_CreateGetInteger( p_vout, "video-title-position" );
268 p_vout->p->psz_title = NULL;
270 var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
271 var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
272 var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
274 /* Zoom object var */
275 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
276 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
278 text.psz_string = _("Zoom");
279 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
281 var_Get( p_vout, "zoom", &old_val );
283 for( i = 0; p_zoom_values[i].f_value; i++ )
285 if( old_val.f_float == p_zoom_values[i].f_value )
286 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
287 val.f_float = p_zoom_values[i].f_value;
288 text.psz_string = _( p_zoom_values[i].psz_label );
289 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
292 var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
294 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
296 /* Crop offset vars */
297 var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
298 var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
299 var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
300 var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
302 var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
303 var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
304 var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
305 var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
307 /* Crop object var */
308 var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
309 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
311 text.psz_string = _("Crop");
312 var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
314 val.psz_string = (char*)"";
315 var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
317 for( i = 0; p_crop_values[i].psz_value; i++ )
319 val.psz_string = (char*)p_crop_values[i].psz_value;
320 text.psz_string = _( p_crop_values[i].psz_label );
321 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
324 /* update triggered every time the vout's crop parameters are changed */
325 var_Create( p_vout, "crop-update", VLC_VAR_VOID );
327 /* Add custom crop ratios */
328 psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
329 AddCustomRatios( p_vout, "crop", psz_buf );
332 var_AddCallback( p_vout, "crop", CropCallback, NULL );
333 var_Get( p_vout, "crop", &old_val );
334 if( old_val.psz_string && *old_val.psz_string )
335 var_TriggerCallback( p_vout, "crop" );
336 free( old_val.psz_string );
338 /* Monitor pixel aspect-ratio */
339 var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
340 var_Get( p_vout, "monitor-par", &val );
341 if( val.psz_string && *val.psz_string )
343 char *psz_parser = strchr( val.psz_string, ':' );
344 unsigned int i_aspect_num = 0, i_aspect_den = 0;
348 i_aspect_num = strtol( val.psz_string, 0, 10 );
349 i_aspect_den = strtol( ++psz_parser, 0, 10 );
353 i_aspect = atof( val.psz_string );
354 vlc_ureduce( &i_aspect_num, &i_aspect_den,
355 i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
357 if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
359 p_vout->p->i_par_num = i_aspect_num;
360 p_vout->p->i_par_den = i_aspect_den;
362 vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
363 p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
365 msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
366 p_vout->p->i_par_num, p_vout->p->i_par_den );
369 free( val.psz_string );
371 /* Aspect-ratio object var */
372 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
373 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
375 text.psz_string = _("Aspect-ratio");
376 var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
378 val.psz_string = (char*)"";
379 var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
381 for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
383 val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
384 text.psz_string = _( p_aspect_ratio_values[i].psz_label );
385 var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
388 /* Add custom aspect ratios */
389 psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
390 AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
393 var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
394 var_Get( p_vout, "aspect-ratio", &old_val );
395 if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
396 var_TriggerCallback( p_vout, "aspect-ratio" );
397 free( old_val.psz_string );
399 /* Add variables to manage scaling video */
400 var_Create( p_vout, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
401 | VLC_VAR_ISCOMMAND );
402 text.psz_string = _("Autoscale video");
403 var_Change( p_vout, "autoscale", VLC_VAR_SETTEXT, &text, NULL );
404 var_AddCallback( p_vout, "autoscale", ScalingCallback, NULL );
405 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
407 var_Create( p_vout, "scale", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT
408 | VLC_VAR_ISCOMMAND );
409 text.psz_string = _("Scale factor");
410 var_Change( p_vout, "scale", VLC_VAR_SETTEXT, &text, NULL );
411 var_AddCallback( p_vout, "scale", ScalingCallback, NULL );
412 p_vout->i_zoom = (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
414 /* Initialize the dimensions of the video window */
415 InitWindowSize( p_vout, &p_vout->i_window_width,
416 &p_vout->i_window_height );
418 /* Add a variable to indicate if the window should be on top of others */
419 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
420 | VLC_VAR_ISCOMMAND );
421 text.psz_string = _("Always on top");
422 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
423 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
425 /* Add a variable to indicate whether we want window decoration or not */
426 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
428 /* Add a fullscreen variable */
429 if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
431 /* user requested fullscreen */
432 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
434 text.psz_string = _("Fullscreen");
435 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
436 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
438 /* Add a snapshot variable */
439 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
440 text.psz_string = _("Snapshot");
441 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
442 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
444 /* Mouse coordinates */
445 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
446 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
447 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
448 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
449 var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
451 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
452 var_SetBool( p_vout, "intf-change", true );
455 /*****************************************************************************
456 * vout_Snapshot: generates a snapshot.
457 *****************************************************************************/
459 * This function will inject a subpicture into the vout with the provided
462 static int VoutSnapshotPip( vout_thread_t *p_vout, picture_t *p_pic )
464 video_format_t fmt_in = p_pic->format;
465 video_format_t fmt_out;
467 subpicture_t *p_subpic;
470 memset( &fmt_out, 0, sizeof(fmt_out) );
472 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
475 image_handler_t *p_image = image_HandlerCreate( p_vout );
479 p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
481 image_HandlerDelete( p_image );
486 p_subpic = subpicture_New();
489 picture_Release( p_pip );
493 p_subpic->i_channel = 0;
494 p_subpic->i_start = mdate();
495 p_subpic->i_stop = mdate() + 4000000;
496 p_subpic->b_ephemer = true;
497 p_subpic->b_fade = true;
498 p_subpic->i_original_picture_width = fmt_out.i_width * 4;
499 p_subpic->i_original_picture_height = fmt_out.i_height * 4;
500 fmt_out.i_aspect = 0;
502 fmt_out.i_sar_den = 0;
504 p_subpic->p_region = subpicture_region_New( &fmt_out );
505 if( p_subpic->p_region )
507 picture_Release( p_subpic->p_region->p_picture );
508 p_subpic->p_region->p_picture = p_pip;
512 picture_Release( p_pip );
515 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
519 * This function will return the default directory used for snapshots
521 static char *VoutSnapshotGetDefaultDirectory( void )
523 char *psz_path = NULL;
524 #if defined(__APPLE__) || defined(SYS_BEOS)
526 if( asprintf( &psz_path, "%s/Desktop",
527 config_GetHomeDir() ) == -1 )
530 #elif defined(WIN32) && !defined(UNDER_CE)
532 /* Get the My Pictures folder path */
533 char *p_mypicturesdir = NULL;
534 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
536 #ifndef CSIDL_FLAG_CREATE
537 # define CSIDL_FLAG_CREATE 0x8000
539 #ifndef CSIDL_MYPICTURES
540 # define CSIDL_MYPICTURES 0x27
542 #ifndef SHGFP_TYPE_CURRENT
543 # define SHGFP_TYPE_CURRENT 0
546 HINSTANCE shfolder_dll;
547 SHGETFOLDERPATH SHGetFolderPath ;
549 /* load the shfolder dll to retrieve SHGetFolderPath */
550 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
552 wchar_t wdir[PATH_MAX];
553 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
554 _T("SHGetFolderPathW") );
555 if ((SHGetFolderPath != NULL )
556 && SUCCEEDED (SHGetFolderPath (NULL,
557 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
558 NULL, SHGFP_TYPE_CURRENT,
560 p_mypicturesdir = FromWide (wdir);
562 FreeLibrary( shfolder_dll );
565 if( p_mypicturesdir == NULL )
566 psz_path = strdup( config_GetHomeDir() );
568 psz_path = p_mypicturesdir;
572 /* XXX: This saves in the data directory. Shouldn't we try saving
573 * to psz_homedir/Desktop or something nicer ? */
574 char *psz_datadir = config_GetUserDataDir();
577 if( asprintf( &psz_path, "%s", psz_datadir ) == -1 )
587 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_picture_private )
589 /* FIXME find why it is sometimes needed (format being wrong/invalid) */
590 picture_t pic = *p_picture_private;
591 pic.format = p_vout->fmt_out;
592 picture_t *p_pic = &pic;
595 char *psz_path = var_GetNonEmptyString( p_vout, "snapshot-path" );
597 /* Embedded snapshot : if snapshot-path == object:object_ptr */
599 const bool b_embedded_snapshot = psz_path && sscanf( psz_path, "object:%p", &p_obj ) > 0;
601 char *psz_format = NULL;
602 if( !b_embedded_snapshot )
604 psz_format = var_GetNonEmptyString( p_vout, "snapshot-format" );
606 psz_format = strdup( "png" );
614 vlc_fourcc_t i_format = VLC_FOURCC('p','n','g',' ');
615 if( psz_format && image_Ext2Fourcc( psz_format ) )
616 i_format = image_Ext2Fourcc( psz_format );
618 int i_override_width = var_GetInteger( p_vout, "snapshot-width" );
619 int i_override_height = var_GetInteger( p_vout, "snapshot-height" );
620 /* If snapshot-width and/or snapshot height were not specified for embed snapshot ,
621 use a default snapshot width of 320.
622 TODO removed that weird behavior */
624 if( b_embedded_snapshot && !i_override_width && !i_override_height )
625 i_override_width = 320;
628 video_format_t fmt_out;
629 if( picture_Export( VLC_OBJECT(p_vout), &p_out, &fmt_out,
630 p_pic, i_format, i_override_width, i_override_height ) )
632 msg_Err( p_vout, "Failed to convert image for snapshot" );
636 if( b_embedded_snapshot )
638 /* TODO fix that ugliness */
639 snapshot_t *p_snapshot = p_obj;
641 vlc_mutex_lock( &p_snapshot->p_mutex );
642 p_snapshot->i_width = fmt_out.i_width;
643 p_snapshot->i_height = fmt_out.i_height;
644 p_snapshot->date = p_pic->date;
646 p_snapshot->i_datasize = 0;
647 p_snapshot->p_data = NULL;
650 p_snapshot->p_data = malloc( p_out->i_buffer );
651 if( p_snapshot->p_data )
653 p_snapshot->i_datasize = p_out->i_buffer;
654 memcpy( p_snapshot->p_data, p_out->p_buffer, p_out->i_buffer );
658 vlc_cond_signal( &p_snapshot->p_condvar );
659 vlc_mutex_unlock( &p_snapshot->p_mutex );
666 block_Release( p_out );
673 /* Get default directory if none provided */
676 psz_path = VoutSnapshotGetDefaultDirectory();
679 msg_Err( p_vout, "no path specified for snapshots" );
686 DIR *p_path = utf8_opendir( psz_path );
689 /* The use specified a directory path */
693 char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
696 char *psz_tmp = str_format( p_vout, psz_prefix );
698 psz_prefix = psz_tmp;
702 psz_prefix = strdup( "vlcsnap-" );
707 if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
709 int i_num = var_GetInteger( p_vout, "snapshot-num" );
714 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
715 psz_path, psz_prefix, i_num++, psz_format ) < 0 )
720 if( utf8_stat( psz_filename, &st ) )
722 free( psz_filename );
725 var_SetInteger( p_vout, "snapshot-num", i_num );
730 time_t lcurtime = time( NULL ) ;
732 if( !localtime_r( &lcurtime, &curtime ) )
734 const unsigned int i_id = (p_pic->date / 100000) & 0xFFFFFF;
736 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
738 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
739 psz_path, psz_prefix, i_id, psz_format ) < 0 )
744 /* suffix with the last decimal digit in 10s of seconds resolution
746 const int i_id = (p_pic->date / (100*1000)) & 0xFF;
747 char psz_curtime[128];
749 if( !strftime( psz_curtime, sizeof(psz_curtime), "%Y-%m-%d-%Hh%Mm%Ss", &curtime ) )
750 strcpy( psz_curtime, "error" );
752 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
753 psz_path, psz_prefix, psz_curtime, i_id, psz_format ) < 0 )
761 /* The user specified a full path name (including file name) */
762 psz_filename = str_format( p_vout, psz_path );
763 path_sanitize( psz_filename );
769 /* Save the snapshot */
770 FILE *p_file = utf8_fopen( psz_filename, "wb" );
773 msg_Err( p_vout, "Failed to open '%s'", psz_filename );
774 free( psz_filename );
777 if( fwrite( p_out->p_buffer, p_out->i_buffer, 1, p_file ) != 1 )
779 msg_Err( p_vout, "Failed to write to '%s'", psz_filename );
781 free( psz_filename );
787 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
788 vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
789 "%s", psz_filename );
791 /* Generate a media player event - Right now just trigger a global libvlc var
792 CHECK: Could not find a more local object. The goal is to communicate
793 vout_thread with libvlc_media_player or its input_thread*/
794 var_SetString( p_vout->p_libvlc, "vout-snapshottaken", psz_filename );
796 free( psz_filename );
799 if( var_GetBool( p_vout, "snapshot-preview" ) )
801 if( VoutSnapshotPip( p_vout, p_pic ) )
802 msg_Warn( p_vout, "Failed to display snapshot" );
806 block_Release( p_out );
812 msg_Err( p_vout, "could not create snapshot" );
814 block_Release( p_out );
820 /*****************************************************************************
822 *****************************************************************************/
824 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
825 bool b_add, bool b_setconfig )
828 char *psz_string = config_GetPsz( p_vout, "vout-filter" );
830 /* Todo : Use some generic chain manipulation functions */
831 if( !psz_string ) psz_string = strdup("");
833 psz_parser = strstr( psz_string, psz_name );
838 psz_parser = psz_string;
839 if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
840 psz_string, psz_name ) == -1 )
854 memmove( psz_parser, psz_parser + strlen(psz_name) +
855 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
856 strlen(psz_parser + strlen(psz_name)) + 1 );
858 /* Remove trailing : : */
859 if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
861 *(psz_string+strlen(psz_string ) -1 ) = '\0';
871 config_PutPsz( p_vout, "vout-filter", psz_string );
873 var_SetString( p_vout, "vout-filter", psz_string );
877 /*****************************************************************************
878 * InitWindowSize: find the initial dimensions the video window should have.
879 *****************************************************************************
880 * This function will check the "width", "height" and "zoom" config options and
881 * will calculate the size that the video window should have.
882 *****************************************************************************/
883 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
884 unsigned *pi_height )
886 #define FP_FACTOR 1000 /* our fixed point factor */
888 int i_width = var_GetInteger( p_vout, "width" );
889 int i_height = var_GetInteger( p_vout, "height" );
890 float f_zoom = var_GetFloat( p_vout, "zoom" );
891 uint64_t ll_zoom = (uint64_t)( FP_FACTOR * f_zoom );
893 if( i_width > 0 && i_height > 0)
895 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
896 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
898 else if( i_width > 0 )
900 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
901 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
902 p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
903 FP_FACTOR / p_vout->fmt_in.i_visible_width );
905 else if( i_height > 0 )
907 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
908 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
909 p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
910 FP_FACTOR / p_vout->fmt_in.i_visible_height );
912 else if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 )
914 msg_Warn( p_vout, "aspect ratio screwed up" );
915 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
916 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
918 else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
920 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
921 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
922 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
927 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
929 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
930 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
933 msg_Dbg( p_vout, "window size: %ux%u", *pi_width, *pi_height );
938 /*****************************************************************************
939 * Object variables callbacks
940 *****************************************************************************/
941 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
942 vlc_value_t oldval, vlc_value_t newval, void *p_data )
944 vout_thread_t *p_vout = (vout_thread_t *)p_this;
945 (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
946 InitWindowSize( p_vout, &p_vout->i_window_width,
947 &p_vout->i_window_height );
948 vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
949 p_vout->i_window_height );
953 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
954 vlc_value_t oldval, vlc_value_t newval, void *p_data )
956 vout_thread_t *p_vout = (vout_thread_t *)p_this;
957 int64_t i_aspect_num, i_aspect_den;
958 unsigned int i_width, i_height;
960 (void)oldval; (void)p_data;
962 /* Restore defaults */
963 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
964 p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
965 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
966 p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
968 if( !strcmp( psz_cmd, "crop" ) )
970 char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
973 /* We're using the 3:4 syntax */
974 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
975 if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
977 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
978 if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
980 i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
981 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
982 i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
983 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
985 if( i_width < p_vout->fmt_render.i_visible_width )
987 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
988 (p_vout->fmt_render.i_visible_width - i_width) / 2;
989 p_vout->fmt_in.i_visible_width = i_width;
993 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
994 (p_vout->fmt_render.i_visible_height - i_height) / 2;
995 p_vout->fmt_in.i_visible_height = i_height;
1000 psz_parser = strchr( newval.psz_string, 'x' );
1003 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1004 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1006 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1007 if( psz_end != psz_parser ) goto crop_end;
1009 psz_parser = strchr( ++psz_end, '+' );
1010 i_crop_height = strtol( psz_end, &psz_end, 10 );
1011 if( psz_end != psz_parser ) goto crop_end;
1013 psz_parser = strchr( ++psz_end, '+' );
1014 i_crop_left = strtol( psz_end, &psz_end, 10 );
1015 if( psz_end != psz_parser ) goto crop_end;
1018 i_crop_top = strtol( psz_end, &psz_end, 10 );
1019 if( *psz_end != '\0' ) goto crop_end;
1021 i_width = i_crop_width;
1022 p_vout->fmt_in.i_visible_width = i_width;
1024 i_height = i_crop_height;
1025 p_vout->fmt_in.i_visible_height = i_height;
1027 p_vout->fmt_in.i_x_offset = i_crop_left;
1028 p_vout->fmt_in.i_y_offset = i_crop_top;
1032 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1033 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1035 psz_parser = strchr( newval.psz_string, '+' );
1036 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1037 if( psz_end != psz_parser ) goto crop_end;
1039 psz_parser = strchr( ++psz_end, '+' );
1040 i_crop_top = strtol( psz_end, &psz_end, 10 );
1041 if( psz_end != psz_parser ) goto crop_end;
1043 psz_parser = strchr( ++psz_end, '+' );
1044 i_crop_right = strtol( psz_end, &psz_end, 10 );
1045 if( psz_end != psz_parser ) goto crop_end;
1048 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1049 if( *psz_end != '\0' ) goto crop_end;
1051 i_width = p_vout->fmt_render.i_visible_width
1052 - i_crop_left - i_crop_right;
1053 p_vout->fmt_in.i_visible_width = i_width;
1055 i_height = p_vout->fmt_render.i_visible_height
1056 - i_crop_top - i_crop_bottom;
1057 p_vout->fmt_in.i_visible_height = i_height;
1059 p_vout->fmt_in.i_x_offset = i_crop_left;
1060 p_vout->fmt_in.i_y_offset = i_crop_top;
1064 else if( !strcmp( psz_cmd, "crop-top" )
1065 || !strcmp( psz_cmd, "crop-left" )
1066 || !strcmp( psz_cmd, "crop-bottom" )
1067 || !strcmp( psz_cmd, "crop-right" ) )
1069 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1071 i_crop_top = var_GetInteger( p_vout, "crop-top" );
1072 i_crop_left = var_GetInteger( p_vout, "crop-left" );
1073 i_crop_right = var_GetInteger( p_vout, "crop-right" );
1074 i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1076 i_width = p_vout->fmt_render.i_visible_width
1077 - i_crop_left - i_crop_right;
1078 p_vout->fmt_in.i_visible_width = i_width;
1080 i_height = p_vout->fmt_render.i_visible_height
1081 - i_crop_top - i_crop_bottom;
1082 p_vout->fmt_in.i_visible_height = i_height;
1084 p_vout->fmt_in.i_x_offset = i_crop_left;
1085 p_vout->fmt_in.i_y_offset = i_crop_top;
1089 InitWindowSize( p_vout, &p_vout->i_window_width,
1090 &p_vout->i_window_height );
1092 p_vout->i_changes |= VOUT_CROP_CHANGE;
1094 msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1095 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1096 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1097 p_vout->fmt_in.i_visible_width,
1098 p_vout->fmt_in.i_visible_height );
1100 var_SetVoid( p_vout, "crop-update" );
1105 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1106 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1108 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1109 unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1112 char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1113 (void)psz_cmd; (void)oldval; (void)p_data;
1115 /* Restore defaults */
1116 p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1117 p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1118 p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1119 p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1121 if( !psz_parser ) goto aspect_end;
1123 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1124 if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1126 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1127 if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1129 i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1130 i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1131 vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1132 p_vout->fmt_in.i_sar_num = i_sar_num;
1133 p_vout->fmt_in.i_sar_den = i_sar_den;
1134 p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1135 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1138 if( p_vout->p->i_par_num && p_vout->p->i_par_den )
1140 p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
1141 p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
1142 p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1143 p_vout->p->i_par_den / p_vout->p->i_par_num;
1144 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1147 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1149 vlc_ureduce( &i_aspect_num, &i_aspect_den,
1150 p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1151 msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1152 i_aspect_num, i_aspect_den,
1153 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1155 if( var_Get( p_vout, "crop", &val ) )
1156 return VLC_EGENERIC;
1158 int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1159 free( val.psz_string );
1163 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
1164 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1166 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1167 (void)oldval; (void)newval; (void)p_data;
1169 vlc_mutex_lock( &p_vout->change_lock );
1171 if( !strcmp( psz_cmd, "autoscale" ) )
1173 p_vout->i_changes |= VOUT_SCALE_CHANGE;
1175 else if( !strcmp( psz_cmd, "scale" ) )
1177 p_vout->i_changes |= VOUT_ZOOM_CHANGE;
1180 vlc_mutex_unlock( &p_vout->change_lock );
1185 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1186 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1188 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1190 vlc_mutex_lock( &p_vout->change_lock );
1191 p_vout->i_changes |= VOUT_ON_TOP_CHANGE;
1192 p_vout->b_on_top = newval.b_bool;
1193 vlc_mutex_unlock( &p_vout->change_lock );
1195 /* Modify libvlc as well because the vout might have to be restarted */
1196 var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1197 var_Set( p_vout->p_libvlc, "video-on-top", newval );
1199 (void)psz_cmd; (void)oldval; (void)p_data;
1203 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1204 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1206 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1208 (void)psz_cmd; (void)oldval; (void)p_data;
1210 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1212 /* Modify libvlc as well because the vout might have to be restarted */
1213 var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1214 var_Set( p_vout->p_libvlc, "fullscreen", newval );
1217 var_Set( p_vout, "intf-change", val );
1221 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1222 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1224 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1226 /* FIXME: this is definitely not thread-safe */
1227 p_vout->p->b_snapshot = true;
1228 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1229 VLC_UNUSED(newval); VLC_UNUSED(p_data);
1233 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1234 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1236 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1238 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1239 p_vout->p->b_title_show = newval.b_bool;
1243 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1244 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1246 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1247 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1248 p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1252 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1253 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1255 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1257 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1258 p_vout->p->i_title_position = newval.i_int;