@@ -275,6 +275,8 @@ void OBCameraNodeDriver::init() {
275275 usb_port_ = declare_parameter<std::string>(" usb_port" , " " );
276276 net_device_ip_ = declare_parameter<std::string>(" net_device_ip" , " " );
277277 net_device_port_ = static_cast <int >(declare_parameter<int >(" net_device_port" , 0 ));
278+ bag_filename_ = declare_parameter<std::string>(" bag_filename" , " " );
279+ bag_replay_loop_ = static_cast <int >(declare_parameter<bool >(" bag_replay_loop" , true ));
278280 enumerate_net_device_ = declare_parameter<bool >(" enumerate_net_device" , false );
279281 uvc_backend_ = declare_parameter<std::string>(" uvc_backend" , " libuvc" );
280282 device_access_mode_str_ = declare_parameter<std::string>(" device_access_mode" , " Default" );
@@ -456,6 +458,8 @@ void OBCameraNodeDriver::queryDevice() {
456458
457459 if (!enumerate_net_device_ && !net_device_ip_.empty () && net_device_port_ != 0 ) {
458460 connectNetDevice (net_device_ip_, net_device_port_);
461+ } else if (!bag_filename_.empty ()) {
462+ openPlaybackDevice (bag_filename_, bag_replay_loop_);
459463 } else {
460464 auto device_list = ctx_->queryDeviceList ();
461465 if (device_list->getCount () != 0 ) {
@@ -1227,6 +1231,51 @@ void OBCameraNodeDriver::connectNetDevice(const std::string &net_device_ip, int
12271231 }
12281232}
12291233
1234+ void OBCameraNodeDriver::openPlaybackDevice (const std::string &bagfile_path, bool bag_replay_loop) {
1235+ if (bagfile_path.empty () || !std::filesystem::exists (bagfile_path)) {
1236+ RCLCPP_ERROR_STREAM (logger_, " Invalid bag file path " << bagfile_path);
1237+ return ;
1238+ }
1239+
1240+ auto playback_device = std::make_shared<ob::PlaybackDevice>(bagfile_path.c_str ());
1241+ if (playback_device == nullptr ) {
1242+ RCLCPP_ERROR_STREAM (logger_, " Failed to open playback device " << bagfile_path);
1243+ return ;
1244+ }
1245+ try {
1246+ RCLCPP_INFO_STREAM (logger_, " Opening bag file " << bagfile_path);
1247+
1248+ // Automatically restart playback when reaching file end
1249+ playback_device->setPlaybackStatusChangeCallback ([&](OBPlaybackStatus status)
1250+ {
1251+ RCLCPP_INFO (logger_, " Playback status changed to %d" , status);
1252+
1253+ if (status == OB_PLAYBACK_STOPPED ) {
1254+ if (bag_replay_loop && is_alive_ && rclcpp::ok ()) {
1255+ // restart bag file
1256+ ob_camera_node_->clean ();
1257+ std::this_thread::sleep_for (std::chrono::milliseconds (1000 ));
1258+ initializeDevice (playback_device);
1259+ } else {
1260+ // stop it here
1261+ rclcpp::shutdown ();
1262+ }
1263+ }
1264+ });
1265+
1266+ initializeDevice (playback_device);
1267+ if (!device_connected_) {
1268+ RCLCPP_ERROR_STREAM (logger_, " Failed to initialize playback device " << bagfile_path);
1269+ }
1270+ } catch (const std::exception &e) {
1271+ RCLCPP_ERROR_STREAM (logger_, " Exception during playback device initialization: " << e.what ());
1272+ device_connected_ = false ;
1273+ } catch (...) {
1274+ RCLCPP_ERROR_STREAM (logger_, " Unknown exception during playback device initialization" );
1275+ device_connected_ = false ;
1276+ }
1277+ }
1278+
12301279void OBCameraNodeDriver::startDevice (const std::shared_ptr<ob::DeviceList> &list) {
12311280 if (device_connected_.load ()) {
12321281 return ;
0 commit comments