forked from pathartl/TeamsPresence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraDetectionService.cs
76 lines (61 loc) · 2.18 KB
/
CameraDetectionService.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace TeamsPresence
{
public class CameraStatusChangedEventArgs : EventArgs
{
public CameraStatus Status { get; set; }
public string AppName { get; set; }
}
public class CameraDetectionService
{
private const string SubKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam";
private const string AppNamePattern = @"_[\d|\w]{13}$";
public event EventHandler<CameraStatusChangedEventArgs> StatusChanged;
private int PollingRate;
private string ActiveAppName = "";
public CameraDetectionService(int pollingRate)
{
PollingRate = pollingRate;
}
public void Start()
{
var appNameRegex = new Regex(AppNamePattern);
while (true)
{
var activeCameraApp = GetActiveCameraApp();
if (activeCameraApp != null)
activeCameraApp = appNameRegex.Replace(activeCameraApp, "", 1);
if (activeCameraApp != ActiveAppName)
{
ActiveAppName = activeCameraApp;
StatusChanged?.Invoke(this, new CameraStatusChangedEventArgs()
{
Status = ActiveAppName == "" ? CameraStatus.Inactive : CameraStatus.Active,
AppName = ActiveAppName
});
}
Thread.Sleep(PollingRate);
}
}
private string GetActiveCameraApp()
{
var key = Registry.CurrentUser.OpenSubKey(SubKey);
foreach (var app in key.GetSubKeyNames())
{
var lastUsedTimeStop = Registry.CurrentUser.OpenSubKey($@"{SubKey}\{app}")?.GetValue("LastUsedTimeStop");
if (lastUsedTimeStop != null && (long)lastUsedTimeStop == 0)
{
return app;
}
}
return "";
}
}
}