-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainWindow.xaml.cs
431 lines (343 loc) · 15.7 KB
/
MainWindow.xaml.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
using AppRTCDemo.Commands;
using AppRTCDemo.Model;
using AVSPEED;
using iConfRTCModel;
using iConfRTCWPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace AppRTCDemo
{
/*
█████╗ ██╗ ██╗███████╗██████╗ ███████╗███████╗██████╗
██╔══██╗██║ ██║██╔════╝██╔══██╗██╔════╝██╔════╝██╔══██╗
███████║██║ ██║███████╗██████╔╝█████╗ █████╗ ██║ ██║
██╔══██║╚██╗ ██╔╝╚════██║██╔═══╝ ██╔══╝ ██╔══╝ ██║ ██║
██║ ██║ ╚████╔╝ ███████║██║ ███████╗███████╗██████╔╝
╚═╝ ╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚══════╝╚══════╝╚═════╝
iConfRTC WPF Demo
A demo similar to the AppRTC demo, but that uses the iConfRTC SDK.
Feedback, comments
Email us at : [email protected]
or vist https://avspeed.com
*/
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
AppModel model;
/// <summary>
/// the iConfRTC Control
/// </summary>
RTCControl rtcControl;
protected List<CurrentParticipants> currParticipants;
private CustomAdorner myAdorner;
public MainWindow()
{
InitializeComponent();
//initilize webrtc
RTC.Init();
rtcControl = new RTCControl();
currParticipants = new List<CurrentParticipants>();
//setup events
rtcControl.ErrorConnectSignaling += RtcControl_ErrorConnectSignaling;
rtcControl.ConnectedToSignaling += RtcControl_ConnectedToSignaling;
rtcControl.RTCInitialized += WebRTCInitialized;
rtcControl.UserJoinedMeeting += RtcControl_UserJoinedMeeting;
rtcControl.UserLeftMeeting += RtcControl_UserLeftMeeting;
rtcControl.ILeftMeeting += RtcControl_ILeftMeeting;
rtcControl.IJoinedMeeting += RtcControl_IJoinedMeeting;
rtcControl.MeetingMessageReceived += RtcControl_MeetingMessageReceived;
model = new AppModel();
this.DataContext = model;
model.PropertyChanged += Model_PropertyChanged;
//set the initial signaling url .. this is where our signalr server is running from
model.SignalingUrl = System.Configuration.ConfigurationManager.AppSettings["SignalingUrl"];
rtcControl.SignalingType = SignalingTypes.Socketio; //SignalingTypes.SignalR; // SignalingTypes.Socketio;
rtcControl.SignalingUrl = model.SignalingUrl;
SetupCommands();
ShowMessage("Initializing WebRTC", System.Windows.Media.Brushes.MediumBlue, true);
rtcControl.Width = 480;
rtcControl.Height = 360;
//we add our own video to the list of videos
videoList.Children.Add(rtcControl);
//we initialize webrtc - mandatory call!
}
private CustomAdorner AttachLoadingAdorner(UIElement el, string textToDisplay)
{
CustomAdorner loading = new CustomAdorner(el);
loading.FontSize = 15;
loading.OverlayedText = textToDisplay;
loading.Typeface = new Typeface(FontFamily, FontStyles.Normal,
FontWeights.Bold, FontStretch);
var layer = AdornerLayer.GetAdornerLayer(el);
layer.Add(loading);
return loading;
}
private void RtcControl_IJoinedMeeting(object sender, UserArgs e)
{
ShowMessage("Welcome to Meeting : " + e.MeetingID, System.Windows.Media.Brushes.DarkSlateBlue, true);
myAdorner = AttachLoadingAdorner(rtcControl, e.UserName);
//if you do not want to start the participant video right away remove this ..
rtcControl.StartVideo();
//start viewing sessions other than e.Session which is my session
ProcessParticipants(e.Participants, e.Session, e.Sharing);
}
private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
}
private void RtcControl_ConnectedToSignaling(object sender, string hubId)
{
ShowMessage("Connected To Signaling..", brush: System.Windows.Media.Brushes.Green, stay: false,
fontSize: 14);
//move to second screen
rtcControl.Visibility = System.Windows.Visibility.Visible;
gridLanding.Visibility = System.Windows.Visibility.Hidden;
gridHead.Visibility = System.Windows.Visibility.Hidden;
}
private void RtcControl_ErrorConnectSignaling(object sender, string error)
{
ShowMessage("Error connecting to Signaling server. : " + error,
brush: System.Windows.Media.Brushes.IndianRed, stay: false, fontSize: 14);
}
private void RtcControl_MeetingMessageReceived(object sender, MeetingMessageEventArgs e)
{
if (!gridChat.IsVisible)
gridChat.Visibility = System.Windows.Visibility.Visible;
if (e.FromUser != rtcControl.MyUserName)
{
chatLog.AppendText(e.FromUser + " : " + e.Message + Environment.NewLine);
}
}
private void RtcControl_ILeftMeeting(object sender, UserArgs e)
{
foreach (var item in currParticipants)
{
videoList.Children.RemoveAt(item.ElementPosition);
//viewerControl = null;
}
currParticipants.Clear();
var layer = AdornerLayer.GetAdornerLayer(rtcControl);
layer.Remove(myAdorner);
rtcControl.Visibility = System.Windows.Visibility.Hidden;
gridLanding.Visibility = System.Windows.Visibility.Visible;
gridHead.Visibility = System.Windows.Visibility.Visible;
ShowMessage("You left the Meeting : " + e.MeetingID);
}
private void RtcControl_UserLeftMeeting(object sender, UserArgs e)
{
try
{
if (currParticipants == null || currParticipants.Count == 0)
return;
var currParticipant = currParticipants.First(participant => participant.Session == e.Session);
if ((currParticipant != null)
&& (videoList.Children[currParticipant.ElementPosition] != null))
{
videoList.Children.RemoveAt(currParticipant.ElementPosition);
}
currParticipants.RemoveAll(x => x.Session == e.Session);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
private void MainWindow1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (rtcControl.MyMeetingID != String.Empty)
{
rtcControl.LeaveMeeting();
}
e.Cancel = false;
}
private void SetupCommands()
{
var commands = new List<Command>();
Command command = new Command();
command.Obj = imgChat;
command.Execute += delegate() { model.ShowChat = !model.ShowChat; };
commands.Add(command);
command = new Command();
command.Obj = imgMuteAudio;
command.Execute += delegate()
{
model.AudioMuted = !model.AudioMuted;
rtcControl.MuteAudio(model.AudioMuted);
};
commands.Add(command);
command = new Command();
command.Obj = imgMuteVideo;
command.Execute += delegate()
{
model.VideoMuted = !model.VideoMuted;
rtcControl.MuteVideo(model.VideoMuted);
};
commands.Add(command);
command = new Command();
command.Obj = imgLeave;
command.Execute += delegate() { rtcControl.LeaveMeeting(); };
commands.Add(command);
foreach (var item in commands)
{
CommandHelper c = new CommandHelper((UIElement) item.Obj);
c.AddCommand(item);
}
}
private void btnJoinMeeting_Click(object sender, RoutedEventArgs e)
{
rtcControl.JoinMeeting(model.UserName, model.MeetingID);
}
private void WebRTCInitialized(object sender)
{
//you can add a turn server here
//rtcControl.AddIceServer(url: "numb.viagenie.ca", userName: "[email protected]", password: "avspeedwebrtc", clearFirst: false, type: "turn");
model.WebRTCInitialized = true;
HideMessage();
//
//you can select your video device here for example by passing the identifying label or the Id for the device
//note that we have seen cases where device ids change, so the device id is not 100% reliable
//check the NewDevices event to capture the label and Id of the device
//
//sample code
//iconfRTC.SelectDevice("HD Pro Webcam C920 (046d:082d)", DeviceType.Video);
//rtcControl.AddIceServer("stun.voiparound.com");
}
void gridBigVideo_MouseEnter(object sender, MouseEventArgs e)
{
}
//void h_InConference(iConfRTC.Shared.ConferenceEventArgs e)
//{
// ShowMessage((e.userName == model.UserName ? "You " : e.userName) + " joined " + e.conferenceId);
// // MessageBox.Show("You (" + e.userName + ") have joined conference + e.conferenceId);
// h.EnableOverlay("In conference : " + e.conferenceId, "position:absolute;bottom:0px;color:#FFF;text-align:center;font-size:20px;background-color:rgba(221,221,221,0.3);width:640px;padding:10px0;z-index:2147483647;font-family: Verdana, Geneva, sans-serif;", true);
//}
private void ProcessParticipants(List<MeetingParticipants> participants, string sessionJoined, bool isSharing)
{
foreach (var participant in participants)
{
if (participant.Session != rtcControl.MySession) //you are already seeing yourself :)
{
var sessionExists = currParticipants.Any(p => p.Session == participant.Session);
if (!sessionExists)
{
var viewer = new RTCControl
{
SignalingType = SignalingTypes.Socketio,
SignalingUrl = model.SignalingUrl,
Height = 360,
Width = 480,
Visibility = System.Windows.Visibility.Visible,
ToolTip = participant.UserName,
MySession = participant.Session
};
int elementPosition = videoList.Children.Add(viewer);
currParticipants.Add(new CurrentParticipants
{
Session = participant.Session,
UserName = participant.UserName,
ElementPosition = elementPosition,
Viewer = viewer
});
MainWindow1.UpdateLayout();
AttachLoadingAdorner(viewer, participant.UserName);
//only call webrtc functions when WebRTC is ready!!
viewer.RTCInitialized += (((object a) =>
{
//you can add a turn server here
//viewer.AddIceServer(url: "numb.viagenie.ca", userName: "[email protected]", password: "avspeedwebrtc", clearFirst: false, type: "turn");
//viewer.AddIceServer("stun.voiparound.com");
//webrtc is ready, connect to signaling
viewer.ViewSession(participant.UserName, participant.Session);
}));
}
}
}
}
private void RtcControl_UserJoinedMeeting(object sender, UserArgs e)
{
//the new peer session available event is fired
//a peer joins your conference room or when you join the conference room
//and are not alone in the conference room
ProcessParticipants(e.Participants, e.Session, e.Sharing);
}
private void txtMeetingID_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.btnJoinMeeting.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
e.Handled = true;
}
}
private void chatText_GotFocus(object sender, RoutedEventArgs e)
{
if (chatText.Text == "Type here")
{
chatText.Text = "";
}
}
private void chatText_LostFocus(object sender, RoutedEventArgs e)
{
if (chatText.Text.Trim() == "")
{
chatText.Text = "Type here";
}
}
private void chatText_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
string textToSend = chatText.Text.Trim();
if (textToSend != "")
{
rtcControl.SendMessageToMeeting(textToSend);
chatLog.AppendText("You : " + textToSend + Environment.NewLine);
chatText.Clear();
e.Handled = true;
}
}
}
/// <summary>
/// Show message at the top of window
/// </summary>
/// <param name="message">message to show</param>
/// <param name="brush"> back color </param>
private void ShowMessage(string message, System.Windows.Media.Brush brush = null, bool stay = false,
Double fontSize = 29.0)
{
if (brush != null)
gridMessage.Background = brush;
else gridMessage.Background = System.Windows.Media.Brushes.Green;
gridMessage.Visibility = Visibility.Visible;
lblMessage.FontSize = fontSize;
lblMessage.Content = message;
if (!stay)
{
var _timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(3000);
_timer.Tick += new EventHandler(delegate(object s, EventArgs a)
{
gridMessage.Visibility = Visibility.Hidden;
lblMessage.Content = String.Empty;
_timer.Stop();
});
// Start the timer
_timer.Start();
}
}
/// <summary>
/// Hide message displayed at the top of the window in case ShowMessage was called with stay = true .
/// </summary>
private void HideMessage()
{
gridMessage.Visibility = Visibility.Hidden;
}
}
}