private void ShowJPEG(JPEGData jpegData) { if (InvokeRequired) { BeginInvoke(new ShowJpegDelegate(ShowJPEG), jpegData); } else { Debug.WriteLine("ShowJPEG imagetime:" + jpegData.DateTime.ToLocalTime()); Trace.WriteLine("ShowJPEG imagetime:" + jpegData.DateTime.ToLocalTime() + ", Decoding:" + jpegData.HardwareDecodingStatus); if (jpegData.DateTime != _currentShownTime && _selectedItem != null) { MemoryStream ms = new MemoryStream(jpegData.Bytes); Bitmap newBitmap = new Bitmap(ms); if (newBitmap.Width != pictureBox.Width || newBitmap.Height != pictureBox.Height) { pictureBox.Image = new Bitmap(newBitmap, pictureBox.Size); } else { pictureBox.Image = newBitmap; } if (jpegData.CroppingDefined) { Debug.WriteLine("Image has been cropped: " + jpegData.CropWidth + "x" + jpegData.CropHeight); } ms.Close(); ms.Dispose(); textBoxTime.Text = jpegData.DateTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"); // Inform the PlybackController of the time information - so skipping can be done correctly _currentTimeInformation = new PlaybackTimeInformationData() { Item = _selectedItem.FQID, CurrentTime = jpegData.DateTime, NextTime = jpegData.NextDateTime, PreviousTime = jpegData.PreviousDateTime }; EnvironmentManager.Instance.SendMessage( new VideoOS.Platform.Messaging.Message(MessageId.SmartClient.PlaybackTimeInformation, _currentTimeInformation), _playbackFQID); _currentShownTime = jpegData.DateTime; if (_mode == PlaybackPlayModeData.Stop) { // When playback is stopped, we move the time to where the user have scrolled, or if the user pressed // one of the navigation buttons (Next..., Prev...) EnvironmentManager.Instance.SendMessage(new VideoOS.Platform.Messaging.Message(MessageId.SmartClient.PlaybackCommand, new PlaybackCommandData() { Command = PlaybackData.Goto, DateTime = jpegData.DateTime }), _playbackFQID); } Debug.WriteLine("Image time: " + jpegData.DateTime.ToLocalTime().ToString("HH.mm.ss.fff") + ", Mode=" + _mode); } _requestInProgress = false; } }
private void JPEGFetchThread() { bool errorRecovery = false; while (!_stop) { if (_performCloseVideoSource) { if (_jpegVideoSource != null) { _jpegVideoSource.Close(); _jpegVideoSource = null; } _performCloseVideoSource = false; } if (_newlySelectedItem != null) { _selectedItem = _newlySelectedItem; _jpegVideoSource = new JPEGVideoSource(_selectedItem); if (checkBoxAspect.Checked) { // Keeping aspect ratio can only work when the Media Toolkit knows the actual displayed area _jpegVideoSource.Width = pictureBox.Width; _jpegVideoSource.Height = pictureBox.Height; _jpegVideoSource.SetKeepAspectRatio(checkBoxAspect.Checked, checkBoxFill.Checked); // Must be done before Init } try { _jpegVideoSource.Init(); JPEGData jpegData = _currentShownTime == DateTime.MinValue ? _jpegVideoSource.GetBegin() : _jpegVideoSource.GetAtOrBefore(_currentShownTime) as JPEGData; if (jpegData != null) { _requestInProgress = true; ShowJPEG(jpegData); } else { ShowError(""); // Clear any error messages } _newlySelectedItem = null; errorRecovery = false; } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to server ..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } } if (errorRecovery) { Thread.Sleep(3000); continue; } if (_setNewResolution && _jpegVideoSource != null && _requestInProgress == false) { try { _jpegVideoSource.Width = _newWidth; _jpegVideoSource.Height = _newHeight; _jpegVideoSource.SetWidthHeight(); _setNewResolution = false; JPEGData jpegData; jpegData = _jpegVideoSource.GetAtOrBefore(_currentShownTime) as JPEGData; if (jpegData != null) { _requestInProgress = true; _currentShownTime = DateTime.MinValue; ShowJPEG(jpegData); } } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to recorder..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } } if (_requestInProgress == false && _jpegVideoSource != null && _nextCommand != MyPlayCommand.None) { JPEGData jpegData = null; try { switch (_nextCommand) { case MyPlayCommand.Start: jpegData = _jpegVideoSource.GetBegin(); break; case MyPlayCommand.NextFrame: jpegData = _jpegVideoSource.GetNext() as JPEGData; break; case MyPlayCommand.NextSequence: jpegData = _jpegVideoSource.GetNextSequence(); break; case MyPlayCommand.PrevFrame: jpegData = _jpegVideoSource.GetPrevious(); break; case MyPlayCommand.PrevSequence: jpegData = _jpegVideoSource.GetPreviousSequence(); break; case MyPlayCommand.End: jpegData = _jpegVideoSource.GetEnd(); break; } } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to recorder..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } if (jpegData != null) { _requestInProgress = true; ShowJPEG(jpegData); } _nextCommand = MyPlayCommand.None; } if (_nextToFetchTime != DateTime.MinValue && _requestInProgress == false && _jpegVideoSource != null) { bool willResultInSameFrame = false; // Lets validate if we are just asking for the same frame if (_currentTimeInformation != null) { if (_currentTimeInformation.PreviousTime < _nextToFetchTime && _currentTimeInformation.NextTime > _nextToFetchTime) { willResultInSameFrame = true; } } if (willResultInSameFrame) { Debug.WriteLine("Now Fetch ignored: " + _nextToFetchTime.ToLongTimeString() + " - nextToFetch=" + _nextToFetchTime.ToLongTimeString()); // Same frame -> Ignore request _requestInProgress = false; _nextToFetchTime = DateTime.MinValue; } else { Debug.WriteLine("Now Fetch: " + _nextToFetchTime.ToLongTimeString()); DateTime time = _nextToFetchTime; _nextToFetchTime = DateTime.MinValue; try { DateTime localTime = time.Kind == DateTimeKind.Local ? time : time.ToLocalTime(); DateTime utcTime = time.Kind == DateTimeKind.Local ? time.ToUniversalTime() : time; BeginInvoke( new MethodInvoker(delegate() { textBoxAsked.Text = localTime.ToString("yyyy-MM-dd HH:mm:ss.fff"); })); JPEGData jpegData; jpegData = _jpegVideoSource.GetAtOrBefore(utcTime) as JPEGData; if (jpegData == null && _mode == PlaybackPlayModeData.Stop) { jpegData = _jpegVideoSource.GetNearest(utcTime) as JPEGData; } if (_mode == PlaybackPlayModeData.Reverse) { while (jpegData != null && jpegData.DateTime > utcTime) { jpegData = _jpegVideoSource.GetPrevious(); } } else if (_mode == PlaybackPlayModeData.Forward) { if (jpegData != null && jpegData.DateTime < utcTime) { jpegData = _jpegVideoSource.Get(utcTime) as JPEGData; } } if (jpegData != null) { _requestInProgress = true; ShowJPEG(jpegData); } } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to server ..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } } } Thread.Sleep(5); } _fetchThread = null; }