/
UserCall.cs
244 lines (206 loc) · 8.96 KB
/
UserCall.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Rtc.Signaling;
using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Collaboration.ConferenceManagement;
using Microsoft.Rtc.Collaboration.Conferencing;
using Microsoft.Rtc.Collaboration.AudioVideo;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.AudioFormat;
namespace Locations
{
public enum UserCallTransferPath
{
Previous,
Next
}
public class UserCall
{
string _uri;
Location _location;
BackToBackCall _b2bCall;
AudioVideoCall _controlAVCall;
SpeechRecognitionEngine _speechRecognitionEngine;
UserCallTransferPath _userCallTransferPath = UserCallTransferPath.Next;
public string Uri { get { return _uri; } }
public Location Location { get { return _location; } }
public UserCallTransferPath TransferPath { get { return _userCallTransferPath; } }
public event EventHandler Terminated;
void Log(string data)
{
Console.WriteLine("Location \"{0}\" {1}", _uri, data);
}
public UserCall(string uri, AudioVideoCall avCall, Location location)
{
_uri = uri;
JoinLocation(avCall, location);
}
public void JoinLocation(AudioVideoCall incomingCall, Location location)
{
_location = location;
// Call Leg 1
BackToBackCallSettings settings1 = new BackToBackCallSettings(incomingCall);
// Call Leg 2
BackToBackCallSettings settings2 = new BackToBackCallSettings(new AudioVideoCall(location.Conversation));
settings2.CallEstablishOptions = new AudioVideoCallEstablishOptions()
{
UseGeneratedIdentityForTrustedConference = true,
SupportsReplaces = CapabilitySupport.Supported
};
// Create and establish the back to back call.
_b2bCall = new BackToBackCall(settings1, settings2);
_b2bCall.StateChanged += new EventHandler<BackToBackCallStateChangedEventArgs>(_b2bCall_StateChanged);
_b2bCall.BeginEstablish(
ar =>
{
try
{
_b2bCall.EndEstablish(ar);
EstablishControlCall();
}
catch (RealTimeException ex)
{
Log(ex.ToString());
}
},
null);
}
void _b2bCall_StateChanged(object sender, BackToBackCallStateChangedEventArgs e)
{
Log("B2BCall StateChanged. PreviousState=" + e.PreviousState + " State=" + e.State);
if (e.State == BackToBackCallState.Terminated)
{
if (Terminated != null)
{
Terminated(this, EventArgs.Empty);
}
}
}
// For some reason, m_callLocalIdentity field is hidden((
ConversationParticipant GetLocalParticipant()
{
try
{
FieldInfo fi = typeof(Call).GetField("m_callLocalIdentity", BindingFlags.NonPublic | BindingFlags.Instance);
if (fi == null)
return null;
return (ConversationParticipant)fi.GetValue(_b2bCall.Call2);
}
catch (Exception)
{
return null;
}
}
void EstablishControlCall()
{
AudioVideoCallEstablishOptions ceo = new AudioVideoCallEstablishOptions()
{
UseGeneratedIdentityForTrustedConference = true
};
_controlAVCall = new AudioVideoCall(_location.Conversation);
_controlAVCall.AudioVideoFlowConfigurationRequested += new EventHandler<AudioVideoFlowConfigurationRequestedEventArgs>(_controlAVCall_AudioVideoFlowConfigurationRequested);
_controlAVCall.BeginEstablish(ceo,
ar =>
{
try
{
_controlAVCall.EndEstablish(ar);
List<IncomingAudioRoute> routesin = new List<IncomingAudioRoute>();
List<OutgoingAudioRoute> routesout = new List<OutgoingAudioRoute>();
ParticipantEndpoint localEndpoint = GetLocalParticipant().GetEndpoints()[0];
ParticipantEndpoint remoteEndpoint = _controlAVCall.RemoteEndpoint;
routesin.Add(new IncomingAudioRoute(localEndpoint));
routesin.Add(new IncomingAudioRoute(remoteEndpoint));
routesout.Add(new OutgoingAudioRoute(localEndpoint));
routesout.Add(new OutgoingAudioRoute(remoteEndpoint));
_controlAVCall.AudioVideoMcuRouting.BeginUpdateAudioRoutes(routesout, routesin,
ar2 =>
{
try
{
_controlAVCall.AudioVideoMcuRouting.EndUpdateAudioRoutes(ar2);
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
void _controlAVCall_AudioVideoFlowConfigurationRequested(object sender, AudioVideoFlowConfigurationRequestedEventArgs e)
{
Log("ControlAVCall AudioVideoFlowConfigurationRequested");
e.Flow.StateChanged += new EventHandler<MediaFlowStateChangedEventArgs>(Flow_StateChanged);
}
void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)
{
Log("ControlAVCall Flow_StateChanged PreviousState=" + e.PreviousState + " State=" + e.State);
AudioVideoFlow avFlow = (AudioVideoFlow)sender;
if (avFlow.State == MediaFlowState.Active)
{
SpeechRecognitionConnector speechRecognitionConnector = new SpeechRecognitionConnector();
speechRecognitionConnector.AttachFlow(avFlow);
SpeechRecognitionStream stream = speechRecognitionConnector.Start();
_speechRecognitionEngine = new SpeechRecognitionEngine();
_speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_speechRecognitionEngine_SpeechRecognized);
_speechRecognitionEngine.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>(_speechRecognitionEngine_LoadGrammarCompleted);
Choices pathChoice = new Choices(new string[] { "previous", "next" });
Grammar gr = new Grammar(new GrammarBuilder(pathChoice));
_speechRecognitionEngine.LoadGrammarAsync(gr);
SpeechAudioFormatInfo speechAudioFormatInfo = new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, Microsoft.Speech.AudioFormat.AudioChannel.Mono);
_speechRecognitionEngine.SetInputToAudioStream(stream, speechAudioFormatInfo);
_speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
else
{
if (avFlow.SpeechRecognitionConnector != null)
{
avFlow.SpeechRecognitionConnector.DetachFlow();
}
}
}
void _speechRecognitionEngine_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
{
Log("_speechRecognitionEngine_LoadGrammarCompleted");
}
void _speechRecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Log("_speechRecognitionEngine_SpeechRecognized " +
"Confidence=" + e.Result.Confidence + " " +
"Text=" + e.Result.Text);
if (e.Result.Text == "next")
{
_userCallTransferPath = UserCallTransferPath.Next;
}
else if (e.Result.Text == "previous")
{
_userCallTransferPath = UserCallTransferPath.Previous;
}
// Performing a self-transfer
AudioVideoCall avCall = (AudioVideoCall)_b2bCall.Call1;
avCall.BeginTransfer(avCall,
ar =>
{
try
{
avCall.EndTransfer(ar);
}
catch (Exception ex)
{
Log(ex.ToString());
}
},
null);
}
}
}