title: WPF MediaKit sharing MediaPlayer base
body: <p>I have been using WPFMediaKit to render a DirectShow graph. Here is my setup.</p>
  <p>I am having trouble using one <code>MediaPlayerBase</code> multiple times with multiple D3DRender instances. I have an <code>IVideoEngine</code> that returns a single graph (via <code>MediaPlayerBase</code>) that is used to preview. The <code>IVideoEngine</code> internally manages the DirectShow graph for switching camera inputs/etc. The idea is that this single graph (via <code>MediaPlayerBase</code>) may be used multiple times, simultaneously, or (more likely) at separate times via the <code>D3DRenderer</code> base class.</p>
  <p>I created a new <code>RenderElementControl</code> that simply renders a <code>MediaPlayerBase</code> onto the surface. It works great for the first instance used for a particular instance of the <code>MediaPlayerBase</code>, but when using the <code>RenderElementControl</code> again, no video is rendered.</p>
  <p>Here is my source code to specifically render a <code>MediaPlayerBase</code>.</p>
  <pre><code>public class RenderElementControl : D3DRenderer
  {
      private readonly MediaPlayerBase _mediaPlayerBase;
  
      public RenderElementControl(MediaPlayerBase mediaPlayerBase)
      {
          _mediaPlayerBase = mediaPlayerBase;
          Loaded += OnLoaded;
          Unloaded += OnUnloaded;
      }
  
      private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
      {
          _mediaPlayerBase.NewAllocatorFrame -= OnMediaPlayerNewAllocatorFramePrivate;
          _mediaPlayerBase.NewAllocatorSurface -= OnMediaPlayerNewAllocatorSurfacePrivate;
      }
  
      private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
      {
          _mediaPlayerBase.NewAllocatorFrame += OnMediaPlayerNewAllocatorFramePrivate;
          _mediaPlayerBase.NewAllocatorSurface += OnMediaPlayerNewAllocatorSurfacePrivate;
          _mediaPlayerBase.Dispatcher.DoEvents();
      }
  
      private void OnMediaPlayerNewAllocatorSurfacePrivate(object sender, IntPtr pSurface)
      {
          SetBackBuffer(pSurface);
      }
  
      private void OnMediaPlayerNewAllocatorFramePrivate()
      {
          InvalidateVideoImage();
      }
  }
  </code></pre>
  <h2>The question</h2>
  <p>Why does this control not work for a second instance of a single <code>MediaPlayerBase</code>? How do I make it so that I can use multiple <code>RenderElementControl</code>, possibly at the same time, with the same <code>MediaPlayerBase</code>?</p>
  <p><strong>Note</strong>: For those not familiar with WPFMediaKit, here is the source code for <a href="https://wpfmediakit.codeplex.com/SourceControl/latest#WPFMediaKit/WPFMediaKit/DirectShow/Controls/D3DRenderer.cs" rel="nofollow noreferrer">D3DRenderer</a> and <a href="https://wpfmediakit.codeplex.com/SourceControl/latest#WPFMediaKit/WPFMediaKit/DirectShow/MediaPlayers/MediaUriPlayer.cs" rel="nofollow noreferrer">MediaPlayerBase</a> that handles rendering a DirectShow render (VMR or EMR).</p>