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

feat: functionality to gracefully unload / disconnect a stream #57

Merged
merged 1 commit into from
Aug 9, 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
5 changes: 5 additions & 0 deletions demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
// turn:<username>:<password>@turn.eyevinn.technology:3478
const m = server.match(/^turn:(\S+):(\S+)@(\S+):(\d+)/);
if (m) {
const [_, username, credential, host, port] = m;

Check warning on line 92 in demo/demo.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is assigned a value but never used
iceServers.push({
urls: 'turn:' + host + ':' + port,
username: username,
Expand Down Expand Up @@ -151,6 +151,11 @@
await player.load(new URL(channelUrl));
});

const stopButton = document.querySelector<HTMLButtonElement>('#stop');
stopButton?.addEventListener('click', async () => {
await player.unload();
});

clientTimeMsElement = document.querySelector<HTMLSpanElement>('#localTimeMs');
window.setInterval(updateClientClock, 1);
});
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ <h1>Media Server independent WebRTC player</h1>
<label for="channelUrl">URL</label>
<input id="channelUrl" type="text" value="" />
<button id="play">Play</button>
<button id="stop">Stop</button>
<input type="checkbox" id="preroll" />
<label for="preroll">Preroll</label>
<label for="prerollUrl">VMAP</label>
Expand Down
1 change: 1 addition & 0 deletions src/adapters/Adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DEFAULT_CONNECT_TIMEOUT = 2000;

Check warning on line 1 in src/adapters/Adapter.ts

View workflow job for this annotation

GitHub Actions / lint

'DEFAULT_CONNECT_TIMEOUT' is assigned a value but never used

export interface AdapterConnectOptions {
timeout: number;
Expand All @@ -9,4 +9,5 @@
getPeer(): RTCPeerConnection | undefined;
resetPeer(newPeer: RTCPeerConnection): void;
connect(opts?: AdapterConnectOptions): Promise<void>;
disconnect(): Promise<void>;
}
4 changes: 4 additions & 0 deletions src/adapters/EyevinnAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
private localPeer: RTCPeerConnection | undefined;
private channelUrl: URL;
private debug: boolean;
private iceGatheringTimeout: any;

Check warning on line 9 in src/adapters/EyevinnAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
private waitingForCandidates = false;
private resourceUrl: URL | undefined = undefined;

constructor(
peer: RTCPeerConnection,
channelUrl: URL,
onError: (error: string) => void

Check warning on line 16 in src/adapters/EyevinnAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

'onError' is defined but never used
) {
this.channelUrl = channelUrl;
this.debug = true;
Expand Down Expand Up @@ -60,17 +60,21 @@
);
}

async disconnect() {
return;
}

private log(...args: any[]) {

Check warning on line 67 in src/adapters/EyevinnAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (this.debug) {
console.log('WebRTC-player', ...args);
}
}

private error(...args: any[]) {

Check warning on line 73 in src/adapters/EyevinnAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
console.error('WebRTC-player', ...args);
}

private onIceGatheringStateChange(event: Event) {

Check warning on line 77 in src/adapters/EyevinnAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

'event' is defined but never used
if (!this.localPeer) {
this.log('Local RTC peer not initialized');
return;
Expand Down
12 changes: 12 additions & 0 deletions src/adapters/WHEPAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
return this.localPeer;
}

async connect(opts?: AdapterConnectOptions) {

Check warning on line 54 in src/adapters/WHEPAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

'opts' is defined but never used
try {
await this.initSdpExchange();
} catch (error) {
Expand All @@ -59,6 +59,18 @@
}
}

async disconnect() {
if (this.resource) {
this.log(`Disconnecting by removing resource ${this.resource}`);
const response = await fetch(this.resource, {
method: 'DELETE'
});
if (response.ok) {
this.log(`Successfully removed resource`);
}
}
}

private async initSdpExchange() {
clearTimeout(this.iceGatheringTimeout);

Expand Down Expand Up @@ -109,7 +121,7 @@
this.log(candidate.candidate);
}

private onIceGatheringStateChange(event: Event) {

Check warning on line 124 in src/adapters/WHEPAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

'event' is defined but never used
if (this.localPeer) {
this.log('IceGatheringState', this.localPeer.iceGatheringState);
if (
Expand Down Expand Up @@ -147,7 +159,7 @@
private getResouceUrlFromHeaders(headers: Headers): string | null {
if (headers.get('Location') && headers.get('Location')?.match(/^\//)) {
const resourceUrl = new URL(
headers.get('Location')!,

Check warning on line 162 in src/adapters/WHEPAdapter.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
this.channelUrl.origin
);
return resourceUrl.toString();
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/WHPPAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class WHPPAdapter implements Adapter {
}
}

async disconnect() {
return;
}

private log(...args: any[]) {
if (this.debug) {
console.log('WebRTC-player', ...args);
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export class WebRTCPlayer extends EventEmitter {
this.videoElement.muted = false;
}

async unload() {
await this.adapter.disconnect();
this.stop();
}

stop() {
clearInterval(this.statsInterval);
this.peer.close();
Expand Down