I’m now crawling up the steep learning curve of Windows Media Foundation and I’m focusing on two very similar code samples to help me understand the technology. Even though both use Media Session for a very simple “Play” program that plays video from a file, there are important subtle differences.
The first sample is Microsoft’s MF Play located at:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd979592(v=vs.85).aspx
It does not initialize COM and does not perform any critical section locks yet it plays video very well. Will the lack of using COM restrict it’s use in other ways such as when processing multiple video streams to separate windows by multithreading? Since this code is online, I may be naively assuming this code is more current.
The second sample is from the book “Developing Microsoft Media Foundation Applications - By Anton Polinger”. I downloaded sample code from here: https://www.microsoftpressstore.com/content/images/9780735656598/downloads/9780735656598_files.zip
This Play program in the Chapter 3 folder is slightly more complex due to the use of these COM initialization functions:
// initialize COM
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
...
// uninitialize COM
CoUninitialize();
It also uses several critical section locks using:
CComCritSecLock<CComAutoCriticalSection> lock(m_critSec);
But mysteriously there are no corresponding unlocks(). So can someone please explain these potential important differences between these two code samples and which ones I should use? I’m concerned that if I don’t use the COM methods I may have problems later when I attempt to stream multiple videos to multiple windows, or worse I could have reliability problems.
On a side note, the Polinger code works but does not handle window resizing while playing video. I attempted to add in the code similar to the way the MS code does by using this code after a window resizing event:
m_pVideoDisplay->SetVideoPosition(NULL, &rcDest)
Using this just caused the program to freeze.
Any help would be greatly appreciated!
About "It does not initialize COM" - you have seen not all code of it - research Media Session Playback Example - you will find call MFStartup
in player.cpp - it is enough for MediaFoundation (MF). I have read book of Polinger and in the code it calls some COM thread model sensitive function - for example, DirectX. But, from my experience it looks like that MF calls CoInitialize from context of MFStartup
calling. More over, in code from book of Polinger there is call Apartment thread model: hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
, but MF supports execution from Multithreaded model - it does not sensitive for it.
About "But mysteriously there are no corresponding unlocks()." - CComCritSecLock
is object oriented wrapper for section lock and unlocks()
is called in destructor of CComCritSecLock
- ~CComCritSecLock()
.
About resizing - m_pVideoDisplay->SetVideoPosition(NULL, &rcDest)
- it looks like that rcDest
has wrong value - according to
IMFVideoDisplayControl::SetVideoPosition method
The destination rectangle defines a rectangle within the clipping window where the video appears. It is specified in pixels, relative to the client area of the window. To fill the entire window, set the destination rectangle to {0, 0, width, height},
I can advise research my project on CodeProject
site: NativeMediaFoundationPlayer: and WPFMediaFoundationPlayer
Regards, Evgeny Pereguda