srs rtc播放流程

上一篇 http请求播放流程中
SrsGoApiRtcPlay::do_serve_http
// For each RTC session, we use short-term HTTP connection.
1、对于RTC会话,使用HTTP短连接。
// Parse req, the request json object, from body.
2、从body中解析请求的json对象
// Fetch params from req object.
3、从请求对象中获取参数,包括远程对端SDP信息、流地址、客户端IP、api、tid信息。
// Parse app and stream from streamurl.
4、解析出application和流名称。和RTMP中的类似。
// For client to specifies the EIP of server.
5、客户端指定信息
// DTLS/SRTP need reconfig after negotiating sdp.
6、在sdp协商结束后需要重新配置DTLS/SRTP
// It seems remote_sdp doesn't represents the full SDP information.
7、设置remote_sdp, 创建RTC用户配置对象,并设置相应的参数值,生成local_sdp;

创建RTC会话

srs_error_t SrsRtcServer::create_session(SrsRtcUserConfig* conf, SrsSdp& local_sdp, SrsRtcConnection** psession)
{
    srs_error_t err = srs_success;

    SrsContextId cid = _srs_context->get_id();

    SrsRequest* req = conf->req;

    if (conf->publish) {
        SrsRtcStream* source = NULL;
        //  根据req获取或创建source,用于_srs_rtc_sources管理。
        if ((err = _srs_rtc_sources->fetch_or_create(req, &source)) != srs_success) {
            return srs_error_wrap(err, "create source");
        }
        if (!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(conf, local_sdp, session)) != srs_success) {
        srs_freep(session);
        return srs_error_wrap(err, "create session");
    }

    *psession = session;

    return err;
}
srs_error_t SrsRtcServer::do_create_session(SrsRtcUserConfig* conf, SrsSdp& local_sdp, SrsRtcConnection* session)
{
    srs_error_t err = srs_success;

    SrsRequest* req = conf->req;
    //首先根据conf->publish类型为sdp媒体信息添加发布者/播放者
    // first add publisher/player for negotiate sdp media info
    if (conf->publish) {
        if ((err = session->add_publisher(conf, local_sdp)) != srs_success) {
            return srs_error_wrap(err, "add publisher");
        }
    } else {
        if ((err = session->add_player(conf, 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(), conf->publish, true);

    std::string local_pwd = srs_random_str(32);
    std::string local_ufrag = "";
    // TODO: FIXME: Rename for a better name, it's not an username.
    std::string username = "";
    while (true) {
        local_ufrag = srs_random_str(8);

        username = local_ufrag + ":" + conf->remote_sdp.get_ice_ufrag();
        if (!_srs_rtc_manager->find_by_name(username)) {
            break;
        }
    }

    local_sdp.set_ice_ufrag(local_ufrag);
    local_sdp.set_ice_pwd(local_pwd);
    local_sdp.set_fingerprint_algo("sha-256");
    local_sdp.set_fingerprint(_srs_rtc_dtls_certificate->get_fingerprint());

    // We allows to mock the eip of server.
    if (!conf->eip.empty()) {
        string host;
        int port = _srs_config->get_rtc_server_listen();
        srs_parse_hostport(conf->eip, host, port);

        local_sdp.add_candidate(host, port, "host");
        srs_trace("RTC: Use candidate mock_eip %s as %s:%d", conf->eip.c_str(), host.c_str(), port);
    } else {
        std::vector<string> candidate_ips = get_candidate_ips();
        for (int i = 0; i < (int)candidate_ips.size(); ++i) {
            local_sdp.add_candidate(candidate_ips[i], _srs_config->get_rtc_server_listen(), "host");
        }
        srs_trace("RTC: Use candidates %s", srs_join_vector_string(candidate_ips, ", ").c_str());
    }

    // Setup the negotiate DTLS by config.
    local_sdp.session_negotiate_ = local_sdp.session_config_;

    // Setup the negotiate DTLS role.
    if (conf->remote_sdp.get_dtls_role() == "active") {
        local_sdp.session_negotiate_.dtls_role = "passive";
    } else if (conf->remote_sdp.get_dtls_role() == "passive") {
        local_sdp.session_negotiate_.dtls_role = "active";
    } else if (conf->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
        // The default value of the setup attribute in an offer/answer exchange
        // is 'active' in the offer and 'passive' in the answer.
        local_sdp.session_negotiate_.dtls_role = "passive";
    }
    local_sdp.set_dtls_role(local_sdp.session_negotiate_.dtls_role);

    session->set_remote_sdp(conf->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, conf->dtls, conf->srtp, username)) != srs_success) {
        return srs_error_wrap(err, "init");
    }

    // We allows username is optional, but it never empty here.
    _srs_rtc_manager->add_with_name(username, session);

    return err;
}

添加播放者
我们本章内容是播放流程,所以就选取session->add_player进行分析

srs_error_t SrsRtcConnection::add_player(SrsRtcUserConfig* conf, SrsSdp& local_sdp)
{
    srs_error_t err = srs_success;

    SrsRequest* req = conf->req;

    if (_srs_rtc_hijacker) {
        if ((err = _srs_rtc_hijacker->on_before_play(this, req)) != srs_success) {
            return srs_error_wrap(err, "before play");
        }
    }

    if (_srs_config->get_vhost_is_edge(req->vhost)){
        SrsSource* rtmp_source = NULL;
        if ((err = _srs_sources->fetch_or_create(req, _srs_hybrid->srs()->instance(), &rtmp_source)) != srs_success) {
            return srs_error_wrap(err, "create rtmp source");
        }

        if (rtmp_source && (err = rtmp_source->on_edge_play()) != srs_success){
            return srs_error_wrap(err, "rtmp source play");
        }
        // update traffic state
        is_traffic_hit_ = rtmp_source->get_traffic_hit();
    }

    std::map<uint32_t, SrsRtcTrackDescription*> play_sub_relations;
    if ((err = negotiate_play_source_capability(req, conf->remote_sdp, play_sub_relations)) != srs_success) {
        return srs_error_wrap(err, "play negotiate");
    }

    // reconfig dtls/srtp of userconfig after negotiate sdp.
    if (conf->req->ext_abilities.is_support_unencrypted()){
        conf->dtls = conf->srtp = false;
    }

    if (!play_sub_relations.size()) {
        return srs_error_new(ERROR_RTC_SDP_EXCHANGE, "no play relations");
    }

    SrsRtcStreamDescription* stream_desc = new SrsRtcStreamDescription();
    SrsAutoFree(SrsRtcStreamDescription, stream_desc);
    std::map<uint32_t, SrsRtcTrackDescription*>::iterator it = play_sub_relations.begin();
    while (it != play_sub_relations.end()) {
        SrsRtcTrackDescription* track_desc = it->second;

        // TODO: FIXME: we only support one audio track.
        if (track_desc->type_ == "audio" && !stream_desc->audio_track_desc_) {
            stream_desc->audio_track_desc_ = track_desc->copy();
        }

        if (track_desc->type_ == "video") {
            stream_desc->video_track_descs_.push_back(track_desc->copy());
        }
        ++it;
    }

    if ((err = generate_play_local_sdp(req, local_sdp, stream_desc, conf->remote_sdp.is_unified())) != srs_success) {
        return srs_error_wrap(err, "generate local sdp");
    }

    if ((err = create_player(req, play_sub_relations)) != srs_success) {
        return srs_error_wrap(err, "create player");
    }

    return err;
}

最主要的是add_player这个函数了,明天再继续分析

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容