Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d5db12e1b | |||
| 4f59cbcf65 | |||
| 3b3f6b6e40 | |||
| 461cbc6f83 | |||
| a5cda8828f | |||
| 778f0a5687 | |||
| 2aca9bca08 | |||
| 7ed23df3e9 |
@@ -7,17 +7,17 @@
|
|||||||
</template>
|
</template>
|
||||||
</PrimeAvatar>
|
</PrimeAvatar>
|
||||||
|
|
||||||
<div class="flex-1">
|
<div class="flex-1 overflow-hidden">
|
||||||
<div class="text-sm leading-5 font-medium text-color whitespace-nowrap overflow-ellipsis">
|
<div class="overflow-hidden text-sm leading-5 font-medium text-color whitespace-nowrap overflow-ellipsis">
|
||||||
{{ client.displayName }}
|
{{ client.displayName }}
|
||||||
</div>
|
</div>
|
||||||
<div v-if="client.username !== client.displayName" class="mt-1 text-xs leading-5 text-muted-color">
|
<div v-if="client.username !== client.displayName" class="overflow-hidden mt-1 text-xs leading-5 text-muted-color">
|
||||||
{{ client.username }}
|
{{ client.username }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<PrimeBadge v-if="inputMuted" severity="info" value="Muted" />
|
<PrimeBadge v-if="client.outputMuted" severity="info" value="No sound" />
|
||||||
<!-- <PrimeBadge v-if="outputMuted" severity="info" value="No sound" /> -->
|
<PrimeBadge v-else-if="inputMuted" severity="info" value="Muted" />
|
||||||
<PrimeBadge v-if="isMe" severity="secondary" value="You" />
|
<PrimeBadge v-if="isMe" severity="secondary" value="You" />
|
||||||
|
|
||||||
<template v-if="!isMe">
|
<template v-if="!isMe">
|
||||||
@@ -98,13 +98,13 @@ const audioTrack = computed(() => {
|
|||||||
const { setGain } = useAudioContext(audioTrack)
|
const { setGain } = useAudioContext(audioTrack)
|
||||||
|
|
||||||
watch(volume, (volume) => {
|
watch(volume, (volume) => {
|
||||||
// if (outputMuted.value)
|
if (outputMuted.value)
|
||||||
// return
|
return
|
||||||
|
|
||||||
setGain(volume * 0.01)
|
setGain(volume * 0.01)
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
|
|
||||||
// watch(outputMuted, (outputMuted) => {
|
watch(outputMuted, (outputMuted) => {
|
||||||
// setGain(outputMuted ? 0 : (volume.value * 0.01))
|
setGain(outputMuted ? 0 : (volume.value * 0.01))
|
||||||
// })
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { useClients } from '~/composables/use-clients'
|
|||||||
export const useApp = createGlobalState(() => {
|
export const useApp = createGlobalState(() => {
|
||||||
const { clients } = useClients()
|
const { clients } = useClients()
|
||||||
const mediasoup = useMediasoup()
|
const mediasoup = useMediasoup()
|
||||||
|
const signaling = useSignaling()
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
const ready = ref(false)
|
const ready = ref(false)
|
||||||
@@ -36,7 +37,7 @@ export const useApp = createGlobalState(() => {
|
|||||||
toast.add({ severity: 'info', summary: toastText, closable: false, life: 1000 })
|
toast.add({ severity: 'info', summary: toastText, closable: false, life: 1000 })
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(outputMuted, (outputMuted) => {
|
watch(outputMuted, async (outputMuted) => {
|
||||||
if (outputMuted) {
|
if (outputMuted) {
|
||||||
previousInputMuted.value = inputMuted.value
|
previousInputMuted.value = inputMuted.value
|
||||||
muteInput()
|
muteInput()
|
||||||
@@ -45,6 +46,10 @@ export const useApp = createGlobalState(() => {
|
|||||||
inputMuted.value = previousInputMuted.value
|
inputMuted.value = previousInputMuted.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updatedMe = await signaling.socket.value?.emitWithAck('updateClient', {
|
||||||
|
outputMuted,
|
||||||
|
})
|
||||||
|
|
||||||
const toastText = outputMuted ? 'Sound muted' : 'Sound resumed'
|
const toastText = outputMuted ? 'Sound muted' : 'Sound resumed'
|
||||||
toast.add({ severity: 'info', summary: toastText, closable: false, life: 1000 })
|
toast.add({ severity: 'info', summary: toastText, closable: false, life: 1000 })
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
|||||||
if (!me.value) {
|
if (!me.value) {
|
||||||
try {
|
try {
|
||||||
setMe(await chadApi('/me', { method: 'GET' }))
|
setMe(await chadApi('/me', { method: 'GET' }))
|
||||||
|
|
||||||
|
if (to.meta.auth !== false)
|
||||||
return navigateTo({ name: 'Index' })
|
return navigateTo({ name: 'Index' })
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
|||||||
@@ -47,6 +47,7 @@
|
|||||||
<!-- <label for="outputDevice">Output device</label> -->
|
<!-- <label for="outputDevice">Output device</label> -->
|
||||||
<!-- </PrimeFloatLabel> -->
|
<!-- </PrimeFloatLabel> -->
|
||||||
|
|
||||||
|
<template v-if="isTauri">
|
||||||
<PrimeDivider align="left">
|
<PrimeDivider align="left">
|
||||||
Hotkeys
|
Hotkeys
|
||||||
</PrimeDivider>
|
</PrimeDivider>
|
||||||
@@ -60,6 +61,7 @@
|
|||||||
<PrimeInputText id="soundToggle" :model-value="toggleOutputHotkey" fluid @keydown="setupToggleOutputHotkey" />
|
<PrimeInputText id="soundToggle" :model-value="toggleOutputHotkey" fluid @keydown="setupToggleOutputHotkey" />
|
||||||
<label for="soundToggle">Toggle sound</label>
|
<label for="soundToggle">Toggle sound</label>
|
||||||
</PrimeFloatLabel>
|
</PrimeFloatLabel>
|
||||||
|
</template>
|
||||||
|
|
||||||
<PrimeDivider align="left">
|
<PrimeDivider align="left">
|
||||||
About
|
About
|
||||||
@@ -72,31 +74,28 @@
|
|||||||
COMMIT_SHA: {{ commitSha }}
|
COMMIT_SHA: {{ commitSha }}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<template v-if="isTauri">
|
||||||
<PrimeButton
|
<PrimeButton
|
||||||
v-if="isTauri"
|
v-if="lastUpdate"
|
||||||
|
class="mt-3"
|
||||||
|
size="small"
|
||||||
|
label="Install new version"
|
||||||
|
fluid
|
||||||
|
severity="success"
|
||||||
|
@click="navigateTo({ name: 'Updater' })"
|
||||||
|
/>
|
||||||
|
<PrimeButton
|
||||||
|
v-else
|
||||||
class="mt-3"
|
class="mt-3"
|
||||||
size="small"
|
size="small"
|
||||||
label="Check for Updates"
|
label="Check for Updates"
|
||||||
fluid
|
fluid
|
||||||
severity="info"
|
severity="info"
|
||||||
:loading="checking"
|
:loading="checking"
|
||||||
@click="onCheckForUpdates"
|
@click="checkForUpdates"
|
||||||
/>
|
/>
|
||||||
</div>
|
|
||||||
|
|
||||||
<PrimeToast position="bottom-center" group="updater">
|
|
||||||
<template #container="slotProps">
|
|
||||||
<div class="p-3">
|
|
||||||
<div class="font-medium text-lg mb-4">
|
|
||||||
{{ slotProps.message.detail }}
|
|
||||||
</div>
|
|
||||||
<div class="flex gap-3">
|
|
||||||
<PrimeButton size="small" label="Update now" @click="() => {}" />
|
|
||||||
<PrimeButton size="small" label="Later" severity="secondary" outlined @click="slotProps.closeCallback()" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
</PrimeToast>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
@@ -106,7 +105,7 @@ definePageMeta({
|
|||||||
name: 'Preferences',
|
name: 'Preferences',
|
||||||
})
|
})
|
||||||
const { isTauri, version, commitSha } = useApp()
|
const { isTauri, version, commitSha } = useApp()
|
||||||
const { checking, checkForUpdates } = useUpdater()
|
const { checking, checkForUpdates, lastUpdate } = useUpdater()
|
||||||
const {
|
const {
|
||||||
inputDeviceId,
|
inputDeviceId,
|
||||||
outputDeviceId,
|
outputDeviceId,
|
||||||
@@ -121,8 +120,6 @@ const {
|
|||||||
audioOutputs,
|
audioOutputs,
|
||||||
} = usePreferences()
|
} = usePreferences()
|
||||||
|
|
||||||
const toast = useToast()
|
|
||||||
|
|
||||||
const setupToggleInputHotkey = (event: KeyboardEvent) => setupHotkey(event, toggleInputHotkey)
|
const setupToggleInputHotkey = (event: KeyboardEvent) => setupHotkey(event, toggleInputHotkey)
|
||||||
const setupToggleOutputHotkey = (event: KeyboardEvent) => setupHotkey(event, toggleOutputHotkey)
|
const setupToggleOutputHotkey = (event: KeyboardEvent) => setupHotkey(event, toggleOutputHotkey)
|
||||||
|
|
||||||
@@ -162,23 +159,4 @@ function setupHotkey(event: KeyboardEvent, model: RemovableRef<string>) {
|
|||||||
|
|
||||||
model.value = hotkey.join('+')
|
model.value = hotkey.join('+')
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onCheckForUpdates() {
|
|
||||||
const update = await checkForUpdates()
|
|
||||||
|
|
||||||
toast.removeGroup('updater')
|
|
||||||
|
|
||||||
if (!update) {
|
|
||||||
toast.add({ severity: 'success', summary: 'You are up to date', closable: false, life: 1000 })
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.add({
|
|
||||||
group: 'updater',
|
|
||||||
severity: 'info',
|
|
||||||
detail: `Version ${update?.version ?? '1.0.1'} is available!`,
|
|
||||||
closable: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import chadApi from '#shared/chad-api'
|
||||||
import { LogOut } from 'lucide-vue-next'
|
import { LogOut } from 'lucide-vue-next'
|
||||||
|
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
@@ -51,8 +52,11 @@ async function save() {
|
|||||||
|
|
||||||
saving.value = true
|
saving.value = true
|
||||||
|
|
||||||
const updatedMe = await signaling.socket.value?.emitWithAck('updateClient', {
|
const updatedMe = await chadApi('/profile', {
|
||||||
|
method: 'PATCH',
|
||||||
|
body: {
|
||||||
displayName: displayName.value,
|
displayName: displayName.value,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
setMe({ ...me.value!, displayName: (updatedMe.displayName as string) })
|
setMe({ ...me.value!, displayName: (updatedMe.displayName as string) })
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
|
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
|
||||||
"productName": "chad",
|
"productName": "chad",
|
||||||
"version": "0.2.13",
|
"version": "0.2.15",
|
||||||
"identifier": "xyz.koptilnya.chad",
|
"identifier": "xyz.koptilnya.chad",
|
||||||
"build": {
|
"build": {
|
||||||
"frontendDist": "../.output/public",
|
"frontendDist": "../.output/public",
|
||||||
|
|||||||
@@ -25,5 +25,50 @@ export const autoConfig: mediasoup.types.RouterOptions = {
|
|||||||
channels: 2,
|
channels: 2,
|
||||||
parameters: { useinbandfec: 1, stereo: 1 },
|
parameters: { useinbandfec: 1, stereo: 1 },
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
kind: 'video',
|
||||||
|
mimeType: 'video/VP8',
|
||||||
|
clockRate: 90000,
|
||||||
|
parameters: {
|
||||||
|
'x-google-start-bitrate': 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: 'video',
|
||||||
|
mimeType: 'video/VP9',
|
||||||
|
clockRate: 90000,
|
||||||
|
parameters: {
|
||||||
|
'profile-id': 2,
|
||||||
|
'x-google-start-bitrate': 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: 'video',
|
||||||
|
mimeType: 'video/h264',
|
||||||
|
clockRate: 90000,
|
||||||
|
parameters: {
|
||||||
|
'packetization-mode': 1,
|
||||||
|
'profile-level-id': '4d0032',
|
||||||
|
'level-asymmetry-allowed': 1,
|
||||||
|
'x-google-start-bitrate': 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: 'video',
|
||||||
|
mimeType: 'video/h264',
|
||||||
|
clockRate: 90000,
|
||||||
|
parameters: {
|
||||||
|
'packetization-mode': 1,
|
||||||
|
'profile-level-id': '42e01f',
|
||||||
|
'level-asymmetry-allowed': 1,
|
||||||
|
'x-google-start-bitrate': 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: 'video',
|
||||||
|
mimeType: 'video/AV1',
|
||||||
|
clockRate: 90000,
|
||||||
|
parameters: {},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import type { FastifyInstance } from 'fastify'
|
import type { FastifyInstance } from 'fastify'
|
||||||
|
import type { Namespace } from '../types/webrtc.ts'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import prisma from '../prisma/client.ts'
|
import prisma from '../prisma/client.ts'
|
||||||
|
import { socketToClient } from '../utils/socket-to-client.ts'
|
||||||
|
|
||||||
export default function (fastify: FastifyInstance) {
|
export default function (fastify: FastifyInstance) {
|
||||||
fastify.get('/preferences', async (req, reply) => {
|
fastify.get('/preferences', async (req, reply) => {
|
||||||
@@ -47,4 +49,49 @@ export default function (fastify: FastifyInstance) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
fastify.patch('/profile', async (req, reply) => {
|
||||||
|
if (!req.user) {
|
||||||
|
reply.code(401).send(false)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const schema = z.object({
|
||||||
|
displayName: z.string().optional(),
|
||||||
|
})
|
||||||
|
const input = schema.parse(req.body)
|
||||||
|
|
||||||
|
const updatedUser = prisma.user.update({
|
||||||
|
where: { id: req.user.id },
|
||||||
|
data: {
|
||||||
|
displayName: input.displayName,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const namespace: Namespace = fastify.io.of('/webrtc')
|
||||||
|
const sockets = await namespace.fetchSockets()
|
||||||
|
|
||||||
|
const found = sockets.find(socket => socket.data.joined && socket.data.userId === req.user!.id)
|
||||||
|
|
||||||
|
if (found) {
|
||||||
|
found.data.displayName = input.displayName
|
||||||
|
namespace.emit('clientChanged', found.id, socketToClient(found))
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedUser
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
fastify.log.error(err)
|
||||||
|
reply.code(400)
|
||||||
|
|
||||||
|
if (err instanceof z.ZodError) {
|
||||||
|
reply.send({ error: z.prettifyError(err) })
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
reply.send({ error: err.message })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,145 +1,15 @@
|
|||||||
import type { User } from '@prisma/client'
|
|
||||||
import type { types } from 'mediasoup'
|
import type { types } from 'mediasoup'
|
||||||
import type { Namespace, RemoteSocket, Socket, Server as SocketServer } from 'socket.io'
|
import type { Server as SocketServer } from 'socket.io'
|
||||||
|
import type {
|
||||||
|
Namespace,
|
||||||
|
SomeSocket,
|
||||||
|
} from '../types/webrtc.ts'
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import prisma from '../prisma/client.ts'
|
import prisma from '../prisma/client.ts'
|
||||||
|
import { socketToClient } from '../utils/socket-to-client.ts'
|
||||||
interface ChadClient {
|
|
||||||
socketId: string
|
|
||||||
userId: User['id']
|
|
||||||
username: User['username']
|
|
||||||
displayName: User['displayName']
|
|
||||||
inputMuted: boolean
|
|
||||||
outputMuted: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ProducerShort {
|
|
||||||
producerId: types.Producer['id']
|
|
||||||
kind: types.MediaKind
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ErrorCallbackResult {
|
|
||||||
error: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SuccessCallbackResult {
|
|
||||||
ok: true
|
|
||||||
}
|
|
||||||
|
|
||||||
type EventCallback<T = SuccessCallbackResult> = (result: T | ErrorCallbackResult) => void
|
|
||||||
|
|
||||||
interface ClientToServerEvents {
|
|
||||||
join: (
|
|
||||||
options: {
|
|
||||||
rtpCapabilities: types.RtpCapabilities
|
|
||||||
},
|
|
||||||
cb: EventCallback<ChadClient[]>
|
|
||||||
) => void
|
|
||||||
getRtpCapabilities: (
|
|
||||||
cb: EventCallback<types.RtpCapabilities>
|
|
||||||
) => void
|
|
||||||
createTransport: (
|
|
||||||
options: {
|
|
||||||
producing: boolean
|
|
||||||
consuming: boolean
|
|
||||||
},
|
|
||||||
cb: EventCallback<Pick<types.WebRtcTransport, 'id' | 'iceParameters' | 'iceCandidates' | 'dtlsParameters'>>
|
|
||||||
) => void
|
|
||||||
connectTransport: (
|
|
||||||
options: {
|
|
||||||
transportId: types.WebRtcTransport['id']
|
|
||||||
dtlsParameters: types.WebRtcTransport['dtlsParameters']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
produce: (
|
|
||||||
options: {
|
|
||||||
transportId: types.WebRtcTransport['id']
|
|
||||||
kind: types.MediaKind
|
|
||||||
rtpParameters: types.RtpParameters
|
|
||||||
},
|
|
||||||
cb: EventCallback<{ id: types.Producer['id'] }>
|
|
||||||
) => void
|
|
||||||
closeProducer: (
|
|
||||||
options: {
|
|
||||||
producerId: types.Producer['id']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
pauseProducer: (
|
|
||||||
options: {
|
|
||||||
producerId: types.Producer['id']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
resumeProducer: (
|
|
||||||
options: {
|
|
||||||
producerId: types.Producer['id']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
pauseConsumer: (
|
|
||||||
options: {
|
|
||||||
consumerId: types.Consumer['id']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
resumeConsumer: (
|
|
||||||
options: {
|
|
||||||
consumerId: types.Consumer['id']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
updateClient: (
|
|
||||||
options: Partial<Omit<ChadClient, 'socketId' | 'userId'>>,
|
|
||||||
cb: EventCallback<ChadClient>
|
|
||||||
) => void
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ServerToClientEvents {
|
|
||||||
authenticated: () => void
|
|
||||||
newPeer: (arg: ChadClient) => void
|
|
||||||
producers: (arg: ProducerShort[]) => void
|
|
||||||
newConsumer: (
|
|
||||||
arg: {
|
|
||||||
socketId: string
|
|
||||||
producerId: types.Producer['id']
|
|
||||||
id: types.Consumer['id']
|
|
||||||
kind: types.MediaKind
|
|
||||||
rtpParameters: types.RtpParameters
|
|
||||||
type: types.ConsumerType
|
|
||||||
appData: types.Producer['appData']
|
|
||||||
producerPaused: types.Consumer['producerPaused']
|
|
||||||
},
|
|
||||||
cb: EventCallback
|
|
||||||
) => void
|
|
||||||
peerClosed: (arg: string) => void
|
|
||||||
consumerClosed: (arg: { consumerId: string }) => void
|
|
||||||
consumerPaused: (arg: { consumerId: string }) => void
|
|
||||||
consumerResumed: (arg: { consumerId: string }) => void
|
|
||||||
consumerScore: (arg: { consumerId: string, score: types.ConsumerScore }) => void
|
|
||||||
clientChanged: (clientId: ChadClient['socketId'], client: ChadClient) => void
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InterServerEvent {}
|
|
||||||
|
|
||||||
interface SocketData {
|
|
||||||
joined: boolean
|
|
||||||
userId: User['id']
|
|
||||||
username: User['username']
|
|
||||||
displayName: User['displayName']
|
|
||||||
inputMuted: boolean
|
|
||||||
outputMuted: boolean
|
|
||||||
rtpCapabilities: types.RtpCapabilities
|
|
||||||
transports: Map<types.WebRtcTransport['id'], types.WebRtcTransport>
|
|
||||||
producers: Map<types.Producer['id'], types.Producer>
|
|
||||||
consumers: Map<types.Consumer['id'], types.Consumer>
|
|
||||||
}
|
|
||||||
|
|
||||||
type SomeSocket = Socket<ClientToServerEvents, ServerToClientEvents, InterServerEvent, SocketData> | RemoteSocket<ServerToClientEvents, SocketData>
|
|
||||||
|
|
||||||
export default function (io: SocketServer, router: types.Router) {
|
export default function (io: SocketServer, router: types.Router) {
|
||||||
const namespace: Namespace<ClientToServerEvents, ServerToClientEvents, InterServerEvent, SocketData> = io.of('/webrtc')
|
const namespace: Namespace = io.of('/webrtc')
|
||||||
|
|
||||||
namespace.on('connection', async (socket) => {
|
namespace.on('connection', async (socket) => {
|
||||||
consola.info('[WebRtc]', 'Client connected', socket.id)
|
consola.info('[WebRtc]', 'Client connected', socket.id)
|
||||||
@@ -278,7 +148,7 @@ export default function (io: SocketServer, router: types.Router) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('produce', async ({ transportId, kind, rtpParameters }, cb) => {
|
socket.on('produce', async ({ transportId, kind, rtpParameters, appData }, cb) => {
|
||||||
if (!socket.data.joined) {
|
if (!socket.data.joined) {
|
||||||
consola.error('Peer not joined yet')
|
consola.error('Peer not joined yet')
|
||||||
cb({ error: 'Peer not joined yet' })
|
cb({ error: 'Peer not joined yet' })
|
||||||
@@ -296,7 +166,7 @@ export default function (io: SocketServer, router: types.Router) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const producer = await transport.produce({ kind, rtpParameters, appData: { socketId: socket.id } })
|
const producer = await transport.produce({ kind, rtpParameters, appData: { ...appData, socketId: socket.id } })
|
||||||
|
|
||||||
socket.data.producers.set(producer.id, producer)
|
socket.data.producers.set(producer.id, producer)
|
||||||
|
|
||||||
@@ -439,24 +309,11 @@ export default function (io: SocketServer, router: types.Router) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
socket.on('updateClient', async (updatedClient, cb) => {
|
socket.on('updateClient', async (updatedClient, cb) => {
|
||||||
if (updatedClient.displayName) {
|
if (typeof updatedClient.inputMuted === 'boolean') {
|
||||||
await prisma.user.update({
|
|
||||||
where: {
|
|
||||||
id: socket.data.userId,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
displayName: updatedClient.displayName,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.data.displayName = updatedClient.displayName
|
|
||||||
}
|
|
||||||
|
|
||||||
if (updatedClient.inputMuted) {
|
|
||||||
socket.data.inputMuted = updatedClient.inputMuted
|
socket.data.inputMuted = updatedClient.inputMuted
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updatedClient.outputMuted) {
|
if (typeof updatedClient.outputMuted === 'boolean') {
|
||||||
socket.data.outputMuted = updatedClient.outputMuted
|
socket.data.outputMuted = updatedClient.outputMuted
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -583,15 +440,4 @@ export default function (io: SocketServer, router: types.Router) {
|
|||||||
consola.error('_createConsumer() | failed:%o', error)
|
consola.error('_createConsumer() | failed:%o', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function socketToClient(socket: SomeSocket): ChadClient {
|
|
||||||
return {
|
|
||||||
socketId: socket.id,
|
|
||||||
userId: socket.data.userId,
|
|
||||||
username: socket.data.username,
|
|
||||||
displayName: socket.data.displayName,
|
|
||||||
inputMuted: socket.data.inputMuted,
|
|
||||||
outputMuted: socket.data.outputMuted,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
140
server/types/webrtc.ts
Normal file
140
server/types/webrtc.ts
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
import type { types } from 'mediasoup'
|
||||||
|
import type { RemoteSocket, Socket, Namespace as SocketNamespace } from 'socket.io'
|
||||||
|
import type { User } from '../prisma/client'
|
||||||
|
|
||||||
|
export interface ChadClient {
|
||||||
|
socketId: string
|
||||||
|
userId: User['id']
|
||||||
|
username: User['username']
|
||||||
|
displayName: User['displayName']
|
||||||
|
inputMuted: boolean
|
||||||
|
outputMuted: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProducerShort {
|
||||||
|
producerId: types.Producer['id']
|
||||||
|
kind: types.MediaKind
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ErrorCallbackResult {
|
||||||
|
error: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SuccessCallbackResult {
|
||||||
|
ok: true
|
||||||
|
}
|
||||||
|
|
||||||
|
export type EventCallback<T = SuccessCallbackResult> = (result: T | ErrorCallbackResult) => void
|
||||||
|
|
||||||
|
export interface ClientToServerEvents {
|
||||||
|
join: (
|
||||||
|
options: {
|
||||||
|
rtpCapabilities: types.RtpCapabilities
|
||||||
|
},
|
||||||
|
cb: EventCallback<ChadClient[]>
|
||||||
|
) => void
|
||||||
|
getRtpCapabilities: (
|
||||||
|
cb: EventCallback<types.RtpCapabilities>
|
||||||
|
) => void
|
||||||
|
createTransport: (
|
||||||
|
options: {
|
||||||
|
producing: boolean
|
||||||
|
consuming: boolean
|
||||||
|
},
|
||||||
|
cb: EventCallback<Pick<types.WebRtcTransport, 'id' | 'iceParameters' | 'iceCandidates' | 'dtlsParameters'>>
|
||||||
|
) => void
|
||||||
|
connectTransport: (
|
||||||
|
options: {
|
||||||
|
transportId: types.WebRtcTransport['id']
|
||||||
|
dtlsParameters: types.WebRtcTransport['dtlsParameters']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
produce: (
|
||||||
|
options: {
|
||||||
|
transportId: types.WebRtcTransport['id']
|
||||||
|
kind: types.MediaKind
|
||||||
|
rtpParameters: types.RtpParameters
|
||||||
|
appData: { source: 'share' | string }
|
||||||
|
},
|
||||||
|
cb: EventCallback<{ id: types.Producer['id'] }>
|
||||||
|
) => void
|
||||||
|
closeProducer: (
|
||||||
|
options: {
|
||||||
|
producerId: types.Producer['id']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
pauseProducer: (
|
||||||
|
options: {
|
||||||
|
producerId: types.Producer['id']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
resumeProducer: (
|
||||||
|
options: {
|
||||||
|
producerId: types.Producer['id']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
pauseConsumer: (
|
||||||
|
options: {
|
||||||
|
consumerId: types.Consumer['id']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
resumeConsumer: (
|
||||||
|
options: {
|
||||||
|
consumerId: types.Consumer['id']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
updateClient: (
|
||||||
|
options: Partial<Pick<ChadClient, 'inputMuted' | 'outputMuted'>>,
|
||||||
|
cb: EventCallback<ChadClient>
|
||||||
|
) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServerToClientEvents {
|
||||||
|
authenticated: () => void
|
||||||
|
newPeer: (arg: ChadClient) => void
|
||||||
|
producers: (arg: ProducerShort[]) => void
|
||||||
|
newConsumer: (
|
||||||
|
arg: {
|
||||||
|
socketId: string
|
||||||
|
producerId: types.Producer['id']
|
||||||
|
id: types.Consumer['id']
|
||||||
|
kind: types.MediaKind
|
||||||
|
rtpParameters: types.RtpParameters
|
||||||
|
type: types.ConsumerType
|
||||||
|
appData: types.Producer['appData']
|
||||||
|
producerPaused: types.Consumer['producerPaused']
|
||||||
|
},
|
||||||
|
cb: EventCallback
|
||||||
|
) => void
|
||||||
|
peerClosed: (arg: string) => void
|
||||||
|
consumerClosed: (arg: { consumerId: string }) => void
|
||||||
|
consumerPaused: (arg: { consumerId: string }) => void
|
||||||
|
consumerResumed: (arg: { consumerId: string }) => void
|
||||||
|
consumerScore: (arg: { consumerId: string, score: types.ConsumerScore }) => void
|
||||||
|
clientChanged: (clientId: ChadClient['socketId'], client: ChadClient) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InterServerEvent {}
|
||||||
|
|
||||||
|
export interface SocketData {
|
||||||
|
joined: boolean
|
||||||
|
userId: User['id']
|
||||||
|
username: User['username']
|
||||||
|
displayName: User['displayName']
|
||||||
|
inputMuted: boolean
|
||||||
|
outputMuted: boolean
|
||||||
|
rtpCapabilities: types.RtpCapabilities
|
||||||
|
transports: Map<types.WebRtcTransport['id'], types.WebRtcTransport>
|
||||||
|
producers: Map<types.Producer['id'], types.Producer>
|
||||||
|
consumers: Map<types.Consumer['id'], types.Consumer>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SomeSocket = Socket<ClientToServerEvents, ServerToClientEvents, InterServerEvent, SocketData> | RemoteSocket<ServerToClientEvents, SocketData>
|
||||||
|
|
||||||
|
export type Namespace = SocketNamespace<ClientToServerEvents, ServerToClientEvents, InterServerEvent, SocketData>
|
||||||
12
server/utils/socket-to-client.ts
Normal file
12
server/utils/socket-to-client.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import type { ChadClient, SomeSocket } from '../types/webrtc.ts'
|
||||||
|
|
||||||
|
export function socketToClient(socket: SomeSocket): ChadClient {
|
||||||
|
return {
|
||||||
|
socketId: socket.id,
|
||||||
|
userId: socket.data.userId,
|
||||||
|
username: socket.data.username,
|
||||||
|
displayName: socket.data.displayName,
|
||||||
|
inputMuted: socket.data.inputMuted,
|
||||||
|
outputMuted: socket.data.outputMuted,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user