Ejemplo n.º 1
0
        public void SetGraphicItem(GraphicsCaptureItem item)
        {
            this.ResetState();
            this.item = item;

            if (this.item != null)
            {
                this.renderTargetDescription = new Texture2DDescription
                {
                    CpuAccessFlags    = CpuAccessFlags.None,
                    Width             = item.Size.Width,
                    Height            = item.Size.Height,
                    Usage             = ResourceUsage.Default,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    ArraySize         = 1,
                    BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    OptionFlags       = ResourceOptionFlags.Shared,
                    MipLevels         = 1,
                    SampleDescription = new SampleDescription(1, 0),
                };
                this.renderTarget = new Texture2D(d3dDevice, this.renderTargetDescription);

                framePool = Direct3D11CaptureFramePool.Create(device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, this.item.Size);
                session   = framePool.CreateCaptureSession(this.item);
                lastSize  = this.item.Size;

                framePool.FrameArrived += this.OnFrameArrived;
                session.StartCapture();
            }
        }
Ejemplo n.º 2
0
        public void StartCapture(IntPtr hWnd, Device device, Factory factory)
        {
            var capturePicker = new GraphicsCapturePicker();

            // ReSharper disable once PossibleInvalidCastException
            // ReSharper disable once SuspiciousTypeConversion.Global
            var initializer = (IInitializeWithWindow)(object)capturePicker;

            initializer.Initialize(hWnd);

            _captureItem = capturePicker.PickSingleItemAsync().AsTask().Result;
            if (_captureItem == null)
            {
                return;
            }

            _captureItem.Closed += CaptureItemOnClosed;

            var hr = NativeMethods.CreateDirect3D11DeviceFromDXGIDevice(device.NativePointer, out var pUnknown);

            if (hr != 0)
            {
                StopCapture();
                return;
            }

            var winrtDevice = (IDirect3DDevice)Marshal.GetObjectForIUnknown(pUnknown);

            Marshal.Release(pUnknown);

            _captureFramePool = Direct3D11CaptureFramePool.Create(winrtDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, _captureItem.Size);
            _captureSession   = _captureFramePool.CreateCaptureSession(_captureItem);
            _captureSession.StartCapture();
            IsCapturing = true;
        }
        private async Task StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();

            _item     = item;
            _lastSize = _item.Size;

            _canvasDevice = new CanvasDevice();

            _framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();

            await Task.Delay(500);

            var frame = _framePool.TryGetNextFrame();

            await ProcessFrame(frame);

            StopCapture();
        }
Ejemplo n.º 4
0
        public void StartCaptureInternal(GraphicsCaptureItem item)
        {
            StopCapture();
            _item      = item;
            _lastSize  = _item.Size;
            _swapChain = new CanvasSwapChain(_canvasDevice, _item.Size.Width, _item.Size.Height, 96);

            swapChain.SwapChain = _swapChain;

            _framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                60,                                        // Number of frames
                _item.Size);                               // Size of the buffers
            _session = _framePool.CreateCaptureSession(_item);
            _framePool.FrameArrived += (s, a) =>
            {
                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };
            _item.Closed += (s, a) =>
            {
                StopCapture();
            };
            _session.StartCapture();
        }
Ejemplo n.º 5
0
        public void UpdateResolution(SizeInt32 size)
        {
            if (size.Height <= 0 || size.Width <= 0)
            {
                return;
            }

            if (_framePool is not null && _session is not null)
            {
                // Unsubscribe from old framePool
                _framePool.FrameArrived -= OnFrameArrived;

                // Dispose old session and framePool
                _session.Dispose();
                _framePool.Dispose();
            }

            _canvasDevice = CanvasDevice.GetSharedDevice();

            // Create a frame pool with room for only 1 frame because we're getting a single frame, not a video.
            _framePool =
                Direct3D11CaptureFramePool.Create(_canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, size);

            _session = _framePool.CreateCaptureSession(GraphicsCaptureItem.CreateFromVisual(_capturedVisual));

            _framePool.FrameArrived += OnFrameArrived;

            _session.StartCapture();
        }
