/// <summary> /// Shut down capture /// </summary> private void CloseInterfaces() { int hr; try { if (_mediaCtrl != null) { // Stop the graph hr = _mediaCtrl.Stop(); _mediaCtrl = null; } #if DEBUG if (_rot != null) { _rot.Dispose(); } #endif if (_graphBuilder != null) { Marshal.ReleaseComObject(_graphBuilder); _graphBuilder = null; } _state = VideoCaptureState.UNINITIALIZED; GC.Collect(); } catch (Exception e) { TraceException.WriteLine(e); } }
public void InitializeCapture(bool recording) { try { #if DEBUG Stopwatch benchmark = Stopwatch.StartNew(); #endif CloseInterfaces(); if (!Directory.Exists(_strFolderName)) { // Create the directory it does not exist. Directory.CreateDirectory(_strFolderName); } string tmp = DateTime.Now.ToString("yyymmddHHmmssfff"); _strFileName = _strFolderName + Path.DirectorySeparatorChar + tmp + ".avi"; // Set up the capture graph SetupGraph(_strCapture, _strCompressor, _strFileName, _fps, _videoWidth, _videoHeight, _hOwner, recording); #if DEBUG benchmark.Stop(); Debug.WriteLine("SetupGraph: " + ((double)(benchmark.Elapsed.TotalMilliseconds)).ToString("0.00 ms")); #endif ChangeState(VideoCaptureState.STOPPED); } catch (Exception e) { TraceException.WriteLine(e); Dispose(); } }
private void Trace(string filename, string methodname, string errorMessage, string dataString) { var ex = new TraceException( $"[{filename}:{methodname}] => {errorMessage} : {dataString}" ) { Source = string.Format($"{filename}:{methodname}") }; Log(ex); }
/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> public void Dispose() { try { _fontOverlay.Dispose(); _transparentBrush.Dispose(); _transparentFont.Dispose(); } catch (Exception e) { TraceException.WriteLine(e); } }
private void standardLayoutToolStripMenuItem_Click(object sender, EventArgs e) { try { DockingPanel.SuspendLayout(true); //check for null forms - Not working - figure out the disposal of forms. if (_videoForm.IsDisposed == true) { _videoForm = new VideoForm(_telemetryComms.Data); } _videoForm.Show(DockingPanel, DockState.Document); if (_mapForm.IsDisposed == true) { _mapForm = new MapForm(); } _mapForm.Show(DockingPanel, DockState.DockRight); if (_settingsForm.IsDisposed == true) { _settingsForm = new SettingsForm(); } _settingsForm.Show(DockingPanel, DockState.DockLeft); if (_outputForm.IsDisposed == true) { _outputForm = new OutputForm(); } _outputForm.Show(DockingPanel, DockState.DockBottom); if (_telemetryForm.IsDisposed == true) { _telemetryForm = new TelemetryForm(_telemetryComms.Data); } if (_telemetryForm.IsDisposed == true) { _telemetryForm = new TelemetryForm(_telemetryComms.Data); } _telemetryForm.Show(DockingPanel, DockState.Float); //if (_geForm.IsDisposed == true) // _geForm = new GEForm(); //_geForm.Show(DockingPanel, DockState.Float); //save current layout _deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString); DockingPanel.ResumeLayout(true, true); } catch (Exception ex) { TraceException.WriteLine(ex); } }
/// <summary> /// Checks the HR. /// </summary> /// <param name="hr">The hr.</param> /// <param name="msg">The MSG.</param> static void checkHR(int hr, string msg) { try { if (hr < 0) { Trace.WriteLine(msg); DsError.ThrowExceptionForHR(hr); } } catch (Exception e) { TraceException.WriteLine(e); } }
/// <summary> /// Initializes a new instance of the <see cref="HUD"/> class. /// </summary> public HUD() { try { _speed = 0; _altitude = 0; _latitude = 0; _longitude = 0; _headingMagneticNorth = 0; _roll = 0; _pitch = 0; _yaw = 0; _message = "Initialized..."; _videoWidth = 640; _videoHeight = 480; // TO DO: fix the font sizes // ADD: more parameters for color, font size, etc _fontOverlay = new Font("Tahoma", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); _transparentFont = new Font("Tahoma", 10, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); _transparentBrush = new SolidBrush(_color); _pen1 = new Pen(_color, 1); _pen2 = new Pen(_color, 1); _pen2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; _brush = new SolidBrush(Color.FromArgb(100, 0, 0, 0)); // Construct 2 new StringFormat objects _format1 = new StringFormat(StringFormatFlags.NoClip); _format1.LineAlignment = StringAlignment.Near; _format1.Alignment = StringAlignment.Center; _format2 = new StringFormat(StringFormatFlags.NoClip); _format2.LineAlignment = StringAlignment.Near; _format2.Alignment = StringAlignment.Near; } catch (Exception e) { TraceException.WriteLine(e); } }
public void InitializeComPort() { try { //open the COM port _sp.Open(); Trace.WriteLine("SerialCom: " + _sp.PortName + " opened)", "TelemetryComms"); } catch (IOException ioex) { TraceException.WriteLine(ioex); } catch (Exception ex) { TraceException.WriteLine(ex); } }
public VideoCapture(IntPtr owner) { try { _hOwner = owner; ChangeState(VideoCaptureState.UNINITIALIZED); _hud_enabled = false; _hud = new HUD(); _hud_enabled = true; } catch (Exception e) { TraceException.WriteLine(e); }; }
//Serial Port DataReceived Handler private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { byte[] data = new byte[1024]; int ret = _sp.Read(data, 0, data.Length); for (int i = 0; i < ret; i++) { _recievedData.Enqueue(data[i]); } ProcessData(); } catch (Exception ex) { TraceException.WriteLine(ex); } }
/// <summary> /// Draws the HUD. /// </summary> /// <param name="src">The SRC.</param> /// <param name="dst">The DST.</param> public void DrawHUD(Bitmap src, Bitmap dst) { try { _transparentBrush.Color = _color; _pen1.Color = _color; _pen2.Color = _color; //_brush.Color = _color_brush; //Initialize Graphics _g = Graphics.FromImage(src); _g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; _g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; _g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; //.ClearTypeGridFit; // Draw top and bottom black bands _rectangleTextBkg[0] = new Rectangle(0, 0, _videoWidth, 20); _rectangleTextBkg[1] = new Rectangle(0, _videoHeight - 20, _videoWidth, _videoHeight); _g.FillRectangles(_brush, _rectangleTextBkg); _s = DateTime.Now.ToString(); _s += " " + _message; _g.DrawString(_s, _fontOverlay, _transparentBrush, (RectangleF)_rectangleTextBkg[1], _format2); // Draw numeric boxes for: // 1. Heading _rectangleHUD[0] = new Rectangle((_videoWidth / 2) - 25, 30, 50, 15); // 2. Altitude _rectangleHUD[1] = new Rectangle(_videoWidth - 95, (_videoHeight / 2) - 8, 50, 15); // 3. Speed _rectangleHUD[2] = new Rectangle(45, (_videoHeight / 2) - 8, 50, 15); //_g.DrawRectangles(_pen1, _rectangleHUD); _g.FillRectangles(_brush, _rectangleHUD); //Draw numeric values inside each box: // 1. Heading _g.DrawString(_headingMagneticNorth.ToString(), _transparentFont, _transparentBrush, (RectangleF)_rectangleHUD[0], _format1); // 2. Altitude _g.DrawString(_altitude.ToString(), _transparentFont, _transparentBrush, (RectangleF)_rectangleHUD[1], _format1); //3. Speed _g.DrawString(_speed.ToString(), _transparentFont, _transparentBrush, (RectangleF)_rectangleHUD[2], _format1); //Draw aircraft indicator _pointsHUD3[0].X = _videoWidth / 2 - 20; _pointsHUD3[0].Y = _videoHeight / 2; _pointsHUD3[1].X = _videoWidth / 2 - 10; _pointsHUD3[1].Y = _videoHeight / 2; _pointsHUD3[2].X = _videoWidth / 2 - 5; _pointsHUD3[2].Y = _videoHeight / 2 + 10; _pointsHUD3[3].X = _videoWidth / 2; _pointsHUD3[3].Y = _videoHeight / 2 + 3; _pointsHUD3[4].X = _videoWidth / 2 + 5; _pointsHUD3[4].Y = _videoHeight / 2 + 10; _pointsHUD3[5].X = _videoWidth / 2 + 10; _pointsHUD3[5].Y = _videoHeight / 2; _pointsHUD3[6].X = _videoWidth / 2 + 20; _pointsHUD3[6].Y = _videoHeight / 2; _g.DrawLines(_pen1, _pointsHUD3); // Draw horizontal speed indicator Rectangle rectangleObj = new Rectangle(_videoWidth / 2 - _reticlesize / 2, _videoHeight / 2 - _reticlesize / 2, _reticlesize, _reticlesize); _g.DrawEllipse(_pen1, rectangleObj); // this line should represent the horizontal direction and speed up to X m/s _pointsHUD1[0].X = _videoWidth / 2 + 20; _pointsHUD1[0].Y = _videoHeight / 2 - 30; _pointsHUD1[1].X = _videoWidth / 2; _pointsHUD1[1].Y = _videoHeight / 2; _g.DrawLines(_pen1, _pointsHUD1); double pitch = _pitch; if (_pitch >= 90) { pitch = 180 - _pitch; } if (_pitch <= -90) { pitch = -180 - _pitch; } // save existing matrices _oldState = _g.Save(); //yaw _g.TranslateTransform((float)_yaw * _yaw_resolution, 0, MatrixOrder.Append); //pitch _g.TranslateTransform(0, (float)pitch * _pitch_resolution, MatrixOrder.Append); if (_pitch >= 90 || _pitch <= -90) { _g.RotateTransform((float)180, MatrixOrder.Append); } // make rotation point the origin _g.TranslateTransform(-_videoWidth / 2, -_videoHeight / 2); // roll _g.RotateTransform((float)-_roll, MatrixOrder.Append); // translate back _g.TranslateTransform(_videoWidth / 2, _videoHeight / 2, MatrixOrder.Append); //DrawHUD horizont line _pointsHUD1[0].X = _videoWidth / 2 - 150; _pointsHUD1[0].Y = _videoHeight / 2; _pointsHUD1[1].X = _videoWidth / 2 - 25; _pointsHUD1[1].Y = _videoHeight / 2; _g.DrawLines(_pen1, _pointsHUD1); _pointsHUD1[0].X = _videoWidth / 2 + 150; _pointsHUD1[0].Y = _videoHeight / 2; _pointsHUD1[1].X = _videoWidth / 2 + 25; _pointsHUD1[1].Y = _videoHeight / 2; _g.DrawLines(_pen1, _pointsHUD1); rectangleObj = new Rectangle(_videoWidth / 2 - _reticlesize1 / 2, _videoHeight / 2 - _reticlesize1 / 2, _reticlesize1, _reticlesize1); _g.DrawEllipse(_pen1, rectangleObj); for (int i = 1; i <= 36; i++) { _pointsHUD2[0].X = _videoWidth / 2 - 75; _pointsHUD2[0].Y = _videoHeight / 2 + _pitch_resolution * 10 * i - 10; _pointsHUD2[1].X = _videoWidth / 2 - 75; _pointsHUD2[1].Y = _videoHeight / 2 + _pitch_resolution * 10 * i; _pointsHUD2[2].X = _videoWidth / 2 - 25; _pointsHUD2[2].Y = _videoHeight / 2 + _pitch_resolution * 10 * i; _g.DrawLines(_pen2, _pointsHUD2); _pointsHUD2[0].X = _videoWidth / 2 + 75; _pointsHUD2[0].Y = _videoHeight / 2 + _pitch_resolution * 10 * i - 10; _pointsHUD2[1].X = _videoWidth / 2 + 75; _pointsHUD2[1].Y = _videoHeight / 2 + _pitch_resolution * 10 * i; _pointsHUD2[2].X = _videoWidth / 2 + 25; _pointsHUD2[2].Y = _videoHeight / 2 + _pitch_resolution * 10 * i; _g.DrawLines(_pen2, _pointsHUD2); _pointsHUD2[0].X = _videoWidth / 2 - 75; _pointsHUD2[0].Y = _videoHeight / 2 - _pitch_resolution * 10 * i + 10; _pointsHUD2[1].X = _videoWidth / 2 - 75; _pointsHUD2[1].Y = _videoHeight / 2 - _pitch_resolution * 10 * i; _pointsHUD2[2].X = _videoWidth / 2 - 25; _pointsHUD2[2].Y = _videoHeight / 2 - _pitch_resolution * 10 * i; _g.DrawLines(_pen1, _pointsHUD2); _pointsHUD2[0].X = _videoWidth / 2 + 75; _pointsHUD2[0].Y = _videoHeight / 2 - _pitch_resolution * 10 * i + 10; _pointsHUD2[1].X = _videoWidth / 2 + 75; _pointsHUD2[1].Y = _videoHeight / 2 - _pitch_resolution * 10 * i; _pointsHUD2[2].X = _videoWidth / 2 + 25; _pointsHUD2[2].Y = _videoHeight / 2 - _pitch_resolution * 10 * i; _g.DrawLines(_pen1, _pointsHUD2); // Draw numeric boxes _rectanglePitch[0] = new Rectangle(_videoWidth / 2 - 125, _videoHeight / 2 + _pitch_resolution * 10 * i - 15, 50, 15); _rectanglePitch[1] = new Rectangle(_videoWidth / 2 + 75, _videoHeight / 2 + _pitch_resolution * 10 * i - 15, 50, 15); _g.DrawString((-i * 10).ToString(), _fontOverlay, _transparentBrush, (RectangleF)_rectanglePitch[0], _format1); _g.DrawString((-i * 10).ToString(), _fontOverlay, _transparentBrush, (RectangleF)_rectanglePitch[1], _format1); _rectanglePitch[2] = new Rectangle(_videoWidth / 2 - 125, _videoHeight / 2 - _pitch_resolution * 10 * i, 50, 15); _rectanglePitch[3] = new Rectangle(_videoWidth / 2 + 75, _videoHeight / 2 - _pitch_resolution * 10 * i, 50, 15); _g.DrawString((i * 10).ToString(), _fontOverlay, _transparentBrush, (RectangleF)_rectanglePitch[2], _format1); _g.DrawString((i * 10).ToString(), _fontOverlay, _transparentBrush, (RectangleF)_rectanglePitch[3], _format1); } _g.Restore(_oldState); // restore old _oldState = _g.Save(); // save existing matrices // make rotation point the origin _g.TranslateTransform(-_videoWidth / 2, -_videoHeight + 100); // roll _g.RotateTransform((float)_roll, MatrixOrder.Append); // translate back _g.TranslateTransform(_videoWidth / 2, _videoHeight - 100, MatrixOrder.Append); src.RotateFlip(RotateFlipType.RotateNoneFlipY); _g.Dispose(); _g = Graphics.FromImage(dst); _g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // draw the overlay bitmap over the video's bitmap _g.DrawImage(src, 0, 0, src.Width, src.Height); _g.Dispose(); } catch (Exception e) { TraceException.WriteLine(e); } }
/// <summary> /// Mark underling layer exception /// </summary> /// <param name="m">The trace message to write.</param> /// <param name="t">One of the <see cref="System.Diagnostics.TraceEventType"/> values that specifies /// the event type of the trace data.</param> /// <param name="i">A numeric identifier for the event.</param> /// <param name="e">Exception couch by the underling layer </param> protected void MarkException(string m, TraceEventType t, int i, Exception e) { LastLayerError = new TraceException(m, t, i, e); }
/// <summary> /// build the capture graph for grabber. /// </summary> /// <param name="strCapture">The STR capture.</param> /// <param name="strCompressor">The STR compressor.</param> /// <param name="strFileName">Name of the STR file.</param> /// <param name="iFrameRate">The i frame rate.</param> /// <param name="iWidth">Width of the i.</param> /// <param name="iHeight">Height of the i.</param> /// <param name="owner">The owner.</param> /// <param name="record">if set to <c>true</c> [record].</param> private void SetupGraph(string strCapture, string strCompressor, string strFileName, int iFrameRate, int iWidth, int iHeight, IntPtr owner, bool record) { ICaptureGraphBuilder2 captureGraphBuilder = null; ISampleGrabber sampGrabber = null; IBaseFilter theIPinTee = null; IBaseFilter mux = null; IFileSinkFilter sink = null; IBaseFilter captureDevice = null; IBaseFilter captureCompressor = null; IBaseFilter theRenderer = null; int hr = 0; try { //Create the filter for the selected video input captureDevice = CreateFilter(FilterCategory.VideoInputDevice, strCapture); //Create the filter for the selected video compressor captureCompressor = CreateFilter(FilterCategory.VideoCompressorCategory, strCompressor); //Create the Graph _graphBuilder = (IGraphBuilder) new FilterGraph(); //Create the Capture Graph Builder captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Attach the filter graph to the capture graph hr = captureGraphBuilder.SetFiltergraph(this._graphBuilder); checkHR(hr, "Error attaching filter graph to capture graph"); //Add the Video input device to the graph hr = _graphBuilder.AddFilter(captureDevice, "QUAVS input filter"); checkHR(hr, "Error attaching video input"); //setup cature device SetConfigParms(captureGraphBuilder, captureDevice, iFrameRate, iWidth, iHeight); //Add a sample grabber sampGrabber = (ISampleGrabber) new SampleGrabber(); ConfigureSampleGrabber(sampGrabber); hr = _graphBuilder.AddFilter((IBaseFilter)sampGrabber, "QUAVS SampleGrabber"); checkHR(hr, "Error adding sample grabber"); //connect capture device to SampleGrabber hr = _graphBuilder.Connect(GetPin(captureDevice, "Capture"), GetPin((IBaseFilter)sampGrabber, "Input")); checkHR(hr, "Error attaching sample grabber to capture pin"); //Add Ininite Pin Tee theIPinTee = (IBaseFilter) new InfTee(); hr = _graphBuilder.AddFilter(theIPinTee, "QUAVS Pin Tee"); checkHR(hr, "Error adding infinite tee pin"); //connect capture SampleGrabber to IPinTee hr = _graphBuilder.Connect(GetPin((IBaseFilter)sampGrabber, "Output"), GetPin(theIPinTee, "Input")); checkHR(hr, "Error adding SampleGrabber"); if (record) { //Add the Video compressor filter to the graph hr = _graphBuilder.AddFilter(captureCompressor, "QUAVS compressor filter"); checkHR(hr, "Error adding compressor filter"); //connect capture IPinTee output1 to compressor hr = _graphBuilder.Connect(GetPin(theIPinTee, "Output1"), GetPin(captureCompressor, "Input")); checkHR(hr, "Error adding TO DO"); //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink hr = captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, strFileName, out mux, out sink); checkHR(hr, "Error adding mux filter or setting output file name"); //connect compressor to mux output hr = _graphBuilder.Connect(GetPin(captureCompressor, "Output"), GetPin(mux, "Input 01")); checkHR(hr, "Error connecting the compressor to mux"); // Get the default video renderer theRenderer = new VideoRendererDefault() as IBaseFilter; hr = _graphBuilder.AddFilter(theRenderer, "Renderer"); checkHR(hr, "Error adding screen renderer"); //connect capture TO DO hr = _graphBuilder.Connect(GetPin(theIPinTee, "Output2"), GetPin(theRenderer, "VMR Input0")); checkHR(hr, "Error connecting screen renderer"); } else { // Get the default video renderer theRenderer = new VideoRendererDefault() as IBaseFilter; hr = _graphBuilder.AddFilter(theRenderer, "Renderer"); checkHR(hr, "Error adding screen renderer"); //connect capture TO DO hr = _graphBuilder.Connect(GetPin(theIPinTee, "Output1"), GetPin(theRenderer, "VMR Input0")); checkHR(hr, "Error connecting screen renderer"); } SaveSizeInfo(sampGrabber); #if DEBUG _rot = new DsROTEntry(_graphBuilder); #endif if (owner != IntPtr.Zero) { //get the video window from the graph IVideoWindow videoWindow = null; videoWindow = (IVideoWindow)_graphBuilder; //Set the owener of the videoWindow to an IntPtr of some sort (the Handle of any control - could be a form / button etc.) hr = videoWindow.put_Owner(owner); DsError.ThrowExceptionForHR(hr); //Set the style of the video window hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren); DsError.ThrowExceptionForHR(hr); hr = videoWindow.SetWindowPosition(0, 0, iWidth, iHeight); DsError.ThrowExceptionForHR(hr); // Make the video window visible hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); } //Create the media control for controlling the graph _mediaCtrl = (IMediaControl)this._graphBuilder; } catch (Exception e) { TraceException.WriteLine(e); //Trace.WriteLine(e.Message); CloseInterfaces(); } finally { if (sink != null) { Marshal.ReleaseComObject(sink); sink = null; } if (mux != null) { Marshal.ReleaseComObject(mux); mux = null; } if (captureGraphBuilder != null) { Marshal.ReleaseComObject(captureGraphBuilder); captureGraphBuilder = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (theIPinTee != null) { Marshal.ReleaseComObject(theIPinTee); theIPinTee = null; } if (captureDevice != null) { Marshal.ReleaseComObject(captureDevice); captureDevice = null; } if (captureCompressor != null) { Marshal.ReleaseComObject(captureCompressor); captureCompressor = null; } if (theRenderer != null) { Marshal.ReleaseComObject(theRenderer); theRenderer = null; } } }