-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathForm1.cs
498 lines (433 loc) · 17 KB
/
Form1.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MumbleSharp;
using MumbleSharp.Model;
using Message = MumbleSharp.Model.Message;
namespace MumbleGuiClient
{
public partial class Form1 : Form
{
MumbleConnection connection;
ConnectionMumbleProtocol protocol;
MicrophoneRecorder recorder;
SpeakerPlayback playback;
struct ChannelInfo
{
public string Name;
public uint Id;
public uint Parent;
}
struct UserInfo
{
public uint Id;
public bool Deaf;
public bool Muted;
public bool SelfDeaf;
public bool SelfMuted;
public bool Supress;
public uint Channel;
}
class TreeNode<T> : TreeNode
{
public T Value;
}
public Form1()
{
InitializeComponent();
protocol = new ConnectionMumbleProtocol();
protocol.channelMessageReceivedDelegate = ChannelMessageReceivedDelegate;
protocol.personalMessageReceivedDelegate = PersonalMessageReceivedDelegate;
protocol.encodedVoice = EncodedVoiceDelegate;
protocol.userJoinedDelegate = UserJoinedDelegate;
protocol.userStateChangedDelegate = UserStateChangedDelegate;
protocol.userStateChannelChangedDelegate = UserStateChannelChangedDelegate;
protocol.userLeftDelegate = UserLeftDelegate;
protocol.channelJoinedDelegate = ChannelJoinedDelegate;
protocol.channelLeftDelegate = ChannelLeftDelegate;
protocol.serverConfigDelegate = ServerConfigDelegate;
tvUsers.ExpandAll();
tvUsers.StartUpdating();
playback = new SpeakerPlayback();
int playbackDeviceCount = NAudio.Wave.WaveOut.DeviceCount;
cbPlaybackDevices.Items.Add("Default Playback Device");
for (int i = 0; i < playbackDeviceCount; i++)
{
NAudio.Wave.WaveOutCapabilities deviceInfo = NAudio.Wave.WaveOut.GetCapabilities(i);
string deviceText = string.Format("{0}, {1} channels", deviceInfo.ProductName, deviceInfo.Channels);
cbPlaybackDevices.Items.Add(deviceText);
}
cbPlaybackDevices.SelectedIndex = 0;
SpeakerPlayback.SelectedDevice = cbPlaybackDevices.SelectedIndex - 1;
recorder = new MicrophoneRecorder(protocol);
int recorderDeviceCount = NAudio.Wave.WaveIn.DeviceCount;
for (int i = 0; i < recorderDeviceCount; i++)
{
NAudio.Wave.WaveInCapabilities deviceInfo = NAudio.Wave.WaveIn.GetCapabilities(i);
string deviceText = string.Format("{0}, {1} channels", deviceInfo.ProductName, deviceInfo.Channels);
cbRecordingDevices.Items.Add(deviceText);
}
if (recorderDeviceCount > 0)
{
MicrophoneRecorder.SelectedDevice = 0;
cbRecordingDevices.SelectedIndex = 0;
}
numVoiceDetectorThreshold.Value = Convert.ToDecimal(recorder.VoiceDetectionThreshold)*100;
}
UserInfo GetUserInfo(User user)
{
return new UserInfo
{
Id = user.Id,
Deaf = user.Deaf,
Muted = user.Muted,
SelfDeaf = user.SelfDeaf,
SelfMuted = user.SelfMuted,
Supress = user.Suppress,
Channel = user.Channel.Id
};
}
ChannelInfo GetChannelInfo(Channel channel)
{
return new ChannelInfo
{
Name = channel.Name,
Id = channel.Id,
Parent = channel.Parent
};
}
TreeNode GetUserNode(uint user_id, TreeNode rootNode)
{
foreach (TreeNode node in rootNode.Nodes)
{
if (node is TreeNode<UserInfo>) if (((TreeNode<UserInfo>)node).Value.Id == user_id)
return node;
if (node is TreeNode<ChannelInfo>)
{
TreeNode subNode = GetUserNode(user_id, node);
if (subNode != null) return subNode;
}
}
return null;
}
TreeNode GetChannelNode(uint channel_id, TreeNode rootNode)
{
if (rootNode is TreeNode<ChannelInfo>)
if (((TreeNode<ChannelInfo>)rootNode).Value.Id == channel_id)
return rootNode;
foreach (TreeNode node in rootNode.Nodes)
{
if (node is TreeNode<ChannelInfo>)
{
if (((TreeNode<ChannelInfo>)node).Value.Id == channel_id)
return node;
TreeNode subNode = GetChannelNode(channel_id, node);
if (subNode != null) return subNode;
}
}
return null;
}
TreeNode MakeChannelNode(Channel channel)
{
TreeNode<ChannelInfo> result = new TreeNode<ChannelInfo>();
result.Text = channel.Name;
result.BackColor = Color.LightBlue;
result.Value = GetChannelInfo(channel);
return result;
}
TreeNode MakeUserNode(User user)
{
TreeNode<UserInfo> result = new TreeNode<UserInfo>();
result.Text = user.Name;
result.BackColor = Color.LightGreen;
result.Value = GetUserInfo(user);
return result;
}
bool DeleteUserNode(uint user_id, TreeNode rootNode)
{
TreeNode<UserInfo> user = null;
foreach (TreeNode node in rootNode.Nodes)
{
if (node is TreeNode<UserInfo>) if (((TreeNode<UserInfo>)node).Value.Id == user_id)
{
user = node as TreeNode<UserInfo>;
break;
}
if (node is TreeNode<ChannelInfo>)
{
if (DeleteUserNode(user_id, node))
return true;
}
}
if (user != null)
{
user.Remove();
return true;
}
return false;
return false;
}
bool DeleteChannelNode(uint channel_id, TreeNode rootNode)
{
if (rootNode is TreeNode<ChannelInfo>) if (((TreeNode<ChannelInfo>)rootNode).Value.Id == channel_id)
{
rootNode.Remove();
return true;
}
TreeNode channelNode = null;
foreach (TreeNode node in rootNode.Nodes)
{
if (node is TreeNode<ChannelInfo>)
{
if (((TreeNode<ChannelInfo>)node).Value.Id == channel_id)
{
channelNode = node;
break;
}
if (DeleteUserNode(channel_id, node))
return true;
}
}
if (channelNode != null)
{
channelNode.Remove();
return true;
}
return false;
}
private void btnSend_Click(object sender, EventArgs e)
{
string message = tbSendMessage.Text;
Channel target = protocol.LocalUser.Channel;
tbLog.BeginInvoke((MethodInvoker)(() =>
{
tbLog.AppendText(string.Format("[{0:HH:mm:ss}] {1} to {2}: {3}\n", DateTime.Now, protocol.LocalUser.Name, protocol.LocalUser.Channel.Name, message));
}));
var msg = new MumbleProto.TextMessage
{
Actor = protocol.LocalUser.Id,
Message = tbSendMessage.Text,
};
if (msg.ChannelIds == null)
msg.ChannelIds = new uint[] { target.Id };
else
msg.ChannelIds = msg.ChannelIds.Concat(new uint[] { target.Id }).ToArray();
connection.SendControl<MumbleProto.TextMessage>(MumbleSharp.Packets.PacketType.TextMessage, msg);
tbSendMessage.Text = "";
}
private void mumbleUpdater_Tick(object sender, EventArgs e)
{
if (connection != null)
if (connection.Process())
Thread.Yield();
else
Thread.Sleep(1);
}
private void tvUsers_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (tvUsers.SelectedNode is TreeNode<ChannelInfo>)
{
ChannelInfo channel = ((TreeNode<ChannelInfo>)tvUsers.SelectedNode).Value;
//Enter that channel, needs the functionality in connection or protocol.
protocol.Channels.SingleOrDefault(a => a.Id == channel.Id)?.Join();
}
}
//--------------------------
void EncodedVoiceDelegate(BasicMumbleProtocol proto, byte[] data, uint userId, long sequence, MumbleSharp.Audio.Codecs.IVoiceCodec codec, MumbleSharp.Audio.SpeechTarget target)
{
User user = proto.Users.FirstOrDefault(u => u.Id == userId);
AddPlayback(user);
TreeNode<UserInfo> userNode = null;
foreach (TreeNode<ChannelInfo> chanelNode in tvUsers.Nodes)
{
foreach (TreeNode<UserInfo> subNode in chanelNode.Nodes.OfType<TreeNode<UserInfo>>())
{
if (subNode.Value.Id == user.Id)
userNode = (TreeNode<UserInfo>)subNode;
}
if (userNode != null)
{
break;
}
}
if (userNode != null)
{
tvUsers.AddNotifyingNode(userNode, " [SPEAK]", TimeSpan.FromMilliseconds(500));
}
}
void ChannelJoinedDelegate(BasicMumbleProtocol proto, Channel channel)
{
TreeNode<ChannelInfo> channelNode = null;
if (tvUsers.Nodes.Count > 0)
channelNode = (TreeNode<ChannelInfo>)GetChannelNode(channel.Id, tvUsers.Nodes[0]);
if (channelNode == null)
{
channelNode = (TreeNode<ChannelInfo>)MakeChannelNode(channel);
TreeNode<ChannelInfo> channeParentlNode = null;
if (channel.Id > 0)
{
if (tvUsers.Nodes.Count > 0)
channeParentlNode = (TreeNode<ChannelInfo>)GetChannelNode(channel.Parent, tvUsers.Nodes[0]);
}
if (channeParentlNode == null)
tvUsers.Nodes.Add(channelNode);
else
channeParentlNode.Nodes.Add(channelNode);
}
}
void ChannelLeftDelegate(BasicMumbleProtocol proto, Channel channel)
{
DeleteChannelNode(channel.Id, tvUsers.Nodes[0]);
}
void UserJoinedDelegate(BasicMumbleProtocol proto, User user)
{
TreeNode<UserInfo> userNode = (TreeNode<UserInfo>)MakeUserNode(user);
TreeNode channelNode = GetChannelNode(user.Channel.Id, tvUsers.Nodes[0]);
if (channelNode == null)
{
channelNode = MakeChannelNode(user.Channel);
TreeNode parentChannelNode = GetChannelNode(user.Channel.Parent, tvUsers.Nodes[0]);
parentChannelNode.Nodes.Add(channelNode);
}
channelNode.Nodes.Add(userNode);
}
void UserStateChangedDelegate(BasicMumbleProtocol proto, User user)
{
TreeNode<UserInfo> userNode = (TreeNode<UserInfo>)GetUserNode(user.Id, tvUsers.Nodes[0]);
if (userNode == null)
{
//Just for safety:
//this should never happen as the UserJoinedDelegate should have already been called
//therefore the userNode should always exist when UserStateChangedDelegate is called
UserJoinedDelegate(proto, user);
}
else
{
userNode.Value = GetUserInfo(user);
}
}
void UserStateChannelChangedDelegate(BasicMumbleProtocol proto, User user, uint oldChannelId)
{
TreeNode<UserInfo> userNode = (TreeNode<UserInfo>)GetUserNode(user.Id, tvUsers.Nodes[0]);
GetChannelNode(oldChannelId, tvUsers.Nodes[0])
.Nodes.Remove(userNode);
GetChannelNode(user.Channel.Id, tvUsers.Nodes[0])
.Nodes.Add(userNode);
}
private void AddPlayback(User user)
{
if (user.Id != connection.Protocol.LocalUser.Id)
SpeakerPlayback.AddPlayer(user.Id, user.Voice);
}
void UserLeftDelegate(BasicMumbleProtocol proto, User user)
{
DeleteUserNode(user.Id, tvUsers.Nodes[0]);
SpeakerPlayback.RemovePlayer(user.Id);
}
void ChannelMessageReceivedDelegate(BasicMumbleProtocol proto, ChannelMessage message)
{
if (message.Channel.Equals(proto.LocalUser.Channel))
tbLog.BeginInvoke((MethodInvoker)(() =>
{
tbLog.AppendText(string.Format("[{0:HH:mm:ss}] {1} to {2}: {3}\n", DateTime.Now, message.Sender.Name, message.Channel.Name, message.Text));
}));
}
void PersonalMessageReceivedDelegate(BasicMumbleProtocol proto, PersonalMessage message)
{
tbLog.BeginInvoke((MethodInvoker)(() =>
{
tbLog.AppendText(string.Format("[{0:HH:mm:ss}] {1} to you: {2}\n", DateTime.Now, message.Sender.Name, message.Text));
}));
}
void ServerConfigDelegate(BasicMumbleProtocol proto, MumbleProto.ServerConfig serverConfig)
{
tbLog.BeginInvoke((MethodInvoker)(() =>
{
tbLog.AppendText(string.Format("{0}\n", serverConfig.WelcomeText));
}));
}
private void btnRecord_Click(object sender, EventArgs e)
{
if (recorder._recording)
{
btnRecord.Text = "record";
recorder.Stop();
}
else
{
btnRecord.Text = "stop";
recorder.Record();
}
}
private void tvUsers_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
}
private void tvUsers_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
}
private void cbPlaybackDevices_SelectedIndexChanged(object sender, EventArgs e)
{
SpeakerPlayback.SelectedDevice = cbPlaybackDevices.SelectedIndex - 1;
}
private void cbRecordingDevices_SelectedIndexChanged(object sender, EventArgs e)
{
MicrophoneRecorder.SelectedDevice = cbRecordingDevices.SelectedIndex;
}
private void btnConnect_Click(object sender, EventArgs e)
{
string name = textBoxUserName.Text;
string pass = textBoxUserPassword.Text;
int port;
string addr;
if(textBoxServer.Text.Contains(':'))
{
var server = textBoxServer.Text.Split(':');
addr = server[0];
port = Int32.Parse(server[1]);
}
else
{
addr = textBoxServer.Text;
port = 64738;
}
if (connection != null)
{
connection.Close();
connection = null;
protocol.Close();
tvUsers.Nodes.Clear();
}
string srvConnectName = textBoxUserName.Text + "@" + addr + ":" + port;
connection = new MumbleConnection(new IPEndPoint(Dns.GetHostAddresses(addr).First(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork), port), protocol);
connection.Connect(name, pass, new string[0], srvConnectName);
while (connection.Protocol.LocalUser == null)
{
connection.Process();
Thread.Sleep(1);
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
if (connection != null)
{
connection.Close();
connection = null;
protocol.Close();
tvUsers.Nodes.Clear();
}
}
private void numVoiceDetectionThreshold_ValueChanged(object sender, EventArgs e)
{
recorder.VoiceDetectionThreshold = Convert.ToSingle(numVoiceDetectorThreshold.Value / 100);
}
//----------------------------
}
}