1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 11:51:57 +00:00

add candidates ip list, add rtc_upload.html to test

This commit is contained in:
HuyaJohn 2020-03-06 23:28:15 -08:00
parent a36ed6954f
commit b730458d51
7 changed files with 176 additions and 9 deletions

View file

@ -23,6 +23,8 @@ http_server {
rtc {
enabled on;
listen 9527;
# candidate device ip: *(all interface), 192.168.1.1 ...
candidate *;
}
stats {
network 0;

View file

@ -0,0 +1,97 @@
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8">
</head>
<body>
<br>local_media_player: <br>
<video id = "local_media_player" autoplay></video>
<br>rtc_media_player: <br>
<video id = "rtc_media_player" autoplay></video>
<script>
var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
var url = "http://hw.com:1985/api/v1/sdp/";
var method = "POST";
var shouldBeAsync = true;
var request = new XMLHttpRequest();
request.open(method, url, shouldBeAsync);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var pc = new PeerConnection(null);
var sendSdpOffer = function(desc) {
console.log('sendSdpOffer:', desc);
pc.setLocalDescription(desc);
var sdp_json = {"sdp":desc.sdp, "app":"webrtc", "stream":"test"};
request.send(JSON.stringify(sdp_json));
};
pc.onaddstream = function(event) {
console.log('onaddstream');
document.getElementById('rtc_media_player').srcObject = event.stream;
rtc_media_player.load();
};
pc.onicecandidate = function(event) {
console.log('onicecandidate');
};
pc.onconnectionstatechange = function(event) {
console.log('onconnectionstatechange');
};
pc.onicegatheringstatechange = function(event) {
console.log('onicegatheringstatechange');
};
pc.onsignalingstatechange = function(event) {
console.log('onsignalingstatechange');
};
navigator.webkitGetUserMedia({
"audio": true,
"video": {
width: 800,
height:600,
frameRate:30,
bitRate:8000,
}
}, function(stream){
document.getElementById('local_media_player').srcObject = stream;
pc.addStream(stream);
pc.createOffer(sendSdpOffer, function (error) {
console.log('Failure callback: ' + error);
});
}, function(error){
//处理媒体流创建失败错误
console.log('getUserMedia error: ' + error);
});
request.onerror = function(event) {
console.log('http error');
};
request.onload = function () {
console.log('onload,' , request.responseText);
var json = JSON.parse(request.responseText);
console.log('onmessage viewerResponse:', json.sdp);
pc.setRemoteDescription(new SessionDescription({type:'answer', sdp:json.sdp}));
}
</script>
</body>
</html>

View file

@ -4255,6 +4255,23 @@ int SrsConfig::get_rtc_listen()
return ::atoi(conf->arg0().c_str());
}
std::string SrsConfig::get_rtc_candidates()
{
static string DEFAULT = "*";
SrsConfDirective* conf = root->get("rtc");
if (!conf) {
return DEFAULT;
}
conf = conf->get("candidate");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return (conf->arg0().c_str());
}
SrsConfDirective* SrsConfig::get_vhost(string vhost, bool try_default_vhost)
{
srs_assert(root);

View file

@ -496,6 +496,7 @@ public:
virtual int get_rtc_enabled();
virtual bool get_rtc_enabled(SrsConfDirective* conf);
virtual int get_rtc_listen();
virtual std::string get_rtc_candidates();
// vhost specified section
public:

View file

@ -850,19 +850,20 @@ srs_error_t SrsGoApiSdp::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage*
obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS));
obj->set("server", SrsJsonAny::integer(stat->server_id()));
string candidate_str = "candidate:1 1 udp 2115783679 192.168.170.129:9527 typ host generation 0 ufrag "
+ local_sdp.get_ice_ufrag() + "netwrok-cost 50";
// XXX: ice candidate
//string candidate_str = "candidate:1 1 udp 2115783679 192.168.170.129:9527 typ host generation 0 ufrag "
// + local_sdp.get_ice_ufrag() + "netwrok-cost 50";
SrsJsonObject* candidate_obj = SrsJsonAny::object();
//SrsJsonObject* candidate_obj = SrsJsonAny::object();
//SrsAutoFree(SrsJsonObject, candidate_obj);
candidate_obj->set("candidate", SrsJsonAny::str(candidate_str.c_str()));
candidate_obj->set("sdpMid", SrsJsonAny::str("0"));
candidate_obj->set("sdpMLineIndex", SrsJsonAny::str("0"));
//candidate_obj->set("candidate", SrsJsonAny::str(candidate_str.c_str()));
//candidate_obj->set("sdpMid", SrsJsonAny::str("0"));
//candidate_obj->set("sdpMLineIndex", SrsJsonAny::str("0"));
if (r->is_http_post()) {
obj->set("sdp", SrsJsonAny::str(local_sdp_str.c_str()));
obj->set("candidate", candidate_obj);
// obj->set("candidate", candidate_obj);
} else {
return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed);
}