Ejemplo n.º 6
0
        private void CreateCaptureItemDependendStuff()
        {
            _framePool = Direct3D11CaptureFramePool.Create(_device, PixelFormat, 2, _item.Size);
            _framePool.FrameArrived += OnFrameArrived;
            _session = _framePool.CreateCaptureSession(_item);
            _session.IsCursorCaptureEnabled = !PresentationToNDIAddIn.Properties.Settings.Default.HideMouse;

            var description = new SwapChainDescription1
            {
                Width             = _item.Size.Width,
                Height            = _item.Size.Height,
                Format            = SharpDxFormat,
                Stereo            = false,
                SampleDescription = new SampleDescription {
                    Count = 1, Quality = 0
                },
                Usage       = Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = Scaling.Stretch,
                SwapEffect  = SwapEffect.FlipSequential,
                AlphaMode   = AlphaMode.Premultiplied,
                Flags       = SwapChainFlags.None
            };

            _swapChain = new SwapChain1(_factory, _d3dDevice, ref description);
            _session.StartCapture();
        }
Ejemplo n.º 7
0
 public ModernCaptureMonitorSession(IDirect3DDevice device, ModernCaptureMonitorDescription description) : base(description)
 {
     this.CaptureItem = WinRTCaptureHelper.CreateItemForMonitor(description.MonitorInfo.Hmon);
     this.FramePool   = Direct3D11CaptureFramePool.Create(device,
                                                          description.HdrMetadata.EnableHdrProcessing ? DirectXPixelFormat.R16G16B16A16Float : DirectXPixelFormat.B8G8R8A8UIntNormalized,
                                                          2, CaptureItem.Size);
     this.Session = FramePool.CreateCaptureSession(CaptureItem);
     this.Session.IsCursorCaptureEnabled = description.CaptureCursor;
 }
Ejemplo n.º 8
0
        public ScreenGrabberWindowsCapture(MonitorInfo screen) : base(screen)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                var device = WindowsCaptureHelper.CreateDirect3DDeviceFromSharpDXDevice(new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport));
                _d3dDevice = WindowsCaptureHelper.CreateSharpDXDevice(device);
                var item   = WindowsCaptureHelper.CreateItemForMonitor(Screen.HMon);

                var factory = new Factory2();

                var description = new SwapChainDescription1
                {
                    Width             = item.Size.Width,
                    Height            = item.Size.Height,
                    Format            = Format.B8G8R8A8_UNorm,
                    Stereo            = false,
                    SampleDescription = new SampleDescription
                    {
                        Count   = 1,
                        Quality = 0
                    },
                    Usage       = Usage.RenderTargetOutput,
                    BufferCount = 2,
                    Scaling     = Scaling.Stretch,
                    SwapEffect  = SwapEffect.FlipSequential,
                    AlphaMode   = AlphaMode.Premultiplied,
                    Flags       = SwapChainFlags.None
                };

                _swapChain = new SwapChain1(factory, _d3dDevice, ref description);
                _framePool = Direct3D11CaptureFramePool.Create(device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, item.Size);
                _session   = _framePool.CreateCaptureSession(item);
                _session.IsCursorCaptureEnabled = false;

                _swapChain.ResizeBuffers(2, item.Size.Width, item.Size.Height, Format.B8G8R8A8_UNorm, SwapChainFlags.None);

                _screenTexture = new Texture2D(_d3dDevice, new Texture2DDescription
                {
                    CpuAccessFlags    = CpuAccessFlags.Read,
                    BindFlags         = BindFlags.None,
                    Format            = Format.B8G8R8A8_UNorm,
                    Width             = item.Size.Width,
                    Height            = item.Size.Height,
                    OptionFlags       = ResourceOptionFlags.None,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Staging
                });

                _framePool.FrameArrived += OnFrameArrived;

                _session.StartCapture();
            });
        }
