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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
| namespace z_remote_control.Utils { using SIPSorceryMedia.Abstractions; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks;
public class ZVideoSource : IVideoSource, IDisposable { public static readonly List<VideoFormat> SupportedFormats = new List<VideoFormat>() { new VideoFormat( VideoCodecsEnum.VP8, 96 ), new VideoFormat( VideoCodecsEnum.H264, 100, parameters: "packetization-mode=1" ) };
private int _frameSpacing; private readonly byte[] _myI420Buffer; private readonly Timer _sendTestPatternTimer; private bool _isStarted; private bool _isPaused; private bool _isClosed; private bool _isMaxFrameRate; private int _frameCount; private readonly IVideoEncoder _videoEncoder; private readonly MediaFormatManager<VideoFormat> _formatManager;
public event RawVideoSampleDelegate OnVideoSourceRawSample;
public event RawVideoSampleFasterDelegate OnVideoSourceRawSampleFaster;
public event EncodedSampleDelegate OnVideoSourceEncodedSample;
public event SourceErrorDelegate OnVideoSourceError; private const int SCREEN_WIDTH = 1280; private const int SCREEN_HEIGHT = 720;
public ZVideoSource(IVideoEncoder encoder = null) { if (encoder != null) { _videoEncoder = encoder; _formatManager = new MediaFormatManager<VideoFormat>(SupportedFormats); } _myI420Buffer = new byte[10 * 1024 * 1024]; UpdateBuffer(); _sendTestPatternTimer = new Timer( GenerateTestPattern, null, -1, -1 ); _frameSpacing = 33; }
private void UpdateBuffer() { var source = ZScreenUtils.CaptureScreen( SCREEN_WIDTH, SCREEN_HEIGHT ); var i420Byte = PixelConverter.BGRtoI420( source, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH * 3 ); Buffer.BlockCopy( i420Byte, 0, _myI420Buffer, 0, i420Byte.Length ); }
public void RestrictFormats(Func<VideoFormat, bool> filter) => _formatManager.RestrictFormats(filter);
public List<VideoFormat> GetVideoSourceFormats() => _formatManager.GetSourceFormats();
public void SetVideoSourceFormat(VideoFormat videoFormat) => _formatManager.SetSelectedFormat(videoFormat);
public List<VideoFormat> GetVideoSinkFormats() => _formatManager.GetSourceFormats();
public void SetVideoSinkFormat(VideoFormat videoFormat) => _formatManager.SetSelectedFormat(videoFormat);
public void ForceKeyFrame() => _videoEncoder?.ForceKeyFrame();
public bool HasEncodedVideoSubscribers() => OnVideoSourceEncodedSample != null;
public void ExternalVideoSourceRawSample ( uint durationMilliseconds, int width, int height, byte[] sample, VideoPixelFormatsEnum pixelFormat ) { }
public void ExternalVideoSourceRawSampleFaster ( uint durationMilliseconds, RawImage rawImage ) { }
public bool IsVideoSourcePaused() => _isPaused;
public void SetFrameRate(int framesPerSecond) { if (framesPerSecond < 1 || framesPerSecond > 60) { Console.WriteLine( string.Format( @"Frames per second not in the allowed range of {0} to {1}, ignoring.", 1, 60 ), Array.Empty<object>() ); } else { _frameSpacing = 1000 / framesPerSecond; if (!_isStarted) return; _sendTestPatternTimer.Change( 0, _frameSpacing ); } }
public void SetMaxFrameRate(bool isMaxFrameRate) { if (_isMaxFrameRate == isMaxFrameRate) return; _isMaxFrameRate = isMaxFrameRate; if (!_isStarted) return; if (_isMaxFrameRate) { _sendTestPatternTimer.Change( -1, -1 ); GenerateMaxFrames(); } else _sendTestPatternTimer.Change( 0, _frameSpacing ); }
public Task PauseVideo() { _isPaused = true; _sendTestPatternTimer.Change( -1, -1 ); return Task.CompletedTask; }
public Task ResumeVideo() { _isPaused = false; _sendTestPatternTimer.Change( 0, _frameSpacing ); return Task.CompletedTask; }
public Task StartVideo() { if (!_isStarted) { _isStarted = true; if (_isMaxFrameRate) GenerateMaxFrames(); else _sendTestPatternTimer.Change( 0, _frameSpacing ); } return Task.CompletedTask; }
public Task CloseVideo() { if (_isClosed) return Task.CompletedTask; _isClosed = true; ManualResetEventSlim mre = new ManualResetEventSlim(); _sendTestPatternTimer?.Dispose(mre.WaitHandle); return Task.Run(() => mre.Wait(1000)); }
private void GenerateMaxFrames() { DateTime now = DateTime.Now; while (!_isClosed && _isMaxFrameRate) { _frameSpacing = Convert.ToInt32(DateTime.Now.Subtract(now).TotalMilliseconds); GenerateTestPattern(null); now = DateTime.Now; } }
private void GenerateTestPattern(object state) { lock (_sendTestPatternTimer) { if (_isClosed || OnVideoSourceRawSample == null && OnVideoSourceEncodedSample == null) return; ++_frameCount; StampI420Buffer( _myI420Buffer, SCREEN_WIDTH, SCREEN_HEIGHT, _frameCount ); if (OnVideoSourceRawSample != null) GenerateRawSample( SCREEN_WIDTH, SCREEN_HEIGHT, _myI420Buffer ); if (_videoEncoder != null && OnVideoSourceEncodedSample != null) { VideoFormat selectedFormat = _formatManager.SelectedFormat; if (!selectedFormat.IsEmpty()) { IVideoEncoder videoEncoder = _videoEncoder; UpdateBuffer(); byte[] testI420Buffer = _myI420Buffer; selectedFormat = _formatManager.SelectedFormat; int codec = (int)selectedFormat.Codec; byte[] sample = videoEncoder.EncodeVideo( SCREEN_WIDTH, SCREEN_HEIGHT, testI420Buffer, VideoPixelFormatsEnum.I420, (VideoCodecsEnum)codec ); if (sample != null) OnVideoSourceEncodedSample( 90000U / (_frameSpacing > 0 ? 1000U / (uint)_frameSpacing : 30U), sample ); } } if (_frameCount != int.MaxValue) return; _frameCount = 0; } }
private void GenerateRawSample ( int width, int height, byte[] i420Buffer ) { byte[] sample = PixelConverter.I420toBGR( i420Buffer, width, height, out int _ ); RawVideoSampleDelegate videoSourceRawSample = OnVideoSourceRawSample; if (videoSourceRawSample == null) return; videoSourceRawSample( (uint)_frameSpacing, width, height, sample, VideoPixelFormatsEnum.Bgr ); }
public static void StampI420Buffer ( byte[] i420Buffer, int width, int height, int frameNumber ) { int num1 = width - 20 - 10; int num2 = height - 20 - 10; for (int index1 = num2; index1 < num2 + 20; ++index1) { for (int index2 = num1; index2 < num1 + 20; ++index2) i420Buffer[index1 * width + index2] = (byte)(frameNumber % byte.MaxValue); } }
public void Dispose() { _isClosed = true; _sendTestPatternTimer?.Dispose(); _videoEncoder?.Dispose(); } } }
|