Skip to content

Commit c2e261e

Browse files
authored
PR #13840 from OhadMeir: Better handling of DDS streams information
2 parents ed59975 + 1136412 commit c2e261e

File tree

14 files changed

+147
-90
lines changed

14 files changed

+147
-90
lines changed

include/librealsense2/h/rs_frame.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ int rs2_get_frame_points_count(const rs2_frame* frame, rs2_error** error);
262262
*/
263263
const rs2_stream_profile* rs2_get_frame_stream_profile(const rs2_frame* frame, rs2_error** error);
264264

265+
/**
266+
* Returns the stream profile name
267+
* \param[in] profile stream profile whose name we want to get
268+
* \param[out] error If non-null, receives any error that occurs during this call, otherwise, errors are ignored
269+
* \return Stream name, as a null terminated string
270+
*/
271+
const char * rs2_get_stream_profile_name( const rs2_stream_profile * profile, rs2_error ** error );
272+
265273
/**
266274
* Test if the given frame can be extended to the requested extension
267275
* \param[in] frame Realsense frame

include/librealsense2/hpp/rs_frame.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,19 @@ namespace rs2
112112
*/
113113
std::string stream_name() const
114114
{
115-
std::stringstream ss;
116-
ss << rs2_stream_to_string(stream_type());
117-
if (stream_index() != 0) ss << " " << stream_index();
118-
return ss.str();
115+
rs2_error * e = nullptr;
116+
std::string name = rs2_get_stream_profile_name( _profile, &e );
117+
118+
if( name.empty() )
119+
{
120+
std::stringstream ss;
121+
ss << rs2_stream_to_string( stream_type() );
122+
if( stream_index() != 0 )
123+
ss << " " << stream_index();
124+
name = ss.str();
125+
}
126+
127+
return name;
119128
}
120129

121130
/**

src/core/stream-profile-interface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <memory>
1111
#include <vector>
1212
#include <iosfwd>
13+
#include <string>
1314

1415

1516
namespace librealsense {
@@ -29,6 +30,9 @@ class stream_profile_interface
2930
virtual int get_tag() const = 0;
3031
virtual void tag_profile( int tag ) = 0;
3132

33+
virtual const char * get_name() = 0;
34+
virtual void set_name( const std::string & name ) = 0;
35+
3236
virtual std::shared_ptr< stream_profile_interface > clone() const = 0;
3337
virtual rs2_stream_profile * get_c_wrapper() const = 0;
3438
virtual void set_c_wrapper( rs2_stream_profile * wrapper ) = 0;

src/dds/rs-dds-device-proxy.cpp

Lines changed: 90 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &
229229
_software_sensors.push_back( sensor_info.proxy );
230230
}
231231
auto stream_type = to_rs2_stream_type( stream->type_string() );
232-
int index = get_index_from_stream_name( stream->name() );
232+
int index = get_unique_index_in_sensor( sensor_info.proxy, stream->name(), stream_type );
233233
sid_index sidx( environment::get_instance().generate_stream_id(), index);
234234
sid_index type_and_index( stream_type, index );
235235
_stream_name_to_librs_stream[stream->name()]
@@ -253,29 +253,44 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &
253253
for( auto & profile : profiles )
254254
{
255255
//LOG_DEBUG( " " << profile->details_to_string() );
256-
if( video_stream )
256+
try
257257
{
258-
auto video_profile = std::static_pointer_cast< realdds::dds_video_stream_profile >( profile );
259-
auto raw_stream_profile = sensor.add_video_stream(
260-
to_rs2_video_stream( stream_type, sidx, video_profile, video_stream->get_intrinsics() ),
261-
profile == default_profile );
262-
_stream_name_to_profiles[stream->name()].push_back( raw_stream_profile );
258+
if( video_stream )
259+
{
260+
auto video_profile = std::static_pointer_cast< realdds::dds_video_stream_profile >( profile );
261+
auto raw_stream_profile = sensor.add_video_stream(
262+
to_rs2_video_stream( stream_type, sidx, video_profile, video_stream->get_intrinsics() ),
263+
profile == default_profile, stream->name() );
264+
_stream_name_to_profiles[stream->name()].push_back( raw_stream_profile );
265+
}
266+
else if( motion_stream )
267+
{
268+
auto motion_profile = std::static_pointer_cast< realdds::dds_motion_stream_profile >( profile );
269+
auto raw_stream_profile = sensor.add_motion_stream(
270+
to_rs2_motion_stream( stream_type, sidx, motion_profile, motion_stream->get_gyro_intrinsics() ),
271+
profile == default_profile, stream->name() );
272+
_stream_name_to_profiles[stream->name()].push_back( raw_stream_profile );
273+
}
263274
}
264-
else if( motion_stream )
275+
catch( std::exception const& e )
265276
{
266-
auto motion_profile = std::static_pointer_cast< realdds::dds_motion_stream_profile >( profile );
267-
auto raw_stream_profile = sensor.add_motion_stream(
268-
to_rs2_motion_stream( stream_type, sidx, motion_profile, motion_stream->get_gyro_intrinsics() ),
269-
profile == default_profile );
270-
_stream_name_to_profiles[stream->name()].push_back( raw_stream_profile );
277+
LOG_ERROR( "Cannot add profile " << profile->details_to_string() << " to stream " << stream->name()
278+
<< ". " << e.what() );
271279
}
272280
// NOTE: the raw profile will be cloned and overriden by the format converter!
273281
}
274282

275283
auto & options = stream->options();
276284
for( auto & option : options )
277285
{
278-
sensor_info.proxy->add_option( option );
286+
try
287+
{
288+
sensor_info.proxy->add_option( option );
289+
}
290+
catch( std::exception const& e )
291+
{
292+
LOG_ERROR( e.what() );
293+
}
279294
}
280295

281296
auto & recommended_filters = stream->recommended_filters();
@@ -302,28 +317,32 @@ dds_device_proxy::dds_device_proxy( std::shared_ptr< const device_info > const &
302317
auto source_profile = source_profiles[0];
303318

304319
sid_index type_and_index( source_profile->get_stream_type(), source_profile->get_stream_index() );
305-
sid_index sidx = sensor_info.type_and_index_to_dds_stream_sidx.at( type_and_index );
306-
307-
auto & streams = sensor_proxy->streams();
308-
auto stream_iter = streams.find( sidx );
309-
if( stream_iter == streams.end() )
320+
const auto & it = sensor_info.type_and_index_to_dds_stream_sidx.find( type_and_index );
321+
if( it != sensor_info.type_and_index_to_dds_stream_sidx.end())
310322
{
311-
LOG_ERROR( "No dds stream " << sidx.to_string() << " found for '" << sensor_name << "' " << profile
312-
<< " -> " << source_profile << " " << type_and_index.to_string() );
313-
continue;
314-
}
323+
sid_index sidx = (*it).second;
324+
325+
auto & streams = sensor_proxy->streams();
326+
auto stream_iter = streams.find( sidx );
327+
if( stream_iter == streams.end() )
328+
{
329+
LOG_ERROR( "No dds stream " << sidx.to_string() << " found for '" << sensor_name << "' " << profile
330+
<< " -> " << source_profile << " " << type_and_index.to_string() );
331+
continue;
332+
}
315333

316-
//LOG_DEBUG( " " << profile << " -> " << source_profile << " " << type_and_index.to_string() );
317-
profile->set_unique_id( sidx.sid ); // Was lost on clone
334+
//LOG_DEBUG( " " << profile << " -> " << source_profile << " " << type_and_index.to_string() );
335+
profile->set_unique_id( sidx.sid ); // Was lost on clone
318336

319-
// NOTE: the 'initialization_done' call above creates target profiles from the raw profiles we supplied it.
320-
// The raw profile intrinsics will be overriden to call the target's intrinsics function (which by default
321-
// calls the raw again, creating an infinite loop), so we must override the target:
322-
set_profile_intrinsics( profile, stream_iter->second );
337+
// NOTE: the 'get_stream_profiles' call above creates target profiles from the raw profiles we supplied it.
338+
// The raw profile intrinsics will be overriden to call the target's intrinsics function (which by default
339+
// calls the raw again, creating an infinite loop), so we must override the target:
340+
set_profile_intrinsics( profile, stream_iter->second );
323341

324-
_stream_name_to_profiles.at( stream_iter->second->name() ).push_back( profile ); // For extrinsics
342+
_stream_name_to_profiles.at( stream_iter->second->name() ).push_back( profile ); // For extrinsics
325343

326-
tag_default_profile_of_stream( profile, stream_iter->second );
344+
tag_default_profile_of_stream( profile, stream_iter->second );
345+
}
327346
}
328347
}
329348

@@ -432,12 +451,48 @@ int dds_device_proxy::get_index_from_stream_name( const std::string & name ) con
432451
{
433452
int index = 0;
434453

435-
auto delimiter_pos = name.find( '_' );
454+
// If camera sends stream index the info is appended to the stream name with underscore after the name
455+
// e.g 'Infrared_1'. However, there might be other underscores in the name, e.g. 'Compressed_Color_1'
456+
auto delimiter_pos = name.find_last_of( '_' );
436457
if( delimiter_pos != std::string::npos )
437458
{
438459
std::string index_as_string = name.substr( delimiter_pos + 1, name.size() );
439-
index = std::stoi( index_as_string );
460+
try
461+
{
462+
index = std::stoi(index_as_string);
463+
}
464+
catch (...)
465+
{
466+
//Do nothing, return default index
467+
}
468+
}
469+
470+
return index;
471+
}
472+
473+
474+
int dds_device_proxy::get_unique_index_in_sensor( const std::shared_ptr< dds_sensor_proxy > & sensor,
475+
const std::string & stream_name, rs2_stream stream_type ) const
476+
{
477+
bool index_adjusted = false;
478+
int index = get_index_from_stream_name( stream_name );
479+
480+
// Sending stream index as part of stream name is optional. We want to distinguish streams of same type in the
481+
// sensor so we create an internal, running index for it.
482+
do
483+
{
484+
index_adjusted = false;
485+
for( auto & stream : sensor->streams() )
486+
{
487+
// We can have multiple streams with same index (e.g. 0) but not two of the same type
488+
if( index == stream.first.index && stream_type == to_rs2_stream_type( stream.second->type_string() ) )
489+
{
490+
index++;
491+
index_adjusted = true;
492+
}
493+
}
440494
}
495+
while( index_adjusted );
441496

442497
return index;
443498
}
@@ -558,7 +613,8 @@ void dds_device_proxy::tag_default_profile_of_stream(
558613
// Superset = picked up in pipeline with enable_all_stream() config
559614
// We want color and depth to be default, and add infrared as superset
560615
int tag = PROFILE_TAG_SUPERSET;
561-
if( strcmp( stream->type_string(), "color" ) == 0 || strcmp( stream->type_string(), "depth" ) == 0 )
616+
if( ( strcmp( stream->type_string(), "color" ) == 0 && profile->get_stream_index() == 0 ) || // Now we have cameras with more then one color stream, take the first
617+
strcmp( stream->type_string(), "depth" ) == 0 )
562618
tag |= PROFILE_TAG_DEFAULT;
563619
else if( strcmp( stream->type_string(), "ir" ) != 0 || profile->get_stream_index() >= 2 )
564620
return; // leave untagged

src/dds/rs-dds-device-proxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class dds_device_proxy
5555
rsutils::subscription _metadata_subscription;
5656

5757
int get_index_from_stream_name( const std::string & name ) const;
58+
int get_unique_index_in_sensor( const std::shared_ptr< dds_sensor_proxy > & sensor,
59+
const std::string & stream_name, rs2_stream stream_type ) const;
5860
void set_profile_intrinsics( std::shared_ptr< stream_profile_interface > const & profile,
5961
const std::shared_ptr< realdds::dds_stream > & stream ) const;
6062
void set_video_profile_intrinsics( std::shared_ptr< stream_profile_interface > const & profile,

src/dds/rs-dds-sensor-proxy.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -79,45 +79,6 @@ void dds_sensor_proxy::add_dds_stream( sid_index sidx, std::shared_ptr< realdds:
7979
}
8080

8181

82-
std::shared_ptr< stream_profile_interface > dds_sensor_proxy::add_video_stream( rs2_video_stream video_stream,
83-
bool is_default )
84-
{
85-
auto profile = std::make_shared< video_stream_profile >();
86-
profile->set_dims( video_stream.width, video_stream.height );
87-
profile->set_format( video_stream.fmt );
88-
profile->set_framerate( video_stream.fps );
89-
profile->set_stream_index( video_stream.index );
90-
profile->set_stream_type( video_stream.type );
91-
profile->set_unique_id( video_stream.uid );
92-
profile->set_intrinsics( [=]() { //
93-
return video_stream.intrinsics;
94-
} );
95-
if( is_default )
96-
profile->tag_profile( profile_tag::PROFILE_TAG_DEFAULT );
97-
_sw_profiles.push_back( profile );
98-
99-
return profile;
100-
}
101-
102-
103-
std::shared_ptr< stream_profile_interface > dds_sensor_proxy::add_motion_stream( rs2_motion_stream motion_stream,
104-
bool is_default )
105-
{
106-
auto profile = std::make_shared< motion_stream_profile >();
107-
profile->set_format( motion_stream.fmt );
108-
profile->set_framerate( motion_stream.fps );
109-
profile->set_stream_index( motion_stream.index );
110-
profile->set_stream_type( motion_stream.type );
111-
profile->set_unique_id( motion_stream.uid );
112-
profile->set_intrinsics( [=]() { return motion_stream.intrinsics; } );
113-
if( is_default )
114-
profile->tag_profile( profile_tag::PROFILE_TAG_DEFAULT );
115-
_sw_profiles.push_back( profile );
116-
117-
return profile;
118-
}
119-
120-
12182
stream_profiles dds_sensor_proxy::init_stream_profiles()
12283
{
12384
auto profiles = get_raw_stream_profiles();

src/dds/rs-dds-sensor-proxy.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class dds_sensor_proxy : public software_sensor
7575
const std::string & get_name() const { return _name; }
7676

7777
void add_dds_stream( sid_index sidx, std::shared_ptr< realdds::dds_stream > const & stream );
78-
std::shared_ptr<stream_profile_interface> add_video_stream( rs2_video_stream video_stream, bool is_default ) override;
79-
std::shared_ptr<stream_profile_interface> add_motion_stream( rs2_motion_stream motion_stream, bool is_default ) override;
8078

8179
void open( const stream_profiles & profiles ) override;
8280
void start( rs2_frame_callback_sptr callback ) override;

src/proc/formats-converter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ stream_profiles formats_converter::get_all_possible_profiles( const stream_profi
123123
cloned_profile->set_format( target.format );
124124
cloned_profile->set_stream_index( target.index );
125125
cloned_profile->set_stream_type( target.stream );
126+
// UVC raw profile name is not set, default name can be created based on type and index, but raw UVC index is always 0.
127+
// Use temporary variable to generate name without changing original raw_profile object.
128+
auto && tmp_raw_profile = std::dynamic_pointer_cast< stream_profile_base >( raw_profile ).get();
129+
tmp_raw_profile->set_stream_index( target.index );
130+
cloned_profile->set_name( tmp_raw_profile->get_name() );
126131

127132
auto cloned_vsp = As< video_stream_profile, stream_profile_interface >( cloned_profile );
128133
if( cloned_vsp )

src/realsense.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ EXPORTS
7676
rs2_get_frame_stride_in_bytes
7777
rs2_get_frame_bits_per_pixel
7878
rs2_get_frame_stream_profile
79+
rs2_get_stream_profile_name
7980
rs2_get_frame_vertices
8081
rs2_get_frame_texture_coordinates
8182
rs2_get_frame_points_count

src/rs.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,13 @@ const rs2_stream_profile* rs2_get_frame_stream_profile(const rs2_frame* frame_re
14221422
}
14231423
HANDLE_EXCEPTIONS_AND_RETURN(nullptr, frame_ref)
14241424

1425+
const char * rs2_get_stream_profile_name( const rs2_stream_profile * profile, rs2_error ** error ) BEGIN_API_CALL
1426+
{
1427+
VALIDATE_NOT_NULL( profile );
1428+
return profile->profile->get_name();
1429+
}
1430+
HANDLE_EXCEPTIONS_AND_RETURN( nullptr, profile )
1431+
14251432
int rs2_get_frame_bits_per_pixel(const rs2_frame* frame_ref, rs2_error** error) BEGIN_API_CALL
14261433
{
14271434
VALIDATE_NOT_NULL(frame_ref);

0 commit comments

Comments
 (0)