Skip to content

Commit 6a7419e

Browse files
fix(VIDEO-19984): withCredentials support for new hls package (#903)
1 parent e674123 commit 6a7419e

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/plugins/adaptive-streaming/adaptive-streaming.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ export default async function adaptiveStreamingPlugin(player, options) {
1010
...abrStrategies[options.strategy],
1111
videoPreference: hdrSupported ? { preferHDR: true } : undefined
1212
};
13+
14+
// Add xhrSetup configuration for withCredentials support
15+
if (options.withCredentials) {
16+
config.xhrSetup = function(xhr) {
17+
xhr.withCredentials = true;
18+
};
19+
}
20+
1321
player.tech_.options_.hlsjsConfig = config;
1422
player.on('loadstart', () => qualityLevels(player, options).init());
1523
player.qualityMenu();

src/plugins/cloudinary/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class CloudinaryContext {
9696
this.player.adaptiveStreaming({
9797
...options.adaptiveStreaming,
9898
isDash,
99-
debug: options.debug
99+
debug: options.debug,
100+
withCredentials: options.withCredentials
100101
}).then(() => {
101102
refresh();
102103
});

test/adaptive-streaming.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,71 @@ describe('Adaptive streaming tests', () => {
3535
})()`);
3636
expect(JSON.parse(error)).toEqual(null); // expect no error
3737
});
38+
39+
it('Should support withCredentials for hls sources', async () => {
40+
jest.setTimeout(35000);
41+
await page.waitFor(1000);
42+
43+
const result = await page.evaluate(`(async () => {
44+
// Create a new player with withCredentials
45+
const testPlayer = cloudinary.videoPlayer('test-player', {
46+
cloud_name: 'demo',
47+
withCredentials: true
48+
});
49+
50+
// Set hls source
51+
testPlayer.source('snow_horses', {
52+
sourceTypes: ['hls'],
53+
transformation: { streaming_profile: 'hd' }
54+
});
55+
56+
// Wait for adaptive streaming to load
57+
await new Promise(resolve => setTimeout(resolve, 2000));
58+
59+
// Check if hlsjsConfig has xhrSetup
60+
const tech = testPlayer.videojs.tech();
61+
const hlsjsConfig = tech.options_.hlsjsConfig;
62+
63+
return {
64+
hasXhrSetup: typeof hlsjsConfig.xhrSetup === 'function',
65+
withCredentials: hlsjsConfig.withCredentials
66+
};
67+
})()`);
68+
69+
expect(result.hasXhrSetup).toBe(true);
70+
});
71+
72+
it('Should not configure xhrSetup when withCredentials is false', async () => {
73+
jest.setTimeout(35000);
74+
await page.waitFor(1000);
75+
76+
const result = await page.evaluate(`(async () => {
77+
// Create a new player without withCredentials
78+
const testPlayer = cloudinary.videoPlayer('test-player-2', {
79+
cloud_name: 'demo',
80+
withCredentials: false
81+
});
82+
83+
// Set hls source
84+
testPlayer.source('snow_horses', {
85+
sourceTypes: ['hls'],
86+
transformation: { streaming_profile: 'hd' }
87+
});
88+
89+
// Wait for adaptive streaming to load
90+
await new Promise(resolve => setTimeout(resolve, 2000));
91+
92+
// Check if hlsjsConfig has xhrSetup
93+
const tech = testPlayer.videojs.tech();
94+
const hlsjsConfig = tech.options_.hlsjsConfig;
95+
96+
return {
97+
hasXhrSetup: typeof hlsjsConfig.xhrSetup === 'function',
98+
configExists: !!hlsjsConfig
99+
};
100+
})()`);
101+
102+
expect(result.hasXhrSetup).toBe(false);
103+
expect(result.configExists).toBe(true);
104+
});
38105
});

test/unit/videoSource.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ describe('tests withCredentials', () => {
296296
let srcs = source.generateSources().map(s => s.withCredentials);
297297
expect(srcs[0]).toEqual(false);
298298
});
299+
it('test withCredentials for hls sources', () => {
300+
let sourceDef = {
301+
cloudinaryConfig: cld,
302+
withCredentials: true
303+
};
304+
let source = new VideoSource('sea_turtle', sourceDef);
305+
source.sourceTypes(['hls']);
306+
expect(source.withCredentials).toEqual(true);
307+
});
299308
});
300309

301310
describe('getType method tests', () => {

0 commit comments

Comments
 (0)