private void OpenPlaybackSession()
 {
     if (_viewItemManager.SelectedCamera != null)
     {
         _selectedItem = Configuration.Instance.GetItem(_viewItemManager.SelectedCamera.FQID);
         if (_selectedItem != null)
         {
             labelName.Text = _selectedItem.Name;
             lock (_bitmapVideoSourceNextLock)
             {
                 _bitmapVideoSourceNext        = new BitmapVideoSource(_selectedItem);
                 _bitmapVideoSourceNext.Width  = pictureBox.Width;
                 _bitmapVideoSourceNext.Height = pictureBox.Height;
                 _bitmapVideoSourceNext.SetKeepAspectRatio(true, true);
             }
             _nextToFetchTime = DateTime.UtcNow;
             _stopPlayback    = false;
         }
     }
 }
Exemplo n.º 2
0
        private void BitmapFetchThread()
        {
            bool errorRecovery = false;
            BitmapVideoSource _bitmapVideoSource = null;

            while (!_stop)
            {
                if (_performCloseVideoSource)
                {
                    if (_bitmapVideoSource != null)
                    {
                        _bitmapVideoSource.Close();
                        _bitmapVideoSource = null;
                    }
                    _performCloseVideoSource = false;
                }

                if (_newlySelectedItem != null)
                {
                    _selectedItem             = _newlySelectedItem;
                    _bitmapVideoSource        = new BitmapVideoSource(_selectedItem);
                    _bitmapVideoSource.Width  = pictureBox.Width;
                    _bitmapVideoSource.Height = pictureBox.Height;
                    _bitmapVideoSource.SetKeepAspectRatio(true, true);

                    try
                    {
                        _bitmapVideoSource.Init();

                        BitmapData bitmapData = _bitmapVideoSource.GetAtOrBefore(DateTime.Now) as BitmapData;
                        if (bitmapData != null)
                        {
                            ShowBitmap(bitmapData);
                        }
                        else
                        {
                            ShowError("");      // Clear any error messages
                        }
                        _newlySelectedItem = null;
                        errorRecovery      = false;

                        BeginInvoke(new MethodInvoker(delegate() { groupBoxPlayback.Enabled = true; }));
                    } catch (Exception ex)
                    {
                        if (ex is CommunicationMIPException)
                        {
                            ShowError("Connection lost to server ...");
                        }
                        else
                        {
                            ShowError(ex.Message);
                        }
                        errorRecovery      = true;
                        _bitmapVideoSource = null;
                        _newlySelectedItem = _selectedItem;     // Redo the Initialization
                    }
                }

                if (errorRecovery || _bitmapVideoSource == null)
                {
                    if (this.IsHandleCreated)
                    {
                        BeginInvoke(new MethodInvoker(delegate() { groupBoxPlayback.Enabled = false; }));
                    }
                    if (_mode != PlaybackData.PlayStop)
                    {
                        buttonStop_Click(null, null);
                    }
                    Thread.Sleep(3000);
                    continue;
                }

                if (_performReSize)
                {
                    _bitmapVideoSource.Width  = _reSizeWidth;
                    _bitmapVideoSource.Height = _reSizeHeight;
                    _bitmapVideoSource.SetWidthHeight();
                    if (_mode == PlaybackPlayModeData.Stop)
                    {
                        _nextToFetchTime = _currentShownTime;
                        _redisplay       = true;
                    }
                }

                if (_nextCommand != MyPlayCommand.None && _requestInProgress == false)
                {
                    try
                    {
                        BitmapData bitmapData = null;
                        switch (_nextCommand)
                        {
                        case MyPlayCommand.Start:
                            bitmapData = _bitmapVideoSource.GetBegin();
                            break;

                        case MyPlayCommand.End:
                            bitmapData = _bitmapVideoSource.GetEnd();
                            break;

                        case MyPlayCommand.PrevSequence:
                            bitmapData = _bitmapVideoSource.GetPreviousSequence();
                            break;

                        case MyPlayCommand.NextSequence:
                            bitmapData = _bitmapVideoSource.GetNextSequence();
                            break;

                        case MyPlayCommand.PrevFrame:
                            bitmapData = _bitmapVideoSource.GetPrevious();
                            break;

                        case MyPlayCommand.NextFrame:
                            bitmapData = _bitmapVideoSource.GetNext() as BitmapData;
                            break;
                        }
                        if (bitmapData != null)
                        {
                            ShowBitmap(bitmapData);
                            bitmapData.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is CommunicationMIPException)
                        {
                            ShowError("Connection lost to server ...");
                        }
                        else
                        {
                            ShowError(ex.Message);
                        }
                        errorRecovery      = true;
                        _bitmapVideoSource = null;
                        _newlySelectedItem = _selectedItem;     // Redo the Initialization
                        _nextCommand       = MyPlayCommand.None;
                        Thread.Sleep(3000);
                        continue;
                    }
                    _nextCommand = MyPlayCommand.None;
                }

                if (_nextToFetchTime != DateTime.MinValue && _requestInProgress == false)
                {
                    Debug.WriteLine("NextToFetch = " + _nextToFetchTime.ToString());

                    DateTime time      = _nextToFetchTime;
                    DateTime localTime = time.Kind == DateTimeKind.Local ? time : time.ToLocalTime();
                    DateTime utcTime   = time.Kind == DateTimeKind.Local ? time.ToUniversalTime() : time;

                    // Next 10 lines new for MIPSDK 2014
                    bool willResultInSameFrame = false;
                    // Lets validate if we are just asking for the same frame
                    if (_currentTimeInformation != null)
                    {
                        if (_mode == PlaybackPlayModeData.Forward &&
                            _currentTimeInformation.NextTime > utcTime)
                        {
                            willResultInSameFrame = true;
                        }
                        if (_mode == PlaybackPlayModeData.Reverse &&
                            _currentTimeInformation.PreviousTime < utcTime)
                        {
                            willResultInSameFrame = true;
                        }
                    }
                    _nextToFetchTime = DateTime.MinValue;

                    if (willResultInSameFrame)
                    {
                        // Same frame -> Ignore request
                    }
                    else
                    {
                        try
                        {
                            BeginInvoke(
                                new MethodInvoker(delegate() { textBoxAsked.Text = localTime.ToString("yyyy-MM-dd HH:mm:ss.fff"); }));

                            BitmapData bitmapData = null;
                            switch (_mode)
                            {
                            case PlaybackPlayModeData.Stop:
                                bitmapData = _bitmapVideoSource.GetAtOrBefore(utcTime) as BitmapData;

                                // Next 2 lines new for MIPSDK 2014
                                if (bitmapData != null && bitmapData.IsPreviousAvailable == false)
                                {
                                    bitmapData.PreviousDateTime = bitmapData.DateTime - TimeSpan.FromMilliseconds(10);
                                }
                                break;

                            case PlaybackPlayModeData.Forward:
                                _bitmapVideoSource.GoTo(utcTime, _mode);
                                bitmapData = _bitmapVideoSource.GetNext() as BitmapData;

                                // Next 2 lines new for MIPSDK 2014
                                if (bitmapData != null && bitmapData.IsPreviousAvailable == false)
                                {
                                    if (utcTime - TimeSpan.FromMilliseconds(10) < bitmapData.DateTime)
                                    {
                                        bitmapData.PreviousDateTime = utcTime - TimeSpan.FromMilliseconds(10);
                                    }
                                    else
                                    {
                                        bitmapData.PreviousDateTime = bitmapData.DateTime;
                                    }
                                }
                                break;

                            case PlaybackPlayModeData.Reverse:
                                _bitmapVideoSource.GoTo(utcTime, _mode);
                                bitmapData = _bitmapVideoSource.GetPrevious();
                                // Next 2 lines new for MIPSDK 2014
                                if (bitmapData != null && bitmapData.IsPreviousAvailable == false)
                                {
                                    bitmapData.PreviousDateTime = bitmapData.DateTime - TimeSpan.FromMilliseconds(10);
                                }
                                break;
                            }

                            if (bitmapData != null)
                            {
                                ShowBitmap(bitmapData);
                                bitmapData.Dispose();
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex is CommunicationMIPException)
                            {
                                ShowError("Connection lost to server ...");
                            }
                            else
                            {
                                ShowError(ex.Message);
                            }
                            errorRecovery      = true;
                            _bitmapVideoSource = null;
                            _nextCommand       = MyPlayCommand.None;
                            _newlySelectedItem = _selectedItem;                             // Redo the Initialization
                        }
                    }
                }
            }
            _fetchThread = null;
        }