1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Free rtc publish. Refactor TAB to spaces

This commit is contained in:
winlin 2020-04-25 22:30:55 +08:00
parent cd3c15ec4e
commit ebdc03416a
6 changed files with 46 additions and 155 deletions

View file

@ -149,111 +149,4 @@
</body> </body>
</html> </html>
<script>
/*
var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
var url = document.location.protocol + "//" + document.domain + ":1985/rtc/v1/publish/";
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();
var sendViewerOfferFn = function(desc) {
console.log('sendViewerOfferFn:', desc);
pc.setLocalDescription(desc);
var sdp_json = {"sdp":desc.sdp, "app":"live", "stream":"livestream", "streamurl":"webrtc://localhost/live/livestream"};
request.send(JSON.stringify(sdp_json));
};
pc.addTransceiver("audio", {
direction: "sendonly"
}
);
pc.addTransceiver("video", {
direction: "sendonly"
}
);
var constraints = {
"audio": true,
"video": {
"width": {
"min": "480",
"max": "1920"
},
"height": {
"min": "320",
"max": "1080"
},
"frameRate": {
"min": "15",
"max": "60"
}
}
};
navigator.mediaDevices.getUserMedia(constraints).then(
function(mediaStream) {
var video = document.getElementById('rtc_media_player');
video.srcObject = mediaStream;
video.onloadedmetadata = function(e) {
console.log('play');
video.play();
};
pc.addStream(mediaStream);
pc.createOffer(sendViewerOfferFn,
function(error) {
console.log('sendViewerOfferFn error:' + error);
},
);
}).catch(
function(err) {
console.log(err);
console.log(err.name + ": " + err.message);
}
);
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');
};
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>
</html> </html>

View file

@ -373,7 +373,7 @@ srs_error_t SrsRtc::on_publish()
return err; return err;
} }
if (!_srs_config->get_rtc_enabled(req->vhost)) { if (!_srs_config->get_rtc_enabled(req->vhost)) {
return err; return err;
} }

View file

@ -1559,7 +1559,7 @@ srs_error_t SrsRtcPublisher::on_rtp(SrsUdpMuxSocket* skt, char* buf, int nb_buf)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
SrsRtpSharedPacket* rtp_shared_pkt = new SrsRtpSharedPacket(); SrsRtpSharedPacket* rtp_shared_pkt = new SrsRtpSharedPacket();
SrsAutoFree(SrsRtpSharedPacket, rtp_shared_pkt); SrsAutoFree(SrsRtpSharedPacket, rtp_shared_pkt);
if ((err = rtp_shared_pkt->decode(buf, nb_buf)) != srs_success) { if ((err = rtp_shared_pkt->decode(buf, nb_buf)) != srs_success) {
return srs_error_wrap(err, "rtp packet decode failed"); return srs_error_wrap(err, "rtp packet decode failed");
@ -1631,7 +1631,7 @@ srs_error_t SrsRtcPublisher::on_rtcp_sender_report(char* buf, int nb_buf, SrsUdp
// @see: https://tools.ietf.org/html/rfc3550#section-6.4.1 // @see: https://tools.ietf.org/html/rfc3550#section-6.4.1
/* /*
0 1 2 3 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P| RC | PT=SR=200 | length | header |V=2|P| RC | PT=SR=200 | length |
@ -1714,7 +1714,7 @@ srs_error_t SrsRtcPublisher::on_rtcp_xr(char* buf, int nb_buf, SrsUdpMuxSocket*
/* /*
@see: http://www.rfc-editor.org/rfc/rfc3611.html#section-2 @see: http://www.rfc-editor.org/rfc/rfc3611.html#section-2
0 1 2 3 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|V=2|P|reserved | PT=XR=207 | length | |V=2|P|reserved | PT=XR=207 | length |
@ -1838,7 +1838,7 @@ srs_error_t SrsRtcPublisher::send_rtcp_xr_rrtr(SrsUdpMuxSocket* skt, uint32_t ss
/* /*
@see: http://www.rfc-editor.org/rfc/rfc3611.html#section-2 @see: http://www.rfc-editor.org/rfc/rfc3611.html#section-2
0 1 2 3 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|V=2|P|reserved | PT=XR=207 | length | |V=2|P|reserved | PT=XR=207 | length |
@ -1850,15 +1850,15 @@ srs_error_t SrsRtcPublisher::send_rtcp_xr_rrtr(SrsUdpMuxSocket* skt, uint32_t ss
@see: http://www.rfc-editor.org/rfc/rfc3611.html#section-4.4 @see: http://www.rfc-editor.org/rfc/rfc3611.html#section-4.4
0 1 2 3 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| BT=4 | reserved | block length = 2 | | BT=4 | reserved | block length = 2 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| NTP timestamp, most significant word | | NTP timestamp, most significant word |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| NTP timestamp, least significant word | | NTP timestamp, least significant word |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/ */
srs_utime_t now = srs_update_system_time(); srs_utime_t now = srs_update_system_time();
SrsNtp cur_ntp = SrsNtp::from_time_ms(now / 1000); SrsNtp cur_ntp = SrsNtp::from_time_ms(now / 1000);
@ -2150,7 +2150,7 @@ SrsRtcSession::SrsRtcSession(SrsRtcServer* rtc_svr, const SrsRequest& req, const
dtls_session = new SrsDtlsSession(this); dtls_session = new SrsDtlsSession(this);
// TODO: FIXME: Check error. // TODO: FIXME: Check error.
dtls_session->initialize(req); dtls_session->initialize(req);
strd = NULL; sender = NULL;
username = un; username = un;
@ -2165,17 +2165,14 @@ SrsRtcSession::SrsRtcSession(SrsRtcServer* rtc_svr, const SrsRequest& req, const
// TODO: FIXME: Support reload. // TODO: FIXME: Support reload.
sessionStunTimeout = _srs_config->get_rtc_stun_timeout(req.vhost); sessionStunTimeout = _srs_config->get_rtc_stun_timeout(req.vhost);
rtc_publisher = NULL; publisher = NULL;
} }
SrsRtcSession::~SrsRtcSession() SrsRtcSession::~SrsRtcSession()
{ {
srs_freep(sender);
srs_freep(publisher);
srs_freep(dtls_session); srs_freep(dtls_session);
if (strd) {
strd->stop();
}
srs_freep(strd);
} }
void SrsRtcSession::set_local_sdp(const SrsSdp& sdp) void SrsRtcSession::set_local_sdp(const SrsSdp& sdp)
@ -2199,19 +2196,19 @@ srs_error_t SrsRtcSession::on_stun(SrsUdpMuxSocket* skt, SrsStunPacket* stun_req
last_stun_time = srs_get_system_time(); last_stun_time = srs_get_system_time();
if (strd && strd->sendonly_ukt) { if (sender && sender->sendonly_ukt) {
// We are running in the ice-lite(server) mode. If client have multi network interface, // We are running in the ice-lite(server) mode. If client have multi network interface,
// we only choose one candidate pair which is determined by client. // we only choose one candidate pair which is determined by client.
if (stun_req->get_use_candidate() && strd->sendonly_ukt->get_peer_id() != skt->get_peer_id()) { if (stun_req->get_use_candidate() && sender->sendonly_ukt->get_peer_id() != skt->get_peer_id()) {
strd->update_sendonly_socket(skt); sender->update_sendonly_socket(skt);
} }
} }
if (rtc_publisher && rtc_publisher->sendonly_ukt) { if (publisher && publisher->sendonly_ukt) {
// We are running in the ice-lite(server) mode. If client have multi network interface, // We are running in the ice-lite(server) mode. If client have multi network interface,
// we only choose one candidate pair which is determined by client. // we only choose one candidate pair which is determined by client.
if (stun_req->get_use_candidate() && rtc_publisher->sendonly_ukt->get_peer_id() != skt->get_peer_id()) { if (stun_req->get_use_candidate() && publisher->sendonly_ukt->get_peer_id() != skt->get_peer_id()) {
rtc_publisher->update_sendonly_socket(skt); publisher->update_sendonly_socket(skt);
} }
} }
} }
@ -2428,20 +2425,20 @@ srs_error_t SrsRtcSession::on_rtcp_ps_feedback(char* buf, int nb_buf, SrsUdpMuxS
srs_error_t SrsRtcSession::on_rtcp_xr(char* buf, int nb_buf, SrsUdpMuxSocket* skt) srs_error_t SrsRtcSession::on_rtcp_xr(char* buf, int nb_buf, SrsUdpMuxSocket* skt)
{ {
if (rtc_publisher == NULL) { if (publisher == NULL) {
return srs_error_new(ERROR_RTC_RTCP, "rtc publisher null"); return srs_error_new(ERROR_RTC_RTCP, "rtc publisher null");
} }
return rtc_publisher->on_rtcp_xr(buf, nb_buf, skt); return publisher->on_rtcp_xr(buf, nb_buf, skt);
} }
srs_error_t SrsRtcSession::on_rtcp_sender_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt) srs_error_t SrsRtcSession::on_rtcp_sender_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt)
{ {
if (rtc_publisher == NULL) { if (publisher == NULL) {
return srs_error_new(ERROR_RTC_RTCP, "rtc publisher null"); return srs_error_new(ERROR_RTC_RTCP, "rtc publisher null");
} }
return rtc_publisher->on_rtcp_sender_report(buf, nb_buf, skt); return publisher->on_rtcp_sender_report(buf, nb_buf, skt);
} }
srs_error_t SrsRtcSession::on_rtcp_receiver_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt) srs_error_t SrsRtcSession::on_rtcp_receiver_report(char* buf, int nb_buf, SrsUdpMuxSocket* skt)
@ -2540,8 +2537,8 @@ srs_error_t SrsRtcSession::start_play(SrsUdpMuxSocket* skt)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
srs_freep(strd); srs_freep(sender);
strd = new SrsRtcSenderThread(this, skt, _srs_context->get_id()); sender = new SrsRtcSenderThread(this, skt, _srs_context->get_id());
uint32_t video_ssrc = 0; uint32_t video_ssrc = 0;
uint32_t audio_ssrc = 0; uint32_t audio_ssrc = 0;
@ -2558,11 +2555,11 @@ srs_error_t SrsRtcSession::start_play(SrsUdpMuxSocket* skt)
} }
} }
if ((err =strd->initialize(video_ssrc, audio_ssrc, video_payload_type, audio_payload_type)) != srs_success) { if ((err = sender->initialize(video_ssrc, audio_ssrc, video_payload_type, audio_payload_type)) != srs_success) {
return srs_error_wrap(err, "SrsRtcSenderThread init"); return srs_error_wrap(err, "SrsRtcSenderThread init");
} }
if ((err = strd->start()) != srs_success) { if ((err = sender->start()) != srs_success) {
return srs_error_wrap(err, "start SrsRtcSenderThread"); return srs_error_wrap(err, "start SrsRtcSenderThread");
} }
@ -2573,7 +2570,8 @@ srs_error_t SrsRtcSession::start_publish(SrsUdpMuxSocket* skt)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
rtc_publisher = new SrsRtcPublisher(this); srs_freep(publisher);
publisher = new SrsRtcPublisher(this);
uint32_t video_ssrc = 0; uint32_t video_ssrc = 0;
uint32_t audio_ssrc = 0; uint32_t audio_ssrc = 0;
@ -2591,7 +2589,7 @@ srs_error_t SrsRtcSession::start_publish(SrsUdpMuxSocket* skt)
} }
// FIXME: err process. // FIXME: err process.
if ((err = rtc_publisher->initialize(skt, video_ssrc, audio_ssrc, request)) != srs_success) { if ((err = publisher->initialize(skt, video_ssrc, audio_ssrc, request)) != srs_success) {
return srs_error_wrap(err, "rtc publisher init"); return srs_error_wrap(err, "rtc publisher init");
} }
@ -2662,7 +2660,7 @@ srs_error_t SrsRtcSession::on_rtcp(SrsUdpMuxSocket* skt)
err = on_rtcp_ps_feedback(ph, length, skt); err = on_rtcp_ps_feedback(ph, length, skt);
break; break;
} }
case kXR: { case kXR: {
err = on_rtcp_xr(ph, length, skt); err = on_rtcp_xr(ph, length, skt);
break; break;
} }
@ -2687,7 +2685,7 @@ srs_error_t SrsRtcSession::on_rtp(SrsUdpMuxSocket* skt)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (rtc_publisher == NULL) { if (publisher == NULL) {
return srs_error_new(ERROR_RTC_RTCP, "rtc publisher null"); return srs_error_new(ERROR_RTC_RTCP, "rtc publisher null");
} }
@ -2701,7 +2699,7 @@ srs_error_t SrsRtcSession::on_rtp(SrsUdpMuxSocket* skt)
return srs_error_wrap(err, "rtp unprotect failed"); return srs_error_wrap(err, "rtp unprotect failed");
} }
return rtc_publisher->on_rtp(skt, unprotected_buf, nb_unprotected_buf); return publisher->on_rtp(skt, unprotected_buf, nb_unprotected_buf);
} }
SrsUdpMuxSender::SrsUdpMuxSender(SrsRtcServer* s) SrsUdpMuxSender::SrsUdpMuxSender(SrsRtcServer* s)

