From cf9fab57cc9fefc814f49e084f1030bda343dca0 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 19 Oct 2026 16:11:36 +0200 Subject: [PATCH] filechooser: play sound and video in the preview Sound and video files get a play/pause and a stop button under their picture in the panel beside the files. Nothing plays before they are pressed, and nothing is loaded before: GTK+ needs none of this to build or to run, it uses what it finds installed. - GStreamer 1.x, opened as a library where it uses the same GLib as the program (on Linux and the BSDs, and from Homebrew or MacPorts). It scales the pictures to the size of the preview itself. - xine-lib 1.2, opened as a library, with its raw video output. - FFmpeg, as the programs: ffmpeg decodes the pictures into a pipe at 25 a second and the size of the preview, ffplay plays the sound; pausing stops both. - On Windows the Media Control Interface every version has, from 3.1 on, with its video window put over the preview. The preferences have a switch for all of it and, as the panel, a choice of what plays. Where nothing is installed, the panel says what to install. The buttons take their icons and tooltips from the stock items Play, Pause and Stop, so they are translated already. --- gtk/Makefile.am | 2 + gtk/gtkfilechooserdefault.c | 251 +++++++ gtk/gtkfilechooserprivate.h | 9 + gtk/gtkfilechoosersettings.c | 59 ++ gtk/gtkfilechoosersettings.h | 10 + gtk/gtkfilemedia.c | 1191 ++++++++++++++++++++++++++++++++++ gtk/gtkfilemedia.h | 50 ++ 7 files changed, 1572 insertions(+) create mode 100644 gtk/gtkfilemedia.c create mode 100644 gtk/gtkfilemedia.h diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 066124381..e26af3c07 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -396,6 +396,7 @@ gtk_private_h_sources = \ gtkfileoperation.h \ gtkfilesystem.h \ gtkfilesystemmodel.h \ + gtkfilemedia.h \ gtkfilethumbnail.h \ gtktimeprivate.h \ gtkiconcache.h \ @@ -506,6 +507,7 @@ gtk_base_c_sources = \ gtkfilefilter.c \ gtkfilesystem.c \ gtkfilesystemmodel.c \ + gtkfilemedia.c \ gtkfilethumbnail.c \ gtkfixed.c \ gtkfontbutton.c \ diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 5181a0b08..fbdccb1af 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -43,6 +43,7 @@ #include "gtkfilechooser.h" #include "gtkfilesystem.h" #include "gtkfilesystemmodel.h" +#include "gtkfilemedia.h" #include "gtkfilethumbnail.h" #include "gtkframe.h" #include "gtkhbox.h" @@ -1122,6 +1123,7 @@ gtk_file_chooser_default_finalize (GObject *object) recent_clear_model (impl, FALSE); g_free (impl->preview_display_name); + g_free (impl->media_backend); g_free (impl->edited_new_text); if (impl->rename_file) @@ -1430,6 +1432,16 @@ preview_panel_stop (GtkFileChooserDefault *impl) g_cancellable_cancel (impl->preview_panel_cancellable); g_clear_object (&impl->preview_panel_cancellable); } + if (impl->media) + { + _gtk_file_media_stop (impl->media); + impl->media = NULL; + } + if (impl->media_done_id) + { + g_source_remove (impl->media_done_id); + impl->media_done_id = 0; + } } /* A line "Key: value" more in the table of the panel */ @@ -1628,6 +1640,143 @@ preview_panel_thumbnail_done (GFile *file, preview_panel_set_picture (impl, thumbnail, &small); } +/* Sound and video, started by the user only */ + +/* A button with the icon of @stock_id and its label as the tooltip */ +static void +preview_panel_media_button (GtkWidget *button, + const gchar *stock_id) +{ + GtkStockItem item; + + gtk_button_set_image (GTK_BUTTON (button), + gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON)); + if (gtk_stock_lookup (stock_id, &item)) + { + gchar *label = g_strdup (item.label), *from, *to; + + for (from = to = label; *from; from++) + if (*from != '_') + *to++ = *from; + *to = '\0'; + gtk_widget_set_tooltip_text (button, label); + g_free (label); + } +} + +static void +preview_panel_play_icon (GtkFileChooserDefault *impl, + gboolean playing) +{ + preview_panel_media_button (impl->preview_panel_play, + playing ? GTK_STOCK_MEDIA_PAUSE : GTK_STOCK_MEDIA_PLAY); +} + +static void +preview_panel_media_frame (GdkPixbuf *frame, + gpointer data) +{ + GtkFileChooserDefault *impl = data; + GtkImage *image = GTK_IMAGE (impl->preview_panel_image); + + if (gtk_image_get_storage_type (image) == GTK_IMAGE_PIXBUF && + gtk_image_get_pixbuf (image) == frame) + gtk_widget_queue_draw (impl->preview_panel_image); + else + gtk_image_set_from_pixbuf (image, frame); +} + +/* The picture of the file again, as before it played */ +static gboolean +preview_panel_media_over (gpointer data) +{ + GtkFileChooserDefault *impl = data; + + impl->media_done_id = 0; + preview_panel_update (impl); + if (impl->media_failed) + gtk_label_set_text (GTK_LABEL (impl->preview_panel_note), _("Could not play the file")); + + return FALSE; +} + +static void +preview_panel_media_done (GtkFileMediaEnd end, + gpointer data) +{ + GtkFileChooserDefault *impl = data; + + impl->media_failed = end == GTK_FILE_MEDIA_FAILED; + /* not from within the player */ + if (impl->media_done_id == 0) + impl->media_done_id = gdk_threads_add_idle (preview_panel_media_over, impl); +} + +static void +preview_panel_play_clicked (GtkButton *button, + GtkFileChooserDefault *impl) +{ + if (!_gtk_file_media_available (impl->media_backend)) + return; + if (impl->media) + { + impl->media_paused = !impl->media_paused; + _gtk_file_media_pause (impl->media, impl->media_paused); + preview_panel_play_icon (impl, !impl->media_paused); + return; + } + + impl->media = _gtk_file_media_play (impl->preview_file, impl->media_backend, + impl->preview_panel_image, PREVIEW_PANEL_IMAGE, + preview_panel_media_frame, preview_panel_media_done, impl); + impl->media_paused = FALSE; + if (impl->media) + preview_panel_play_icon (impl, TRUE); + else + gtk_label_set_text (GTK_LABEL (impl->preview_panel_note), _("Could not play the file")); +} + +static void +preview_panel_stop_clicked (GtkButton *button, + GtkFileChooserDefault *impl) +{ + if (impl->media) + preview_panel_update (impl); +} + +/* Play and stop under the picture of sound and video, or what to + * install for them */ +static void +preview_panel_media_controls (GtkFileChooserDefault *impl, + GFileInfo *info) +{ + gboolean plays = info && impl->play_media && _gtk_file_media_plays (info); + gboolean available = plays && _gtk_file_media_available (impl->media_backend); + + gtk_widget_set_visible (impl->preview_panel_media, plays); + gtk_widget_set_sensitive (impl->preview_panel_play, available); + preview_panel_play_icon (impl, FALSE); + if (plays && !available && g_strcmp0 (impl->media_backend, "auto") != 0) + { + /* the one chosen in the preferences */ + gchar *note = g_strdup_printf (_("%s (not installed)"), + _gtk_file_media_backend_name (impl->media_backend)); + + gtk_label_set_text (GTK_LABEL (impl->preview_panel_note), note); + g_free (note); + } + else if (plays && !available) + gtk_label_set_text (GTK_LABEL (impl->preview_panel_note), +#ifdef G_OS_WIN32 + _("To play sound and video here, install GStreamer.") +#else + _("To play sound and video here, install GStreamer, xine or FFmpeg.") +#endif + ); + else + gtk_label_set_text (GTK_LABEL (impl->preview_panel_note), ""); +} + static void preview_panel_update (GtkFileChooserDefault *impl) { @@ -1646,6 +1795,8 @@ preview_panel_update (GtkFileChooserDefault *impl) preview_panel_clear_rows (impl); g_object_set_data (G_OBJECT (impl->preview_panel_info), I_("gtk-dimensions"), NULL); + preview_panel_media_controls (impl, info); + if (info == NULL) { gtk_image_clear (GTK_IMAGE (impl->preview_panel_image)); @@ -1760,6 +1911,9 @@ preview_panel_sync (GtkFileChooserDefault *impl) impl->preview_panel_image = NULL; impl->preview_panel_name = NULL; impl->preview_panel_info = NULL; + impl->preview_panel_media = NULL; + impl->preview_panel_play = NULL; + impl->preview_panel_note = NULL; } return; } @@ -1773,6 +1927,32 @@ preview_panel_sync (GtkFileChooserDefault *impl) gtk_widget_set_size_request (impl->preview_panel_image, -1, PREVIEW_PANEL_IMAGE); gtk_box_pack_start (GTK_BOX (impl->preview_panel), impl->preview_panel_image, FALSE, FALSE, 0); + { + GtkWidget *row, *button; + + impl->preview_panel_media = gtk_vbox_new (FALSE, 0); + row = gtk_hbox_new (FALSE, 6); + impl->preview_panel_play = gtk_button_new (); + g_signal_connect (impl->preview_panel_play, "clicked", + G_CALLBACK (preview_panel_play_clicked), impl); + gtk_box_pack_start (GTK_BOX (row), impl->preview_panel_play, FALSE, FALSE, 0); + button = gtk_button_new (); + preview_panel_media_button (button, GTK_STOCK_MEDIA_STOP); + g_signal_connect (button, "clicked", G_CALLBACK (preview_panel_stop_clicked), impl); + gtk_box_pack_start (GTK_BOX (row), button, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (impl->preview_panel_media), row, FALSE, FALSE, 0); + + impl->preview_panel_note = gtk_label_new (NULL); + gtk_label_set_line_wrap (GTK_LABEL (impl->preview_panel_note), TRUE); + gtk_misc_set_alignment (GTK_MISC (impl->preview_panel_note), 0.0, 0.0); + gtk_widget_set_size_request (impl->preview_panel_note, PREVIEW_PANEL_WIDTH, -1); + gtk_box_pack_start (GTK_BOX (impl->preview_panel_media), impl->preview_panel_note, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (impl->preview_panel), impl->preview_panel_media, FALSE, FALSE, 0); + gtk_widget_set_no_show_all (impl->preview_panel_media, TRUE); + gtk_widget_show_all (row); + gtk_widget_show (impl->preview_panel_note); + } + impl->preview_panel_name = gtk_label_new (NULL); gtk_label_set_line_wrap (GTK_LABEL (impl->preview_panel_name), TRUE); gtk_label_set_line_wrap_mode (GTK_LABEL (impl->preview_panel_name), PANGO_WRAP_WORD_CHAR); @@ -6266,6 +6446,32 @@ preferences_preview_toggled_cb (GtkToggleButton *button, settings_save (impl); } +static void +preferences_media_toggled_cb (GtkToggleButton *button, + GtkFileChooserDefault *impl) +{ + impl->play_media = gtk_toggle_button_get_active (button); + gtk_widget_set_sensitive (g_object_get_data (G_OBJECT (button), "gtk-backends"), impl->play_media); + if (impl->preview_panel) + preview_panel_queue_update (impl); + settings_save (impl); +} + +static void +preferences_backend_changed_cb (GtkComboBox *combo, + GtkFileChooserDefault *impl) +{ + gint active = gtk_combo_box_get_active (combo); + + if (active < 0) + return; + g_free (impl->media_backend); + impl->media_backend = g_strdup (_gtk_file_media_backends ()[active]); + if (impl->preview_panel) + preview_panel_queue_update (impl); + settings_save (impl); +} + static void preferences_hidden_toggled_cb (GtkToggleButton *button, GtkFileChooserDefault *impl) @@ -6404,6 +6610,46 @@ preferences_cb (GtkMenuItem *item, g_signal_connect (button, "toggled", G_CALLBACK (preferences_hidden_toggled_cb), impl); gtk_box_pack_start (GTK_BOX (section), button, FALSE, FALSE, 0); + /* Sound and video in the preview, with what is installed */ + section = preferences_section (box, _("Audio/Video")); + button = gtk_check_button_new_with_mnemonic (_("_Play sound and video in the preview")); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), impl->play_media); + gtk_box_pack_start (GTK_BOX (section), button, FALSE, FALSE, 0); + { + const gchar * const *backends = _gtk_file_media_backends (); + GtkWidget *row = gtk_hbox_new (FALSE, 6); + GtkWidget *label = gtk_label_new_with_mnemonic (_("Play _with:")); + GtkWidget *combo = gtk_combo_box_new_text (); + gint i; + + for (i = 0; backends[i]; i++) + { + gchar *text; + + if (i == 0) + text = g_strdup (_("Whatever is installed")); + else if (_gtk_file_media_available (backends[i])) + text = g_strdup (_gtk_file_media_backend_name (backends[i])); + else + /* Translators: a program for sound and video that is not installed */ + text = g_strdup_printf (_("%s (not installed)"), _gtk_file_media_backend_name (backends[i])); + gtk_combo_box_append_text (GTK_COMBO_BOX (combo), text); + g_free (text); + if (g_strcmp0 (backends[i], impl->media_backend) == 0) + gtk_combo_box_set_active (GTK_COMBO_BOX (combo), i); + } + if (gtk_combo_box_get_active (GTK_COMBO_BOX (combo)) < 0) + gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); + g_signal_connect (combo, "changed", G_CALLBACK (preferences_backend_changed_cb), impl); + gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); + gtk_box_pack_start (GTK_BOX (row), label, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (row), combo, FALSE, FALSE, 0); + gtk_widget_set_sensitive (row, impl->play_media); + gtk_box_pack_start (GTK_BOX (section), row, FALSE, FALSE, 0); + g_object_set_data (G_OBJECT (button), I_("gtk-backends"), row); + } + g_signal_connect (button, "toggled", G_CALLBACK (preferences_media_toggled_cb), impl); + /* Where to start, unless the program says */ section = preferences_section (box, _("Start With")); radio = gtk_radio_button_new_with_mnemonic (NULL, _("_Recently used files")); @@ -11682,6 +11928,9 @@ settings_load (GtkFileChooserDefault *impl) type_format = _gtk_file_chooser_settings_get_type_format (settings); show_thumbnails = _gtk_file_chooser_settings_get_show_thumbnails (settings); show_preview_panel = _gtk_file_chooser_settings_get_show_preview_panel (settings); + impl->play_media = _gtk_file_chooser_settings_get_play_media (settings); + g_free (impl->media_backend); + impl->media_backend = g_strdup (_gtk_file_chooser_settings_get_media_backend (settings)); thumbnail_max_size = _gtk_file_chooser_settings_get_thumbnail_max_size (settings, FALSE); thumbnail_max_size_remote = _gtk_file_chooser_settings_get_thumbnail_max_size (settings, TRUE); sort_column = _gtk_file_chooser_settings_get_sort_column (settings); @@ -11793,6 +12042,8 @@ settings_save (GtkFileChooserDefault *impl) _gtk_file_chooser_settings_set_show_type_column (settings, impl->show_type_column); _gtk_file_chooser_settings_set_show_thumbnails (settings, impl->show_thumbnails); _gtk_file_chooser_settings_set_show_preview_panel (settings, impl->show_preview_panel); + _gtk_file_chooser_settings_set_play_media (settings, impl->play_media); + _gtk_file_chooser_settings_set_media_backend (settings, impl->media_backend); _gtk_file_chooser_settings_set_thumbnail_max_size (settings, FALSE, impl->thumbnail_max_size); _gtk_file_chooser_settings_set_thumbnail_max_size (settings, TRUE, impl->thumbnail_max_size_remote); _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column); diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 4191348fa..fa984b59e 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -346,6 +346,12 @@ struct _GtkFileChooserDefault GtkWidget *preview_panel_image; GtkWidget *preview_panel_name; GtkWidget *preview_panel_info; + GtkWidget *preview_panel_media; /* play and stop, with a note */ + GtkWidget *preview_panel_play; + GtkWidget *preview_panel_note; + struct _GtkFileMedia *media; /* what plays there */ + gchar *media_backend; + guint media_done_id; /* thumbnails of files up to these sizes, in MB; of none on network * shares if 0 */ @@ -423,6 +429,9 @@ struct _GtkFileChooserDefault guint show_thumbnails : 1; guint compact_view : 1; /* the icon view with small icons beside the names */ guint show_preview_panel : 1; + guint play_media : 1; + guint media_paused : 1; + guint media_failed : 1; guint drag_several : 1; /* a drag of all selected files */ guint create_folders : 1; guint auto_selecting_first_row : 1; diff --git a/gtk/gtkfilechoosersettings.c b/gtk/gtkfilechoosersettings.c index e573baf8b..b62e04589 100644 --- a/gtk/gtkfilechoosersettings.c +++ b/gtk/gtkfilechoosersettings.c @@ -47,6 +47,8 @@ #define TYPE_FORMAT_KEY "TypeFormat" #define SHOW_THUMBNAILS_KEY "ShowThumbnails" #define SHOW_PREVIEW_PANEL_KEY "ShowPreviewPanel" +#define PLAY_MEDIA_KEY "PlayMedia" +#define MEDIA_BACKEND_KEY "MediaBackend" #define THUMBNAIL_MAX_SIZE_KEY "ThumbnailMaxSize" #define THUMBNAIL_MAX_SIZE_REMOTE_KEY "ThumbnailMaxSizeRemote" #define GEOMETRY_X_KEY "GeometryX" @@ -267,6 +269,19 @@ ensure_settings_read (GtkFileChooserSettings *settings) else settings->show_preview_panel = value != FALSE; + /* Sound and video in the preview, and with what */ + + value = g_key_file_get_boolean (key_file, SETTINGS_GROUP, + PLAY_MEDIA_KEY, &error); + if (error) + warn_if_invalid_key_and_clear_error (PLAY_MEDIA_KEY, &error); + else + settings->play_media = value != FALSE; + + g_free (settings->media_backend); + settings->media_backend = g_key_file_get_string (key_file, SETTINGS_GROUP, + MEDIA_BACKEND_KEY, NULL); + /* Geometry */ get_int_key (key_file, SETTINGS_GROUP, GEOMETRY_X_KEY, &settings->geometry_x); @@ -339,9 +354,18 @@ ensure_settings_read (GtkFileChooserSettings *settings) G_DEFINE_TYPE (GtkFileChooserSettings, _gtk_file_chooser_settings, G_TYPE_OBJECT) +static void +_gtk_file_chooser_settings_finalize (GObject *object) +{ + g_free (((GtkFileChooserSettings *) object)->media_backend); + + G_OBJECT_CLASS (_gtk_file_chooser_settings_parent_class)->finalize (object); +} + static void _gtk_file_chooser_settings_class_init (GtkFileChooserSettingsClass *class) { + G_OBJECT_CLASS (class)->finalize = _gtk_file_chooser_settings_finalize; } static void @@ -359,6 +383,7 @@ _gtk_file_chooser_settings_init (GtkFileChooserSettings *settings) settings->show_thumbnails = TRUE; settings->thumbnail_max_size = 64; settings->thumbnail_max_size_remote = 0; + settings->play_media = TRUE; settings->geometry_x = -1; settings->geometry_y = -1; settings->geometry_width = -1; @@ -490,6 +515,36 @@ _gtk_file_chooser_settings_get_show_preview_panel (GtkFileChooserSettings *setti return settings->show_preview_panel; } +gboolean +_gtk_file_chooser_settings_get_play_media (GtkFileChooserSettings *settings) +{ + ensure_settings_read (settings); + return settings->play_media; +} + +void +_gtk_file_chooser_settings_set_play_media (GtkFileChooserSettings *settings, + gboolean play_media) +{ + settings->play_media = play_media != FALSE; +} + +/* "auto", or one of _gtk_file_media_backends() */ +const gchar * +_gtk_file_chooser_settings_get_media_backend (GtkFileChooserSettings *settings) +{ + ensure_settings_read (settings); + return settings->media_backend ? settings->media_backend : "auto"; +} + +void +_gtk_file_chooser_settings_set_media_backend (GtkFileChooserSettings *settings, + const gchar *backend) +{ + g_free (settings->media_backend); + settings->media_backend = g_strdup (backend); +} + gboolean _gtk_file_chooser_settings_get_show_thumbnails (GtkFileChooserSettings *settings) { @@ -706,6 +761,10 @@ _gtk_file_chooser_settings_save (GtkFileChooserSettings *settings, SHOW_THUMBNAILS_KEY, settings->show_thumbnails); g_key_file_set_boolean (key_file, SETTINGS_GROUP, SHOW_PREVIEW_PANEL_KEY, settings->show_preview_panel); + g_key_file_set_boolean (key_file, SETTINGS_GROUP, + PLAY_MEDIA_KEY, settings->play_media); + g_key_file_set_string (key_file, SETTINGS_GROUP, + MEDIA_BACKEND_KEY, settings->media_backend ? settings->media_backend : "auto"); g_key_file_set_integer (key_file, SETTINGS_GROUP, THUMBNAIL_MAX_SIZE_KEY, settings->thumbnail_max_size); g_key_file_set_integer (key_file, SETTINGS_GROUP, diff --git a/gtk/gtkfilechoosersettings.h b/gtk/gtkfilechoosersettings.h index 7b499fe01..0f37a7017 100644 --- a/gtk/gtkfilechoosersettings.h +++ b/gtk/gtkfilechoosersettings.h @@ -73,6 +73,9 @@ struct _GtkFileChooserSettings guint show_type_column : 1; guint show_thumbnails : 1; guint show_preview_panel : 1; + guint play_media : 1; + + gchar *media_backend; }; struct _GtkFileChooserSettingsClass @@ -104,6 +107,13 @@ gboolean _gtk_file_chooser_settings_get_show_preview_panel (GtkFileChooserSettin void _gtk_file_chooser_settings_set_show_preview_panel (GtkFileChooserSettings *settings, gboolean show_preview_panel); +gboolean _gtk_file_chooser_settings_get_play_media (GtkFileChooserSettings *settings); +void _gtk_file_chooser_settings_set_play_media (GtkFileChooserSettings *settings, + gboolean play_media); +const gchar *_gtk_file_chooser_settings_get_media_backend (GtkFileChooserSettings *settings); +void _gtk_file_chooser_settings_set_media_backend (GtkFileChooserSettings *settings, + const gchar *backend); + gint _gtk_file_chooser_settings_get_thumbnail_max_size (GtkFileChooserSettings *settings, gboolean remote); void _gtk_file_chooser_settings_set_thumbnail_max_size (GtkFileChooserSettings *settings, diff --git a/gtk/gtkfilemedia.c b/gtk/gtkfilemedia.c new file mode 100644 index 000000000..92c9de23b --- /dev/null +++ b/gtk/gtkfilemedia.c @@ -0,0 +1,1191 @@ +/* GTK - The GIMP Toolkit + * gtkfilemedia.c: sound and video in the preview of the file chooser + * + * Nothing here is needed to build GTK+, nor to run it: what plays is + * what is installed, found when it is asked for. + * + * - GStreamer 1.x, opened as a library, on every system it is + * installed on. Video comes back as pictures of the size of the + * preview, scaled by GStreamer. + * - xine-lib 1.2, opened as a library, with its "raw" video output. + * - FFmpeg, as the programs ffmpeg (the pictures, through a pipe) + * and ffplay (the sound). + * - On Windows, the Media Control Interface every version since 3.1 + * has, which draws video into a window of its own over the preview. + * + * Nothing starts before the user asks; stopping stops it all. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + */ + +#include "config.h" + +#include +#include +#include + +#include "gtkfilemedia.h" +#include "gtkwidget.h" + +#ifdef G_OS_WIN32 +#include +#include "win32/gdkwin32.h" +#else +#include +#include +#include +#include +#endif + +#define FRAME_RATE 25 + +typedef struct _Backend Backend; + +struct _GtkFileMedia +{ + const Backend *backend; + GtkFileMediaFrame frame; + GtkFileMediaDone done; + gpointer data; + gint size; + GtkWidget *area; + gboolean over; + + /* the newest picture, from another thread or a pipe */ + GMutex lock; + guchar *pending; + gint width, height; + gboolean fresh; + guint idle; + GdkPixbuf *pixbuf; + + /* GStreamer, xine */ + gpointer player, bus, stream, port, audio, visual; + + /* FFmpeg */ + GPid video_pid, audio_pid; + guint video_watch, audio_watch, read_watch, tick; + GIOChannel *channel; + GByteArray *bytes; + gboolean paused, video_done; + + /* MCI */ + gchar *alias; + gulong allocate_id; + guint poll; +}; + +struct _Backend +{ + const gchar *id; + const gchar *name; + gboolean (*available) (void); + gboolean (*start) (GtkFileMedia *media, GFile *file); + void (*pause) (GtkFileMedia *media, gboolean pause); + void (*stop) (GtkFileMedia *media); +}; + +/* Pictures */ + +static gboolean +frame_idle (gpointer data) +{ + GtkFileMedia *media = data; + gint width, height, y; + + g_mutex_lock (&media->lock); + media->idle = 0; + if (!media->fresh) + { + g_mutex_unlock (&media->lock); + return FALSE; + } + media->fresh = FALSE; + + /* not bigger than the preview: xine gives them as they are */ + width = media->width; + height = media->height; + if (width > media->size || height > media->size) + { + if (width > height) + { + height = MAX (1, height * media->size / width); + width = media->size; + } + else + { + width = MAX (1, width * media->size / height); + height = media->size; + } + } + + if (media->pixbuf == NULL || + gdk_pixbuf_get_width (media->pixbuf) != width || + gdk_pixbuf_get_height (media->pixbuf) != height) + { + if (media->pixbuf) + g_object_unref (media->pixbuf); + media->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height); + } + + if (width == media->width && height == media->height) + for (y = 0; y < height; y++) + memcpy (gdk_pixbuf_get_pixels (media->pixbuf) + y * gdk_pixbuf_get_rowstride (media->pixbuf), + media->pending + y * width * 3, width * 3); + else + { + GdkPixbuf *whole = gdk_pixbuf_new_from_data (media->pending, GDK_COLORSPACE_RGB, FALSE, 8, + media->width, media->height, media->width * 3, + NULL, NULL); + + gdk_pixbuf_scale (whole, media->pixbuf, 0, 0, width, height, 0, 0, + (gdouble) width / media->width, (gdouble) height / media->height, + GDK_INTERP_BILINEAR); + g_object_unref (whole); + } + g_mutex_unlock (&media->lock); + + media->frame (media->pixbuf, media->data); + + return FALSE; +} + +/* A picture of @width × @height RGB pixels, rows @stride apart; from + * any thread */ +static void +frame_put (GtkFileMedia *media, + const guchar *rgb, + gint width, + gint height, + gint stride) +{ + gint y; + + g_mutex_lock (&media->lock); + if (width != media->width || height != media->height) + { + g_free (media->pending); + media->pending = g_malloc ((gsize) width * height * 3); + media->width = width; + media->height = height; + } + for (y = 0; y < height; y++) + memcpy (media->pending + (gsize) y * width * 3, rgb + (gsize) y * stride, width * 3); + media->fresh = TRUE; + if (media->idle == 0) + media->idle = gdk_threads_add_idle (frame_idle, media); + g_mutex_unlock (&media->lock); +} + + +/* The end, told once, in the main thread; @media may be gone after */ +static void +media_over (GtkFileMedia *media, + GtkFileMediaEnd end) +{ + if (media->over) + return; + media->over = TRUE; + media->done (end, media->data); +} + +static GModule * +open_library (const gchar * const *names) +{ + gint i; + + for (i = 0; names[i]; i++) + { + GModule *module = g_module_open (names[i], G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL); + + if (module) + return module; + } + + return NULL; +} + +static gboolean +find_symbols (GModule *module, + const gchar * const *names, + gpointer *symbols) +{ + gint i; + + for (i = 0; names[i]; i++) + if (!g_module_symbol (module, names[i], &symbols[i])) + return FALSE; + + return TRUE; +} + +/* GStreamer */ + +typedef struct +{ + gpointer memory; + gint flags; + guint8 *data; + gsize size; + gsize maxsize; + gpointer user_data[4]; + gpointer reserved[4]; +} GstMap; + +static struct +{ + gboolean (*init_check) (int *, char ***, GError **); + gpointer (*element_factory_make) (const gchar *, const gchar *); + gpointer (*parse_bin) (const gchar *, gboolean, GError **); + gpointer (*bin_get_by_name) (gpointer, const gchar *); + gint (*element_set_state) (gpointer, gint); + gpointer (*element_get_bus) (gpointer); + void (*bus_add_signal_watch) (gpointer); + void (*bus_remove_signal_watch) (gpointer); + gpointer (*sample_get_buffer) (gpointer); + gpointer (*sample_get_caps) (gpointer); + gpointer (*caps_get_structure) (gpointer, guint); + gboolean (*structure_get_int) (gpointer, const gchar *, gint *); + gboolean (*buffer_map) (gpointer, GstMap *, gint); + void (*buffer_unmap) (gpointer, GstMap *); + void (*mini_object_unref) (gpointer); + void (*object_unref) (gpointer); +} gst; + +enum { GST_NULL = 1, GST_PAUSED = 3, GST_PLAYING = 4 }; + +/* Only a GStreamer that uses our GLib: the one of the GStreamer + * installers for Windows (of the MSVC build) and macOS (the framework) + * has its own, and two of them in one program do not go together */ +static gboolean +gst_same_glib (GModule *module) +{ +#ifdef G_OS_WIN32 + return GetModuleHandleW (L"gobject-2.0-0.dll") == NULL; +#else + gpointer object_new; + + return g_module_symbol (module, "g_object_new", &object_new) && + object_new == (gpointer) g_object_new; +#endif +} + +static gboolean +gst_available (void) +{ + static const gchar * const libraries[] = { +#if defined (G_OS_WIN32) + "libgstreamer-1.0-0.dll", +#elif defined (__APPLE__) + "libgstreamer-1.0.0.dylib", + "/opt/homebrew/lib/libgstreamer-1.0.0.dylib", + "/opt/local/lib/libgstreamer-1.0.0.dylib", +#else + "libgstreamer-1.0.so.0", +#endif + NULL + }; + static const gchar * const symbols[] = { + "gst_init_check", "gst_element_factory_make", "gst_parse_bin_from_description", + "gst_bin_get_by_name", "gst_element_set_state", "gst_element_get_bus", + "gst_bus_add_signal_watch", "gst_bus_remove_signal_watch", "gst_sample_get_buffer", + "gst_sample_get_caps", "gst_caps_get_structure", "gst_structure_get_int", + "gst_buffer_map", "gst_buffer_unmap", "gst_mini_object_unref", "gst_object_unref", NULL + }; + static gsize looked_up; + static gboolean found; + + if (g_once_init_enter (&looked_up)) + { + GModule *module = open_library (libraries); + + found = module && find_symbols (module, symbols, (gpointer *) &gst) && + gst_same_glib (module); + if (module && !found) + g_module_close (module); + g_once_init_leave (&looked_up, 1); + } + + return found; +} + +/* In a thread of GStreamer */ +static gint +gst_new_sample (gpointer sink, + gpointer data) +{ + GtkFileMedia *media = data; + gpointer sample = NULL, buffer, structure; + gint width = 0, height = 0; + GstMap map; + + g_signal_emit_by_name (sink, "pull-sample", &sample); + if (sample == NULL) + return 0; + + structure = gst.caps_get_structure (gst.sample_get_caps (sample), 0); + buffer = gst.sample_get_buffer (sample); + if (structure && buffer && + gst.structure_get_int (structure, "width", &width) && + gst.structure_get_int (structure, "height", &height) && + width > 0 && height > 0 && + gst.buffer_map (buffer, &map, 1 /* GST_MAP_READ */)) + { + /* rows of RGB at 4 bytes */ + gint stride = (width * 3 + 3) & ~3; + + if (map.size >= (gsize) stride * (height - 1) + width * 3) + frame_put (media, map.data, width, height, stride); + gst.buffer_unmap (buffer, &map); + } + gst.mini_object_unref (sample); + + return 0; /* GST_FLOW_OK */ +} + +static void +gst_eos (gpointer bus, + gpointer message, + gpointer data) +{ + media_over (data, GTK_FILE_MEDIA_ENDED); +} + +static void +gst_error (gpointer bus, + gpointer message, + gpointer data) +{ + media_over (data, GTK_FILE_MEDIA_FAILED); +} + +static gboolean +gst_start (GtkFileMedia *media, + GFile *file) +{ + static gsize initialized; + static gboolean ready; + gchar *description, *uri; + gpointer sink, frames; + + if (g_once_init_enter (&initialized)) + { + ready = gst.init_check (NULL, NULL, NULL); + g_once_init_leave (&initialized, 1); + } + if (!ready) + return FALSE; + + media->player = gst.element_factory_make ("playbin", NULL); + if (media->player == NULL) + return FALSE; + + /* pictures of the size of the preview, keeping their shape */ + description = g_strdup_printf ("videoconvert ! videoscale ! " + "video/x-raw,format=RGB,pixel-aspect-ratio=1/1,width=[1,%d],height=[1,%d] ! " + "appsink name=frames max-buffers=2 drop=true emit-signals=true", + media->size, media->size); + sink = gst.parse_bin (description, TRUE, NULL); + g_free (description); + if (sink == NULL) + { + gst.object_unref (media->player); + media->player = NULL; + return FALSE; + } + frames = gst.bin_get_by_name (sink, "frames"); + if (frames) + { + g_signal_connect (frames, "new-sample", G_CALLBACK (gst_new_sample), media); + gst.object_unref (frames); + } + + uri = g_file_get_uri (file); + g_object_set (media->player, "uri", uri, "video-sink", sink, NULL); + g_free (uri); + + media->bus = gst.element_get_bus (media->player); + gst.bus_add_signal_watch (media->bus); + g_signal_connect (media->bus, "message::eos", G_CALLBACK (gst_eos), media); + g_signal_connect (media->bus, "message::error", G_CALLBACK (gst_error), media); + + if (gst.element_set_state (media->player, GST_PLAYING) == 0 /* FAILURE */) + return FALSE; + + return TRUE; +} + +static void +gst_pause (GtkFileMedia *media, + gboolean pause) +{ + gst.element_set_state (media->player, pause ? GST_PAUSED : GST_PLAYING); +} + +static void +gst_stop (GtkFileMedia *media) +{ + if (media->bus) + { + g_signal_handlers_disconnect_by_func (media->bus, gst_eos, media); + g_signal_handlers_disconnect_by_func (media->bus, gst_error, media); + gst.bus_remove_signal_watch (media->bus); + gst.object_unref (media->bus); + media->bus = NULL; + } + if (media->player) + { + /* waits for its threads */ + gst.element_set_state (media->player, GST_NULL); + gst.object_unref (media->player); + media->player = NULL; + } +} + +#ifndef G_OS_WIN32 + +/* xine-lib 1.2 */ + +typedef struct +{ + void *user_data; + int supported_formats; + void (*raw_output_cb) (void *, int, int, int, double, void *, void *, void *); + void (*raw_overlay_cb) (void *, int, void *); +} XineRawVisual; + +typedef struct +{ + int type; + void *stream; + void *data; + int data_length; +} XineEvent; + +#define XINE_VISUAL_TYPE_RAW 12 +#define XINE_VORAW_RGB 4 +#define XINE_PARAM_SPEED 1 +#define XINE_SPEED_PAUSE 0 +#define XINE_SPEED_NORMAL 4 +#define XINE_EVENT_UI_PLAYBACK_FINISHED 1 + +static struct +{ + gpointer (*new_) (void); + void (*init) (gpointer); + void (*exit) (gpointer); + gpointer (*open_video_driver) (gpointer, const char *, int, void *); + gpointer (*open_audio_driver) (gpointer, const char *, void *); + void (*close_video_driver) (gpointer, gpointer); + void (*close_audio_driver) (gpointer, gpointer); + gpointer (*stream_new) (gpointer, gpointer, gpointer); + int (*open) (gpointer, const char *); + int (*play) (gpointer, int, int); + void (*close) (gpointer); + void (*dispose) (gpointer); + void (*set_param) (gpointer, int, int); + gpointer (*event_new_queue) (gpointer); + void (*event_dispose_queue) (gpointer); + void (*event_create_listener_thread) (gpointer, void (*) (void *, const XineEvent *), void *); +} xine; + +static gboolean +xine_available (void) +{ + static const gchar * const libraries[] = { "libxine.so.2", NULL }; + static const gchar * const symbols[] = { + "xine_new", "xine_init", "xine_exit", "xine_open_video_driver", + "xine_open_audio_driver", "xine_close_video_driver", "xine_close_audio_driver", + "xine_stream_new", "xine_open", "xine_play", "xine_close", "xine_dispose", + "xine_set_param", "xine_event_new_queue", "xine_event_dispose_queue", + "xine_event_create_listener_thread", NULL + }; + static gsize looked_up; + static gboolean found; + + if (g_once_init_enter (&looked_up)) + { + GModule *module = open_library (libraries); + + found = module && find_symbols (module, symbols, (gpointer *) &xine); + if (module && !found) + g_module_close (module); + g_once_init_leave (&looked_up, 1); + } + + return found; +} + +/* In a thread of xine: packed RGB, as it is */ +static void +xine_frame (void *data, + int format, + int width, + int height, + double aspect, + void *rgb, + void *unused1, + void *unused2) +{ + if (format == XINE_VORAW_RGB && width > 0 && height > 0) + frame_put (data, rgb, width, height, width * 3); +} + +static void +xine_overlay (void *data, + int n, + void *overlays) +{ +} + +/* xine's end, told from its thread */ +static gboolean +done_idle (gpointer data) +{ + GtkFileMedia *media = data; + + g_mutex_lock (&media->lock); + media->tick = 0; + g_mutex_unlock (&media->lock); + media_over (media, GTK_FILE_MEDIA_ENDED); + + return FALSE; +} + +/* In a thread of xine */ +static void +xine_event (void *data, + const XineEvent *event) +{ + GtkFileMedia *media = data; + + if (event->type == XINE_EVENT_UI_PLAYBACK_FINISHED) + { + g_mutex_lock (&media->lock); + if (media->tick == 0) + media->tick = gdk_threads_add_idle (done_idle, media); + g_mutex_unlock (&media->lock); + } +} + +static gboolean +xine_start (GtkFileMedia *media, + GFile *file) +{ + XineRawVisual *visual = g_new0 (XineRawVisual, 1); + gchar *mrl; + + media->visual = visual; + media->player = xine.new_ (); + if (media->player == NULL) + return FALSE; + xine.init (media->player); + + visual->user_data = media; + visual->supported_formats = XINE_VORAW_RGB; + visual->raw_output_cb = xine_frame; + visual->raw_overlay_cb = xine_overlay; + media->port = xine.open_video_driver (media->player, "raw", XINE_VISUAL_TYPE_RAW, visual); + media->audio = xine.open_audio_driver (media->player, NULL, NULL); + media->stream = xine.stream_new (media->player, media->audio, media->port); + if (media->stream == NULL) + return FALSE; + + media->bus = xine.event_new_queue (media->stream); + if (media->bus) + xine.event_create_listener_thread (media->bus, xine_event, media); + + mrl = g_file_get_uri (file); + if (!xine.open (media->stream, mrl) || !xine.play (media->stream, 0, 0)) + { + g_free (mrl); + return FALSE; + } + g_free (mrl); + + return TRUE; +} + +static void +xine_pause (GtkFileMedia *media, + gboolean pause) +{ + xine.set_param (media->stream, XINE_PARAM_SPEED, pause ? XINE_SPEED_PAUSE : XINE_SPEED_NORMAL); +} + +static void +xine_stop (GtkFileMedia *media) +{ + if (media->stream) + { + xine.close (media->stream); + /* ends the listener thread */ + if (media->bus) + xine.event_dispose_queue (media->bus); + xine.dispose (media->stream); + } + if (media->port) + xine.close_video_driver (media->player, media->port); + if (media->audio) + xine.close_audio_driver (media->player, media->audio); + if (media->player) + xine.exit (media->player); + g_free (media->visual); + media->stream = media->bus = media->port = media->audio = media->player = media->visual = NULL; +} + +/* FFmpeg, as programs: ffmpeg decodes the pictures, at the size of the + * preview and FRAME_RATE a second, into a pipe; ffplay plays the sound */ + +static gboolean +ffmpeg_available (void) +{ + static gsize looked_up; + static gboolean found; + + if (g_once_init_enter (&looked_up)) + { + gchar *ffmpeg = g_find_program_in_path ("ffmpeg"); + gchar *ffplay = g_find_program_in_path ("ffplay"); + + found = ffmpeg && ffplay; + g_free (ffmpeg); + g_free (ffplay); + g_once_init_leave (&looked_up, 1); + } + + return found; +} + +static gboolean ffmpeg_read (GIOChannel *channel, GIOCondition condition, gpointer data); + +static void +ffmpeg_check_over (GtkFileMedia *media) +{ + if (media->video_pid == 0 && media->audio_pid == 0 && media->video_done && + (media->bytes == NULL || media->bytes->len == 0)) + media_over (media, GTK_FILE_MEDIA_ENDED); +} + +static void +ffmpeg_exited (GPid pid, + gint status, + gpointer data) +{ + GtkFileMedia *media = data; + + g_spawn_close_pid (pid); + if (pid == media->video_pid) + { + media->video_pid = 0; + media->video_watch = 0; + } + else + { + media->audio_pid = 0; + media->audio_watch = 0; + } + ffmpeg_check_over (media); +} + +/* One picture of PPM (P6) at the start of the bytes read: its size, and + * where its pixels start; FALSE while it is not all there */ +static gboolean +ffmpeg_ppm (GByteArray *bytes, + gint *width, + gint *height, + gsize *start) +{ + gint fields[3], n = 0; + gsize i = 2; + + if (bytes->len < 2 || bytes->data[0] != 'P' || bytes->data[1] != '6') + return FALSE; + + while (n < 3 && i < bytes->len) + { + if (g_ascii_isdigit (bytes->data[i])) + { + fields[n] = 0; + while (i < bytes->len && g_ascii_isdigit (bytes->data[i]) && fields[n] < 100000) + fields[n] = fields[n] * 10 + bytes->data[i++] - '0'; + n++; + } + else + i++; + } + if (n < 3 || i >= bytes->len || fields[0] <= 0 || fields[1] <= 0 || fields[2] != 255) + return FALSE; + + *width = fields[0]; + *height = fields[1]; + *start = i + 1; /* one white space after the maximum */ + + return bytes->len >= *start + (gsize) *width * *height * 3; +} + +/* One picture a tick, from what was read; the pipe is read on while + * less than two are there, and ffmpeg waits meanwhile */ +static gboolean +ffmpeg_tick (gpointer data) +{ + GtkFileMedia *media = data; + gint width, height; + gsize start; + + if (ffmpeg_ppm (media->bytes, &width, &height, &start)) + { + frame_put (media, media->bytes->data + start, width, height, width * 3); + g_byte_array_remove_range (media->bytes, 0, start + (gsize) width * height * 3); + } + else if (media->video_done) + { + /* all shown, but for what is no whole picture */ + g_byte_array_set_size (media->bytes, 0); + media->tick = 0; + ffmpeg_check_over (media); + return FALSE; + } + + if (media->read_watch == 0 && media->channel && !media->video_done) + media->read_watch = g_io_add_watch (media->channel, G_IO_IN | G_IO_HUP | G_IO_ERR, + ffmpeg_read, media); + + return TRUE; +} + +static gboolean +ffmpeg_read (GIOChannel *channel, + GIOCondition condition, + gpointer data) +{ + GtkFileMedia *media = data; + guchar buffer[16384]; + gssize n; + gint width, height; + gsize start; + + n = read (g_io_channel_unix_get_fd (channel), buffer, sizeof buffer); + if (n > 0) + g_byte_array_append (media->bytes, buffer, n); + else if (n == 0 || (errno != EAGAIN && errno != EINTR)) + { + media->video_done = TRUE; + media->read_watch = 0; + return FALSE; + } + + /* two pictures there: enough for now */ + if (ffmpeg_ppm (media->bytes, &width, &height, &start) && + media->bytes->len >= 2 * (start + (gsize) width * height * 3)) + { + media->read_watch = 0; + return FALSE; + } + + return TRUE; +} + +static gboolean +ffmpeg_start (GtkFileMedia *media, + GFile *file) +{ + gchar *path = g_file_get_path (file); + gchar *filter = g_strdup_printf ("fps=%d,scale=w=%d:h=%d:force_original_aspect_ratio=decrease", + FRAME_RATE, media->size, media->size); + gchar *video[] = { "ffmpeg", "-nostdin", "-loglevel", "quiet", "-i", path, "-an", "-sn", + "-vf", filter, "-f", "image2pipe", "-vcodec", "ppm", "pipe:1", NULL }; + gchar *audio[] = { "ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet", "-vn", path, NULL }; + gint out = -1; + gboolean started; + + started = path != NULL && + g_spawn_async_with_pipes (NULL, audio, NULL, + G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD | + G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &media->audio_pid, NULL, NULL, NULL, NULL); + if (started) + media->audio_watch = g_child_watch_add (media->audio_pid, ffmpeg_exited, media); + + if (started && + g_spawn_async_with_pipes (NULL, video, NULL, + G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD | + G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &media->video_pid, NULL, &out, NULL, NULL)) + { + media->video_watch = g_child_watch_add (media->video_pid, ffmpeg_exited, media); + fcntl (out, F_SETFL, fcntl (out, F_GETFL) | O_NONBLOCK); + media->channel = g_io_channel_unix_new (out); + g_io_channel_set_close_on_unref (media->channel, TRUE); + media->bytes = g_byte_array_new (); + media->tick = gdk_threads_add_timeout (1000 / FRAME_RATE, ffmpeg_tick, media); + } + else + media->video_done = TRUE; + + g_free (filter); + g_free (path); + + return started; +} + +static void +ffmpeg_pause (GtkFileMedia *media, + gboolean pause) +{ + if (media->video_pid) + kill (media->video_pid, pause ? SIGSTOP : SIGCONT); + if (media->audio_pid) + kill (media->audio_pid, pause ? SIGSTOP : SIGCONT); + + /* the pictures wait too */ + if (pause && media->tick) + { + g_source_remove (media->tick); + media->tick = 0; + } + else if (!pause && media->tick == 0 && media->bytes) + media->tick = gdk_threads_add_timeout (1000 / FRAME_RATE, ffmpeg_tick, media); +} + +static void +ffmpeg_stop (GtkFileMedia *media) +{ + GPid pids[2] = { media->video_pid, media->audio_pid }; + guint watches[3] = { media->video_watch, media->audio_watch, media->read_watch }; + gint i; + + for (i = 0; i < 3; i++) + if (watches[i]) + g_source_remove (watches[i]); + if (media->tick) + g_source_remove (media->tick); + for (i = 0; i < 2; i++) + if (pids[i]) + { + kill (pids[i], SIGKILL); + kill (pids[i], SIGCONT); + /* reaped by GLib */ + g_child_watch_add (pids[i], (GChildWatchFunc) g_spawn_close_pid, NULL); + } + if (media->channel) + g_io_channel_unref (media->channel); + if (media->bytes) + g_byte_array_free (media->bytes, TRUE); + media->video_pid = media->audio_pid = 0; + media->video_watch = media->audio_watch = media->read_watch = media->tick = 0; + media->channel = NULL; + media->bytes = NULL; +} + +#endif /* !G_OS_WIN32 */ + +/* Windows: the Media Control Interface, with its video window over the + * preview */ + +#ifdef G_OS_WIN32 + +typedef MCIERROR (WINAPI *MciSendStringFunc) (LPCWSTR, LPWSTR, UINT, HWND); + +static MciSendStringFunc mci_send_string; + +static gboolean +mci_available (void) +{ + static gsize looked_up; + + if (g_once_init_enter (&looked_up)) + { + HMODULE winmm = LoadLibraryW (L"winmm.dll"); + + if (winmm) + mci_send_string = (MciSendStringFunc) GetProcAddress (winmm, "mciSendStringW"); + g_once_init_leave (&looked_up, 1); + } + + return mci_send_string != NULL; +} + +/* Sends a command, with "%s" for the alias; the answer in @reply */ +static gboolean +mci_command (GtkFileMedia *media, + const gchar *format, + const gchar *argument, + gchar **reply) +{ + gchar *command = g_strdup_printf (format, media->alias, argument); + gunichar2 *wcommand = g_utf8_to_utf16 (command, -1, NULL, NULL, NULL); + wchar_t answer[128]; + MCIERROR error; + + answer[0] = 0; + error = wcommand ? mci_send_string (wcommand, answer, G_N_ELEMENTS (answer), NULL) : 1; + if (reply) + *reply = error ? NULL : g_utf16_to_utf8 (answer, -1, NULL, NULL, NULL); + g_free (wcommand); + g_free (command); + + return error == 0; +} + +/* The video window over the preview, keeping the shape of the video */ +static void +mci_place (GtkWidget *area, + GtkAllocation *allocation, + GtkFileMedia *media) +{ + GtkWidget *toplevel = gtk_widget_get_toplevel (area); + gchar *source = NULL, *place; + gint x, y, sx, sy, width, height, w, h, scale; + + if (!mci_command (media, "where %s source", NULL, &source) || source == NULL || + sscanf (source, "%d %d %d %d", &sx, &sy, &w, &h) != 4 || w <= 0 || h <= 0 || + !gtk_widget_translate_coordinates (area, toplevel, 0, 0, &x, &y)) + { + g_free (source); + return; + } + g_free (source); + + width = area->allocation.width; + height = area->allocation.height; + if (w * height > h * width) + height = MAX (1, h * width / w); + else + width = MAX (1, w * height / h); + x += (area->allocation.width - width) / 2; + y += (area->allocation.height - height) / 2; + + scale = gdk_window_get_scale_factor (toplevel->window); + place = g_strdup_printf ("%d %d %d %d", x * scale, y * scale, width * scale, height * scale); + mci_command (media, "put %s window at %s", place, NULL); + g_free (place); +} + +static gboolean +mci_poll (gpointer data) +{ + GtkFileMedia *media = data; + gchar *mode = NULL; + + mci_command (media, "status %s mode", NULL, &mode); + if (mode == NULL || strcmp (mode, "stopped") == 0) + { + g_free (mode); + media->poll = 0; + media_over (media, mode ? GTK_FILE_MEDIA_ENDED : GTK_FILE_MEDIA_FAILED); + return FALSE; + } + g_free (mode); + + return TRUE; +} + +static gboolean +mci_start (GtkFileMedia *media, + GFile *file) +{ + static guint serial; + GtkWidget *toplevel = gtk_widget_get_toplevel (media->area); + gchar *path = g_file_get_path (file), *open; + + if (path == NULL || !gtk_widget_get_realized (toplevel)) + { + g_free (path); + return FALSE; + } + + media->alias = g_strdup_printf ("gtkpreview%u", ++serial); + open = g_strdup_printf ("open \"%s\" type mpegvideo alias %%s parent %lu style child", + path, (gulong) (gsize) gdk_win32_drawable_get_handle (toplevel->window)); + g_free (path); + if (!mci_command (media, open, NULL, NULL)) + { + g_free (open); + g_free (media->alias); + media->alias = NULL; + return FALSE; + } + g_free (open); + + mci_place (media->area, &media->area->allocation, media); + media->allocate_id = g_signal_connect (media->area, "size-allocate", G_CALLBACK (mci_place), media); + if (!mci_command (media, "play %s", NULL, NULL)) + return FALSE; + media->poll = gdk_threads_add_timeout (250, mci_poll, media); + + return TRUE; +} + +static void +mci_pause (GtkFileMedia *media, + gboolean pause) +{ + mci_command (media, pause ? "pause %s" : "resume %s", NULL, NULL); +} + +static void +mci_stop (GtkFileMedia *media) +{ + if (media->poll) + g_source_remove (media->poll); + if (media->allocate_id) + g_signal_handler_disconnect (media->area, media->allocate_id); + if (media->alias) + mci_command (media, "close %s", NULL, NULL); + g_free (media->alias); + media->alias = NULL; + media->poll = 0; + media->allocate_id = 0; +} + +#endif /* G_OS_WIN32 */ + +static const Backend backends[] = { + { "gstreamer", "GStreamer", gst_available, gst_start, gst_pause, gst_stop }, +#ifndef G_OS_WIN32 + { "xine", "xine", xine_available, xine_start, xine_pause, xine_stop }, + { "ffmpeg", "FFmpeg", ffmpeg_available, ffmpeg_start, ffmpeg_pause, ffmpeg_stop }, +#else + { "mci", "Windows Media (MCI)", mci_available, mci_start, mci_pause, mci_stop }, +#endif +}; + +/* "auto" and the ones there are on this system */ +const gchar * const * +_gtk_file_media_backends (void) +{ + static const gchar *ids[G_N_ELEMENTS (backends) + 2]; + + if (ids[0] == NULL) + { + gint i; + + for (i = 0; i < G_N_ELEMENTS (backends); i++) + ids[i + 1] = backends[i].id; + ids[0] = "auto"; + } + + return ids; +} + +const gchar * +_gtk_file_media_backend_name (const gchar *backend) +{ + gint i; + + for (i = 0; i < G_N_ELEMENTS (backends); i++) + if (g_strcmp0 (backends[i].id, backend) == 0) + return backends[i].name; + + return NULL; +} + +gboolean +_gtk_file_media_available (const gchar *backend) +{ + gint i; + + for (i = 0; i < G_N_ELEMENTS (backends); i++) + if ((g_strcmp0 (backend, "auto") == 0 || g_strcmp0 (backends[i].id, backend) == 0) && + backends[i].available ()) + return TRUE; + + return FALSE; +} + +/* Whether @info is of sound or video */ +gboolean +_gtk_file_media_plays (GFileInfo *info) +{ + const gchar *type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE); + gchar *mime; + gboolean plays; + + if (type == NULL) + return FALSE; + mime = g_content_type_get_mime_type (type); + plays = mime && (g_str_has_prefix (mime, "audio/") || g_str_has_prefix (mime, "video/")); + g_free (mime); + + return plays; +} + +static void +media_free (GtkFileMedia *media) +{ + if (media->idle) + g_source_remove (media->idle); + if (media->pixbuf) + g_object_unref (media->pixbuf); + g_free (media->pending); + g_mutex_clear (&media->lock); + g_object_unref (media->area); + g_slice_free (GtkFileMedia, media); +} + +/* Plays @file with @backend, or the first that can if "auto"; @frame + * gets the pictures, at most @size big, @done the end. NULL if none + * could. */ +GtkFileMedia * +_gtk_file_media_play (GFile *file, + const gchar *backend, + GtkWidget *area, + gint size, + GtkFileMediaFrame frame, + GtkFileMediaDone done, + gpointer data) +{ + GtkFileMedia *media = g_slice_new0 (GtkFileMedia); + gint i; + + g_mutex_init (&media->lock); + media->frame = frame; + media->done = done; + media->data = data; + media->size = size; + media->area = g_object_ref (area); + + for (i = 0; i < G_N_ELEMENTS (backends); i++) + if ((g_strcmp0 (backend, "auto") == 0 || g_strcmp0 (backends[i].id, backend) == 0) && + backends[i].available ()) + { + media->backend = &backends[i]; + if (backends[i].start (media, file)) + return media; + backends[i].stop (media); + media->backend = NULL; + } + + media_free (media); + + return NULL; +} + +void +_gtk_file_media_pause (GtkFileMedia *media, + gboolean pause) +{ + if (media->paused != pause) + media->backend->pause (media, pause); + media->paused = pause; +} + +/* Stops and frees @media; no callback comes after */ +void +_gtk_file_media_stop (GtkFileMedia *media) +{ + media->over = TRUE; + media->backend->stop (media); + + g_mutex_lock (&media->lock); + if (media->tick) /* xine's end */ + g_source_remove (media->tick); + media->tick = 0; + g_mutex_unlock (&media->lock); + + media_free (media); +} diff --git a/gtk/gtkfilemedia.h b/gtk/gtkfilemedia.h new file mode 100644 index 000000000..be550d3f9 --- /dev/null +++ b/gtk/gtkfilemedia.h @@ -0,0 +1,50 @@ +/* GTK - The GIMP Toolkit + * gtkfilemedia.h: sound and video in the preview of the file chooser + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + */ + +#ifndef __GTK_FILE_MEDIA_H__ +#define __GTK_FILE_MEDIA_H__ + +#include +#include + +G_BEGIN_DECLS + +typedef struct _GtkFileMedia GtkFileMedia; + +typedef enum +{ + GTK_FILE_MEDIA_ENDED, + GTK_FILE_MEDIA_FAILED +} GtkFileMediaEnd; + +/* @frame is the same picture each time, changed in place */ +typedef void (*GtkFileMediaFrame) (GdkPixbuf *frame, + gpointer data); +typedef void (*GtkFileMediaDone) (GtkFileMediaEnd end, + gpointer data); + +const gchar * const *_gtk_file_media_backends (void); +const gchar *_gtk_file_media_backend_name (const gchar *backend); +gboolean _gtk_file_media_available (const gchar *backend); +gboolean _gtk_file_media_plays (GFileInfo *info); + +GtkFileMedia *_gtk_file_media_play (GFile *file, + const gchar *backend, + GtkWidget *area, + gint size, + GtkFileMediaFrame frame, + GtkFileMediaDone done, + gpointer data); +void _gtk_file_media_pause (GtkFileMedia *media, + gboolean pause); +void _gtk_file_media_stop (GtkFileMedia *media); + +G_END_DECLS + +#endif /* __GTK_FILE_MEDIA_H__ */