-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathEncoder.cs
196 lines (169 loc) · 6.75 KB
/
Encoder.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Capture;
using Windows.Graphics.DirectX.Direct3D11;
using Windows.Media.Core;
using Windows.Media.MediaProperties;
using Windows.Media.Transcoding;
using Windows.Storage.Streams;
using ScreenSenderComponent;
namespace CaptureEncoder
{
public sealed class Encoder : IDisposable
{
public Encoder(IDirect3DDevice device, GraphicsCaptureItem item)
{
_device = device;
_captureItem = item;
_isRecording = false;
CreateMediaObjects();
}
public IAsyncOperation<bool> EncodeAsync(IRandomAccessStream stream, uint width, uint height, uint bitrateInBps, uint frameRate, LoopbackAudioCapture loopbackAudioCapture, bool withCursor = true)
{
return EncodeInternalAsync(stream, width, height, bitrateInBps, frameRate, loopbackAudioCapture, withCursor).AsAsyncOperation();
}
private async Task<bool> EncodeInternalAsync(IRandomAccessStream stream, uint width, uint height, uint bitrateInBps, uint frameRate, LoopbackAudioCapture loopbackAudioCapture, bool withCursor = true)
{
if (!_isRecording)
{
_isRecording = true;
_frameGenerator = new CaptureFrameWait(
_device,
_captureItem,
_captureItem.Size,
loopbackAudioCapture);
using (_frameGenerator)
{
await _frameGenerator.InitializeCapture(_captureItem.Size, loopbackAudioCapture, withCursor);
var encodingProfile = new MediaEncodingProfile();
encodingProfile.Container.Subtype = "MPEG4";
encodingProfile.Video.Subtype = "H264";
encodingProfile.Video.Width = width;
encodingProfile.Video.Height = height;
encodingProfile.Video.Bitrate = bitrateInBps;
encodingProfile.Video.FrameRate.Numerator = frameRate;
encodingProfile.Video.FrameRate.Denominator = 1;
encodingProfile.Video.PixelAspectRatio.Numerator = 1;
encodingProfile.Video.PixelAspectRatio.Denominator = 1;
var transcode = await _transcoder.PrepareMediaStreamSourceTranscodeAsync(_mediaStreamSource, stream, encodingProfile);
if (transcode.CanTranscode)
{
await transcode.TranscodeAsync();
return true;
}
}
}
return false;
}
public void Dispose()
{
if (_closed)
{
return;
}
_closed = true;
DisposeInternal();
_isRecording = false;
}
private void DisposeInternal()
{
_frameGenerator.Dispose();
}
private void CreateMediaObjects()
{
// Create our encoding profile based on the size of the item
int width = _captureItem.Size.Width;
int height = _captureItem.Size.Height;
// Describe our input: uncompressed BGRA8 buffers
var videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, (uint)width, (uint)height);
_videoDescriptor = new VideoStreamDescriptor(videoProperties);
// Create our MediaStreamSource
_mediaStreamSource = new MediaStreamSource(_videoDescriptor);
_mediaStreamSource.BufferTime = TimeSpan.FromSeconds(0);
_mediaStreamSource.Starting += OnMediaStreamSourceStarting;
_mediaStreamSource.SampleRequested += OnMediaStreamSourceSampleRequested;
// Create our transcoder
_transcoder = new MediaTranscoder();
_transcoder.HardwareAccelerationEnabled = true;
}
private void OnMediaStreamSourceSampleRequested(MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args)
{
if (_isRecording && !_closed)
{
try
{
using (var frame = _frameGenerator.WaitForNewFrame())
{
if (frame == null)
{
args.Request.Sample = null;
DisposeInternal();
return;
}
var timeStamp = frame.SystemRelativeTime;
var sample = MediaStreamSample.CreateFromDirect3D11Surface(frame.Surface, timeStamp);
args.Request.Sample = sample;
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
Debug.WriteLine(e);
args.Request.Sample = null;
DisposeInternal();
}
}
else
{
args.Request.Sample = null;
DisposeInternal();
}
}
private void OnMediaStreamSourceStarting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args)
{
using (var frame = _frameGenerator.WaitForNewFrame())
{
args.Request.SetActualStartPosition(frame.SystemRelativeTime);
}
}
private IDirect3DDevice _device;
private GraphicsCaptureItem _captureItem;
private CaptureFrameWait _frameGenerator;
private VideoStreamDescriptor _videoDescriptor;
private MediaStreamSource _mediaStreamSource;
private MediaTranscoder _transcoder;
private bool _isRecording;
private bool _closed = false;
}
public struct SizeUInt32
{
public uint Width;
public uint Height;
}
// Presets are made to match MediaEncodingProfile for ease of use
public static class EncoderPresets
{
public static SizeUInt32[] Resolutions => new SizeUInt32[]
{
new SizeUInt32() { Width = 1280, Height = 720 },
new SizeUInt32() { Width = 1920, Height = 1080 },
new SizeUInt32() { Width = 3840, Height = 2160 },
new SizeUInt32() { Width = 7680, Height = 4320 }
};
public static uint[] Bitrates => new uint[]
{
9000000,
18000000,
36000000,
72000000,
};
public static uint[] FrameRates => new uint[]
{
30,
60
};
}
}