Skip to content

Commit

Permalink
Merge pull request #663 from Stremio/feat/streaming-server-transcodin…
Browse files Browse the repository at this point in the history
…g-profile

feat: handle device-info route for streaming server
  • Loading branch information
tymmesyde authored Mar 25, 2024
2 parents 44679c0 + 62190c2 commit 5c98d34
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
49 changes: 46 additions & 3 deletions src/models/streaming_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::types::addon::ResourcePath;
use crate::types::api::SuccessResponse;
use crate::types::profile::{AuthKey, Profile};
use crate::types::streaming_server::{
GetHTTPSResponse, NetworkInfo, Settings, SettingsResponse, Statistics,
DeviceInfo, GetHTTPSResponse, NetworkInfo, Settings, SettingsResponse, Statistics,
};
use enclose::enclose;
use futures::{FutureExt, TryFutureExt};
Expand Down Expand Up @@ -51,6 +51,7 @@ pub struct StreamingServer {
pub remote_url: Option<Url>,
pub playback_devices: Loadable<Vec<PlaybackDevice>, EnvError>,
pub network_info: Loadable<NetworkInfo, EnvError>,
pub device_info: Loadable<DeviceInfo, EnvError>,
pub torrent: Option<(String, Loadable<ResourcePath, EnvError>)>,
/// [`Loadable::Loading`] is used only on the first statistics request.
pub statistics: Option<Loadable<Statistics, EnvError>>,
Expand All @@ -62,6 +63,7 @@ impl StreamingServer {
get_settings::<E>(&profile.settings.streaming_server_url),
get_playback_devices::<E>(&profile.settings.streaming_server_url),
get_network_info::<E>(&profile.settings.streaming_server_url),
get_device_info::<E>(&profile.settings.streaming_server_url),
]);
(
Self {
Expand All @@ -74,6 +76,7 @@ impl StreamingServer {
remote_url: None,
playback_devices: Loadable::Loading,
network_info: Loadable::Loading,
device_info: Loadable::Loading,
torrent: None,
statistics: None,
},
Expand All @@ -88,16 +91,19 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
Msg::Action(Action::StreamingServer(ActionStreamingServer::Reload)) => {
let settings_effects = eq_update(&mut self.settings, Loadable::Loading);
let network_info_effects = eq_update(&mut self.network_info, Loadable::Loading);
let device_info_effects = eq_update(&mut self.device_info, Loadable::Loading);
let base_url_effects = eq_update(&mut self.base_url, None);
let remote_url_effects = eq_update(&mut self.remote_url, None);
Effects::many(vec![
get_settings::<E>(&self.selected.transport_url),
get_playback_devices::<E>(&self.selected.transport_url),
get_network_info::<E>(&self.selected.transport_url),
get_device_info::<E>(&self.selected.transport_url),
])
.unchanged()
.join(settings_effects)
.join(network_info_effects)
.join(device_info_effects)
.join(base_url_effects)
.join(remote_url_effects)
}
Expand Down Expand Up @@ -224,6 +230,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
};
self.settings = Loadable::Loading;
self.network_info = Loadable::Loading;
self.device_info = Loadable::Loading;
self.base_url = None;
self.remote_url = None;
self.torrent = None;
Expand All @@ -232,6 +239,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
get_settings::<E>(&self.selected.transport_url),
get_playback_devices::<E>(&self.selected.transport_url),
get_network_info::<E>(&self.selected.transport_url),
get_device_info::<E>(&self.selected.transport_url),
])
}
Msg::Internal(Internal::StreamingServerSettingsResult(url, result))
Expand Down Expand Up @@ -262,13 +270,16 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
eq_update(&mut self.playback_devices, Loadable::Err(error.to_owned()));
let network_info_effects =
eq_update(&mut self.network_info, Loadable::Err(error.to_owned()));
let device_info_effects =
eq_update(&mut self.device_info, Loadable::Err(error.to_owned()));
let settings_effects =
eq_update(&mut self.settings, Loadable::Err(error.to_owned()));
let torrent_effects = eq_update(&mut self.torrent, None);
base_url_effects
.join(remote_url_effects)
.join(playback_devices_effects)
.join(network_info_effects)
.join(device_info_effects)
.join(settings_effects)
.join(torrent_effects)
}
Expand Down Expand Up @@ -300,6 +311,17 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
}
}
}
Msg::Internal(Internal::StreamingServerDeviceInfoResult(url, result))
if self.selected.transport_url == *url && self.device_info.is_loading() =>
{
match result {
Ok(device_info) => eq_update(
&mut self.device_info,
Loadable::Ready(device_info.to_owned()),
),
Err(error) => eq_update(&mut self.device_info, Loadable::Err(error.to_owned())),
}
}
Msg::Internal(Internal::StreamingServerUpdateSettingsResult(url, result))
if self.selected.transport_url == *url =>
{
Expand All @@ -312,13 +334,16 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
eq_update(&mut self.playback_devices, Loadable::Err(error.to_owned()));
let network_info_effects =
eq_update(&mut self.network_info, Loadable::Err(error.to_owned()));
let device_info_effects =
eq_update(&mut self.device_info, Loadable::Err(error.to_owned()));
let settings_effects =
eq_update(&mut self.settings, Loadable::Err(error.to_owned()));
let torrent_effects = eq_update(&mut self.torrent, None);
base_url_effects
.join(remote_url_effects)
.join(playback_devices_effects)
.join(network_info_effects)
.join(device_info_effects)
.join(settings_effects)
.join(torrent_effects)
}
Expand Down Expand Up @@ -432,28 +457,46 @@ fn get_network_info<E: Env + 'static>(url: &Url) -> Effect {
.into()
}

