[fractal] Download of audio messages



commit b5b5a2ab7d03082f372fd27b6c661278d69033bf
Author: sonjita <sonjaleaheinze gmail com>
Date:   Thu Dec 12 16:54:05 2019 +0100

    Download of audio messages
    
    Before, an audio message was directly streamed from the server.
    The matrix server does not allow Range Requests. Therefore, whenever
    the message was stored there, it was not possible to play the audio
    from a different place than the beginning.
    
    Now, an audio message is downloaded the same way an image is.
    Therefore the user can jump to any position in the audio.

 fractal-gtk/src/widgets/inline_player.rs |  3 +--
 fractal-gtk/src/widgets/message.rs       | 14 ++++++++++----
 fractal-matrix-api/src/backend/media.rs  | 10 ----------
 fractal-matrix-api/src/backend/mod.rs    |  3 ---
 fractal-matrix-api/src/backend/types.rs  |  1 -
 fractal-matrix-api/src/util.rs           | 23 -----------------------
 6 files changed, 11 insertions(+), 43 deletions(-)
---
diff --git a/fractal-gtk/src/widgets/inline_player.rs b/fractal-gtk/src/widgets/inline_player.rs
index 3cb4a854..f9318010 100644
--- a/fractal-gtk/src/widgets/inline_player.rs
+++ b/fractal-gtk/src/widgets/inline_player.rs
@@ -180,7 +180,6 @@ impl Default for AudioPlayerWidget {
 impl AudioPlayerWidget {
     pub fn new() -> Rc<Self> {
         let w = Rc::new(Self::default());
-        Self::init(&w);
 
         // When the widget is attached to a parent,
         // since it's a rust struct and not a widget the
@@ -204,7 +203,7 @@ impl AudioPlayerWidget {
     }
 
     #[cfg_attr(rustfmt, rustfmt_skip)]
-    fn init(s: &Rc<Self>) {
+    pub fn init(s: &Rc<Self>) {
         Self::connect_control_buttons(s);
         Self::connect_gst_signals(s);
     }
diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs
index 25404e94..726350da 100644
--- a/fractal-gtk/src/widgets/message.rs
+++ b/fractal-gtk/src/widgets/message.rs
@@ -8,6 +8,7 @@ use chrono::prelude::*;
 use glib;
 use gtk;
 use gtk::prelude::*;
+use gtk::WidgetExt;
 use pango;
 use url::Url;
 
@@ -28,6 +29,7 @@ use crate::uitypes::MessageContent as Message;
 use crate::uitypes::RowType;
 use crate::widgets;
 use crate::widgets::message_menu::MessageMenu;
+use crate::widgets::AudioPlayerWidget;
 use crate::widgets::AvatarExt;
 
 /* A message row in the room history */
@@ -375,13 +377,14 @@ impl MessageBox {
     fn build_room_audio_player(&self, msg: &Message) -> gtk::Box {
         let bx = gtk::Box::new(gtk::Orientation::Horizontal, 6);
         let player = widgets::AudioPlayerWidget::new();
+        bx.set_opacity(0.3);
 
         let url = msg.url.clone().unwrap_or_default();
         let backend = self.backend.clone();
 
         let (tx, rx): (Sender<String>, Receiver<String>) = channel();
         backend
-            .send(BKCommand::GetMediaUrl(
+            .send(BKCommand::GetMediaAsync(
                 self.server_url.clone(),
                 url.clone(),
                 tx,
@@ -390,7 +393,7 @@ impl MessageBox {
 
         gtk::timeout_add(
             50,
-            clone!(player => move || {
+            clone!(player, bx => move || {
                 match rx.try_recv() {
                     Err(TryRecvError::Empty) => gtk::Continue(true),
                     Err(TryRecvError::Disconnected) => {
@@ -399,9 +402,12 @@ impl MessageBox {
                         APPOP!(show_error, (msg));
                         gtk::Continue(true)
                     },
-                    Ok(uri) => {
-                        info!("AUDIO URI: {}", &uri);
+                    Ok(directory) => {
+                        info!("AUDIO DIRECTORY: {}", &directory);
+                        let uri = format!("file://{}", directory);
                         player.initialize_stream(&uri);
+                        AudioPlayerWidget::init(&player);
+                        bx.set_opacity(1.0);
                         gtk::Continue(false)
                     }
                 }
diff --git a/fractal-matrix-api/src/backend/media.rs b/fractal-matrix-api/src/backend/media.rs
index 0f966605..aa0982a0 100644
--- a/fractal-matrix-api/src/backend/media.rs
+++ b/fractal-matrix-api/src/backend/media.rs
@@ -14,7 +14,6 @@ use crate::util::download_file;
 use crate::util::dw_media;
 use crate::util::get_prev_batch_from;
 use crate::util::json_q;
-use crate::util::resolve_media_url;
 use crate::util::semaphore;
 use crate::util::ContentType;
 use crate::util::ResultExpectLog;
@@ -60,15 +59,6 @@ pub fn get_media_list_async(
     });
 }
 
-pub fn get_media_url(bk: &Backend, baseu: Url, media: String, tx: Sender<String>) {
-    semaphore(bk.limit_threads.clone(), move || {
-        let uri = resolve_media_url(&baseu, &media, ContentType::Download)
-            .map(Url::into_string)
-            .unwrap_or_default();
-        tx.send(uri).expect_log("Connection closed");
-    });
-}
-
 pub fn get_file_async(url: Url, tx: Sender<String>) -> Result<(), Error> {
     let name = url
         .path_segments()
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 0905398e..92b2c020 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -357,9 +357,6 @@ impl Backend {
                         .expect_log("Connection closed");
                 });
             }
-            Ok(BKCommand::GetMediaUrl(server, media, ctx)) => {
-                media::get_media_url(self, server, media, ctx)
-            }
             Ok(BKCommand::GetFileAsync(url, ctx)) => {
                 let r = media::get_file_async(url, ctx);
                 bkerror!(r, tx, BKResponse::GetFileAsyncError);
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index 7cf4dfee..6e7ee575 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -55,7 +55,6 @@ pub enum BKCommand {
     GetFileAsync(Url, Sender<String>),
     GetAvatarAsync(Url, Option<Member>, Sender<String>),
     GetMedia(Url, String),
-    GetMediaUrl(Url, String, Sender<String>),
     GetUserInfoAsync(Url, String, Option<Sender<(String, String)>>),
     GetUserNameAsync(Url, String, Sender<String>),
     SendMsg(Url, AccessToken, Message),
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index 4b53e4c0..c3aa28bb 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -207,29 +207,6 @@ pub fn get_prev_batch_from(
     Ok(prev_batch)
 }
 
-pub fn resolve_media_url(base: &Url, url: &str, media_type: ContentType) -> Result<Url, Error> {
-    let caps = globals::MATRIX_RE
-        .captures(url)
-        .ok_or(Error::BackendError)?;
-    let server = String::from(&caps["server"]);
-    let media = String::from(&caps["media"]);
-
-    let (params, path) = if let ContentType::Thumbnail(w, h) = media_type {
-        (
-            vec![
-                ("width", w.to_string()),
-                ("height", h.to_string()),
-                ("method", String::from("scale")),
-            ],
-            format!("thumbnail/{}/{}", server, media),
-        )
-    } else {
-        (vec![], format!("download/{}/{}", server, media))
-    };
-
-    media_url(base, &path, &params)
-}
-
 pub fn dw_media(
     base: &Url,
     url: &str,


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