未验证 提交 aed72977 编写于 作者: E Eran 提交者: GitHub

PR #9503 from Avishag: Fix device_watcher; start it on register_internal_device_callback

......@@ -818,6 +818,7 @@ namespace librealsense
public:
virtual void start(device_changed_callback callback) = 0;
virtual void stop() = 0;
virtual bool is_stopped() const = 0;
virtual ~device_watcher() {};
};
}
......
......@@ -126,6 +126,7 @@ namespace librealsense
environment::get_instance().set_time_service(_backend->create_time_service());
_device_watcher = _backend->create_device_watcher();
assert(_device_watcher->is_stopped());
}
......@@ -448,12 +449,26 @@ namespace librealsense
}
}
}
void context::start_device_watcher()
{
_device_watcher->start([this](platform::backend_device_group old, platform::backend_device_group curr)
{
on_device_changed(old, curr, _playback_devices, _playback_devices);
});
}
uint64_t context::register_internal_device_callback(devices_changed_callback_ptr callback)
{
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
auto callback_id = unique_id::generate_id();
_devices_changed_callbacks.insert(std::make_pair(callback_id, std::move(callback)));
if (_device_watcher->is_stopped())
{
start_device_watcher();
}
return callback_id;
}
......@@ -461,17 +476,22 @@ namespace librealsense
{
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
_devices_changed_callbacks.erase(cb_id);
if (_devices_changed_callback == nullptr && _devices_changed_callbacks.size() == 0) // There are no register callbacks any more _device_watcher can be stopped
{
_device_watcher->stop();
}
}
void context::set_devices_changed_callback(devices_changed_callback_ptr callback)
{
_device_watcher->stop();
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
_devices_changed_callback = std::move(callback);
_device_watcher->start([this](platform::backend_device_group old, platform::backend_device_group curr)
if (_device_watcher->is_stopped())
{
on_device_changed(old, curr, _playback_devices, _playback_devices);
});
start_device_watcher();
}
}
std::vector<platform::uvc_device_info> filter_by_product(const std::vector<platform::uvc_device_info>& devices, const std::set<uint16_t>& pid_list)
......
......@@ -141,9 +141,11 @@ namespace librealsense
void raise_devices_changed(const std::vector<rs2_device_info>& removed, const std::vector<rs2_device_info>& added);
int find_stream_profile(const stream_interface& p);
std::shared_ptr<lazy<rs2_extrinsics>> fetch_edge(int from, int to);
void start_device_watcher();
std::shared_ptr<platform::backend> _backend;
std::shared_ptr<platform::device_watcher> _device_watcher;
std::map<std::string, std::weak_ptr<device_info>> _playback_devices;
std::map<uint64_t, devices_changed_callback_ptr> _devices_changed_callbacks;
......
......@@ -146,7 +146,7 @@ namespace librealsense
win_event_device_watcher(const backend * backend)
{
_data._backend = backend;
_data._stopped = false;
_data._stopped = true;
_data._last = backend_device_group(backend->query_uvc_devices(), backend->query_usb_devices(), backend->query_hid_devices());
}
~win_event_device_watcher() { stop(); }
......@@ -169,6 +169,12 @@ namespace librealsense
if (_thread.joinable()) _thread.join();
}
}
bool is_stopped() const override
{
return _data._stopped;
}
private:
std::thread _thread;
std::mutex _m;
......
......@@ -649,6 +649,10 @@ namespace librealsense
}, _entity_id, call_type::device_watcher_stop);
}
bool record_device_watcher::is_stopped() const
{
return _source_watcher->is_stopped();
}
void record_uvc_device::probe_and_commit(stream_profile profile, frame_callback callback, int buffers)
{
......@@ -1216,6 +1220,11 @@ namespace librealsense
}
}
bool playback_device_watcher::is_stopped() const
{
return !_alive.load();
}
void playback_device_watcher::raise_callback(backend_device_group old, backend_device_group curr)
{
_dispatcher.invoke([=](dispatcher::cancellable_timer t) {
......
......@@ -415,6 +415,7 @@ namespace librealsense
void stop() override;
bool is_stopped() const override;
private:
const record_backend* _owner;
std::shared_ptr<device_watcher> _source_watcher;
......@@ -492,7 +493,7 @@ namespace librealsense
~playback_device_watcher();
void start(device_changed_callback callback) override;
void stop() override;
bool is_stopped() const override;
void raise_callback(backend_device_group old, backend_device_group curr);
private:
......
......@@ -1625,6 +1625,11 @@ namespace librealsense
_callback_inflight.wait_until_empty();
}
bool is_stopped() const override
{
return !_active_object.is_active();
}
private:
active_object<> _active_object;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册