/// <summary> /// Resize video /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public int ResizeVideo(short width, short height) { TRACE(string.Format("ResizeVideo: {0}x{1}", width, height)); int hr = S_Ok; if (m_pVideoDisplay != null) { try { MFRect rcDest = new MFRect(); MFVideoNormalizedRect nRect = new MFVideoNormalizedRect(); nRect.left = 0; nRect.right = 1; nRect.top = 0; nRect.bottom = 1; rcDest.left = 0; rcDest.top = 0; rcDest.right = width; rcDest.bottom = height; m_pVideoDisplay.SetVideoPosition(nRect, rcDest); } catch (Exception e) { hr = Marshal.GetHRForException(e); } } return(hr); }
public HResult ResizeVideo(short width, short height) { HResult hr = HResult.S_OK; TRACE(string.Format("ResizeVideo: {0}x{1}", width, height)); if (m_pVideoDisplay != null) { try { MFRect rcDest = new MFRect(); MFVideoNormalizedRect nRect = new MFVideoNormalizedRect(); nRect.left = 0; nRect.right = 1; nRect.top = 0; nRect.bottom = 1; rcDest.left = 0; rcDest.top = 0; rcDest.right = width; rcDest.bottom = height; hr = m_pVideoDisplay.SetVideoPosition(nRect, rcDest); MFError.ThrowExceptionForHR(hr); } catch (Exception ce) { hr = (HResult)Marshal.GetHRForException(ce); } } return(hr); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Handle size changed events on this control /// </summary> /// <param name="errMsg">the error message</param> /// <param name="ex">the exception. Can be null</param> /// <history> /// 01 Nov 18 Cynic - Originally Written /// </history> private void ctlTantaEVRStreamDisplay_SizeChanged(object sender, EventArgs e) { LogMessage("ctlTantaEVRStreamDisplay_SizeChanged"); HResult hr; if (evrVideoDisplay == null) { return; } try { // we are going to use the size changed event to reset the size of the video on the display. // This is controlled by two windows. // The source window determines which portion of the video is displayed. It is specified in // normalized coordinates. In other words, a value between 0 and 1. To display the entire video // image we would set the source rectangle to { 0, 0, 1, 1}. To display the bottom right quarter // we would set it to { 0.75f, 0.75f, 1, 1}. The default source rectangle is { 0, 0, 1, 1}. // The destination rectangle defines a rectangle within the clipping window (the video surface) where the video appears. // in this control this is the surface of a child panel control. This values is specified in pixels, relative to // the client area of the control. To fill the entire control, set the destination rectangle to { 0, 0, width, height}, // where width and height are dimensions of the window client area. MFRect destinationRect = new MFRect(); MFVideoNormalizedRect sourceRect = new MFVideoNormalizedRect(); // populate a MFVideoNormalizedRect structure that specifies the source rectangle. // This parameter can be NULL. If this parameter is NULL, the source rectangle does not change. sourceRect.left = 0; sourceRect.right = 1; sourceRect.top = 0; sourceRect.bottom = 1; // populate the destination rectangle. This parameter can be NULL. If this parameter is NULL, // the destination rectangle does not change. destinationRect.left = 0; destinationRect.top = 0; destinationRect.right = panelDisplayPanel.Width; destinationRect.bottom = panelDisplayPanel.Height; // now set the video display coordinates hr = evrVideoDisplay.SetVideoPosition(sourceRect, destinationRect); if (hr != HResult.S_OK) { throw new Exception("ctlTantaEVRStreamDisplay_SizeChanged failed. Err=" + hr.ToString()); } } catch (Exception ex) { LogMessage("ctlTantaEVRStreamDisplay_SizeChanged failed exception happened. ex=" + ex.Message); // NotifyPlayerErrored(ex.Message, ex); } }
public bool HitTest(Point pt) { if (m_pMapper == null) { throw new COMException("null mapper", MFError.MF_E_INVALIDREQUEST); } int hr = 0; bool bHit = false; float x = -1, y = -1; // Normalize the coordinates (ie, calculate them as a percentage of // the video window's entire client area). Rectangle r = m_hwndVideo.ClientRectangle; MFRect rc = new MFRect(r.Left, r.Top, r.Right, r.Bottom); float x1 = (float)pt.X / rc.right; float y1 = (float)pt.Y / rc.bottom; // Map these coordinates into the coordinate space of the substream. m_pMapper.MapOutputCoordinateToInputStream( x1, y1, // Output coordinates 0, // Output stream (the mixer only has one) 1, // Input stream (1 = substream) out x, out y // Receives the normalized input coordinates. ); // If the mapped coordinates fall within [0-1], it's a hit. if (Succeeded(hr)) { bHit = ((x >= 0) && (x <= 1) && (y >= 0) && (y <= 1)); } if (bHit) { // Store the hit point. // We adjust the hit point by the substream scaling factor, so that the // hit point is now scaled to the reference stream. // Example: // - The hit point is (0.5,0.5), the center of the image. // - The scaling factor is 0.5, so the substream image is 50% the size // of the reference stream. // The adjusted hit point is (0.25,0.25) FROM the origin of the substream // rectangle but IN units of the reference stream. See DShowPlayer::Track // for where this is used. m_ptHitTrack = new PointF(x * m_fScale, y * m_fScale); } return(bHit ? true : false); }
//----------------------------------------------------------------------------- // SetDestinationRect // // Sets the region within the video window where the video is drawn. //----------------------------------------------------------------------------- public void SetDestinationRect(MFRect rcDest) { if (!m_rcDestRect.Equals(rcDest)) { lock (this) { m_rcDestRect.left = rcDest.left; m_rcDestRect.right = rcDest.right; m_rcDestRect.top = rcDest.top; m_rcDestRect.bottom = rcDest.bottom; UpdateDestRect(); } } }
void VideoInternalWindow_Resize(object sender, EventArgs e) { int top = 0; int left = 0; int width = this.Width; int height = this.Height; if (_detachedWindow != null) { top = _detachedWindow.ClientRectangle.Top; left = _detachedWindow.ClientRectangle.Left; width = _detachedWindow.ClientRectangle.Width; height = _detachedWindow.ClientRectangle.Height; } if (_isInit) { if (_vw != null) { _vw.SetWindowPosition(top, left, width, height); } else if (_evr != null & !_delayedInit) { try { MFRect rcDest = new MFRect(); MFVideoNormalizedRect nRect = new MFVideoNormalizedRect(); nRect.left = 0; nRect.right = 1; nRect.top = 0; nRect.bottom = 1; rcDest.left = top; rcDest.top = left; rcDest.right = width; rcDest.bottom = height; _evr.SetVideoPosition(nRect, rcDest); } catch (Exception ex) { #if DEBUG MessageBox.Show(ex.Message, "Failed to set EVR Window Position"); #endif } } } }
protected IDirect3DSurface9 m_pSurfaceRepaint; // Surface for repaint requests. #endregion public D3DPresentEngine() { m_iFrames = 0; m_hwnd = IntPtr.Zero; m_DeviceResetToken = 0; m_pD3D9 = null; m_pDevice = null; m_pDeviceManager = null; m_pSurfaceRepaint = null; m_rcDestRect = new MFRect(); m_DisplayMode = new D3DDISPLAYMODE(); InitializeD3D(); CreateD3DDevice(); }
//----------------------------------------------------------------------------- // UpdateDestRect // // Updates the target rectangle by clipping it to the video window's client // area. // // Called whenever the application sets the video window or the destination // rectangle. //----------------------------------------------------------------------------- protected void UpdateDestRect() { if (m_hwnd != IntPtr.Zero) { MediaFoundation.Misc.MFRect rcView = new MFRect(); GetClientRect(m_hwnd, rcView); // Clip the destination rectangle to the window's client area. if (m_rcDestRect.right > rcView.right) { m_rcDestRect.right = rcView.right; } if (m_rcDestRect.bottom > rcView.bottom) { m_rcDestRect.bottom = rcView.bottom; } } }
protected static extern bool GetClientRect( IntPtr hwnd, // handle to a window [Out] MFRect r // determine return value );
private void InitializeEVR(IBaseFilter pEVR, int dwStreams, out IMFVideoDisplayControl ppDisplay) { IMFVideoRenderer pRenderer; IMFVideoDisplayControl pDisplay; IEVRFilterConfig pConfig; IMFVideoPresenter pPresenter; // Before doing anything else, set any custom presenter or mixer. // Presenter? if (m_clsidPresenter != Guid.Empty) { Type type = Type.GetTypeFromCLSID(m_clsidPresenter); // An error here means that the custom presenter sample from // http://mfnet.sourceforge.net hasn't been installed or // registered. pPresenter = (IMFVideoPresenter)Activator.CreateInstance(type); try { pRenderer = (IMFVideoRenderer)pEVR; pRenderer.InitializeRenderer(null, pPresenter); } finally { //Marshal.ReleaseComObject(pPresenter); } } // Continue with the rest of the set-up. // Set the video window. object o; IMFGetService pGetService = null; pGetService = (IMFGetService)pEVR; pGetService.GetService(MFServices.MR_VIDEO_RENDER_SERVICE, typeof(IMFVideoDisplayControl).GUID, out o); try { pDisplay = (IMFVideoDisplayControl)o; } catch { Marshal.ReleaseComObject(o); throw; } try { // Set the number of streams. pDisplay.SetVideoWindow(m_hwndVideo.Handle); if (dwStreams > 1) { pConfig = (IEVRFilterConfig)pEVR; pConfig.SetNumberOfStreams(dwStreams); } // Set the display position to the entire window. Rectangle r = m_hwndVideo.ClientRectangle; MFRect rc = new MFRect(r.Left, r.Top, r.Right, r.Bottom); pDisplay.SetVideoPosition(null, rc); // Return the IMFVideoDisplayControl pointer to the caller. ppDisplay = pDisplay; } finally { //Marshal.ReleaseComObject(pDisplay); } m_pMixer = null; }
public void Track(Point pt) { if (m_pMixer == null || m_pMapper == null) { throw new COMException("null mixer or mapper", MFError.MF_E_INVALIDREQUEST); } Rectangle r = m_hwndVideo.ClientRectangle; MFRect rc = new MFRect(r.Left, r.Top, r.Right, r.Bottom); // x, y: Mouse coordinates, normalized relative to the composition rectangle. float x = (float)pt.X / rc.right; float y = (float)pt.Y / rc.bottom; // Map the mouse coordinates to the reference stream. m_pMapper.MapOutputCoordinateToInputStream( x, y, // Output coordinates 0, // Output stream (the mixer only has one) 0, // Input stream (0 = ref stream) out x, out y // Receives the normalized input coordinates. ); // Offset by the original hit point. x -= m_ptHitTrack.X; y -= m_ptHitTrack.Y; float max_offset = 1.0f - m_fScale; // Maximum left and top positions for the substream. MFVideoNormalizedRect nrcDest = new MFVideoNormalizedRect(); if (x < 0) { nrcDest.left = 0; nrcDest.right = m_fScale; } else if (x > max_offset) { nrcDest.right = 1; nrcDest.left = max_offset; } else { nrcDest.left = x; nrcDest.right = x + m_fScale; } if (y < 0) { nrcDest.top = 0; nrcDest.bottom = m_fScale; } else if (y > max_offset) { nrcDest.bottom = 1; nrcDest.top = max_offset; } else { nrcDest.top = y; nrcDest.bottom = y + m_fScale; } // Set the new position. m_pMixer.SetStreamOutputRect(1, nrcDest); }
private void SetVideoPositions() { var hiddenWindowContentSize = _hiddenWindow.ContentPixelSize; var screenWidth = Convert.ToInt32(Math.Round(hiddenWindowContentSize.Width)); var screenHeight = Convert.ToInt32(Math.Round(hiddenWindowContentSize.Height)); _logger.Info("window content width: {0}, window height: {1}", screenWidth, screenHeight); // Set the display position to the entire window. var rc = new MFRect(0, 0, screenWidth, screenHeight); _mPDisplay.SetVideoPosition(null, rc); //_mPDisplay.SetFullscreen(true); // Get Aspect Ratio int aspectX; int aspectY; decimal heightAsPercentOfWidth = 0; var basicVideo2 = (IBasicVideo2)m_graph; basicVideo2.GetPreferredAspectRatio(out aspectX, out aspectY); var sourceHeight = 0; var sourceWidth = 0; _basicVideo.GetVideoSize(out sourceWidth, out sourceHeight); if (aspectX == 0 || aspectY == 0 || sourceWidth > 0 || sourceHeight > 0) { aspectX = sourceWidth; aspectY = sourceHeight; } if (aspectX > 0 && aspectY > 0) { heightAsPercentOfWidth = decimal.Divide(aspectY, aspectX); } // Adjust Video Size var iAdjustedHeight = 0; if (aspectX > 0 && aspectY > 0) { var adjustedHeight = Convert.ToDouble(heightAsPercentOfWidth * screenWidth); iAdjustedHeight = Convert.ToInt32(Math.Round(adjustedHeight)); } //SET MADVR WINDOW TO FULL SCREEN AND SET POSITION if (screenHeight >= iAdjustedHeight && iAdjustedHeight > 0) { double totalMargin = (screenHeight - iAdjustedHeight); var topMargin = Convert.ToInt32(Math.Round(totalMargin / 2)); _basicVideo.SetDestinationPosition(0, topMargin, screenWidth, iAdjustedHeight); } else if (screenHeight < iAdjustedHeight && iAdjustedHeight > 0) { var adjustedWidth = Convert.ToDouble(screenHeight / heightAsPercentOfWidth); var iAdjustedWidth = Convert.ToInt32(Math.Round(adjustedWidth)); if (iAdjustedWidth == 1919) { iAdjustedWidth = 1920; } double totalMargin = (screenWidth - iAdjustedWidth); var leftMargin = Convert.ToInt32(Math.Round(totalMargin / 2)); _basicVideo.SetDestinationPosition(leftMargin, 0, iAdjustedWidth, screenHeight); } _videoWindow.SetWindowPosition(0, 0, screenWidth, screenHeight); }