Skip to content

Commit

Permalink
PR #12200 from Eran: Refactor device.h
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Sep 26, 2023
2 parents 7ee3caf + 39e3059 commit 640d281
Show file tree
Hide file tree
Showing 45 changed files with 833 additions and 624 deletions.
8 changes: 8 additions & 0 deletions src/basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
// Any extension relying on these APIs must be compiled and distributed together with realsense2.dll
#pragma warning(disable : 4275) /* disable: C4275: non dll-interface class used as base for dll-interface class */
#pragma warning(disable : 4251) /* disable: C4251: class needs to have dll-interface to be used by clients of class */

// Compiler Warning (level 2) C4250: 'class1' : inherits 'class2::member' via dominance
// This happens for librealsense::device, basically warning us of something that we know and is OK:
// 'librealsense::device': inherits 'info_container::create_snapshot' via dominance (and repeats for 3 more functions)
// And for anything else using both an interface and a container, options_container etc.
#pragma warning(disable: 4250)


#ifdef WIN32
#define LRS_EXTENSION_API __declspec(dllexport)
#else
Expand Down
6 changes: 6 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/advanced_mode.h"
"${CMAKE_CURRENT_LIST_DIR}/enum-helpers.h"
"${CMAKE_CURRENT_LIST_DIR}/depth-frame.h"
"${CMAKE_CURRENT_LIST_DIR}/device-interface.h"
"${CMAKE_CURRENT_LIST_DIR}/disparity-frame.h"
"${CMAKE_CURRENT_LIST_DIR}/frame-continuation.h"
"${CMAKE_CURRENT_LIST_DIR}/frame-additional-data.h"
"${CMAKE_CURRENT_LIST_DIR}/frame-header.h"
"${CMAKE_CURRENT_LIST_DIR}/frame-holder.h"
"${CMAKE_CURRENT_LIST_DIR}/frame-interface.h"
"${CMAKE_CURRENT_LIST_DIR}/info-interface.h"
"${CMAKE_CURRENT_LIST_DIR}/roi.h"
"${CMAKE_CURRENT_LIST_DIR}/matcher-factory.h"
"${CMAKE_CURRENT_LIST_DIR}/matcher-factory.cpp"
"${CMAKE_CURRENT_LIST_DIR}/motion.h"
"${CMAKE_CURRENT_LIST_DIR}/motion-frame.h"
"${CMAKE_CURRENT_LIST_DIR}/video.h"
Expand All @@ -26,4 +30,6 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/pose-frame.h"
"${CMAKE_CURRENT_LIST_DIR}/processing.h"
"${CMAKE_CURRENT_LIST_DIR}/serialization.h"
"${CMAKE_CURRENT_LIST_DIR}/stream-profile.h"
"${CMAKE_CURRENT_LIST_DIR}/tagged-profile.h"
)
73 changes: 73 additions & 0 deletions src/core/device-interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#pragma once

#include "info-interface.h"
#include "tagged-profile.h"
#include "stream-profile.h"

#include <librealsense2/h/rs_sensor.h>
#include <functional>
#include <vector>
#include <memory>
#include <utility> // std::pair


namespace librealsense {


class sensor_interface;
class matcher;
class frame_holder;
class context;
class device_info;
class stream_interface;
class stream_profile_interface;


// Base class for all devices in librealsense
//
class device_interface
: public virtual info_interface
, public std::enable_shared_from_this< device_interface >
{
public:
virtual ~device_interface() = default;

// Return the context to which this device belongs
// Typically, same as get_device_info()->context()
//
virtual std::shared_ptr< context > get_context() const = 0;

// Returns the device_info object from which this device was created
//
virtual std::shared_ptr< const device_info > get_device_info() const = 0;

virtual sensor_interface & get_sensor( size_t i ) = 0;
virtual const sensor_interface & get_sensor( size_t i ) const = 0;
virtual size_t get_sensors_count() const = 0;

virtual void hardware_reset() = 0;

virtual std::shared_ptr< matcher > create_matcher( const frame_holder & frame ) const = 0;

virtual std::pair< uint32_t, rs2_extrinsics > get_extrinsics( const stream_interface & ) const = 0;

virtual bool is_valid() const = 0;

virtual std::vector< tagged_profile > get_profiles_tags() const = 0;

using stream_profiles = std::vector< std::shared_ptr< stream_profile_interface > >;
virtual void tag_profiles( stream_profiles profiles ) const = 0;

// Return true if this device should use compression when recording to a bag file
//
virtual bool compress_while_record() const = 0;

virtual bool contradicts( const stream_profile_interface * a,
const std::vector< stream_profile > & others ) const = 0;
};


} // namespace librealsense
30 changes: 30 additions & 0 deletions src/core/info-interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#pragma once

