ipcamera_driver.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Alireza Hosseini.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Alireza Hosseini */
31 
34 #include <opencv2/video/video.hpp>
35 
36 IpCameraDriver::IpCameraDriver() : pnh_("~"), image_transport_(pnh_), camera_info_manager_(pnh_)
37 {
38  camera_pub_ = image_transport_.advertiseCamera("/camera/image", 10);
39 
40  pnh_.param<std::string>("video_url", video_url_, "rtsp://admin:A123456789@192.168.1.64/live.sdp?:network-cache=300");
41  pnh_.getParam("camera_info_url", camera_info_url_);
42  pnh_.param<std::string>("frame_id", frame_id_, "cam_link");
43 
45 
47 
49  {
51  {
52  ROS_INFO_STREAM("Loaded camera calibration from " << camera_info_url_);
53  }
54  else
55  {
56  ROS_WARN_STREAM("Could not load camera info, using an uncalibrated config.");
57  }
58  }
59  else
60  {
61  ROS_WARN_STREAM("Given camera info url: " << camera_info_url_ << " is not valid, using an uncalibrated config.");
62  }
63 
64  ROS_INFO_STREAM("Trying to connect to " << video_url_);
65  cap_.open(video_url_);
66 }
67 
69 {
70  cv::Mat frame;
71  ros::Rate loop(33);
72  while (ros::ok())
73  {
74  if (cap_.isOpened())
75  {
76  ROS_INFO_ONCE("connection established");
78  {
79  cap_ >> frame;
80  if (frame.empty()) {
81  ros::spinOnce();
82  loop.sleep();
83  continue;
84  }
85  cv_bridge::CvImage out_msg;
86  out_msg.header.frame_id = frame_id_;
87  out_msg.header.stamp = ros::Time::now();
89  out_msg.image = frame;
90 
91  sensor_msgs::CameraInfo camera_info;
92  camera_info = camera_info_manager_.getCameraInfo();
93  camera_info.header.frame_id = frame_id_;
94  camera_info.header.stamp = ros::Time::now();
95 
96  sensor_msgs::Image rosimg;
97  out_msg.toImageMsg(rosimg);
98  camera_pub_.publish(rosimg, camera_info, ros::Time::now());
99  }
100  }
101  else
102  {
103  cap_.release();
104  ROS_WARN_STREAM_DELAYED_THROTTLE(10, "Video stream is not available, retrying...");
105  cap_.open(video_url_);
106  }
107 
108  ros::spinOnce();
109  loop.sleep();
110  }
111  return true;
112 }
113 
114 bool IpCameraDriver::refreshSrvCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
115 {
116  ROS_INFO("refreshing");
117  cap_.release();
118  if (!cap_.open(video_url_))
119  {
120  ROS_ERROR_STREAM("Connecting to " << video_url_ << " failed.");
121  }
122  return true;
123 }
124 
125 int main(int argc, char **argv)
126 {
127  ros::init(argc, argv, "mrl_ip_camera");
128  IpCameraDriver ipCamera;
129  ipCamera.publish();
130  return 0;
131 }
camera_info_manager::CameraInfoManager camera_info_manager_
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
bool refreshSrvCallback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
sensor_msgs::CameraInfo getCameraInfo(void)
cv::VideoCapture cap_
#define ROS_INFO_ONCE(...)
image_transport::CameraPublisher camera_pub_
bool loadCameraInfo(const std::string &url)
std::string camera_info_url_
uint32_t getNumSubscribers() const
ros::NodeHandle pnh_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string encoding
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
std::string frame_id_
bool validateURL(const std::string &url)
int main(int argc, char **argv)
std::string video_url_
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
image_transport::ImageTransport image_transport_
#define ROS_INFO(...)
ros::ServiceServer refresh_service_server_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
#define ROS_WARN_STREAM_DELAYED_THROTTLE(rate, args)
#define ROS_WARN_STREAM(args)
bool sleep()
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
static Time now()
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
sensor_msgs::ImagePtr toImageMsg() const
bool setCameraName(const std::string &cname)
std_msgs::Header header


ipcamera_driver
Author(s): Alireza Hosseini
autogenerated on Sun Dec 13 2020 03:16:15