obws/client/
sources.rs

1use super::Client;
2use crate::{
3    error::Result,
4    requests::sources::{Request, SaveScreenshot, SourceId, TakeScreenshot},
5    responses::sources as responses,
6};
7
8/// API functions related to sources.
9pub struct Sources<'a> {
10    pub(super) client: &'a Client,
11}
12
13impl<'a> Sources<'a> {
14    /// Gets the active and show state of a source.
15    #[doc(alias = "GetSourceActive")]
16    pub async fn active(&self, source: SourceId<'_>) -> Result<responses::SourceActive> {
17        self.client.send_message(Request::Active { source }).await
18    }
19
20    /// Gets a Base64-encoded screenshot of a source.
21    ///
22    /// The [`TakeScreenshot::width`] and [`TakeScreenshot::height`] parameters are treated as
23    /// "scale to inner", meaning the smallest ratio will be used and the aspect ratio of the
24    /// original resolution is kept. If [`TakeScreenshot::width`] and [`TakeScreenshot::height`] are
25    /// not specified, the compressed image will use the full resolution of the source.
26    #[doc(alias = "GetSourceScreenshot")]
27    pub async fn take_screenshot(&self, settings: TakeScreenshot<'_>) -> Result<String> {
28        self.client
29            .send_message::<_, responses::ImageData>(Request::TakeScreenshot(settings))
30            .await
31            .map(|id| id.image_data)
32    }
33
34    /// Saves a screenshot of a source to the file system.
35    ///
36    /// The [`SaveScreenshot::width`] and [`SaveScreenshot::height`] parameters are treated as
37    /// "scale to inner", meaning the smallest ratio will be used and the aspect ratio of the
38    /// original resolution is kept. If [`SaveScreenshot::width`] and [`SaveScreenshot::height`] are
39    /// not specified, the compressed image will use the full resolution of the source.
40    #[doc(alias = "SaveSourceScreenshot")]
41    pub async fn save_screenshot(&self, settings: SaveScreenshot<'_>) -> Result<()> {
42        self.client
43            .send_message(Request::SaveScreenshot(settings))
44            .await
45    }
46}