locked
Media Foundation for recording audio RRS feed

  • Question

  • I'm writing an application to record audio using Media Foundation... at a later stage I will want to add video channels but just want to get the audio working at the moment. So I'm using a file name with .mp4 extension and calling:

    enum { SAMPLES_PER_SECOND = 44100, AUDIO_CHANNELS = 2, BITS_PER_SAMPLE = 16 };

    // Create the sink writer // giving me C00D36D5 when I choose .wav file cs::hr_check( MFCreateSinkWriterFromURL( aWideFileName // wide string, not UTF8! // ^ I wonder if this can be a WEB url?! , NULL // no byte stream to write to , NULL // can input an IMFAttributes interface , &pWriter // result is pointer to MFSinkWriter interface ), "MFCreateSinkWriterFromURL"); // will throw if can't open this file

     

    the cs::hr_check is just a check of HRESULT, reporting an error and throws an exception if it fails... and this bit is fine (although it fails if I try a .wav file)

    next Im creating the media type and I can't understand exactly what I need, but I'm processing straight 16 bit PCM... so I do as follows:

    	CComPtr<IMFMediaType> pMFType;
    	cs::hr_check( MFCreateMediaType(&pMFType), "MFCreateMediaType" );
    	cs::hr_check(pMFType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio), "SetGUID");
    	cs::hr_check(pMFType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM), "MF_MT_SUBTYPE" );
    	cs::hr_check(pMFType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, AUDIO_CHANNELS),"MF_MT_AUDIO_NUM_CHANNELS");
    	cs::hr_check(pMFType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, SAMPLES_PER_SECOND), "MF_MT_AUDIO_SAMPLES_PER_SECOND");
    	cs::hr_check(pMFType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, AUDIO_CHANNELS*BITS_PER_SAMPLE/8), "MF_MT_AUDIO_BLOCK_ALIGNMENT");
    	cs::hr_check(pMFType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, SAMPLES_PER_SECOND*BITS_PER_SAMPLE*AUDIO_CHANNELS/8), "MF_MT_AUDIO_AVG_BYTES_PER_SECOND");
    	cs::hr_check(pMFType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, BITS_PER_SAMPLE), "MF_MT_AUDIO_BITS_PER_SAMPLE");
    	cs::hr_check(pMFType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE), "MF_MT_ALL_SAMPLES_INDEPENDENT");
     
    //  C00D36B4 media type is invalid, inconsistent, or not supported by this object for mp3 
    	cs::hr_check( pWriter->AddStream(pMFType, &iStreamIndex), "AddStream"); 

    That also seems to work adding my audio stream to the SinkWriter

    but then I think I want to tell it to accept my same 16 bit PCM as input and it fails :(

    // C00D5212 for mp4
    	cs::hr_check(
    		pWriter->SetInputMediaType(iStreamIndex, pMFType, NULL)
    		, "SetInputMediaType"
    	);

    I'm kind of hoping someone can help me understand how to set this sink writer to record my file.

    Also I don't know what to use for HRESULT error codes. ERRLOOK always says "message not found". How do other people identify what these codes mean?

    Thanks in advance,

    Perdie


    ~~~ PEr aRDUa ad asTrA ~~~ (through adversity to the stars)



    • Edited by Perdie Monday, December 17, 2012 11:47 AM
    Monday, December 17, 2012 11:43 AM

Answers

  • Thank you Matthew, those errors do make sense to me and by choosing a .wma or .wmv file it is recording my audio now.

    Note: Incase anyone else wants a play with it, I uploaded a simplified version here:

    interim source code zip

     
    • Edited by Perdie Saturday, December 22, 2012 10:38 AM problem solved
    • Marked as answer by Perdie Saturday, December 22, 2012 10:38 AM
    Wednesday, December 19, 2012 12:08 AM

All replies

  • The error codes are listed in mferror.h in the SDK include directory.

    0xC00D36D5L is MF_E_NOT_FOUND "The specified object or value does not exist." You're getting this because Media Foundation does not come with a .wav sink. There is a .wav sink sample which you could include with your application and register locally: http://msdn.microsoft.com/en-us/library/windows/desktop/bb970469(v=vs.85).aspx

    0xC00D36B4L is MF_E_INVALIDMEDIATYPE "The data specified for the media type is invalid, inconsistent, or not supported by this object." You're getting this because Media Foundation does not come with a .mp3 encoder; you could try AAC instead.

    0xC00D5212L is MF_E_TOPO_CODEC_NOT_FOUND "No suitable transform was found to encode or decode the content." I don't know why you're getting this with PCM.


    Matthew van Eerde

    Monday, December 17, 2012 2:53 PM
  • Thank you Matthew, those errors do make sense to me and by choosing a .wma or .wmv file it is recording my audio now.

    Note: Incase anyone else wants a play with it, I uploaded a simplified version here:

    interim source code zip

     
    • Edited by Perdie Saturday, December 22, 2012 10:38 AM problem solved
    • Marked as answer by Perdie Saturday, December 22, 2012 10:38 AM
    Wednesday, December 19, 2012 12:08 AM