[fractal] Fix how to get the duration of audio/video



commit b6d6e3b2a989e1eb70eee5e5184775eebf993314
Author: sonjita <sonjaleaheinze gmail com>
Date:   Mon Jan 27 17:17:57 2020 +0100

    Fix how to get the duration of audio/video
    
    Before, the gtk main thread sometimes froze when sending an audio or video
    attachment. That was due to trying to get the duration of the audio/video
    using gstreamer_editing_services. Now, using gstreamer_pbutils,
    that doesn't happen anymore.
    
    Furthermore, when creating an audio player in the room history, the
    duration of the audio was displayed as 00:00 until play was hit. Now,
    the duration of the audio gets displayed correctly from the moment of the
    creation of the player on.

 Cargo.lock                               |  1 +
 fractal-gtk/Cargo.toml                   |  1 +
 fractal-gtk/src/appop/message.rs         | 33 ++++++++++++++++++--------------
 fractal-gtk/src/widgets/inline_player.rs | 17 +++++++++++++++-
 fractal-gtk/src/widgets/mod.rs           |  2 +-
 meson.build                              |  1 +
 6 files changed, 39 insertions(+), 16 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 1dcbadfb..82b3debd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -561,6 +561,7 @@ dependencies = [
  "gspell 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "gstreamer 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "gstreamer-editing-services 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "gstreamer-pbutils 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "gstreamer-player 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "gtk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "html2pango 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/fractal-gtk/Cargo.toml b/fractal-gtk/Cargo.toml
index 682c5607..ca411ac1 100644
--- a/fractal-gtk/Cargo.toml
+++ b/fractal-gtk/Cargo.toml
@@ -16,6 +16,7 @@ gdk = "0.11.0"
 gdk-pixbuf = "0.7.0"
 gdk-pixbuf-sys = "0.9.0"
 gstreamer-editing-services = "0.14.0"
+gstreamer-pbutils = "0.14.0"
 glib = "0.8.0"
 html2pango = "0.3.1"
 itertools = "0.8.0"
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index e15cdcf9..d1c8365a 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -1,8 +1,6 @@
 use comrak::{markdown_to_html, ComrakOptions};
 use gdk_pixbuf::Pixbuf;
 use gio::prelude::FileExt;
-use gstreamer_editing_services::prelude::*;
-use gstreamer_editing_services::UriClipAsset;
 use gtk;
 use gtk::prelude::*;
 use lazy_static::lazy_static;
@@ -585,20 +583,27 @@ fn get_image_media_info(file: &str, mimetype: &str) -> Option<JsonValue> {
 }
 
 fn get_audio_video_media_info(file: &str, mimetype: &str) -> Option<JsonValue> {
-    let nfile = format!("file://{}", file);
-    let uri = UriClipAsset::request_sync(&nfile).ok()?;
-    let duration = uri.get_duration().mseconds()?;
     let size = fs::metadata(file).ok()?.len();
 
-    let info = json!({
-        "info": {
-            "size": size,
-            "mimetype": mimetype,
-            "duration": duration,
-        }
-    });
-
-    Some(info)
+    if let Some(duration) = widgets::inline_player::get_media_duration(file)
+        .ok()
+        .and_then(|d| d.mseconds())
+    {
+        Some(json!({
+            "info": {
+                "size": size,
+                "mimetype": mimetype,
+                "duration": duration,
+            }
+        }))
+    } else {
+        Some(json!({
+            "info": {
+                "size": size,
+                "mimetype": mimetype,
+            }
+        }))
+    }
 }
 
 fn get_file_media_info(file: &str, mimetype: &str) -> Option<JsonValue> {
diff --git a/fractal-gtk/src/widgets/inline_player.rs b/fractal-gtk/src/widgets/inline_player.rs
index d8c379b9..d69a5e6f 100644
--- a/fractal-gtk/src/widgets/inline_player.rs
+++ b/fractal-gtk/src/widgets/inline_player.rs
@@ -18,9 +18,10 @@
 // SPDX-License-Identifier: GPL-3.0-or-later
 
 use fractal_api::clone;
+
 use gst::prelude::*;
 use gst::ClockTime;
-use gst_player;
+use gstreamer_pbutils::Discoverer;
 use log::{error, info, warn};
 
 use gtk;
@@ -503,6 +504,13 @@ impl<T: MediaPlayer + 'static> PlayerExt for T {
                     Ok(path) => {
                         info!("MEDIA PATH: {}", &path);
                         *local_path.borrow_mut() = Some(path.clone());
+                        if ! start_playing {
+                            if let Some(controls) = player.get_controls() {
+                                if let Ok(duration) = get_media_duration(&path) {
+                                    controls.timer.on_duration_changed(Duration(duration))
+                                }
+                            }
+                        }
                         let uri = format!("file://{}", path);
                         player.get_player().set_uri(&uri);
                         if player.get_controls().is_some() {
@@ -642,3 +650,10 @@ fn adjust_box_margins_to_video_dimensions(bx: &gtk::Box, video_width: i32, video
         }
     }
 }
+
+pub fn get_media_duration(file: &str) -> Result<ClockTime, glib::Error> {
+    let timeout = ClockTime::from_seconds(1);
+    let discoverer = Discoverer::new(timeout)?;
+    let info = discoverer.discover_uri(&format!("file://{}", file))?;
+    Ok(info.get_duration())
+}
diff --git a/fractal-gtk/src/widgets/mod.rs b/fractal-gtk/src/widgets/mod.rs
index c135a50e..5594b322 100644
--- a/fractal-gtk/src/widgets/mod.rs
+++ b/fractal-gtk/src/widgets/mod.rs
@@ -5,7 +5,7 @@ mod divider;
 pub mod error_dialog;
 pub mod file_dialog;
 pub mod image;
-mod inline_player;
+pub mod inline_player;
 mod kicked_dialog;
 mod login;
 pub mod media_viewer;
diff --git a/meson.build b/meson.build
index f762dda9..920becb7 100644
--- a/meson.build
+++ b/meson.build
@@ -24,6 +24,7 @@ dependency('gstreamer-player-1.0', version: '>= 1.12')
 dependency('gstreamer-plugins-base-1.0', version: '>= 1.12')
 dependency('gstreamer-plugins-bad-1.0', version: '>= 1.12')
 dependency('gstreamer-bad-audio-1.0', version: '>= 1.12')
+dependency('gstreamer-pbutils-1.0', version: '>= 1.14')
 dependency('gst-editing-services-1.0', version: '>= 1.14.4')
 
 cargo = find_program('cargo', required: true)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]