Repository: ossrs/srs
File: trunk/src/app/srs_app_source.cpp (C++)

Code before:
    // Don't start exec when source is not active.
    if (!is_active) {
        return err;
    }
    
    if ((err = ng_exec->on_publish(req)) != srs_success) {
        return srs_error_wrap(err, "start exec failed");
    }
    srs_trace("vhost %s exec reload success", vhost.c_str());
    
    return err;
}

srs_error_t SrsOriginHub::create_forwarders()
{
    srs_error_t err = srs_success;
    
    if (!_srs_config->get_forward_enabled(req->vhost)) {
        return err;
    }
    
    srs_utime_t queue_size = _srs_config->get_queue_length(req->vhost);

    // For backend config
    if (true) {
        SrsConfDirective* conf = _srs_config->get_forward_backend(req->vhost);
        if (conf) {
            // only get first backend url
            std::string backend_url = conf->arg0();

            // get urls on forward backend
            std::vector<std::string> urls;
            if ((err = SrsHttpHooks::on_forward_backend(backend_url, req, urls)) != srs_success) {
                return srs_error_wrap(err, "get forward backend failed");
            }

            // create forwarders by urls
            std::vector<std::string>::iterator it;
            for (it = urls.begin(); it != urls.end(); ++it) {
                std::string url = *it;

                // create temp SrsRequest by url
                SrsRequest* freq = new SrsRequest();
                SrsAutoFree(SrsRequest, freq);
                srs_parse_rtmp_url(url, freq->tcUrl, freq->stream);
                srs_discovery_tc_url(freq->tcUrl, freq->schema, freq->host, freq->vhost, freq->app, freq->stream, freq->port, freq->param);

                // create forwarder
                SrsForwarder* forwarder = new SrsForwarder(this);
                forwarders.push_back(forwarder);


Review comment (suggestion):
Translate to English:
'Change it into a function.'
Make sure to maintain the markdown structure.

`TRANS_BY_GPT3`

Code after:
    // Don't start exec when source is not active.
    if (!is_active) {
        return err;
    }
    
    if ((err = ng_exec->on_publish(req_)) != srs_success) {
        return srs_error_wrap(err, "start exec failed");
    }
    srs_trace("vhost %s exec reload success", vhost.c_str());
    
    return err;
}

srs_error_t SrsOriginHub::create_forwarders()
{
    srs_error_t err = srs_success;
    
    if (!_srs_config->get_forward_enabled(req_->vhost)) {
        return err;
    }

    // For backend config
    // If backend is enabled and applied, ignore destination.
    bool applied_backend_server = false;
    if ((err = create_backend_forwarders(applied_backend_server)) != srs_success) {
        return srs_error_wrap(err, "create backend applied=%d", applied_backend_server);
    }

    // Already applied backend server, ignore destination.
    if (applied_backend_server) {
        return err;
    }

    // For destanition config
    SrsConfDirective* conf = _srs_config->get_forwards(req_->vhost);
    for (int i = 0; conf && i < (int)conf->args.size(); i++) {
        std::string forward_server = conf->args.at(i);
        
        SrsForwarder* forwarder = new SrsForwarder(this);
        forwarders.push_back(forwarder);
        
        // initialize the forwarder with request.
        if ((err = forwarder->initialize(req_, forward_server)) != srs_success) {
            return srs_error_wrap(err, "init forwarder");
        }

        srs_utime_t queue_size = _srs_config->get_queue_length(req_->vhost);
        forwarder->set_queue_size(queue_size);
        
        if ((err = forwarder->on_publish()) != srs_success) {
            return srs_error_wrap(err, "start forwarder failed, vhost=%s, app=%s, stream=%s, forward-to=%s",