Skip to main content

Microsoft Silverlight

Answered Question MediaStreamSource and WMVRSS Feed

(1)

moorglade
moorglade

Member

Member

2 points

9 Posts

MediaStreamSource and WMV

I'm trying to play a WMV stream from raw samples by using MediaStreamSource, but I can't figure out what kind of data I should use to set the MediaStreamAttributeKeys.CodecPrivateData.

MSDN on CodecPrivateData:

Codec data that the pipeline needs to initialize and render correctly. For video, this is other header information. For audio, this is the base16-encoded WaveFormatEx structure.

Well, I know what "the base16-encoded WaveFormatEx structure" is, but "other header information" seems a little vague.

If I set the FourCC attribute to "WMV3" (which is supported by Silverlight) and do not provide anything for CodecPrivateData, the format is not being accepted by Silverlight.

My guess would be that maybe I'm supposed to use the VideoInfoHeader structure (which is somewhat similar to WaveFormatEx), but I cannot find any evidence to support this idea. I also know that there is something (a small byte array in fact) called "Private Data" which is appended to the VideoInfoHeader (and also to WaveFormatEx).

I would really appreciate it if anyone could shed light on the issue.

moorglade
moorglade

Member

Member

2 points

9 Posts

Re: MediaStreamSource and WMV

After studying the ASF specifications (section 9.2) and performing some experiments, I finally figured out what is needed to initialize MediaStreamSource for WMV content.

I still am a bit confused about byte order in one place (everything has to be converted to little-endian order, but some data is given as a byte array, and I'm not sure what kind of values it actually carries). There is also one place where I'm supposed to specify the image size, but when I put there anything other than 320x240, Silverlight doesn't accept the format (I get AG_E_INVALID_FORMAT or AG_E_NETWORK_ERROR). Still, setting the values to 320x240 doesn't seem to "break" anything.

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft

Re: MediaStreamSource and WMV

Hi Moorglade,

Why do you want to use "raw samples"?  Since Silverlight supports MediaElement, why not point to the wmv file directly?

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

moorglade
moorglade

Member

Member

2 points

9 Posts

Re: MediaStreamSource and WMV

Jonathan Shen – MSFT:

Why do you want to use "raw samples"?  Since Silverlight supports MediaElement, why not point to the wmv file directly?

Simply because there is no WMV file. I use DirectShow and Windows Media DMOs to record live streams and I send samples through sockets to my server and then to Silverlight clients.

Anyway, I think I finally got it working. I still don't know why I have to set the size to 320x240 when it's actually 800x600, but I suppose I shouldn't worry about that.

suprenium
suprenium

Member

Member

2 points

1 Posts

Re: MediaStreamSource and WMV

I have the same problem with CodecPrivateData.....

Maybe you can halp me with initialization of MediaStreamSource for WMV content.

 Thank you in advance,

 

Roman

 

moorglade
moorglade

Member

Member

2 points

9 Posts

Answered Question

Re: MediaStreamSource and WMV

Basically, the data you need to provide is described in the ASF specification, section 9.2.

In my code it looks like that:

private uint 	EncodedImageWidth;
private uint 	EncodedImageHeight;
private byte 	ReservedFlags;
private ushort 	FormatDataSize;

private uint 	biSize;
private int 	biWidth;
private int 	biHeight;
private ushort 	biPlanes;
private ushort 	biBitCount;
private uint 	biCompression;
private uint 	biSizeImage;
private int 	biXPelsPerMeter;
private int 	biYPelsPerMeter;
private uint 	biClrUsed;
private uint 	biClrImportant;
private byte[] 	CodecSpecificData = null;

The second part is just a BITMAPINFOHEADER structure with some codec data appended to it. To use it with MediaStreamSource, you need to convert the data to a base16-encoded string (little-endian byte order). The strange thing is, I have to set EncodedImageWidth/EncodedImageHeight to 320x240 to get it working.

Denis_by
Denis_by

Member

Member

9 points

8 Posts

Re: MediaStreamSource and WMV

It seems I have the same issue here. I'm trying to play ASF using MediaStreamSource. I came to the point when audio stream is played by MediaElement well but video stream is a big problem. I've tried to find some information about the format of that "other header information" described at http://msdn.microsoft.com/en-us/library/system.windows.media.mediastreamattributekeys(VS.95).aspx, and it seems that this post is the most descriptive on the topic. Anyway I have the same structure and passing it as MediaStreamAttributeKeys.CodecPrivateData. The thing is that from my experiments everything except EncodedImageWidth and EncodedImageHeight is not used by MediaElement.

moorglade, could you please give more details about your solution?

May be somebody from the silverlight team could share the details about the formatting of the "other header information" in general?

moorglade
moorglade

Member

Member

2 points

9 Posts

Re: MediaStreamSource and WMV

Denis_by:
moorglade, could you please give more details about your solution?

Well, I'm not sure what details could possibly be helpful in your situation, so maybe I'll just give you the code?
Just don't pay to much attention to the comments, they tend to be weird. (Nobody except me reads them anyway.)

Unless you've already done that, you should probably also check Supported Media Formats, Protocols, and Log Fields at MSDN to make sure that Silverlight can play your video.

Denis_by
Denis_by

Member

Member

9 points

8 Posts

Re: MediaStreamSource and WMV

moorglade, thank you for the answer and for the source codes. I've went through those and found that the implementation is almost the same. I've then tried to test playback with your MediaStreamSource and the result was also the same.

I've made then some more tests with my implementation and another thing which I've figured out is that when I set MediaElement.AutoPlay property to false and do not start the playback then the first sample is rendered correctly. So I assume that codec initialization is done correctly. Then the problem is possibly somewhere with samples preparation.Confused

moorglade
moorglade

Member

Member

2 points

9 Posts

Re: MediaStreamSource and WMV

Denis_by:
I've made then some more tests with my implementation and another thing which I've figured out is that when I set MediaElement.AutoPlay property to false and do not start the playback then the first sample is rendered correctly. So I assume that codec initialization is done correctly. Then the problem is possibly somewhere with samples preparation.

That has just reminded me of one thing - the first sample you pass to a MediaElement in Silverlight should always contain a key frame. I've found it to be less of a problem with audio, as I think in the WMA format which I was using every sample is a "key frame". But when dealing with video I had to wait for the first key frame to arrive before starting playback. So I guess you could check that, if you haven't already.

Denis_by
Denis_by

Member

Member

9 points

8 Posts

Re: MediaStreamSource and WMV

Here is what I've actually done and now it works fine:

 

 
1    mediaStreamAttributes[MediaStreamAttributeKeys.CodecPrivateData] =
2        BitConverter.ToString(formatData.BitmapInfoHeader.CodecSpecificData.ToArray()).Replace("-", "");
 

where CodecSpecificData are those codec specific data bytes described in section 9.2 of ASF specification.

Thanks for your help :)

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities