@@ -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
0 commit comments