locked
Error occurs when to create media source in pmp RRS feed

  • Question

  • Hi,

    I need to put my media source into PMP, and was doing this with the hints from the link:

    http://social.msdn.microsoft.com/forums/en-US/mediafoundationdevelopment/thread/9e69f687-eb38-45f3-ae9f-2bd4a2f7b786/

    Below is my code -

    HRESULT MFPlayer::BeginOpenURL(const WCHAR *sURL)

    {

    ...

      if (m_bPMPSet) // Need to create the resolver from inside the PMP
     {
      CComPtr<IMFPMPHost> pMFPMPHost;
      CHECK_HR( hr = MFGetService(m_pSession, MF_PMP_SERVICE, __uuidof(IMFPMPHost), (void**)&pMFPMPHost));
      CHECK_HR( hr = pMFPMPHost->CreateObjectByCLSID(CLSID_MFSourceResolver, NULL, IID_IMFSourceResolver, (void**)&m_pResolver));
     }
     else
     {
      CHECK_HR( hr = MFCreateSourceResolver(&m_pResolver));
     }

     // Ask the source resolver to create the media source.
     CHECK_HR( hr = m_pResolver->BeginCreateObjectFromURL(
      sURL,
      MF_RESOLUTION_MEDIASOURCE,
      pProps,
      &m_pCancelToken,
      &m_OnOpenURL,
      NULL
      ));

    ...

    }


    HRESULT MFPlayer::OnOpenURL(IMFAsyncResult* pResult)

    {

    ...

     hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); // hr = 0x80010106, Cannot change thread mode after it is set.

     CComPtr<IMFTopology> pTopology;
     CComPtr<IUnknown> pUnk;
     CComPtr<IMFMediaSource> pSource;

     if (m_pResolver.p)
     {
      hr = m_pResolver->EndCreateObjectFromURL(pResult, &type, &pUnk); //hr = 0x8001010e, The application called an interface that was marshalled for a different thread.
     }

    ...

    }

    As the OnOpenURL is a IMFAsyncCallback and is called from the default MF work queue thread, which should be MTA per

    http://msdn.microsoft.com/en-us/library/ee892371(VS.85).aspx

    But the returned hr of my code of CoInitializeEx(NULL, COINIT_MULTITHREADED) seems contradict with it. Why

    My own application threads are all using CoInitializeEx(NULL, COINIT_MULTITHREADED). Is there anything else I can check? Could this be a Media Source Resolver bug?

    Thanks.

     


    Wednesday, April 6, 2011 7:30 AM

Answers

  • This is the dreaded third apartment mode that comes into play when dealing with cross-process calls -- the neutral threaded apartment.  The MF workqueue thread that issues the callback is MTA, but RPC (remote procedure call) creates a thread to handle the request in the application process that is in the NTA.  Whenever you have a callback that might be called cross-process in this manner, it should be able to handle being run in the NTA.  Session callbacks do not have to worry about this as the cross-process work occurs internally and all callbacks are issued from the local process.  I probably should add this data to that MF & COM document.
    • Marked as answer by Ciger Thursday, June 23, 2011 7:14 AM
    Tuesday, May 10, 2011 6:02 PM

All replies

  • Well, though I still do not fully understand COM stuffs around, this problem was finally solved by using the IGlobalInterfaceTable. The source resolver pointer used in BeginOpenURL() can not be used directly in OnOpenURL(), but had to be registered/retrieved through IGlobalInterfaceTable.

    As to the first error returned by CoInitializeEx(NULL, COINIT_MULTITHREADED); , it still looks like the MF work thread that calling my Invoke() is running in STA, isn't it?

    Thursday, April 14, 2011 9:02 AM
  • This is the dreaded third apartment mode that comes into play when dealing with cross-process calls -- the neutral threaded apartment.  The MF workqueue thread that issues the callback is MTA, but RPC (remote procedure call) creates a thread to handle the request in the application process that is in the NTA.  Whenever you have a callback that might be called cross-process in this manner, it should be able to handle being run in the NTA.  Session callbacks do not have to worry about this as the cross-process work occurs internally and all callbacks are issued from the local process.  I probably should add this data to that MF & COM document.
    • Marked as answer by Ciger Thursday, June 23, 2011 7:14 AM
    Tuesday, May 10, 2011 6:02 PM