Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can update options automatically, get notification of value change #12483

Merged
merged 20 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ff66099
Add options_updater class
OhadMeir Dec 4, 2023
3f8a59d
Add option_value_update callback to the API, syntethic_sensor uses op…
OhadMeir Dec 4, 2023
efddef6
Handle PR#12483 comments. Rename to options_watcher, better use of su…
OhadMeir Dec 11, 2023
ad468ab
More PR#12483 comments. Callback registration at sensor_interface lev…
OhadMeir Dec 12, 2023
8f5c4a9
Set options_watcher interval from settings
OhadMeir Dec 12, 2023
01511e0
Fix includes
OhadMeir Dec 13, 2023
5370268
Register all options to option-watcher
OhadMeir Dec 13, 2023
ec27297
Fix cppcheck issue
OhadMeir Dec 13, 2023
7117f5d
Catch options-watcher query exceptions
OhadMeir Dec 17, 2023
0f75400
Fix unit-tests compilation errors
OhadMeir Dec 17, 2023
ad5658e
Catch options-watcher initialization query exception
OhadMeir Dec 17, 2023
2c650b1
options-watcher using one map. API callbacks function renamed
OhadMeir Dec 20, 2023
5fa97e6
Fix cpp unit tests. options-watcher thread stops as soon as possible
OhadMeir Dec 24, 2023
6730ac6
options-watcher queries option before starting thread
OhadMeir Dec 24, 2023
3af987f
Powering on UVC before updating options. Using condition variable to …
OhadMeir Dec 26, 2023
a128169
Fix typo. Thread loop waits first because update happens right before…
OhadMeir Dec 26, 2023
cd0b14e
Adding destruction flag to stop queries as soon as possible
OhadMeir Dec 26, 2023
08f25fb
options-watcher catching join exception in stop. viewer catches excep…
OhadMeir Dec 26, 2023
9bbe102
Add on option changed to python wrapper. Add options-watcher unit test
OhadMeir Dec 28, 2023
649bffe
Add test cases to the options-wathcer unit test
OhadMeir Jan 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/small-heap.h"
"${CMAKE_CURRENT_LIST_DIR}/basics.h"
"${CMAKE_CURRENT_LIST_DIR}/feature-interface.h"
"${CMAKE_CURRENT_LIST_DIR}/syntethic-options-watcher.h"
maloel marked this conversation as resolved.
Show resolved Hide resolved
"${CMAKE_CURRENT_LIST_DIR}/syntethic-options-watcher.cpp"
)

if(BUILD_WITH_DDS)
Expand Down
21 changes: 13 additions & 8 deletions src/core/options-watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ options_watcher::~options_watcher()
{
try
{
std::lock_guard< std::mutex > lock( _mutex );
_options.clear();
{
std::lock_guard< std::mutex > lock( _mutex );
maloel marked this conversation as resolved.
Show resolved Hide resolved
_options.clear();
}
stop();
}
catch( ... )
Expand Down Expand Up @@ -76,17 +78,16 @@ void options_watcher::start()
{
if( ! _updater.joinable() ) // If not already started
{
if( _should_query_for_first_time )
{
_updater = std::thread( [this]() {
update_options();
_should_query_for_first_time = false;
}
_updater = std::thread( [this]() { thread_loop(); } );
thread_loop();
maloel marked this conversation as resolved.
Show resolved Hide resolved
} );
}
}

void options_watcher::stop()
{
_stopping.notify_all();
if( _updater.joinable() )
_updater.join();
}
Expand All @@ -108,7 +109,8 @@ void options_watcher::thread_loop()
if( should_stop() )
break;

std::this_thread::sleep_for( _update_interval );
std::unique_lock< std::mutex > lock( _mutex );
_stopping.wait_for( lock, _update_interval );
}
}

Expand All @@ -118,6 +120,9 @@ std::map< rs2_option, std::shared_ptr< option > > options_watcher::update_option

std::lock_guard< std::mutex > lock( _mutex );

if( should_stop() )
return updated_options;

for( auto & opt : _options )
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/core/options-watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class options_watcher
std::chrono::milliseconds _update_interval;
std::thread _updater;
std::mutex _mutex;
bool _should_query_for_first_time = true;
std::condition_variable _stopping;
};


