Repository: ossrs/srs
File: trunk/src/srt/srt_to_rtmp.cpp (C++)

Code before:
}

srs_error_t rtmp_client::write_audio_raw_frame(char* frame, int frame_size, SrsRawAacStreamCodec* codec, uint32_t dts) {
    srs_error_t err = srs_success;
    
    char* data = NULL;
    int size = 0;
    if ((err = _aac_ptr->mux_aac2flv(frame, frame_size, codec, dts, &data, &size)) != srs_success) {
        return srs_error_wrap(err, "mux aac to flv");
    }
    if (_srs_config->get_srt_mix_correct()) {
        _rtmp_queue.insert_rtmp_data((unsigned char*)data, size, (int64_t)dts, SrsFrameTypeAudio);
        rtmp_write_work();
    } else {
        rtmp_write_packet(SrsFrameTypeAudio, dts, data, size);
    }

    return err;
}

srs_error_t rtmp_client::rtmp_write_packet(char type, uint32_t timestamp, char* data, int size) {
    srs_error_t err = srs_success;
    SrsSharedPtrMessage* msg = NULL;

    if (!_rtmp_conn_ptr) {
        return srs_error_wrap(err, "rtmp connection is close");
    }

    if ((err = srs_rtmp_create_msg(type, timestamp, data, size, _rtmp_conn_ptr->sid(), &msg)) != srs_success) {
        return srs_error_wrap(err, "create message");
    }
    srs_assert(msg);
    
    // send out encoded msg.
    if ((err = _rtmp_conn_ptr->send_and_free_message(msg)) != srs_success) {
        srs_error("rtmp client in srt2rtmp send message fail, url:%s", _url.c_str());
        close();
        return srs_error_wrap(err, "send messages");
    }
    
    return err;
}

void rtmp_client::rtmp_write_work() {
    rtmp_packet_info_s packet_info;
    bool ret = false;
    
    do {
        ret = _rtmp_queue.get_rtmp_data(packet_info);
        if (ret) {
            rtmp_write_packet(packet_info._type, packet_info._dts, (char*)packet_info._data, packet_info._len);

Review comment (bug):
At this time, when err is success, wrap(success) will cause the code outside to be success, which is incorrect.

Here, the error should be created using srs_error_new.

`TRANS_BY_GPT3`

Code after:
srs_error_t rtmp_client::write_audio_raw_frame(char* frame, int frame_size, SrsRawAacStreamCodec* codec, uint32_t dts) {
    srs_error_t err = srs_success;
    
    char* data = NULL;
    int size = 0;
    if ((err = _aac_ptr->mux_aac2flv(frame, frame_size, codec, dts, &data, &size)) != srs_success) {
        return srs_error_wrap(err, "mux aac to flv");
    }
    if (_srs_config->get_srt_mix_correct()) {
        _rtmp_queue.insert_rtmp_data((unsigned char*)data, size, (int64_t)dts, SrsFrameTypeAudio);
        err = rtmp_write_work();
    } else {
        err = rtmp_write_packet(SrsFrameTypeAudio, dts, data, size);
    }

    return err;
}

srs_error_t rtmp_client::rtmp_write_packet(char type, uint32_t timestamp, char* data, int size) {
    srs_error_t err = srs_success;
    SrsSharedPtrMessage* msg = NULL;

    if (!_rtmp_conn_ptr) {
        //when rtmp connection is closed, it's not error and just return;
        srs_freepa(data);
        return err;
    }

    if ((err = srs_rtmp_create_msg(type, timestamp, data, size, _rtmp_conn_ptr->sid(), &msg)) != srs_success) {
        return srs_error_wrap(err, "create message fail, url:%s", _url.c_str());
    }
    srs_assert(msg);
    
    // send out encoded msg.
    if ((err = _rtmp_conn_ptr->send_and_free_message(msg)) != srs_success) {
        close();
        return srs_error_wrap(err, "rtmp client in srt2rtmp send message fail, url:%s", _url.c_str());
    }
    
    return err;
}

srs_error_t rtmp_client::rtmp_write_work() {
    srs_error_t err = srs_success;
    rtmp_packet_info_s packet_info;
    bool ret = false;
    
    do {
        ret = _rtmp_queue.get_rtmp_data(packet_info);
        if (ret) {
            err = rtmp_write_packet(packet_info._type, packet_info._dts, (char*)packet_info._data, packet_info._len);