@@ -16,7 +16,7 @@ namespace Babylon::Plugins
1616 auto mediaStreamObject{Napi::Persistent (GetConstructor (env).New ({}))};
1717 auto mediaStream{MediaStream::Unwrap (mediaStreamObject.Value ())};
1818
19- return mediaStream->ApplyInitialConstraintsAsync (constraints).then (mediaStream-> m_runtimeScheduler , arcana::cancellation::none (), [mediaStreamObject{std::move (mediaStreamObject)}]() {
19+ return mediaStream->ApplyInitialConstraintsAsync (constraints).then (arcana::inline_scheduler , arcana::cancellation::none (), [mediaStreamObject{std::move (mediaStreamObject)}]() {
2020 return mediaStreamObject.Value ();
2121 });
2222 }
@@ -58,14 +58,18 @@ namespace Babylon::Plugins
5858
5959 MediaStream::~MediaStream ()
6060 {
61- // TODO: Is this still necessary?
62- // if (m_cameraDevice != nullptr)
63- // {
64- // // The cameraDevice should be destroyed on the JS thread as it may need to access main thread resources
65- // // move ownership of the cameraDevice to a lambda and dispatch it with the runtimeScheduler so the destructor
66- // // is called from that thread.
67- // m_runtimeScheduler.Get()([cameraDevice = std::move(m_cameraDevice)]() {});
68- // }
61+ m_cancellationSource.cancel ();
62+
63+ // HACK: This is a hack to make sure the camera device is destroyed on the JS thread.
64+ // The napi-jsi adapter currently calls the destructors of JS objects possibly on the wrong thread.
65+ // Once this is fixed, this hack will no longer be needed.
66+ if (m_cameraDevice != nullptr )
67+ {
68+ // The cameraDevice should be destroyed on the JS thread as it may need to access main thread resources
69+ // move ownership of the cameraDevice to a lambda and dispatch it with the runtimeScheduler so the destructor
70+ // is called from that thread.
71+ m_runtimeScheduler.Get ()([cameraDevice = std::move (m_cameraDevice)]() {});
72+ }
6973
7074 // Wait for async operations to complete.
7175 m_runtimeScheduler.Rundown ();
@@ -124,7 +128,7 @@ namespace Babylon::Plugins
124128 // Create a persistent ref to the constraints object so it isn't destructed during our async work
125129 auto constraintsRef{Napi::Persistent (constraints)};
126130
127- return m_cameraDevice->OpenAsync (bestCamera.value ().second ).then (m_runtimeScheduler.Get (), arcana::cancellation::none () , [this , constraintsRef{std::move (constraintsRef)}](CameraDevice::CameraDimensions cameraDimensions) {
131+ return m_cameraDevice->OpenAsync (bestCamera.value ().second ).then (m_runtimeScheduler.Get (), m_cancellationSource , [this , constraintsRef{std::move (constraintsRef)}](CameraDevice::CameraDimensions cameraDimensions) {
128132 this ->Width = cameraDimensions.width ;
129133 this ->Height = cameraDimensions.height ;
130134
0 commit comments