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

Code before:
                continue;
            }
            if (!ctx.bridger && (err = ctx.create_bridger(i, r)) != srs_success) {
                return srs_error_wrap(err, "BridgerContext::create_bridger");
            }
            srs_assert(ctx.bridger);
            bridgers_[ssrc] = ctx.bridger;
            SimulcastBridgerAdapter* b = (SimulcastBridgerAdapter*)ctx.bridger;
            sources_.push_back(b->source_);
            return ctx.bridger->packet_video(pkt);
        }
        return err;
    }

public:
    SimulcastBridgerAdapter(std::vector<SrsRtcVideoRecvTrack*>& video_tracks): SrsRtmpFromRtcBridger(NULL), r(NULL) {
        for (size_t i=0; i<video_tracks.size(); ++i) {
            SrsRtcVideoRecvTrack* track = video_tracks.at(i);
            contexts_.emplace_back(BridgerContext{track->track_desc_, NULL, NULL});
        }
    }

    srs_error_t transcode_audio(SrsRtpPacket *pkt) {
        srs_error_t err = srs_success;

        // to common message.
        uint32_t ts = pkt->get_avsync_time();
        if (is_first_audio) {
            int header_len = 0;
            uint8_t* header = NULL;
            codec_->aac_codec_header(&header, &header_len);

            SrsCommonMessage out_rtmp;
            packet_aac(&out_rtmp, (char *)header, header_len, ts, is_first_audio);

            if ((err = dispatch_audio(&out_rtmp)) != srs_success) {
                return srs_error_wrap(err, "source on audio");
            }

            is_first_audio = false;
        }

        std::vector<SrsAudioFrame *> out_pkts;
        SrsRtpRawPayload *payload = dynamic_cast<SrsRtpRawPayload *>(pkt->payload());

        SrsAudioFrame frame;
        frame.add_sample(payload->payload, payload->nn_payload);
        frame.dts = ts;
        frame.cts = 0;

        err = codec_->transcode(&frame, out_pkts);

Review comment (suggestion):
The entire function below is also repetitive code, only the implementation of `dispatch_audio` is necessary.

`TRANS_BY_GPT3`

Code after:

        for (size_t i =0; i<contexts_.size(); i++) {
            BridgerContext& ctx = contexts_.at(i);
            if (!ctx.track_desc->has_ssrc(ssrc)) {
                continue;
            }
            if (!ctx.bridger && (err = ctx.create_bridger(i, r)) != srs_success) {
                return srs_error_wrap(err, "BridgerContext::create_bridger");
            }
            srs_assert(ctx.bridger);
            bridgers_[ssrc] = ctx.bridger;
            SimulcastBridgerAdapter* b = (SimulcastBridgerAdapter*)ctx.bridger;
            sources_.push_back(b->source_);
            return ctx.bridger->packet_video(pkt);
        }
        return err;
    }

public:
    SimulcastBridgerAdapter(std::vector<SrsRtcVideoRecvTrack*>& video_tracks): SrsRtmpFromRtcBridger(NULL), r(NULL) {
        for (size_t i=0; i<video_tracks.size(); ++i) {
            SrsRtcVideoRecvTrack* track = video_tracks.at(i);
            contexts_.push_back(BridgerContext{track->track_desc_, NULL, NULL});
        }
    }

    srs_error_t transcode_audio(SrsRtpPacket *pkt) {
        srs_error_t err = srs_success;

        // to common message.
        uint32_t ts = pkt->get_avsync_time();
        if (is_first_audio) {
            int header_len = 0;
            uint8_t* header = NULL;
            codec_->aac_codec_header(&header, &header_len);

            SrsCommonMessage out_rtmp;
            packet_aac(&out_rtmp, (char *)header, header_len, ts, is_first_audio);

            if ((err = dispatch_audio(&out_rtmp)) != srs_success) {
                return srs_error_wrap(err, "source on audio");
            }

            is_first_audio = false;
        }

        std::vector<SrsAudioFrame *> out_pkts;
        SrsRtpRawPayload *payload = dynamic_cast<SrsRtpRawPayload *>(pkt->payload());

        SrsAudioFrame frame;
        frame.add_sample(payload->payload, payload->nn_payload);