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 WebRTC Region Issue #427

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .changeset/silent-pillows-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@livepeer/core-web': patch
'@livepeer/core': patch
'@livepeer/core-react': patch
'@livepeer/react': patch
---

**Fix:** fixed a rare bug where coturn could be located in a different region from playback, resulting in playback failing.
8 changes: 4 additions & 4 deletions .changeset/stale-tigers-buy.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
'@livepeer/core': patch
'@livepeer/core-web': patch
'@livepeer/core-react': patch
'@livepeer/react': patch
'@livepeer/core': minor
'@livepeer/core-web': minor
'@livepeer/core-react': minor
'@livepeer/react': minor
---

**Feature:** added track selectors to WebRTC so developers can override video and audio tracks.
Expand Down
36 changes: 30 additions & 6 deletions packages/core-web/src/media/browser/webrtc/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export async function constructClientOffer(
return null;
}

// Regular expression to match the playback ID at the end of the URL
// It looks for a string that follows the last "+" or "/" and continues to the end of the pathname
const playbackIdPattern = /([/+])([^/+?]+)$/;
const REPLACE_PLACEHOLDER = 'PLAYBACK_ID';

let cachedRedirectUrl: URL | null = null;

async function postSDPOffer(
endpoint: string,
data: string,
Expand All @@ -190,6 +197,20 @@ async function postSDPOffer(

const url = new URL(endpoint);

const parsedMatches = url.pathname.match(playbackIdPattern);

// if we both have a cached redirect URL and a match for the playback ID,
// use these to shortcut the typical webrtc redirect flow
if (cachedRedirectUrl && parsedMatches?.[2]) {
const clonedCachedUrl = new URL(cachedRedirectUrl);

url.host = clonedCachedUrl.host;
url.pathname = clonedCachedUrl.pathname.replace(
REPLACE_PLACEHOLDER,
parsedMatches[2],
);
}

if (config?.constant) {
url.searchParams.append('constant', 'true');
}
Expand Down Expand Up @@ -217,18 +238,16 @@ async function postSDPOffer(
return response;
}

let cachedRedirectHost: string | null = null;

export async function getRedirectUrl(
endpoint: string,
abortController: AbortController,
timeout?: number,
) {
try {
if (cachedRedirectHost) {
if (cachedRedirectUrl) {
const inputUrl = new URL(endpoint);

inputUrl.host = cachedRedirectHost;
inputUrl.host = cachedRedirectUrl.host;

return inputUrl;
}
Expand All @@ -247,8 +266,13 @@ export async function getRedirectUrl(

const parsedUrl = new URL(response.url);

if (parsedUrl?.host) {
cachedRedirectHost = parsedUrl.host;
if (parsedUrl) {
const cachedUrl = new URL(parsedUrl);
cachedUrl.pathname = cachedUrl.pathname.replace(
playbackIdPattern,
`$1${REPLACE_PLACEHOLDER}`,
);
cachedRedirectUrl = cachedUrl;
}

return parsedUrl;
Expand Down
Loading