Exemple #1
0
        /// <summary>
        /// Called from RenderWindow to add the renderer to the graph, create a sample grabber, add it
        /// to the graph and connect it all up
        /// </summary>
        /// <param name="icgb">ICaptureGraphBuilder2 to use</param>
        /// <param name="pCallback">ICaptureGraphBuilder2 to use</param>
        /// <param name="sType">string to use in creating filter graph object descriptions</param>
        /// <param name="pPin">Pin to connect from</param>
        /// <param name="ibfRenderer">Renderer to add</param>
        private void RenderWindowHelper(ICaptureGraphBuilder2 icgb, AVCallback pCallback, string sType, IPin pPin, IBaseFilter ibfRenderer)
        {
            int hr;

            // Add the renderer to the graph
            hr = m_pGraph.AddFilter(ibfRenderer, sType + " Renderer");
            DESError.ThrowExceptionForHR(hr);

            // Do everything else
            RenderHelper(icgb, pCallback, sType, pPin, null, ibfRenderer);
        }
Exemple #2
0
        /// <summary>
        /// Common routine used by RenderTo*
        /// </summary>
        /// <param name="icgb">ICaptureGraphBuilder2 to use</param>
        /// <param name="pCallback">Callback to use (or null)</param>
        /// <param name="sType">string to use in creating filter graph object descriptions</param>
        /// <param name="pPin">Pin to connect from</param>
        /// <param name="ibfCompressor">Compressor to use, or null for none</param>
        /// <param name="pOutput">Endpoint (renderer or file writer) to connect to</param>
        private void RenderHelper(ICaptureGraphBuilder2 icgb, AVCallback pCallback, string sType, IPin pPin, IBaseFilter ibfCompressor, IBaseFilter pOutput)
        {
            int         hr;
            IBaseFilter ibfSampleGrabber = null;

            try
            {
                // If no callback was provided, don't create a samplegrabber
                if (pCallback != null)
                {
                    ISampleGrabber isg = (ISampleGrabber) new SampleGrabber();
                    ibfSampleGrabber = (IBaseFilter)isg;

                    hr = isg.SetCallback(pCallback, 1);
                    DESError.ThrowExceptionForHR(hr);

                    hr = m_pGraph.AddFilter(ibfSampleGrabber, sType + " sample grabber");
                    DESError.ThrowExceptionForHR(hr);
                }

                // If a compressor was provided, add it to the graph and connect it up
                if (ibfCompressor != null)
                {
                    // Connect the pin.
                    hr = m_pGraph.AddFilter(ibfCompressor, sType + " Compressor");
                    DESError.ThrowExceptionForHR(hr);

                    hr = icgb.RenderStream(null, null, pPin, ibfSampleGrabber, ibfCompressor);
                    DESError.ThrowExceptionForHR(hr);

                    // Connect the pin.
                    hr = icgb.RenderStream(null, null, ibfCompressor, null, pOutput);
                    DESError.ThrowExceptionForHR(hr);
                }
                else
                {
                    // Just connect the SampleGrabber (if any)
                    hr = icgb.RenderStream(null, null, pPin, ibfSampleGrabber, pOutput);
                    DESError.ThrowExceptionForHR(hr);
                }
            }
            finally
            {
                if (ibfSampleGrabber != null)
                {
                    Marshal.ReleaseComObject(ibfSampleGrabber);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Configure the graph to output the results to an AVI file.
        /// </summary>
        /// <param name="sOutputFile">File name for output (must not be null)</param>
        /// <param name="ibfVideoCompressor">IBaseFilter of a video compressor to use (or null for none).
        /// Note that <b><i>no</i></b> configuration of this compressor is done by this method.  It merely adds it
        /// to the graph in the appropriate place.  Also, the pointer is not invalidated, so any configuration
        /// of the compressor that needs to be done after being added to the graph can still be done.</param>
        /// <param name="ibfAudioCompressor">IBaseFilter of an audio compressor to use (or null for none).
        /// Note that <b><i>no</i></b> configuration of this compressor is done by this method.  It merely adds it
        /// to the graph in the appropriate place.  Also, the pointer is not invalidated, so any configuration
        /// of the compressor that needs to be done after being added to the graph can still be done.</param>
        /// <param name="pVideoCallback">Callback routine to be called for each video frame or null for no callback</param>
        /// <param name="pAudioCallback">Callback routine to be called for each audio frame or null for no callback</param>
        /// <remarks>
        /// The callback routines are invoked once for each sample.  This allows for additional processing to
        /// be performed on the video or audio buffers.
        /// </remarks>
        public void RenderToAVI(
            string sOutputFile,
            IBaseFilter ibfVideoCompressor,
            IBaseFilter ibfAudioCompressor,
            IDESCombineCB pVideoCallback,
            IDESCombineCB pAudioCallback)
        {
            int  hr;
            IPin pPin;

            if (sOutputFile == null)
            {
                throw new Exception("Output file name cannot be null");
            }

            // Perform initialization common to all render routines
            RenderCommon();

            // Contains useful routines for creating the graph
            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            try
            {
                hr = icgb.SetFiltergraph(m_pGraph);
                DESError.ThrowExceptionForHR(hr);

                // Create the file writer
                IBaseFilter     pMux;
                IFileSinkFilter pFilter;
                hr = icgb.SetOutputFileName(MediaSubType.Avi, sOutputFile, out pMux, out pFilter);
                DESError.ThrowExceptionForHR(hr);

                // We don't need this, so let it go
                Marshal.ReleaseComObject(pFilter);

                try
                {
                    int NumGroups;
                    hr = m_pTimeline.GetGroupCount(out NumGroups);
                    DESError.ThrowExceptionForHR(hr);

                    // Walk the groups.  For this class, there is one group that
                    // contains all the video, and a second group for the audio.
                    for (int i = 0; i < NumGroups; i++)
                    {
                        IAMTimelineObj pGroup;

                        hr = m_pTimeline.GetGroup(out pGroup, i);
                        DESError.ThrowExceptionForHR(hr);

                        try
                        {
                            // Inform the graph we will be writing to disk (rather than previewing)
                            IAMTimelineGroup pTLGroup = (IAMTimelineGroup)pGroup;
                            hr = pTLGroup.SetPreviewMode(false);
                            DESError.ThrowExceptionForHR(hr);
                        }
                        finally
                        {
                            Marshal.ReleaseComObject(pGroup);
                        }

                        // Get the IPin for the current group
                        hr = m_pRenderEngine.GetGroupOutputPin(i, out pPin);
                        DESError.ThrowExceptionForHR(hr);

                        try
                        {
                            if (IsVideo(pPin))
                            {
                                // Create a sample grabber, add it to the graph and connect it all up
                                AVCallback mcb = new AVCallback(m_Video, pVideoCallback, (IMediaEventSink)m_pGraph, EC_VideoFileComplete);
                                RenderHelper(icgb, mcb, "Video", pPin, ibfVideoCompressor, pMux);
                            }
                            else
                            {
                                // Create a sample grabber, add it to the graph and connect it all up
                                AVCallback mcb = new AVCallback(m_Audio, pAudioCallback, (IMediaEventSink)m_pGraph, EC_AudioFileComplete);
                                RenderHelper(icgb, mcb, "Audio", pPin, ibfAudioCompressor, pMux);
                            }
                        }
                        finally
                        {
                            Marshal.ReleaseComObject(pPin);
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(pMux);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(icgb);
            }
        }
Exemple #4
0
        /// <summary>
        /// Configure the graph to output the results to a video window.
        /// </summary>
        /// <remarks>
        /// The callback routines are invoked once for each sample.  This allows for additional processing to
        /// be performed on the video or audio buffers.
        /// </remarks>
        /// <param name="hWnd">Window handle to render to, or IntPtr.Zero to render to its own window</param>
        /// <param name="pVideoCallback">Callback routine to be called for each video frame or null for no callback</param>
        /// <param name="pAudioCallback">Callback routine to be called for each audio frame or null for no callback</param>
        /// <param name="video">Render only video</param>
        /// <param name="audio">Render only audio</param>
        private void RenderToWindow(IntPtr hWnd, IDESCombineCB pVideoCallback, IDESCombineCB pAudioCallback, bool video, bool audio)
        {
            int            hr;
            IPin           pPin;
            IVideoWindow   pVidWindow;
            IAMTimelineObj pGroup;

            // Perform initialization common to all render routines
            RenderCommon();

            // Contains useful routines for creating the graph
            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            try
            {
                hr = icgb.SetFiltergraph(m_pGraph);
                DESError.ThrowExceptionForHR(hr);

                int NumGroups;
                hr = m_pTimeline.GetGroupCount(out NumGroups);
                DESError.ThrowExceptionForHR(hr);

                // Walk the groups.  For DESCombine, there is one group that
                // contains all the video, and a second group for the audio.
                for (int i = 0; i < NumGroups; i++)
                {
                    hr = m_pTimeline.GetGroup(out pGroup, i);
                    DESError.ThrowExceptionForHR(hr);

                    try
                    {
                        // Inform the graph we will be previewing (rather than writing to disk)
                        IAMTimelineGroup pTLGroup = (IAMTimelineGroup)pGroup;
                        hr = pTLGroup.SetPreviewMode(true);
                        DESError.ThrowExceptionForHR(hr);
                    }
                    finally
                    {
                        // Release the group
                        Marshal.ReleaseComObject(pGroup);
                    }

                    // Get the IPin for the current group
                    hr = m_pRenderEngine.GetGroupOutputPin(i, out pPin);
                    DESError.ThrowExceptionForHR(hr);

                    try
                    {
                        // If this is the video pin
                        if (video && IsVideo(pPin))
                        {
                            // Get a video renderer
                            IBaseFilter ibfVideoRenderer = (IBaseFilter) new VideoRenderer();

                            try
                            {
                                // Create a sample grabber, add it to the graph and connect it all up
                                AVCallback mcb = new AVCallback(m_Video, pVideoCallback, (IMediaEventSink)m_pGraph, EC_VideoFileComplete);
                                RenderWindowHelper(icgb, mcb, "Video", pPin, ibfVideoRenderer);
                            }
                            finally
                            {
                                Marshal.ReleaseComObject(ibfVideoRenderer);
                            }
                        }
                        else if (audio)
                        {
                            // Get an audio renderer
                            IBaseFilter ibfAudioRenderer = (IBaseFilter) new AudioRender();

                            try
                            {
                                // Create a sample grabber, add it to the graph and connect it all up
                                AVCallback mcb = new AVCallback(m_Audio, pAudioCallback, (IMediaEventSink)m_pGraph, EC_AudioFileComplete);
                                RenderWindowHelper(icgb, mcb, "Audio", pPin, ibfAudioRenderer);
                            }
                            finally
                            {
                                Marshal.ReleaseComObject(ibfAudioRenderer);
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(pPin);
                    }
                }

                if (video)
                {
                    // Configure the video window
                    pVidWindow = (IVideoWindow)m_pGraph;

                    // If a window handle was supplied, use it
                    if (hWnd != IntPtr.Zero)
                    {
                        hr = pVidWindow.put_Owner(hWnd);
                        DESError.ThrowExceptionForHR(hr);
                    }
                    else
                    {
                        // Use our own window

                        hr = pVidWindow.put_Caption("Video Rendering Window");
                        DESError.ThrowExceptionForHR(hr);

                        // since no user interaction is allowed, remove
                        // system menu and maximize/minimize buttons
                        WindowStyle lStyle = 0;
                        hr = pVidWindow.get_WindowStyle(out lStyle);
                        DESError.ThrowExceptionForHR(hr);

                        lStyle &= ~(WindowStyle.MinimizeBox | WindowStyle.MaximizeBox | WindowStyle.SysMenu);
                        hr      = pVidWindow.put_WindowStyle(lStyle);
                        DESError.ThrowExceptionForHR(hr);
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(icgb);
            }
        }
Exemple #5
0
    /// <summary>
    /// Configure the graph to output the results to an AVI file.
    /// </summary>
    /// <param name="sOutputFile">File name for output (must not be null)</param>
    /// <param name="ibfVideoCompressor">IBaseFilter of a video compressor to use (or null for none).  
    /// Note that <b><i>no</i></b> configuration of this compressor is done by this method.  It merely adds it
    /// to the graph in the appropriate place.  Also, the pointer is not invalidated, so any configuration
    /// of the compressor that needs to be done after being added to the graph can still be done.</param>
    /// <param name="ibfAudioCompressor">IBaseFilter of an audio compressor to use (or null for none).
    /// Note that <b><i>no</i></b> configuration of this compressor is done by this method.  It merely adds it
    /// to the graph in the appropriate place.  Also, the pointer is not invalidated, so any configuration
    /// of the compressor that needs to be done after being added to the graph can still be done.</param>
    /// <param name="pVideoCallback">Callback routine to be called for each video frame or null for no callback</param>
    /// <param name="pAudioCallback">Callback routine to be called for each audio frame or null for no callback</param>
    /// <remarks>
    /// The callback routines are invoked once for each sample.  This allows for additional processing to
    /// be performed on the video or audio buffers.
    /// </remarks>
    public void RenderToAVI(
        string sOutputFile,
        IBaseFilter ibfVideoCompressor,
        IBaseFilter ibfAudioCompressor,
        IDESCombineCB pVideoCallback,
        IDESCombineCB pAudioCallback)
    {
      int hr;
      IPin pPin;

      if (sOutputFile == null)
      {
        throw new Exception("Output file name cannot be null");
      }

      // Perform initialization common to all render routines
      RenderCommon();

      // Contains useful routines for creating the graph
      ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

      try
      {
        hr = icgb.SetFiltergraph(m_pGraph);
        DESError.ThrowExceptionForHR(hr);

        // Create the file writer
        IBaseFilter pMux;
        IFileSinkFilter pFilter;
        hr = icgb.SetOutputFileName(MediaSubType.Avi, sOutputFile, out pMux, out pFilter);
        DESError.ThrowExceptionForHR(hr);

        // We don't need this, so let it go
        Marshal.ReleaseComObject(pFilter);

        try
        {
          int NumGroups;
          hr = m_pTimeline.GetGroupCount(out NumGroups);
          DESError.ThrowExceptionForHR(hr);

          // Walk the groups.  For this class, there is one group that 
          // contains all the video, and a second group for the audio.
          for (int i = 0; i < NumGroups; i++)
          {
            IAMTimelineObj pGroup;

            hr = m_pTimeline.GetGroup(out pGroup, i);
            DESError.ThrowExceptionForHR(hr);

            try
            {
              // Inform the graph we will be writing to disk (rather than previewing)
              IAMTimelineGroup pTLGroup = (IAMTimelineGroup)pGroup;
              hr = pTLGroup.SetPreviewMode(false);
              DESError.ThrowExceptionForHR(hr);
            }
            finally
            {
              Marshal.ReleaseComObject(pGroup);
            }

            // Get the IPin for the current group
            hr = m_pRenderEngine.GetGroupOutputPin(i, out pPin);
            DESError.ThrowExceptionForHR(hr);

            try
            {
              if (IsVideo(pPin))
              {
                // Create a sample grabber, add it to the graph and connect it all up
                AVCallback mcb = new AVCallback(m_Video, pVideoCallback, (IMediaEventSink)m_pGraph, EC_VideoFileComplete);
                RenderHelper(icgb, mcb, "Video", pPin, ibfVideoCompressor, pMux);
              }
              else
              {
                // Create a sample grabber, add it to the graph and connect it all up
                AVCallback mcb = new AVCallback(m_Audio, pAudioCallback, (IMediaEventSink)m_pGraph, EC_AudioFileComplete);
                RenderHelper(icgb, mcb, "Audio", pPin, ibfAudioCompressor, pMux);
              }
            }
            finally
            {
              Marshal.ReleaseComObject(pPin);
            }
          }
        }
        finally
        {
          Marshal.ReleaseComObject(pMux);
        }
      }
      finally
      {
        Marshal.ReleaseComObject(icgb);
      }
    }
Exemple #6
0
    /// <summary>
    /// Configure the graph to output the results to a video window.
    /// </summary>
    /// <remarks>
    /// The callback routines are invoked once for each sample.  This allows for additional processing to
    /// be performed on the video or audio buffers.
    /// </remarks>
    /// <param name="hWnd">Window handle to render to, or IntPtr.Zero to render to its own window</param>
    /// <param name="pVideoCallback">Callback routine to be called for each video frame or null for no callback</param>
    /// <param name="pAudioCallback">Callback routine to be called for each audio frame or null for no callback</param>
    /// <param name="video">Render only video</param>
    /// <param name="audio">Render only audio</param>
    private void RenderToWindow(IntPtr hWnd, IDESCombineCB pVideoCallback, IDESCombineCB pAudioCallback, bool video, bool audio)
    {
      int hr;
      IPin pPin;
      IVideoWindow pVidWindow;
      IAMTimelineObj pGroup;

      // Perform initialization common to all render routines
      RenderCommon();

      // Contains useful routines for creating the graph
      ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

      try
      {
        hr = icgb.SetFiltergraph(m_pGraph);
        DESError.ThrowExceptionForHR(hr);

        int NumGroups;
        hr = m_pTimeline.GetGroupCount(out NumGroups);
        DESError.ThrowExceptionForHR(hr);

        // Walk the groups.  For DESCombine, there is one group that 
        // contains all the video, and a second group for the audio.
        for (int i = 0; i < NumGroups; i++)
        {
          hr = m_pTimeline.GetGroup(out pGroup, i);
          DESError.ThrowExceptionForHR(hr);

          try
          {
            // Inform the graph we will be previewing (rather than writing to disk)
            IAMTimelineGroup pTLGroup = (IAMTimelineGroup)pGroup;
            hr = pTLGroup.SetPreviewMode(true);
            DESError.ThrowExceptionForHR(hr);
          }
          finally
          {
            // Release the group
            Marshal.ReleaseComObject(pGroup);
          }

          // Get the IPin for the current group
          hr = m_pRenderEngine.GetGroupOutputPin(i, out pPin);
          DESError.ThrowExceptionForHR(hr);

          try
          {
            // If this is the video pin
            if (video && IsVideo(pPin))
            {
              // Get a video renderer
              IBaseFilter ibfVideoRenderer = (IBaseFilter)new VideoRenderer();

              try
              {
                // Create a sample grabber, add it to the graph and connect it all up
                AVCallback mcb = new AVCallback(m_Video, pVideoCallback, (IMediaEventSink)m_pGraph, EC_VideoFileComplete);
                RenderWindowHelper(icgb, mcb, "Video", pPin, ibfVideoRenderer);
              }
              finally
              {
                Marshal.ReleaseComObject(ibfVideoRenderer);
              }
            }
            else if (audio)
            {
              // Get an audio renderer
              IBaseFilter ibfAudioRenderer = (IBaseFilter)new AudioRender();

              try
              {
                // Create a sample grabber, add it to the graph and connect it all up
                AVCallback mcb = new AVCallback(m_Audio, pAudioCallback, (IMediaEventSink)m_pGraph, EC_AudioFileComplete);
                RenderWindowHelper(icgb, mcb, "Audio", pPin, ibfAudioRenderer);
              }
              finally
              {
                Marshal.ReleaseComObject(ibfAudioRenderer);
              }
            }
          }
          finally
          {
            Marshal.ReleaseComObject(pPin);
          }
        }

        if (video)
        {

          // Configure the video window
          pVidWindow = (IVideoWindow)m_pGraph;

          // If a window handle was supplied, use it
          if (hWnd != IntPtr.Zero)
          {
            hr = pVidWindow.put_Owner(hWnd);
            DESError.ThrowExceptionForHR(hr);
          }
          else
          {
            // Use our own window

            hr = pVidWindow.put_Caption("Video Rendering Window");
            DESError.ThrowExceptionForHR(hr);

            // since no user interaction is allowed, remove
            // system menu and maximize/minimize buttons
            WindowStyle lStyle = 0;
            hr = pVidWindow.get_WindowStyle(out lStyle);
            DESError.ThrowExceptionForHR(hr);

            lStyle &= ~(WindowStyle.MinimizeBox | WindowStyle.MaximizeBox | WindowStyle.SysMenu);
            hr = pVidWindow.put_WindowStyle(lStyle);
            DESError.ThrowExceptionForHR(hr);
          }
        }
      }
      finally
      {
        Marshal.ReleaseComObject(icgb);
      }
    }
Exemple #7
0
    /// <summary>
    /// Common routine used by RenderTo*  
    /// </summary>
    /// <param name="icgb">ICaptureGraphBuilder2 to use</param>
    /// <param name="pCallback">Callback to use (or null)</param>
    /// <param name="sType">string to use in creating filter graph object descriptions</param>
    /// <param name="pPin">Pin to connect from</param>
    /// <param name="ibfCompressor">Compressor to use, or null for none</param>
    /// <param name="pOutput">Endpoint (renderer or file writer) to connect to</param>
    private void RenderHelper(ICaptureGraphBuilder2 icgb, AVCallback pCallback, string sType, IPin pPin, IBaseFilter ibfCompressor, IBaseFilter pOutput)
    {
      int hr;
      IBaseFilter ibfSampleGrabber = null;

      try
      {
        // If no callback was provided, don't create a samplegrabber
        if (pCallback != null)
        {
          ISampleGrabber isg = (ISampleGrabber)new SampleGrabber();
          ibfSampleGrabber = (IBaseFilter)isg;

          hr = isg.SetCallback(pCallback, 1);
          DESError.ThrowExceptionForHR(hr);

          hr = m_pGraph.AddFilter(ibfSampleGrabber, sType + " sample grabber");
          DESError.ThrowExceptionForHR(hr);
        }

        // If a compressor was provided, add it to the graph and connect it up
        if (ibfCompressor != null)
        {
          // Connect the pin.
          hr = m_pGraph.AddFilter(ibfCompressor, sType + " Compressor");
          DESError.ThrowExceptionForHR(hr);

          hr = icgb.RenderStream(null, null, pPin, ibfSampleGrabber, ibfCompressor);
          DESError.ThrowExceptionForHR(hr);

          // Connect the pin.
          hr = icgb.RenderStream(null, null, ibfCompressor, null, pOutput);
          DESError.ThrowExceptionForHR(hr);
        }
        else
        {
          // Just connect the SampleGrabber (if any)
          hr = icgb.RenderStream(null, null, pPin, ibfSampleGrabber, pOutput);
          DESError.ThrowExceptionForHR(hr);
        }
      }
      finally
      {
        if (ibfSampleGrabber != null)
        {
          Marshal.ReleaseComObject(ibfSampleGrabber);
        }
      }
    }
Exemple #8
0
    /// <summary>
    /// Called from RenderWindow to add the renderer to the graph, create a sample grabber, add it 
    /// to the graph and connect it all up
    /// </summary>
    /// <param name="icgb">ICaptureGraphBuilder2 to use</param>
    /// <param name="pCallback">ICaptureGraphBuilder2 to use</param>
    /// <param name="sType">string to use in creating filter graph object descriptions</param>
    /// <param name="pPin">Pin to connect from</param>
    /// <param name="ibfRenderer">Renderer to add</param>
    private void RenderWindowHelper(ICaptureGraphBuilder2 icgb, AVCallback pCallback, string sType, IPin pPin, IBaseFilter ibfRenderer)
    {
      int hr;

      // Add the renderer to the graph
      hr = m_pGraph.AddFilter(ibfRenderer, sType + " Renderer");
      DESError.ThrowExceptionForHR(hr);

      // Do everything else
      RenderHelper(icgb, pCallback, sType, pPin, null, ibfRenderer);
    }