Пример #1
0
        public static async Task <bool> Add(IEffect effect, bool unique)
        {
            if (unique && _effects.Any(e => e.Key == effect.Key))
            {
                return(false);
            }

            _effects.Add(effect);
            await effect.Init();

            return(true);
        }
Пример #2
0
        public static async Task <bool> Add(IEffect effect)
        {
            if (Exists(effect.Key))
            {
                return(false);
            }

            _effects.Add(effect);
            await effect.Init();

            return(true);
        }
        public async Task AddEffect(IEffect effect, params IFilter[] filters)
        {
            await effect.Init(_outputSize);

            var transformOptions = new ExecutionDataflowBlockOptions {
                BoundedCapacity = 20, MaxDegreeOfParallelism = 8
            };
            var filterBlocks = filters.Select(x => new TransformBlock <OutputFrame, OutputFrame>(x.ApplyFilter, transformOptions)).ToList();
            ITargetBlock <OutputFrame> firstBlock;

            if (filterBlocks.Any())
            {
                var linkOptions = new DataflowLinkOptions {
                    PropagateCompletion = true
                };

                for (int i = 0; i < filterBlocks.Count; i++)
                {
                    var thisBlock = (ISourceBlock <OutputFrame>)filterBlocks[i];
                    var nextBlock = (i + 1) >= filterBlocks.Count ? (ITargetBlock <OutputFrame>)_renderTransformBlock : filterBlocks[i + 1];
                    thisBlock.LinkTo(nextBlock, linkOptions);
                }
                firstBlock = filterBlocks[0];
            }
            else
            {
                firstBlock = _renderTransformBlock;
            }

            _ = Task.Run(async() =>
            {
                while (!_cts.IsCancellationRequested)
                {
                    var frame = await effect.GetPixels();
                    await firstBlock.SendAsync(frame);
                }
                firstBlock.Complete();
            });
        }