View file

@ -39,6 +39,8 @@ using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_stun_stack.hpp>
#include <srs_app_dtls.hpp>
#include <srs_app_config.hpp>
#include <srs_service_utility.hpp>
static bool is_stun(const char* data, const int size)
{
@ -71,6 +73,33 @@ static string gen_random_str(int len)
const int SRTP_MASTER_KEY_KEY_LEN = 16;
const int SRTP_MASTER_KEY_SALT_LEN = 14;
SrsCandidate::SrsCandidate()
{
}
SrsCandidate::~SrsCandidate()
{
}
std::vector<std::string> SrsCandidate::get_candidate_ips()
{
std::vector<std::string> candidate_ips;
string candidate = _srs_config->get_rtc_candidates();
if (candidate == "*" || candidate == "0.0.0.0") {
std::vector<std::string> tmp = srs_get_local_ips();
for (int i = 0; i < tmp.size(); ++i) {
if (tmp[i] != "127.0.0.1") {
candidate_ips.push_back(tmp[i]);
}
}
} else {
candidate_ips.push_back(candidate);
}
return candidate_ips;
}
SrsSdpMediaInfo::SrsSdpMediaInfo()
{
}
@ -139,6 +168,15 @@ srs_error_t SrsSdp::encode(string& sdp_str)
{
srs_error_t err = srs_success;
string candidate_lines = "";
std::vector<string> candidate_ips = SrsCandidate::get_candidate_ips();
for (int i = 0; i < candidate_ips.size(); ++i) {
ostringstream os;
os << "a=candidate:10 1 udp 2115783679 " << candidate_ips[i] << " " << _srs_config->get_rtc_listen() <<" typ host generation 0\\r\\n";
candidate_lines += os.str();
}
// FIXME:
sdp_str =
"v=0\\r\\n"
@ -150,7 +188,7 @@ srs_error_t SrsSdp::encode(string& sdp_str)
"a=msid-semantic: WMS 6VrfBKXrwK\\r\\n"
"m=audio 9 UDP/TLS/RTP/SAVPF 111\\r\\n"
"c=IN IP4 0.0.0.0\\r\\n"
"a=candidate:10 1 udp 2115783679 192.168.170.129 9527 typ host generation 0\\r\\n"
+ candidate_lines +
"a=rtcp:9 IN IP4 0.0.0.0\\r\\n"
"a=ice-ufrag:" + ice_ufrag + "\\r\\n"
"a=ice-pwd:" + ice_pwd + "\\r\\n"
@ -170,7 +208,7 @@ srs_error_t SrsSdp::encode(string& sdp_str)
"a=ssrc:3233846890 label:6VrfBKXrwKa0\\r\\n"
"m=video 9 UDP/TLS/RTP/SAVPF 96 98 102\\r\\n"
"c=IN IP4 0.0.0.0\\r\\n"
"a=candidate:10 1 udp 2115783679 192.168.170.129 9527 typ host generation 0\\r\\n"
+ candidate_lines +
"a=rtcp:9 IN IP4 0.0.0.0\\r\\n"
"b=as:2000000\\r\\n"
"a=ice-ufrag:" + ice_ufrag + "\\r\\n"

View file

@ -37,6 +37,16 @@
class SrsServer;
class SrsStunPacket;
class SrsCandidate
{
private:
public:
SrsCandidate();
virtual ~SrsCandidate();
static std::vector<std::string> get_candidate_ips();
};
class SrsSdpMediaInfo
{
private:
@ -151,6 +161,7 @@ public:
virtual ~SrsRtcServer();
public:
virtual srs_error_t initialize();
virtual srs_error_t on_udp_packet(srs_netfd_t fd, const std::string& peer_ip, const int peer_port,
const sockaddr* from, const int fromlen, const char* data, const int size);