public SurfaceWithInfo WaitForNewFrame() { // Let's get a fresh one. _currentFrame?.Dispose(); _frameEvent.Reset(); var signaledEvent = _events[WaitHandle.WaitAny(_events)]; if (signaledEvent == _closedEvent) { Cleanup(); return(null); } var result = new SurfaceWithInfo(); result.SystemRelativeTime = _currentFrame.SystemRelativeTime; using (var multithreadLock = new MultithreadLock(_multithread)) using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(_currentFrame.Surface)) { _sharpDxD3dDevice.ImmediateContext.ClearRenderTargetView(_composeRenderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1)); var width = Math.Clamp(_currentFrame.ContentSize.Width, 0, _currentFrame.Surface.Description.Width); var height = Math.Clamp(_currentFrame.ContentSize.Height, 0, _currentFrame.Surface.Description.Height); var region = new SharpDX.Direct3D11.ResourceRegion(0, 0, 0, width, height, 1); _sharpDxD3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, _composeTexture, 0); var description = sourceTexture.Description; description.Usage = SharpDX.Direct3D11.ResourceUsage.Default; description.BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget; description.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None; description.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None; using (var copyTexture = new SharpDX.Direct3D11.Texture2D(_sharpDxD3dDevice, description)) { _sharpDxD3dDevice.ImmediateContext.CopyResource(_composeTexture, copyTexture); result.Surface = Direct3D11Helpers.CreateDirect3DSurfaceFromSharpDXTexture(copyTexture); } } return(result); }
private async Task SetupEncoding() { if (!GraphicsCaptureSession.IsSupported()) { // Show message to user that screen capture is unsupported return; } // Create the D3D device and SharpDX device if (_device == null) { _device = Direct3D11Helpers.CreateD3DDevice(); } if (_sharpDxD3dDevice == null) { _sharpDxD3dDevice = Direct3D11Helpers.CreateSharpDXDevice(_device); } try { // Let the user pick an item to capture var picker = new GraphicsCapturePicker(); _captureItem = await picker.PickSingleItemAsync(); if (_captureItem == null) { return; } // Initialize a blank texture and render target view for copying frames, using the same size as the capture item _composeTexture = Direct3D11Helpers.InitializeComposeTexture(_sharpDxD3dDevice, _captureItem.Size); _composeRenderTargetView = new SharpDX.Direct3D11.RenderTargetView(_sharpDxD3dDevice, _composeTexture); // This example encodes video using the item's actual size. var width = (uint)_captureItem.Size.Width; var height = (uint)_captureItem.Size.Height; // Make sure the dimensions are are even. Required by some encoders. width = (width % 2 == 0) ? width : width + 1; height = (height % 2 == 0) ? height : height + 1; var temp = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p); var bitrate = temp.Video.Bitrate; uint framerate = 30; _encodingProfile = new MediaEncodingProfile(); _encodingProfile.Container.Subtype = "MPEG4"; _encodingProfile.Video.Subtype = "H264"; _encodingProfile.Video.Width = width; _encodingProfile.Video.Height = height; _encodingProfile.Video.Bitrate = bitrate; _encodingProfile.Video.FrameRate.Numerator = framerate; _encodingProfile.Video.FrameRate.Denominator = 1; _encodingProfile.Video.PixelAspectRatio.Numerator = 1; _encodingProfile.Video.PixelAspectRatio.Denominator = 1; var videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, width, 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; using (var stream = new InMemoryRandomAccessStream()) await EncodeAsync(stream); } catch (Exception ex) { return; } }