Reputation: 338
In my research of Media Foundation I've encountered some seemingly contradictory advice from two very reputable sources.
From MSDN: Media Foundation and COM: https://msdn.microsoft.com/en-us/library/windows/desktop/ee892371(v=vs.85).aspx
In Media Foundation, asynchronous processing and callbacks are handled by work queues. Work queues always have multithreaded apartment (MTA) threads, so an application will have a simpler implementation if it runs on an MTA thread as well. Therefore, it is recommended to call CoInitializeEx with the COINIT_MULTITHREADED flag.
Then from the book “Developing Microsoft Media Foundation Applications - By Anton Polinger” page 24:
Note MF is a free-threaded system, which means that COM interface methods can be invoked from arbitrary threads. Therefore, when calling CoInitializeEx(), you must initialize COM with the apartment-threaded object concurrency by passing in the COINIT_ APARTMENTTHREADED parameter. Your objects might also need to use synchronization primitives, such as locks, to control access to internal variables by concurrently running threads.
Additionally I've seen a lot of Media Foundation example code in GitHub that uses COINIT_APARTMENTTHREADED.
I'm developing an RTSP client that uses Media Foundation to stream multiple IP camera video feeds to Windows display. I'll be using multiple threads in my app so I believe it will be very important to get a definitive answer on this issue. Can someone please explain the contradiction and advise on the correct way to proceed?
Upvotes: 4
Views: 5472
Reputation: 69642
Media Foundation won't use marshaling (that is, they use direct communication) and its objects are using "Both" apartment model, with free threaded marshaling on runtime.
You are free to choose the apartment model, both MTA and STA are going to work out. Worker threads started by Media Foundation will however always be initialized as MTA (specifically because the design of MF does not suggest thread alignment e.g. on work queues and there is no sense in doing STAs; the controlling thread initialized by the application is okay to be STA).
That is, there is nothing wrong with initializing controlling thread as STA. It has no effect on Media Foundation API calls. The documentation suggests MTA initialization for the only reason that having all threads initialized as MTA there is no chance to confused apartments by mistake, which would be especially easy since the API is actively passing COM pointers between threads ignoring standard COM apartment rules. If you understand that you won't be affected by this behavior, STA initialization will work well for you. As you have found many Media Foundation samples, and applications are doing STA initialization.
Upvotes: 4