示例#1
0
 public static void FreePinInfo(PinInfo pinInfo)
 {
     if (pinInfo.filter != null)
     {
         Marshal.ReleaseComObject(pinInfo.filter);
         pinInfo.filter = null;
     }
 }
示例#2
0
		/// <summary> Retrieve the friendly name of a connectorType. </summary>
		private string getName(IPin pin)
		{
			string s = "Unknown pin";
			PinInfo pinInfo = new PinInfo();

			// Direction matches, so add pin name to listbox
			int hr = pin.QueryPinInfo(out pinInfo);
			Marshal.ThrowExceptionForHR(hr);
			s = pinInfo.name + "";

			// The pininfo structure contains a reference to an IBaseFilter,
			// so you must release its reference to prevent resource a leak.
			if (pinInfo.filter != null)
				Marshal.ReleaseComObject(pinInfo.filter); 
			pinInfo.filter = null;

			return (s);
		}
            int IPin.QueryPinInfo(out PinInfo pInfo)
            {
                pInfo.dir = PinDirection.Output;
                pInfo.filter = filter;
                pInfo.name = name;

                return S_OK;
            }
示例#4
0
文件: tuner.cs 项目: ssnake/utuner
        /// <summary>
        ///  Removes all filters downstream from a filter from the graph.
        ///  This is called only by derenderGraph() to remove everything
        ///  from the graph except the devices and compressors. The parameter
        ///  "removeFirstFilter" is used to keep a compressor (that should
        ///  be immediately downstream of the device) if one is begin used.
        /// </summary>
        private void removeDownstream( IBaseFilter filter, bool removeFirstFilter )
        {
            if (filter == null)
                        return;

                    // Get a pin enumerator off the filter
                    IEnumPins pinEnum;
                    int hr = filter.EnumPins( out pinEnum );
                    pinEnum.Reset();
                    if( (hr == 0) && (pinEnum != null) )
                    {
                        // Loop through each pin
                        IPin[] pins = new IPin[1];

                        IntPtr f = IntPtr.Zero;

                        do
                        {
                            // Get the next pin

                            hr = pinEnum.Next(1, pins, f);

                            if( (hr == 0) && (pins[0] != null) )
                            {

                                // Get the pin it is connected to
                                IPin pinTo = null;
                                pins[0].ConnectedTo( out pinTo );
                                if ( pinTo != null )
                                {
                                    // Is this an input pin?
                                    PinInfo info = new PinInfo();
                                    hr = pinTo.QueryPinInfo( out info );
                                    if( (hr == 0) && (info.dir == (PinDirection.Input)) )
                                    {
                                        // Recurse down this branch
                                        removeDownstream( info.filter, true );

                                        // Disconnect
                                        graphBuilder.Disconnect( pinTo );
                                        graphBuilder.Disconnect( pins[0] );

                                        // Remove this filter
                                        // but don't remove the video or audio compressors
                                     //   if ( ( info.filter != videoCompressorFilter ) &&
                                     //        ( info.filter != audioCompressorFilter ) )
                                            graphBuilder.RemoveFilter( info.filter );
                                    }
                                    Marshal.ReleaseComObject( info.filter );
                                    Marshal.ReleaseComObject( pinTo );
                                }
                                Marshal.ReleaseComObject( pins[0] );
                            }
                        }
                        while( hr == 0 );

                        Marshal.ReleaseComObject( pinEnum ); pinEnum = null;
                    }
        }
