00001 /* 00002 License: BSD 00003 https://raw.github.com/robotics-in-concert/rocon_devices/license/LICENSE 00004 */ 00005 00006 #include <rocon_rtsp_camera_relay/rocon_rtsp_camera_relay.hpp> 00007 00008 namespace rocon { 00009 00010 RoconRtspCameraRelay::RoconRtspCameraRelay(ros::NodeHandle& n) : nh_(n) 00011 { 00012 image_transport::ImageTransport it(nh_); 00013 pub_video_ = it.advertise("image", 1); 00014 pub_camera_info_ = nh_.advertise<sensor_msgs::CameraInfo>("camera_info", 1); 00015 pub_status_ = nh_.advertise<std_msgs::String>("status", 1); 00016 } 00017 00018 RoconRtspCameraRelay::~RoconRtspCameraRelay() 00019 { 00020 vcap_.release(); 00021 } 00022 00023 bool RoconRtspCameraRelay::init(const std::string video_stream_url) { 00024 video_stream_address_ = video_stream_url; 00025 00026 if (!vcap_.open(video_stream_address_)) 00027 return false; 00028 else 00029 return true; 00030 } 00031 00032 bool RoconRtspCameraRelay::reset(const std::string video_stream_url) 00033 { 00034 vcap_.release(); 00035 return init(video_stream_url); 00036 } 00037 00038 /* 00039 Convert cv::Mat to sensor_msgs:Image and CameraInfo 00040 */ 00041 void RoconRtspCameraRelay::convertCvToRosImg(const cv::Mat& mat, sensor_msgs::Image& ros_img, sensor_msgs::CameraInfo& ci) 00042 { 00043 cv_bridge::CvImage cv_img; 00044 00045 cv_img.encoding = sensor_msgs::image_encodings::BGR8; 00046 cv_img.image = mat; 00047 cv_img.toImageMsg(ros_img); 00048 ros_img.header.stamp = ros::Time::now(); 00049 ci.header = ros_img.header; 00050 ci.width = ros_img.width; 00051 ci.height = ros_img.height; 00052 00053 return; 00054 } 00055 00056 00057 void RoconRtspCameraRelay::spin() 00058 { 00059 cv::Mat mat; 00060 sensor_msgs::CameraInfo ci; 00061 sensor_msgs::Image ros_img; 00062 std_msgs::String ros_str; 00063 00064 while(ros::ok()) 00065 { 00066 if(!vcap_.read(mat)) { 00067 status_ = "No frame from camera"; 00068 cv::waitKey(); 00069 } 00070 else { 00071 status_ = "live"; 00072 } 00073 00074 ros_str.data = status_; 00075 00076 convertCvToRosImg(mat, ros_img, ci); 00077 pub_video_.publish(ros_img); 00078 pub_camera_info_.publish(ci); 00079 pub_status_.publish(ros_str); 00080 cv::waitKey(1); 00081 } 00082 } 00083 }