Skip to content

Commit

Permalink
Add incoming session filter based on protocols
Browse files Browse the repository at this point in the history
* Add one more filter based on protocols in incoming sessions
  • Loading branch information
JoaoMario109 authored and rafaellehmkuhl committed Apr 26, 2024
1 parent a6f5903 commit 5cf2ff1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
31 changes: 29 additions & 2 deletions src/composables/webRTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class WebRTCManager {
private session: Session | undefined
private rtcConfiguration: RTCConfiguration
private selectedICEIPs: string[] = []
private selectedICEProtocols: string[] = []

private hasEnded = false
private signaller: Signaller
Expand Down Expand Up @@ -81,11 +82,17 @@ export class WebRTCManager {
/**
*
* @param { Ref<Stream | undefined> } selectedStream - Stream to receive stream from
* @param { Ref<string[]> } selectedICEIPs
* @param { Ref<string[]> } selectedICEIPs - ICE IPs allowed to be used in the connection
* @param { Ref<string[]> } selectedICEProtocols - ICE protocols allowed to be used in the connection
* @returns { startStreamReturn }
*/
public startStream(selectedStream: Ref<Stream | undefined>, selectedICEIPs: Ref<string[]>): startStreamReturn {
public startStream(
selectedStream: Ref<Stream | undefined>,
selectedICEIPs: Ref<string[]>,
selectedICEProtocols: Ref<string[]>
): startStreamReturn {
this.selectedICEIPs = selectedICEIPs.value
this.selectedICEProtocols = selectedICEProtocols.value

watch(selectedStream, (newStream, oldStream) => {
if (newStream?.id === oldStream?.id) {
Expand Down Expand Up @@ -122,6 +129,25 @@ export class WebRTCManager {
}
})

watch(selectedICEProtocols, (newProtocols, oldProtocols) => {
if (newProtocols === oldProtocols) {
return
}

const msg = `Selected Protocols changed from "${oldProtocols}" to "${newProtocols}".`
console.debug('[WebRTC] ' + msg)

this.selectedICEProtocols = newProtocols

if (this.streamName !== undefined) {
this.stopSession(msg)
}

if (this.streamName !== undefined) {
this.startSession()
}
})

return {
mediaStream: this.mediaStream,
connected: this.connected,
Expand Down Expand Up @@ -321,6 +347,7 @@ export class WebRTCManager {
this.signaller,
this.rtcConfiguration,
this.selectedICEIPs,
this.selectedICEProtocols,
(event: RTCTrackEvent): void => this.onTrackAdded(event),
(): void => this.onPeerConnected(),
(availableICEIPs: string[]) => (this.availableICEIPs.value = availableICEIPs),
Expand Down
21 changes: 19 additions & 2 deletions src/libs/webrtc/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Session {
private peerConnection: RTCPeerConnection
private availableICEIPs: string[]
private selectedICEIPs: string[]
private selectedICEProtocols: string[]
public rtcConfiguration: RTCConfiguration
public onTrackAdded?: OnTrackAddedCallback
public onPeerConnected?: OnPeerConnectedCallback
Expand All @@ -36,6 +37,7 @@ export class Session {
* @param {Signaller} signaller - The Signaller instance for this Session to use
* @param {RTCConfiguration} rtcConfiguration - Configuration for the RTC connection, such as Turn and Stun servers
* @param {string[]} selectedICEIPs - A whitelist for ICE IP addresses, ignored if empty
* @param {string[]} selectedICEProtocols - A whitelist for protocols allowed, ignored if empty
* @param {OnTrackAddedCallback} onTrackAdded - An optional callback for when a track is added to this session
* @param {OnPeerConnectedCallback} onPeerConnected - An optional callback for when the peer is connected
* @param {onNewIceRemoteAddressCallback} onNewIceRemoteAddress - An optional callback for when a new ICE candidate IP addres is available
Expand All @@ -49,6 +51,7 @@ export class Session {
signaller: Signaller,
rtcConfiguration: RTCConfiguration,
selectedICEIPs: string[] = [],
selectedICEProtocols: string[] = [],
onTrackAdded?: OnTrackAddedCallback,
onPeerConnected?: OnPeerConnectedCallback,
onNewIceRemoteAddress?: onNewIceRemoteAddressCallback,
Expand All @@ -69,6 +72,7 @@ export class Session {
this.ended = false
this.availableICEIPs = []
this.selectedICEIPs = selectedICEIPs
this.selectedICEProtocols = selectedICEProtocols

this.peerConnection = this.createRTCPeerConnection(rtcConfiguration)

Expand Down Expand Up @@ -205,8 +209,21 @@ export class Session {
!this.selectedICEIPs.isEmpty() &&
!this.selectedICEIPs.some((address) => candidate.candidate!.includes(address))
) {
this.onStatusChange?.(`Ignoring ICE candidate ${candidate.candidate}`)
console.debug(`[WebRTC] [Session] ICE candidate ignored: ${JSON.stringify(candidate, null, 4)}`)
this.onStatusChange?.(`Ignoring ICE candidate ${candidate.candidate} by IP filter`)
console.debug(`[WebRTC] [Session] ICE candidate ignored by IP filter: ${JSON.stringify(candidate, null, 4)}`)
return
}

if (
candidate.candidate &&
Array.isArray(this.selectedICEIPs) &&
!this.selectedICEProtocols.isEmpty() &&
!this.selectedICEProtocols.some((protocol) => candidate.candidate!.toLowerCase().includes(protocol))
) {
this.onStatusChange?.(`Ignoring ICE candidate ${candidate.candidate} by Protocol filter`)
console.debug(
`[WebRTC] [Session] ICE candidate ignored by Protocol filter: ${JSON.stringify(candidate, null, 4)}`
)
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/stores/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useVideoStore = defineStore('video', () => {
const activateStream = (streamName: string): void => {
const stream = ref()
const webRtcManager = new WebRTCManager(webRTCSignallingURI.val, rtcConfiguration)
const { mediaStream, connected } = webRtcManager.startStream(stream, allowedIceIps)
const { mediaStream, connected } = webRtcManager.startStream(stream, allowedIceIps, allowedIceProtocols)
activeStreams.value[streamName] = {
// @ts-ignore: This is actually not reactive
stream: stream,
Expand Down

0 comments on commit 5cf2ff1

Please sign in to comment.