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

media_error: Add MediaStatus::from_status_if_negative() helper #414

Merged
merged 1 commit into from
Aug 15, 2023

Conversation

MarijnS95
Copy link
Member

CC @paxbun

Covers a prevalent unwrap_err() pattern for negative status codes, where we want to use any non-negative status code as a useful value and otherwise assert that from_status() turns the raw value into an appropriate error code (never into Ok(()), which should be unreachable in the current implementation).

Note that we cannot rely on from_status() returning Ok(()) or Err(_) and translate the Ok(()) back to the original value, as it handles (as of writing) two positive error codes which only relate to a specific AMediaCodec API, and any unrecognized codes including >0 are treated as UnknownStatus.

The function consumes Into<isize> instead of i32/c_int or ffi::media_status_t to support passing through numbers from various APIs that return 64-bit numbers (e.g. MediaCodec). The value only needs to be truncated (with a panic on try_into()) when it is lower than zero and needs to be converted to ffi::media_status_t.

Copy link
Contributor

@rib rib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urgh, kinda horrible that media_status_t has some non-negative status codes if some apis are designed to convey errors via negative values or else useful values :/

This utility API looks good to me though.

@MarijnS95
Copy link
Member Author

Yeah doesn't have too much to do with this extra util function, but yes it is totally unusable because these error codes are supposedly only returned by API/functions that also have other non-zero positive return values 😩

Come to think of it, I don't think the current code handles those positive codes at all.

Covers a prevalent `unwrap_err()` pattern for negative status codes,
where we want to use any non-negative status code as a useful value and
otherwise assert that `from_status()` turns the raw value into an
appropriate error code (never into `Ok(())`, which should be unreachable
in the current implementation).

Note that we cannot rely on `from_status()` returning `Ok(())` or
`Err(_)` and translate the `Ok(())` back to the original value, as it
handles (as of writing) two positive error codes which only relate to a
specific `AMediaCodec` API, and any unrecognized codes including >0 are
treated as `UnknownStatus`.

The function consumes `Into<isize>` instead of `i32/c_int` or
`ffi::media_status_t` to support passing through numbers from various
APIs that return 64-bit numbers (e.g. `MediaCodec`).  The value only
needs to be truncated (with a panic on `try_into()`) when it is lower
than zero and needs to be converted to `ffi::media_status_t`.
@MarijnS95 MarijnS95 merged commit 74d8d7e into rust-mobile:master Aug 15, 2023
18 checks passed
@MarijnS95 MarijnS95 deleted the negative-error-code-helper branch August 15, 2023 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants