Allocating Buffers for File Reading
[The feature associated with this page, Windows Media Format 11 SDK, is a legacy feature. It has been superseded by Source Reader and Sink Writer. Source Reader and Sink Writer have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use Source Reader and Sink Writer instead of Windows Media Format 11 SDK, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]
In the most basic file-reading scenario, the buffers used to deliver samples are allocated by the reading object (the reader object or the synchronous reader object). You can, however, allocate buffers yourself. For more information about the benefits of allocating your own buffers, see User Allocated Sample Support.
To use your own buffers for file reading, perform the following steps.
- Implement a callback or callbacks for the reader to call when it needs a buffer. If you are reading output samples, use IWMReaderAllocatorEx::AllocateForOutputEx. If you are reading stream samples, use IWMReaderAllocatorEx::AllocateForStreamEx. Include whatever logic for managing buffers that suits your application.
- Allocate a pool of buffers that you will use for file reading.
- Find the size required for your buffers by calling IWMReaderAdvanced::GetMaxOutputSampleSize or IWMReaderAdvanced::GetMaxStreamSampleSize for each output and/or stream for which the buffer is used. If using the synchronous reader, use IWMSyncReader::GetMaxOutputSampleSize or IWMSyncReader::GetMaxStreamSampleSize instead.
- Create each buffer for the pool.
- Set up the reader or synchronous reader for reading. For more information see Reading Files with the Asynchronous Reader or Reading Files with the Synchronous Reader.
- Before beginning writing, call IWMReaderAdvanced::SetAllocateForOutput or IWMReaderAdvanced::SetAllocateForStream for each output and stream for which you are allocating buffers using the reader object. For the synchronous reader, call IWMSyncReader2::SetAllocateForOutput or IWMSyncReader2::SetAllocateForStream instead.
- Begin reading the file.
The reading object will make calls to the appropriate allocator callback and get samples from your application. Your buffer management logic must include a way to signal that a buffer is free to be used again. Typically, a buffer is put back into the pool when its contents are rendered. Depending upon your application, you may need just a few buffers in the pool or many.
Related topics