How can i properly configure ASF media sink in Media Foundation

468 views Asked by At

Here is how i'm trying to configure the ASF media sink:

// Create media type
ComPtr<IMFMediaType> videoOutputType;
Try(MFCreateMediaType(&videoOutputType));
Try(MFSetAttributeSize(videoOutputType.Get(), MF_MT_FRAME_SIZE, 400, 300));
Try(videoOutputType->SetUINT32(MF_MT_AVG_BITRATE, 626000));
Try(videoOutputType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
Try(videoOutputType->SetUINT32(MF_MT_VIDEO_ROTATION, 0));
Try(MFSetAttributeRatio(videoOutputType.Get(), MF_MT_FRAME_RATE, 30000, 1001));
Try(MFSetAttributeRatio(videoOutputType.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1));
Try(videoOutputType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
Try(videoOutputType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_WMV3));

// Create profile
ComPtr<IMFASFProfile> asfProfile;
Try(MFCreateASFProfile(&asfProfile));
ComPtr<IMFASFStreamConfig> streamConfig;
Try(asfProfile->CreateStream(videoOutputType.Get(), &streamConfig));
Try(streamConfig->SetStreamNumber(0));
Try(asfProfile->SetStream(streamConfig.Get()));

// Create media sink
ComPtr<IMFMediaSink> asfMediaSink;
ComPtr<IMFByteStream> outputByteStream(new NetworkOutputByteStream(stream));
Try(MFCreateASFStreamingMediaSink(outputByteStream.Get(), &asfMediaSink));

// Set content info
ComPtr<IMFASFContentInfo> asfContentInfo;
Try(asfMediaSink.As(&asfContentInfo));
Try(asfContentInfo->SetProfile(asfProfile.Get()));

// Create sink writer
Try(MFCreateSinkWriterFromMediaSink(asfMediaSink.Get(), NULL, &this->sinkWriter));

But the method SetProfile is returning the following error: E_INVALIDARG One or more arguments are invalid. So I assume that i am configuring it in a bad way. How can I do it right? I'm not sure how to use ASF media sink because i can't find any good samples about it.

1

There are 1 answers

0
Evgeny Pereguda On BEST ANSWER

I can say that in your code there are at least two big mistakes: 1. you index stream from 0:

streamConfig->SetStreamNumber(0)

It is a mistake - in Tutorial: 1-Pass Windows Media Encoding it is written that:

if (wStreamNumber < 1 || wStreamNumber > 127 )
{
    return MF_E_INVALIDSTREAMNUMBER;
}

In ASF there are max 128 streams, but stream with index 0 is reserved for format needs. You must use index more that 0.

  1. You try create media type by filling attributes - it does not good idea - firstly, you do not know all attributes, which are needed by MediaSink; secondly, you try create MediaType for Windows Video encoder - originally it is a DMO encoder which is changed for Media Foundation - it needs add special codec private data for MediaType via MF_MT_USER_DATA, Configuring a WMV Encoder - it means that MediaSink will try find such type data for Windows Media codec, but will not find it.

These are two mistakes, which are markable for me - I think that you should research tutorials on MSDN.

Regards.