Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: player next stream #536

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/models/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub struct Player {
pub subtitles: Vec<ResourceLoadable<Vec<Subtitles>>>,
pub next_video: Option<Video>,
pub next_streams: Option<ResourceLoadable<Vec<Stream>>>,
pub next_stream: Option<Stream>,
pub series_info: Option<SeriesInfo>,
pub library_item: Option<LibraryItem>,
#[serde(skip_serializing)]
Expand Down Expand Up @@ -173,6 +174,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
);
let next_video_effects = next_video_update(
&mut self.next_video,
&self.next_stream,
&self.selected,
&self.meta_item,
&ctx.profile.settings,
Expand All @@ -182,6 +184,12 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
&self.next_video,
&self.selected,
);
let next_stream_effects = next_stream_update(
&mut self.next_stream,
&self.next_streams,
&self.selected,
&ctx.profile.settings,
);
let series_info_effects =
series_info_update(&mut self.series_info, &self.selected, &self.meta_item);
let library_item_effects = library_item_update::<E>(
Expand Down Expand Up @@ -237,6 +245,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
.join(subtitles_effects)
.join(next_video_effects)
.join(next_streams_effects)
.join(next_stream_effects)
.join(series_info_effects)
.join(library_item_effects)
.join(watched_effects)
Expand Down Expand Up @@ -266,6 +275,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
let subtitles_effects = eq_update(&mut self.subtitles, vec![]);
let next_video_effects = eq_update(&mut self.next_video, None);
let next_streams_effects = eq_update(&mut self.next_streams, None);
let next_stream_effects = eq_update(&mut self.next_stream, None);
let series_info_effects = eq_update(&mut self.series_info, None);
let library_item_effects = eq_update(&mut self.library_item, None);
let watched_effects = eq_update(&mut self.watched, None);
Expand All @@ -282,6 +292,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
.join(subtitles_effects)
.join(next_video_effects)
.join(next_streams_effects)
.join(next_stream_effects)
.join(series_info_effects)
.join(library_item_effects)
.join(watched_effects)
Expand Down Expand Up @@ -471,6 +482,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {

let next_video_effects = next_video_update(
&mut self.next_video,
&self.next_stream,
&self.selected,
&self.meta_item,
&ctx.profile.settings,
Expand All @@ -480,6 +492,13 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
&self.next_video,
&self.selected,
));
let next_stream_effects = next_stream_update(
&mut self.next_stream,
&self.next_streams,
&self.selected,
&ctx.profile.settings,
);

let series_info_effects =
series_info_update(&mut self.series_info, &self.selected, &self.meta_item);
let library_item_effects = library_item_update::<E>(
Expand Down Expand Up @@ -516,6 +535,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
.join(subtitles_effects)
.join(next_video_effects)
.join(next_streams_effects)
.join(next_stream_effects)
.join(series_info_effects)
.join(library_item_effects)
.join(watched_effects)
Expand Down Expand Up @@ -559,6 +579,7 @@ fn switch_to_next_video(

fn next_video_update(
video: &mut Option<Video>,
stream: &Option<Stream>,
selected: &Option<Selected>,
meta_item: &Option<ResourceLoadable<MetaItem>>,
settings: &ProfileSettings,
Expand Down Expand Up @@ -600,8 +621,13 @@ fn next_video_update(
.unwrap_or_default();
next_season != 0 || current_season == next_season
})
.map(|(_, next_video)| next_video)
.cloned(),
.map(|(_, next_video)| {
let mut next_video = next_video.clone();
if let Some(stream) = stream {
next_video.streams = vec![stream.clone()];
}
next_video
}),
_ => None,
};
eq_update(video, next_video)
Expand Down Expand Up @@ -675,6 +701,31 @@ where
}
}

fn next_stream_update(
stream: &mut Option<Stream>,
next_streams: &Option<ResourceLoadable<Vec<Stream>>>,
selected: &Option<Selected>,
settings: &ProfileSettings,
) -> Effects {
let next_stream = match (selected, next_streams) {
(
Some(Selected { stream, .. }),
Some(ResourceLoadable {
content: Some(Loadable::Ready(streams)),
..
}),
) if settings.binge_watching => streams
.iter()
.find(|Stream { behavior_hints, .. }| {
behavior_hints.binge_group == stream.behavior_hints.binge_group
})
.cloned(),
_ => None,
};

eq_update(stream, next_stream)
}

