How to write in-memory PCM data by using IMFSinkWriter?

427 views Asked by At

Hi every great hackers.

I can mix the audio and the video data that are read from files by using IMFSinkWriter, but I can't find how to mix raw PCM data in memory to the final media file by using IMFSinkWriter.

I suppose that in-memory raw PCM data will be generated by any methods.

I used the setting below for sinkwriter and the target audio format is MP3.

hr = writer->AddStream(mt_aud_in, &speech_stream_ind);
mytrace(L"speech_stream_ind = %d\n", speech_stream_ind);
HRTrace(hr); 

hr = MFCreateMediaType(&mt_aud_speech);
HRTrace(hr); 
hr = mt_aud_speech->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
HRTrace(hr); 
hr = mt_aud_speech->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
HRTrace(hr); 
hr = mt_aud_speech->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 2);
HRTrace(hr); 
hr = mt_aud_speech->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 44100);
HRTrace(hr); 
hr = mt_aud_speech->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 16);
HRTrace(hr); 
hr = writer->SetInputMediaType(speech_stream_ind, mt_aud_speech, NULL);
HRTrace(hr);

Regards,

1

There are 1 answers

1
tensor5375 On BEST ANSWER

I solved this problem by setting MF_MT_AUDIO_AVG_BYTES_PER_SECOND attribute.

The resolver that Sink Writer will use needs this attribute.

But MSDN is not saying this important thing.

HRESUlT hr = MFCreateMediaType(&mt_aud_speech_in);
hr = mt_aud_speech_in->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
hr = mt_aud_speech_in->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
hr = mt_aud_speech_in->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 2);
hr = mt_aud_speech_in->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 16);
hr = mt_aud_speech_in->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, 4);
hr = mt_aud_speech_in->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 44100);
hr = mt_aud_speech_in->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, 44100*2*2);