What is the correct sample size for a DirectShow push source filter that provides only audio?

674 Views Asked by At

I have a Delphi 6 Pro program using the DSPACK library to do DirectShow filter work. I am creating my first Push Source Filter that would provide audio from a custom audio source. Currently my biggest question is how to determine the amount of data I would copy into the Filter's output buffer during a FillBuffer() call.

Do I really copy only one audio sample at a time? This would be a mere 2-bytes in my case since each audio sample is 16-bits wide, single-channel. This seems radically inefficient. Or do I copy a block of samples at once, and if so, what is the correct logic to use in determining how many bytes to copy? Is the correct logic to simply use the value returned by Sample.GetSize() as the number of bytes to provide, where Sample is the IMediaSample object passed in to the FillBuffer() call?

Also, if anyone has any tips or caveats regarding creating an audio push source filter versus a video one, please post them here. The sample I am working for is a video filter that streams the current desktop image as a series of bitmaps. I worry that I'll will do something that is inappropriate for an audio only DirectShow filter when using a video filter as my starting point. In addition, audio coming from this filter must be real-time since it involves a live audio connection between two parties.

1

There are 1 best solutions below

0
On

Do I really copy only one audio sample at a time? This would be a mere 2-bytes in my case since each audio sample is 16-bits wide, single-channel. This seems radically inefficient. Or do I copy a block of samples at once...

You copy a block at once. In fact, as you have an upstream/source filter, it is you who is generating the data. So your filter is requesting an attached memory allocator on the output pin to get you a new buffer to carry the data. You obtain the buffer and you have its capacity, say N bytes. You are free to fill the buffer as you wish, putting more or less samples into it, just a few bytes or as much as possible.

To avoid complications, you should be filling data at block alignment, specific to media type. With audio it's typically WAVEFORMATEX::nBlockAlign value. Still even not doing this might be OK for certain peer filter which would buffer data internally and handle alignment themselves. Also with good alignment it would be easier to attach correct time stamps to the data.