@@ -16,7 +16,7 @@ namespace Babylon::Plugins
16
16
auto mediaStreamObject{Napi::Persistent (GetConstructor (env).New ({}))};
17
17
auto mediaStream{MediaStream::Unwrap (mediaStreamObject.Value ())};
18
18
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)}]() {
20
20
return mediaStreamObject.Value ();
21
21
});
22
22
}
@@ -58,14 +58,18 @@ namespace Babylon::Plugins
58
58
59
59
MediaStream::~MediaStream ()
60
60
{
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
+ }
69
73
70
74
// Wait for async operations to complete.
71
75
m_runtimeScheduler.Rundown ();
@@ -124,7 +128,7 @@ namespace Babylon::Plugins
124
128
// Create a persistent ref to the constraints object so it isn't destructed during our async work
125
129
auto constraintsRef{Napi::Persistent (constraints)};
126
130
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) {
128
132
this ->Width = cameraDimensions.width ;
129
133
this ->Height = cameraDimensions.height ;
130
134
0 commit comments