feat(voice): add WebRTC voice channels with Cloudflare TURN support

Add voice channel functionality with WebRTC peer-to-peer audio:
- Add VoiceSignalingConsumer for WebRTC signaling and peer coordination
- Add GlobalConsumer for real-time presence and voice status updates
- Add voice channel type to Channel model with current_voice_channel tracking on UserProfile
- Add voice controls UI (mute, deafen, disconnect) in status panel
- Add voice member list display in sidebar with avatar support
- Add
This commit is contained in:
2026-05-23 20:56:37 +02:00
parent 963441fe50
commit 88bdfc797b
14 changed files with 773 additions and 11 deletions
+172
View File
@@ -157,3 +157,175 @@ class DMConsumer(AsyncWebsocketConsumer):
return av.url if av else None
except Exception:
return None
class VoiceSignalingConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.channel_id = self.scope['url_route']['kwargs']['channel_id']
self.group_name = f'voice_{self.channel_id}'
self.user = self.scope['user']
if not self.user.is_authenticated:
await self.close()
return
# Add to voice channel group
await self.channel_layer.group_add(self.group_name, self.channel_name)
await self.accept()
# Update voice channel in DB
await self.set_voice_channel(self.channel_id)
# Broadcast to voice channel group for WebRTC signaling
await self.channel_layer.group_send(
self.group_name,
{
'type': 'voice_user_joined',
'user_id': self.user.id,
'username': self.user.username,
'avatar': await self.get_avatar(),
}
)
# Broadcast to global updates group to update sidebars for everyone
await self.channel_layer.group_send(
'global_updates',
{
'type': 'voice_presence_change',
'user_id': self.user.id,
'username': self.user.username,
'channel_id': int(self.channel_id),
'action': 'joined',
'avatar': await self.get_avatar(),
}
)
async def disconnect(self, close_code):
# Update voice channel in DB
await self.set_voice_channel(None)
# Broadcast user left to voice group
await self.channel_layer.group_send(
self.group_name,
{
'type': 'voice_user_left',
'user_id': self.user.id,
'username': self.user.username,
}
)
# Broadcast user left to global updates group
await self.channel_layer.group_send(
'global_updates',
{
'type': 'voice_presence_change',
'user_id': self.user.id,
'username': self.user.username,
'channel_id': int(self.channel_id),
'action': 'left',
}
)
await self.channel_layer.group_discard(self.group_name, self.channel_name)
async def receive(self, text_data):
data = json.loads(text_data)
action = data.get('action')
if action == 'signal':
target_id = data.get('target_id')
signal_data = data.get('data')
# Relay signal to the target user
await self.channel_layer.group_send(
self.group_name,
{
'type': 'voice_signal_relay',
'sender_id': self.user.id,
'sender_username': self.user.username,
'target_id': target_id,
'data': signal_data,
}
)
async def voice_user_joined(self, event):
if event['user_id'] != self.user.id:
await self.send(text_data=json.dumps(event))
async def voice_user_left(self, event):
if event['user_id'] != self.user.id:
await self.send(text_data=json.dumps(event))
async def voice_signal_relay(self, event):
if event['target_id'] == self.user.id:
await self.send(text_data=json.dumps(event))
@database_sync_to_async
def get_avatar(self):
try:
av = self.user.profile.avatar
return av.url if av else None
except Exception:
return None
@database_sync_to_async
def set_voice_channel(self, channel_id):
try:
if channel_id:
from chat.models import Channel
channel = Channel.objects.get(id=channel_id)
self.user.profile.current_voice_channel = channel
else:
self.user.profile.current_voice_channel = None
self.user.profile.save(update_fields=['current_voice_channel'])
except Exception as e:
print("Error saving voice channel:", e)
class GlobalConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.user = self.scope['user']
if not self.user.is_authenticated:
await self.close()
return
self.group_name = 'global_updates'
await self.channel_layer.group_add(self.group_name, self.channel_name)
await self.accept()
await self.set_online(True)
await self.channel_layer.group_send(
self.group_name,
{
'type': 'user_presence_change',
'user_id': self.user.id,
'username': self.user.username,
'online': True
}
)
async def disconnect(self, close_code):
await self.channel_layer.group_discard(self.group_name, self.channel_name)
await self.set_online(False)
await self.channel_layer.group_send(
self.group_name,
{
'type': 'user_presence_change',
'user_id': self.user.id,
'username': self.user.username,
'online': False
}
)
async def user_presence_change(self, event):
await self.send(text_data=json.dumps(event))
async def voice_presence_change(self, event):
await self.send(text_data=json.dumps(event))
@database_sync_to_async
def set_online(self, status):
try:
self.user.profile.online = status
self.user.profile.save(update_fields=['online'])
except Exception:
pass