private static void SendTestPattern(object state) { lock (_sendTestPatternTimer) { unsafe { if (OnTestPatternSampleReady != null) { var stampedTestPattern = _testPattern.Clone() as System.Drawing.Image; AddTimeStampAndLocation(stampedTestPattern, DateTime.UtcNow.ToString("dd MMM yyyy HH:mm:ss:fff"), "Test Pattern"); var sampleBuffer = BitmapToRGBA(stampedTestPattern as System.Drawing.Bitmap, _testPattern.Width, _testPattern.Height); var i420Frame = _videoFrameConverter.Convert(sampleBuffer); _presentationTimestamp += VIDEO_TIMESTAMP_SPACING; //i420Frame.key_frame = _forceKeyFrame ? 1 : 0; i420Frame.pts = _presentationTimestamp; byte[] encodedBuffer = _ffmpegEncoder.Encode(AVCodecID.AV_CODEC_ID_VP8, i420Frame, FRAMES_PER_SECOND); if (encodedBuffer != null) { OnTestPatternSampleReady?.Invoke(SDPMediaTypesEnum.video, VIDEO_TIMESTAMP_SPACING, encodedBuffer); } stampedTestPattern.Dispose(); } } } }
private static void SendTestPattern(object state) { lock (_sendTestPatternTimer) { unsafe { if (OnTestPatternSampleReady != null) { var stampedTestPattern = _testPattern.Clone() as System.Drawing.Image; AddTimeStampAndLocation(stampedTestPattern, DateTime.UtcNow.ToString("dd MMM yyyy HH:mm:ss:fff"), "Test Pattern"); var sampleBuffer = PixelConverter.BitmapToRGBA(stampedTestPattern as System.Drawing.Bitmap, _testPattern.Width, _testPattern.Height); byte[] i420Buffer = PixelConverter.RGBAtoYUV420Planar(sampleBuffer, _testPattern.Width, _testPattern.Height); var encodedBuffer = _vp8Encoder.Encode(i420Buffer, false); _presentationTimestamp += VIDEO_TIMESTAMP_SPACING; if (encodedBuffer != null) { OnTestPatternSampleReady?.Invoke(SDPMediaTypesEnum.video, VIDEO_TIMESTAMP_SPACING, encodedBuffer); } stampedTestPattern.Dispose(); } } } }
private static void SendTestPattern(object state) { try { lock (_sendTestPatternTimer) { unsafe { byte[] sampleBuffer = null; byte[] encodedBuffer = null; if (OnTestPatternSampleReady != null) { var stampedTestPattern = _testPattern.Clone() as System.Drawing.Image; AddTimeStampAndLocation(stampedTestPattern, DateTime.UtcNow.ToString("dd MMM yyyy HH:mm:ss:fff"), "Test Pattern"); sampleBuffer = BitmapToRGB24(stampedTestPattern as System.Drawing.Bitmap); fixed(byte *p = sampleBuffer) { byte[] convertedFrame = null; _colorConverter.ConvertRGBtoYUV(p, VideoSubTypesEnum.BGR24, _testPattern.Width, _testPattern.Height, _stride, VideoSubTypesEnum.I420, ref convertedFrame); fixed(byte *q = convertedFrame) { int encodeResult = _vpxEncoder.Encode(q, convertedFrame.Length, 1, ref encodedBuffer); if (encodeResult != 0) { logger.LogWarning("VPX encode of video sample failed."); } } } stampedTestPattern.Dispose(); stampedTestPattern = null; OnTestPatternSampleReady?.Invoke(SDPMediaTypesEnum.video, VP8_TIMESTAMP_SPACING, encodedBuffer); encodedBuffer = null; } else { _sendTestPatternTimer?.Dispose(); _sendTestPatternTimer = null; } } } } catch (Exception excp) { logger.LogError("Exception SendTestPattern. " + excp); } }
private async void SampleTestPattern() { try { logger.LogDebug("Starting test pattern sampling thread."); _isTestPatternSampling = true; Bitmap testPattern = new Bitmap(_testPatternImagePath); // Get the stride. Rectangle rect = new Rectangle(0, 0, testPattern.Width, testPattern.Height); System.Drawing.Imaging.BitmapData bmpData = testPattern.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, testPattern.PixelFormat); // Get the address of the first line. int stride = bmpData.Stride; testPattern.UnlockBits(bmpData); // Initialise the video codec and color converter. SIPSorceryMedia.VpxEncoder vpxEncoder = new VpxEncoder(); vpxEncoder.InitEncoder((uint)testPattern.Width, (uint)testPattern.Height, (uint)stride); SIPSorceryMedia.ImageConvert colorConverter = new ImageConvert(); byte[] sampleBuffer = null; byte[] encodedBuffer = null; int sampleCount = 0; uint rtpTimestamp = 0; while (!_exit) { if (OnTestPatternSampleReady == null) { logger.LogDebug("No active clients, test pattern sampling paused."); break; } else { var stampedTestPattern = testPattern.Clone() as System.Drawing.Image; AddTimeStampAndLocation(stampedTestPattern, DateTime.UtcNow.ToString("dd MMM yyyy HH:mm:ss:fff"), "Test Pattern"); sampleBuffer = BitmapToRGB24(stampedTestPattern as System.Drawing.Bitmap); unsafe { fixed(byte *p = sampleBuffer) { byte[] convertedFrame = null; colorConverter.ConvertRGBtoYUV(p, VideoSubTypesEnum.BGR24, testPattern.Width, testPattern.Height, stride, VideoSubTypesEnum.I420, ref convertedFrame); fixed(byte *q = convertedFrame) { int encodeResult = vpxEncoder.Encode(q, convertedFrame.Length, 1, ref encodedBuffer); if (encodeResult != 0) { logger.LogWarning("VPX encode of video sample failed."); continue; } } } stampedTestPattern.Dispose(); OnTestPatternSampleReady?.Invoke(SDPMediaTypesEnum.video, rtpTimestamp, encodedBuffer); sampleCount++; rtpTimestamp += VP8_TIMESTAMP_SPACING; } await Task.Delay(30); } } testPattern.Dispose(); vpxEncoder.Dispose(); } catch (Exception excp) { logger.LogError("Exception SampleTestPattern. " + excp); } finally { logger.LogDebug("test pattern sampling thread stopped."); _isTestPatternSampling = false; } }