Example #1
0
 private void InitializeCapture(SizeInt32 size)
 {
     _item.Closed += OnClosed;
     _framePool    = Direct3D11CaptureFramePool.CreateFreeThreaded(
         _device,
         DirectXPixelFormat.B8G8R8A8UIntNormalized,
         1,
         size);
     _framePool.FrameArrived += OnFrameArrived;
     _session = _framePool.CreateCaptureSession(_item);
     _session.StartCapture();
 }
 private void StartCapture(GraphicsCaptureItem item)
 {
     _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
         _device,
         DirectXPixelFormat.B8G8R8A8UIntNormalized,
         2,
         item.Size);
     _session = _framePool.CreateCaptureSession(item);
     _framePool.FrameArrived += OnFrameArrived;
     _session.StartCapture();
     _item         = item;
     _item.Closed += OnCaptureItemClosed;
 }
Example #3
0
 private void InitializeCapture()
 {
     this.initializationThread = Thread.CurrentThread.ManagedThreadId;
     this.CurrentItem.Closed  += this.OnClosed;
     this.framePool            = Direct3D11CaptureFramePool.CreateFreeThreaded(
         this.device,
         DirectXPixelFormat.B8G8R8A8UIntNormalized,
         1,
         this.CurrentItem.Size);
     this.framePool.FrameArrived += this.OnFrameArrived;
     this.session = this.framePool.CreateCaptureSession(this.CurrentItem);
     this.session.StartCapture();
 }
        public MediaSampleGenerator(IDirect3DDevice device, GraphicsCaptureItem item)
        {
            m_item      = item;
            m_framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                device,
                pixelFormat: DirectXPixelFormat.B8G8R8A8UIntNormalized,
                numberOfBuffers: 1,
                item.Size);
            m_session = m_framePool.CreateCaptureSession(m_item);
            m_session.IsCursorCaptureEnabled = true;

            m_item.Closed            += OnClosed;
            m_framePool.FrameArrived += OnFrameArrived;
        }
Example #5
0
 public void Start(GraphicsCaptureItem captureItem, StorageFolder outputFolder)
 {
     _captureItem         = captureItem;
     _captureItem.Closed += CaptureItemOnClosed;
     _framePool           = Direct3D11CaptureFramePool.CreateFreeThreaded(
         _device,
         DirectXPixelFormat.B8G8R8A8UIntNormalized,
         1,
         _captureItem.Size);
     _framePool.FrameArrived += OnFrameArrived;
     _outputFolder            = outputFolder;
     _session = _framePool.CreateCaptureSession(_captureItem);
     _session.StartCapture();
 }
        public SimpleCapture(CanvasDevice device, GraphicsCaptureItem item, SizeInt32 size)
        {
            _item   = item;
            _device = device;

            // TODO: Dpi?
            _swapChain = new CanvasSwapChain(_device, size.Width, size.Height, 96);

            _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                _device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                size);
            _session = _framePool.CreateCaptureSession(item);

            _framePool.FrameArrived += OnFrameArrived;
        }
