SuccessFailurePort OpenCamera(CameraInstance camera)
        {
            SuccessFailurePort resultPort = new SuccessFailurePort();

            new OpenRequest(camera.DevicePath).Send(_client);

            Activate(
                Arbiter.Receive <HresultResponse>(false,
                                                  _pipeDataPort,
                                                  delegate(HresultResponse result)
            {
                if (result.hr == 0)
                {
                    resultPort.Post(SuccessResult.Instance);
                }
                else
                {
                    resultPort.Post(Marshal.GetExceptionForHR(result.hr));
                }
            },
                                                  test => test.type == WebcamResponse.OpenDevices
                                                  )
                );

            return(resultPort);
        }
        Port <List <Format> > EnumFormats(CameraInstance camera)
        {
            Port <List <Format> > resultPort = new Port <List <Format> >();

            SpawnIterator(camera, resultPort, DoEnumFormats);

            return(resultPort);
        }
        IEnumerator <ITask> DoEnumFormats(CameraInstance camera, Port <List <Format> > resultPort)
        {
            List <Format> formats = new List <Format>();

            new OpenRequest(camera.DevicePath).Send(_client);

            HresultResponse hr = null;

            do
            {
                yield return(Arbiter.Receive <HresultResponse>(false, _pipeDataPort, success => hr = success));
            } while (hr.type != WebcamResponse.OpenDevices);

            new FormatRequest().Send(_client);

            for (; ;)
            {
                FormatResponse  response = null;
                HresultResponse done     = null;

                yield return(Arbiter.Choice(
                                 Arbiter.Receive <FormatResponse>(false, _pipeDataPort, success => response = success),
                                 Arbiter.Receive <HresultResponse>(false, _pipeDataPort, hresult => done = hresult)
                                 ));

                if (done != null)
                {
                    if (done.type == WebcamResponse.EnumeratFormats)
                    {
                        break;
                    }
                }
                else
                {
                    var format = new Format(
                        response.Width,
                        response.Height,
                        0,
                        0,
                        response.Compression
                        );

                    formats.Add(format);
                }
            }


            resultPort.Post(formats);
        }
        /// <summary>
        /// Tests whether this CameraInstance is equivalent to the supplied Camera object
        /// </summary>
        /// <param name="camera">Camera object to test against</param>
        /// <returns>true if the FriendlyName or DevicePath match.</returns>
        internal bool Equals(CameraInstance camera)
        {
            bool hasFriendlyName = !string.IsNullOrEmpty(_friendlyName);
            bool hasDevicePath   = !string.IsNullOrEmpty(_devicePath);

            if (hasFriendlyName && hasDevicePath)
            {
                return(_friendlyName == camera.FriendlyName &&
                       _devicePath == camera.DevicePath);
            }
            else if (hasDevicePath)
            {
                return(_devicePath == camera.DevicePath);
            }
            else if (hasFriendlyName)
            {
                return(_friendlyName == camera.FriendlyName);
            }
            return(false);
        }
        IEnumerator <ITask> WaitForEnumResponses(Port <List <CameraInstance> > resultPort)
        {
            List <CameraInstance> cameras = new List <CameraInstance>();

            for (; ;)
            {
                EnumResponse    response = null;
                HresultResponse done     = null;

                yield return(Arbiter.Choice(
                                 Arbiter.Receive <EnumResponse>(false, _pipeDataPort, success => response = success),
                                 Arbiter.Receive <HresultResponse>(false, _pipeDataPort, hresult => done = hresult)
                                 ));

                if (done != null)
                {
                    if (done.type == WebcamResponse.EnumerateDevices)
                    {
                        break;
                    }
                }
                else
                {
                    var camera = new CameraInstance();

                    camera.DevicePath   = response.device;
                    camera.FriendlyName = response.name;

                    cameras.Add(camera);
                }
            }

            foreach (var camera in cameras)
            {
                var formatPort = EnumFormats(camera);

                yield return(Arbiter.Receive(false, formatPort, success => camera.SupportedFormats = success));
            }

            resultPort.Post(cameras);
        }
Esempio n. 6
0
        public IEnumerator <ITask> UpdateDeviceHandler(UpdateDevice update)
        {
            CameraInstance selected  = update.Body.Selected;
            bool           updated   = false;
            Exception      exception = null;

            if (_state.Selected != null &&
                _state.Selected.Started)
            {
                var stopPort = StopCapture();

                yield return((Choice)stopPort);

                _state.Selected.Started = false;

                _state.Selected         = null;
                _state.CameraDeviceName = null;
            }


            var devicePort = EnumDevices();
            List <CameraInstance> cameras = null;

            yield return(Arbiter.Receive(false, devicePort, list => cameras = list));

            foreach (var camera in cameras)
            {
                if (selected.Equals(camera))
                {
                    var openPort = OpenCamera(camera);

                    yield return((Choice)openPort);

                    exception = openPort;
                    if (exception != null)
                    {
                        LogError("Unable to open device", exception);
                        continue;
                    }

                    var formatsPort = EnumFormats(camera);

                    yield return(Arbiter.Receive(false, formatsPort, list => selected.SupportedFormats = list));

                    Format requestFormat = selected.Format ?? new Format();
                    bool   setFormat     = selected.IsValidFormat(requestFormat);

                    if (setFormat)
                    {
                        var setFormatPort = SetFormat(requestFormat);

                        yield return((Choice)setFormatPort);

                        exception = setFormatPort;
                        if (exception != null)
                        {
                            LogError("Unable to set the requested format", exception);
                            exception = null;
                        }
                    }

                    var startPort = StartCapture();

                    yield return((Choice)startPort);

                    exception = startPort;
                    if (exception != null)
                    {
                        LogError("Unable to start capture", exception);
                        continue;
                    }

                    _state.CameraDeviceName = selected.FriendlyName;
                    updated = true;
                    break;
                }
            }

            if (updated)
            {
                _state.Cameras          = cameras;
                _state.Selected         = selected;
                _state.Selected.Started = true;
                if (selected.Format != null)
                {
                    _state.ImageSize = new physics.Vector2(selected.Format.Width, selected.Format.Height);
                }

                update.ResponsePort.Post(DefaultUpdateResponseType.Instance);

#if !URT_MINCLR
                SendNotification(_submgrPort, update);
#else
                SendNotification(update);
#endif
            }
            else
            {
                update.ResponsePort.Post(Fault.FromCodeSubcodeReason(
                                             W3C.Soap.FaultCodes.Receiver,
                                             DsspFaultCodes.UnknownEntry,
                                             "Camera not found"
                                             )
                                         );
            }
        }