示例#5
0
 protected void RebuildRelease(PinInfo pInfo, FilterInfo fInfo, IPin pinTo, IPin pPin)
 {
   DsUtils.FreePinInfo(pInfo);
   DirectShowUtil.ReleaseComObject(fInfo.pGraph);
   DirectShowUtil.ReleaseComObject(pinTo);
   pinTo = null;
   DirectShowUtil.ReleaseComObject(pPin);
   pPin = null;
 }
    public static bool RenderOutputPins(IGraphBuilder graphBuilder, IBaseFilter filter, int maxPinsToRender,
                                        bool tryAllFilters)
    {
      int pinsRendered = 0;
      bool bAllConnected = true;
      IEnumPins pinEnum;
      FilterInfo info;
      filter.QueryFilterInfo(out info);
      ReleaseComObject(info.pGraph);

      int hr = filter.EnumPins(out pinEnum);
      if ((hr == 0) && (pinEnum != null))
      {
        Log.Info("got pins");
        pinEnum.Reset();
        IPin[] pins = new IPin[1];
        int iFetched;
        int iPinNo = 0;
        do
        {
          // Get the next pin
          //Log.Info("  get pin:{0}",iPinNo);
          iPinNo++;
          hr = pinEnum.Next(1, pins, out iFetched);
          if (hr == 0)
          {
            if (iFetched == 1 && pins[0] != null)
            {
              PinInfo pinInfo = new PinInfo();
              hr = pins[0].QueryPinInfo(out pinInfo);
              DsUtils.FreePinInfo(pinInfo);
              if (hr == 0)
              {
                Log.Info("  got pin#{0}:{1}", iPinNo - 1, pinInfo.name);
              }
              else
              {
                Log.Info("  got pin:?");
              }
              PinDirection pinDir;
              pins[0].QueryDirection(out pinDir);
              if (pinDir == PinDirection.Output)
              {
                IPin pConnectPin = null;
                hr = pins[0].ConnectedTo(out pConnectPin);
                if (hr != 0 || pConnectPin == null)
                {
                  hr = 0;
                  if (TryConnect(graphBuilder, info.achName, pins[0], tryAllFilters))
                    //if ((hr=graphBuilder.Render(pins[0])) == 0)
                  {
                    Log.Info("  render ok");
                  }
                  else
                  {
                    Log.Error(" render {0} failed:{1:x}, trying alternative graph builder", pinInfo.name, hr);

                    if ((hr = graphBuilder.Render(pins[0])) == 0)
                    {
                      Log.Info(" render ok");
                    }
                    else
                    {
                      Log.Error(" render failed:{0:x}", hr);
                      bAllConnected = false;
                    }
                  }
                  pinsRendered++;
                }
                if (pConnectPin != null)
                {
                  ReleaseComObject(pConnectPin);
                }
                pConnectPin = null;
                //else Log.Info("pin is already connected");
              }
              ReleaseComObject(pins[0]);
            }
            else
            {
              iFetched = 0;
              Log.Info("no pins?");
              break;
            }
          }
          else
          {
            iFetched = 0;
          }
        } while (iFetched == 1 && pinsRendered < maxPinsToRender && bAllConnected);
        ReleaseComObject(pinEnum);
      }
      return bAllConnected;
    }
 public static bool ReConnectAll(IGraphBuilder graphBuilder, IBaseFilter filter)
 {
   bool bAllConnected = true;
   IEnumPins pinEnum;
   FilterInfo info;
   filter.QueryFilterInfo(out info);
   ReleaseComObject(info.pGraph);
   int hr = filter.EnumPins(out pinEnum);
   if ((hr == 0) && (pinEnum != null))
   {
     Log.Info("got pins");
     pinEnum.Reset();
     IPin[] pins = new IPin[1];
     int iFetched;
     int iPinNo = 0;
     do
     {
       // Get the next pin
       //Log.Info("  get pin:{0}",iPinNo);
       iPinNo++;
       hr = pinEnum.Next(1, pins, out iFetched);
       if (hr == 0)
       {
         if (iFetched == 1 && pins[0] != null)
         {
           PinInfo pinInfo = new PinInfo();
           hr = pins[0].QueryPinInfo(out pinInfo);
           DsUtils.FreePinInfo(pinInfo);
           if (hr == 0)
           {
             Log.Info("  got pin#{0}:{1}", iPinNo - 1, pinInfo.name);
           }
           else
           {
             Log.Info("  got pin:?");
           }
           PinDirection pinDir;
           pins[0].QueryDirection(out pinDir);
           if (pinDir == PinDirection.Output)
           {
             IPin other;
             hr = pins[0].ConnectedTo(out other);
             if (hr == 0 && other != null)
             {
               Log.Info("Reconnecting {0}:{1}", info.achName, pinInfo.name);
               hr = graphBuilder.Reconnect(pins[0]);
               if (hr != 0)
               {
                 Log.Warn("Reconnect failed: {0}:{1}, code: 0x{2:x}", info.achName, pinInfo.name, hr);
               }
             }
           }
           ReleaseComObject(pins[0]);
         }
         else
         {
           iFetched = 0;
           Log.Info("no pins?");
           break;
         }
       }
       else
       {
         iFetched = 0;
       }
     } while (iFetched == 1);
     ReleaseComObject(pinEnum);
   }
   return bAllConnected;
 }
 public static void DisconnectOutputPins(IGraphBuilder graphBuilder, IBaseFilter filter)
 {
   IEnumPins pinEnum;
   int hr = filter.EnumPins(out pinEnum);
   if ((hr == 0) && (pinEnum != null))
   {
     //Log.Info("got pins");
     pinEnum.Reset();
     IPin[] pins = new IPin[1];
     int iFetched;
     int iPinNo = 0;
     do
     {
       // Get the next pin
       //Log.Info("  get pin:{0}",iPinNo);
       iPinNo++;
       hr = pinEnum.Next(1, pins, out iFetched);
       if (hr == 0)
       {
         if (iFetched == 1 && pins[0] != null)
         {
           //Log.Info("  find pin info");
           PinInfo pinInfo = new PinInfo();
           hr = pins[0].QueryPinInfo(out pinInfo);
           DsUtils.FreePinInfo(pinInfo);
           if (hr >= 0)
           {
             Log.Info("  got pin#{0}:{1}", iPinNo - 1, pinInfo.name);
           }
           else
           {
             Log.Info("  got pin:?");
           }
           PinDirection pinDir;
           pins[0].QueryDirection(out pinDir);
           if (pinDir == PinDirection.Output)
           {
             //Log.Info("  is output");
             IPin pConnectPin = null;
             hr = pins[0].ConnectedTo(out pConnectPin);
             if (hr == 0 && pConnectPin != null)
             {
               //Log.Info("  pin is connected ");
               hr = pins[0].Disconnect();
               if (hr == 0)
               {
                 Log.Info("  disconnected ok");
               }
               else
               {
                 Log.Error("  disconnected failed ({0:x})", hr);
               }
               ReleaseComObject(pConnectPin);
               pConnectPin = null;
             }
             //else Log.Info("pin is already connected");
           }
           ReleaseComObject(pins[0]);
         }
         else
         {
           iFetched = 0;
           Log.Info("no pins?");
           break;
         }
       }
       else
       {
         iFetched = 0;
       }
     } while (iFetched == 1);
     ReleaseComObject(pinEnum);
   }
 }
 public int QueryPinInfo(out PinInfo pInfo)
 {
     Monitor.Enter(this);
     pInfo.name = PIN_ID;
     pInfo.dir = PinDirection.Output;
     pInfo.filter = parent;
     Monitor.Exit(this);
     return S_OK;
 }
        public int Connect(IPin pReceivePin, AMMediaType pmt)
        {
            Monitor.Enter(this);
            int hr = S_OK;
            pin = null;
            pintype = null;
            allocator = null;
            string id = "Unnamed pin";
            pReceivePin.QueryId(out id);
            PinInfo pi = new PinInfo();
            hr = pReceivePin.QueryPinInfo(out pi);
            if (hr == S_OK)
            {
                FilterInfo fi = new FilterInfo();
                hr = pi.filter.QueryFilterInfo(out fi);
                if (hr == S_OK)
                {
                    id += (" (" + fi.achName);
                }
                Guid guid;
                hr = pi.filter.GetClassID(out guid);
                if (hr == S_OK)
                {
                    id += (", " + guid.ToString());
                }
                id += ")";
            }
            try
            {
                AMMediaType amt = null;
                if (pmt != null)
                {
                    amt = pmt;
                }
                else
            #if false
                {
                    IEnumMediaTypes ie;
                    hr = pReceivePin.EnumMediaTypes(out ie);
                    int fetched;
                    int alloc = Marshal.SizeOf(typeof(AMMediaType));
                    IntPtr mtypePtr = Marshal.AllocCoTaskMem(alloc);
                    while (ie.Next(1, mtypePtr, out fetched) == S_OK)
                    {
                        amt = new AMMediaType();
                        Marshal.PtrToStructure(mtypePtr, amt);
                        hr = pReceivePin.QueryAccept(amt);
                        if (hr == S_OK)
                        {
                            break;
                        }
                        DsUtils.FreeAMMediaType(amt);
                        amt = null;
                    }
                    if (fetched == 0)
                    {
                        amt = null;
                    }
                    Marshal.FreeCoTaskMem(mtypePtr);
                }
                if (amt == null)
            #endif
                {
                    amt = mediatype;
                }
                hr = pReceivePin.QueryAccept(amt);
                if (hr == S_FALSE)
                {
                    log.InfoFormat("No media type for pin '{0}'", id);
                    Monitor.Exit(this);
                    return VFW_E_NO_ACCEPTABLE_TYPES;
                }

                hr = pReceivePin.ReceiveConnection(this, amt);
                if (hr == VFW_E_TYPE_NOT_ACCEPTED)
                {
                    log.InfoFormat("No connection to pin '{0}'", id);
                    Monitor.Exit(this);
                    return VFW_E_NO_ACCEPTABLE_TYPES;
                }
                DsError.ThrowExceptionForHR(hr);

                pin = pReceivePin;
                pintype = amt;
            }
            catch (Exception e)
            {
                LogUtil.ExceptionLog.ErrorFormat("Caught exception in connect ({0}): {1}{2}", id, e.Message, e.StackTrace);
                pin = null;
                pintype = null;
                allocator = null;
                Monitor.Exit(this);
                return VFW_E_NO_TRANSPORT;
            }
            Monitor.Exit(this);
            log.InfoFormat("Connected to pin '{0}'", id);
            return S_OK;
        }