Example #7
0
        private async void InitializeCapture(SizeInt32 size, LoopbackAudioCapture loopbackAudioCapture)
        {
            _item.Closed += OnClosed;
            _framePool    = Direct3D11CaptureFramePool.CreateFreeThreaded(
                _device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                1,
                size);
            _framePool.FrameArrived += OnFrameArrived;
            _session = _framePool.CreateCaptureSession(_item);

            if (loopbackAudioCapture != null)
            {
                await loopbackAudioCapture.Start();
            }

            _session.StartCapture();
        }
        public void StartCapture()
        {
            _multithread = _sharpDxD3dDevice.QueryInterface <SharpDX.Direct3D11.Multithread>();
            _multithread.SetMultithreadProtected(true);
            _frameEvent  = new ManualResetEvent(false);
            _closedEvent = new ManualResetEvent(false);
            _events      = new[] { _closedEvent, _frameEvent };

            _captureItem.Closed += OnClosed;
            _framePool           = Direct3D11CaptureFramePool.CreateFreeThreaded(
                _device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                1,
                _captureItem.Size);
            _framePool.FrameArrived += OnFrameArrived;
            _session = _framePool.CreateCaptureSession(_captureItem);
            _session.StartCapture();
        }
        public async Task <int> InitializeCam()
        {
            if (gcitem == null)
            {
                return(1);
            }

            if (canvasDevice == null)
            {
                canvasDevice = new CanvasDevice();
            }


            if (framePool == null)
            {
                /*
                 * //only 2 frames ... depending on the number of buffers
                 * framePool = Direct3D11CaptureFramePool.Create(
                 * canvasDevice, // D3D device
                 * DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                 * 2, // Number of frames
                 * gcitem.Size); // Size of the buffers
                 */

                framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                    canvasDevice,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    1,
                    gcitem.Size);
            }

            initialRecordTime  = DateTime.Now;
            previousRecordTime = DateTime.Now;

            currentFrame = null;

            unpackList             = new List <UnpackItem>();
            _currentVideoStreamPos = 0;

            tempFile = null;

            return(0);
        }
        public ScreenCaptureEngine(ScreenCaptureRequest request, IDirectXContext dx, Action <SizeInt32> onSizeChanged)
        {
            _dx            = dx;
            _onSizeChanged = onSizeChanged;
            _initSize      = request.InitialSize;

            ScreenCaptureManager.Instance.Logger.Info($"Opening capture {request.Id} ({_initSize.Width}x{_initSize.Height})");

            ScreenCaptureManager.Instance.MainThreadExecutor.Execute(() =>
            {
                _device    = dx == null ? Direct3D11Helper.CreateDevice() : Direct3D11Helper.CreateDirect3DDeviceFromSharpDXDevice(dx.Device);
                _d3dDevice = Direct3D11Helper.CreateSharpDXDevice(_device);
                try
                {
                    _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                        _device,
                        DirectXPixelFormat.B8G8R8A8UIntNormalized,
                        2,
                        request.InitialSize);

                    _framePool.FrameArrived += OnFrameArrived;
                    _session = _framePool.CreateCaptureSession(request.Item);
                    if (ApiInformation.IsApiContractPresent(typeof(Windows.Foundation.UniversalApiContract).FullName, 10))
                    {
                        SetCursor(_session, request.Cursor);
                    }

                    _session.StartCapture();
                }
                catch (Exception e)
                {
                    ScreenCaptureManager.Instance.Logger.Error(e, $"Failed to create capture");
                }
            }, true);

            _screenTexture = CreateTexture(_initSize);

            ScreenCaptureManager.Instance.Logger.Info($"Opened capture {request.Id}");
        }
Example #11
0
        private void InitializeCapture(SizeInt32 size)
        {
            //var dxgiDevice = _d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
            //var adapter = dxgiDevice.GetParent<SharpDX.DXGI.Adapter>();
            //var factory = adapter.GetParent<SharpDX.DXGI.Factory2>();

            //var description = new SharpDX.DXGI.SwapChainDescription1 {
            //    Width = 1,
            //    Height =1,
            //    Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            //    Usage = SharpDX.DXGI.Usage.RenderTargetOutput,
            //    SampleDescription = new SharpDX.DXGI.SampleDescription() {
            //        Count = 1,
            //        Quality = 0
            //    },
            //    BufferCount = 2,
            //    Scaling = SharpDX.DXGI.Scaling.Stretch,
            //    SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential,
            //    AlphaMode = SharpDX.DXGI.AlphaMode.Premultiplied
            //};
            //var swapChain = new SharpDX.DXGI.SwapChain1(factory, dxgiDevice, ref description);

            //using (var backBuffer = swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0))
            //using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
            //{
            //    _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 0));
            //}
            //_swapChain = swapChain;

            _captureItem.Closed += OnClosed;
            _framePool           = Direct3D11CaptureFramePool.CreateFreeThreaded(
                _device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                size);
            _framePool.FrameArrived += OnFrameArrived;
            _session = _framePool.CreateCaptureSession(_captureItem);
            _session.StartCapture();
        }
