Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Fix remaining race condition in media playback #1254

Merged
merged 1 commit into from
Sep 28, 2019

Conversation

fxthomas
Copy link
Contributor

TL;DR: This commit is hopefully the final fix on Airsonic's side for #685. It removes temporary workarounds introduced in #1080, and as such fixes #1160.


The root cause of the issue is the fact that, when we go to the next track in an Airsonic play queue, we change the media source in the ended event, as described in more detail in mediaelement/mediaelement#2650.

In MEJS, this translates as the following two things:

  • In Airsonic's ended event handler, we change the media source (set the src attribute) and call the load() method, followed by the play() method.

  • Another ended event handler was also used internally by the MEJS player, and one of these internal uses called the pause() method (presumably in order to make sure that playback was stopped on some media renderers).

Unfortunately, the order in which these event handlers are called depends (in all modern browsers) on the order in which they are registered.

In our case, the first one is registered inside the <body> tag, but the second one is registered with $(document).ready(...). This means that the first event handler is called before the second.

This means that, in some cases (when we're unlucky, hence the seemingly random nature of the bug), pause() is called after load() but before the media has finished loading.

Apparently, this causes the AbortError: The fetching process for the media resource was aborted by the user agent at the user's request. message to appear (which indicates exactly what's described in the last paragraph), and the playback of the next song is aborted.

eharris
eharris approved these changes Sep 21, 2019
This commit is hopefully the final fix on Airsonic's side for airsonic#685.  It
also fixes airsonic#1160, which was caused by temporary workarounds introduced
in airsonic#1080 while we were looking for a solution.

The root cause of the issue is the fact that, when we go to the next
track in an Airsonic play queue, we change the media source in the
`ended` event.

In MEJS, this translates as the following two things:

* In Airsonic's 'ended' event, we change the media source (set the `src`
  attribute) and call the `load()` method, followed by the `play()`
  method.

* The 'ended' event was also used internally by the MEJS player, and
  one of these internal uses called the `pause()` method (presumably in
  order to make sure that playback was stopped on some media renderers).

Unfortunately, the order in which these events are called depends (in
all modern browsers) on the order in which they are registered.

In our case, the first one is registered inside the `<body>` tag, but
the second one is registered with `$(document).ready(...)`. This means
that the first event handler is called before the second.

This means that, in some cases (when we're unlucky, hence the seemingly
random nature of the bug), `pause()` is called after `load()`
but before the media has finished loading.

Apparently, this causes the `AbortError: The fetching process for the
media resource was aborted by the user agent at the user's request.`
message to appear (which indicates exactly what's described in the last
paragraph), and the playback of the next song is aborted.
@Adnn
Copy link

Adnn commented Sep 24, 2019

@fxthomas I built your branch and ran the resulting container on our "live" server, and I don't have the last song repeating anymore on my Chrome browser, whereas it was always repeating with the latest release. Thank you, it seems you fixed it!

@fxthomas
Copy link
Contributor Author

fxthomas commented Sep 24, 2019

Thanks for testing! I assume [edit: you] didn't find other issues with playback (such as the original bug where the next song isn't played)?

Since this is a core feature that had a lot of difficult to debug issues lately, I want to leave it open until this weekend for either myself or other testers to find issues. If nothing happens, I'll merge it.

@Adnn
Copy link

Adnn commented Sep 25, 2019

I assume didn't find other issues with playback (such as the original bug where the next song isn't played)?

We did not observe any regression in playback behaviour : )

@fxthomas
Copy link
Contributor Author

I did not notice any issues for a few days now, time to merge this!

@fxthomas fxthomas merged commit 2b73a82 into airsonic:master Sep 28, 2019
@fxthomas fxthomas deleted the 1160-fix-mejs-race-condition branch September 28, 2019 14:05
@fxthomas
Copy link
Contributor Author

@muff1nman I was thinking we should probably backport this and make a 10.4.1 release. This affects a significant portion of people and the patch applies cleanly on top of the release-10.4 branch. What do you think?

@jvoisin
Copy link
Contributor

jvoisin commented Oct 11, 2019

Or, a new major release :3

@muff1nman
Copy link
Contributor

@fxthomas sure. TBH I was hoping that we could get a 10.5 released soon, but I think it'd be smarter to do a 10.4.1. Can you submit a PR for this to the 10.4 branch? We should probably also pull in the mariadb change as well.

@fxthomas fxthomas mentioned this pull request Oct 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Last song in playlist repeating.
5 participants