Skip to content

Commit

Permalink
feat: adjust createRoom() and getRoom() data (#101)
Browse files Browse the repository at this point in the history
* Adjust with room API endpoints

---------

Co-authored-by: Yohan Totting <[email protected]>
Co-authored-by: Faiq Naufal <[email protected]>
  • Loading branch information
3 people authored Mar 26, 2024
1 parent 05438b5 commit 8ff1545
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 73 deletions.
77 changes: 60 additions & 17 deletions packages/room/api/api-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { createFetcher } from './fetcher.js'
import type { createApi } from './api.js'
import type { SharedType } from '../../internal/types/types.js'

export declare namespace RoomAPIType {
type CreateFetcher = typeof createFetcher
Expand Down Expand Up @@ -29,7 +30,20 @@ export declare namespace RoomAPIType {
enable_vad?: boolean
}

type Bitrates = {
type BitratesCamelCase = {
audioRed: number
audio: number
video: number
videoHigh: number
videoHighPixels: number
videoMid: number
videoMidPixels: number
videoLow: number
videoLowPixels: number
initialBandwidth: number
}

type BitratesSnakeCase = {
audio: number
audio_red: number
video: number
Expand All @@ -42,35 +56,64 @@ export declare namespace RoomAPIType {
initial_bandwidth: number
}

type QualityPreset = {
sid: number
tid: number
}

type QualityPresets = {
high: QualityPreset
low: QualityPreset
mid: QualityPreset
}

type RoomOptions = {
bitrates: BitratesCamelCase
codecs: string[]
emptyRoomTimeoutMS: number
pliIntervalMS: number
qualityPresets: QualityPresets
}

type RoomUserOptions = SharedType.DeepPartial<RoomOptions>

type RoomResponse = {
id: string
name: string
options: {
bitrates: BitratesSnakeCase
codecs: string[]
empty_room_timeout_ns: number
pli_interval_ns: number
quality_presets: QualityPresets
}
}

type BaseResponseBody = {
code: number
ok: boolean
message: string
}

type CreateRoomResponseBody = BaseResponseBody & {
data: {
room_id: string
name: string
bitrate_configs: Bitrates
codec_preferences: string[]
}
type RoomResponseBody = BaseResponseBody & {
data: RoomResponse
}

type GetRoomResponseBody = BaseResponseBody & {
data: {
room_id: string
name: string
bitrate_configs: Bitrates
codec_preferences: string[]
}
type Room = {
id: string
name: string
options: RoomOptions
}

type RoomReturnBody = BaseResponseBody & {
data: Room
}

type RegisterClientResponseBody = BaseResponseBody & {
data: {
client_id: string
name: string
bitrates: Bitrates
bitrates: BitratesSnakeCase
}
}

Expand All @@ -94,7 +137,7 @@ export declare namespace RoomAPIType {
data: {
client_id: string
name: string
bitrates: Bitrates
bitrates: BitratesSnakeCase
}
}

Expand Down
168 changes: 132 additions & 36 deletions packages/room/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
export const createApi = ({ fetcher }) => {
const Api = class {
_fetcher
/** @type {import('../room-types.js').RoomType.Room | null} */
_room = null

constructor() {
this._fetcher = fetcher
Expand All @@ -12,37 +10,113 @@ export const createApi = ({ fetcher }) => {
/**
* @param {string} [name]
* @param {string} [id]
* @param {import('./api-types.js').RoomAPIType.RoomUserOptions} [options]
* @returns {Promise<import('./api-types.js').RoomAPIType.RoomReturnBody>}
*/
createRoom = async (name = '', id = '') => {
/** @type {import('./api-types.js').RoomAPIType.CreateRoomResponseBody} */
createRoom = async (name = '', id = '', options) => {
const requestOptions = {}

if (typeof options !== 'undefined') {
requestOptions.bitrates =
typeof options.bitrates !== 'undefined'
? {
audio: options.bitrates.audio || 0,
audio_red: options.bitrates.audioRed || 0,
video: options.bitrates.video || 0,
video_high: options.bitrates.videoHigh || 0,
video_high_pixels: options.bitrates.videoHighPixels || 0,
video_mid: options.bitrates.videoMid || 0,
video_mid_pixels: options.bitrates.videoMidPixels || 0,
video_low: options.bitrates.videoLow || 0,
video_low_pixels: options.bitrates.videoLowPixels || 0,
initial_bandwidth: options.bitrates.initialBandwidth || 0,
}
: {}

requestOptions.quality_presets =
typeof options.qualityPresets !== 'undefined'
? {
high: {
sid: options.qualityPresets.high?.sid || 2,
tid: options.qualityPresets.high?.tid || 2,
},
mid: {
sid: options.qualityPresets.mid?.sid || 1,
tid: options.qualityPresets.mid?.tid || 1,
},
low: {
sid: options.qualityPresets.low?.sid || 0,
tid: options.qualityPresets.low?.tid || 0,
},
}
: {}

requestOptions.codecs = options.codecs || []
requestOptions.pli_interval_ns = options.pliIntervalMS
? options.pliIntervalMS * 1_000_000
: 0

requestOptions.empty_room_timeout_ns = options.emptyRoomTimeoutMS
? options.emptyRoomTimeoutMS * 1_000_000
: 0
}

const body = {
id,
name,
options: requestOptions,
}

/** @type {import('./api-types.js').RoomAPIType.RoomResponseBody} */
const response = await this._fetcher.post(`/rooms/create`, {
headers: { Authorization: 'Bearer ' + this._fetcher.getApiKey() },
body: JSON.stringify({ name, id }),
body: JSON.stringify(body),
})

const data = response.data || {}
const bitrates = data.bitrate_configs || {}
const bitrates = data.options?.bitrates || {}
const qualityPresets = data.options?.quality_presets || {}

/** @type {import('./api-types.js').RoomAPIType.RoomReturnBody} */
const room = {
code: response.code || 500,
ok: response.ok || false,
message: response.message || '',
data: {
roomId: data.room_id || '',
roomName: data.name || '',
bitrates: {
audio: bitrates.audio || 0,
audioRed: bitrates.audio_red || 0,
video: bitrates.video || 0,
videoHigh: bitrates.video_high || 0,
videoHighPixels: bitrates.video_high_pixels || 0,
videoMid: bitrates.video_mid || 0,
videoMidPixels: bitrates.video_mid_pixels || 0,
videoLow: bitrates.video_low || 0,
videoLowPixels: bitrates.video_low_pixels || 0,
initialBandwidth: bitrates.initial_bandwidth || 0,
id: data.id || '',
name: data.name || '',
options: {
bitrates: {
audio: bitrates.audio || 0,
audioRed: bitrates.audio_red || 0,
video: bitrates.video || 0,
videoHigh: bitrates.video_high || 0,
videoHighPixels: bitrates.video_high_pixels || 0,
videoMid: bitrates.video_mid || 0,
videoMidPixels: bitrates.video_mid_pixels || 0,
videoLow: bitrates.video_low || 0,
videoLowPixels: bitrates.video_low_pixels || 0,
initialBandwidth: bitrates.initial_bandwidth || 0,
},
codecs: data.options.codecs || [],
pliIntervalMS: data.options.pli_interval_ns / 1_000_000 || 0,
emptyRoomTimeoutMS:
data.options.empty_room_timeout_ns / 1_000_000 || 0,
qualityPresets: {
high: {
sid: qualityPresets.high?.sid,
tid: qualityPresets.high?.tid,
},
mid: {
sid: qualityPresets.mid?.sid,
tid: qualityPresets.mid?.tid,
},
low: {
sid: qualityPresets.low?.sid,
tid: qualityPresets.low?.tid,
},
},
},
codecPreferences: data.codec_preferences || [],
},
}

Expand All @@ -51,40 +125,62 @@ export const createApi = ({ fetcher }) => {

/**
* @param {string} roomId
* @returns {Promise<import('./api-types.js').RoomAPIType.RoomReturnBody>}
*/
getRoom = async (roomId) => {
if (typeof roomId !== 'string' || roomId.trim().length === 0) {
throw new Error('Room ID must be a valid string')
}

/** @type {import('./api-types.js').RoomAPIType.GetRoomResponseBody} */
/** @type {import('./api-types.js').RoomAPIType.RoomResponseBody} */
const response = await this._fetcher.get(`/rooms/${roomId}`, {
headers: { Authorization: 'Bearer ' + this._fetcher.getApiKey() },
})

const data = response.data || {}
const bitrates = data.bitrate_configs || {}
const bitrates = data.options?.bitrates || {}
const qualityPresets = data.options?.quality_presets || {}

/** @type {import('./api-types.js').RoomAPIType.RoomReturnBody} */
const room = {
code: response.code || 500,
ok: response.ok || false,
message: response.message || '',
data: {
roomId: data.room_id || '',
roomName: data.name || '',
bitrates: {
audio: bitrates.audio || 0,
audioRed: bitrates.audio_red || 0,
video: bitrates.video || 0,
videoHigh: bitrates.video_high || 0,
videoHighPixels: bitrates.video_high_pixels || 0,
videoMid: bitrates.video_mid || 0,
videoMidPixels: bitrates.video_mid_pixels || 0,
videoLow: bitrates.video_low || 0,
videoLowPixels: bitrates.video_low_pixels || 0,
initialBandwidth: bitrates.initial_bandwidth || 0,
id: data.id || '',
name: data.name || '',
options: {
bitrates: {
audio: bitrates.audio || 0,
audioRed: bitrates.audio_red || 0,
video: bitrates.video || 0,
videoHigh: bitrates.video_high || 0,
videoHighPixels: bitrates.video_high_pixels || 0,
videoMid: bitrates.video_mid || 0,
videoMidPixels: bitrates.video_mid_pixels || 0,
videoLow: bitrates.video_low || 0,
videoLowPixels: bitrates.video_low_pixels || 0,
initialBandwidth: bitrates.initial_bandwidth || 0,
},
codecs: data.options.codecs || [],
pliIntervalMS: data.options.pli_interval_ns / 1_000_000 || 0,
emptyRoomTimeoutMS:
data.options.empty_room_timeout_ns / 1_000_000 || 0,
qualityPresets: {
high: {
sid: qualityPresets.high?.sid,
tid: qualityPresets.high?.tid,
},
mid: {
sid: qualityPresets.mid?.sid,
tid: qualityPresets.mid?.tid,
},
low: {
sid: qualityPresets.low?.sid,
tid: qualityPresets.low?.tid,
},
},
},
codecPreferences: data.codec_preferences || [],
},
}

Expand Down
20 changes: 0 additions & 20 deletions packages/room/room-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,4 @@ export declare namespace RoomType {
webrtc?: SharedType.DeepPartial<typeof webrtc>
media?: SharedType.DeepPartial<typeof media>
}

type BitrateConfigs = {
audioRed: number
audio: number
video: number
videoHigh: number
videoHighPixels: number
videoMid: number
videoMidPixels: number
videoLow: number
videoLowPixels: number
initialBandwidth: number
}

type Room = {
id: string
name: string
codecPreferences: string[]
bitrateConfigs: BitrateConfigs
}
}

0 comments on commit 8ff1545

Please sign in to comment.