#include "extension.h"
#include <src/basics.h>

#include <librealsense2/h/rs_sensor.h>
#include <string>


namespace librealsense {


// Anything that can support getting rs2_camera_info should implement this.
//
class info_interface : public virtual recordable< info_interface >
{
public:
virtual const std::string & get_info( rs2_camera_info info ) const = 0;
virtual bool supports_info( rs2_camera_info info ) const = 0;

virtual ~info_interface() = default;
};

MAP_EXTENSION( RS2_EXTENSION_INFO, librealsense::info_interface );


} // namespace librealsense
63 changes: 33 additions & 30 deletions src/core/info.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#pragma once

#include "extension.h"
#include "info-interface.h"

#include <map>
#include <string>
#include <memory>


namespace librealsense {

namespace librealsense

// Simple implementation of info_interface
//
class LRS_EXTENSION_API info_container
: public virtual info_interface
, public extension_snapshot
{
class info_interface : public virtual recordable<info_interface>
{
public:
virtual const std::string& get_info(rs2_camera_info info) const = 0;
virtual bool supports_info(rs2_camera_info info) const = 0;

virtual ~info_interface() = default;
};

MAP_EXTENSION(RS2_EXTENSION_INFO, librealsense::info_interface);

class LRS_EXTENSION_API info_container : public virtual info_interface, public extension_snapshot
{
public:
const std::string& get_info(rs2_camera_info info) const override;
bool supports_info(rs2_camera_info info) const override;

void register_info(rs2_camera_info info, const std::string& val);
void update_info(rs2_camera_info info, const std::string& val);
void create_snapshot(std::shared_ptr<info_interface>& snapshot) const override;
void enable_recording(std::function<void(const info_interface&)> record_action) override;
void update(std::shared_ptr<extension_snapshot> ext) override;
private:
std::map<rs2_camera_info, std::string> _camera_info;
};

}
public:
void register_info( rs2_camera_info info, const std::string & val );
void update_info( rs2_camera_info info, const std::string & val );

// info_interface
const std::string & get_info( rs2_camera_info info ) const override;
bool supports_info( rs2_camera_info info ) const override;

// extension_snapshot
void create_snapshot( std::shared_ptr< info_interface > & snapshot ) const override;
void enable_recording( std::function< void( const info_interface & ) > record_action ) override;
void update( std::shared_ptr< extension_snapshot > ext ) override;

private:
std::map< rs2_camera_info, std::string > _camera_info;
};


} // namespace librealsense
166 changes: 166 additions & 0 deletions src/core/matcher-factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#include "matcher-factory.h"
#include "frame-holder.h"

#include <src/sync.h>


