private void WriteSamples(Dictionary <int, Sample> samples) { ++frameIndex; long frameDuration; MediaFactory.FrameRateToAverageTimePerFrame(framerate, 1, out frameDuration); try { foreach (var item in samples) { var streamIndex = item.Key; var sample = item.Value; sample.SampleTime = frameDuration * frameIndex; sample.SampleDuration = frameDuration; sinkWriter.WriteSample(streamIndex, sample); } } catch (SharpDXException e) { Debug.WriteLine(e.Message); } }
/// <summary> /// Draws the given frame to the video. /// </summary> /// <param name="device">The device on which the given framebuffer is created.</param> /// <param name="uploadedTexture">The texture which should be added to the video.</param> protected override void DrawFrameInternal(EngineDevice device, MemoryMappedTexture32bpp uploadedTexture) { // Cancel here if the given texture has an invalid size if (m_videoPixelSize != new Size2(uploadedTexture.Width, uploadedTexture.Height)) { return; } m_frameIndex++; MF.MediaBuffer mediaBuffer = MF.MediaFactory.CreateMemoryBuffer((int)uploadedTexture.SizeInBytes); try { // Write all contents to the MediaBuffer for media foundation int cbMaxLength = 0; int cbCurrentLength = 0; IntPtr mediaBufferPointer = mediaBuffer.Lock(out cbMaxLength, out cbCurrentLength); try { if (this.FlipY) { unsafe { int stride = m_videoPixelSize.Width; int *mediaBufferPointerNative = (int *)mediaBufferPointer.ToPointer(); int *targetBufferPointerNative = (int *)uploadedTexture.Pointer.ToPointer(); for (int loopY = 0; loopY < m_videoPixelSize.Height; loopY++) { for (int loopX = 0; loopX < m_videoPixelSize.Width; loopX++) { int actIndexTarget = loopX + (loopY * m_videoPixelSize.Width); int actIndexSource = loopX + ((m_videoPixelSize.Height - (1 + loopY)) * m_videoPixelSize.Width); mediaBufferPointerNative[actIndexTarget] = targetBufferPointerNative[actIndexSource]; } } } } else { unsafe { int stride = m_videoPixelSize.Width; int *mediaBufferPointerNative = (int *)mediaBufferPointer.ToPointer(); int *targetBufferPointerNative = (int *)uploadedTexture.Pointer.ToPointer(); for (int loopY = 0; loopY < m_videoPixelSize.Height; loopY++) { for (int loopX = 0; loopX < m_videoPixelSize.Width; loopX++) { int actIndex = loopX + (loopY * m_videoPixelSize.Width); mediaBufferPointerNative[actIndex] = targetBufferPointerNative[actIndex]; } } } } } finally { mediaBuffer.Unlock(); } mediaBuffer.CurrentLength = (int)uploadedTexture.SizeInBytes; // Create the sample (includes image and timing information) MF.Sample sample = MF.MediaFactory.CreateSample(); try { sample.AddBuffer(mediaBuffer); long frameDuration = 10 * 1000 * 1000 / m_framerate; sample.SampleTime = frameDuration * m_frameIndex; sample.SampleDuration = frameDuration; m_sinkWriter.WriteSample(m_streamIndex, sample); } finally { GraphicsHelper.SafeDispose(ref sample); } } finally { GraphicsHelper.SafeDispose(ref mediaBuffer); } }