locked
How to use SinkWriter to send audio to speakers? RRS feed

  • Question

  • Hello all,

    I'm getting started with Media Foundation programming and want to use the SinkWriter to send audio to speakers.  I'm having problems and all the examples I can find only write to an output URL but not a MediaSink and so I'm not sure if this is supported.

    My current situation is that I'll run the below code (based on MSDN examples) and MFCreateSinkWriteFromMediaSink returns 0xc00d36b4.  I believe this corresponds to MF_E_INVALIDMEDIATYPE.

     CoCreateInstance (__uuidof (MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof (IMMDeviceEnumerator), (void **)&pEnum);
     pEnum->EnumAudioEndpoints (eRender, DEVICE_STATE_ACTIVE, &pDevices);
     pDevices->Item (0, &pDevice);
     pDevice->GetId (&wStrId);
     MFCreateAttributes (&pAttributes, 1);
     pAttributes->SetString (MF_AUDIO_RENDERER_ATTRIBUTE_ENDPOINT_ID, wStrId);
     MFCreateAudioRenderer (pAttributes, &pMediaSink);
     MFCreateSinkWriterFromMediaSink (pMediaSink, NULL, &pSinkWriter);

    Please let me know if this is possible or if you see anything I can do to get this working.  I'm thinking about switching to the pipeline API but it'll take me some time to get there and I'd love to continue with the SourceReader/SinkWriter model and reuse my current code.

    Thanks,

    Shaun

    Wednesday, August 29, 2012 8:48 AM

Answers

  • The sink writer does not really work well with renderers -- I have heard from some people that they have gotten it to work, but it is not tested well.  The MF_E_INVALIDMEDIATYPE is due to the sink writer trying to configure a default type on the stream sink.  If a default type is not set, the sink writer obtains the first media type from the available type list and sets it as the default type.  For the audio renderer, the available types are generally not full media types and they need some extra parameters set before they can be set as the media type.  To work around this, you can determine what media type you will be using up front and set it on the stream sink before passing it to the sink writer -- this involves using IMFMediaTypeHandler::SetCurrentMediaType.
    • Marked as answer by shaunp1979 Monday, September 3, 2012 6:13 AM
    Friday, August 31, 2012 8:03 PM

All replies

  • The sink writer does not really work well with renderers -- I have heard from some people that they have gotten it to work, but it is not tested well.  The MF_E_INVALIDMEDIATYPE is due to the sink writer trying to configure a default type on the stream sink.  If a default type is not set, the sink writer obtains the first media type from the available type list and sets it as the default type.  For the audio renderer, the available types are generally not full media types and they need some extra parameters set before they can be set as the media type.  To work around this, you can determine what media type you will be using up front and set it on the stream sink before passing it to the sink writer -- this involves using IMFMediaTypeHandler::SetCurrentMediaType.
    • Marked as answer by shaunp1979 Monday, September 3, 2012 6:13 AM
    Friday, August 31, 2012 8:03 PM
  • Hi Matt,

    Thanks for the info.  SetCurrentMediaType worked great!  You've saved me a ton of time.  Thanks!

    Best,

    Shaun

    Monday, September 3, 2012 6:17 AM