Ejemplo n.º 9
0
 public async Task Record(GraphicsCaptureItem _field)
 {
     canvasDevice = new CanvasDevice();
     field        = _field;
     framePool    = Direct3D11CaptureFramePool.Create(
         canvasDevice,            // D3D device
         DirectXPixelFormat.A8P8, // Pixel format
         2,                       // Number of frames
         field.Size);             // Size of the buffers
     session = framePool.CreateCaptureSession(field);
 }
Ejemplo n.º 10
0
        public CaptureEngine(IDirect3DDevice device, GraphicsCaptureItem item)
        {
            _device    = device;
            _d3dDevice = Direct3D11Helper.CreateSharpDXDevice(_device);

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

            _framePool.FrameArrived += OnFrameArrived;
        }
Ejemplo n.º 11
0
        //
        // Renders a the given <see cref="Visual"/> to a <see cref="CanvasBitmap"/>. If <paramref name="size"/> is not
        // specified, uses the size of <paramref name="visual"/>.
        //
        static async Task <CanvasBitmap> RenderVisualToBitmapAsync(Visual visual, SizeInt32?size = null)
        {
            // Get an object that enables capture from a visual.
            var graphicsItem = GraphicsCaptureItem.CreateFromVisual(visual);

            var canvasDevice = CanvasDevice.GetSharedDevice();

            var tcs = new TaskCompletionSource <CanvasBitmap>();

            // Create a frame pool with room for only 1 frame because we're getting a single frame, not a video.
            const int numberOfBuffers = 1;

            using (var framePool = Direct3D11CaptureFramePool.Create(
                       canvasDevice,
                       DirectXPixelFormat.B8G8R8A8UIntNormalized,
                       numberOfBuffers,
                       size ?? graphicsItem.Size))
            {
                void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
                {
                    using (var frame = sender.TryGetNextFrame())
                    {
                        tcs.SetResult(frame != null
                            ? CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, frame.Surface)
                            : null);
                    }
                }

                using (var session = framePool.CreateCaptureSession(graphicsItem))
                {
                    framePool.FrameArrived += OnFrameArrived;

                    // Start capturing. The FrameArrived event will occur shortly.
                    session.StartCapture();

                    // Wait for the frame to arrive.
                    var result = await tcs.Task;

                    // !!!!!!!! NOTE !!!!!!!!
                    // This thread is now running inside the OnFrameArrived callback method.

                    // Unsubscribe now that we have captured the frame.
                    framePool.FrameArrived -= OnFrameArrived;

                    // Yield to allow the OnFrameArrived callback to unwind so that it is safe to
                    // Dispose the session and framepool.
                    await Task.Yield();
                }
            }

            return(await tcs.Task);
        }
Ejemplo n.º 12
0
 private void InitializeCapture(SizeInt32 size)
 {
     _item.Closed += OnClosed;
     //_framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
     _framePool = Direct3D11CaptureFramePool.Create(
         _canvasDevice,
         DirectXPixelFormat.B8G8R8A8UIntNormalized,
         1,
         size);
     _framePool.FrameArrived += OnFrameArrived;
     _session = _framePool.CreateCaptureSession(_item);
     _session.StartCapture();
 }
Ejemplo n.º 13
0
        private async void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();

            var  scale  = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
            var  height = item.Size.Height - 32;
            bool result = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(new Size {
                Width = item.Size.Width / scale, Height = height / scale
            });

            if (!result)
            {
                bool result_full = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
            }

            _item     = item;
            _lastSize = _item.Size;

            _framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers

            _framePool.FrameArrived += (s, a) =>
            {
                // The FrameArrived event is raised for every frame on the thread
                // that created the Direct3D11CaptureFramePool. This means we
                // don't have to do a null-check here, as we know we're the only
                // one dequeueing frames in our application.

                // NOTE: Disposing the frame retires it and returns
                // the buffer to the pool.

                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();
            await SaveRecordingAsync("a.mp4", 5000);
        }
Ejemplo n.º 14
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            item      = i;
            device    = d;
            d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            var size = item.Size;

            if (size.Height == 0 || size.Width == 0)
            {
                size = new SizeInt32()
                {
                    Height = 1, Width = 1
                }
            }
            ;

            var dxgiFactory = new SharpDX.DXGI.Factory2();
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = size.Width,
                Height            = size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                size);
            session  = framePool.CreateCaptureSession(i);
            lastSize = size;

            framePool.FrameArrived += OnFrameArrived;
        }
