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

fix: workaround to make Airplay work on Safari #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/HTMLVideo/HTMLVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var ERROR = require('../error');
var getContentType = require('./getContentType');
var HLS_CONFIG = require('./hlsConfig');

var isSafari = navigator.userAgent.includes("Safari") && !(/(chrome|firefox)/i.test(window.navigator.userAgent)) && (navigator.vendor || "").startsWith("Apple");

function HTMLVideo(options) {
options = options || {};

Expand Down Expand Up @@ -535,6 +537,22 @@ function HTMLVideo(options) {
onPropChanged('audioTracks');
onPropChanged('selectedAudioTrackId');
});

// adds the original stream source, so Safari can use it for AirPlay when it needs to
// see more details here: https://github.com/video-dev/hls.js/issues/5989#issuecomment-1825916766
function attachOriginalSource() {
var source = document.createElement('source');
source.src = stream.url;
source.dataset.id = 'original-stream';
videoElement.appendChild(source);
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to add the corresponding action in the unload command handler after the videoElement.removeAttribute('src'); call
something like:
while (videoElement.child) videoElement.removeChild(videoElement.child)

Copy link
Author

Choose a reason for hiding this comment

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

Done

videoElement.disableRemotePlayback = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

could this be moved on line 28?

Copy link
Author

Choose a reason for hiding this comment

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

No, this has to be done after calling hls.attachMedia(videoElement); and media is attaching, hence this is done on the Hls.Events.MEDIA_ATTACHING event.
I tested this with many other variations and this is the only way that got it working.


hls.off(Hls.Events.MEDIA_ATTACHING, attachOriginalSource);
}

if (isSafari)
hls.on(Hls.Events.MEDIA_ATTACHING, attachOriginalSource);

hls.loadSource(stream.url);
hls.attachMedia(videoElement);
} else {
Expand Down Expand Up @@ -570,6 +588,11 @@ function HTMLVideo(options) {
videoElement.removeAttribute('src');
videoElement.load();
videoElement.currentTime = 0;
Array.from(videoElement.children || []).forEach(function(child) {
if (child && child.dataset && child.dataset.id === 'original-stream') {
videoElement.removeChild(child);
}
});
onPropChanged('stream');
onPropChanged('loaded');
onPropChanged('paused');
Expand Down