-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoiceComms.cpp
196 lines (165 loc) · 4.76 KB
/
VoiceComms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "../../idlib/precompiled.h"
#pragma hdrstop
#include "../Game_local.h"
// This equates to about 1 second
#define MAX_VOICE_PACKET_SIZE 382
idCVar si_voiceChat( "si_voiceChat", "1", CVAR_GAME | CVAR_BOOL | PC_CVAR_ARCHIVE | CVAR_SERVERINFO | CVAR_INFO, "enables or disables voice chat through the server" );
idCVar g_voiceChatDebug( "g_voiceChatDebug", "0", CVAR_GAME | CVAR_INTEGER | CVAR_NOCHEAT, "display info on voicechat net traffic" );
void idMultiplayerGame::ReceiveAndForwardVoiceData( int clientNum, const idBitMsg &inMsg, int messageType ) {
assert( clientNum >= 0 && clientNum < MAX_CLIENTS );
idBitMsg outMsg;
int i;
byte msgBuf[MAX_VOICE_PACKET_SIZE + 2];
idPlayer * from;
from = ( idPlayer * )gameLocal.entities[clientNum];
if( !gameLocal.serverInfo.GetBool( "si_voiceChat" ) || !from ) {
return;
}
// Create a new packet with forwarded data
outMsg.Init( msgBuf, sizeof( msgBuf ) );
outMsg.WriteByte( GAME_UNRELIABLE_MESSAGE_VOICEDATA_SERVER );
outMsg.WriteByte( clientNum );
outMsg.WriteData( inMsg.GetReadData(), inMsg.GetRemainingData() );
if( g_voiceChatDebug.GetInteger() & 2 ) {
common->Printf( "VC: Received %d bytes, forwarding...\n", inMsg.GetRemainingData() );
}
// Forward to appropriate parties
for( i = 0; i < gameLocal.numClients; i++ ) {
idPlayer* to = ( idPlayer * )gameLocal.entities[i];
if( to && to->GetUserInfo() && to->GetUserInfo()->GetBool( "s_voiceChatReceive" ) )
{
if( i != gameLocal.localClientNum && CanTalk( from, to, !!( messageType & 1 ) ) )
{
if( messageType & 2 )
{
// If "from" is testing - then only send back to him
if( from == to )
{
gameLocal.SendUnreliableMessage( outMsg, to->entityNumber );
}
}
else
{
if( to->AllowedVoiceDest( from->entityNumber ) )
{
gameLocal.SendUnreliableMessage( outMsg, to->entityNumber );
if( g_voiceChatDebug.GetInteger() & 2 )
{
common->Printf( " ... to client %d\n", to->entityNumber );
}
}
else
{
if( g_voiceChatDebug.GetInteger() )
{
common->Printf( " ... suppressed packet to client %d\n", to->entityNumber );
}
}
}
}
}
}
#ifdef _USE_VOICECHAT
// Listen servers need to manually call the receive function
if ( gameLocal.isListenServer ) {
// Skip over control byte
outMsg.ReadByte();
idPlayer* to = gameLocal.GetLocalPlayer();
if( to->GetUserInfo()->GetBool( "s_voiceChatReceive" ) )
{
if( CanTalk( from, to, !!( messageType & 1 ) ) )
{
if( messageType & 2 )
{
// If "from" is testing - then only send back to him
if( from == to )
{
ReceiveAndPlayVoiceData( outMsg );
}
}
else
{
if( to->AllowedVoiceDest( from->entityNumber ) )
{
if( g_voiceChatDebug.GetInteger() & 2 )
{
common->Printf( " ... to local client %d\n", gameLocal.localClientNum );
}
ReceiveAndPlayVoiceData( outMsg );
}
}
}
}
}
#endif
}
bool idMultiplayerGame::CanTalk( idPlayer *from, idPlayer *to, bool echo ) {
if ( !to ) {
return false;
}
if ( !from ) {
return false;
}
if ( from->spectating != to->spectating ) {
return false;
}
if ( to->IsPlayerMuted( from ) ) {
return false;
}
if ( to->GetArena() != from->GetArena() ) {
return false;
}
if ( gameLocal.IsTeamGame() && from->team != to->team ) {
return false;
}
if ( from == to ) {
return echo;
}
return true;
}
#ifdef _USE_VOICECHAT
void idMultiplayerGame::XmitVoiceData( void )
{
idBitMsg outMsg;
int count;
byte work;
byte buffer[MAX_VOICE_PACKET_SIZE];
byte msgBuf[MAX_VOICE_PACKET_SIZE + 1];
if( !gameLocal.serverInfo.GetBool( "si_voiceChat" ) )
{
return;
}
outMsg.Init( msgBuf, sizeof( msgBuf ) );
// Grab any new input and send up to the server
count = soundSystem->GetVoiceData( buffer, MAX_VOICE_PACKET_SIZE );
if( count )
{
outMsg.BeginWriting();
outMsg.BeginReading();
work = GAME_RELIABLE_MESSAGE_VOICEDATA_CLIENT + cvarSystem->GetCVarBool( "s_voiceChatEcho" ) + ( cvarSystem->GetCVarInteger( "s_voiceChatTest" ) * 2 );
outMsg.WriteByte( work );
outMsg.WriteData( buffer, count );
// FIXME!!! FIXME!!! This should be unreliable - this is very bad
networkSystem->ClientSendReliableMessage( outMsg );
if( g_voiceChatDebug.GetInteger() & 1 )
{
common->Printf( "VC: sent %d bytes at %d\n", count, gameLocal.time );
}
}
}
void idMultiplayerGame::ReceiveAndPlayVoiceData( const idBitMsg &inMsg )
{
int clientNum;
if( !gameLocal.serverInfo.GetBool( "si_voiceChat" ) )
{
return;
}
clientNum = inMsg.ReadByte();
soundSystem->PlayVoiceData( clientNum, inMsg.GetReadData(), inMsg.GetRemainingData() );
if( g_voiceChatDebug.GetInteger() & 4 )
{
common->Printf( "VC: Playing %d bytes\n", inMsg.GetRemainingData() );
}
}
#endif
// end