This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
<template>
|
||||
<NuxtPage />
|
||||
<NuxtLayout>
|
||||
<NuxtPage />
|
||||
</NuxtLayout>
|
||||
|
||||
<PrimeToast position="bottom-center" />
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
const { consumers, producers } = useMediasoup()
|
||||
</script>
|
||||
|
||||
5
client/app/components.d.ts
vendored
5
client/app/components.d.ts
vendored
@@ -11,10 +11,15 @@ declare module 'vue' {
|
||||
PrimeAvatar: typeof import('primevue/avatar')['default']
|
||||
PrimeBadge: typeof import('primevue/badge')['default']
|
||||
PrimeButton: typeof import('primevue/button')['default']
|
||||
PrimeButtonGroup: typeof import('primevue/buttongroup')['default']
|
||||
PrimeCard: typeof import('primevue/card')['default']
|
||||
PrimeFieldset: typeof import('primevue/fieldset')['default']
|
||||
PrimeFloatLabel: typeof import('primevue/floatlabel')['default']
|
||||
PrimeInputText: typeof import('primevue/inputtext')['default']
|
||||
PrimeMenu: typeof import('primevue/menu')['default']
|
||||
PrimeMessage: typeof import('primevue/message')['default']
|
||||
PrimeSlider: typeof import('primevue/slider')['default']
|
||||
PrimeToast: typeof import('primevue/toast')['default']
|
||||
RouterLink: typeof import('vue-router')['RouterLink']
|
||||
RouterView: typeof import('vue-router')['RouterView']
|
||||
}
|
||||
|
||||
25
client/app/components/AppHeader.vue
Normal file
25
client/app/components/AppHeader.vue
Normal file
@@ -0,0 +1,25 @@
|
||||
<template>
|
||||
<div
|
||||
class="flex items-center justify-between gap-2 border-b-2 border-surface-800 px-3 py-3"
|
||||
:class="{
|
||||
'bg-surface-950': !secondary,
|
||||
'bg-surface-900': secondary,
|
||||
}"
|
||||
style="height: 75px;"
|
||||
>
|
||||
<slot name="left">
|
||||
<h1>
|
||||
{{ title }}
|
||||
</h1>
|
||||
</slot>
|
||||
|
||||
<slot name="right" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
defineProps<{
|
||||
title: string
|
||||
secondary?: boolean
|
||||
}>()
|
||||
</script>
|
||||
@@ -1,31 +1,39 @@
|
||||
<template>
|
||||
<div class="flex items-center gap-3 py-3">
|
||||
<PrimeAvatar icon="pi pi-user" size="small" />
|
||||
<div class="py-3">
|
||||
<div class="flex items-center gap-3 ">
|
||||
<PrimeAvatar
|
||||
icon="pi pi-user"
|
||||
size="small"
|
||||
/>
|
||||
|
||||
<div class="flex-1">
|
||||
<div class="text-sm leading-5 font-medium text-color whitespace-nowrap overflow-ellipsis">
|
||||
{{ client.username }}
|
||||
</div>
|
||||
<div class="mt-1 text-xs leading-5 text-muted-color">
|
||||
{{ client.id }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<PrimeBadge severity="secondary" value="Muted" />
|
||||
|
||||
<PrimeButton icon="pi pi-ellipsis-h" text size="small" @click="menuRef?.toggle" />
|
||||
|
||||
<PrimeMenu ref="menu" popup :model="menuItems" style="translate: calc(-100% + 2rem) 0.5rem">
|
||||
<template #start>
|
||||
<div class="px-4 py-3">
|
||||
<div class="flex justify-between">
|
||||
<span>Volume</span>
|
||||
<span>{{ volume }}</span>
|
||||
</div>
|
||||
<PrimeSlider v-model="volume" class="mt-4" :min="0" :max="100" :step="5" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm leading-5 font-medium text-color whitespace-nowrap overflow-ellipsis">
|
||||
{{ client.username }}
|
||||
</div>
|
||||
<div class="mt-1 text-xs leading-5 text-muted-color">
|
||||
{{ client.id }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<PrimeBadge v-if="client.isMe && inputMuted" severity="info" value="Muted" />
|
||||
<PrimeBadge v-if="client.isMe" severity="secondary" value="You" />
|
||||
|
||||
<template v-if="!client.isMe">
|
||||
<PrimeButton icon="pi pi-ellipsis-h" text size="small" severity="contrast" @click="menuRef?.toggle" />
|
||||
|
||||
<PrimeMenu ref="menu" popup :model="menuItems" style="translate: calc(-100% + 2rem) 0.5rem">
|
||||
<template #start>
|
||||
<div class="px-4 py-3">
|
||||
<div class="flex justify-between">
|
||||
<span>Volume</span>
|
||||
<span>{{ volume }}</span>
|
||||
</div>
|
||||
<PrimeSlider v-model="volume" class="mt-4" :min="0" :max="300" :step="5" />
|
||||
</div>
|
||||
</template>
|
||||
</PrimeMenu>
|
||||
</template>
|
||||
</PrimeMenu>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -33,11 +41,15 @@
|
||||
import type { ChadClient } from '#shared/types'
|
||||
import type { MenuItem } from 'primevue/menuitem'
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
client: ChadClient
|
||||
}>()
|
||||
|
||||
const menuRef = useTemplateRef('menu')
|
||||
const { inputMuted, outputMuted } = useApp()
|
||||
const { getClientConsumers } = useMediasoup()
|
||||
|
||||
const menuRef = useTemplateRef<HTMLAudioElement>('menu')
|
||||
|
||||
const volume = ref(100)
|
||||
|
||||
const menuItems: MenuItem[] = [
|
||||
@@ -51,4 +63,27 @@ const menuItems: MenuItem[] = [
|
||||
disabled: true,
|
||||
},
|
||||
]
|
||||
|
||||
const audioConsumer = computed(() => {
|
||||
const consumers = getClientConsumers(props.client.id)
|
||||
|
||||
return consumers.find(consumer => consumer.track.kind === 'audio')
|
||||
})
|
||||
|
||||
const audioTrack = computed(() => {
|
||||
return audioConsumer.value?.track
|
||||
})
|
||||
|
||||
const { setGain } = useAudioContext(audioTrack)
|
||||
|
||||
watch(volume, (volume) => {
|
||||
if (outputMuted)
|
||||
return
|
||||
|
||||
setGain(volume * 0.01)
|
||||
})
|
||||
|
||||
watch(outputMuted, (outputMuted) => {
|
||||
setGain(outputMuted ? 0 : (volume.value * 0.01))
|
||||
})
|
||||
</script>
|
||||
|
||||
60
client/app/composables/use-app.ts
Normal file
60
client/app/composables/use-app.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { createGlobalState } from '@vueuse/core'
|
||||
|
||||
export const useApp = createGlobalState(() => {
|
||||
const mediasoup = useMediasoup()
|
||||
|
||||
const inputMuted = ref(false)
|
||||
const outputMuted = ref(false)
|
||||
|
||||
const me = computed(() => mediasoup.clients.value.find(client => client.isMe))
|
||||
|
||||
function muteInput() {
|
||||
inputMuted.value = true
|
||||
}
|
||||
|
||||
function unmuteInput() {
|
||||
inputMuted.value = false
|
||||
}
|
||||
|
||||
function toggleInput() {
|
||||
if (inputMuted.value)
|
||||
unmuteInput()
|
||||
else
|
||||
muteInput()
|
||||
}
|
||||
|
||||
function muteOutput() {
|
||||
outputMuted.value = true
|
||||
}
|
||||
|
||||
function unmuteOutput() {
|
||||
outputMuted.value = false
|
||||
}
|
||||
|
||||
function toggleOutput() {
|
||||
if (outputMuted.value)
|
||||
unmuteOutput()
|
||||
else
|
||||
muteOutput()
|
||||
}
|
||||
|
||||
watch(inputMuted, async (state) => {
|
||||
if (state)
|
||||
await mediasoup.muteMic()
|
||||
else
|
||||
await mediasoup.unmuteMic()
|
||||
})
|
||||
|
||||
return {
|
||||
clients: mediasoup.clients,
|
||||
me,
|
||||
inputMuted,
|
||||
muteInput,
|
||||
unmuteInput,
|
||||
toggleInput,
|
||||
outputMuted,
|
||||
muteOutput,
|
||||
unmuteOutput,
|
||||
toggleOutput,
|
||||
}
|
||||
})
|
||||
59
client/app/composables/use-audio-context.ts
Normal file
59
client/app/composables/use-audio-context.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { useEventListener } from '@vueuse/core'
|
||||
|
||||
export default function useAudioContext(audioTrack: Ref<MediaStreamTrack | undefined>) {
|
||||
const ctx = new (window.AudioContext || window.webkitAudioContext)()
|
||||
|
||||
const stream = new MediaStream()
|
||||
|
||||
const sourceNode = shallowRef<MediaStreamAudioSourceNode>()
|
||||
const gainNode = ctx.createGain()
|
||||
|
||||
let hackExecuted = false
|
||||
|
||||
watch(audioTrack, (track, prevTrack) => {
|
||||
if (prevTrack)
|
||||
stream.removeTrack(prevTrack)
|
||||
|
||||
if (!track)
|
||||
return
|
||||
|
||||
stream.addTrack(track)
|
||||
|
||||
if (!hackExecuted) {
|
||||
const audioEl = new Audio()
|
||||
audioEl.srcObject = stream
|
||||
audioEl.muted = true
|
||||
hackExecuted = true
|
||||
}
|
||||
|
||||
sourceNode.value = ctx.createMediaStreamSource(stream)
|
||||
|
||||
connect()
|
||||
}, { immediate: true })
|
||||
|
||||
useEventListener(document, 'click', async () => {
|
||||
if (ctx.state === 'suspended') {
|
||||
await ctx.resume()
|
||||
}
|
||||
|
||||
connect()
|
||||
}, { once: true })
|
||||
|
||||
function connect() {
|
||||
if (!sourceNode.value || ctx.state === 'suspended')
|
||||
return
|
||||
|
||||
sourceNode.value.connect(gainNode)
|
||||
gainNode.connect(ctx.destination)
|
||||
|
||||
ctx.resume()
|
||||
}
|
||||
|
||||
function setGain(value: number) {
|
||||
gainNode.gain.value = value
|
||||
}
|
||||
|
||||
return {
|
||||
setGain,
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import type { ChadClient } from '#shared/types'
|
||||
import { createGlobalState, useLocalStorage } from '@vueuse/core'
|
||||
|
||||
export const useGlobalState = createGlobalState(() => {
|
||||
const username = useLocalStorage<string>('username', '')
|
||||
|
||||
const clients = ref<ChadClient[]>([])
|
||||
|
||||
const me = computed(() => clients.value.find(client => client.isMe))
|
||||
|
||||
const clientByIdMap = computed(() => {
|
||||
return clients.value.reduce<Record<ChadClient['id'], ChadClient>>((result, client) => {
|
||||
result[client.id] = client
|
||||
|
||||
return result
|
||||
}, {})
|
||||
})
|
||||
|
||||
function reset() {
|
||||
clients.value = []
|
||||
}
|
||||
|
||||
return {
|
||||
username,
|
||||
me,
|
||||
clients,
|
||||
clientByIdMap,
|
||||
reset,
|
||||
}
|
||||
})
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { ChadClient, RemoteClient } from '#shared/types'
|
||||
import type { Socket } from 'socket.io-client'
|
||||
import { createSharedComposable } from '@vueuse/core'
|
||||
import * as mediasoupClient from 'mediasoup-client'
|
||||
import { io } from 'socket.io-client'
|
||||
import { usePreferences } from '~/composables/use-preferences'
|
||||
import { useSignaling } from '~/composables/use-signaling'
|
||||
|
||||
const ICE_SERVERS: RTCIceServer[] = [
|
||||
{ urls: 'stun:stun.l.google.com:19302' },
|
||||
@@ -18,9 +18,8 @@ const ICE_SERVERS: RTCIceServer[] = [
|
||||
]
|
||||
|
||||
export const useMediasoup = createSharedComposable(() => {
|
||||
const state = useGlobalState()
|
||||
|
||||
const socket = shallowRef<Socket>()
|
||||
const preferences = usePreferences()
|
||||
const signaling = useSignaling()
|
||||
|
||||
const device = shallowRef<mediasoupClient.Device>()
|
||||
const rtpCapabilities = shallowRef<mediasoupClient.types.RtpCapabilities>()
|
||||
@@ -31,47 +30,118 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
const webcamProducer = shallowRef<mediasoupClient.types.Producer>()
|
||||
const shareProducer = shallowRef<mediasoupClient.types.Producer>()
|
||||
|
||||
const clients = shallowRef<ChadClient[]>([])
|
||||
|
||||
const consumers = shallowRef<Map<string, mediasoupClient.types.Consumer>>(new Map())
|
||||
const producers = shallowRef<Map<string, mediasoupClient.types.Producer>>(new Map())
|
||||
|
||||
watch(socket, (socket, prevSocket) => {
|
||||
if (prevSocket) {
|
||||
prevSocket.close()
|
||||
|
||||
dispose()
|
||||
}
|
||||
|
||||
if (!socket) {
|
||||
watch(signaling.socket, (socket) => {
|
||||
if (!socket)
|
||||
return
|
||||
}
|
||||
|
||||
socket.onAny((event, ...args) => {
|
||||
console.log('[onAny]', event, args)
|
||||
})
|
||||
|
||||
socket.onAnyOutgoing((event, ...args) => {
|
||||
console.log('[onAnyOutgoing]', event, args)
|
||||
})
|
||||
|
||||
socket.on('connect', async () => {
|
||||
if (!state.username.value)
|
||||
state.username.value = socket.id!
|
||||
if (!signaling.socket.value)
|
||||
return
|
||||
|
||||
await join()
|
||||
device.value = new mediasoupClient.Device()
|
||||
rtpCapabilities.value = await signaling.socket.value.emitWithAck('getRtpCapabilities')
|
||||
|
||||
await device.value.load({ routerRtpCapabilities: rtpCapabilities.value! })
|
||||
|
||||
// Send transport
|
||||
{
|
||||
const transportInfo = await signaling.socket.value.emitWithAck('createTransport', { producing: true, consuming: false })
|
||||
sendTransport.value = device.value.createSendTransport({
|
||||
...transportInfo,
|
||||
iceServers: [
|
||||
...ICE_SERVERS,
|
||||
...(transportInfo.iceServers ?? []),
|
||||
],
|
||||
})
|
||||
|
||||
sendTransport.value.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
||||
try {
|
||||
await signaling.socket.value!.emitWithAck('connectTransport', {
|
||||
transportId: sendTransport.value!.id,
|
||||
dtlsParameters,
|
||||
})
|
||||
|
||||
callback()
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
errback(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
sendTransport.value.on('produce', async ({ kind, rtpParameters, appData }, callback, errback) => {
|
||||
try {
|
||||
const { id } = await signaling.socket.value!.emitWithAck('produce', {
|
||||
transportId: sendTransport.value!.id,
|
||||
kind,
|
||||
rtpParameters,
|
||||
appData,
|
||||
})
|
||||
callback({ id })
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
errback(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Recv Transport
|
||||
{
|
||||
const transportInfo = await signaling.socket.value.emitWithAck('createTransport', { producing: false, consuming: true })
|
||||
recvTransport.value = device.value.createRecvTransport({
|
||||
...transportInfo,
|
||||
iceServers: [
|
||||
...ICE_SERVERS,
|
||||
...(transportInfo.iceServers ?? []),
|
||||
],
|
||||
})
|
||||
|
||||
recvTransport.value.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
||||
try {
|
||||
await signaling.socket.value!.emitWithAck('connectTransport', {
|
||||
transportId: recvTransport.value!.id,
|
||||
dtlsParameters,
|
||||
})
|
||||
|
||||
callback()
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
errback(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
clients.value = (await signaling.socket.value.emitWithAck('join', {
|
||||
username: preferences.username.value,
|
||||
rtpCapabilities: rtpCapabilities.value,
|
||||
})).map(transformClient)
|
||||
|
||||
await enableMic()
|
||||
})
|
||||
|
||||
socket.on('newPeer', (client) => {
|
||||
state.clients.value.push(transformClient(client))
|
||||
clients.value.push(transformClient(client))
|
||||
triggerRef(clients)
|
||||
})
|
||||
|
||||
socket.on('peerClosed', (id) => {
|
||||
state.clients.value = state.clients.value.filter(client => client.id !== id)
|
||||
clients.value = clients.value.filter(client => client.id !== id)
|
||||
})
|
||||
|
||||
socket.on(
|
||||
'newConsumer',
|
||||
async (
|
||||
{ id, producerId, kind, rtpParameters, peerId, appData, producerPaused },
|
||||
{ id, producerId, kind, rtpParameters, peerId: clientId, appData },
|
||||
cb,
|
||||
) => {
|
||||
if (!recvTransport.value)
|
||||
@@ -82,30 +152,18 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
producerId,
|
||||
kind,
|
||||
rtpParameters,
|
||||
streamId: `${peerId}-${appData.share ? 'share' : 'mic-webcam'}`,
|
||||
appData: { ...appData, peerId },
|
||||
streamId: `${clientId}-${appData.share ? 'share' : 'mic-webcam'}`,
|
||||
appData: { ...appData, clientId },
|
||||
})
|
||||
|
||||
consumer.on('transportclose', () => {
|
||||
console.log('consumer on transportclose')
|
||||
consumers.value.delete(consumer.id)
|
||||
triggerRef(consumers)
|
||||
|
||||
const client = state.clientByIdMap.value[peerId as string]
|
||||
|
||||
if (!client)
|
||||
return
|
||||
|
||||
client.consumerIds = client.consumerIds.filter(id => id !== consumer.id)
|
||||
})
|
||||
|
||||
consumers.value.set(consumer.id, consumer)
|
||||
triggerRef(consumers)
|
||||
|
||||
console.log(consumer)
|
||||
|
||||
state.clientByIdMap.value[peerId]!.consumerIds.push(consumer.id)
|
||||
|
||||
cb()
|
||||
},
|
||||
)
|
||||
@@ -115,8 +173,6 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
async (
|
||||
{ consumerId },
|
||||
) => {
|
||||
console.log('socket on consumerClosed')
|
||||
|
||||
const consumer = consumers.value.get(consumerId)
|
||||
|
||||
if (!consumer)
|
||||
@@ -124,13 +180,6 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
|
||||
consumers.value.delete(consumer.id)
|
||||
triggerRef(consumers)
|
||||
|
||||
const client = state.clientByIdMap.value[(consumer.appData.peerId || consumer.appData.socketId) as string]
|
||||
|
||||
if (!client)
|
||||
return
|
||||
|
||||
client.consumerIds = client.consumerIds.filter(id => id !== consumer.id)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -143,97 +192,8 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
})
|
||||
}, { immediate: true, flush: 'sync' })
|
||||
|
||||
onScopeDispose(() => {
|
||||
socket.value?.close()
|
||||
})
|
||||
|
||||
async function join() {
|
||||
if (!socket.value)
|
||||
return
|
||||
|
||||
device.value = new mediasoupClient.Device()
|
||||
rtpCapabilities.value = await socket.value.emitWithAck('getRtpCapabilities')
|
||||
|
||||
await device.value.load({ routerRtpCapabilities: rtpCapabilities.value! })
|
||||
|
||||
// Send transport
|
||||
{
|
||||
const transportInfo = await socket.value.emitWithAck('createTransport', { producing: true, consuming: false })
|
||||
sendTransport.value = device.value.createSendTransport({
|
||||
...transportInfo,
|
||||
iceServers: [
|
||||
...ICE_SERVERS,
|
||||
...(transportInfo.iceServers ?? []),
|
||||
],
|
||||
})
|
||||
|
||||
sendTransport.value.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
||||
try {
|
||||
await socket.value!.emitWithAck('connectTransport', {
|
||||
transportId: sendTransport.value!.id,
|
||||
dtlsParameters,
|
||||
})
|
||||
|
||||
callback()
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
errback(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
sendTransport.value.on('produce', async ({ kind, rtpParameters, appData }, callback, errback) => {
|
||||
try {
|
||||
const { id } = await socket.value!.emitWithAck('produce', {
|
||||
transportId: sendTransport.value!.id,
|
||||
kind,
|
||||
rtpParameters,
|
||||
appData,
|
||||
})
|
||||
callback({ id })
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
errback(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Recv Transport
|
||||
{
|
||||
const transportInfo = await socket.value.emitWithAck('createTransport', { producing: false, consuming: true })
|
||||
recvTransport.value = device.value.createRecvTransport({
|
||||
...transportInfo,
|
||||
iceServers: [
|
||||
...ICE_SERVERS,
|
||||
...(transportInfo.iceServers ?? []),
|
||||
],
|
||||
})
|
||||
|
||||
recvTransport.value.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
||||
try {
|
||||
await socket.value!.emitWithAck('connectTransport', {
|
||||
transportId: recvTransport.value!.id,
|
||||
dtlsParameters,
|
||||
})
|
||||
|
||||
callback()
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
errback(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const result = await socket.value.emitWithAck('join', { username: state.username.value, rtpCapabilities: rtpCapabilities.value })
|
||||
|
||||
state.clients.value = (result as RemoteClient[]).map(transformClient)
|
||||
|
||||
await enableMic()
|
||||
function getClientConsumers(clientId: ChadClient['id']) {
|
||||
return consumers.value.values().filter(consumer => consumer.appData.clientId === clientId)
|
||||
}
|
||||
|
||||
async function enableMic() {
|
||||
@@ -281,7 +241,7 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
}
|
||||
|
||||
async function disableMic() {
|
||||
if (!micProducer.value)
|
||||
if (!signaling.socket.value || !micProducer.value)
|
||||
return
|
||||
|
||||
producers.value.delete(micProducer.value.id)
|
||||
@@ -290,7 +250,7 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
try {
|
||||
micProducer.value.close()
|
||||
|
||||
await socket.value?.emitWithAck('closeProducer', {
|
||||
await signaling.socket.value.emitWithAck('closeProducer', {
|
||||
producerId: micProducer.value.id,
|
||||
})
|
||||
}
|
||||
@@ -301,13 +261,13 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
}
|
||||
|
||||
async function muteMic() {
|
||||
if (!micProducer.value)
|
||||
if (!signaling.socket.value || !micProducer.value)
|
||||
return
|
||||
|
||||
try {
|
||||
micProducer.value.pause()
|
||||
|
||||
await socket.value?.emitWithAck('pauseProducer', {
|
||||
await signaling.socket.value.emitWithAck('pauseProducer', {
|
||||
producerId: micProducer.value.id,
|
||||
})
|
||||
}
|
||||
@@ -316,13 +276,13 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
}
|
||||
|
||||
async function unmuteMic() {
|
||||
if (!micProducer.value)
|
||||
if (!signaling.socket.value || !micProducer.value)
|
||||
return
|
||||
|
||||
try {
|
||||
micProducer.value?.resume()
|
||||
micProducer.value.resume()
|
||||
|
||||
await socket.value?.emitWithAck('resumeProducer', {
|
||||
await signaling.socket.value.emitWithAck('resumeProducer', {
|
||||
producerId: micProducer.value.id,
|
||||
})
|
||||
}
|
||||
@@ -330,24 +290,17 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
}
|
||||
}
|
||||
|
||||
async function init() {
|
||||
signaling.connect()
|
||||
}
|
||||
|
||||
function transformClient(client: RemoteClient): ChadClient {
|
||||
return {
|
||||
...client,
|
||||
isMe: client.id === socket.value!.id,
|
||||
consumerIds: [],
|
||||
isMe: client.id === signaling.socket.value!.id,
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (socket.value)
|
||||
return
|
||||
|
||||
socket.value = io('https://api.koptilnya.xyz/webrtc', {
|
||||
path: '/chad/ws',
|
||||
transports: ['websocket'],
|
||||
})
|
||||
}
|
||||
|
||||
function dispose() {
|
||||
device.value = undefined
|
||||
rtpCapabilities.value = undefined
|
||||
@@ -360,21 +313,22 @@ export const useMediasoup = createSharedComposable(() => {
|
||||
|
||||
consumers.value = new Map()
|
||||
producers.value = new Map()
|
||||
|
||||
state.reset()
|
||||
}
|
||||
|
||||
return {
|
||||
init,
|
||||
clients,
|
||||
consumers,
|
||||
producers,
|
||||
sendTransport,
|
||||
recvTransport,
|
||||
socket,
|
||||
rtpCapabilities,
|
||||
device,
|
||||
micProducer,
|
||||
webcamProducer,
|
||||
shareProducer,
|
||||
getClientConsumers,
|
||||
muteMic,
|
||||
unmuteMic,
|
||||
}
|
||||
})
|
||||
|
||||
14
client/app/composables/use-preferences.ts
Normal file
14
client/app/composables/use-preferences.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { createGlobalState, useLocalStorage } from '@vueuse/core'
|
||||
|
||||
export const usePreferences = createGlobalState(() => {
|
||||
const username = useLocalStorage<string>('username', '')
|
||||
|
||||
const audioDevice = shallowRef()
|
||||
const videoDevice = shallowRef()
|
||||
|
||||
return {
|
||||
username,
|
||||
audioDevice,
|
||||
videoDevice,
|
||||
}
|
||||
})
|
||||
58
client/app/composables/use-signaling.ts
Normal file
58
client/app/composables/use-signaling.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { Socket } from 'socket.io-client'
|
||||
import { createSharedComposable } from '@vueuse/core'
|
||||
import { io } from 'socket.io-client'
|
||||
|
||||
export const useSignaling = createSharedComposable(() => {
|
||||
const socket = shallowRef<Socket>()
|
||||
|
||||
const connected = ref(false)
|
||||
|
||||
watch(socket, (socket, prevSocket) => {
|
||||
if (prevSocket) {
|
||||
prevSocket.close()
|
||||
}
|
||||
|
||||
if (!socket) {
|
||||
return
|
||||
}
|
||||
|
||||
if (import.meta.dev) {
|
||||
socket.onAny((event, ...args) => {
|
||||
console.info('[onAny]', event, args)
|
||||
})
|
||||
|
||||
socket.onAnyOutgoing((event, ...args) => {
|
||||
console.info('[onAnyOutgoing]', event, args)
|
||||
})
|
||||
}
|
||||
|
||||
socket.on('connect', async () => {
|
||||
connected.value = true
|
||||
})
|
||||
|
||||
socket.on('disconnect', async () => {
|
||||
connected.value = false
|
||||
})
|
||||
}, { immediate: true, flush: 'sync' })
|
||||
|
||||
onScopeDispose(() => {
|
||||
socket.value?.close()
|
||||
})
|
||||
|
||||
function connect() {
|
||||
if (socket.value)
|
||||
return
|
||||
|
||||
socket.value = io('https://api.koptilnya.xyz/webrtc', {
|
||||
// socket.value = io('http://127.0.0.1:4000/webrtc', {
|
||||
path: '/chad/ws',
|
||||
transports: ['websocket'],
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
socket,
|
||||
connected,
|
||||
connect,
|
||||
}
|
||||
})
|
||||
5
client/app/layouts/auth.vue
Normal file
5
client/app/layouts/auth.vue
Normal file
@@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<div class="w-full h-full flex justify-center items-center p-3">
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
46
client/app/layouts/default.vue
Normal file
46
client/app/layouts/default.vue
Normal file
@@ -0,0 +1,46 @@
|
||||
<template>
|
||||
<div v-auto-animate class="grid grid-cols-2 h-screen">
|
||||
<div class="flex flex-col shadow-xl shadow-surface-950 overflow-y-hidden z-10">
|
||||
<AppHeader title="Шальные сиськи 18+">
|
||||
<template #right>
|
||||
<PrimeButtonGroup class="ml-auto">
|
||||
<PrimeButton
|
||||
icon="pi pi-microphone" size="large" :severity="inputMuted ? 'contrast' : 'secondary'"
|
||||
:outlined="!inputMuted" @click="toggleInput"
|
||||
/>
|
||||
<PrimeButton
|
||||
icon="pi pi-headphones" size="large" :severity="outputMuted ? 'contrast' : 'secondary'"
|
||||
:outlined="!outputMuted" @click="toggleOutput"
|
||||
/>
|
||||
</PrimeButtonGroup>
|
||||
|
||||
<PrimeButton icon="pi pi-cog" size="large" :text="!inPreferences" :severity="inPreferences ? 'contrast' : 'secondary'" @click="onClickPreferences" />
|
||||
</template>
|
||||
</AppHeader>
|
||||
|
||||
<div v-auto-animate class="p-3 overflow-y-auto flex-1 bg-surface-900 overflow-hidden divide-y divide-surface-800">
|
||||
<ClientRow v-for="client of clients" :key="client.id" :client="client" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<slot />
|
||||
</div>
|
||||
|
||||
<PrimeBadge class="fixed top-3 right-3" severity="success" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { vAutoAnimate } from '@formkit/auto-animate'
|
||||
|
||||
const { clients, inputMuted, toggleInput, outputMuted, toggleOutput } = useApp()
|
||||
|
||||
const route = useRoute()
|
||||
|
||||
const inPreferences = computed(() => {
|
||||
return route.name === 'Preferences'
|
||||
})
|
||||
|
||||
function onClickPreferences() {
|
||||
navigateTo(!inPreferences.value ? { name: 'Preferences' } : '/')
|
||||
}
|
||||
</script>
|
||||
@@ -1,8 +1,7 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
const { username } = useGlobalState()
|
||||
const { username } = usePreferences()
|
||||
|
||||
if (!username.value && to.name !== 'Login') {
|
||||
console.log('yes')
|
||||
return navigateTo({ name: 'Login' })
|
||||
}
|
||||
|
||||
@@ -13,7 +12,7 @@ export default defineNuxtRouteMiddleware((to, from) => {
|
||||
|
||||
init()
|
||||
|
||||
if (to.name === 'Login') {
|
||||
if (to.path === 'Login') {
|
||||
return navigateTo('/')
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,56 +1,11 @@
|
||||
<template>
|
||||
<div class="grid grid-cols-2 h-screen">
|
||||
<div class="flex flex-col shadow-xl overflow-y-hidden">
|
||||
<div class="flex items-center bg-surface-950 border-b-2 border-surface-800 px-3 py-3">
|
||||
<h1>
|
||||
Шальные сиськи 18+
|
||||
</h1>
|
||||
|
||||
<div class="inline-flex gap-2 ml-auto">
|
||||
<PrimeButton icon="pi pi-headphones" text size="large" />
|
||||
<PrimeButton icon="pi pi-microphone" text size="large" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-auto-animate class="p-3 overflow-y-auto flex-1 bg-surface-900 overflow-hidden divide-y divide-surface-800">
|
||||
<ClientRow v-for="client of clients" :key="client.id" :client="client" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center justify-center p-3">
|
||||
<PrimeFieldset legend="Important information">
|
||||
The chat is under development.
|
||||
</PrimeFieldset>
|
||||
</div>
|
||||
|
||||
<audio v-for="client in clients" :key="client.id" :ref="(el) => onAudioRef(el, client)" autoplay controls />
|
||||
|
||||
<PrimeBadge class="fixed top-3 right-3" severity="success" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { ChadClient } from '#shared/types'
|
||||
import { vAutoAnimate } from '@formkit/auto-animate'
|
||||
|
||||
const { clients } = useGlobalState()
|
||||
const { consumers } = useMediasoup()
|
||||
|
||||
function onAudioRef(ref: HTMLAudioElement, client: ChadClient) {
|
||||
if (!ref || !client)
|
||||
return
|
||||
|
||||
if (client.isMe)
|
||||
return
|
||||
|
||||
const consumersArray = client.consumerIds.map(id => consumers.value.get(id)!)
|
||||
|
||||
const audioConsumer = consumersArray.find(
|
||||
consumer => consumer.track.kind === 'audio',
|
||||
)
|
||||
|
||||
if (!audioConsumer)
|
||||
return
|
||||
|
||||
const stream = new MediaStream()
|
||||
|
||||
stream.addTrack(audioConsumer!.track)
|
||||
|
||||
ref.srcObject = stream
|
||||
ref.setAttribute('data-consumer-id', audioConsumer.id)
|
||||
}
|
||||
const { clients, inputMuted, toggleInput, outputMuted, toggleOutput } = useApp()
|
||||
</script>
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
<template>
|
||||
<div class="w-full h-full flex justify-center items-center">
|
||||
<PrimeCard class="w-2/5">
|
||||
<template #content>
|
||||
<form class="flex flex-col gap-3" @submit.prevent="submit">
|
||||
<PrimeInputText v-model="username" type="text" placeholder="Username" />
|
||||
<PrimeCard class="w-2/5">
|
||||
<template #content>
|
||||
<form class="flex flex-col gap-3" @submit.prevent="submit">
|
||||
<PrimeFloatLabel variant="on">
|
||||
<PrimeInputText id="username" v-model="localUsername" size="large" class="w-full" />
|
||||
<label for="username">Username</label>
|
||||
</PrimeFloatLabel>
|
||||
|
||||
<PrimeButton size="large" icon="pi pi-arrow-right" icon-pos="right" label="Let's go" :disabled="!username" type="submit" />
|
||||
</form>
|
||||
</template>
|
||||
</PrimeCard>
|
||||
</div>
|
||||
<PrimeButton size="large" icon="pi pi-arrow-right" icon-pos="right" label="Let's go" :disabled="!localUsername" type="submit" />
|
||||
</form>
|
||||
</template>
|
||||
</PrimeCard>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
definePageMeta({
|
||||
name: 'Login',
|
||||
layout: 'login',
|
||||
layout: 'auth',
|
||||
})
|
||||
|
||||
const { username } = useGlobalState()
|
||||
const { init } = useMediasoup()
|
||||
const { username } = usePreferences()
|
||||
|
||||
const localUsername = ref<typeof username.value>()
|
||||
|
||||
async function submit() {
|
||||
if (!username.value)
|
||||
if (!localUsername.value)
|
||||
return
|
||||
|
||||
username.value = localUsername.value
|
||||
|
||||
await navigateTo('/')
|
||||
}
|
||||
</script>
|
||||
|
||||
31
client/app/pages/preferences.vue
Normal file
31
client/app/pages/preferences.vue
Normal file
@@ -0,0 +1,31 @@
|
||||
<template>
|
||||
<div>
|
||||
<AppHeader title="Preferences" secondary />
|
||||
|
||||
<form class="flex flex-col gap-3 p-3" @submit.prevent="save">
|
||||
<PrimeFloatLabel variant="on">
|
||||
<PrimeInputText id="username" v-model="localUsername" size="large" class="w-full" />
|
||||
<label for="username">Username</label>
|
||||
</PrimeFloatLabel>
|
||||
|
||||
<PrimeButton label="Save" type="submit" />
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
definePageMeta({
|
||||
name: 'Preferences',
|
||||
})
|
||||
|
||||
const { username } = usePreferences()
|
||||
const toast = useToast()
|
||||
|
||||
const localUsername = ref(username.value)
|
||||
|
||||
function save() {
|
||||
username.value = localUsername.value
|
||||
|
||||
toast.add({ severity: 'success', summary: 'Saved', life: 3000 })
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user