-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathIHeadsetPlugin.cs
424 lines (390 loc) · 14.4 KB
/
IHeadsetPlugin.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace FSClient {
public abstract class IHeadsetDevice {
[Flags]
public enum HEADSET_LIMITATIONS { NO_UNMUTE, ONLY_ACTIVE_DURING_CALLS, ANAL };
public abstract string GetName();
public abstract string GetManufacturer();
public abstract string GetModel();
public abstract string GetUniqueId();
public abstract HEADSET_LIMITATIONS Initalize(IDeviceHost host);
public abstract void SetCallerId(String name, String number);
public abstract void SetActive(bool active);
public enum HEADSET_EVENT_TYPE { Talk, Hangup, ToggleTalk, ToggleMute, Mute, UnMute, Flash, Redial, RadioOpen, RadioClosed };
public class StatusEventArgs : EventArgs {
public HEADSET_EVENT_TYPE type { get; set; }
public StatusEventArgs(HEADSET_EVENT_TYPE type) {
this.type = type;
}
}
public EventHandler<StatusEventArgs> StatusChanged;
}
public class IDeviceHost {
public class StatusEventArgs : EventArgs {
public PHONE_EVENT_TYPE type { get; set; }
public bool enable { get; set; }
public StatusEventArgs(PHONE_EVENT_TYPE type, bool enable) {
this.type = type;
this.enable = enable;
}
}
public EventHandler<StatusEventArgs> StatusChanged;
public enum PHONE_EVENT_TYPE { Ring, Mute, LineActive, Hold, InCallRing };
internal void CreateEvent(PHONE_EVENT_TYPE type, bool enable) {
if (StatusChanged != null)
StatusChanged(this, new StatusEventArgs(type, enable));
}
}
public class HeadsetPluginManager : PluginManagerBase {
private class DeviceData {
public IDeviceHost host;
public IHeadsetDevice device;
public bool active;
public bool in_use;
public HeadsetPluginData plugin;
public IHeadsetDevice.HEADSET_LIMITATIONS limitations;
public bool HasLimit(IHeadsetDevice.HEADSET_LIMITATIONS limit) {
return (limitations & limit) == limit;
}
}
private class HeadsetPluginData : PluginData {
public HeadsetPluginData(PluginData data) : base(data){}
public HeadsetPluginData() : base() { }
public IHeadsetPlugin headset_plugin {
get { return plugin as IHeadsetPlugin; }
set { plugin = value; }
}
public int error_tries_left = 0;
public DateTime last_error_time = DateTime.Now;//start with now so error count will not reset right away
}
private class PluginError {
public DeviceData device;
public HeadsetPluginData plugin;
public Exception exception;
}
private object devices_lock = new object();
private Broker broker;
private const int DEFAULT_PLUGIN_RETRIES = 5;
private List<DeviceData> devices = new List<DeviceData>();
private List<HeadsetPluginData> plugins = new List<HeadsetPluginData>();
public static HeadsetPluginManager GetPluginManager(SettingsPluginDataCollection settings) {
HeadsetPluginManager manager = new HeadsetPluginManager();
manager.LoadSettings(settings);
return manager;
}
public override IEnumerable<PluginData> GetPlugins() {
return plugins;
}
protected override void SetPlugins(IEnumerable<PluginData> plugins) {
if (this.plugins.Count > 0)
throw new Exception("Cannot call SetPlugins if plugins have already been loaded");
foreach (PluginData data in plugins)
this.plugins.Add(new HeadsetPluginData(data));
}
public string[] AvailableDevices() {
return (from d in devices select d.device.GetName()).ToArray();
}
public string ActiveDevice() {
return (from d in devices where d.active select d.device.GetName()).SingleOrDefault();
}
private string active_device_name;
private string last_active_device_name;
public void SetActiveDevice(String name, bool is_fallback_try=false) {
active_device_name = name;
bool found_already = false;
List<PluginError> errors = null;
var backup_try = "";
lock (devices_lock) {
foreach (DeviceData device in devices) {
try {
var device_name = device.device.GetName();
if (device_name.Contains("Jabra"))
backup_try = device_name;
if (device_name == name && !found_already) {
found_already = true;
if (!device.active) {
device.active = device.in_use = true;
device.device.SetActive(true);
}
}
else if (device.active) {
device.active = device.in_use = false;
device.device.SetActive(false);
}
}
catch (Exception e) {
if (errors == null)
errors = new List<PluginError>();
errors.Add(new PluginError { device = device, exception = e });
}
}
}
if (errors != null)
foreach (PluginError err in errors)
HandleError(err);
if (!found_already && !is_fallback_try && backup_try != "")
SetActiveDevice(backup_try, true);
}
private void Ring(bool enable) {
CreateEvent(IDeviceHost.PHONE_EVENT_TYPE.Ring, enable);
}
private void Mute(bool enable) {
CreateEvent(IDeviceHost.PHONE_EVENT_TYPE.Mute, enable);
}
private void LineActive(bool enable, bool in_call) {
CreateEvent(IDeviceHost.PHONE_EVENT_TYPE.LineActive, enable, in_call);
}
private void Hold(bool enable) {
CreateEvent(IDeviceHost.PHONE_EVENT_TYPE.Hold, enable);
}
private void InCallRing(bool enable) {
CreateEvent(IDeviceHost.PHONE_EVENT_TYPE.InCallRing, enable);
}
private void CreateEvent(IDeviceHost.PHONE_EVENT_TYPE type, bool enable) {
CreateEvent(type, enable, false);
}
private void CreateEvent(IDeviceHost.PHONE_EVENT_TYPE type, bool enable, bool is_during_call) {
List<PluginError> errors = null;
lock (devices_lock) {
foreach (DeviceData data in devices) {
if (!data.active)
continue;
try {
if (type != IDeviceHost.PHONE_EVENT_TYPE.LineActive)
data.host.CreateEvent(type, enable);
else {
if (data.HasLimit(IHeadsetDevice.HEADSET_LIMITATIONS.ONLY_ACTIVE_DURING_CALLS)) {
if (is_during_call)
data.host.CreateEvent(type, enable);
}
else if (!is_during_call)
data.host.CreateEvent(type, enable);
}
}
catch (Exception e) {
if (errors == null)
errors = new List<PluginError>();
errors.Add(new PluginError { device = data, exception = e });
}
}
}
if (errors != null)
foreach (PluginError err in errors)
HandleError(err);
}
public void SetCallerID(String name, String number) {
List<PluginError> errors = null;
lock (devices_lock) {
foreach (DeviceData data in devices) {
if (data.active) {
try {
data.device.SetCallerId(name, number);
}
catch (Exception e) {
if (errors == null)
errors = new List<PluginError>();
errors.Add(new PluginError { device = data, exception = e });
}
}
}
}
if (errors != null)
foreach (PluginError err in errors)
HandleError(err);
}
private void DeviceStatusChanged(object sender, IHeadsetDevice.StatusEventArgs e) {
IHeadsetDevice device = sender as IHeadsetDevice;
DeviceData data = (from d in devices where d.device == device select d).SingleOrDefault();
if (!data.active)
return;
if (!data.in_use && e.type != IHeadsetDevice.HEADSET_EVENT_TYPE.Talk)
return;
switch (e.type) {
case IHeadsetDevice.HEADSET_EVENT_TYPE.Talk:
broker.TalkPressed();
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.ToggleTalk:
broker.TalkTogglePressed();
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.Hangup:
broker.HangupPressed();
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.Mute:
broker.Muted = true;
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.ToggleMute:
broker.Muted = !broker.Muted;
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.UnMute:
broker.Muted = false;
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.Redial:
//broker.RedialPressed();
break;
case IHeadsetDevice.HEADSET_EVENT_TYPE.Flash:
//broker.FlashPressed();
break;
}
}
private void DeviceAdded(object sender, IHeadsetPlugin.DeviceEventArgs e) {
IHeadsetPlugin plugin = sender as IHeadsetPlugin;
DeviceData data = new DeviceData { device = e.device, host = new IDeviceHost(), active = false, in_use = false };
data.plugin = (from p in plugins where p.plugin == plugin select p).Single();
try {
lock (devices_lock) {
devices.Add(data);
}
data.device.StatusChanged += DeviceStatusChanged;
data.limitations = data.device.Initalize(data.host);
data.device.GetModel();
if ((active_device_name == null && data.device.GetName() == last_active_device_name) || active_device_name == data.device.GetName()) {
SetActiveDevice(data.device.GetName());
PortAudio.refresh_devices();
}
}
catch (Exception exp) {
HandleError(new PluginError { device = data, exception = exp });
}
}
private void DeviceRemoved(object sender, IHeadsetPlugin.DeviceEventArgs e) {
DeviceData data = (from d in devices where d.device == e.device select d).SingleOrDefault();
if (data == null)
return;
lock (devices_lock) {
devices.Remove(data);
}
if (data.device.GetName() == active_device_name) {
last_active_device_name = active_device_name;
active_device_name = null;
}
}
private void BrokerDevicesReadyChanged(object sender, bool data) {
LineActive(data, false);
}
private void BrokerAnsweredChanged(object sender, bool data) {
LineActive(data, true);
}
private void BrokerMuteChanged(object sender, bool data) {
Mute(data);
}
private void BrokerCallRingingChanged(object sender, bool data) {
if (data)
SetCallerID(Call.active_call.other_party_name, Call.active_call.other_party_number);
Ring(data);
}
public HeadsetPluginManager(){
broker = Broker.get_instance();
}
public override void LoadPlugins(){
LoadActualPlugins("Headset",typeof (IHeadsetPlugin), plugins);
if ((from p in plugins where p.enabled && p.state != PluginData.PluginDataState.ERROR_LOADING select true).Count() > 0){
broker.DevicesReadyChanged += BrokerDevicesReadyChanged;
broker.call_answeredChanged += BrokerAnsweredChanged;
broker.active_call_ringingChanged += BrokerCallRingingChanged;
broker.MutedChanged += BrokerMuteChanged;
}
}
protected override void HandlePluginLoadReflectionException(PluginData data, ReflectionTypeLoadException ex) {
String err = "Error creating headset plugin from dll \"" + data.dll + "\" due to a loader exception, make sure you have the headset runtime/api/sdk installed error was:\n";
foreach (Exception e in ex.LoaderExceptions)
err += e.Message + "\n";
data.last_error = err;
data.state = PluginData.PluginDataState.ERROR_LOADING;
Utils.PluginLog(PluginManagerName(), err);
}
public override string PluginManagerName(){
return "Headset Plugin Manager";
}
protected override void PluginLoadAddPlugin(PluginData plugin){
plugins.Add(plugin as HeadsetPluginData);
}
protected override void PluginLoadRegisterPlugin(PluginData plugin){
HeadsetPluginData data = plugin as HeadsetPluginData;
try {
data.headset_plugin.DeviceAdded += DeviceAdded;
data.headset_plugin.DeviceRemoved += DeviceRemoved;
data.headset_plugin.Initialize();
data.error_tries_left = DEFAULT_PLUGIN_RETRIES;
data.state = PluginData.PluginDataState.LOADED;
}
catch (Exception e) {
HandleError(new PluginError { plugin = data, exception = e }, PluginData.PluginDataState.ERROR_LOADING);
}
}
protected override PluginData NewPluginData(String dll){
return new HeadsetPluginData();
}
~HeadsetPluginManager() {
Dispose();
}
private bool disposed;
public override void Dispose() {
if (!disposed) {
disposed = true;
foreach (HeadsetPluginData plugin_data in plugins) {
try {
if (plugin_data.plugin != null)
plugin_data.plugin.Terminate();
}
catch (Exception e) { Utils.PluginLog("Headset Plugin Manager", "Error terminating a plugin: " + e.Message); }
}
}
GC.SuppressFinalize(this);
}
private void HandleError(PluginError error, PluginData.PluginDataState failed_state = PluginData.PluginDataState.DISABLED_ERROR) {
if (error.plugin == null)
error.plugin = error.device.plugin;
if ((DateTime.Now - error.plugin.last_error_time).TotalSeconds > 60 * 5) //if no errors for 5 minutes reset error count
error.plugin.error_tries_left = DEFAULT_PLUGIN_RETRIES;
error.plugin.last_error_time = DateTime.Now;
error.plugin.last_error = error.exception.Message;
String restart_msg = error.plugin.error_tries_left > 0 ? " will try to restart/init it " + error.plugin.error_tries_left + " more times" : " will not be restarting it";
Utils.PluginLog(PluginManagerName(), "Plugin " + error.plugin.plugin.ProviderName() + " had an error due to: " + error.exception.Message + "\n" + restart_msg);
List<DeviceData> to_remove = new List<DeviceData>();
lock (devices_lock) {
foreach (DeviceData device in devices) {
if (device.plugin == error.plugin) {
try {
device.device.SetActive(false);
}
catch (Exception) {
Utils.PluginLog(PluginManagerName(), "While handling error wasn't able to deactivate device, not a major issue");
}
to_remove.Add(device);
}
}
}
lock (devices_lock) {
foreach (DeviceData device in to_remove)
devices.Remove(device);
}
error.plugin.plugin.Terminate();
if (error.plugin.error_tries_left-- > 0)
DelayedFunction.DelayedCall("IHeadsetPlugin_PluginStart_ " + error.plugin.plugin.ProviderName(), () => init_plugin(error.plugin), 1000);//give it a second
else
error.plugin.state = failed_state;
}
private void init_plugin(HeadsetPluginData plugin) {
try {
plugin.plugin.Initialize();
}
catch (Exception e) {
HandleError(new PluginError { plugin = plugin, exception = e });
}
}
}
public abstract class IHeadsetPlugin : IPlugin {
public class DeviceEventArgs : EventArgs {
public readonly IHeadsetDevice device;
public DeviceEventArgs(IHeadsetDevice device) {
this.device = device;
}
}
public EventHandler<DeviceEventArgs> DeviceAdded;
public EventHandler<DeviceEventArgs> DeviceRemoved;
}
}