void add_playback_device(context& ctx, std::vector<device_model>& device_models, std::string& error_message, viewer_model& viewer_model, const std::string& file) { bool was_loaded = false; bool failed = false; try { auto dev = ctx.load_device(file); was_loaded = true; device_models.emplace_back(dev, error_message, viewer_model); //Will cause the new device to appear in the left panel if (auto p = dev.as<playback>()) { auto filename = p.file_name(); p.set_status_changed_callback([&viewer_model, &device_models, filename](rs2_playback_status status) { if (status == RS2_PLAYBACK_STATUS_STOPPED) { auto it = std::find_if(device_models.begin(), device_models.end(), [&](const device_model& dm) { if (auto p = dm.dev.as<playback>()) return p.file_name() == filename; return false; }); if (it != device_models.end()) { auto subs = it->subdevices; if (it->_playback_repeat) { //Calling from different since playback callback is from reading thread std::thread{ [subs, &viewer_model]() { for (auto&& sub : subs) { if (sub->streaming) { auto profiles = sub->get_selected_profiles(); sub->play(profiles, viewer_model); } } } }.detach(); } else { for (auto&& sub : subs) { if (sub->streaming) { sub->stop(viewer_model); } } } } } }); } } catch (const error& e) { error_message = to_string() << "Failed to load file " << file << ". Reason: " << error_to_string(e); failed = true; } catch (const std::exception& e) { error_message = to_string() << "Failed to load file " << file << ". Reason: " << e.what(); failed = true; } if (failed && was_loaded) { try { ctx.unload_device(file); } catch (...) {} } }