fn get_device_info<E: Env + 'static>(url: &Url) -> Effect {
let endpoint = url.join("device-info").expect("url builder failed");
let request = Request::get(endpoint.as_str())
.body(())
.expect("request builder failed");
EffectFuture::Concurrent(
E::fetch::<_, DeviceInfo>(request)
.map_ok(|resp| resp)
.map(enclose!((url) move |result|
Msg::Internal(Internal::StreamingServerDeviceInfoResult(url, result))
))
.boxed_env(),
)
.into()
}

fn set_settings<E: Env + 'static>(url: &Url, settings: &Settings) -> Effect {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Body {
remote_https: Option<String>,
cache_size: Option<f64>,
bt_max_connections: u64,
bt_handshake_timeout: u64,
bt_request_timeout: u64,
bt_download_speed_soft_limit: f64,
bt_download_speed_hard_limit: f64,
bt_min_peers_for_stable: u64,
remote_https: Option<String>,
transcode_profile: Option<String>,
}
let body = Body {
remote_https: settings.remote_https.to_owned(),
cache_size: settings.cache_size.to_owned(),
bt_max_connections: settings.bt_max_connections.to_owned(),
bt_handshake_timeout: settings.bt_handshake_timeout.to_owned(),
bt_request_timeout: settings.bt_request_timeout.to_owned(),
bt_download_speed_soft_limit: settings.bt_download_speed_soft_limit.to_owned(),
bt_download_speed_hard_limit: settings.bt_download_speed_hard_limit.to_owned(),
bt_min_peers_for_stable: settings.bt_min_peers_for_stable.to_owned(),
remote_https: settings.remote_https.to_owned(),
transcode_profile: settings.transcode_profile.to_owned(),
};
let endpoint = url.join("settings").expect("url builder failed");
let request = Request::post(endpoint.as_str())
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::types::api::{
use crate::types::library::{LibraryBucket, LibraryItem, LibraryItemId};
use crate::types::profile::{Auth, AuthKey, Profile, User};
use crate::types::resource::{MetaItem, Stream};
use crate::types::streaming_server::{GetHTTPSResponse, NetworkInfo, SettingsResponse, Statistics};
use crate::types::streaming_server::{
DeviceInfo, GetHTTPSResponse, NetworkInfo, SettingsResponse, Statistics,
};
use crate::types::streams::StreamItemState;

pub type CtxStorageResponse = (
Expand Down Expand Up @@ -105,6 +107,8 @@ pub enum Internal {
StreamingServerPlaybackDevicesResult(Url, Result<Vec<PlaybackDevice>, EnvError>),
// Result for network info.
StreamingServerNetworkInfoResult(Url, Result<NetworkInfo, EnvError>),
// Result for device info.
StreamingServerDeviceInfoResult(Url, Result<DeviceInfo, EnvError>),
/// Result for updating streaming server settings.
StreamingServerUpdateSettingsResult(Url, Result<(), EnvError>),
/// Result for creating a torrent.
Expand Down
11 changes: 11 additions & 0 deletions src/types/streaming_server/device_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError};

#[serde_as]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DeviceInfo {
#[serde(default)]
#[serde_as(deserialize_as = "DefaultOnError")]
pub available_hardware_accelerations: Vec<String>,
}
3 changes: 3 additions & 0 deletions src/types/streaming_server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod device_info;
pub use device_info::*;

mod network_info;
pub use network_info::*;

Expand Down
6 changes: 6 additions & 0 deletions src/types/streaming_server/settings.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError};

#[serde_as]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Settings {
pub app_path: String,
pub cache_root: String,
pub server_version: String,
pub remote_https: Option<String>,
// Set to default in case the value is anything other than a string or null
#[serde(default)]
#[serde_as(deserialize_as = "DefaultOnError")]
pub transcode_profile: Option<String>,
pub cache_size: Option<f64>,
pub bt_max_connections: u64,
pub bt_handshake_timeout: u64,
Expand Down
12 changes: 10 additions & 2 deletions src/unit_tests/streaming_server/remote_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::{
api::SuccessResponse,
profile::{Auth, AuthKey, Profile},
streaming_server::{
GetHTTPSResponse, NetworkInfo, Settings as StreamingServerSettings, SettingsResponse,
DeviceInfo, GetHTTPSResponse, NetworkInfo, Settings as StreamingServerSettings,
SettingsResponse,
},
True,
},
Expand All @@ -26,7 +27,6 @@ use crate::{

const STREAMING_SERVER_URL: &str = "http://127.0.0.1:11470";
const STREAMING_SERVER_SETTINGS: StreamingServerSettings = StreamingServerSettings {
remote_https: None,
app_path: String::new(),
cache_root: String::new(),
server_version: String::new(),
Expand All @@ -37,6 +37,8 @@ const STREAMING_SERVER_SETTINGS: StreamingServerSettings = StreamingServerSettin
bt_download_speed_soft_limit: 0.0,
bt_download_speed_hard_limit: 0.0,
bt_min_peers_for_stable: 0,
remote_https: None,
transcode_profile: None,
};

const AVAILABLE_INTERFACE: &str = "192.168.0.10";
Expand Down Expand Up @@ -76,6 +78,12 @@ fn remote_endpoint() {
}) as Box<dyn Any + Send>)
.boxed_env()
}
Request { url, .. } if url == "http://127.0.0.1:11470/device-info" => {
future::ok(Box::new(DeviceInfo {
available_hardware_accelerations: vec![],
}) as Box<dyn Any + Send>)
.boxed_env()
}
Request { url, .. } if url.starts_with("http://127.0.0.1:11470/get-https") => {
future::ok(Box::new(GetHTTPSResponse {
ip_address: AVAILABLE_INTERFACE.to_string(),
Expand Down

0 comments on commit 5c98d34

Please sign in to comment.