Example #12
0
        public CapturePreview(IDirect3DDevice device, GraphicsCaptureItem item)
        {
            _item      = item;
            _device    = device;
            _d3dDevice = Direct3D11Helpers.CreateSharpDXDevice(device);

            var dxgiDevice = _d3dDevice.QueryInterface <SharpDX.DXGI.Device>();
            var adapter    = dxgiDevice.GetParent <SharpDX.DXGI.Adapter>();
            var factory    = adapter.GetParent <SharpDX.DXGI.Factory2>();

            var description = new SharpDX.DXGI.SwapChainDescription1
            {
                Width             = item.Size.Width,
                Height            = item.Size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied
            };

            _swapChain = new SharpDX.DXGI.SwapChain1(factory, dxgiDevice, ref description);

            _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                _device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                item.Size);
            _session  = _framePool.CreateCaptureSession(item);
            _lastSize = item.Size;

            _framePool.FrameArrived += OnFrameArrived;
        }
Example #13
0
        private async void MainToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            // Select what we want to capture
            var picker = new GraphicsCapturePicker();
            var item   = await picker.PickSingleItemAsync();

            if (item != null)
            {
                // Get a temporary file to save our gif to
                var file = await GetTempFileAsync();

                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    // Get the various d3d objects we'll need
                    var d3dDevice = Direct3D11Helpers.CreateSharpDXDevice(_device);

                    // Create our encoder
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, stream);

                    // Write the application block
                    // http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
                    var containerProperties = encoder.BitmapContainerProperties;
                    await containerProperties.SetPropertiesAsync(new[]
                    {
                        new KeyValuePair <string, BitmapTypedValue>("/appext/application", new BitmapTypedValue(PropertyValue.CreateUInt8Array(Encoding.ASCII.GetBytes("NETSCAPE2.0")), PropertyType.UInt8Array)),
                        // The first value is the size of the block, which is the fixed value 3.
                        // The second value is the looping extension, which is the fixed value 1.
                        // The third and fourth values comprise an unsigned 2-byte integer (little endian).
                        //     The value of 0 means to loop infinitely.
                        // The final value is the block terminator, which is the fixed value 0.
                        new KeyValuePair <string, BitmapTypedValue>("/appext/data", new BitmapTypedValue(PropertyValue.CreateUInt8Array(new byte[] { 3, 1, 0, 0, 0 }), PropertyType.UInt8Array)),
                    });

                    // Setup Windows.Graphics.Capture
                    var itemSize  = item.Size;
                    var framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                        _device,
                        DirectXPixelFormat.B8G8R8A8UIntNormalized,
                        1,
                        itemSize);
                    var session = framePool.CreateCaptureSession(item);

                    // We need a blank texture (background) and a texture that will hold the frame we'll be encoding
                    var description = new SharpDX.Direct3D11.Texture2DDescription
                    {
                        Width             = itemSize.Width,
                        Height            = itemSize.Height,
                        MipLevels         = 1,
                        ArraySize         = 1,
                        Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SampleDescription = new SharpDX.DXGI.SampleDescription()
                        {
                            Count   = 1,
                            Quality = 0
                        },
                        Usage          = SharpDX.Direct3D11.ResourceUsage.Default,
                        BindFlags      = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget,
                        CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                        OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
                    };
                    var gifTexture       = new SharpDX.Direct3D11.Texture2D(d3dDevice, description);
                    var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(d3dDevice, gifTexture);

                    // Encode frames as they arrive. Because we created our frame pool using
                    // Direct3D11CaptureFramePool::CreateFreeThreaded, this lambda will fire on a different thread
                    // than our current one. If you'd like the callback to fire on your thread, create the frame pool
                    // using Direct3D11CaptureFramePool::Create and make sure your thread has a DispatcherQueue and you
                    // are pumping messages.
                    TimeSpan lastTimeStamp = TimeSpan.MinValue;
                    var      frameCount    = 0;
                    framePool.FrameArrived += async(s, a) =>
                    {
                        using (var frame = s.TryGetNextFrame())
                        {
                            var contentSize = frame.ContentSize;
                            var timeStamp   = frame.SystemRelativeTime;
                            using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(frame.Surface))
                            {
                                var width  = Math.Clamp(contentSize.Width, 0, itemSize.Width);
                                var height = Math.Clamp(contentSize.Height, 0, itemSize.Height);

                                var region = new SharpDX.Direct3D11.ResourceRegion(0, 0, 0, width, height, 1);

                                d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
                                d3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, gifTexture, 0);
                            }

                            if (lastTimeStamp == TimeSpan.MinValue)
                            {
                                lastTimeStamp = timeStamp;
                            }
                            var timeStampDelta = timeStamp - lastTimeStamp;
                            lastTimeStamp = timeStamp;
                            var milliseconds = timeStampDelta.TotalMilliseconds;
                            // Use 10ms units
                            var frameDelay = milliseconds / 10;

                            if (frameCount > 0)
                            {
                                await encoder.GoToNextFrameAsync();
                            }

                            // Write our frame delay
                            await encoder.BitmapProperties.SetPropertiesAsync(new[]
                            {
                                new KeyValuePair <string, BitmapTypedValue>("/grctlext/Delay", new BitmapTypedValue(PropertyValue.CreateUInt16((ushort)frameDelay), PropertyType.UInt16)),
                            });

                            // Write the frame to our image
                            var gifSurface = Direct3D11Helpers.CreateDirect3DSurfaceFromSharpDXTexture(gifTexture);
                            var copy       = await SoftwareBitmap.CreateCopyFromSurfaceAsync(gifSurface);

                            encoder.SetSoftwareBitmap(copy);
                            frameCount++;
                        }
                    };

                    session.StartCapture();

                    await _semaphore.WaitAsync();

                    session.Dispose();
                    framePool.Dispose();
                    await Task.Delay(1000);

                    await encoder.FlushAsync();

                    var newFile = await PickGifAsync();

                    if (newFile == null)
                    {
                        await file.DeleteAsync();

                        return;
                    }
                    await file.MoveAndReplaceAsync(newFile);

                    await Launcher.LaunchFileAsync(newFile);
                }
            }
        }
        private async void ScreenshotButton_Click(object sender, RoutedEventArgs e)
        {
            var filePicker = new FileSavePicker();

            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.SuggestedFileName      = "screenshot";
            filePicker.DefaultFileExtension   = ".png";
            filePicker.FileTypeChoices.Add("PNG Image", new string[] { ".png" });
            var file = await filePicker.PickSaveFileAsync();

            if (file != null)
            {
                var capturePicker = new GraphicsCapturePicker();
                var item          = await capturePicker.PickSingleItemAsync();

                if (item != null)
                {
                    var framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                        _device,
                        DirectXPixelFormat.B8G8R8A8UIntNormalized,
                        1,
                        item.Size);
                    var session = framePool.CreateCaptureSession(item);

                    var completionSource = new TaskCompletionSource <Direct3D11Texture2D>();
                    framePool.FrameArrived += (s, a) =>
                    {
                        using (var frame = s.TryGetNextFrame())
                        {
                            var frameTexture = Direct3D11Texture2D.CreateFromDirect3DSurface(frame.Surface);
                            var description  = frameTexture.Description2D;
                            description.Usage          = Direct3DUsage.Staging;
                            description.BindFlags      = 0;
                            description.CpuAccessFlags = Direct3D11CpuAccessFlag.AccessRead;
                            description.MiscFlags      = 0;
                            var copyTexture = _device.CreateTexture2D(description);

                            _deviceContext.CopyResource(copyTexture, frameTexture);

                            session.Dispose();
                            framePool.Dispose();

                            completionSource.SetResult(copyTexture);
                        }
                    };

                    session.StartCapture();
                    var texture = await completionSource.Task;
                    var bits    = texture.GetBytes();

                    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                        encoder.SetPixelData(
                            BitmapPixelFormat.Bgra8,
                            BitmapAlphaMode.Premultiplied,
                            (uint)item.Size.Width,
                            (uint)item.Size.Height,
                            1.0,
                            1.0,
                            bits);
                        await encoder.FlushAsync();
                    }

                    await Launcher.LaunchFileAsync(file);
                }
            }
        }
