Exemplo n.º 1
0
 /// <summary>
 /// Stop capture on one of the camera's output ports.
 /// </summary>
 /// <param name="port">An output port of the camera component.</param>
 public void StopCapture(OutputPortBase port)
 {
     if (port == this.Camera.StillPort || port == this.Camera.VideoPort)
     {
         port.SetImageCapture(false);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Remove a callback handler for a given port.
 /// </summary>
 /// <param name="port">The port we are removing the callback handler on.</param>
 public static void RemoveCallback(OutputPortBase port)
 {
     if (WorkingHandlers.ContainsKey(port))
     {
         WorkingHandlers.Remove(port);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Finds and returns a <see cref="IOutputCallbackHandler"/> for a given port. If no handler is registered, a
        /// <see cref="DefaultOutputCallbackHandler"/> will be returned.
        /// </summary>
        /// <param name="port">The port we are retrieving the callback handler on.</param>
        /// <returns>A <see cref="IOutputCallbackHandler"/> for a given port. If no handler is registered, a
        /// <see cref="DefaultOutputCallbackHandler"/> will be returned.</returns>
        public static IOutputCallbackHandler FindCallback(OutputPortBase port)
        {
            if (WorkingHandlers.ContainsKey(port))
            {
                return(WorkingHandlers[port]);
            }

            return(new DefaultOutputCallbackHandler(port));
        }
Exemplo n.º 4
0
        private void ConfigureConnectionCallback(OutputPortBase output, InputPortBase input)
        {
            output.SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
            input.SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);

            this.NativeCallback = new MMALConnection.MMAL_CONNECTION_CALLBACK_T(this.NativeConnectionCallback);
            IntPtr ptrCallback = Marshal.GetFunctionPointerForDelegate(this.NativeCallback);

            this.Ptr->Callback = ptrCallback;

            this.ConnectionPool = new MMALPoolImpl(this.Ptr->Pool);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Facility to create a connection between two port objects.
        /// </summary>
        /// <param name="output">The output port of the connection.</param>
        /// <param name="input">The input port of the connection.</param>
        /// <param name="inputComponent">The managed instance of the component we are connecting to.</param>
        /// <param name="useCallback">When set to true, enable the connection callback delegate (adversely affects performance).</param>
        /// <returns>A new managed connection object.</returns>
        internal static MMALConnectionImpl CreateConnection(OutputPortBase output, InputPortBase input, MMALDownstreamComponent inputComponent, bool useCallback)
        {
            IntPtr ptr = IntPtr.Zero;

            if (useCallback)
            {
                MMALCheck(MMALConnection.mmal_connection_create(&ptr, output.Ptr, input.Ptr, MMALConnection.MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT), "Unable to create connection");
            }
            else
            {
                MMALCheck(MMALConnection.mmal_connection_create(&ptr, output.Ptr, input.Ptr, MMALConnection.MMAL_CONNECTION_FLAG_TUNNELLING | MMALConnection.MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT), "Unable to create connection");
            }

            return(new MMALConnectionImpl((MMAL_CONNECTION_T *)ptr, output, input, inputComponent, output.ComponentReference, useCallback));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new instance of <see cref="MMALConnectionImpl"/>.
        /// </summary>
        /// <param name="ptr">The native connection pointer.</param>
        /// <param name="output">The upstream component's output port.</param>
        /// <param name="input">The downstream component's input port.</param>
        /// <param name="inputComponent">The upstream component.</param>
        /// <param name="outputComponent">The downstream component.</param>
        /// <param name="useCallback">Configure the connection to intercept native callbacks. Note: will adversely impact performance.</param>
        protected MMALConnectionImpl(MMAL_CONNECTION_T *ptr, OutputPortBase output, InputPortBase input, MMALDownstreamComponent inputComponent, MMALComponentBase outputComponent, bool useCallback)
        {
            this.Ptr                 = ptr;
            this.OutputPort          = output;
            this.InputPort           = input;
            this.DownstreamComponent = inputComponent;
            this.UpstreamComponent   = outputComponent;

            if (useCallback)
            {
                this.ConfigureConnectionCallback(output, input);
            }

            this.Enable();

            if (useCallback)
            {
                this.OutputPort.SendAllBuffers(this.ConnectionPool);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Acts as an isolated processor specifically used when capturing raw frames from the camera component.
        /// </summary>
        /// <param name="cameraPort">The camera component port (still or video).</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The awaitable task.</returns>
        private async Task ProcessRawAsync(OutputPortBase cameraPort,
                                           CancellationToken cancellationToken = default(CancellationToken))
        {
            cameraPort.Trigger = false;

            var t = Task.Run(async() =>
            {
                while (!cameraPort.Trigger)
                {
                    await Task.Delay(50).ConfigureAwait(false);
                }
            }, cancellationToken);

            cameraPort.DisablePort();
            cameraPort.Start();

            this.StartCapture(cameraPort);
            await t.ConfigureAwait(false);

            cameraPort.Handler?.PostProcess();
            this.StopCapture(cameraPort);
            this.Camera.CleanPortPools();
        }
 /// <summary>
 /// Creates a new instance of <see cref="VideoOutputCallbackHandler"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 public VideoOutputCallbackHandler(OutputPortBase port)
     : base(port)
 {
 }
 /// <summary>
 /// Creates a new instance of <see cref="OutputCallbackHandlerBase"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 protected OutputCallbackHandlerBase(OutputPortBase port)
 {
     this.WorkingPort = port;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Creates a new instance of <see cref="FastImageOutputCallbackHandler"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 /// <param name="encoding">The <see cref="MMALEncoding"/> type to restrict on.</param>
 public FastImageOutputCallbackHandler(OutputPortBase port, MMALEncoding encoding)
     : base(port, encoding)
 {
 }
Exemplo n.º 11
0
 /// <summary>
 /// Creates a new instance of <see cref="FastImageOutputCallbackHandler"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 public FastImageOutputCallbackHandler(OutputPortBase port)
     : base(port)
 {
 }
Exemplo n.º 12
0
 /// <summary>
 /// Force capture to stop on a port (Still or Video).
 /// </summary>
 /// <param name="port">The capture port.</param>
 public void ForceStop(OutputPortBase port)
 {
     port.Trigger = true;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Creates a new instance of <see cref="OutputCallbackHandlerBase"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 /// <param name="encodingType">The <see cref="MMALEncoding"/> type to restrict on.</param>
 protected OutputCallbackHandlerBase(OutputPortBase port, MMALEncoding encodingType)
 {
     this.WorkingPort  = port;
     this.EncodingType = encodingType;
 }
Exemplo n.º 14
0
        /// <summary>
        /// Helper method to begin processing image data. Starts the Camera port and awaits until processing is complete.
        /// Cleans up resources upon finish.
        /// </summary>
        /// <param name="cameraPort">The camera port which image data is coming from.</param>
        /// <param name="cancellationToken">A CancellationToken to observe while waiting for a task to complete.</param>
        /// <returns>The awaitable Task.</returns>
        public async Task ProcessAsync(OutputPortBase cameraPort, CancellationToken cancellationToken = default(CancellationToken))
        {
            var handlerComponents = this.PopulateProcessingList();

            if (handlerComponents.Count == 0)
            {
                await this.ProcessRawAsync(cameraPort, cancellationToken);

                return;
            }

            List <Task> tasks = new List <Task>();

            // Enable all connections associated with these components
            foreach (var component in handlerComponents)
            {
                component.EnableConnections();
                component.ForceStopProcessing = false;

                foreach (var port in component.ProcessingPorts.Values)
                {
                    port.Trigger = false;
                    if (port.ConnectedReference == null)
                    {
                        tasks.Add(Task.Run(async() =>
                        {
                            while (!port.Trigger)
                            {
                                await Task.Delay(50).ConfigureAwait(false);
                            }
                        }, cancellationToken));

                        port.Start();
                    }
                }
            }

            // We now begin capturing on the camera, processing will commence based on the pipeline configured.
            this.StartCapture(cameraPort);

            if (cancellationToken == CancellationToken.None)
            {
                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            else
            {
                await Task.WhenAny(Task.WhenAll(tasks), cancellationToken.AsTask()).ConfigureAwait(false);

                foreach (var component in handlerComponents)
                {
                    component.ForceStopProcessing = true;
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }

            // Disable the image encoder output port.
            foreach (var component in handlerComponents)
            {
                foreach (var port in component.ProcessingPorts.Values)
                {
                    // Apply any final processing on each component
                    port.Handler?.PostProcess();

                    if (port.ConnectedReference == null)
                    {
                        port.DisablePort();
                    }
                }

                component.CleanPortPools();
                component.DisableConnections();
            }

            this.StopCapture(cameraPort);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Creates a new instance of <see cref="VideoOutputCallbackHandler"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 /// <param name="encoding">The <see cref="MMALEncoding"/> type to restrict on.</param>
 public VideoOutputCallbackHandler(OutputPortBase port, MMALEncoding encoding)
     : base(port, encoding)
 {
 }
Exemplo n.º 16
0
 /// <summary>
 /// Creates a new instance of <see cref="DefaultOutputCallbackHandler"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 /// <param name="encodingType">The <see cref="MMALEncoding"/> type to restrict on.</param>
 public DefaultOutputCallbackHandler(OutputPortBase port, MMALEncoding encodingType)
     : base(port, encodingType)
 {
 }
Exemplo n.º 17
0
 /// <summary>
 /// Creates a new instance of <see cref="DefaultOutputCallbackHandler"/>.
 /// </summary>
 /// <param name="port">The working <see cref="OutputPortBase"/>.</param>
 public DefaultOutputCallbackHandler(OutputPortBase port)
     : base(port)
 {
 }