DirectShowMonikerMBS.BindBaseFilter Causes USB Camera to Un-Mount

I’ve adapted the code in the “PlayCap with frame grabber event” MBS example program to capture data from multiple cameras, basically by changing all of the various DirectShow properties to arrays and iterating through the setup for each camera. It works fine with one camera or with two different cameras, i.e. the built-in webcam and one of my external USB cams, but when I try to use two identical external webcams (same VID/PID), the second one unmounts (I hear the Windows descending tone sound) at the moment the call to DirectShowMonikerMBS.BindBaseFilter occurs after one of them has already been added to the system. It re-mounts a couple of seconds later, but that obviously doesn’t help me. I know that it’s this call that causes the un-mounting because it happens exactly when I single-step over it.

This is my “FindCaptureDevices” method where the problem occurs:

// Create an enumerator for the video capture devices
dim devenum as new DirectShowEnumMonikerMBS(DirectShowEnumMonikerMBS.CLSID_VideoInputDeviceCategory)
if devenum.Handle = 0 then
  MsgBox "No video capture device found."
end if

dim dev as DirectShowMonikerMBS = devenum.NextObject

// Loop through devices, adding a DirectShowBaseFilterMBS for each device to our srcfilters array
While dev <> nil
   // Bind Moniker to a filter object
      dim f as DirectShowBaseFilterMBS = dev.BindBaseFilter // ******* This is where the device unmounts
      srcfilters.AddRow f
  dev = devenum.NextObject

and here’s the setup loop that calls it:

FindCaptureDevices // This loads the srcfilters array

For CamNum as Integer = 0 to srcfilters.LastRowIndex
  // Create the filter graph
  graphs.AddRow new DirectShowGraphBuilderMBS
  // Create the capture graph builder
  Captures.AddRow new DirectShowCaptureGraphBuilderMBS
  // Obtain interfaces for media control and Video Window
  MC.AddRow Graphs(CamNum).MediaControl
  VideoWindows.AddRow Graphs(CamNum).VideoWindow
  'MEE = Graph.MediaEventEx // Doesn't seem necessary?
  // Attach the filter graph to the capture graph
  grabbers.AddRow new MyDirectShowSampleGrabberMBS
  grabberFilter = grabbers(CamNum).BaseFilter
  // Add Capture filter to our graph.
  Graphs(CamNum).AddFilter srcfilters(CamNum), "Video Capture"
  Graphs(CamNum).AddFilter grabberFilter, "Grabber"
  // Render the preview pin on the video capture filter
  Captures(CamNum).RenderStream(DirectShowPinMBS.PIN_CATEGORY_PREVIEW, Captures(CamNum).MEDIATYPE_Video, srcfilters(CamNum), grabberFilter)
  // Set video window style and position
  // Start previewing video data
  if MC(CamNum).Lasterror = 1 then // not yet ready, try again
  end if
  // Visibility gets overridden by mc.Run, have to set it afterwards
  VideoWindows(CamNum).Visible = TabPanel1.SelectedPanelIndex = 1

Any suggestions, @Christian_Schmitz ?

Seems like maybe the instantiation of Graph.MediaEventEX is required after all - I added that back in and now it works. One shouldn’t mess with stuff one doesn’t understand :slight_smile:

I am earning my money that way. :joy:

1 Like

Happening again, so Graph.MediaEventEX seems to not be the fix. Will try reordering my capture setup steps.

I’m now thinking this is a USB hardware issue, probably power-related. The problem doesn’t occur when the cameras are plugged directly into my big office PC, and it doesn’t occur on my laptop when they’re connected via a powered hub. The problem happens when they’re plugged into the two ports on my Asus laptop, and maybe they’re power-starved.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.