Example #15
0
 private void InitializeFramePool()
 {
     framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(device, DirectXPixelFormat.B8G8R8A8UIntNormalized,
                                                               1, item.Size);
     framePool.FrameArrived += OnFrameArrived;
 }
Example #16
0
        static void Main(string[] args)
        {
            // Get a capture item that represents the primary monitor
            var monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
            var item    = CaptureHelper.CreateItemForMonitor(monitor);
            var size    = item.Size;

            // Setup D3D
            var device     = Direct3D11Helper.CreateDevice();
            var d3dDevice  = Direct3D11Helper.CreateSharpDXDevice(device);
            var d3dContext = d3dDevice.ImmediateContext;

            // Create our staging texture
            var description = new SharpDX.Direct3D11.Texture2DDescription
            {
                Width             = size.Width,
                Height            = size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = SharpDX.Direct3D11.ResourceUsage.Staging,
                BindFlags      = SharpDX.Direct3D11.BindFlags.None,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
            };
            var stagingTexture = new SharpDX.Direct3D11.Texture2D(d3dDevice, description);

            // Setup capture
            var framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                1,
                size);
            var session = framePool.CreateCaptureSession(item);

            var imageNum = 0;
            var endEvent = new ManualResetEvent(false);

            framePool.FrameArrived += (sender, a) =>
            {
                using (var frame = sender.TryGetNextFrame())
                    using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                    {
                        // Copy to our staging texture
                        d3dContext.CopyResource(bitmap, stagingTexture);

                        // Map our texture and get the bits
                        var mapped       = d3dContext.MapSubresource(stagingTexture, 0, SharpDX.Direct3D11.MapMode.Read, SharpDX.Direct3D11.MapFlags.None);
                        var source       = mapped.DataPointer;
                        var sourceStride = mapped.RowPitch;

                        // Allocate some memory to hold our copy
                        var bytes = new byte[size.Width * size.Height * 4]; // 4 bytes per pixel
                        unsafe
                        {
                            fixed(byte *bytesPointer = bytes)
                            {
                                var dest       = (IntPtr)bytesPointer;
                                var destStride = size.Width * 4;

                                for (int i = 0; i < size.Height; i++)
                                {
                                    SharpDX.Utilities.CopyMemory(dest, source, destStride);

                                    source = IntPtr.Add(source, sourceStride);
                                    dest   = IntPtr.Add(dest, destStride);
                                }
                            }
                        }

                        // Don't forget to unmap when you're done!
                        d3dContext.UnmapSubresource(stagingTexture, 0);

                        // Encode it
                        // NOTE: Waiting here will stall the capture
                        EncodeBytesAsync($"image{imageNum}.png", size.Width, size.Height, bytes).Wait();

                        imageNum++;
                        if (imageNum > 10)
                        {
                            framePool.Dispose();
                            session.Dispose();
                            endEvent.Set();
                        }
                    }
            };

            // Start the capture and wait
            session.StartCapture();
            endEvent.WaitOne();
        }