Skip to content

Commit

Permalink
Enhance RoomSession join() signature (#631)
Browse files Browse the repository at this point in the history
* remove useless const

* update VideoAuthorization interface

* add getJoinMediaParams with its own tests

* use getJoinMediaParams on RoomSession join

* update error msg

* remove unused interface

* allow rtcPeer to be sendonly now

* move default media constraints from RoomSession to join method

* improve override and add comments

* fix tests

* tests

* changesets
  • Loading branch information
edolix authored Aug 26, 2022
1 parent f1102bb commit c00b343
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 101 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-badgers-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/webrtc': patch
---

Allow RTC connection with sendonly direction
5 changes: 5 additions & 0 deletions .changeset/quiet-trains-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/js': minor
---

Enhance `.join()` signature with an optional argument to control the media to send and receive.
5 changes: 5 additions & 0 deletions .changeset/tricky-geckos-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/core': patch
---

Update internal interfaces for the Authorization block
4 changes: 4 additions & 0 deletions packages/core/src/redux/features/session/sessionSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ describe('SessionState Tests', () => {
authState: {
media_allowed: 'all',
audio_allowed: 'both',
join_as: 'member',
project: '8f0a119a-cda7-4497-a47d-c81493b824d4',
resource: '9c80f1e8-9430-4070-a043-937eb3a96b38',
room: {
name: 'lobby',
display_name: 'Lobby',
scopes: ['room.self.audio_mute', 'room.self.audio_unmute'],
meta: {},
},
scope_id: '26675883-8499-4ee9-85eb-691c4aa209f8',
scopes: ['video'],
Expand All @@ -33,6 +36,7 @@ describe('SessionState Tests', () => {
type: 'video',
user_name: 'Joe',
video_allowed: 'both',
meta: {},
},
authError: undefined,
authCount: 1,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,20 @@ export const rpcConnectResultVRT: RPCConnectResult = {
scopes: ['video'],
scope_id: '26675883-8499-4ee9-85eb-691c4aa209f8',
resource: '9c80f1e8-9430-4070-a043-937eb3a96b38',
join_as: 'member',
user_name: 'Joe',
room: {
name: 'lobby',
display_name: 'Lobby',
scopes: ['room.self.audio_mute', 'room.self.audio_unmute'],
meta: {},
},
signature:
'SGZtkRD9fvuBAOUp1UF56zESxdEvGT6qSGZtkRD9fvuBAOUp1UF56zESxdEvGT6q',
media_allowed: 'all',
audio_allowed: 'both',
video_allowed: 'both',
meta: {},
},
protocol:
'signalwire_SGZtkRD9fvuBAOUp1UF56zESxdEvGT6qSGZtkRD9fvuBAOUp1UF56zESxdEvGT6q_03e8c927-8ea3-4661-86d5-778c3e03296a_8f0a119a-cda7-4497-a47d-c81493b824d4',
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export interface SessionRequestObject {
}

export type MediaAllowed = 'all' | 'audio-only' | 'video-only'
export type MediaDirectionAllowed = 'none' | 'receive' | 'both'
type MediaDirectionAllowed = 'none' | 'receive' | 'both'
type AudioAllowed = MediaDirectionAllowed
type VideoAllowed = MediaDirectionAllowed

Expand All @@ -169,16 +169,20 @@ export interface VideoAuthorization {
scopes: string[]
scope_id: string
resource: string
join_as: 'member' | 'audience'
user_name: string
room?: {
name: string
display_name: string
scopes: string[]
meta: Record<any, any>
}
signature: string
expires_at?: number
media_allowed: MediaAllowed
media_allowed?: MediaAllowed
audio_allowed: AudioAllowed
video_allowed: VideoAllowed
meta: Record<any, any>
}

export type ChatAuthorizationChannels = Record<
Expand Down
10 changes: 6 additions & 4 deletions packages/js/src/BaseRoomSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
RoomMethods,
StartScreenShareOptions,
RoomSessionConnectionContract,
RoomSessionJoinAudienceParams,
BaseRoomSessionJoinParams,
} from './utils/interfaces'
import {
ROOM_COMPONENT_LISTENERS,
Expand All @@ -55,9 +55,11 @@ export interface BaseRoomSession<T>
/**
* Joins the room session.
*/
join(): Promise<T>
/** @internal */
joinAudience(options?: RoomSessionJoinAudienceParams): Promise<T>
join(options?: BaseRoomSessionJoinParams): Promise<T>

/** @deprecated Use {@link join} instead. */
joinAudience(options?: BaseRoomSessionJoinParams): Promise<T>

/**
* Leaves the room. This detaches all the locally originating streams from the
* room.
Expand Down
97 changes: 50 additions & 47 deletions packages/js/src/RoomSession.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { UserOptions, getLogger, VideoAuthorization } from '@signalwire/core'
import { createClient } from './createClient'
import { BaseRoomSession } from './BaseRoomSession'
import {
getJoinAudienceMediaParams,
isValidJoinAudienceMediaParams,
} from './utils/roomSession'
import { checkMediaParams, getJoinMediaParams } from './utils/roomSession'
import type { MakeRoomOptions } from './Client'
import type { RoomSessionJoinAudienceParams } from './utils/interfaces'

const VIDEO_CONSTRAINTS: MediaTrackConstraints = {
width: { ideal: 1280, min: 320 },
height: { ideal: 720, min: 180 },
aspectRatio: { ideal: 16 / 9 },
}
import type { BaseRoomSessionJoinParams } from './utils/interfaces'

/**
* List of properties/methods the user shouldn't be able to
Expand Down Expand Up @@ -86,8 +77,8 @@ export interface RoomSession extends BaseRoomSession<RoomSession> {
*/
export const RoomSession = function (roomOptions: RoomSessionOptions) {
const {
audio = true,
video = true,
audio: audioFromConstructor,
video: videoFromConstructor,
iceServers,
rootElement,
applyLocalVideoOverlay = true,
Expand All @@ -97,10 +88,19 @@ export const RoomSession = function (roomOptions: RoomSessionOptions) {
...userOptions
} = roomOptions

const deprecatedParams = ['audio', 'video']
deprecatedParams.forEach((param) => {
if (param in roomOptions) {
getLogger().warn(
`The '${param}' parameter on the RoomSession constructor is deprecated. Set it on the '.join()' function instead.`
)
}
})

const client = createClient<RoomSession>(userOptions)
const room = client.rooms.makeRoomObject({
audio,
video: video === true ? VIDEO_CONSTRAINTS : video,
// audio,
// video: video === true ? VIDEO_CONSTRAINTS : video,
negotiateAudio: true,
negotiateVideo: true,
iceServers,
Expand All @@ -118,57 +118,52 @@ export const RoomSession = function (roomOptions: RoomSessionOptions) {
client.disconnect()
})

const join = () => {
const join = (params?: BaseRoomSessionJoinParams) => {
return new Promise(async (resolve, reject) => {
try {
// @ts-expect-error
room.attachPreConnectWorkers()

await client.connect()

room.once('room.subscribed', (payload) => {
// @ts-expect-error
room.attachOnSubscribedWorkers(payload)
resolve(room)
})

await room.join()
} catch (error) {
getLogger().error('RoomSession Join', error)
// Disconnect the underlay client in case of media/signaling errors
client.disconnect()

reject(error)
}
})
}
// Fallback to the constructor values for backwards compat.
const audio = params?.audio ?? audioFromConstructor
const video = params?.video ?? videoFromConstructor

const joinAudience = (params?: RoomSessionJoinAudienceParams) => {
return new Promise(async (resolve, reject) => {
try {
// @ts-expect-error
room.attachPreConnectWorkers()

const session = await client.connect()

// @ts-expect-error
const authState: VideoAuthorization = session._sessionAuthState
const mediaOptions = getJoinAudienceMediaParams({
const authState: VideoAuthorization = client._sessionAuthState
const mediaOptions = getJoinMediaParams({
authState,
audio,
video,
...params,
})

if (!isValidJoinAudienceMediaParams(mediaOptions)) {
await session.disconnect()
if (!checkMediaParams(mediaOptions)) {
client.disconnect()
return reject(
new Error(
'[joinAudience] Either (or both) `audio` and `video` must be `true` when calling this method.'
`Invalid arguments to join the room. The token used has join_as: '${
authState.join_as
}'. \n${JSON.stringify(params, null, 2)}\n`
)
)
}

/**
* audio and video might be objects with MediaStreamConstraints
* so if we must send media, we make sure to use the user's
* preferences.
* Note: params.sendAudio: `true` will override audio: `false` so
* we're using `||` instead of `??` for that reason.
*/
// @ts-expect-error
room.updateMediaOptions(mediaOptions)
room.updateMediaOptions({
audio: mediaOptions.mustSendAudio ? audio || true : false,
video: mediaOptions.mustSendVideo ? video || true : false,
negotiateAudio: mediaOptions.mustRecvAudio,
negotiateVideo: mediaOptions.mustRecvVideo,
})

room.once('room.subscribed', (payload) => {
// @ts-expect-error
Expand All @@ -178,7 +173,7 @@ export const RoomSession = function (roomOptions: RoomSessionOptions) {

await room.join()
} catch (error) {
getLogger().error('RoomSession JoinAudience', error)
getLogger().error('RoomSession Join', error)
// Disconnect the underlay client in case of media/signaling errors
client.disconnect()

Expand All @@ -187,6 +182,14 @@ export const RoomSession = function (roomOptions: RoomSessionOptions) {
})
}

/** @deprecated */
const joinAudience = (params?: BaseRoomSessionJoinParams) => {
getLogger().warn(
`The 'joinAudience(params)' method is deprecated. Please, use 'join(params)' instead.`
)
return join(params)
}

const interceptors = {
join,
joinAudience,
Expand Down
6 changes: 5 additions & 1 deletion packages/js/src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,13 @@ export interface RoomSessionDeviceMethods
export interface RoomScreenShareMethods
extends RoomMemberSelfMethodsInterface {}

export interface RoomSessionJoinAudienceParams {
export interface BaseRoomSessionJoinParams {
audio?: MediaStreamConstraints['audio']
video?: MediaStreamConstraints['video']
receiveAudio?: boolean
receiveVideo?: boolean
sendAudio?: boolean
sendVideo?: boolean
}

export type PagingCursor =
Expand Down
Loading

0 comments on commit c00b343

Please sign in to comment.