View file

@ -312,7 +312,7 @@ private:
SrsSdp local_sdp; SrsSdp local_sdp;
SrsRtcSessionStateType session_state; SrsRtcSessionStateType session_state;
SrsDtlsSession* dtls_session; SrsDtlsSession* dtls_session;
SrsRtcSenderThread* strd; SrsRtcSenderThread* sender;
std::string username; std::string username;
std::string peer_id; std::string peer_id;
srs_utime_t last_stun_time; srs_utime_t last_stun_time;
@ -330,7 +330,7 @@ public:
SrsRequest request; SrsRequest request;
SrsSource* source; SrsSource* source;
private: private:
SrsRtcPublisher* rtc_publisher; SrsRtcPublisher* publisher;
public: public:
SrsRtcSession(SrsRtcServer* rtc_svr, const SrsRequest& req, const std::string& un, int context_id); SrsRtcSession(SrsRtcServer* rtc_svr, const SrsRequest& req, const std::string& un, int context_id);
virtual ~SrsRtcSession(); virtual ~SrsRtcSession();

View file

@ -143,7 +143,7 @@ srs_error_t SrsSessionInfo::encode(std::ostringstream& os)
if (! ice_options_.empty()) { if (! ice_options_.empty()) {
os << "a=ice-options:" << ice_options_ << kCRLF; os << "a=ice-options:" << ice_options_ << kCRLF;
} else { } else {
// @see: https://webrtcglossary.com/trickle-ice/ // @see: https://webrtcglossary.com/trickle-ice/
// Trickle ICE is an optimization of the ICE specification for NAT traversal. // Trickle ICE is an optimization of the ICE specification for NAT traversal.
os << "a=ice-options:trickle" << kCRLF; os << "a=ice-options:trickle" << kCRLF;
} }
@ -362,7 +362,7 @@ srs_error_t SrsMediaDesc::encode(std::ostringstream& os)
int foundation = 0; int foundation = 0;
int component_id = 1; /* RTP */ int component_id = 1; /* RTP */
for (std::vector<SrsCandidate>::iterator iter = candidates_.begin(); iter != candidates_.end(); ++iter) { for (std::vector<SrsCandidate>::iterator iter = candidates_.begin(); iter != candidates_.end(); ++iter) {
// @see: https://tools.ietf.org/html/draft-ietf-ice-rfc5245bis-00#section-4.2 // @see: https://tools.ietf.org/html/draft-ietf-ice-rfc5245bis-00#section-4.2
uint32_t priority = (1<<24)*(126) + (1<<8)*(65535) + (1)*(256 - component_id); uint32_t priority = (1<<24)*(126) + (1<<8)*(65535) + (1)*(256 - component_id);
@ -424,7 +424,7 @@ srs_error_t SrsMediaDesc::parse_attribute(const std::string& content)
sendrecv_ = true; sendrecv_ = true;
} else if (attribute == "inactive") { } else if (attribute == "inactive") {
inactive_ = true; inactive_ = true;
} else { } else {
return session_info_.parse_attribute(attribute, value); return session_info_.parse_attribute(attribute, value);
} }
@ -756,7 +756,7 @@ void SrsSdp::add_candidate(const std::string& ip, const int& port, const std::st
candidate.type_ = type; candidate.type_ = type;
for (std::vector<SrsMediaDesc>::iterator iter = media_descs_.begin(); iter != media_descs_.end(); ++iter) { for (std::vector<SrsMediaDesc>::iterator iter = media_descs_.begin(); iter != media_descs_.end(); ++iter) {
iter->candidates_.push_back(candidate); iter->candidates_.push_back(candidate);
} }
} }

View file

@ -63,13 +63,13 @@ SrsRtpHeader::~SrsRtpHeader()
srs_error_t SrsRtpHeader::decode(SrsBuffer* stream) srs_error_t SrsRtpHeader::decode(SrsBuffer* stream)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (stream->size() < kRtpHeaderFixedSize) { if (stream->size() < kRtpHeaderFixedSize) {
return srs_error_new(ERROR_RTC_RTP_MUXER, "rtp payload incorrect"); return srs_error_new(ERROR_RTC_RTP_MUXER, "rtp payload incorrect");
} }
/* /*
0 1 2 3 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+