-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
145 lines (113 loc) · 4.02 KB
/
MainForm.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
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 System.Xml;
namespace myCamViewer
{
public partial class MainForm : Form
{
private CameraList camList = new CameraList();
// Server configuration URL
private readonly string configUrl = "http://demo.macroscop.com:8080/configex?login=root";
// Object that limits number of threads that can access pictureBox to 1
private object lockObj = new object();
// Token to stop video translation
private static CancellationTokenSource cts = new CancellationTokenSource();
private SynchronizationContext sync = new SynchronizationContext();
public MainForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateCamList();
if (camList.Count != 0)
listBox1.SelectedIndex = 0;
}
void updatePB (Image img)
{
// update image on main thread
sync.Post(new SendOrPostCallback(_ => pictureBox1.Image = img), null);
}
private void UpdateCamList()
{
camList = extractCamerasFromXml(loadXmlDocument(configUrl));
listBox1.Items.Clear();
foreach (var cam in camList)
{
listBox1.Items.Add(cam.Name);
}
}
/// <summary>
/// Exctracts list of cameras from given Xml document
/// </summary>
/// <param name="doc">Xml document that has info about video channels</param>
/// <returns>CameraList object</returns>
private CameraList extractCamerasFromXml(XmlDocument doc)
{
CameraList cl = new CameraList();
var els = doc.SelectNodes("Configuration/Channels/ChannelInfo");
foreach (XmlNode el in els)
{
var xmlEl = (XmlElement)el;
string name = xmlEl.GetAttribute("Name");
string id = xmlEl.GetAttribute("Id");
Camera cam = new Camera(id, name);
cam.Url = $"http://demo.macroscop.com:8080/mobile?login=root&channelid={id}&resolutionX=640&resolutionY=480&fps=25";
cl.Add(cam);
}
return cl;
}
/// <summary>
/// Loads XML document from given URL
/// </summary>
/// <param name="url">Url string</param>
/// <returns></returns>
private XmlDocument loadXmlDocument(string url)
{
string xmlStr;
using (var wc = new WebClient())
{
xmlStr = wc.DownloadString(url);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
return xmlDoc;
}
private void updCamListBtn_Click(object sender, EventArgs e)
{
UpdateCamList();
}
void ChangeCamera (string name)
{
// stop active video translation
cts.Cancel();
// create new cancellation token
cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
// get connection url
string url = camList.GetByName(name).Url;
// start new video translation
Task.Run(() => VideoDecoder.StartAsync(updatePB, url, token: token));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ChangeCamera(listBox1.SelectedItem.ToString());
}
private void MainForm_Shown(object sender, EventArgs e)
{
if (camList.Count == 0)
MessageBox.Show("No cameras were loaded :<");
else
MessageBox.Show($"Successfully loaded {camList.Count} cameras!");
}
}
}