示例#11
0
        private void UnRender(IPin pinOutOrigin)
        {
            int hr;
            //hr = pinOutOrigin.Disconnect();
            IPin pinOutEnd = null;
            hr = pinOutOrigin.ConnectedTo(out pinOutEnd);
            if (pinOutEnd != null)
            {
                try
                {
                    PinInfo pInfo = new PinInfo();
                    hr = pinOutEnd.QueryPinInfo(out pInfo);
                    if (hr >= 0)
                    {
                        if (pInfo.filter != null)
                        {
                            try
                            {
                                IEnumPins ppEnum;
                                hr = pInfo.filter.EnumPins(out ppEnum);
                                if (hr >= 0)
                                {
                                    try
                                    {
                                        // Walk the pins looking for a match
                                        IPin[] pPins = new IPin[1];
                                        //22 int lFetched;
                                        //22 while ((ppEnum.Next(1, pPins, out lFetched) >= 0) && (lFetched == 1))
                                        while (ppEnum.Next(1, pPins, IntPtr.Zero) >= 0)
                                        {
                                            try
                                            {
                                                // Read the direction
                                                PinDirection ppindir;
                                                hr = pPins[0].QueryDirection(out ppindir);
                                                if (hr >= 0)
                                                {
                                                    // Is it the right direction?
                                                    if (ppindir == PinDirection.Output)
                                                    {
                                                        if (pPins[0] != null)
                                                        {
                                                            UnRender(pPins[0]);
                                                        }
                                                    }
                                                }
                                            }
                                            finally
                                            {
                                                Marshal.ReleaseComObject(pPins[0]);
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        Marshal.ReleaseComObject(ppEnum);
                                    }
                                }

                                hr = graphBuilder.RemoveFilter(pInfo.filter);
                            }
                            finally
                            {
                                Marshal.ReleaseComObject(pInfo.filter);
                            }
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(pinOutEnd);
                }
            }
        }