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

remove rs2 structs from context.h #12289

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


#pragma once
#include "context.h"
#include "types.h" // notification
#include "core/extension.h"
#include "device.h"
Expand Down
8 changes: 4 additions & 4 deletions src/backend-device-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ backend_device_factory::backend_device_factory( context & ctx, callback && cb )
auto old_list = create_devices_from_group( old, RS2_PRODUCT_LINE_ANY );
auto new_list = create_devices_from_group( curr, RS2_PRODUCT_LINE_ANY );

std::vector< rs2_device_info > devices_removed;
std::vector< std::shared_ptr< device_info > > devices_removed;
for( auto & device_removed : subtract_sets( old_list, new_list ) )
{
devices_removed.push_back( { _context.shared_from_this(), device_removed } );
devices_removed.push_back( device_removed );
LOG_DEBUG( "Device disconnected: " << device_removed->get_address() );
}

std::vector< rs2_device_info > devices_added;
std::vector< std::shared_ptr< device_info > > devices_added;
for( auto & device_added : subtract_sets( new_list, old_list ) )
{
devices_added.push_back( { _context.shared_from_this(), device_added } );
devices_added.push_back( device_added );
LOG_DEBUG( "Device connected: " << device_added->get_address() );
}

Expand Down
38 changes: 13 additions & 25 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ namespace librealsense

_factories.push_back( std::make_shared< backend_device_factory >(
*this,
[this]( std::vector< rs2_device_info > const & removed, std::vector< rs2_device_info > const & added )
[this]( std::vector< std::shared_ptr< device_info > > const & removed,
std::vector< std::shared_ptr< device_info > > const & added )
{ invoke_devices_changed_callbacks( removed, added ); } ) );

#ifdef BUILD_WITH_DDS
_factories.push_back( std::make_shared< rsdds_device_factory >(
*this,
[this]( std::vector< rs2_device_info > const & removed, std::vector< rs2_device_info > const & added )
[this]( std::vector< std::shared_ptr< device_info > > const & removed,
std::vector< std::shared_ptr< device_info > > const & added )
{ invoke_devices_changed_callbacks( removed, added ); } ) );
#endif
}
Expand Down Expand Up @@ -94,29 +96,17 @@ namespace librealsense
}


void context::invoke_devices_changed_callbacks( std::vector< rs2_device_info > const & rs2_devices_info_removed,
std::vector< rs2_device_info > const & rs2_devices_info_added )
void context::invoke_devices_changed_callbacks(
std::vector< std::shared_ptr< device_info > > const & rs2_devices_info_removed,
std::vector< std::shared_ptr< device_info > > const & rs2_devices_info_added )
{
_devices_changed.raise( rs2_devices_info_removed, rs2_devices_info_added );
}


rsutils::subscription context::on_device_changes( devices_changed_callback_ptr callback )
rsutils::subscription context::on_device_changes( devices_changed_callback && callback )
{
return _devices_changed.subscribe(
[ctx = shared_from_this(), callback]( std::vector< rs2_device_info > const & removed,
std::vector< rs2_device_info > const & added )
{
try
{
callback->on_devices_changed( new rs2_device_list( { ctx, removed } ),
new rs2_device_list( { ctx, added } ) );
}
catch( std::exception const & e )
{
LOG_ERROR( "Exception thrown from user callback handler: " << e.what() );
}
} );
return _devices_changed.subscribe( std::move( callback ) );
}


Expand All @@ -129,9 +119,8 @@ namespace librealsense
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 );
std::vector< std::shared_ptr< device_info > > rs2_device_info_added{ dev };
invoke_devices_changed_callbacks( {}, rs2_device_info_added );
}


Expand All @@ -147,9 +136,8 @@ namespace librealsense

if( dev_info )
{
std::vector< rs2_device_info > rs2_device_info_added;
std::vector< rs2_device_info > rs2_device_info_removed{ { shared_from_this(), dev_info } };
invoke_devices_changed_callbacks( rs2_device_info_removed, rs2_device_info_added );
std::vector< std::shared_ptr< device_info > > rs2_device_info_removed{ dev_info };
invoke_devices_changed_callbacks( rs2_device_info_removed, {} );
}
}

Expand Down
43 changes: 9 additions & 34 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,11 @@
#include <map>


