Skip to content

Commit bfc17b4

Browse files
authored
feat: Add Airplay support when overriding native HLS in Safari/iOS (#1543)
1 parent f6a4f79 commit bfc17b4

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"video.js": "^7 || ^8"
6868
},
6969
"peerDependencies": {
70-
"video.js": "^8.14.0"
70+
"video.js": "^8.19.0"
7171
},
7272
"devDependencies": {
7373
"@babel/cli": "^7.21.0",

src/videojs-http-streaming.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,19 @@ class VhsHandler extends Component {
10791079

10801080
this.mediaSourceUrl_ = window.URL.createObjectURL(this.playlistController_.mediaSource);
10811081

1082-
this.tech_.src(this.mediaSourceUrl_);
1082+
// If we are playing HLS with MSE in Safari, add source elements for both the blob and manifest URLs.
1083+
// The latter will enable Airplay playback on receiver devices.
1084+
if ((
1085+
videojs.browser.IS_ANY_SAFARI || videojs.browser.IS_IOS) &&
1086+
this.options_.overrideNative &&
1087+
this.options_.sourceType === 'hls' &&
1088+
typeof this.tech_.addSourceElement === 'function'
1089+
) {
1090+
this.tech_.addSourceElement(this.mediaSourceUrl_);
1091+
this.tech_.addSourceElement(this.source_.src);
1092+
} else {
1093+
this.tech_.src(this.mediaSourceUrl_);
1094+
}
10831095
}
10841096

10851097
createKeySessions_() {

test/videojs-http-streaming.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,6 +3121,45 @@ QUnit.test(
31213121
}
31223122
);
31233123

3124+
QUnit.test('uses source elements when overriding native HLS in Safari/iOS', function(assert) {
3125+
const origIsAnySafari = videojs.browser.IS_ANY_SAFARI;
3126+
const addSourceElementCalls = [];
3127+
let srcCalls = 0;
3128+
3129+
videojs.browser.IS_ANY_SAFARI = true;
3130+
3131+
const player = createPlayer({ html5: { vhs: { overrideNative: true } } });
3132+
3133+
player.tech_.addSourceElement = function(url) {
3134+
addSourceElementCalls.push(url);
3135+
};
3136+
3137+
player.tech_.src = function() {
3138+
srcCalls++;
3139+
};
3140+
3141+
player.src({
3142+
src: 'http://example.com/manifest/main.m3u8',
3143+
type: 'application/x-mpegURL'
3144+
});
3145+
3146+
this.clock.tick(1);
3147+
3148+
assert.equal(addSourceElementCalls.length, 2, '2 source elements added');
3149+
assert.equal(srcCalls, 0, 'tech.src() not called');
3150+
3151+
const blobUrl = addSourceElementCalls[0];
3152+
const manifestUrl = addSourceElementCalls[1];
3153+
3154+
assert.ok(blobUrl.startsWith('blob:'), 'First source element is a blob URL');
3155+
assert.equal(manifestUrl, 'http://example.com/manifest/main.m3u8', 'Second source element is the manifest URL');
3156+
3157+
// Clean up and restore original flags
3158+
player.dispose();
3159+
3160+
videojs.browser.IS_ANY_SAFARI = origIsAnySafari;
3161+
});
3162+
31243163
QUnit.test('re-emits mediachange events', function(assert) {
31253164
let mediaChanges = 0;
31263165

0 commit comments

Comments
 (0)