From 057e0b164548561fd7ecbaa67003c8a90fec1fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Birm=C3=A9?= Date: Thu, 4 Jan 2024 19:53:34 +0100 Subject: [PATCH] fix(#45): resolve redirect before handing over to engine --- docs/changelog.md | 4 ++-- src/plugins/plugin_loop.ts | 4 +++- src/plugins/plugin_playlist.ts | 7 +++++-- src/plugins/plugin_webhook.test.ts | 2 +- src/plugins/plugin_webhook.ts | 5 +++-- src/plugins/utils.ts | 9 +++++++++ 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index a2734f5..c78bfd9 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -12,7 +12,7 @@ Channel Engine v4.3.11 including the following bugfixes and features: - Option to protect session health endpoint with a key - Fix timeOffset - Once enabled, Forever - Fix missing Newlines in Dummy Webvtt Response -- Fix performance bottlenecks when running in High Availability mode with Redis. +- Fix performance bottlenecks when running in High Availability mode with Redis. - Fix disable eventstream by default - Fix HA & Demux - Audio Increment Not Respecting Position Diff-Threshold - Fix Subtitle Track Playhead is dependant Of Video Track @@ -28,7 +28,7 @@ New Features: ### Version 1.8.1 -Verify URLs that must be HLS URLs +Verify URLs that must be HLS URLs ### Version 1.8.2 diff --git a/src/plugins/plugin_loop.ts b/src/plugins/plugin_loop.ts index 39120ce..f53f402 100644 --- a/src/plugins/plugin_loop.ts +++ b/src/plugins/plugin_loop.ts @@ -13,7 +13,8 @@ import { getDefaultChannelAudioProfile, getDefaultChannelVideoProfile, getDefaultChannelSubtitleProfile, - getVodUrlWithPreroll + getVodUrlWithPreroll, + resolveRedirect } from './utils'; class LoopAssetManager implements IAssetManager { @@ -30,6 +31,7 @@ class LoopAssetManager implements IAssetManager { // eslint-disable-next-line @typescript-eslint/no-unused-vars async getNextVod(vodRequest: VodRequest): Promise { let hlsUrl = this.vodToLoop.toString(); + hlsUrl = await resolveRedirect(this.vodToLoop.toString()); if (this.prerollVod) { hlsUrl = getVodUrlWithPreroll( this.vodToLoop.toString(), diff --git a/src/plugins/plugin_playlist.ts b/src/plugins/plugin_playlist.ts index f552893..74dc576 100644 --- a/src/plugins/plugin_playlist.ts +++ b/src/plugins/plugin_playlist.ts @@ -14,7 +14,8 @@ import { getDefaultChannelAudioProfile, getDefaultChannelSubtitleProfile, getDefaultChannelVideoProfile, - getVodUrlWithPreroll + getVodUrlWithPreroll, + resolveRedirect } from './utils'; interface PlaylistUrl { @@ -92,7 +93,9 @@ class PlaylistAssetManager implements IAssetManager { await this._updatePlaylist(vodRequest.playlistId); } if (playlist.position !== -1) { - let hlsUrl = playlist.hlsUrls[playlist.position].toString(); + let hlsUrl = await resolveRedirect( + playlist.hlsUrls[playlist.position].toString() + ); if (this.prerollVod) { hlsUrl = getVodUrlWithPreroll( playlist.hlsUrls[playlist.position].toString(), diff --git a/src/plugins/plugin_webhook.test.ts b/src/plugins/plugin_webhook.test.ts index 59c4512..f7ced25 100644 --- a/src/plugins/plugin_webhook.test.ts +++ b/src/plugins/plugin_webhook.test.ts @@ -37,7 +37,7 @@ describe('webhook plugin', () => { expect(fetchMock.mock.calls[0][0]).toEqual( 'http://localhost:8002/nextVod?channelId=test' ); - expect(fetchMock.mock.calls[1][0]).toEqual( + expect(fetchMock.mock.calls[2][0]).toEqual( 'http://localhost:8002/nextVod?channelId=test' ); }); diff --git a/src/plugins/plugin_webhook.ts b/src/plugins/plugin_webhook.ts index c40f26c..b81259f 100644 --- a/src/plugins/plugin_webhook.ts +++ b/src/plugins/plugin_webhook.ts @@ -14,7 +14,8 @@ import { getDefaultChannelAudioProfile, getDefaultChannelVideoProfile, getDefaultChannelSubtitleProfile, - getVodUrlWithPreroll + getVodUrlWithPreroll, + resolveRedirect } from './utils'; export interface WebHookNextVodResponse { @@ -52,7 +53,7 @@ class WebHookAssetManager implements IAssetManager { }); if (response.ok) { const payload: WebHookNextVodResponse = await response.json(); - let hlsUrl = payload.hlsUrl; + let hlsUrl = await resolveRedirect(payload.hlsUrl); if (payload.prerollUrl && payload.prerollDurationMs) { hlsUrl = getVodUrlWithPreroll( hlsUrl, diff --git a/src/plugins/utils.ts b/src/plugins/utils.ts index e3c6a39..3012fb4 100644 --- a/src/plugins/utils.ts +++ b/src/plugins/utils.ts @@ -259,3 +259,12 @@ export function getVodUrlWithPreroll( } return url; } + +export async function resolveRedirect(url: string) { + const response = await fetch(url); + if (response.redirected) { + console.log('Redirect: ' + response.url); + return response.url || url; + } + return url; +}