struct rs2_devices_changed_callback;


namespace librealsense
{
class context;
class device_factory;
class device_info;
class stream_profile_interface;
}

struct rs2_device_info
{
std::shared_ptr<librealsense::context> ctx;
std::shared_ptr<librealsense::device_info> info;
};

struct rs2_device_list
{
std::shared_ptr<librealsense::context> ctx;
std::vector<rs2_device_info> list;
};

struct rs2_stream_profile
{
librealsense::stream_profile_interface* profile;
std::shared_ptr<librealsense::stream_profile_interface> clone;
};


namespace librealsense
{
class device_factory;

class context : public std::enable_shared_from_this<context>
{
Expand Down Expand Up @@ -70,11 +43,13 @@ namespace librealsense
//
std::vector< std::shared_ptr< device_info > > query_devices( int mask ) const;

using devices_changed_callback_ptr = std::shared_ptr< rs2_devices_changed_callback >;
using devices_changed_callback
= std::function< void( std::vector< std::shared_ptr< device_info > > const & devices_removed,
std::vector< std::shared_ptr< device_info > > const & devices_added ) >;

// Subscribe to a notification to receive when the device-list changes.
//
rsutils::subscription on_device_changes( devices_changed_callback_ptr callback );
rsutils::subscription on_device_changes( devices_changed_callback && );

// Let the context maintain a list of custom devices. These can be anything, like playback devices or devices
// maintained by the user.
Expand All @@ -84,13 +59,13 @@ namespace librealsense
const nlohmann::json & get_settings() const { return _settings; }

private:
void invoke_devices_changed_callbacks( std::vector< rs2_device_info > const & devices_removed,
std::vector< rs2_device_info > const & devices_added );
void invoke_devices_changed_callbacks( std::vector< std::shared_ptr< device_info > > const & devices_removed,
std::vector< std::shared_ptr< device_info > > const & devices_added );

std::map< std::string /*address*/, std::weak_ptr< device_info > > _user_devices;

rsutils::signal< std::vector< rs2_device_info > const & /*removed*/,
std::vector< rs2_device_info > const & /*added*/ >
rsutils::signal< std::vector< std::shared_ptr< device_info > > const & /*removed*/,
std::vector< std::shared_ptr< device_info > > const & /*added*/ >
_devices_changed;

nlohmann::json _settings; // Save operation settings
Expand Down
8 changes: 4 additions & 4 deletions src/dds/rsdds-device-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ rsdds_device_factory::rsdds_device_factory( context & ctx, callback && cb )
_subscription = _watcher_singleton->subscribe(
[this, cb = std::move( cb )]( std::shared_ptr< realdds::dds_device > const & dev, bool added )
{
std::vector< rs2_device_info > infos_added;
std::vector< rs2_device_info > infos_removed;
std::vector< std::shared_ptr< device_info > > infos_added;
std::vector< std::shared_ptr< device_info > > infos_removed;
auto ctx = _context.shared_from_this();
auto dev_info = std::make_shared< dds_device_info >( ctx, dev );
if( added )
infos_added.push_back( { ctx, dev_info } );
infos_added.push_back( dev_info );
else
infos_removed.push_back( { ctx, dev_info } );
infos_removed.push_back( dev_info );
cb( infos_removed, infos_added );
} );
}
Expand Down
15 changes: 5 additions & 10 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ device::device( std::shared_ptr< const device_info > const & dev_info,
{
std::weak_ptr< std::atomic< bool > > weak_alive = _is_alive;
std::weak_ptr< const device_info > weak_dev_info = _dev_info;
auto cb = new devices_changed_callback_internal(
[weak_alive, weak_dev_info]( rs2_device_list * removed, rs2_device_list * added )
_device_change_subscription = get_context()->on_device_changes(
[weak_alive, weak_dev_info]( std::vector< std::shared_ptr< device_info > > const & removed,
std::vector< std::shared_ptr< device_info > > const & added )
{
// The callback can be called from one thread while the object is being destroyed by another.
// Check if members can still be accessed.
Expand All @@ -54,21 +55,15 @@ device::device( std::shared_ptr< const device_info > const & dev_info,
return;

// Update is_valid variable when device is invalid
for( auto & dev_info : removed->list )
for( auto & dev_info : removed )
{
if( dev_info.info->is_same_as( this_dev_info ) )
if( dev_info->is_same_as( this_dev_info ) )
{
*alive = false;
return;
}
}
} );

_device_change_subscription = get_context()->on_device_changes( { cb,
[]( rs2_devices_changed_callback * p )
{
p->release();
} } );
}
}

Expand Down
32 changes: 13 additions & 19 deletions src/device_hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@ namespace librealsense
{
_device_list = _ctx->query_devices(mask);

auto cb = new hub_devices_changed_callback([&,mask](rs2::event_information&)
{
std::unique_lock<std::mutex> lock(_mutex);

_device_list = _ctx->query_devices(mask);

// Current device will point to the first available device
_camera_index = 0;
if (_device_list.size() > 0)
{
_cv.notify_all();
}
});

_device_change_subscription = _ctx->on_device_changes( { cb,
[]( rs2_devices_changed_callback * p )
{
p->release();
} } );
_device_change_subscription = _ctx->on_device_changes(
[&, mask]( std::vector< std::shared_ptr< device_info > > const & /*removed*/,
std::vector< std::shared_ptr< device_info > > const & /*added*/ )
{
std::unique_lock< std::mutex > lock( _mutex );

_device_list = _ctx->query_devices( mask );

// Current device will point to the first available device
_camera_index = 0;
if( _device_list.size() > 0 )
_cv.notify_all();
} );
}

device_hub::~device_hub()
Expand Down
1 change: 0 additions & 1 deletion src/ds/d400/d400-active.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstddef>

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata-parser.h"

Expand Down
1 change: 0 additions & 1 deletion src/ds/d400/d400-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright(c) 2016 Intel Corporation. All Rights Reserved.

#include <src/device.h>
#include <src/context.h>
#include <src/image.h>
#include <src/metadata-parser.h>
#include <src/metadata.h>
Expand Down
1 change: 0 additions & 1 deletion src/ds/d400/d400-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstddef>

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata-parser.h"

Expand Down
1 change: 0 additions & 1 deletion src/ds/d400/d400-nonmonochrome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstddef>

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata-parser.h"

Expand Down
1 change: 0 additions & 1 deletion src/ds/d500/d500-active.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstddef>

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata-parser.h"

Expand Down
1 change: 0 additions & 1 deletion src/ds/d500/d500-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright(c) 2022 Intel Corporation. All Rights Reserved.

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata-parser.h"
#include "metadata.h"
Expand Down
1 change: 0 additions & 1 deletion src/ds/d500/d500-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstddef>

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata-parser.h"

Expand Down
1 change: 0 additions & 1 deletion src/ds/ds-motion-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "ds-motion-common.h"

#include "algo.h"
#include "context.h"
#include "hid-sensor.h"
#include "environment.h"
#include "metadata.h"
Expand Down
1 change: 0 additions & 1 deletion src/ds/ds-timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <iterator>

#include "device.h"
#include "context.h"
#include "image.h"
#include "metadata.h"

Expand Down
3 changes: 2 additions & 1 deletion src/fw-update/fw-update-factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

#include "platform/platform-device-info.h"
#include "core/streaming.h"
#include "context.h"

namespace librealsense
{
class context;

class fw_update_info : public platform::platform_device_info
{
typedef platform::platform_device_info super;
Expand Down
2 changes: 1 addition & 1 deletion src/gl/pointcloud-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "pointcloud-gl.h"
#include "option.h"
#include "environment.h"
#include "context.h"
#include "stream.h"

#include <iostream>
#include <chrono>
Expand Down
1 change: 0 additions & 1 deletion src/gl/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "colorizer-gl.h"
#include "upload.h"
#include "option.h"
#include "context.h"

#ifndef NOMINMAX
#define NOMINMAX
Expand Down
1 change: 0 additions & 1 deletion src/media/playback/playback_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "core/motion.h"
#include <map>
#include "types.h"
#include "context.h"
#include "ds/d400/d400-options.h"
#include "media/ros/ros_reader.h"

Expand Down
2 changes: 2 additions & 0 deletions src/pipeline/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "pipeline.h"
#include "platform/platform-device-info.h"
#include "media/playback/playback-device-info.h"
#include "context.h"


namespace librealsense
{
Expand Down
Loading
Loading