Ejemplo n.º 15
0
        private void _startCaptureInternal(GraphicsCaptureItem item)
        {
            _captureVideoFilePath = System.IO.Path.Combine(_captureFolder.Path, "ouput.wmv");

            _mediaEncoder.OpenVideoWriter(_captureVideoFilePath, item.Size.Width, item.Size.Height);

            _framePool = Direct3D11CaptureFramePool.Create(CanvasDevice.GetSharedDevice(), DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, item.Size);
            _seesion   = _framePool.CreateCaptureSession(item);
            _seesion.StartCapture();
            _capturing = true;

            QueryPerformanceCounter(out long counter);
            _startTime = TimeSpan.FromTicks(counter);
            Task.Run(_onFrameArraved);
        }
Ejemplo n.º 16
0
        private async void CpatureScreen_Click(object sender, RoutedEventArgs e)
        {
            Windows.Graphics.Capture.GraphicsCapturePicker graphicsCapturePicker = new Windows.Graphics.Capture.GraphicsCapturePicker();
            Windows.Graphics.Capture.GraphicsCaptureItem   graphicsCaptureItem   = await graphicsCapturePicker.PickSingleItemAsync();

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

            _franePool = Direct3D11CaptureFramePool.Create(canvasDevice, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, graphicsCaptureItem.Size);

            _franePool.FrameArrived += async(s, args) =>
            {
                using (var frame = _franePool.TryGetNextFrame())
                {
                    var canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, frame.Surface);

                    //await CpatureToImageFile(canvasBitmap);
                    CreateClipInMemory(canvasDevice, canvasBitmap);
                }
            };

            graphicsCaptureItem.Closed += (s, o) =>
            {
                CaptureStop_Click(null, null);
            };

            _session = _franePool.CreateCaptureSession(graphicsCaptureItem);

            timeSpans = new List <TimeSpan>();
            count     = 0;

            _session.StartCapture();

            QueryPerformanceCounter(out long qpc);
            QueryPerformanceFrequency(out long frq);
            performanceFrequency = frq;
            var milliseconds = 1000f * qpc / performanceFrequency;

            _timeSpan = TimeSpan.FromMilliseconds(milliseconds);

            CaptureAudio();

            btnCaptureStop.Visibility  = Visibility.Visible;
            btnCaptureStart.Visibility = Visibility.Collapsed;
        }
Ejemplo n.º 17
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            item       = i;
            device     = d;
            d3dDevice  = Direct3D11Helper.CreateSharpDXDevice(device);
            wicFactory = new ImagingFactory();

            // 建立DirectX图形基础设施工厂(DirectX Graphics Infrastructure,DXGI)
            var dxgiFactory = new SharpDX.DXGI.Factory2();

            // 建立交换链
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = item.Size.Width,
                Height            = item.Size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false, // 立体声
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,   // 连续翻转
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,     // 预乘
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                i.Size);

            // 创建捕获会话
            session = framePool.CreateCaptureSession(i);
            // 记录最后帧尺寸
            lastSize = i.Size;

            // 帧已到达事件添加
            framePool.FrameArrived += OnFrameArrived;
        }
Ejemplo n.º 18
0
        private void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // 下面参数暂时不能修改
            Direct3D11CaptureFramePool framePool = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                                                           // 要在其中存储捕获的框架的缓冲区数量
                1,
                // 每个缓冲区大小
                item.Size); // Size of the buffers

            // 界面刷新的时候将会触发这个事件
            framePool.FrameArrived += async(s, a) =>
            {
                using (var frame = framePool.TryGetNextFrame())
                {
                    try
                    {
                        // 将获取到的 Direct3D11CaptureFrame 转 win2d 的
                        CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface(
                            _canvasDevice,
                            frame.Surface);

                        CanvasComposition.Resize(_surface, canvasBitmap.Size);

                        using (var session = CanvasComposition.CreateDrawingSession(_surface))
                        {
                            session.Clear(Colors.Transparent);
                            session.DrawImage(canvasBitmap);
                        }
                    }
                    catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult))
                    {
                        // 设备丢失
                        await new MessageDialog("捕获应用窗口失败").ShowAsync();
                    }
                }
            };

            captureSession = framePool.CreateCaptureSession(item);
            captureSession.StartCapture();

            // 作为字段防止内存回收
            _direct3D11CaptureFramePool = framePool;
            _graphicsCaptureSession     = captureSession;
        }
