forked from realsenseai/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice-model.cpp
More file actions
3338 lines (2966 loc) · 149 KB
/
device-model.cpp
File metadata and controls
3338 lines (2966 loc) · 149 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs_advanced_mode.hpp>
#include <librealsense2/rs.hpp>
#include <rs-config.h>
#include <third-party/filesystem/glob.h>
#include <imgui.h>
#include <imgui_internal.h>
#include "imgui-fonts-karla.hpp"
#include "imgui-fonts-fontawesome.hpp"
#include "imgui-fonts-monofont.hpp"
#include "os.h"
#include "viewer.h"
#include "on-chip-calib.h"
#include "subdevice-model.h"
#include "device-model.h"
using namespace rs400;
using namespace nlohmann;
using namespace rs2::sw_update;
namespace rs2
{
void imgui_easy_theming(ImFont*& font_14, ImFont*& font_18, ImFont*& monofont)
{
ImGuiStyle& style = ImGui::GetStyle();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
const int OVERSAMPLE = config_file::instance().get(configurations::performance::font_oversample);
static const ImWchar icons_ranges[] = { 0xf000, 0xf999, 0 }; // will not be copied by AddFont* so keep in scope.
{
ImFontConfig config_words;
config_words.OversampleV = OVERSAMPLE;
config_words.OversampleH = OVERSAMPLE;
font_14 = io.Fonts->AddFontFromMemoryCompressedTTF(karla_regular_compressed_data, karla_regular_compressed_size, 16.f);
ImFontConfig config_glyphs;
config_glyphs.MergeMode = true;
config_glyphs.OversampleV = OVERSAMPLE;
config_glyphs.OversampleH = OVERSAMPLE;
font_14 = io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data,
font_awesome_compressed_size, 14.f, &config_glyphs, icons_ranges);
}
// Load 18px size fonts
{
ImFontConfig config_words;
config_words.OversampleV = OVERSAMPLE;
config_words.OversampleH = OVERSAMPLE;
font_18 = io.Fonts->AddFontFromMemoryCompressedTTF(karla_regular_compressed_data, karla_regular_compressed_size, 21.f, &config_words);
ImFontConfig config_glyphs;
config_glyphs.MergeMode = true;
config_glyphs.OversampleV = OVERSAMPLE;
config_glyphs.OversampleH = OVERSAMPLE;
font_18 = io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data,
font_awesome_compressed_size, 20.f, &config_glyphs, icons_ranges);
}
// Load monofont
{
ImFontConfig config_words;
config_words.OversampleV = OVERSAMPLE;
config_words.OversampleH = OVERSAMPLE;
monofont = io.Fonts->AddFontFromMemoryCompressedTTF(monospace_compressed_data, monospace_compressed_size, 15.f);
ImFontConfig config_glyphs;
config_glyphs.MergeMode = true;
config_glyphs.OversampleV = OVERSAMPLE;
config_glyphs.OversampleH = OVERSAMPLE;
monofont = io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data,
font_awesome_compressed_size, 14.f, &config_glyphs, icons_ranges);
}
style.WindowRounding = 0.0f;
style.ScrollbarRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg] = dark_window_background;
style.Colors[ImGuiCol_Border] = black;
style.Colors[ImGuiCol_BorderShadow] = transparent;
style.Colors[ImGuiCol_FrameBg] = dark_window_background;
style.Colors[ImGuiCol_ScrollbarBg] = scrollbar_bg;
style.Colors[ImGuiCol_ScrollbarGrab] = scrollbar_grab;
style.Colors[ImGuiCol_ScrollbarGrabHovered] = scrollbar_grab + 0.1f;
style.Colors[ImGuiCol_ScrollbarGrabActive] = scrollbar_grab + (-0.1f);
style.Colors[ImGuiCol_ComboBg] = dark_window_background;
style.Colors[ImGuiCol_CheckMark] = regular_blue;
style.Colors[ImGuiCol_SliderGrab] = regular_blue;
style.Colors[ImGuiCol_SliderGrabActive] = regular_blue;
style.Colors[ImGuiCol_Button] = button_color;
style.Colors[ImGuiCol_ButtonHovered] = button_color + 0.1f;
style.Colors[ImGuiCol_ButtonActive] = button_color + (-0.1f);
style.Colors[ImGuiCol_Header] = header_color;
style.Colors[ImGuiCol_HeaderActive] = header_color + (-0.1f);
style.Colors[ImGuiCol_HeaderHovered] = header_color + 0.1f;
style.Colors[ImGuiCol_TitleBg] = title_color;
style.Colors[ImGuiCol_TitleBgCollapsed] = title_color;
style.Colors[ImGuiCol_TitleBgActive] = header_color;
}
void open_issue(std::string body)
{
std::string link = "https://github.com/IntelRealSense/librealsense/issues/new?body=" + url_encode(body);
open_url(link.c_str());
}
void open_issue(const device_models_list& devices)
{
std::stringstream ss;
rs2_error* e = nullptr;
ss << "| | |\n";
ss << "|---|---|\n";
ss << "|**librealsense**|" << api_version_to_string(rs2_get_api_version(&e)) << (is_debug() ? " DEBUG" : " RELEASE") << "|\n";
ss << "|**OS**|" << get_os_name() << "|\n";
for (auto& dm : devices)
{
for (auto& kvp : dm->infos)
{
if (kvp.first != "Recommended Firmware Version" &&
kvp.first != "Debug Op Code" &&
kvp.first != "Physical Port" &&
kvp.first != "Product Id")
ss << "|**" << kvp.first << "**|" << kvp.second << "|\n";
}
}
ss << "\nPlease provide a description of the problem";
open_issue(ss.str());
}
template <typename T>
std::string safe_call(T t)
{
try
{
t();
return "";
}
catch (const error& e)
{
return error_to_string(e);
}
catch (const std::exception& e)
{
return e.what();
}
catch (...)
{
return "Unknown error occurred";
}
}
std::pair<std::string, std::string> get_device_name(const device& dev)
{
// retrieve device name
std::string name = (dev.supports(RS2_CAMERA_INFO_NAME)) ? dev.get_info(RS2_CAMERA_INFO_NAME) : "Unknown";
// retrieve device serial number
std::string serial = (dev.supports(RS2_CAMERA_INFO_SERIAL_NUMBER)) ? dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER) : "Unknown";
std::stringstream s;
if (dev.is<playback>())
{
auto playback_dev = dev.as<playback>();
s << "Playback device: ";
name += (rsutils::string::from() << " (File: " << playback_dev.file_name() << ")");
}
s << std::setw(25) << std::left << name;
return std::make_pair(s.str(), serial); // push name and sn to list
}
void device_model::reset()
{
syncer->remove_syncer(dev_syncer);
subdevices.resize(0);
_recorder.reset();
}
device_model::~device_model()
{
for (auto&& n : related_notifications) n->dismiss(false);
_updates->set_device_status(*_updates_profile, false);
}
bool device_model::check_for_bundled_fw_update(const rs2::context &ctx, std::shared_ptr<notifications_model> not_model , bool reset_delay )
{
// LibRS can have a "bundled" FW binary downloaded during CMake. That's the version
// "available" to us, but it may not be there (e.g., no internet connection to download
// it). Lacking an available version, we try to let the user choose a "recommended"
// version for download. The recommended version is defined by the device (and comes
// from a #define).
// 'notification_type_is_displayed()' is used to detect if fw_update notification is on to avoid displaying it during FW update process when
// the device enters recovery mode
if( ! not_model->notification_type_is_displayed< fw_update_notification_model >()
&& ( dev.is< updatable >() || dev.is< update_device >() ) )
{
std::string fw;
std::string recommended_fw_ver;
int product_line = 0;
// Override with device info if info is available
if (dev.is<updatable>())
{
fw = dev.supports( RS2_CAMERA_INFO_FIRMWARE_VERSION )
? dev.get_info( RS2_CAMERA_INFO_FIRMWARE_VERSION )
: "";
recommended_fw_ver = dev.supports(RS2_CAMERA_INFO_RECOMMENDED_FIRMWARE_VERSION)
? dev.get_info(RS2_CAMERA_INFO_RECOMMENDED_FIRMWARE_VERSION)
: "";
}
product_line = dev.supports(RS2_CAMERA_INFO_PRODUCT_LINE)
? parse_product_line(dev.get_info(RS2_CAMERA_INFO_PRODUCT_LINE))
: -1; // invalid product line, will be handled later on
bool allow_rc_firmware = config_file::instance().get_or_default(
configurations::update::allow_rc_firmware,
false );
bool is_rc = ( product_line == RS2_PRODUCT_LINE_D400 ) && allow_rc_firmware;
std::string pid = dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID);
std::string available_fw_ver = get_available_firmware_version( product_line, pid);
std::shared_ptr< firmware_update_manager > manager = nullptr;
if( dev.is<update_device>() || is_upgradeable( fw, available_fw_ver) )
{
recommended_fw_ver = available_fw_ver;
auto image = get_default_fw_image(product_line, pid);
if (image.empty())
{
not_model->add_log("could not detect a bundled FW version for the connected device", RS2_LOG_SEVERITY_WARN);
return false;
}
manager = std::make_shared< firmware_update_manager >( not_model,
*this,
dev,
ctx,
image,
true );
}
auto dev_name = get_device_name(dev);
if( dev.is<update_device>() || is_upgradeable( fw, recommended_fw_ver) )
{
std::stringstream msg;
if (dev.is<update_device>())
{
msg << dev_name.first << "\n(S/N " << dev.get_info(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID) << ")\n";
}
else
{
msg << dev_name.first << " (S/N " << dev_name.second << ")\n"
<< "Current Version: " << fw << "\n";
}
if (is_rc)
msg << "Release Candidate: " << recommended_fw_ver << " Pre-Release";
else
msg << "Recommended Version: " << recommended_fw_ver;
auto n = std::make_shared< fw_update_notification_model >( msg.str(),
manager,
false );
// The FW update delay ID include the dismissed recommended version and the device serial number
// This way a newer FW recommended version will not be dismissed
n->delay_id = "fw_update_alert." + recommended_fw_ver + "." + dev_name.second;
n->enable_complex_dismiss = true;
// If a delay request received in the past, reset it.
if( reset_delay ) n->reset_delay();
if( ! n->is_delayed() )
{
not_model->add_notification( n );
related_notifications.push_back( n );
return true;
}
}
else
{
if( ! fw.empty() && ! recommended_fw_ver.empty() )
{
std::stringstream msg;
msg << "Current FW >= Bundled FW for: " << dev_name.first << " (S/N " << dev_name.second << ")\n"
<< "Current Version: " << fw << "\n"
<< "Recommended Version: " << recommended_fw_ver;
not_model->add_log(msg.str(), RS2_LOG_SEVERITY_DEBUG);
}
}
}
return false;
}
void device_model::refresh_notifications(viewer_model& viewer)
{
for (auto&& n : related_notifications) n->dismiss(false);
auto name = get_device_name(dev);
// Inhibit on DQT / Playback device
if( _allow_remove && ( ! dev.is< playback >() ) )
check_for_device_updates(viewer);
if ((bool)config_file::instance().get(configurations::update::recommend_calibration))
{
for (auto&& model : subdevices)
{
if (model->supports_on_chip_calib())
{
// Make sure we don't spam calibration remainders too often:
time_t rawtime;
time(&rawtime);
std::string id = rsutils::string::from() << configurations::viewer::last_calib_notice << "." << name.second;
long long last_time = config_file::instance().get_or_default(id.c_str(), (long long)0);
std::string msg = rsutils::string::from()
<< name.first << " (S/N " << name.second << ")";
auto manager = std::make_shared<on_chip_calib_manager>(viewer, model, *this, dev);
auto n = std::make_shared<autocalib_notification_model>(
msg, manager, false);
// Recommend calibration once a week per device
if (rawtime - last_time < 60)
{
n->snoozed = true;
}
// NOTE: For now do not pre-emptively suggest auto-calibration
// TODO: Revert in later release
//viewer.not_model->add_notification(n);
//related_notifications.push_back(n);
}
}
}
}
device_model::device_model(device& dev, std::string& error_message, viewer_model& viewer, bool new_device_connected, bool remove)
: dev(dev),
_calib_model(dev, viewer.not_model),
syncer(viewer.syncer),
_update_readonly_options_timer(std::chrono::seconds(6)),
_detected_objects(std::make_shared< atomic_objects_in_frame >()),
_updates(viewer.updates),
_updates_profile(std::make_shared<dev_updates_profile::update_profile>()),
_allow_remove(remove)
{
auto name = get_device_name(dev);
id = rsutils::string::from() << name.first << ", " << name.second;
for (auto&& sub : dev.query_sensors())
{
auto s = std::make_shared<sensor>(sub);
auto objects = std::make_shared< atomic_objects_in_frame >();
// checking if the sensor is color_sensor or is D405 (with integrated RGB in depth sensor)
if (s->is<color_sensor>() || (dev.supports(RS2_CAMERA_INFO_PRODUCT_ID) && !strcmp(dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID), "0B5B")))
objects = _detected_objects;
auto model = std::make_shared<subdevice_model>(dev, std::make_shared<sensor>(sub), objects, error_message, viewer, new_device_connected);
subdevices.push_back(model);
}
// Initialize static camera info:
for (auto i = 0; i < RS2_CAMERA_INFO_COUNT; i++)
{
auto info = static_cast<rs2_camera_info>(i);
try
{
if (dev.supports(info))
{
auto value = dev.get_info(info);
infos.push_back({ std::string(rs2_camera_info_to_string(info)),
std::string(value) });
}
}
catch (...)
{
infos.push_back({ std::string(rs2_camera_info_to_string(info)),
std::string("???") });
}
}
if (dev.is<playback>())
{
for (auto&& sub : subdevices)
{
for (auto&& p : sub->profiles)
{
sub->stream_enabled[p.unique_id()] = true;
}
}
play_defaults(viewer);
}
refresh_notifications(viewer);
auto path = get_folder_path( special_folder::user_documents );
path += "librealsense2/presets/";
try
{
std::string name = dev.get_info(RS2_CAMERA_INFO_NAME);
std::smatch match;
if( ! std::regex_search( name, match, std::regex( "^Intel RealSense (\\S+)" ) ) )
throw std::runtime_error( "cannot parse device name from '" + name + "'" );
glob(
path,
std::string( match[1] ) + " *.preset",
[&]( std::string const & file ) {
advanced_mode_settings_file_names.insert( path + file );
},
false ); // recursive
}
catch( const std::exception & e )
{
LOG_WARNING( "Exception caught trying to detect presets: " << e.what() );
}
}
void device_model::play_defaults(viewer_model& viewer)
{
if (!dev_syncer)
dev_syncer = viewer.syncer->create_syncer();
for (auto&& sub : subdevices)
{
if (!sub->streaming)
{
std::vector<rs2::stream_profile> profiles;
try
{
profiles = sub->get_selected_profiles();
}
catch (...)
{
continue;
}
if (profiles.empty())
continue;
std::string friendly_name = sub->s->get_info(RS2_CAMERA_INFO_NAME);
if ((friendly_name.find("Tracking") != std::string::npos) ||
(friendly_name.find("Motion") != std::string::npos))
{
viewer.synchronization_enable_prev_state = viewer.synchronization_enable.load();
viewer.synchronization_enable = false;
}
sub->play(profiles, viewer, dev_syncer);
for (auto&& profile : profiles)
{
viewer.begin_stream(sub, profile);
}
}
}
}
void device_model::start_recording(const std::string& path, std::string& error_message)
{
if (_recorder != nullptr)
{
return; //already recording
}
try
{
int compression_mode = config_file::instance().get(configurations::record::compression_mode);
if (compression_mode == 2)
_recorder = std::make_shared<recorder>(path, dev);
else
_recorder = std::make_shared<recorder>(path, dev, compression_mode == 0);
for (auto&& sub_dev_model : subdevices)
{
sub_dev_model->_is_being_recorded = true;
}
is_recording = true;
}
catch (const rs2::error& e)
{
error_message = error_to_string(e);
}
catch (const std::exception& e)
{
error_message = e.what();
}
}
void device_model::stop_recording(viewer_model& viewer)
{
auto saved_to_filename = _recorder->filename();
_recorder.reset();
for (auto&& sub_dev_model : subdevices)
{
sub_dev_model->_is_being_recorded = false;
}
is_recording = false;
notification_data nd{ rsutils::string::from() << "Saved recording to:\n" << saved_to_filename,
RS2_LOG_SEVERITY_INFO,
RS2_NOTIFICATION_CATEGORY_UNKNOWN_ERROR };
viewer.not_model->add_notification(nd);
}
void device_model::pause_record()
{
_recorder->pause();
}
void device_model::resume_record()
{
_recorder->resume();
}
int device_model::draw_playback_controls(ux_window& window, ImFont* font, viewer_model& viewer)
{
auto p = dev.as<playback>();
rs2_playback_status current_playback_status = p.current_status();
const int playback_control_height = 35;
const float combo_box_width = 90.f;
const float icon_width = 28;
const float line_width = 255; //Ideally should use: ImGui::GetContentRegionMax().x
//Line looks like this ("-" == space, "[]" == icon, "[ ]" == combo_box): |-[]-[]-[]-[]-[]-[ ]-[]-|
const int num_icons_in_line = 6;
const int num_combo_boxes_in_line = 1;
const int num_spaces_in_line = num_icons_in_line + num_combo_boxes_in_line + 1;
const float required_row_width = (num_combo_boxes_in_line * combo_box_width) + (num_icons_in_line * icon_width);
float space_width = std::max(line_width - required_row_width, 0.f) / num_spaces_in_line;
ImVec2 button_dim = { icon_width, icon_width };
const bool supports_playback_step = current_playback_status == RS2_PLAYBACK_STATUS_PAUSED;
ImGui::PushFont(font);
//////////////////// Step Backwards Button ////////////////////
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + space_width);
std::string label = rsutils::string::from() << textual_icons::step_backward << "##Step Backward " << id;
if (ImGui::ButtonEx(label.c_str(), button_dim, supports_playback_step ? 0 : ImGuiButtonFlags_Disabled))
{
int fps = 0;
for (auto&& s : viewer.streams)
{
if (s.second.profile.fps() > fps)
fps = s.second.profile.fps();
}
auto curr_frame = p.get_position();
uint64_t step = fps ? uint64_t(1000.0 / (float)fps * 1e6) : 1000000ULL;
if (curr_frame >= step)
{
p.seek(std::chrono::nanoseconds(curr_frame - step));
}
}
if (ImGui::IsItemHovered())
{
std::string tooltip = rsutils::string::from() << "Step Backwards" << (supports_playback_step ? "" : "(Not available)");
ImGui::SetTooltip("%s", tooltip.c_str());
}
ImGui::SameLine();
//////////////////// Step Backwards Button ////////////////////
//////////////////// Stop Button ////////////////////
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + space_width);
label = rsutils::string::from() << textual_icons::stop << "##Stop Playback " << id;
if (ImGui::ButtonEx(label.c_str(), button_dim))
{
bool prev = _playback_repeat;
_playback_repeat = false;
p.stop();
_playback_repeat = prev;
}
if (ImGui::IsItemHovered())
{
std::string tooltip = rsutils::string::from() << "Stop Playback";
ImGui::SetTooltip("%s", tooltip.c_str());
}
ImGui::SameLine();
//////////////////// Stop Button ////////////////////
//////////////////// Pause/Play Button ////////////////////
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + space_width);
if (current_playback_status == RS2_PLAYBACK_STATUS_PAUSED || current_playback_status == RS2_PLAYBACK_STATUS_STOPPED)
{
label = rsutils::string::from() << textual_icons::play << "##Play " << id;
if (ImGui::ButtonEx(label.c_str(), button_dim))
{
if (current_playback_status == RS2_PLAYBACK_STATUS_STOPPED)
{
play_defaults(viewer);
}
else
{
syncer->on_frame = [] {};
for (auto&& s : subdevices)
{
s->on_frame = [] {};
if (s->streaming)
s->resume();
}
viewer.paused = false;
p.resume();
}
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip(current_playback_status == RS2_PLAYBACK_STATUS_PAUSED ? "Resume Playback" : "Start Playback");
}
}
else
{
label = rsutils::string::from() << textual_icons::pause << "##Pause Playback " << id;
if (ImGui::ButtonEx(label.c_str(), button_dim))
{
p.pause();
for (auto&& s : subdevices)
{
if (s->streaming)
s->pause();
}
viewer.paused = true;
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("Pause Playback");
}
}
ImGui::SameLine();
//////////////////// Pause/Play Button ////////////////////
//////////////////// Step Forward Button ////////////////////
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + space_width);
label = rsutils::string::from() << textual_icons::step_forward << "##Step Forward " << id;
if (ImGui::ButtonEx(label.c_str(), button_dim, supports_playback_step ? 0 : ImGuiButtonFlags_Disabled))
{
int fps = 0;
for (auto&& s : viewer.streams)
{
if (s.second.profile.fps() > fps)
fps = s.second.profile.fps();
}
auto curr_frame = p.get_position();
uint64_t step = fps ? uint64_t(1000.0 / (float)fps * 1e6) : 1000000ULL;
p.seek(std::chrono::nanoseconds(curr_frame + step));
}
if (ImGui::IsItemHovered())
{
std::string tooltip = rsutils::string::from() << "Step Forward" << (supports_playback_step ? "" : "(Not available)");
ImGui::SetTooltip("%s", tooltip.c_str());
}
ImGui::SameLine();
//////////////////// Step Forward Button ////////////////////
/////////////////// Repeat Button /////////////////////
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + space_width);
if (_playback_repeat)
{
ImGui::PushStyleColor(ImGuiCol_Text, light_blue);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, light_blue);
}
else
{
ImGui::PushStyleColor(ImGuiCol_Text, white);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, white);
}
label = rsutils::string::from() << textual_icons::repeat << "##Repeat " << id;
if (ImGui::ButtonEx(label.c_str(), button_dim))
{
_playback_repeat = !_playback_repeat;
}
if (ImGui::IsItemHovered())
{
std::string tooltip = rsutils::string::from() << (_playback_repeat ? "Disable " : "Enable ") << "Repeat ";
ImGui::SetTooltip("%s", tooltip.c_str());
}
ImGui::PopStyleColor(2);
ImGui::SameLine();
/////////////////// Repeat Button /////////////////////
//////////////////// Speed combo box ////////////////////
auto pos = ImGui::GetCursorPos();
const float speed_combo_box_v_alignment = 3.f;
ImGui::SetCursorPos({ pos.x + space_width, pos.y + speed_combo_box_v_alignment });
ImGui::PushItemWidth(combo_box_width);
ImGui::PushStyleColor(ImGuiCol_FrameBg, sensor_bg);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, sensor_bg);
label = rsutils::string::from() << "## " << id;
if (ImGui::Combo(label.c_str(), &playback_speed_index, "Speed: x0.25\0Speed: x0.5\0Speed: x1\0Speed: x1.5\0Speed: x2\0\0", -1, false))
{
float speed = 1;
switch (playback_speed_index)
{
case 0: speed = 0.25f; break;
case 1: speed = 0.5f; break;
case 2: speed = 1.0f; break;
case 3: speed = 1.5f; break;
case 4: speed = 2.0f; break;
default:
throw std::runtime_error( rsutils::string::from() << "Speed #" << playback_speed_index << " is unhandled");
}
p.set_playback_speed(speed);
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("Change playback speed rate");
}
ImGui::PopStyleColor(2);
ImGui::SameLine();
//Restore Y movement
pos = ImGui::GetCursorPos();
ImGui::SetCursorPos({ pos.x, pos.y - speed_combo_box_v_alignment });
//////////////////// Speed combo box ////////////////////
//////////////////// Info Icon ////////////////////
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + space_width);
draw_info_icon(window, font, button_dim);
//////////////////// Info Icon ////////////////////
ImGui::PopFont();
return playback_control_height;
}
std::string device_model::pretty_time(std::chrono::nanoseconds duration)
{
using namespace std::chrono;
auto hhh = duration_cast<hours>(duration);
duration -= hhh;
auto mm = duration_cast<minutes>(duration);
duration -= mm;
auto ss = duration_cast<seconds>(duration);
duration -= ss;
auto ms = duration_cast<milliseconds>(duration);
std::ostringstream stream;
stream << std::setfill('0') << std::setw(hhh.count() >= 10 ? 2 : 1) << hhh.count() << ':' <<
std::setfill('0') << std::setw(2) << mm.count() << ':' <<
std::setfill('0') << std::setw(2) << ss.count() << '.' <<
std::setfill('0') << std::setw(3) << ms.count();
return stream.str();
}
int device_model::draw_seek_bar()
{
auto pos = ImGui::GetCursorPos();
auto p = dev.as<playback>();
//rs2_playback_status current_playback_status = p.current_status();
int64_t playback_total_duration = p.get_duration().count();
auto progress = p.get_position();
double part = (1.0 * progress) / playback_total_duration;
seek_pos = static_cast<int>(std::max(0.0, std::min(part, 1.0)) * 100);
auto playback_status = p.current_status();
if (seek_pos != 0 && playback_status == RS2_PLAYBACK_STATUS_STOPPED)
{
seek_pos = 0;
}
float seek_bar_width = 300.f;
ImGui::PushItemWidth(seek_bar_width);
std::string label1 = "## " + id;
if (ImGui::SeekSlider(label1.c_str(), &seek_pos, ""))
{
//Seek was dragged
if (playback_status != RS2_PLAYBACK_STATUS_STOPPED) //Ignore seek when playback is stopped
{
auto duration_db = std::chrono::duration_cast<std::chrono::duration<double, std::nano>>(p.get_duration());
auto single_percent = duration_db.count() / 100;
auto seek_time = std::chrono::duration<double, std::nano>(seek_pos * single_percent);
p.seek(std::chrono::duration_cast<std::chrono::nanoseconds>(seek_time));
}
}
ImGui::SetCursorPos({ pos.x, pos.y + 17 });
std::string time_elapsed = pretty_time(std::chrono::nanoseconds(progress));
std::string duration_str = pretty_time(std::chrono::nanoseconds(playback_total_duration));
ImGui::Text("%s", time_elapsed.c_str());
ImGui::SameLine();
float pos_y = ImGui::GetCursorPosY();
ImGui::SetCursorPos({ pos.x + seek_bar_width - 45 , pos_y });
ImGui::Text("%s", duration_str.c_str());
return 50;
}
int device_model::draw_playback_panel(ux_window& window, ImFont* font, viewer_model& view)
{
ImGui::PushStyleColor(ImGuiCol_Button, sensor_bg);
ImGui::PushStyleColor(ImGuiCol_Text, light_grey);
ImGui::PushStyleColor(ImGuiCol_PopupBg, almost_white_bg);
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, from_rgba(0, 0xae, 0xff, 255));
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, white);
auto pos = ImGui::GetCursorPos();
auto controls_height = draw_playback_controls(window, font, view);
float seek_bar_left_alignment = 4.f;
ImGui::SetCursorPos({ pos.x + seek_bar_left_alignment, pos.y + controls_height });
ImGui::PushFont(font);
auto seek_bar_height = draw_seek_bar();
ImGui::PopFont();
ImGui::PopStyleColor(5);
return controls_height + seek_bar_height;
}
bool device_model::prompt_toggle_advanced_mode(bool enable_advanced_mode, const std::string& message_text, std::vector<std::string>& restarting_device_info, viewer_model& view, ux_window& window, const std::string& error_message)
{
bool keep_showing = true;
bool yes_was_chosen = false;
if (yes_no_dialog("Advanced Mode", message_text, yes_was_chosen, window, error_message))
{
if (yes_was_chosen)
{
dev.as<advanced_mode>().toggle_advanced_mode(enable_advanced_mode);
restarting_device_info = get_device_info(dev, false);
view.not_model->add_log(enable_advanced_mode ? "Turning on advanced mode..." : "Turning off advanced mode...");
}
keep_showing = false;
}
return keep_showing;
}
bool device_model::draw_advanced_controls(viewer_model& view, ux_window& window, std::string& error_message)
{
bool was_set = false;
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, { 0.9f, 0.9f, 0.9f, 1 });
auto is_advanced_mode = dev.is<advanced_mode>();
if (is_advanced_mode && ImGui::TreeNode("Advanced Controls"))
{
try
{
auto advanced = dev.as<advanced_mode>();
if (advanced.is_enabled())
{
draw_advanced_mode_controls(advanced, amc, get_curr_advanced_controls, was_set, error_message);
}
else
{
ImGui::TextColored(redish, "Device is not in advanced mode");
std::string button_text = rsutils::string::from() << "Turn on Advanced Mode" << "##" << id;
static bool show_yes_no_modal = false;
if (ImGui::Button(button_text.c_str(), ImVec2{ 226, 0 }))
{
show_yes_no_modal = true;
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("Advanced mode is a persistent camera state unlocking calibration formats and depth generation controls\nYou can always reset the camera to factory defaults by disabling advanced mode");
}
if (show_yes_no_modal)
{
show_yes_no_modal = prompt_toggle_advanced_mode(true, "\t\tAre you sure you want to turn on Advanced Mode?\t\t", restarting_device_info, view, window, error_message);
}
}
}
catch (const std::exception& ex)
{
error_message = ex.what();
}
ImGui::TreePop();
}
ImGui::PopStyleColor();
return was_set;
}
void device_model::draw_info_icon(ux_window& window, ImFont* font, const ImVec2& size)
{
std::string info_button_name = rsutils::string::from() << textual_icons::info_circle << "##" << id;
auto info_button_color = show_device_info ? light_blue : light_grey;
ImGui::PushStyleColor(ImGuiCol_Text, info_button_color);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, info_button_color);
if (ImGui::Button(info_button_name.c_str(), size))
{
show_device_info = !show_device_info;
}
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("%s", show_device_info ? "Hide Device Details" : "Show Device Details");
window.link_hovered();
}
ImGui::PopStyleColor(2);
}
void device_model::begin_update_unsigned(viewer_model& viewer, std::string& error_message)
{
try
{
std::vector<uint8_t> data;
auto ret = file_dialog_open(open_file, "Unsigned Firmware Image\0*.bin\0", NULL, NULL);
if (ret)
{
std::ifstream file(ret, std::ios::binary | std::ios::in);
if (file.good())
{
data = std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
}
else
{
error_message = rsutils::string::from() << "Could not open file '" << ret << "'";
return;
}
}
else return; // Aborted by the user
auto manager = std::make_shared<firmware_update_manager>(viewer.not_model, *this, dev, viewer.ctx, data, false);
auto n = std::make_shared<fw_update_notification_model>(
"Manual Update requested", manager, true);
viewer.not_model->add_notification(n);
for (auto&& n : related_notifications)
if (dynamic_cast<fw_update_notification_model*>(n.get()))
n->dismiss(false);
auto invoke = [n](std::function<void()> action) {
n->invoke(action);
};
manager->start(invoke);
}
catch (const error& e)
{
error_message = error_to_string(e);
}
catch (const std::exception& e)
{
error_message = e.what();
}
}
void device_model::begin_update(std::vector<uint8_t> data, viewer_model& viewer, std::string& error_message)
{
try
{
if (data.size() == 0)
{
auto ret = file_dialog_open(open_file, "Signed Firmware Image\0*.bin\0", NULL, NULL);
if (ret)
{
std::ifstream file(ret, std::ios::binary | std::ios::in);
if (file.good())
{
data = std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
}
else
{
error_message = rsutils::string::from() << "Could not open file '" << ret << "'";
return;
}
}
else return; // Aborted by the user
}
auto manager = std::make_shared<firmware_update_manager>(viewer.not_model, *this, dev, viewer.ctx, data, true);
auto n = std::make_shared<fw_update_notification_model>(
"Manual Update requested", manager, true);
viewer.not_model->add_notification(n);