namespace librealsense {


std::shared_ptr< matcher > matcher_factory::create( rs2_matchers matcher,
std::vector< stream_interface * > const & profiles )
{
switch( matcher )
{
case RS2_MATCHER_DI:
return create_DI_matcher( profiles );
case RS2_MATCHER_DI_C:
return create_DI_C_matcher( profiles );
case RS2_MATCHER_DLR_C:
return create_DLR_C_matcher( profiles );
case RS2_MATCHER_DLR:
return create_DLR_matcher( profiles );
case RS2_MATCHER_DIC:
return create_DIC_matcher( profiles );
case RS2_MATCHER_DIC_C:
return create_DIC_C_matcher( profiles );

case RS2_MATCHER_DEFAULT:
default:
return create_timestamp_matcher( profiles );
}
}


std::shared_ptr< matcher > matcher_factory::create_DLR_C_matcher( std::vector< stream_interface * > const & profiles )
{
auto color = find_profile( RS2_STREAM_COLOR, 0, profiles );
if( ! color )
{
LOG_DEBUG( "Created default matcher" );
return create_timestamp_matcher( profiles );
}

return create_timestamp_composite_matcher( { create_DLR_matcher( profiles ), create_identity_matcher( color ) } );
}


std::shared_ptr< matcher > matcher_factory::create_DI_C_matcher( std::vector< stream_interface * > const & profiles )
{
auto color = find_profile( RS2_STREAM_COLOR, 0, profiles );
if( ! color )
{
LOG_DEBUG( "Created default matcher" );
return create_timestamp_matcher( profiles );
}

return create_timestamp_composite_matcher( { create_DI_matcher( profiles ), create_identity_matcher( color ) } );
}


std::shared_ptr< matcher > matcher_factory::create_DLR_matcher( std::vector< stream_interface * > const & profiles )
{
auto depth = find_profile( RS2_STREAM_DEPTH, 0, profiles );
auto left = find_profile( RS2_STREAM_INFRARED, 1, profiles );
auto right = find_profile( RS2_STREAM_INFRARED, 2, profiles );

if( ! depth || ! left || ! right )
{
LOG_DEBUG( "Created default matcher" );
return create_timestamp_matcher( profiles );
}
return create_frame_number_matcher( { depth, left, right } );
}


std::shared_ptr< matcher > matcher_factory::create_DI_matcher( std::vector< stream_interface * > const & profiles )
{
auto depth = find_profile( RS2_STREAM_DEPTH, 0, profiles );
auto ir = find_profile( RS2_STREAM_INFRARED, 1, profiles );

if( ! depth || ! ir )
{
LOG_DEBUG( "Created default matcher" );
return create_timestamp_matcher( profiles );
}
return create_frame_number_matcher( { depth, ir } );
}


std::shared_ptr< matcher > matcher_factory::create_DIC_matcher( std::vector< stream_interface * > const & profiles )
{
std::vector< std::shared_ptr< matcher > > matchers;
if( auto depth = find_profile( RS2_STREAM_DEPTH, -1, profiles ) )
matchers.push_back( create_identity_matcher( depth ) );
if( auto ir = find_profile( RS2_STREAM_INFRARED, -1, profiles ) )
matchers.push_back( create_identity_matcher( ir ) );
if( auto confidence = find_profile( RS2_STREAM_CONFIDENCE, -1, profiles ) )
matchers.push_back( create_identity_matcher( confidence ) );

if( matchers.empty() )
{
LOG_ERROR( "no depth, ir, or confidence streams found for matcher" );
for( auto && p : profiles )
LOG_DEBUG( p->get_stream_type() << '/' << p->get_unique_id() );
throw std::runtime_error( "no depth, ir, or confidence streams found for matcher" );
}

return create_timestamp_composite_matcher( matchers );
}


std::shared_ptr< matcher > matcher_factory::create_DIC_C_matcher( std::vector< stream_interface * > const & profiles )
{
auto color = find_profile( RS2_STREAM_COLOR, 0, profiles );
if( ! color )
throw std::runtime_error( "no color stream found for matcher" );

return create_timestamp_composite_matcher( { create_DIC_matcher( profiles ), create_identity_matcher( color ) } );
}


std::shared_ptr< matcher >
matcher_factory::create_frame_number_matcher( std::vector< stream_interface * > const & profiles )
{
std::vector< std::shared_ptr< matcher > > matchers;
for( auto & p : profiles )
matchers.push_back( std::make_shared< identity_matcher >( p->get_unique_id(), p->get_stream_type() ) );

return create_frame_number_composite_matcher( matchers );
}


std::shared_ptr< matcher >
matcher_factory::create_timestamp_matcher( std::vector< stream_interface * > const & profiles )
{
std::vector< std::shared_ptr< matcher > > matchers;
for( auto & p : profiles )
matchers.push_back( std::make_shared< identity_matcher >( p->get_unique_id(), p->get_stream_type() ) );

return create_timestamp_composite_matcher( matchers );
}


std::shared_ptr< matcher > matcher_factory::create_identity_matcher( stream_interface * profile )
{
return std::make_shared< identity_matcher >( profile->get_unique_id(), profile->get_stream_type() );
}

std::shared_ptr< matcher >
matcher_factory::create_frame_number_composite_matcher( std::vector< std::shared_ptr< matcher > > const & matchers )
{
return std::make_shared< frame_number_composite_matcher >( matchers );
}


std::shared_ptr< matcher >
matcher_factory::create_timestamp_composite_matcher( std::vector< std::shared_ptr< matcher > > const & matchers )
{
return std::make_shared< timestamp_composite_matcher >( matchers );
}


} // namespace librealsense
Loading

0 comments on commit 640d281

Please sign in to comment.