Ejemplo n.º 19
0
        private void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();

            _item     = item;
            _lastSize = _item.Size;

            _framePool = Direct3D11CaptureFramePool.Create(
                this.measureCanvas.Device,                 // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers

            _framePool.FrameArrived += (s, a) =>
            {
                // The FrameArrived event is raised for every frame on the thread
                // that created the Direct3D11CaptureFramePool. This means we
                // don't have to do a null-check here, as we know we're the only
                // one dequeueing frames in our application.

                // NOTE: Disposing the frame retires it and returns
                // the buffer to the pool.

                using (var frame = _framePool.TryGetNextFrame())
                {
                    if (frame == null)
                    {
                        return;
                    }
                    ProcessFrame(frame);
                }

                StopCapture();// NOTE: just need one frame
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.IsCursorCaptureEnabled = false;
            _session.StartCapture();
        }
Ejemplo n.º 20
0
        public ScreenCapture(GraphicsCaptureItem i)
        {
            item = i;

            device    = Direct3D11Helper.CreateDevice();
            d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            SizeInt32 correctedSize = item.Size;

            correctedSize.Width  = correctedSize.Width / 8 * 8;
            correctedSize.Height = correctedSize.Height / 8 * 8;

            var dxgiFactory = new Factory2();
            var description = new SwapChainDescription1()
            {
                Width             = correctedSize.Width,
                Height            = correctedSize.Height,
                Format            = Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = Scaling.Stretch,
                SwapEffect  = SwapEffect.FlipSequential,
                AlphaMode   = AlphaMode.Premultiplied,
                Flags       = SwapChainFlags.None
            };

            swapChain = new SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                correctedSize);
            session  = framePool.CreateCaptureSession(i);
            lastSize = correctedSize;

            framePool.FrameArrived += OnFrameArrived;
        }
Ejemplo n.º 21
0
        public void StartCaptureInternal(GraphicsCaptureItem item)
        {
            // Stop the previous capture if we had one.
            StopCapture();
            _item         = item;
            _lastSize     = _item.Size;
            _canvasDevice = new CanvasDevice();
            _framePool    = Direct3D11CaptureFramePool.Create(
                _canvasDevice,                             // D3D device
                DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format
                2,                                         // Number of frames
                _item.Size);                               // Size of the buffers
            renderTarget = new CanvasRenderTarget(_canvasDevice, _item.Size.Width, _item.Size.Height, 96);
            foreach (var clip in ClipImages.Values)
            {
                clip.Start(_canvasDevice);
            }
            _framePool.FrameArrived += (s, a) =>
            {
                // The FrameArrived event is raised for every frame on the thread
                // that created the Direct3D11CaptureFramePool. This means we
                // don't have to do a null-check here, as we know we're the only
                // one dequeueing frames in our application.

                // NOTE: Disposing the frame retires it and returns
                // the buffer to the pool.

                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();
        }
Ejemplo n.º 22
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            __item      = i;
            __device    = d;
            __d3dDevice = Direct3D11Helper.CreateSharpDXDevice(__device);

            var dxgiFactory = new SharpDX.DXGI.Factory2();
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = __item.Size.Width,
                Height            = __item.Size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            __swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, __d3dDevice, ref description);

            __framePool = Direct3D11CaptureFramePool.Create(
                __device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                i.Size);
            __session  = __framePool.CreateCaptureSession(i);
            __lastSize = i.Size;

            // Disable mouse cursor to be visible
            __session.IsCursorCaptureEnabled = false;

            __framePool.FrameArrived += OnFrameArrived;
        }
