// Set the video window within the control specified by hControl private void ConfigVideoWindow(Control hControl) { int hr; IVideoWindow ivw = m_graphBuilder as IVideoWindow; // Set the parent hr = ivw.put_Owner(hControl.Handle); DsError.ThrowExceptionForHR(hr); // Turn off captions, etc hr = ivw.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings); DsError.ThrowExceptionForHR(hr); // Yes, make it visible hr = ivw.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); // Move to upper left corner Rectangle rc = hControl.ClientRectangle; hr = ivw.SetWindowPosition(0, 0, rc.Right, rc.Bottom); DsError.ThrowExceptionForHR(hr); }
/// <summary> /// Get the image from the Still pin. The returned image can turned into a bitmap with /// Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip); /// If the image is upside down, you can fix it with /// b.RotateFlip(RotateFlipType.RotateNoneFlipY); /// </summary> /// <returns>Returned pointer to be freed by caller with Marshal.FreeCoTaskMem</returns> public IntPtr Click() { int hr; // get ready to wait for new image m_PictureReady.Reset(); m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight); try { m_WantOne = true; // If we are using a still pin, ask for a picture if (m_VidControl != null) { // Tell the camera to send an image hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger); DsError.ThrowExceptionForHR(hr); } // Start waiting if (!m_PictureReady.WaitOne(9000, false)) { throw new Exception("Timeout waiting to get picture"); } } catch { Marshal.FreeCoTaskMem(m_ipBuffer); m_ipBuffer = IntPtr.Zero; throw; } // Got one return(m_ipBuffer); }
public int InitializeDevice(IntPtr dwUserID, ref VMR9AllocationInfo lpAllocInfo, ref int lpNumBuffers) { int width = 1; int height = 1; float fTU = 1.0f; float fTV = 1.0f; if (vmrSurfaceAllocatorNotify == null) { return(E_FAIL); } int hr = 0; try { IntPtr unmanagedDevice = OpenSebJ.eRender.device.GetObjectByValue(DxMagicNumber); IntPtr hMonitor = OpenSebJ.eRender.getMonitor(); hr = vmrSurfaceAllocatorNotify.SetD3DDevice(unmanagedDevice, hMonitor); DsError.ThrowExceptionForHR(hr); if (OpenSebJ.eRender.device.DeviceCaps.TextureCaps.SupportsPower2) { while (width < lpAllocInfo.dwWidth) { width = width << 1; } while (height < lpAllocInfo.dwHeight) { height = height << 1; } fTU = (float)(lpAllocInfo.dwWidth) / (float)(width); fTV = (float)(lpAllocInfo.dwHeight) / (float)(height); //scene.SetSrcRect(fTU, fTV); lpAllocInfo.dwWidth = width; lpAllocInfo.dwHeight = height; } // NOTE: // we need to make sure that we create textures because // surfaces can not be textured onto a primitive. lpAllocInfo.dwFlags |= VMR9SurfaceAllocationFlags.TextureSurface; DeleteSurfaces(); unmanagedSurfaces = new IntPtr[lpNumBuffers]; hr = vmrSurfaceAllocatorNotify.AllocateSurfaceHelper(ref lpAllocInfo, ref lpNumBuffers, unmanagedSurfaces); // If we couldn't create a texture surface and // the format is not an alpha format, // then we probably cannot create a texture. // So what we need to do is create a private texture // and copy the decoded images onto it. if (hr < 0) { DeleteSurfaces(); FourCC fcc = new FourCC("0000"); // is surface YUV ? if (lpAllocInfo.Format > fcc.ToInt32()) { // create the private texture privateTexture = new Texture( OpenSebJ.eRender.device, lpAllocInfo.dwWidth, lpAllocInfo.dwHeight, 1, Usage.RenderTarget, OpenSebJ.eRender.adapterInfo.CurrentDisplayMode.Format, Pool.Default ); privateSurface = privateTexture.GetSurfaceLevel(0); } lpAllocInfo.dwFlags &= ~VMR9SurfaceAllocationFlags.TextureSurface; lpAllocInfo.dwFlags |= VMR9SurfaceAllocationFlags.OffscreenSurface; unmanagedSurfaces = new IntPtr[lpNumBuffers]; hr = vmrSurfaceAllocatorNotify.AllocateSurfaceHelper(ref lpAllocInfo, ref lpNumBuffers, unmanagedSurfaces); if (hr < 0) { return(hr); } } else { surfaces = new Hashtable(unmanagedSurfaces.Length); textures = new Hashtable(unmanagedSurfaces.Length); for (int i = 0; i < lpNumBuffers; i++) { Surface surf = new Surface(unmanagedSurfaces[i]); Texture text = (Texture)surf.GetContainer(new Guid("85C31227-3DE5-4f00-9B3A-F11AC38C18B5")); surfaces.Add(unmanagedSurfaces[i], surf); textures.Add(unmanagedSurfaces[i], text); } } //return scene.Init(device); //System.Windows.Forms.MessageBox.Show(device.ToString()); //VideoRender.eRender.createDevice(device); return(0); } catch (DirectXException e) { return(e.ErrorCode); } catch { return(E_FAIL); } }
/// build the capture graph for grabber. private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pCaptureOut = null; IPin pSampleIn = null; IPin pRenderIn = null; // Get the graphbuilder object m_graphBuilder = new FilterGraph() as IFilterGraph2; try { #if DEBUG m_rot = new DsROTEntry(m_graphBuilder); #endif // add the video input device hr = m_graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); // Find the still pin m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); // Didn't find one. Is there a preview pin? if (m_pinStill == null) { m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0); } // Still haven't found one. Need to put a splitter in so we have // one stream to capture the bitmap from, and one to display. Ok, we // don't *have* to do it that way, but we are going to anyway. if (m_pinStill == null) { IPin pRaw = null; IPin pSmart = null; // There is no still pin m_VidControl = null; // Add a splitter IBaseFilter iSmartTee = (IBaseFilter) new SmartTee(); try { hr = m_graphBuilder.AddFilter(iSmartTee, "SmartTee"); DsError.ThrowExceptionForHR(hr); // Find the find the capture pin from the video device and the // input pin for the splitter, and connnect them pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0); hr = m_graphBuilder.Connect(pRaw, pSmart); DsError.ThrowExceptionForHR(hr); // Now set the capture and still pins (from the splitter) m_pinStill = DsFindPin.ByName(iSmartTee, "Preview"); pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture"); // If any of the default config items are set, perform the config // on the actual video device (rather than the splitter) if (iHeight + iWidth + iBPP > 0) { SetConfigParms(pRaw, iWidth, iHeight, iBPP); } } finally { if (pRaw != null) { Marshal.ReleaseComObject(pRaw); } if (pRaw != pSmart) { Marshal.ReleaseComObject(pSmart); } if (pRaw != iSmartTee) { Marshal.ReleaseComObject(iSmartTee); } } } else { // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); // If any of the default config items are set if (iHeight + iWidth + iBPP > 0) { SetConfigParms(m_pinStill, iWidth, iHeight, iBPP); } } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Configure the sample grabber IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Get the default video renderer IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter; hr = m_graphBuilder.AddFilter(pRenderer, "Renderer"); DsError.ThrowExceptionForHR(hr); pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0); // Add the sample grabber to the graph hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); if (m_VidControl == null) { // Connect the Still pin to the sample grabber hr = m_graphBuilder.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); // Connect the capture pin to the renderer hr = m_graphBuilder.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); } else { // Connect the capture pin to the renderer hr = m_graphBuilder.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); // Connect the Still pin to the sample grabber hr = m_graphBuilder.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); } // Learn the video properties SaveSizeInfo(sampGrabber); ConfigVideoWindow(hControl); // Start the graph IMediaControl mediaCtrl = m_graphBuilder as IMediaControl; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pCaptureOut != null) { Marshal.ReleaseComObject(pCaptureOut); pCaptureOut = null; } if (pRenderIn != null) { Marshal.ReleaseComObject(pRenderIn); pRenderIn = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = null; } } }