Skip to content

Commit

Permalink
PR #12269 from Eran: remove context knowledge of playback devices
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel authored Oct 9, 2023
2 parents 3f17622 + a68a259 commit 33c876b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
45 changes: 15 additions & 30 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -156,46 +156,31 @@ namespace librealsense
_devices_changed_callback = std::move(callback);
}

std::shared_ptr<playback_device_info> 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<device_info> 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 )
{
Expand Down
17 changes: 6 additions & 11 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "types.h" // devices_changed_callback_ptr

#include <rsutils/lazy.h>
#include <nlohmann/json.hpp>
#include <vector>
#include <map>
Expand Down Expand Up @@ -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<context>
Expand Down Expand Up @@ -69,19 +66,19 @@ namespace librealsense
void unregister_internal_device_callback(uint64_t cb_id);
void set_devices_changed_callback(devices_changed_callback_ptr callback);

std::shared_ptr<playback_device_info> 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<device_info> software_device);

const nlohmann::json & get_settings() const { return _settings; }

private:
void invoke_devices_changed_callbacks( std::vector<rs2_device_info> & rs2_devices_info_removed,
std::vector<rs2_device_info> & rs2_devices_info_added );
void raise_devices_changed(const std::vector<rs2_device_info>& removed, const std::vector<rs2_device_info>& added);

std::map<std::string, std::weak_ptr<device_info>> _playback_devices;
std::map< std::string, std::weak_ptr< device_info > > _user_devices;
std::map<uint64_t, devices_changed_callback_ptr> _devices_changed_callbacks;

nlohmann::json _settings; // Save operation settings
Expand All @@ -90,9 +87,7 @@ namespace librealsense
std::vector< std::shared_ptr< device_factory > > _factories;

devices_changed_callback_ptr _devices_changed_callback;
std::map<int, std::weak_ptr<const stream_interface>> _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;
};

}
4 changes: 3 additions & 1 deletion src/pipeline/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<device_interface> config::resolve_device_requests(std::shared_ptr<pipeline> pipe, const std::chrono::milliseconds& timeout)
Expand Down
10 changes: 7 additions & 3 deletions src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -1564,15 +1565,18 @@ 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)

void rs2_context_remove_device(rs2_context* ctx, const char* file, rs2_error** error) BEGIN_API_CALL
{
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)

Expand Down

0 comments on commit 33c876b

Please sign in to comment.