Expand Down
1 change: 1 addition & 0 deletions src/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ void log_callback_end( uint32_t fps,
const std::map< uint32_t, rs2_stream > & fourcc_to_rs2_stream_map )
: sensor_base( name, device )
, _raw_sensor( raw_sensor )
, _options_watcher( _raw_sensor )
{
nlohmann::json const & settings = device->get_context()->get_settings();
if( auto interval_j = rsutils::json::nested( settings, std::string( "options-update-interval", 23 ) ) )
Expand Down
9 changes: 7 additions & 2 deletions src/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "source.h"
#include "core/extension.h"
#include "proc/formats-converter.h"
#include <src/core/options-watcher.h>
#include <src/syntethic-options-watcher.h>

#include <rsutils/lazy.h>
#include <rsutils/signal.h>
Expand Down Expand Up @@ -193,6 +193,11 @@ namespace librealsense

std::shared_ptr< std::map< uint32_t, rs2_format > > & get_fourcc_to_rs2_format_map();
std::shared_ptr< std::map< uint32_t, rs2_stream > > & get_fourcc_to_rs2_stream_map();

// Sometimes it is more efficient to prepare for large or repeating operations. Depending on the actual sensor
// type we might want to change power state or encapsulate small transactions into a large one.
virtual void prepare_for_bulk_operation() {};
maloel marked this conversation as resolved.
Show resolved Hide resolved
virtual void finished_bulk_operation(){};
};

// A sensor pointer to another "raw sensor", usually UVC/HID
Expand Down Expand Up @@ -257,7 +262,7 @@ namespace librealsense
formats_converter _formats_converter;
std::vector<rs2_option> _cached_processing_blocks_options;

options_watcher _options_watcher;
syntethic_options_watcher _options_watcher;
};


Expand Down
32 changes: 32 additions & 0 deletions src/syntethic-options-watcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.


#include <src/syntethic-options-watcher.h>
#include <src/sensor.h>


namespace librealsense {


syntethic_options_watcher::syntethic_options_watcher( const std::shared_ptr< raw_sensor_base > & raw_sensor )
: _raw_sensor( raw_sensor )
{
}

std::map< rs2_option, std::shared_ptr< option > > syntethic_options_watcher::update_options()
{
std::map< rs2_option, std::shared_ptr< option > > updated_options;

std::shared_ptr< raw_sensor_base > strong = _raw_sensor.lock();
if( ! strong )
return updated_options;

strong->prepare_for_bulk_operation();
updated_options = options_watcher::update_options();
strong->finished_bulk_operation();

return updated_options;
}

} // namespace librealsense
26 changes: 26 additions & 0 deletions src/syntethic-options-watcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#pragma once


#include <src/core/options-watcher.h>


namespace librealsense {

class raw_sensor_base;

// Used by syntethic sensor and uses the raw_sensor bulk operations.
class syntethic_options_watcher : public options_watcher
{
public:
syntethic_options_watcher( const std::shared_ptr< raw_sensor_base > & raw_sensor );

protected:
std::map< rs2_option, std::shared_ptr< option > > update_options() override;

std::weak_ptr< raw_sensor_base > _raw_sensor;
};


} // namespace librealsense
10 changes: 10 additions & 0 deletions src/uvc-sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ void uvc_sensor::register_pu( rs2_option id )
}


void uvc_sensor::prepare_for_bulk_operation()
{
acquire_power();
}

void uvc_sensor::finished_bulk_operation()
{
release_power();
}

void uvc_sensor::register_xu( platform::extension_unit xu )
{
_xus.push_back( std::move( xu ) );
Expand Down
3 changes: 3 additions & 0 deletions src/uvc-sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class uvc_sensor : public raw_sensor_base
void register_xu( platform::extension_unit xu );
void register_pu( rs2_option id );

virtual void prepare_for_bulk_operation() override;
virtual void finished_bulk_operation() override;

std::vector< platform::stream_profile > get_configuration() const { return _internal_config; }
std::shared_ptr< platform::uvc_device > get_uvc_device() { return _device; }
platform::usb_spec get_usb_specification() const { return _device->get_usb_specification(); }
Expand Down
Loading