-
Notifications
You must be signed in to change notification settings - Fork 69
/
MpegServer.cs
110 lines (91 loc) · 3.62 KB
/
MpegServer.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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SpaceTime.Core
{
public class MpegServer
{
public MpegServer(int port, Action<Graphics> drawFrame)
{
Port = port;
DrawFrame = drawFrame;
}
public int Port { get; set; }
public Action<Graphics> DrawFrame { get; set; }
private Thread WThread;
private void WorkerThread()
{
var tcp = new TcpListener(IPAddress.Any, Port);
tcp.Start();
ThreadPool.QueueUserWorkItem((x) =>
{
while (true)
{
var client = tcp.AcceptTcpClient();
using (var ns = client.GetStream())
using (var sr = new StreamReader(ns))
using (var sw = new StreamWriter(ns))
{
try
{
var httpReqBuf = new byte[1024];
ns.Read(httpReqBuf, 0, httpReqBuf.Length);
sw.WriteLine("HTTP/1.1 200 OK");
// if (Content.Length != 0) sw.WriteLine("Content-Length: " + Content.Length);
sw.WriteLine("Content-Type: video/mpeg");
sw.WriteLine("Date: " + DateTime.Now.ToUniversalTime().ToString("r"));
sw.WriteLine("Server: mws");
sw.WriteLine("Connection: keep-alive");
sw.WriteLine("");
sw.Flush();
var frame = new Bitmap(1920, 1080, PixelFormat.Format32bppArgb);
var eps = new EncoderParameters();
using (var g = Graphics.FromImage(frame))
{
while (client.Connected)
{
DrawFrame(g);
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
frame.Save(ns, jgpEncoder, myEncoderParameters);
ns.Flush();
Thread.Sleep(1000 / 30 /*FPS */);
}
}
}
catch (Exception)
{
// Console.WriteLine(e);
}
}
}
});
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
public void Start()
{
WThread = new Thread(WorkerThread);
// WThread.Priority = ThreadPriority.AboveNormal;
// WThread.IsBackground = false;
WThread.Start();
}
}
}