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: set/create events #71

Merged
merged 9 commits into from
Feb 14, 2024
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"Wcme",
"WCME",
"webex",
"webrtc"
"webrtc",
"sdpchange"
],
"flagWords": [],
"ignorePaths": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webex/webrtc-core",
"version": "2.4.0",
"version": "2.4.1",
SomeBody16 marked this conversation as resolved.
Show resolved Hide resolved
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"browser": "dist/umd/index.js",
Expand Down
30 changes: 28 additions & 2 deletions src/peer-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ type IceGatheringStateChangeEvent = {
target: EventTarget | null;
};

type SdpChangeEvent = {
type: 'offer' | 'answer';
munged: boolean;
sdp?: string;
};
SomeBody16 marked this conversation as resolved.
Show resolved Hide resolved

enum PeerConnectionEvents {
IceGatheringStateChange = 'icegatheringstatechange',
ConnectionStateChange = 'connectionstatechange',
SdpChange = 'sdpchange',
}

interface PeerConnectionEventHandlers extends EventMap {
[PeerConnectionEvents.IceGatheringStateChange]: (ev: IceGatheringStateChangeEvent) => void;
[PeerConnectionEvents.ConnectionStateChange]: (state: ConnectionState) => void;
[PeerConnectionEvents.SdpChange]: (ev: SdpChangeEvent) => void;
}

type ConnectionType = 'UDP' | 'TCP' | 'TURN-TLS' | 'TURN-TCP' | 'TURN-UDP' | 'unknown';
Expand Down Expand Up @@ -186,7 +194,13 @@ class PeerConnection extends EventEmitter<PeerConnectionEventHandlers> {
* That received offer should be delivered through the signaling server to a remote peer.
*/
async createOffer(options?: RTCOfferOptions): Promise<RTCSessionDescriptionInit> {
return this.pc.createOffer(options);
const offer = await this.pc.createOffer(options);
this.emit(PeerConnection.Events.SdpChange, {
type: 'offer',
munged: false,
sdp: offer.sdp,
});
return offer;
}

/**
Expand Down Expand Up @@ -215,6 +229,12 @@ class PeerConnection extends EventEmitter<PeerConnectionEventHandlers> {
});
}

this.emit(PeerConnection.Events.SdpChange, {
type: 'offer',
munged: true,
sdp: description?.sdp,
});

return this.pc.setLocalDescription(description);
SomeBody16 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -230,6 +250,12 @@ class PeerConnection extends EventEmitter<PeerConnectionEventHandlers> {
async setRemoteDescription(
description: RTCSessionDescription | RTCSessionDescriptionInit
): Promise<void> {
this.emit(PeerConnection.Events.SdpChange, {
type: 'answer',
munged: true,
sdp: description.sdp,
});

return this.pc.setRemoteDescription(description);
SomeBody16 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -344,4 +370,4 @@ class PeerConnection extends EventEmitter<PeerConnectionEventHandlers> {
}

export { ConnectionState } from './connection-state-handler';
export { MediaStreamTrackKind, RTCDataChannelOptions, PeerConnection };
export { MediaStreamTrackKind, RTCDataChannelOptions, PeerConnection, SdpChangeEvent };
Loading