fn series_info_update(
series_info: &mut Option<SeriesInfo>,
selected: &Option<Selected>,
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod catalog_with_filters;
mod ctx;
mod deep_links;
mod meta_details;
mod player;
mod serde;

mod data_export;
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/player/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod next_stream;
156 changes: 156 additions & 0 deletions src/unit_tests/player/next_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use crate::{
constants::{META_RESOURCE_NAME, STREAM_RESOURCE_NAME},
models::{
ctx::Ctx,
player::{Player, Selected},
},
runtime::{
msg::{Action, ActionLoad},
EnvFutureExt, Runtime, RuntimeAction, TryEnvFuture,
},
types::{
addon::{ResourcePath, ResourceRequest, ResourceResponse},
profile::{Profile, Settings},
resource::{
MetaItem, MetaItemPreview, SeriesInfo, Stream, StreamBehaviorHints, StreamSource, Video,
},
},
unit_tests::{default_fetch_handler, Request, TestEnv, FETCH_HANDLER},
};
use futures::future;
use std::any::Any;
use stremio_derive::Model;

fn create_stream(binge_group: &str) -> Stream {
Stream {
source: StreamSource::Url {
url: "https://source_url".parse().unwrap(),
},
name: None,
description: None,
thumbnail: None,
subtitles: vec![],
behavior_hints: StreamBehaviorHints {
binge_group: Some(binge_group.to_owned()),
..Default::default()
},
}
}

fn create_video(season: u32, episode: u32) -> Video {
Video {
id: format!("tt123456:{season}:{episode}"),
title: format!("video_{episode}"),
released: None,
overview: None,
thumbnail: None,
streams: vec![],
series_info: Some(SeriesInfo { season, episode }),
trailer_streams: vec![],
}
}

#[test]
fn next_stream() {
#[derive(Model, Default, Clone, Debug)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
player: Player,
}

fn fetch_handler(request: Request) -> TryEnvFuture<Box<dyn Any + Send>> {
match request {
Request { url, .. } if url == "https://transport_url/meta/series/tt123456.json" => {
future::ok(Box::new(ResourceResponse::Meta {
meta: MetaItem {
preview: MetaItemPreview {
id: "tt123456".to_owned(),
r#type: "series".to_owned(),
..Default::default()
},
videos: vec![create_video(1, 1), create_video(1, 2)],
},
}) as Box<dyn Any + Send>)
.boxed_env()
}
Request { url, .. }
if url == "https://transport_url/stream/series/tt123456%3A1%3A2.json" =>
{
future::ok(Box::new(ResourceResponse::Streams {
streams: vec![create_stream("binge_group"), create_stream("binge_group_1")],
}) as Box<dyn Any + Send>)
.boxed_env()
}
_ => default_fetch_handler(request),
}
}

let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");

*FETCH_HANDLER.write().unwrap() = Box::new(fetch_handler);

let (runtime, _rx) = Runtime::<TestEnv, _>::new(
TestModel {
ctx: Ctx {
profile: Profile {
settings: Settings {
binge_watching: true,
..Default::default()
},
..Default::default()
},
..Default::default()
},
player: Player::default(),
},
vec![],
1000,
);

let stream = create_stream("binge_group");
let meta_request = ResourceRequest {
base: "https://transport_url/manifest.json".parse().unwrap(),
path: ResourcePath {
resource: META_RESOURCE_NAME.to_owned(),
r#type: "series".to_owned(),
id: "tt123456".to_owned(),
extra: vec![],
},
};
let stream_request = ResourceRequest {
base: "https://transport_url/manifest.json".parse().unwrap(),
path: ResourcePath {
resource: STREAM_RESOURCE_NAME.to_owned(),
r#type: "series".to_owned(),
id: "tt123456:1:1".to_owned(),
extra: vec![],
},
};

TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::Load(ActionLoad::Player(Box::new(Selected {
stream: stream.clone(),
stream_request: Some(stream_request),
meta_request: Some(meta_request),
subtitles_path: None,
}))),
});
});

assert_eq!(
runtime
.model()
.unwrap()
.player
.next_stream
.as_ref()
.unwrap()
.behavior_hints
.binge_group,
stream.behavior_hints.binge_group,
"next stream has same binge group"
);
}