Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ICE to server #44

Merged
merged 4 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions webrtc_ros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,44 @@ 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
cv_bridge
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 ##
###########
Expand Down
34 changes: 34 additions & 0 deletions webrtc_ros/launch/webrtc_ros.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<launch>
<!--ICE servers are used to resolve remote connections over the internet. If you-->
<!--are in a single network where the devices can find each other by IP address,-->
<!--this is not needed:-->
<node name="ice_server_provider" pkg="webrtc_ros" type="ice_server_service.py">
<!--These are the servers used for resolving remote addresses, google provides-->
<!--some free to use ones, here as defaults. You could setup your own or use-->
<!--another provider-->
<rosparam param="stun_servers">
['stun:stun1.l.google.com:19302', 'stun:stun2.l.google.com:19302']
</rosparam>
<!--Turn servers are used to route traffic from behind challenging networks.-->
<!--Not always needed, but in some corporate environments it might be. -->
<!--This is the url of the turn server (Ex: coturn)-->
<rosparam param="turn_server_uris">
[]
</rosparam>
<!--You need credentials to access your turn server. The best way to do-->
<!--that (the way we support) is to use a seperate rest api with a shared-->
<!--secret with coturn. This is the uri, username, and password that will -->
<!--be passed to that endpoint. They will be passed to the enpoint as a -->
<!--post request with fields username and password in the request body-->
<!--It expects the server to respond with the username and password for-->
<!--the turn server in fields username and password held in the response-->
<!--data-->
<param name="turn_server_creds_uri" value=""/>
<param name="turn_server_creds_username" value=""/>
<param name="turn_server_creds_password" value=""/>
</node>
<node name="webrtc_server" pkg="webrtc_ros" type="webrtc_ros_server_node">
<param name="port" value="9090"/>
</node>
</launch>
3 changes: 3 additions & 0 deletions webrtc_ros/msg/IceServer.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
string uri
string username
string password
8 changes: 8 additions & 0 deletions webrtc_ros/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<depend>nodelet</depend>
<depend>async_web_server_cpp</depend>

<build_export_depend>message_runtime</build_export_depend>

<build_depend>message_generation</build_depend>

<build_depend>std_msgs</build_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>std_msgs</exec_depend>

<export>
<nodelet plugin="${prefix}/nodelet_plugins.xml" />
</export>
Expand Down
59 changes: 59 additions & 0 deletions webrtc_ros/src/ice_server_service.py
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 18 additions & 2 deletions webrtc_ros/src/webrtc_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <webrtc/media/base/videosourceinterface.h>
#include <webrtc/base/bind.h>
#include <webrtc_ros/ros_video_capturer.h>
#include <webrtc_ros/GetIceServers.h>

namespace webrtc_ros
{
Expand Down Expand Up @@ -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<serv.response.servers.size(); i++){
webrtc::PeerConnectionInterface::IceServer server;
server.uri = serv.response.servers[i].uri;
if(!serv.response.servers[i].username.empty() && !serv.response.servers[i].password.empty()){
server.username = serv.response.servers[i].username;
server.password = serv.response.servers[i].password;
}
config.servers.push_back(server);
}
}
}

WebrtcClientWeakPtr weak_this(keep_alive_this_);
webrtc_observer_proxy_ = new rtc::RefCountedObject<WebrtcClientObserverProxy>(weak_this);
peer_connection_ = peer_connection_factory_->CreatePeerConnection(
webrtc::PeerConnectionInterface::RTCConfiguration(),
config,
nullptr,
nullptr,
webrtc_observer_proxy_.get()
Expand Down
4 changes: 4 additions & 0 deletions webrtc_ros/srv/GetIceServers.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## args
---
## Resp
IceServer[] servers