Playback failures as typed causes: retry, hand over or stop

2026-12-04来源 dev.toEnglish

A video player is told "playback failed" in many ways, and from the outside they look alike: the picture is gone. The wifi dropped for a moment. The file is in a format the first player cannot read. The server answered, but not with a stream. One audio track has no decoder on this device.

What the app should do next differs each time. Ask for the stream again and carry on from where it stopped. Hand the file to another player. Or stop and tell the viewer in plain words.

In Yonto Player this is one design decision: a failure is a value that names its cause, and it is never just a message string. Retry and hand-over decisions read the cause. This post is about that type and the small pieces of code that read it. (A normal end of playback is not a failure; it is a state of its own and stays out of this.)

Why not branch on the message

The obvious way is to catch an error from the player, look at its message or error code, and branch. That works until a message is reworded, or the platform adds a code, or a second player reports the same problem in its own words. Then a rule stops matching without anyone noticing.

So each player layer translates what its platform reports into one enum, once, at the edge. Above that, the code knows causes. The text the viewer reads is a property of the cause, and the type's own documentation says why: the cause decides what the viewer is told and whether it is tried again or handed to another player, so a message reworded or added changes neither.

A simplified sketch, with generic names and fewer cases than the real type:

// Simplified sketch, not the real source. enum class Cause(val recoverable: Boolean) { Format(recoverable = false), // this player does not read the format Network(recoverable = true), // the stream stopped arriving Unavailable(recoverable = true), // the server answered, but not with the stream Damaged(recoverable = true), // the player stumbled on the file Unknown(recoverable = true), // ...a few more, each with a sentence of its own for the viewer } data class Failure(val cause: Cause, val detail: String) 

The detail is the platform's own error code. It is shown beside the message so a report can quote it, and no decision reads it.

Rule one: ask again, from where it stopped

recoverable says whether asking the source for the stream again may fix the failure. When a recovery is configured for the run and the cause is recoverable, the playback run asks again and starts from the position it reached. It does this up to 2 times before the viewer sees the failure.

Two details shape it. The count of tries lives outside the player, which each try rebuilds, so a fresh player does not reset it. And the count resets only after the stream has played for 30 continuous seconds, so a long film can survive several separate interruptions, while a stream that does not hold runs out of tries and shows its message.

Some causes are not retried. A plain HTTP address that the system forbids is one: asking again would get the same refusal. A start that never begins is another, and it ends with its own sentence and a Retry button instead. The sentences are listed word for word in the troubleshooting page, and each one is the message of one cause.

Rule two: which causes a second player might do better on

Yonto Player has two players: the device's own first, and a second for what the first cannot read. The video formats page lists which files go where.

Whether to hand over is a separate rule from whether to retry, kept in a small function:

// Simplified sketch of the hand-over rule, not the real source. fun mayReadElsewhere(failure: Failure) = failure.cause == Cause.Format || failure.cause == Cause.Damaged 

Format is there because that is exactly what the second player is for. Damaged is there because a file the first player stumbled on, such as a sound output that refused what it was handed, may read fine in the second. Network is not, because a second player would meet the same dead connection.

There is a third rule for platforms whose errors name their cause badly. Some report an unreadable file as a refused request, so for those the question flips: everything except what is plainly the network, the address or withheld headers might be the file's format. It is a second function over the same cause, not a special case in the player.

The hand-over is invisible to the session

A small class wraps both players and looks like one to the session above it. When a failure passes the rule, it builds the second player, which is only created when needed, and gives it the first one's position, speed and wish to play. The session sees one player that took a moment longer to start.

Each player's events reach the session only while that player is the current one, so a late callback from the first cannot undo the second. When the hand-over followed a first failure and the second also reports that it could not read the file, the session is shown the first player's failure, since that one named the cause better.

A player can also say that it has no decoder for one track, such as the audio of a file. That is its own event and not an error. The same wrapper answers it with the same hand-over: the whole file carries on in the second player, and the viewer sees no failure screen.

A test that forces the decisions

All of this would drift if the table lived only in people's heads. A table-driven test lists every cause beside its rules: its message, whether it is recoverable, whether a second player may read it, and whether it could be the format. Another assertion checks that the table has exactly the causes the enum has. Add a case to the enum and the build fails until the test says what the new cause does under each rule.

What to take from it

If you build a player, or anything that reacts to why something stopped:

  • Model the reason as a type at the edge, and keep platform codes as detail for reports.
  • Put the rules next to the cause, as plain fields or small functions, and test the whole table so a new cause has to choose.
  • Keep retry and hand-over as separate questions: a cause can be worth asking again and still not be worth a second player.
  • Bound the retries, and keep the count where the retry itself cannot reset it.

The app is closed source, so the snippets above are sketches. The plugin SDK and command-line tool that feed it streams are open source, if you want to see the other side of that boundary.

Read more

原文:https://www.a8q9.cn/en/article/playback-failures-as-typed-causes-retry-hand-over-or-stop-6202fe.html

相关文章