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

Code before:
    for (uint16_t i = 0; i < (uint16_t)cnt; ++i) {
        int index = cache_index((start + i));
        SrsRtpPacket* pkt = cache_video_pkts_[index].pkt;

        // fix crash when pkt->payload() if pkt is nullptr;
        if (!pkt) continue;

        SrsRtpFUAPayload2* fua_payload = dynamic_cast<SrsRtpFUAPayload2*>(pkt->payload());
        if (!fua_payload) continue;

        if (fua_payload->start) {
            ++fu_s_c;
        }

        if (fua_payload->end) {
            ++fu_e_c;
        }
    }

    return fu_s_c == fu_e_c;
}

extern srs_error_t do_bridger_for_single(SrsRtmpFromRtcBridger *&bridger, SrsRequest *r);

std::string update_stream(size_t i, const SrsRtcTrackDescription* track_desc, const SrsRequest* rr) {
    auto ext = track_desc->rid_.rid;
    if (ext.empty()) {
        ext = std::to_string(i);
    }
    return rr->stream + "_" + ext;
}

struct BridgerContext {
    SrsRtcTrackDescription* track_desc;
    SrsRequest* r;
    SrsRtmpFromRtcBridger* bridger;

    srs_error_t create_bridger(size_t i, SrsRequest* rr) {
        assert(!bridger);
        assert(!r);
        assert(rr);
        r = rr->copy();
        r->stream = update_stream(i, track_desc, rr);
        auto err = do_bridger_for_single(bridger, r);
        if (err != srs_success) {
            return srs_error_wrap(err, "do_bridger_for_single");
        }
        err = bridger->on_publish();
        if (err != srs_success) {
            return srs_error_wrap(err, "bridger on publish");
        }

Review comment (suggestion):
You can add some comments here to indicate the change of stream name from simulcast rtc to rtmp.

`TRANS_BY_GPT3`

Code after:
    for (uint16_t i = 0; i < (uint16_t)cnt; ++i) {
        int index = cache_index((start + i));
        SrsRtpPacket* pkt = cache_video_pkts_[index].pkt;

        // fix crash when pkt->payload() if pkt is nullptr;
        if (!pkt) continue;

        SrsRtpFUAPayload2* fua_payload = dynamic_cast<SrsRtpFUAPayload2*>(pkt->payload());
        if (!fua_payload) continue;

        if (fua_payload->start) {
            ++fu_s_c;
        }

        if (fua_payload->end) {
            ++fu_e_c;
        }
    }

    return fu_s_c == fu_e_c;
}

extern srs_error_t do_bridger_for_single(SrsRtmpFromRtcBridger *&bridger, SrsRequest *r);

// NOTE: One simulcast(low,mid,high) stream to Multiple rtmp streams (rtmp://xxx_{low,mid,high})
std::string update_stream(size_t i, const SrsRtcTrackDescription* track_desc, const SrsRequest* rr) {
    std::stringstream name;
    name << rr->stream << '_';
    const std::string& ext = track_desc->rid_.rid;
    if (ext.empty()) {
        name << i;
    } else {
        name << ext;
    }
    return name.str();
}

struct BridgerContext {
    SrsRtcTrackDescription* track_desc;
    SrsRequest* r;
    SrsRtmpFromRtcBridger* bridger;

    srs_error_t create_bridger(size_t i, SrsRequest* rr) {
        srs_assert(!bridger);
        srs_assert(!r);
        srs_assert(rr);
        r = rr->copy();
        r->stream = update_stream(i, track_desc, rr);
        srs_error_t err = do_bridger_for_single(bridger, r);
        if (err != srs_success) {
            return srs_error_wrap(err, "do_bridger_for_single");