Skip to content

Commit

Permalink
srt publish core dump bug (#2429)
Browse files Browse the repository at this point in the history
* solve srt push bugs

Co-authored-by: shiwei <shiwei05@kuaishou.com>
  • Loading branch information
2 people authored and winlinvip committed Jun 24, 2021
1 parent fed57f6 commit bb9aefd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion trunk/src/srt/srt_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SRT_DATA_MSG::SRT_DATA_MSG(unsigned char* data_p, unsigned int len, const std::s

SRT_DATA_MSG::~SRT_DATA_MSG() {
if (_data_p && (_len > 0)) {
delete _data_p;
delete[] _data_p;
}
}

Expand Down
47 changes: 32 additions & 15 deletions trunk/src/srt/srt_to_rtmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ srs_error_t rtmp_client::connect() {

if ((err = _rtmp_conn_ptr->publish(SRS_CONSTS_RTMP_PROTOCOL_CHUNK_SIZE)) != srs_success) {
close();
return srs_error_wrap(err, "publish error, url:%s", _url.c_str());
return srs_error_wrap(err, "rtmp client in srt2rtmp publish fail url:%s", _url.c_str());
}
_connect_flag = true;
return err;
Expand Down Expand Up @@ -330,9 +330,9 @@ srs_error_t rtmp_client::write_h264_sps_pps(uint32_t dts, uint32_t pts) {

if (_srs_config->get_srt_mix_correct()) {
_rtmp_queue.insert_rtmp_data((unsigned char*)flv, nb_flv, (int64_t)dts, SrsFrameTypeVideo);
rtmp_write_work();
err = rtmp_write_work();
} else {
rtmp_write_packet(SrsFrameTypeVideo, dts, flv, nb_flv);
err = rtmp_write_packet(SrsFrameTypeVideo, dts, flv, nb_flv);
}

// reset sps and pps.
Expand Down Expand Up @@ -376,9 +376,9 @@ srs_error_t rtmp_client::write_h264_ipb_frame(char* frame, int frame_size, uint3
}
if (_srs_config->get_srt_mix_correct()) {
_rtmp_queue.insert_rtmp_data((unsigned char*)flv, nb_flv, (int64_t)dts, SrsFrameTypeVideo);
rtmp_write_work();
err = rtmp_write_work();
} else {
rtmp_write_packet(SrsFrameTypeVideo, dts, flv, nb_flv);
err = rtmp_write_packet(SrsFrameTypeVideo, dts, flv, nb_flv);
}

return err;
Expand All @@ -394,9 +394,9 @@ srs_error_t rtmp_client::write_audio_raw_frame(char* frame, int frame_size, SrsR
}
if (_srs_config->get_srt_mix_correct()) {
_rtmp_queue.insert_rtmp_data((unsigned char*)data, size, (int64_t)dts, SrsFrameTypeAudio);
rtmp_write_work();
err = rtmp_write_work();
} else {
rtmp_write_packet(SrsFrameTypeAudio, dts, data, size);
err = rtmp_write_packet(SrsFrameTypeAudio, dts, data, size);
}

return err;
Expand All @@ -406,39 +406,50 @@ srs_error_t rtmp_client::rtmp_write_packet(char type, uint32_t timestamp, char*
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");
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, "send messages");
return srs_error_wrap(err, "rtmp client in srt2rtmp send message fail, url:%s", _url.c_str());
}

return err;
}

void rtmp_client::rtmp_write_work() {
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) {
rtmp_write_packet(packet_info._type, packet_info._dts, (char*)packet_info._data, packet_info._len);
err = rtmp_write_packet(packet_info._type, packet_info._dts, (char*)packet_info._data, packet_info._len);
if (err != srs_success) {
break;
}
}
} while(ret);
return;

return err;
}

srs_error_t rtmp_client::on_ts_video(std::shared_ptr<SrsBuffer> avs_ptr, uint64_t dts, uint64_t pts) {
srs_error_t err = srs_success;

// ensure rtmp connected.
if ((err = connect()) != srs_success) {
return srs_error_wrap(err, "connect");
return err;
}
dts = dts / 90;
pts = pts / 90;
Expand Down Expand Up @@ -612,6 +623,7 @@ srs_error_t rtmp_client::on_ts_audio(std::shared_ptr<SrsBuffer> avs_ptr, uint64_
void rtmp_client::on_data_callback(SRT_DATA_MSG_PTR data_ptr, unsigned int media_type,
uint64_t dts, uint64_t pts)
{
srs_error_t err = srs_success;
if (!data_ptr || (data_ptr->get_data() == nullptr) || (data_ptr->data_len() == 0)) {
assert(0);
return;
Expand All @@ -620,11 +632,16 @@ void rtmp_client::on_data_callback(SRT_DATA_MSG_PTR data_ptr, unsigned int media
auto avs_ptr = std::make_shared<SrsBuffer>((char*)data_ptr->get_data(), data_ptr->data_len());

if (media_type == STREAM_TYPE_VIDEO_H264) {
on_ts_video(avs_ptr, dts, pts);
err = on_ts_video(avs_ptr, dts, pts);
} else if (media_type == STREAM_TYPE_AUDIO_AAC) {
on_ts_audio(avs_ptr, dts, pts);
err = on_ts_audio(avs_ptr, dts, pts);
} else {
srs_error("mpegts demux unkown stream type:0x%02x, only support h264+aac", media_type);
return;
}

if (err != srs_success) {
srs_error("send media data error:", srs_error_code(err));
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/srt/srt_to_rtmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class rtmp_client : public ts_media_data_callback_I, public std::enable_shared_f

int get_sample_rate(char sound_rate);

void rtmp_write_work();
srs_error_t rtmp_write_work();

private:
virtual srs_error_t rtmp_write_packet(char type, uint32_t timestamp, char* data, int size);
Expand Down

0 comments on commit bb9aefd

Please sign in to comment.