diff --git a/trunk/src/app/srs_app_rtc_api.cpp b/trunk/src/app/srs_app_rtc_api.cpp index fbe278b88..2ff98ef1b 100644 --- a/trunk/src/app/srs_app_rtc_api.cpp +++ b/trunk/src/app/srs_app_rtc_api.cpp @@ -140,59 +140,61 @@ srs_error_t SrsGoApiRtcPlay::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMe srs_trace("RTC play %s, api=%s, clientip=%s, app=%s, stream=%s, offer=%dB, eip=%s, srtp=%s, dtls=%s", streamurl.c_str(), api.c_str(), clientip.c_str(), app.c_str(), stream_name.c_str(), remote_sdp_str.length(), eip.c_str(), - srtp.c_str(), dtls.c_str()); + srtp.c_str(), dtls.c_str() + ); + + // The RTC user config object. + SrsRtcUserConfig ruc; + ruc.eip_ = eip; + ruc.publish_ = false; + ruc.dtls_ = (dtls != "false"); + + if (srtp.empty()) { + ruc.srtp_ = _srs_config->get_rtc_server_encrypt(); + } else { + ruc.srtp_ = (srtp != "false"); + } // TODO: FIXME: It seems remote_sdp doesn't represents the full SDP information. - SrsSdp remote_sdp; - if ((err = remote_sdp.parse(remote_sdp_str)) != srs_success) { + if ((err = ruc.remote_sdp_.parse(remote_sdp_str)) != srs_success) { return srs_error_wrap(err, "parse sdp failed: %s", remote_sdp_str.c_str()); } - if ((err = check_remote_sdp(remote_sdp)) != srs_success) { + if ((err = check_remote_sdp(ruc.remote_sdp_)) != srs_success) { return srs_error_wrap(err, "remote sdp check failed"); } - SrsRequest request; - request.app = app; - request.stream = stream_name; + ruc.req_->app = app; + ruc.req_->stream = stream_name; // TODO: FIXME: Parse vhost. // discovery vhost, resolve the vhost from config SrsConfDirective* parsed_vhost = _srs_config->get_vhost(""); if (parsed_vhost) { - request.vhost = parsed_vhost->arg0(); + ruc.req_->vhost = parsed_vhost->arg0(); } SrsSdp local_sdp; // Config for SDP and session. - local_sdp.session_config_.dtls_role = _srs_config->get_rtc_dtls_role(request.vhost); - local_sdp.session_config_.dtls_version = _srs_config->get_rtc_dtls_version(request.vhost); + local_sdp.session_config_.dtls_role = _srs_config->get_rtc_dtls_role(ruc.req_->vhost); + local_sdp.session_config_.dtls_version = _srs_config->get_rtc_dtls_version(ruc.req_->vhost); // Whether enabled. bool server_enabled = _srs_config->get_rtc_server_enabled(); - bool rtc_enabled = _srs_config->get_rtc_enabled(request.vhost); + bool rtc_enabled = _srs_config->get_rtc_enabled(ruc.req_->vhost); if (server_enabled && !rtc_enabled) { - srs_warn("RTC disabled in vhost %s", request.vhost.c_str()); + srs_warn("RTC disabled in vhost %s", ruc.req_->vhost.c_str()); } if (!server_enabled || !rtc_enabled) { return srs_error_new(ERROR_RTC_DISABLED, "Disabled server=%d, rtc=%d, vhost=%s", - server_enabled, rtc_enabled, request.vhost.c_str()); + server_enabled, rtc_enabled, ruc.req_->vhost.c_str()); } - bool srtp_enabled = true; - if (srtp.empty()) { - srtp_enabled = _srs_config->get_rtc_server_encrypt(); - } else { - srtp_enabled = (srtp != "false"); - } - - bool dtls_enabled = (dtls != "false"); - // TODO: FIXME: When server enabled, but vhost disabled, should report error. SrsRtcConnection* session = NULL; - if ((err = server_->create_session(&request, remote_sdp, local_sdp, eip, false, dtls_enabled, srtp_enabled, &session)) != srs_success) { - return srs_error_wrap(err, "create session, dtls=%u, srtp=%u, eip=%s", dtls_enabled, srtp_enabled, eip.c_str()); + if ((err = server_->create_session(&ruc, local_sdp, &session)) != srs_success) { + return srs_error_wrap(err, "create session, dtls=%u, srtp=%u, eip=%s", ruc.dtls_, ruc.srtp_, eip.c_str()); } ostringstream os; @@ -213,7 +215,7 @@ srs_error_t SrsGoApiRtcPlay::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMe res->set("sessionid", SrsJsonAny::str(session->username().c_str())); srs_trace("RTC username=%s, dtls=%u, srtp=%u, offer=%dB, answer=%dB", session->username().c_str(), - dtls_enabled, srtp_enabled, remote_sdp_str.length(), local_sdp_str.length()); + ruc.dtls_, ruc.srtp_, remote_sdp_str.length(), local_sdp_str.length()); srs_trace("RTC remote offer: %s", srs_string_replace(remote_sdp_str.c_str(), "\r\n", "\\r\\n").c_str()); srs_trace("RTC local answer: %s", local_sdp_str.c_str()); @@ -301,7 +303,7 @@ srs_error_t SrsGoApiRtcPlay::exchange_sdp(SrsRequest* req, const SrsSdp& remote_ } } - // Only choose one match opus codec. + // Only choose one match opus. break; } @@ -500,50 +502,55 @@ srs_error_t SrsGoApiRtcPublish::do_serve_http(ISrsHttpResponseWriter* w, ISrsHtt string eip = r->query_get("eip"); srs_trace("RTC publish %s, api=%s, clientip=%s, app=%s, stream=%s, offer=%dB, eip=%s", - streamurl.c_str(), api.c_str(), clientip.c_str(), app.c_str(), stream_name.c_str(), remote_sdp_str.length(), eip.c_str()); + streamurl.c_str(), api.c_str(), clientip.c_str(), app.c_str(), stream_name.c_str(), remote_sdp_str.length(), eip.c_str() + ); + + // The RTC user config object. + SrsRtcUserConfig ruc; + ruc.eip_ = eip; + ruc.publish_ = true; + ruc.dtls_ = ruc.srtp_ = true; // TODO: FIXME: It seems remote_sdp doesn't represents the full SDP information. - SrsSdp remote_sdp; - if ((err = remote_sdp.parse(remote_sdp_str)) != srs_success) { + if ((err = ruc.remote_sdp_.parse(remote_sdp_str)) != srs_success) { return srs_error_wrap(err, "parse sdp failed: %s", remote_sdp_str.c_str()); } - if ((err = check_remote_sdp(remote_sdp)) != srs_success) { + if ((err = check_remote_sdp(ruc.remote_sdp_)) != srs_success) { return srs_error_wrap(err, "remote sdp check failed"); } - SrsRequest request; - request.app = app; - request.stream = stream_name; + ruc.req_->app = app; + ruc.req_->stream = stream_name; // TODO: FIXME: Parse vhost. // discovery vhost, resolve the vhost from config SrsConfDirective* parsed_vhost = _srs_config->get_vhost(""); if (parsed_vhost) { - request.vhost = parsed_vhost->arg0(); + ruc.req_->vhost = parsed_vhost->arg0(); } SrsSdp local_sdp; // TODO: FIXME: move to create_session. // Config for SDP and session. - local_sdp.session_config_.dtls_role = _srs_config->get_rtc_dtls_role(request.vhost); - local_sdp.session_config_.dtls_version = _srs_config->get_rtc_dtls_version(request.vhost); + local_sdp.session_config_.dtls_role = _srs_config->get_rtc_dtls_role(ruc.req_->vhost); + local_sdp.session_config_.dtls_version = _srs_config->get_rtc_dtls_version(ruc.req_->vhost); // Whether enabled. bool server_enabled = _srs_config->get_rtc_server_enabled(); - bool rtc_enabled = _srs_config->get_rtc_enabled(request.vhost); + bool rtc_enabled = _srs_config->get_rtc_enabled(ruc.req_->vhost); if (server_enabled && !rtc_enabled) { - srs_warn("RTC disabled in vhost %s", request.vhost.c_str()); + srs_warn("RTC disabled in vhost %s", ruc.req_->vhost.c_str()); } if (!server_enabled || !rtc_enabled) { return srs_error_new(ERROR_RTC_DISABLED, "Disabled server=%d, rtc=%d, vhost=%s", - server_enabled, rtc_enabled, request.vhost.c_str()); + server_enabled, rtc_enabled, ruc.req_->vhost.c_str()); } // TODO: FIXME: When server enabled, but vhost disabled, should report error. SrsRtcConnection* session = NULL; - if ((err = server_->create_session(&request, remote_sdp, local_sdp, eip, true, true, true, &session)) != srs_success) { + if ((err = server_->create_session(&ruc, local_sdp, &session)) != srs_success) { return srs_error_wrap(err, "create session"); } @@ -674,7 +681,7 @@ srs_error_t SrsGoApiRtcPublish::exchange_sdp(SrsRequest* req, const SrsSdp& remo } } - // Only choose one match opus codec. + // Only choose one match opus. break; } diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index 297dacf59..2a83cb19c 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -1806,19 +1806,21 @@ const SrsContextId& SrsRtcConnection::context_id() return cid_; } -srs_error_t SrsRtcConnection::add_publisher(SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp) +srs_error_t SrsRtcConnection::add_publisher(SrsRtcUserConfig* ruc, SrsSdp& local_sdp) { srs_error_t err = srs_success; + SrsRequest* req = ruc->req_; + SrsRtcStreamDescription* stream_desc = new SrsRtcStreamDescription(); SrsAutoFree(SrsRtcStreamDescription, stream_desc); // TODO: FIXME: Change to api of stream desc. - if ((err = negotiate_publish_capability(req, remote_sdp, stream_desc)) != srs_success) { + if ((err = negotiate_publish_capability(ruc, stream_desc)) != srs_success) { return srs_error_wrap(err, "publish negotiate"); } - if ((err = generate_publish_local_sdp(req, local_sdp, stream_desc, remote_sdp.is_unified())) != srs_success) { + if ((err = generate_publish_local_sdp(req, local_sdp, stream_desc, ruc->remote_sdp_.is_unified())) != srs_success) { return srs_error_wrap(err, "generate local sdp"); } @@ -1846,10 +1848,12 @@ srs_error_t SrsRtcConnection::add_publisher(SrsRequest* req, const SrsSdp& remot } // TODO: FIXME: Error when play before publishing. -srs_error_t SrsRtcConnection::add_player(SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp) +srs_error_t SrsRtcConnection::add_player(SrsRtcUserConfig* ruc, SrsSdp& local_sdp) { srs_error_t err = srs_success; + SrsRequest* req = ruc->req_; + if (_srs_rtc_hijacker) { if ((err = _srs_rtc_hijacker->on_before_play(this, req)) != srs_success) { return srs_error_wrap(err, "before play"); @@ -1857,7 +1861,7 @@ srs_error_t SrsRtcConnection::add_player(SrsRequest* req, const SrsSdp& remote_s } std::map play_sub_relations; - if ((err = negotiate_play_capability(req, remote_sdp, play_sub_relations)) != srs_success) { + if ((err = negotiate_play_capability(ruc, play_sub_relations)) != srs_success) { return srs_error_wrap(err, "play negotiate"); } @@ -1882,7 +1886,7 @@ srs_error_t SrsRtcConnection::add_player(SrsRequest* req, const SrsSdp& remote_s ++it; } - if ((err = generate_play_local_sdp(req, local_sdp, stream_desc, remote_sdp.is_unified())) != srs_success) { + if ((err = generate_play_local_sdp(req, local_sdp, stream_desc, ruc->remote_sdp_.is_unified())) != srs_success) { return srs_error_wrap(err, "generate local sdp"); } @@ -2692,7 +2696,7 @@ bool srs_sdp_has_h264_profile(const SrsSdp& sdp, const string& profile) return false; } -srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, const SrsSdp& remote_sdp, SrsRtcStreamDescription* stream_desc) +srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRtcUserConfig* ruc, SrsRtcStreamDescription* stream_desc) { srs_error_t err = srs_success; @@ -2700,13 +2704,16 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons return srs_error_new(ERROR_RTC_SDP_EXCHANGE, "stream description is NULL"); } + SrsRequest* req = ruc->req_; + const SrsSdp& remote_sdp = ruc->remote_sdp_; + bool nack_enabled = _srs_config->get_rtc_nack_enabled(req->vhost); bool twcc_enabled = _srs_config->get_rtc_twcc_enabled(req->vhost); // TODO: FIME: Should check packetization-mode=1 also. bool has_42e01f = srs_sdp_has_h264_profile(remote_sdp, "42e01f"); - for (size_t i = 0; i < remote_sdp.media_descs_.size(); ++i) { - const SrsMediaDesc& remote_media_desc = remote_sdp.media_descs_[i]; + for (int i = 0; i < (int)remote_sdp.media_descs_.size(); ++i) { + const SrsMediaDesc& remote_media_desc = remote_sdp.media_descs_.at(i); SrsRtcTrackDescription* track_desc = new SrsRtcTrackDescription(); SrsAutoFree(SrsRtcTrackDescription, track_desc); @@ -2736,23 +2743,29 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons return srs_error_new(ERROR_RTC_SDP_EXCHANGE, "no valid found opus payload type"); } - for (std::vector::iterator iter = payloads.begin(); iter != payloads.end(); ++iter) { - // if the playload is opus, and the encoding_param_ is channel - SrsAudioPayload* audio_payload = new SrsAudioPayload(iter->payload_type_, iter->encoding_name_, iter->clock_rate_, ::atol(iter->encoding_param_.c_str())); - audio_payload->set_opus_param_desc(iter->format_specific_param_); + for (int j = 0; j < (int)payloads.size(); j++) { + const SrsMediaPayloadType& payload = payloads.at(j); + + // if the payload is opus, and the encoding_param_ is channel + SrsAudioPayload* audio_payload = new SrsAudioPayload(payload.payload_type_, payload.encoding_name_, payload.clock_rate_, ::atol(payload.encoding_param_.c_str())); + audio_payload->set_opus_param_desc(payload.format_specific_param_); + // TODO: FIXME: Only support some transport algorithms. - for (int j = 0; j < (int)iter->rtcp_fb_.size(); ++j) { + for (int k = 0; k < (int)payload.rtcp_fb_.size(); ++k) { + const string& rtcp_fb = payload.rtcp_fb_.at(k); + if (nack_enabled) { - if (iter->rtcp_fb_.at(j) == "nack" || iter->rtcp_fb_.at(j) == "nack pli") { - audio_payload->rtcp_fbs_.push_back(iter->rtcp_fb_.at(j)); + if (rtcp_fb == "nack" || rtcp_fb == "nack pli") { + audio_payload->rtcp_fbs_.push_back(rtcp_fb); } } if (twcc_enabled && remote_twcc_id) { - if (iter->rtcp_fb_.at(j) == "transport-cc") { - audio_payload->rtcp_fbs_.push_back(iter->rtcp_fb_.at(j)); + if (rtcp_fb == "transport-cc") { + audio_payload->rtcp_fbs_.push_back(rtcp_fb); } } } + track_desc->type_ = "audio"; track_desc->set_codec_payload((SrsCodecPayload*)audio_payload); // Only choose one match opus codec. @@ -2765,13 +2778,15 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons } std::deque backup_payloads; - for (std::vector::iterator iter = payloads.begin(); iter != payloads.end(); ++iter) { - if (iter->format_specific_param_.empty()) { - backup_payloads.push_front(*iter); + for (int j = 0; j < (int)payloads.size(); j++) { + const SrsMediaPayloadType& payload = payloads.at(j); + + if (payload.format_specific_param_.empty()) { + backup_payloads.push_front(payload); continue; } H264SpecificParam h264_param; - if ((err = srs_parse_h264_fmtp(iter->format_specific_param_, h264_param)) != srs_success) { + if ((err = srs_parse_h264_fmtp(payload.format_specific_param_, h264_param)) != srs_success) { srs_error_reset(err); continue; } @@ -2779,21 +2794,23 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons bool profile_matched = (!has_42e01f || h264_param.profile_level_id == "42e01f"); // Try to pick the "best match" H.264 payload type. - if (h264_param.packetization_mode == "1" && h264_param.level_asymmerty_allow == "1" && profile_matched) { + if (profile_matched && h264_param.packetization_mode == "1" && h264_param.level_asymmerty_allow == "1") { // if the playload is opus, and the encoding_param_ is channel - SrsVideoPayload* video_payload = new SrsVideoPayload(iter->payload_type_, iter->encoding_name_, iter->clock_rate_); - video_payload->set_h264_param_desc(iter->format_specific_param_); + SrsVideoPayload* video_payload = new SrsVideoPayload(payload.payload_type_, payload.encoding_name_, payload.clock_rate_); + video_payload->set_h264_param_desc(payload.format_specific_param_); // TODO: FIXME: Only support some transport algorithms. - for (int j = 0; j < (int)iter->rtcp_fb_.size(); ++j) { + for (int k = 0; k < (int)payload.rtcp_fb_.size(); ++k) { + const string& rtcp_fb = payload.rtcp_fb_.at(k); + if (nack_enabled) { - if (iter->rtcp_fb_.at(j) == "nack" || iter->rtcp_fb_.at(j) == "nack pli") { - video_payload->rtcp_fbs_.push_back(iter->rtcp_fb_.at(j)); + if (rtcp_fb == "nack" || rtcp_fb == "nack pli") { + video_payload->rtcp_fbs_.push_back(rtcp_fb); } } if (twcc_enabled && remote_twcc_id) { - if (iter->rtcp_fb_.at(j) == "transport-cc") { - video_payload->rtcp_fbs_.push_back(iter->rtcp_fb_.at(j)); + if (rtcp_fb == "transport-cc") { + video_payload->rtcp_fbs_.push_back(rtcp_fb); } } } @@ -2804,34 +2821,35 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons break; } - backup_payloads.push_back(*iter); + backup_payloads.push_back(payload); } // Try my best to pick at least one media payload type. if (!track_desc->media_ && ! backup_payloads.empty()) { - SrsMediaPayloadType media_pt= backup_payloads.front(); + const SrsMediaPayloadType& payload = backup_payloads.front(); + // if the playload is opus, and the encoding_param_ is channel - SrsVideoPayload* video_payload = new SrsVideoPayload(media_pt.payload_type_, media_pt.encoding_name_, media_pt.clock_rate_); + SrsVideoPayload* video_payload = new SrsVideoPayload(payload.payload_type_, payload.encoding_name_, payload.clock_rate_); - std::vector rtcp_fbs = media_pt.rtcp_fb_; // TODO: FIXME: Only support some transport algorithms. - for (int j = 0; j < (int)rtcp_fbs.size(); ++j) { + for (int k = 0; k < (int)payload.rtcp_fb_.size(); ++k) { + const string& rtcp_fb = payload.rtcp_fb_.at(k); + if (nack_enabled) { - if (rtcp_fbs.at(j) == "nack" || rtcp_fbs.at(j) == "nack pli") { - video_payload->rtcp_fbs_.push_back(rtcp_fbs.at(j)); + if (rtcp_fb == "nack" || rtcp_fb == "nack pli") { + video_payload->rtcp_fbs_.push_back(rtcp_fb); } } if (twcc_enabled && remote_twcc_id) { - if (rtcp_fbs.at(j) == "transport-cc") { - video_payload->rtcp_fbs_.push_back(rtcp_fbs.at(j)); + if (rtcp_fb == "transport-cc") { + video_payload->rtcp_fbs_.push_back(rtcp_fb); } } } track_desc->set_codec_payload((SrsCodecPayload*)video_payload); - - srs_warn("choose backup H.264 payload type=%d", backup_payloads.front().payload_type_); + srs_warn("choose backup H.264 payload type=%d", payload.payload_type_); } // TODO: FIXME: Support RRTR? @@ -2845,8 +2863,9 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons track_desc->create_auxiliary_payload(remote_media_desc.find_media_with_encoding_name("ulpfec")); std::string track_id; - for (int i = 0; i < (int)remote_media_desc.ssrc_infos_.size(); ++i) { - SrsSSRCInfo ssrc_info = remote_media_desc.ssrc_infos_.at(i); + for (int j = 0; j < (int)remote_media_desc.ssrc_infos_.size(); ++j) { + const SrsSSRCInfo& ssrc_info = remote_media_desc.ssrc_infos_.at(j); + // ssrc have same track id, will be description in the same track description. if(track_id != ssrc_info.msid_tracker_) { SrsRtcTrackDescription* track_desc_copy = track_desc->copy(); @@ -2864,8 +2883,9 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRequest* req, cons } // set track fec_ssrc and rtx_ssrc - for (int i = 0; i < (int)remote_media_desc.ssrc_groups_.size(); ++i) { - SrsSSRCGroup ssrc_group = remote_media_desc.ssrc_groups_.at(i); + for (int j = 0; j < (int)remote_media_desc.ssrc_groups_.size(); ++j) { + const SrsSSRCGroup& ssrc_group = remote_media_desc.ssrc_groups_.at(j); + SrsRtcTrackDescription* track_desc = stream_desc->find_track_description_by_ssrc(ssrc_group.ssrcs_[0]); if (!track_desc) { continue; @@ -2987,10 +3007,13 @@ srs_error_t SrsRtcConnection::generate_publish_local_sdp(SrsRequest* req, SrsSdp return err; } -srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRequest* req, const SrsSdp& remote_sdp, std::map& sub_relations) +srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRtcUserConfig* ruc, std::map& sub_relations) { srs_error_t err = srs_success; + SrsRequest* req = ruc->req_; + const SrsSdp& remote_sdp = ruc->remote_sdp_; + bool nack_enabled = _srs_config->get_rtc_nack_enabled(req->vhost); bool twcc_enabled = _srs_config->get_rtc_twcc_enabled(req->vhost); // TODO: FIME: Should check packetization-mode=1 also. @@ -3001,8 +3024,8 @@ srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRequest* req, const S return srs_error_wrap(err, "fetch rtc source"); } - for (size_t i = 0; i < remote_sdp.media_descs_.size(); ++i) { - const SrsMediaDesc& remote_media_desc = remote_sdp.media_descs_[i]; + for (int i = 0; i < (int)remote_sdp.media_descs_.size(); ++i) { + const SrsMediaDesc& remote_media_desc = remote_sdp.media_descs_.at(i); // Whether feature enabled in remote extmap. int remote_twcc_id = 0; @@ -3036,7 +3059,7 @@ srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRequest* req, const S remote_payload = payloads.at(0); for (int j = 0; j < (int)payloads.size(); j++) { - SrsMediaPayloadType& payload = payloads.at(j); + const SrsMediaPayloadType& payload = payloads.at(j); // If exists 42e01f profile, choose it; otherwise, use the first payload. // TODO: FIME: Should check packetization-mode=1 also. @@ -3049,8 +3072,8 @@ srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRequest* req, const S track_descs = source->get_track_desc("video", "H264"); } - for (int i = 0; i < (int)track_descs.size(); ++i) { - SrsRtcTrackDescription* track = track_descs[i]->copy(); + for (int j = 0; j < (int)track_descs.size(); ++j) { + SrsRtcTrackDescription* track = track_descs.at(j)->copy(); // Use remote/source/offer PayloadType. track->media_->pt_of_publisher_ = track->media_->pt_; @@ -3102,86 +3125,6 @@ srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRequest* req, const S return err; } -srs_error_t SrsRtcConnection::negotiate_play_capability(SrsRequest* req, SrsRtcStreamDescription* req_stream_desc, - std::map& sub_relations) -{ - srs_error_t err = srs_success; - - SrsRtcStream* source = NULL; - if ((err = _srs_rtc_sources->fetch_or_create(req, &source)) != srs_success) { - return srs_error_wrap(err, "fetch rtc source"); - } - - std::vector src_track_descs; - //negotiate audio media - if(NULL != req_stream_desc->audio_track_desc_) { - SrsRtcTrackDescription* req_audio_track = req_stream_desc->audio_track_desc_; - int remote_twcc_id = req_audio_track->get_rtp_extension_id(kTWCCExt); - - src_track_descs = source->get_track_desc("audio", "opus"); - if (src_track_descs.size() > 0) { - // FIXME: use source sdp or subscribe sdp? native subscribe may have no sdp - SrsRtcTrackDescription *track = src_track_descs[0]->copy(); - - // Use remote/source/offer PayloadType. - track->media_->pt_of_publisher_ = track->media_->pt_; - track->media_->pt_ = req_audio_track->media_->pt_; - - if (req_audio_track->red_ && track->red_) { - track->red_->pt_of_publisher_ = track->red_->pt_; - track->red_->pt_ = req_audio_track->red_->pt_; - } - - track->del_rtp_extension_desc(kTWCCExt); - if (remote_twcc_id > 0) { - track->add_rtp_extension_desc(remote_twcc_id, kTWCCExt); - } - - track->mid_ = req_audio_track->mid_; - sub_relations.insert(make_pair(track->ssrc_, track)); - track->set_direction("sendonly"); - track->ssrc_ = SrsRtcSSRCGenerator::instance()->generate_ssrc(); - } - } - - //negotiate video media - std::vector req_video_tracks = req_stream_desc->video_track_descs_; - src_track_descs = source->get_track_desc("video", "h264"); - for(int i = 0; i < (int)req_video_tracks.size(); ++i) { - SrsRtcTrackDescription* req_video = req_video_tracks.at(i); - int remote_twcc_id = req_video->get_rtp_extension_id(kTWCCExt); - - for(int j = 0; j < (int)src_track_descs.size(); ++j) { - SrsRtcTrackDescription* src_video = src_track_descs.at(j); - if(req_video->id_ == src_video->id_) { - // FIXME: use source sdp or subscribe sdp? native subscribe may have no sdp - SrsRtcTrackDescription *track = src_video->copy(); - - // Use remote/source/offer PayloadType. - track->media_->pt_of_publisher_ = track->media_->pt_; - track->media_->pt_ = req_video->media_->pt_; - - if (req_video->red_ && track->red_) { - track->red_->pt_of_publisher_ = track->red_->pt_; - track->red_->pt_ = req_video->red_->pt_; - } - - track->del_rtp_extension_desc(kTWCCExt); - if (remote_twcc_id > 0) { - track->add_rtp_extension_desc(remote_twcc_id, kTWCCExt); - } - - track->mid_ = req_video->mid_; - sub_relations.insert(make_pair(track->ssrc_, track)); - track->set_direction("sendonly"); - track->ssrc_ = SrsRtcSSRCGenerator::instance()->generate_ssrc(); - } - } - } - - return err; -} - srs_error_t SrsRtcConnection::fetch_source_capability(SrsRequest* req, std::map& sub_relations) { srs_error_t err = srs_success; diff --git a/trunk/src/app/srs_app_rtc_conn.hpp b/trunk/src/app/srs_app_rtc_conn.hpp index 694f63c73..0f219f58b 100644 --- a/trunk/src/app/srs_app_rtc_conn.hpp +++ b/trunk/src/app/srs_app_rtc_conn.hpp @@ -64,6 +64,7 @@ class SrsRtcVideoSendTrack; class SrsErrorPithyPrint; class SrsPithyPrint; class SrsStatistic; +class SrsRtcUserConfig; const uint8_t kSR = 200; const uint8_t kRR = 201; @@ -476,8 +477,8 @@ public: void switch_to_context(); const SrsContextId& context_id(); public: - srs_error_t add_publisher(SrsRequest* request, const SrsSdp& remote_sdp, SrsSdp& local_sdp); - srs_error_t add_player(SrsRequest* request, const SrsSdp& remote_sdp, SrsSdp& local_sdp); + srs_error_t add_publisher(SrsRtcUserConfig* ruc, SrsSdp& local_sdp); + srs_error_t add_player(SrsRtcUserConfig* ruc, SrsSdp& local_sdp); public: // Before initialize, user must set the local SDP, which is used to inititlize DTLS. srs_error_t initialize(SrsRequest* r, bool dtls, bool srtp, std::string username); @@ -525,12 +526,11 @@ public: private: srs_error_t on_binding_request(SrsStunPacket* r); // publish media capabilitiy negotiate - srs_error_t negotiate_publish_capability(SrsRequest* req, const SrsSdp& remote_sdp, SrsRtcStreamDescription* stream_desc); + srs_error_t negotiate_publish_capability(SrsRtcUserConfig* ruc, SrsRtcStreamDescription* stream_desc); srs_error_t generate_publish_local_sdp(SrsRequest* req, SrsSdp& local_sdp, SrsRtcStreamDescription* stream_desc, bool unified_plan); // play media capabilitiy negotiate //TODO: Use StreamDescription to negotiate and remove first negotiate_play_capability function - srs_error_t negotiate_play_capability(SrsRequest* req, const SrsSdp& remote_sdp, std::map& sub_relations); - srs_error_t negotiate_play_capability(SrsRequest* req, SrsRtcStreamDescription* req_stream_desc, std::map& sub_relations); + srs_error_t negotiate_play_capability(SrsRtcUserConfig* ruc, std::map& sub_relations); srs_error_t fetch_source_capability(SrsRequest* req, std::map& sub_relations); srs_error_t generate_play_local_sdp(SrsRequest* req, SrsSdp& local_sdp, SrsRtcStreamDescription* stream_desc, bool unified_plan); srs_error_t create_player(SrsRequest* request, std::map sub_relations); diff --git a/trunk/src/app/srs_app_rtc_server.cpp b/trunk/src/app/srs_app_rtc_server.cpp index 311da5050..af00cbd0c 100644 --- a/trunk/src/app/srs_app_rtc_server.cpp +++ b/trunk/src/app/srs_app_rtc_server.cpp @@ -243,6 +243,18 @@ ISrsRtcServerHijacker::~ISrsRtcServerHijacker() { } +SrsRtcUserConfig::SrsRtcUserConfig() +{ + req_ = new SrsRequest(); + publish_ = false; + dtls_ = srtp_ = true; +} + +SrsRtcUserConfig::~SrsRtcUserConfig() +{ + srs_freep(req_); +} + SrsRtcServer::SrsRtcServer() { handler = NULL; @@ -505,27 +517,26 @@ srs_error_t SrsRtcServer::listen_api() return err; } -srs_error_t SrsRtcServer::create_session( - SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp, const std::string& mock_eip, - bool publish, bool dtls, bool srtp, - SrsRtcConnection** psession -) { +srs_error_t SrsRtcServer::create_session(SrsRtcUserConfig* ruc, SrsSdp& local_sdp, SrsRtcConnection** psession) +{ srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); + SrsRequest* req = ruc->req_; + SrsRtcStream* source = NULL; if ((err = _srs_rtc_sources->fetch_or_create(req, &source)) != srs_success) { return srs_error_wrap(err, "create source"); } - if (publish && !source->can_publish()) { + if (ruc->publish_ && !source->can_publish()) { return srs_error_new(ERROR_RTC_SOURCE_BUSY, "stream %s busy", req->get_stream_url().c_str()); } // TODO: FIXME: add do_create_session to error process. SrsRtcConnection* session = new SrsRtcConnection(this, cid); - if ((err = do_create_session(session, req, remote_sdp, local_sdp, mock_eip, publish, dtls, srtp)) != srs_success) { + if ((err = do_create_session(ruc, local_sdp, session)) != srs_success) { srs_freep(session); return srs_error_wrap(err, "create session"); } @@ -535,26 +546,25 @@ srs_error_t SrsRtcServer::create_session( return err; } -srs_error_t SrsRtcServer::do_create_session( - SrsRtcConnection* session, SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp, const std::string& mock_eip, - bool publish, bool dtls, bool srtp -) +srs_error_t SrsRtcServer::do_create_session(SrsRtcUserConfig* ruc, SrsSdp& local_sdp, SrsRtcConnection* session) { srs_error_t err = srs_success; + SrsRequest* req = ruc->req_; + // first add publisher/player for negotiate sdp media info - if (publish) { - if ((err = session->add_publisher(req, remote_sdp, local_sdp)) != srs_success) { + if (ruc->publish_) { + if ((err = session->add_publisher(ruc, local_sdp)) != srs_success) { return srs_error_wrap(err, "add publisher"); } } else { - if ((err = session->add_player(req, remote_sdp, local_sdp)) != srs_success) { + if ((err = session->add_player(ruc, local_sdp)) != srs_success) { return srs_error_wrap(err, "add player"); } } // All tracks default as inactive, so we must enable them. - session->set_all_tracks_status(req->get_stream_url(), publish, true); + session->set_all_tracks_status(req->get_stream_url(), ruc->publish_, true); std::string local_pwd = srs_random_str(32); std::string local_ufrag = ""; @@ -563,7 +573,7 @@ srs_error_t SrsRtcServer::do_create_session( while (true) { local_ufrag = srs_random_str(8); - username = local_ufrag + ":" + remote_sdp.get_ice_ufrag(); + username = local_ufrag + ":" + ruc->remote_sdp_.get_ice_ufrag(); if (!_srs_rtc_manager->find_by_name(username)) { break; } @@ -575,13 +585,13 @@ srs_error_t SrsRtcServer::do_create_session( local_sdp.set_fingerprint(_srs_rtc_dtls_certificate->get_fingerprint()); // We allows to mock the eip of server. - if (!mock_eip.empty()) { + if (!ruc->eip_.empty()) { string host; int port = _srs_config->get_rtc_server_listen(); - srs_parse_hostport(mock_eip, host, port); + srs_parse_hostport(ruc->eip_, host, port); local_sdp.add_candidate(host, port, "host"); - srs_trace("RTC: Use candidate mock_eip %s as %s:%d", mock_eip.c_str(), host.c_str(), port); + srs_trace("RTC: Use candidate mock_eip %s as %s:%d", ruc->eip_.c_str(), host.c_str(), port); } else { std::vector candidate_ips = get_candidate_ips(); for (int i = 0; i < (int)candidate_ips.size(); ++i) { @@ -594,11 +604,11 @@ srs_error_t SrsRtcServer::do_create_session( local_sdp.session_negotiate_ = local_sdp.session_config_; // Setup the negotiate DTLS role. - if (remote_sdp.get_dtls_role() == "active") { + if (ruc->remote_sdp_.get_dtls_role() == "active") { local_sdp.session_negotiate_.dtls_role = "passive"; - } else if (remote_sdp.get_dtls_role() == "passive") { + } else if (ruc->remote_sdp_.get_dtls_role() == "passive") { local_sdp.session_negotiate_.dtls_role = "active"; - } else if (remote_sdp.get_dtls_role() == "actpass") { + } else if (ruc->remote_sdp_.get_dtls_role() == "actpass") { local_sdp.session_negotiate_.dtls_role = local_sdp.session_config_.dtls_role; } else { // @see: https://tools.ietf.org/html/rfc4145#section-4.1 @@ -608,13 +618,13 @@ srs_error_t SrsRtcServer::do_create_session( } local_sdp.set_dtls_role(local_sdp.session_negotiate_.dtls_role); - session->set_remote_sdp(remote_sdp); + session->set_remote_sdp(ruc->remote_sdp_); // We must setup the local SDP, then initialize the session object. session->set_local_sdp(local_sdp); session->set_state(WAITING_STUN); // Before session initialize, we must setup the local SDP. - if ((err = session->initialize(req, dtls, srtp, username)) != srs_success) { + if ((err = session->initialize(req, ruc->dtls_, ruc->srtp_, username)) != srs_success) { return srs_error_wrap(err, "init"); } diff --git a/trunk/src/app/srs_app_rtc_server.hpp b/trunk/src/app/srs_app_rtc_server.hpp index 16aa12857..1a375b241 100644 --- a/trunk/src/app/srs_app_rtc_server.hpp +++ b/trunk/src/app/srs_app_rtc_server.hpp @@ -31,6 +31,7 @@ #include #include #include +#include #include @@ -84,6 +85,24 @@ public: virtual srs_error_t on_udp_packet(SrsUdpMuxSocket* skt, SrsRtcConnection* session, bool* pconsumed) = 0; }; +// The user config for RTC publish or play. +class SrsRtcUserConfig +{ +public: + // Original variables from API. + SrsSdp remote_sdp_; + std::string eip_; + + // Generated data. + SrsRequest* req_; + bool publish_; + bool dtls_; + bool srtp_; +public: + SrsRtcUserConfig(); + virtual ~SrsRtcUserConfig(); +}; + // The RTC server instance, listen UDP port, handle UDP packet, manage RTC connections. class SrsRtcServer : virtual public ISrsUdpMuxHandler, virtual public ISrsHourGlass, virtual public ISrsReloadHandler { @@ -112,16 +131,9 @@ public: srs_error_t listen_api(); public: // Peer start offering, we answer it. - srs_error_t create_session( - SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp, const std::string& mock_eip, - bool publish, bool dtls, bool srtp, - SrsRtcConnection** psession - ); + srs_error_t create_session(SrsRtcUserConfig* ruc, SrsSdp& local_sdp, SrsRtcConnection** psession); private: - srs_error_t do_create_session( - SrsRtcConnection* session, SrsRequest* req, const SrsSdp& remote_sdp, SrsSdp& local_sdp, - const std::string& mock_eip, bool publish, bool dtls, bool srtp - ); + srs_error_t do_create_session(SrsRtcUserConfig* ruc, SrsSdp& local_sdp, SrsRtcConnection* session); public: SrsRtcConnection* find_session_by_username(const std::string& ufrag); // interface ISrsHourGlass