diff --git a/src/stremio_app/stremio_player/communication.rs b/src/stremio_app/stremio_player/communication.rs index e6d68e5..2cca8d4 100644 --- a/src/stremio_app/stremio_player/communication.rs +++ b/src/stremio_app/stremio_player/communication.rs @@ -41,9 +41,17 @@ impl PlayerProprChange { } } } + +#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +struct PlayerEndedError { + message: String, + critical: bool, +} #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] pub struct PlayerEnded { reason: String, + #[serde(skip_serializing_if = "Option::is_none")] + error: Option, } impl PlayerEnded { fn string_from_end_reason(data: EndFileReason) -> String { @@ -53,9 +61,24 @@ impl PlayerEnded { _ => "other".to_string(), } } - pub fn from_end_reason(data: EndFileReason) -> Self { + pub fn from_end_reason(data: EndFileReason, error: &str) -> Self { Self { reason: Self::string_from_end_reason(data), + error: if data == mpv_end_file_reason::Error { + if error.is_empty() { + Some(PlayerEndedError { + message: "Unknown error".to_string(), + critical: true, + }) + } else { + Some(PlayerEndedError { + message: error.to_string(), + critical: true, + }) + } + } else { + None + }, } } } diff --git a/src/stremio_app/stremio_player/communication_tests.rs b/src/stremio_app/stremio_player/communication_tests.rs index 55b21e6..986e154 100644 --- a/src/stremio_app/stremio_player/communication_tests.rs +++ b/src/stremio_app/stremio_player/communication_tests.rs @@ -59,26 +59,42 @@ fn propr_change_tokens() { #[test] fn ended_tokens() { + let error_tokens: [Token; 12] = [ + Token::Struct { + name: "PlayerEnded", + len: 2, + }, + Token::Str("reason"), + Token::Str("error"), + Token::Str("error"), + Token::Some, + Token::Struct { + name: "PlayerEndedError", + len: 2, + }, + Token::Str("message"), + Token::Str("Unknown error"), + Token::Str("critical"), + Token::Bool(true), + Token::StructEnd, + Token::StructEnd, + ]; let tokens: [Token; 4] = [ Token::Struct { name: "PlayerEnded", len: 1, }, Token::Str("reason"), - Token::None, + Token::Str("quit"), Token::StructEnd, ]; - let mut typed_tokens = tokens.clone(); - typed_tokens[2] = Token::Str("error"); assert_tokens( - &PlayerEnded::from_end_reason(mpv_end_file_reason::Error), - &typed_tokens, + &PlayerEnded::from_end_reason(mpv_end_file_reason::Error, ""), + &error_tokens, ); - let mut typed_tokens = tokens.clone(); - typed_tokens[2] = Token::Str("quit"); assert_tokens( - &PlayerEnded::from_end_reason(mpv_end_file_reason::Quit), - &typed_tokens, + &PlayerEnded::from_end_reason(mpv_end_file_reason::Quit, ""), + &tokens, ); } diff --git a/src/stremio_app/stremio_player/player.rs b/src/stremio_app/stremio_player/player.rs index 06501b5..8f1fa8d 100644 --- a/src/stremio_app/stremio_player/player.rs +++ b/src/stremio_app/stremio_player/player.rs @@ -103,11 +103,20 @@ fn create_event_thread( } // -1.0 means to block and wait for an event. - let event = match event_context.wait_event(-1.) { - Some(Ok(event)) => event, + let (event, error) = match event_context.wait_event(-1.) { + Some(Ok(event)) => (event, ""), Some(Err(error)) => { - eprintln!("Event errored: {error:?}"); - continue; + if let libmpv2::Error::Raw(e) = error { + ( + Event::EndFile( + libmpv2_sys::mpv_end_file_reason_MPV_END_FILE_REASON_ERROR, + ), + libmpv2_sys::mpv_error_str(e), + ) + } else { + eprintln!("Unhandled event error: {error:?}"); + continue; + } } // dummy event received (may be created on a wake up call or on timeout) None => continue, @@ -124,7 +133,7 @@ fn create_event_thread( ), Event::EndFile(reason) => PlayerResponse( "mpv-event-ended", - PlayerEvent::End(PlayerEnded::from_end_reason(reason)), + PlayerEvent::End(PlayerEnded::from_end_reason(reason, error)), ), Event::Shutdown => { break;