Skip to content

Commit

Permalink
Merge pull request #6 from yaymukund/handle-end-file
Browse files Browse the repository at this point in the history
Don't crash upon reaching EndFile, or if observing properties.
  • Loading branch information
ParadoxSpiral authored Apr 25, 2019
2 parents 5b753b2 + 23bf04d commit 73ae0ed
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/wrapper/events/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ impl Mpv {
mpv_event_id::StartFile => Some(Ok(Event::StartFile)),
mpv_event_id::EndFile => {
let end_file = *(event.data as *mut mpv_sys::mpv_event_end_file);
Some(if let Err(e) = mpv_err((), end_file.error) {
Err(e)

if let Err(e) = mpv_err((), end_file.error) {
Some(Err(e))
} else if end_file.reason.is_positive() {
Some(Ok(Event::EndFile(end_file.reason as _)))
} else {
assert!(end_file.reason.is_positive());
Ok(Event::EndFile(end_file.reason as _))
})
None
}
}
mpv_event_id::FileLoaded => Some(Ok(Event::FileLoaded)),
mpv_event_id::Idle => Some(Ok(Event::Idle)),
Expand All @@ -217,13 +219,20 @@ impl Mpv {
mpv_event_id::PlaybackRestart => Some(Ok(Event::PlaybackRestart)),
mpv_event_id::PropertyChange => {
let property = *(event.data as *mut mpv_sys::mpv_event_property);
Some(mpv_cstr_to_str!(property.name).and_then(|name| {
Ok(Event::PropertyChange {
name,
change: PropertyData::from_raw(property.format, property.data)?,
reply_userdata: event.reply_userdata,
})
}))

// This happens if the property is not available. For example,
// if you reached EndFile while observing a property.
if property.format == mpv_format::None {
None
} else {
Some(mpv_cstr_to_str!(property.name).and_then(|name| {
Ok(Event::PropertyChange {
name,
change: PropertyData::from_raw(property.format, property.data)?,
reply_userdata: event.reply_userdata,
})
}))
}
}
mpv_event_id::QueueOverflow => Some(Ok(Event::QueueOverflow)),
id => Some(Ok(Event::Deprecated(id))),
Expand Down

0 comments on commit 73ae0ed

Please sign in to comment.