-
Notifications
You must be signed in to change notification settings - Fork 1
/
VirtualDesktopTimecodeServer.cs
executable file
·116 lines (100 loc) · 3.61 KB
/
VirtualDesktopTimecodeServer.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace VirtualDesktopTimecodeServer
{
public sealed class VirtualDesktopTimecodeServer
{
public static VirtualDesktopTimecodeServer Instance
{
get
{
return lazy.Value;
}
}
public void SetServerLoopTimeInMilliseconds(int value)
{
_serverLoopTimeInMilliseconds = value;
}
public void SetVideoPath(string videoPath)
{
_videoPath = videoPath;
}
public void SetIsPlaying(bool isPlaying)
{
if (_isPlaying != isPlaying)
{
_isPlaying = isPlaying;
_isPlayingChanged = true;
}
}
public void SetVideoPositionInSeconds(string videoPosition)
{
_videoPositionInSecondsString = videoPosition;
}
private VirtualDesktopTimecodeServer()
{
StartServer();
}
private void StartServer()
{
_server = new TcpListener(IPAddress.Any, 2000);
_server.Start();
AcceptConnection();
}
private void AcceptConnection()
{
_server.BeginAcceptTcpClient(HandleConnection, _server);
}
private async void HandleConnection(IAsyncResult result)
{
try
{
AcceptConnection();
TcpClient client = _server.EndAcceptTcpClient(result);
NetworkStream ns = client.GetStream();
String lastTimecode = "";
String lastVideoFile = "";
while (true)
{
await Task.Delay(TimeSpan.FromMilliseconds(_serverLoopTimeInMilliseconds));
if (_videoPath != lastVideoFile)
{
lastVideoFile = _videoPath;
byte[] videoFile = Encoding.Default.GetBytes("C " + _videoPath + "\n");
ns.Write(videoFile, 0, videoFile.Length);
}
if (_isPlayingChanged)
{
_isPlayingChanged = false;
if (!_isPlayingChanged)
{
byte[] stop = Encoding.Default.GetBytes("S\n");
ns.Write(stop, 0, stop.Length);
}
}
if (_isPlaying)
{
if (lastTimecode != _videoPositionInSecondsString)
{
lastTimecode = _videoPositionInSecondsString;
byte[] timecode = Encoding.Default.GetBytes("P " + _videoPositionInSecondsString + "\n");
ns.Write(timecode, 0, timecode.Length);
}
}
}
}
catch (Exception) { };
}
private TcpListener _server;
private string _videoPath = String.Empty;
private bool _isPlayingChanged = true;
private bool _isPlaying = false;
private string _videoPositionInSecondsString = "0.0";
private int _serverLoopTimeInMilliseconds = 20;
private static readonly Lazy<VirtualDesktopTimecodeServer> lazy =
new Lazy<VirtualDesktopTimecodeServer>(() => new VirtualDesktopTimecodeServer());
}
}