Skip to main content

matrix_sdk/send_queue/
progress.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Progress facilities for the media upload system.
16
17use std::ops::Add;
18
19use eyeball::SharedObservable;
20#[cfg(feature = "unstable-msc4274")]
21use matrix_sdk_base::store::AccumulatedSentMediaInfo;
22use matrix_sdk_base::{media::MediaRequestParameters, store::DependentQueuedRequestKind};
23use matrix_sdk_common::executor::spawn;
24use ruma::{TransactionId, events::room::MediaSource};
25use tokio::sync::broadcast;
26use tracing::warn;
27
28use crate::{
29    Room, TransmissionProgress,
30    send_queue::{QueueStorage, RoomSendQueue, RoomSendQueueStorageError, RoomSendQueueUpdate},
31};
32
33/// Progress of an operation in abstract units.
34///
35/// Contrary to [`TransmissionProgress`], this allows tracking the progress of
36/// sending or receiving a payload in estimated pseudo units representing a
37/// percentage. This is helpful in cases where the exact progress in bytes isn't
38/// known, for instance, because encryption (which changes the size) happens on
39/// the fly.
40#[derive(Clone, Copy, Debug, Default)]
41pub struct AbstractProgress {
42    /// How many units were already transferred.
43    pub current: usize,
44    /// How many units there are in total.
45    pub total: usize,
46}
47
48// AbstractProgress can be added together, which adds their components.
49impl Add for AbstractProgress {
50    type Output = Self;
51
52    fn add(self, other: Self) -> Self::Output {
53        Self { current: self.current + other.current, total: self.total + other.total }
54    }
55}
56
57/// Information needed to compute the progress of uploading a media and its
58/// associated thumbnail.
59#[derive(Clone, Copy, Debug)]
60pub(super) struct MediaUploadProgressInfo {
61    /// The index of the uploaded item if this is a gallery upload. Otherwise,
62    /// zero.
63    pub index: u64,
64    /// The total number of bytes in the file currently being uploaded.
65    pub bytes: usize,
66    /// Some offset for the [`AbstractProgress`] computations.
67    ///
68    /// For a file upload, the offsets include the size of the thumbnail as part
69    /// of the already uploaded data and total. For a thumbnail upload, this
70    /// includes the size of the file to be uploaded in the total.
71    pub offsets: AbstractProgress,
72}
73
74impl RoomSendQueue {
75    /// Create metadata required to compute the progress of a media upload.
76    pub(super) async fn create_media_upload_progress_info(
77        own_txn_id: &TransactionId,
78        related_to: &TransactionId,
79        cache_key: &MediaRequestParameters,
80        thumbnail_source: Option<&MediaSource>,
81        #[cfg(feature = "unstable-msc4274")] accumulated: &[AccumulatedSentMediaInfo],
82        room: &Room,
83        queue: &QueueStorage,
84    ) -> MediaUploadProgressInfo {
85        // Determine the item's index, if this is a gallery upload.
86        let index = {
87            cfg_select! {
88                feature = "unstable-msc4274" => accumulated.len(),
89                _ => {
90                    0 // Before MSC4274 only a single file (and thumbnail) could be sent per event.
91                }
92            }
93        };
94
95        // Get the size of the file being uploaded from the event cache.
96        let bytes = match room.client().media_store().lock().await {
97            Ok(cache) => match cache.get_media_content(cache_key).await {
98                Ok(Some(content)) => content.len(),
99                Ok(None) => 0,
100                Err(err) => {
101                    warn!("error when reading media content from media store: {err}");
102                    0
103                }
104            },
105            Err(err) => {
106                warn!("couldn't acquire media store lock: {err}");
107                0
108            }
109        };
110
111        let offsets = {
112            // If we're uploading a file, we may have already uploaded a
113            // thumbnail; get its size from the in-memory thumbnail sizes cache.
114            // This will account in the current and total size, for the overall
115            // progress of thumbnail+file.
116            let already_uploaded_thumbnail_bytes = if thumbnail_source.is_some() {
117                queue
118                    .thumbnail_file_sizes
119                    .lock()
120                    .get(related_to)
121                    .and_then(|sizes| sizes.get(index))
122                    .copied()
123                    .flatten()
124            } else {
125                None
126            };
127
128            let already_uploaded_thumbnail_bytes = already_uploaded_thumbnail_bytes.unwrap_or(0);
129
130            // If we're uploading a thumbnail, get the size of the file to be
131            // uploaded after it, from the database. This will account in the
132            // total progress of the file+thumbnail upload (we're currently
133            // uploading the thumbnail, in the first step).
134            let pending_file_bytes = match RoomSendQueue::get_dependent_pending_file_upload_size(
135                own_txn_id, room,
136            )
137            .await
138            {
139                Ok(maybe_size) => maybe_size.unwrap_or(0),
140                Err(err) => {
141                    warn!(
142                        "error when getting pending file upload size: {err}; using 0 as fallback"
143                    );
144                    0
145                }
146            };
147
148            // In nominal cases where the send queue is used correctly, only one
149            // of these two values will be non-zero.
150            AbstractProgress {
151                current: already_uploaded_thumbnail_bytes,
152                total: already_uploaded_thumbnail_bytes + pending_file_bytes,
153            }
154        };
155
156        MediaUploadProgressInfo { index: index as u64, bytes, offsets }
157    }
158
159    /// Determine the size of a pending file upload, if this is a thumbnail
160    /// upload or return 0 otherwise.
161    async fn get_dependent_pending_file_upload_size(
162        txn_id: &TransactionId,
163        room: &Room,
164    ) -> Result<Option<usize>, RoomSendQueueStorageError> {
165        let client = room.client();
166        let dependent_requests =
167            client.state_store().load_dependent_queued_requests(room.room_id()).await?;
168
169        // Try to find a depending request which depends on the target one, and
170        // that's a media upload.
171        let Some((cache_key, parent_is_thumbnail_upload)) =
172            dependent_requests.into_iter().find_map(|r| {
173                if r.parent_transaction_id != txn_id {
174                    return None;
175                }
176
177                if let DependentQueuedRequestKind::UploadFileOrThumbnail {
178                    cache_key,
179                    parent_is_thumbnail_upload,
180                    ..
181                } = r.kind
182                {
183                    Some((cache_key, parent_is_thumbnail_upload))
184                } else {
185                    None
186                }
187            })
188        else {
189            // If there's none, we're done here.
190            return Ok(None);
191        };
192
193        // If this is not a thumbnail upload, we're uploading a gallery and the
194        // dependent request is for the next gallery item.
195        if !parent_is_thumbnail_upload {
196            return Ok(None);
197        }
198
199        let media_store_guard = client.media_store().lock().await?;
200
201        let maybe_content = media_store_guard.get_media_content(&cache_key).await?;
202
203        Ok(maybe_content.map(|c| c.len()))
204    }
205
206    /// Create an observable to watch a media's upload progress.
207    pub(super) fn create_media_upload_progress_observable(
208        media_upload_info: &MediaUploadProgressInfo,
209        related_txn_id: &TransactionId,
210        update_sender: &broadcast::Sender<RoomSendQueueUpdate>,
211    ) -> SharedObservable<TransmissionProgress> {
212        let progress: SharedObservable<TransmissionProgress> = Default::default();
213        let mut subscriber = progress.subscribe();
214
215        let related_txn_id = related_txn_id.to_owned();
216        let update_sender = update_sender.clone();
217        let media_upload_info = *media_upload_info;
218
219        // Watch and communicate the progress on a detached background task.
220        // Once the progress observable is dropped, next() will return None and
221        // the task will end.
222        spawn(async move {
223            while let Some(progress) = subscriber.next().await {
224                // Purposefully don't use `send_update` here, because we don't
225                // want to notify the global listeners about an upload progress
226                // update.
227                let _ = update_sender.send(RoomSendQueueUpdate::MediaUpload {
228                    related_to: related_txn_id.clone(),
229                    file: None,
230                    index: media_upload_info.index,
231                    progress: estimate_media_upload_progress(progress, media_upload_info.bytes)
232                        + media_upload_info.offsets,
233                });
234            }
235        });
236
237        progress
238    }
239}
240
241/// Estimates the upload progress for a single media file (either a thumbnail or
242/// a file).
243///
244/// This proportionally maps the upload progress given in actual bytes sent
245/// (possibly after encryption) into units of the unencrypted file sizes.
246///
247/// # Arguments
248///
249/// - `progress` - The [`TransmissionProgress`] of uploading the file (possibly
250///   after encryption).
251///
252/// - `bytes` - The total number of bytes in the file before encryption.
253fn estimate_media_upload_progress(
254    progress: TransmissionProgress,
255    bytes: usize,
256) -> AbstractProgress {
257    if progress.total == 0 {
258        return AbstractProgress { current: 0, total: 0 };
259    }
260
261    // Did the file finish uploading?
262    if progress.current == progress.total {
263        return AbstractProgress { current: bytes, total: bytes };
264    }
265
266    // The file is still uploading. Use the rule of 3 to proportionally map the
267    // progress into units of the original file size.
268    AbstractProgress {
269        current: (progress.current as f64 / progress.total as f64 * bytes as f64).round() as usize,
270        total: bytes,
271    }
272}