-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVideoConnection.cs
94 lines (71 loc) · 3.31 KB
/
VideoConnection.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
using BadVideoStreaming.Comms;
using System.Drawing;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace BadVideoStreaming
{
public abstract class VideoConnection
{
public Action<int, int, int, long, byte[]>? onNewFrame;
protected FrameTiming sentFrameTimer = new FrameTiming();
protected FrameTiming receivedFrameTimer = new FrameTiming();
public double TimePerFrameSentMS => sentFrameTimer.AverageTimePerFrameMs;
public double TimePerFrameReceivedMS => receivedFrameTimer.AverageTimePerFrameMs;
public abstract void SendFrame(byte streamID, Bitmap frame);
}
public class UdpVideoConnection : VideoConnection
{
private VideoStreamingEndpoint receivedStream;
private VideoStreamingOrigin sendingStream;
public UdpVideoConnection(string sendAddress, string receiveAddress, Connection metadataConnection, Action<int, int, int, long, byte[]> onNewFrame)
{
// Initialize the VideoStreaming classes here
this.onNewFrame = (width, height, streamid, timestamp, imageData) =>
{
// time it here
receivedFrameTimer.MarkFrameTime();
// call the onNewFrame parameter
onNewFrame(width, height, streamid, timestamp, imageData);
};
this.receivedStream = new VideoStreamingEndpoint(receiveAddress, metadataConnection, this.onNewFrame);
this.sendingStream = new VideoStreamingOrigin(sendAddress, metadataConnection);
}
public override void SendFrame(byte streamID, Bitmap frame)
{
sentFrameTimer.MarkFrameTime();
// Send the frame using UDP here
sendingStream.SendFrame(new Frame(streamID, frame));
}
}
public class TcpVideoConnection : VideoConnection, MessageHandler
{
private const string Tag = "Frame";
private Connection metaDataConnection;
public TcpVideoConnection(Connection metaDataConnection, Action<int, int, int, long, byte[]> onNewFrame)
{
this.metaDataConnection = metaDataConnection;
this.onNewFrame = (width, height, streamid, timestamp, imageData) =>
{
// time it here
receivedFrameTimer.MarkFrameTime();
// call the onNewFrame parameter
onNewFrame(streamid, width, height, timestamp, imageData);
};
metaDataConnection.AddMessageHandler(this);
}
public override void SendFrame(byte streamID, Bitmap frame)
{
sentFrameTimer.MarkFrameTime();
Frame videoFrame = new Frame(streamID, frame);
string base64Frame = Convert.ToBase64String(videoFrame.GetBytes().ToArray(), Base64FormattingOptions.None);
metaDataConnection.Send(new Message { tag = Tag, message = base64Frame });
}
public void OnStart(Connection connection) { }
public string GetTag() => Tag;
public void Receive(Message message)
{
byte[] frameData = Convert.FromBase64String(message.message.Trim());
Frame frame = new Frame(frameData);
onNewFrame?.Invoke(frame.width, frame.height, frame.streamid, frame.timestamp, frame.imageData.ToArray());
}
}
}