From 9ed28472361efa105a933453d6c66349e376c07d Mon Sep 17 00:00:00 2001 From: Michael Sobrepera Date: Wed, 29 Apr 2020 08:35:49 -0400 Subject: [PATCH] add ICE to server (#44) * add stun to server * Squashed commit of the following: commit 5b3a562af3ea15685d439d87fefa1a8ac3008dee Author: Michael Sobrepera Date: Mon Apr 27 13:04:01 2020 -0400 more stupid typos commit aa364a248be507ba128cbcf3150579dc8265e01f Author: Michael Sobrepera Date: Mon Apr 27 13:02:19 2020 -0400 fixed the same typo in the rest of the file commit 42570192add2e6e4ee08de86d827378275c3139c Author: Michael Sobrepera Date: Mon Apr 27 13:01:03 2020 -0400 fixed typo in python import of service commit 165c0d3b53cdc12dbbd58f0aadef04856606decd Author: Michael Sobrepera Date: Mon Apr 27 12:59:02 2020 -0400 fixed launch file wrong name in param commit 9d7189b6697d9be727432a54f8eb924ddc6814bf Author: Michael Sobrepera Date: Mon Apr 27 12:58:19 2020 -0400 made python node runable commit 39a51e704aa34f380a4da49b9dc81c5bdb69f4b8 Author: Michael Sobrepera Date: Mon Apr 27 11:38:35 2020 -0400 changed to using a service and made a python demo source * fixed missing package dependencies * fixed problems in cmakelists --- webrtc_ros/CMakeLists.txt | 24 +++++++++++ webrtc_ros/launch/webrtc_ros.launch | 34 ++++++++++++++++ webrtc_ros/msg/IceServer.msg | 3 ++ webrtc_ros/package.xml | 8 ++++ webrtc_ros/src/ice_server_service.py | 59 ++++++++++++++++++++++++++++ webrtc_ros/src/webrtc_client.cpp | 20 +++++++++- webrtc_ros/srv/GetIceServers.srv | 4 ++ 7 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 webrtc_ros/launch/webrtc_ros.launch create mode 100644 webrtc_ros/msg/IceServer.msg create mode 100755 webrtc_ros/src/ice_server_service.py create mode 100644 webrtc_ros/srv/GetIceServers.srv diff --git a/webrtc_ros/CMakeLists.txt b/webrtc_ros/CMakeLists.txt index a8cb826..794f81c 100644 --- a/webrtc_ros/CMakeLists.txt +++ b/webrtc_ros/CMakeLists.txt @@ -7,10 +7,27 @@ find_package(catkin REQUIRED COMPONENTS image_transport nodelet roscpp + std_msgs + message_generation ) find_package(webrtc REQUIRED) find_package(X11 REQUIRED) +add_message_files( + FILES + IceServer.msg +) + +add_service_files( + FILES + GetIceServers.srv +) + +generate_messages( + DEPENDENCIES + std_msgs +) + catkin_package( CATKIN_DEPENDS async_web_server_cpp @@ -18,9 +35,16 @@ catkin_package( image_transport nodelet roscpp + message_runtime + std_msgs DEPENDS webrtc ) +catkin_install_python( + PROGRAMS src/ice_server_service.py + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +) + ########### ## Build ## ########### diff --git a/webrtc_ros/launch/webrtc_ros.launch b/webrtc_ros/launch/webrtc_ros.launch new file mode 100644 index 0000000..c2083af --- /dev/null +++ b/webrtc_ros/launch/webrtc_ros.launch @@ -0,0 +1,34 @@ + + + + + + + + + + + ['stun:stun1.l.google.com:19302', 'stun:stun2.l.google.com:19302'] + + + + + + [] + + + + + + + + + + + + + + + + + diff --git a/webrtc_ros/msg/IceServer.msg b/webrtc_ros/msg/IceServer.msg new file mode 100644 index 0000000..0e6aa4d --- /dev/null +++ b/webrtc_ros/msg/IceServer.msg @@ -0,0 +1,3 @@ +string uri +string username +string password diff --git a/webrtc_ros/package.xml b/webrtc_ros/package.xml index 7b06ca5..cbab112 100644 --- a/webrtc_ros/package.xml +++ b/webrtc_ros/package.xml @@ -22,6 +22,14 @@ nodelet async_web_server_cpp + message_runtime + + message_generation + + std_msgs + std_msgs + std_msgs + diff --git a/webrtc_ros/src/ice_server_service.py b/webrtc_ros/src/ice_server_service.py new file mode 100755 index 0000000..1c64b68 --- /dev/null +++ b/webrtc_ros/src/ice_server_service.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +import json +import rospy +import requests +from webrtc_ros.msg import IceServer +from webrtc_ros.srv import GetIceServers, GetIceServersResponse + + +class IceServerManager(object): + """ Manages providing ice server information to the webrtc system """ + + def __init__(self): + rospy.init_node('ice_server_provider') + + self.stun_servers = rospy.get_param('stun_servers', [ + 'stun:stun1.l.google.com:19302', 'stun:stun2.l.google.com:19302']) + self.turn_server_uris = rospy.get_param('turn_server_uris', '') + self.turn_creds_uri = rospy.get_param('turn_server_creds_uri', '') + self.turn_creds_username = rospy.get_param( + 'turn_server_creds_username', '') + self.turn_creds_password = rospy.get_param( + 'turn_server_creds_password', '') + + self.get_ice_servers_service = rospy.Service( + 'get_ice_servers', GetIceServers, self.get_ice_servers) + + rospy.loginfo('Ice Server Provider Up') + rospy.spin() + + def get_turn_creds(self): + """Get the credentials from the turn server.""" + if self.turn_creds_uri: + resp = requests.post(self.turn_creds_uri, + {'username': self.turn_creds_username, + 'password': self.turn_creds_password}) + if(('username' in resp.data) and ('password' in resp.data)): + return resp.data + return False + + def get_ice_servers(self, _): + """Callback for service. Returns the ice servers""" + resp = GetIceServersResponse() + turn_creds = self.get_turn_creds() + if turn_creds: + for uri in self.turn_server_uris: + serv = IceServer() + serv.username = turn_creds['username'] + serv.password = turn_creds['password'] + resp.servers.append(serv) + for suri in self.stun_servers: + serv = IceServer() + serv.uri = suri + resp.servers.append(serv) + return resp + + +if __name__ == '__main__': + IceServerManager() diff --git a/webrtc_ros/src/webrtc_client.cpp b/webrtc_ros/src/webrtc_client.cpp index bd68101..5622776 100644 --- a/webrtc_ros/src/webrtc_client.cpp +++ b/webrtc_ros/src/webrtc_client.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace webrtc_ros { @@ -112,11 +113,26 @@ bool WebrtcClient::initPeerConnection() } if (!peer_connection_) { - webrtc::PeerConnectionInterface::IceServers servers; + webrtc::PeerConnectionInterface::RTCConfiguration config; + if(ros::service::exists("get_ice_servers", false)){ + GetIceServers serv; + if(ros::service::call("get_ice_servers", serv)){ + for(int i=0; i(weak_this); peer_connection_ = peer_connection_factory_->CreatePeerConnection( - webrtc::PeerConnectionInterface::RTCConfiguration(), + config, nullptr, nullptr, webrtc_observer_proxy_.get() diff --git a/webrtc_ros/srv/GetIceServers.srv b/webrtc_ros/srv/GetIceServers.srv new file mode 100644 index 0000000..01549bb --- /dev/null +++ b/webrtc_ros/srv/GetIceServers.srv @@ -0,0 +1,4 @@ +## args +--- +## Resp +IceServer[] servers