Ejemplo n.º 23
0
        public async Task EncodeAsync(IRandomAccessStream stream)
        {
            if (!_isRecording)
            {
                _isRecording = true;

                // Initialize our capture objects
                _framePool = Direct3D11CaptureFramePool.Create(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    1,
                    _captureItem.Size);
                _captureSession = _framePool.CreateCaptureSession(_captureItem);

                // Start the capture
                _captureSession.StartCapture();

                try
                {
                    var encodingProfile = new MediaEncodingProfile();
                    encodingProfile.Container.Subtype                  = "MPEG4";
                    encodingProfile.Video.Subtype                      = "H264";
                    encodingProfile.Video.Width                        = (uint)_captureItem.Size.Width;
                    encodingProfile.Video.Height                       = (uint)_captureItem.Size.Height;
                    encodingProfile.Video.Bitrate                      = _videoDescriptor.EncodingProperties.Bitrate;
                    encodingProfile.Video.FrameRate.Numerator          = c_frameRateN;
                    encodingProfile.Video.FrameRate.Denominator        = 1;
                    encodingProfile.Video.PixelAspectRatio.Numerator   = 1;
                    encodingProfile.Video.PixelAspectRatio.Denominator = 1;
                    var transcode = await _transcoder.PrepareMediaStreamSourceTranscodeAsync(_mediaStreamSource, stream, encodingProfile);

                    await transcode.TranscodeAsync();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }
        }
Ejemplo n.º 24
0
        public async Task StartCaptureAsyncInternal()
        {
            Logger.Debug("Screen", "StartCaptureAsyncInternal");

            var picker = new GraphicsCapturePicker();
            GraphicsCaptureItem item = await picker.PickSingleItemAsync();

            if (item == null)
            {
                return;
            }

            StopCaptureInternal();

            this.item     = item;
            this.lastSize = item.Size;

            screenCaptureTask = CreateScreenCaptureTask();
            screenCaptureTask.Start();

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

            framePool = Direct3D11CaptureFramePool.Create(
                canvasDevice,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                item.Size);

            framePool.FrameArrived += FramePool_FrameArrived;
            item.Closed            += (s, a) =>
            {
                StopCaptureInternal();
            };
            session = framePool.CreateCaptureSession(item);
            session.StartCapture();
        }
Ejemplo n.º 25
0
        public Capture(CanvasDevice device, GraphicsCaptureItem item)
        {
            _item   = item;
            _device = device;

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

            _dispatcherQueueController = DispatcherQueueController.CreateOnDedicatedThread();
            _dispatcherQueue           = _dispatcherQueueController.DispatcherQueue;

            // We don't want to return from the constructor untill the frame pool and
            // the capture session are both initialized. We could do this on the UI thread,
            // but you really shouldn't. This will update as fast as the screen refresh rate,
            // so it would cause performance issues on the UI thread.
            var wait    = new AutoResetEvent(false);
            var success = _dispatcherQueue.TryEnqueue(() =>
            {
                _framePool = Direct3D11CaptureFramePool.Create(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    item.Size);
                _session  = _framePool.CreateCaptureSession(item);
                _lastSize = item.Size;

                _framePool.FrameArrived += OnFrameArrived;

                wait.Set();
            });

            if (!success)
            {
                throw new Exception("Could not enqueue work!");
            }

            wait.WaitOne();
        }
Ejemplo n.º 26
0
        public void StartCaptureInternal(GraphicsCaptureItem item)
        {
            StopCapture();

            _item     = item;
            _lastSize = _item.Size;

            _framePool = Direct3D11CaptureFramePool.Create(_canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, _item.Size);
            _framePool.FrameArrived += (s, a) =>
            {
                using (var frame = _framePool.TryGetNextFrame())
                {
                    ProcessFrame(frame);
                }
            };

            _item.Closed += (s, a) =>
            {
                StopCapture();
            };

            _session = _framePool.CreateCaptureSession(_item);
            _session.StartCapture();
        }