IntelRealSense / librealsense

Intel® RealSense™ SDK
https://www.intelrealsense.com/
Apache License 2.0
7.53k stars 4.81k forks source link

Add configuration option to call CoInitializeEx with COINIT_APARTMENTTHREADED flag #4238

Closed tim-depthkit closed 3 years ago

tim-depthkit commented 5 years ago
Required Info
Camera Model N/A
Firmware Version N/A
Operating System & Version Win (8.1/10)
Kernel Version (Linux Only)
Platform PC
SDK Version 2.20.0 and up
Language C
Segment VR/AR

Issue Description

Currently the WMF backend initialized COM as follows: CoInitializeEx(nullptr, COINIT_MULTITHREADED); // when using COINIT_APARTMENTTHREADED, calling _pISensor->SetEventSink(NULL) to stop sensor can take several seconds

However in applications that have COM initializations on other threads using COINIT_APARTMENTTHREADED (like the Qt framework, for example), this can lead to a deadlock inside COM functions.

As the comment suggests, there is a reason for initializing in this way, but the SDK user should be allowed to configure this if necessary, via a CMake build option or similar.

dorodnic commented 5 years ago

Yep, that make sense. Do you think this can be covered as a CMake parameter or dedicated API?

tim-depthkit commented 5 years ago

A CMake option would be sufficient for my use case.

On Wed, Jun 19, 2019 at 7:50 AM Sergey Dorodnicov notifications@github.com wrote:

Yep, that make sense. Do you think this can be covered as a CMake parameter or dedicated API?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IntelRealSense/librealsense/issues/4238?email_source=notifications&email_token=AGDHJGYORQYCS5HMQYDL3T3P3IMQFA5CNFSM4HZDXXD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYBTRFA#issuecomment-503527572, or mute the thread https://github.com/notifications/unsubscribe-auth/AGDHJG7SHWR47BE6E7USWD3P3IMQFANCNFSM4HZDXXDQ .

-- Timothy Scaffidi Head of Engineering Scatter | https://scatter.nyc

RealSenseCustomerSupport commented 4 years ago

We are planning on implementing this. The rough schedule is Q3. Thanks.

RealSenseSupport commented 3 years ago

The change has been merged into the development branch. Please let us know if there is any concerns. Thank you.

RealSenseSupport commented 3 years ago

This enhancement is included in the v2.40.0 release. Thank you. Release Notes and search for DSO-15197 Closing this case.

kayychou commented 7 months ago

Hi @tim-depthkit, @dorodnic,

I encounter issues using the Qt framework after initializing my Intel camera D415 so reading these posts is really helpful to understand the root cause.

My issue (Main-UI blocked when opening a QFileDialog) seems to be solved by using librealsense compiled with the cmake option COM_MULTITHREADED set to OFF.

Could you please give me more insights regarding the impact this option can have on the SDK behaviour ? The doc mentions that "stop sensor can take several seconds". @tim-depthkit Have you noticed unwanted side effects with the option COM_MULTITHREADED set to OFF ? @RealSenseCustomerSupport to what extent do you recommend to preferably use the SDK with this option set to ON ?

Thanks for your support!

MartyG-RealSense commented 7 months ago

Hi @kayychou The only available RealSense related reference to COM_MULTITHREADED is a line in the lrs_options CMake file that provides for COM_MULTITHREADED the description "Set OFF to initialize COM library with COINIT_APARTMENTTHREADED". It is set to On by default.

https://github.com/IntelRealSense/librealsense/blob/master/CMake/lrs_options.cmake#L8C27-L8C90

A Microsoft reference at the link below suggests that COINT_APARTMENTTHREADED should be used (i.e set to On) when there is a thread that creates a window. So you could interpret that to mean that when COM_MULTITHREADED is set to Off, COINT_APARTMENTTHREADED is On, which is useful in a thread that needs to create a window.

https://learn.microsoft.com/en-us/windows/win32/learnwin32/initializing-the-com-library