Example #1
0
    // Retrieve capabilities of a video device
    internal VideoCapabilities(IAMStreamConfig videoStreamConfig, int index)
    {
      AMMediaType mediaType = null;
      VideoStreamConfigCaps caps = new VideoStreamConfigCaps();

      try
      {
        // retrieve capabilities struct at the specified index
        int hr = videoStreamConfig.GetStreamCaps(index, out mediaType, caps);

        if (hr != 0)
          Marshal.ThrowExceptionForHR(hr);

        // extract info
        FrameSize = caps.InputSize;
        FrameRate = (int)(10000000 / caps.MinFrameInterval);
      }
      finally
      {
        if (mediaType != null)
          mediaType.Dispose();
      }
    }
    // Set frame's size and rate for the specified stream configuration
    private void SetFrameSizeAndRate(IAMStreamConfig streamConfig, Size size, int frameRate)
    {
      bool sizeSet = false;
      AMMediaType mediaType;

      // get current format
      streamConfig.GetFormat(out mediaType);

      // change frame size if required
      if ((size.Width != 0) && (size.Height != 0))
      {
        // iterate through device's capabilities to find mediaType for desired resolution
        int capabilitiesCount = 0, capabilitySize = 0;
        AMMediaType newMediaType = null;
        VideoStreamConfigCaps caps = new VideoStreamConfigCaps();

        streamConfig.GetNumberOfCapabilities(out capabilitiesCount, out capabilitySize);

        for (int i = 0; i < capabilitiesCount; i++)
        {
          if (streamConfig.GetStreamCaps(i, out newMediaType, caps) == 0)
          {
            if (caps.InputSize == size)
            {
              mediaType.Dispose();
              mediaType = newMediaType;
              sizeSet = true;
              break;
            }
            else
            {
              newMediaType.Dispose();
            }
          }
        }
      }

      VideoInfoHeader infoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.FormatPtr, typeof(VideoInfoHeader));

      // try changing size manually if failed finding mediaType before
      if ((size.Width != 0) && (size.Height != 0) && (!sizeSet))
      {
        infoHeader.BmiHeader.Width = size.Width;
        infoHeader.BmiHeader.Height = size.Height;
      }
      // change frame rate if required
      if (frameRate != 0)
      {
        infoHeader.AverageTimePerFrame = 10000000 / frameRate;
      }

      // copy the media structure back
      Marshal.StructureToPtr(infoHeader, mediaType.FormatPtr, false);

      // set the new format
      streamConfig.SetFormat(mediaType);

      mediaType.Dispose();
    }