diff --git a/src/context.cpp b/src/context.cpp index 89fb99e284..96e0694614 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -2,7 +2,7 @@ // Copyright(c) 2015 Intel Corporation. All Rights Reserved. #include "context.h" -#include "media/playback/playback-device-info.h" +#include "device-info.h" #include "backend-device-factory.h" #ifdef BUILD_WITH_DDS @@ -76,7 +76,7 @@ namespace librealsense list.push_back( dev_info ); } } - for( auto & item : _playback_devices ) + for( auto & item : _user_devices ) { if( auto dev_info = item.second.lock() ) { @@ -156,46 +156,31 @@ namespace librealsense _devices_changed_callback = std::move(callback); } - std::shared_ptr context::add_device(const std::string& file) - { - auto it = _playback_devices.find(file); - if (it != _playback_devices.end() && it->second.lock()) - { - //Already exists - throw librealsense::invalid_value_exception( rsutils::string::from() - << "File \"" << file << "\" already loaded to context" ); - } - auto dinfo = std::make_shared< playback_device_info >( shared_from_this(), file ); - _playback_devices[file] = dinfo; - - std::vector< rs2_device_info > rs2_device_info_added{ { shared_from_this(), dinfo } }; - std::vector< rs2_device_info > rs2_device_info_removed; - invoke_devices_changed_callbacks( rs2_device_info_removed, rs2_device_info_added ); - return dinfo; - } - - void context::add_software_device(std::shared_ptr dev) + void context::add_device( std::shared_ptr< device_info > const & dev ) { auto address = dev->get_address(); - auto it = _playback_devices.find(address); - if (it != _playback_devices.end() && it->second.lock()) - throw librealsense::invalid_value_exception( "File \"" + address + "\" already loaded to context" ); - _playback_devices[address] = dev; + auto it = _user_devices.find( address ); + if( it != _user_devices.end() && it->second.lock() ) + throw librealsense::invalid_value_exception( "device already in context: " + address ); + _user_devices[address] = dev; std::vector< rs2_device_info > rs2_device_info_added{ { shared_from_this(), dev } }; std::vector< rs2_device_info > rs2_device_info_removed; invoke_devices_changed_callbacks( rs2_device_info_removed, rs2_device_info_added ); } - void context::remove_device(const std::string& file) + + void context::remove_device( std::shared_ptr< device_info > const & dev ) { - auto it = _playback_devices.find(file); - if(it == _playback_devices.end() ) - return; + auto address = dev->get_address(); + + auto it = _user_devices.find( address ); + if(it == _user_devices.end() ) + return; // Why not throw?! auto dev_info = it->second.lock(); - _playback_devices.erase(it); + _user_devices.erase(it); if( dev_info ) { diff --git a/src/context.h b/src/context.h index e5413c51d7..fef0d444ea 100644 --- a/src/context.h +++ b/src/context.h @@ -5,7 +5,6 @@ #include "types.h" // devices_changed_callback_ptr -#include #include #include #include @@ -39,8 +38,6 @@ struct rs2_stream_profile namespace librealsense { - class playback_device_info; - class stream_interface; class device_factory; class context : public std::enable_shared_from_this @@ -69,11 +66,11 @@ namespace librealsense void unregister_internal_device_callback(uint64_t cb_id); void set_devices_changed_callback(devices_changed_callback_ptr callback); - std::shared_ptr add_device(const std::string& file); - void remove_device(const std::string& file); + // Let the context maintain a list of custom devices. These can be anything, like playback devices or devices + // maintained by the user. + void add_device( std::shared_ptr< device_info > const & ); + void remove_device( std::shared_ptr< device_info > const & ); - void add_software_device(std::shared_ptr software_device); - const nlohmann::json & get_settings() const { return _settings; } private: @@ -81,7 +78,7 @@ namespace librealsense std::vector & rs2_devices_info_added ); void raise_devices_changed(const std::vector& removed, const std::vector& added); - std::map> _playback_devices; + std::map< std::string, std::weak_ptr< device_info > > _user_devices; std::map _devices_changed_callbacks; nlohmann::json _settings; // Save operation settings @@ -90,9 +87,7 @@ namespace librealsense std::vector< std::shared_ptr< device_factory > > _factories; devices_changed_callback_ptr _devices_changed_callback; - std::map> _streams; - std::map< int, std::map< int, std::weak_ptr< rsutils::lazy< rs2_extrinsics > > > > _extrinsics; - std::mutex _streams_mutex, _devices_changed_callbacks_mtx; + std::mutex _devices_changed_callbacks_mtx; }; } diff --git a/src/pipeline/config.cpp b/src/pipeline/config.cpp index 6fb49036db..3fc663edaf 100644 --- a/src/pipeline/config.cpp +++ b/src/pipeline/config.cpp @@ -253,7 +253,9 @@ namespace librealsense return pdev->create_device(); } - return ctx->add_device(file)->create_device(); + auto dev_info = std::make_shared< playback_device_info >( ctx, file ); + ctx->add_device( dev_info ); + return dev_info->create_device(); } std::shared_ptr config::resolve_device_requests(std::shared_ptr pipe, const std::chrono::milliseconds& timeout) diff --git a/src/rs.cpp b/src/rs.cpp index 61dd1811fd..aae13abdd2 100644 --- a/src/rs.cpp +++ b/src/rs.cpp @@ -1551,7 +1551,8 @@ rs2_device* rs2_context_add_device(rs2_context* ctx, const char* file, rs2_error VALIDATE_NOT_NULL(ctx); VALIDATE_NOT_NULL(file); - auto dev_info = ctx->ctx->add_device(file); + auto dev_info = std::make_shared< playback_device_info >( ctx->ctx, file ); + ctx->ctx->add_device( dev_info ); return new rs2_device{ dev_info->create_device() }; } HANDLE_EXCEPTIONS_AND_RETURN(nullptr, ctx, file) @@ -1564,7 +1565,7 @@ void rs2_context_add_software_device(rs2_context* ctx, rs2_device* dev, rs2_erro auto dev_info = std::make_shared< software_device_info >( ctx->ctx ); dev_info->set_device( std::dynamic_pointer_cast< software_device >( dev->device ) ); - ctx->ctx->add_software_device( dev_info ); + ctx->ctx->add_device( dev_info ); } HANDLE_EXCEPTIONS_AND_RETURN(, ctx, dev) @@ -1572,7 +1573,10 @@ void rs2_context_remove_device(rs2_context* ctx, const char* file, rs2_error** e { VALIDATE_NOT_NULL(ctx); VALIDATE_NOT_NULL(file); - ctx->ctx->remove_device(file); + // The context uses the address from the device-info to maintain the list of devices. I.e., we need a device-info + // that uses a device-info that is_same_as() the one created above, in rs2_context_add_device: + auto dev_info = std::make_shared< playback_device_info >( ctx->ctx, file ); + ctx->ctx->remove_device( dev_info ); } HANDLE_EXCEPTIONS_AND_RETURN(, ctx, file)