From e7714cd445d2dc5ee4c0dd65bedf48f9920d8faf Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 19 Aug 2015 15:14:26 +0800 Subject: [PATCH] add packets interval in ms to set the right send_min_interval --- trunk/conf/full.conf | 3 ++- trunk/research/librtmp/srs_rtmp_dump.c | 4 +++- trunk/src/app/srs_app_config.cpp | 6 ++--- trunk/src/app/srs_app_config.hpp | 2 +- trunk/src/app/srs_app_rtmp_conn.cpp | 10 ++++----- trunk/src/app/srs_app_rtmp_conn.hpp | 2 +- trunk/src/app/srs_app_source.cpp | 1 + trunk/src/libs/srs_librtmp.cpp | 31 +++++++++++++++++--------- trunk/src/libs/srs_librtmp.hpp | 11 +++++++++ 9 files changed, 48 insertions(+), 22 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index f81ba989e..50a124db1 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -875,8 +875,9 @@ vhost stream.control.com { # delivery packets in constant interval(not cbr). # @remark 0 to disable the minimal interval. # @remark >0 to make the srs to send message one by one. + # @remark user can get the right packets interval in ms by srs_rtmp_dump. # default: 0 - send_min_interval 10; + send_min_interval 10.0; # whether reduce the sequence header, # for some client which cannot got duplicated sequence header, # while the sequence header is not changed yet. diff --git a/trunk/research/librtmp/srs_rtmp_dump.c b/trunk/research/librtmp/srs_rtmp_dump.c index 065df1a92..49ea8edb9 100644 --- a/trunk/research/librtmp/srs_rtmp_dump.c +++ b/trunk/research/librtmp/srs_rtmp_dump.c @@ -256,8 +256,10 @@ int main(int argc, char** argv) } } + int64_t nb_packets = 0; u_int32_t pre_timestamp = 0; int64_t pre_now = srs_utils_time_ms(); + int64_t start_time = pre_now; for (;;) { int size; char type; @@ -269,7 +271,7 @@ int main(int argc, char** argv) goto rtmp_destroy; } - if (srs_human_print_rtmp_packet3(type, timestamp, data, size, pre_timestamp, pre_now) != 0) { + if (srs_human_print_rtmp_packet4(type, timestamp, data, size, pre_timestamp, pre_now, start_time, nb_packets++) != 0) { srs_human_trace("print rtmp packet failed."); goto rtmp_destroy; } diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 0d1796186..b4371d2aa 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -2518,9 +2518,9 @@ bool SrsConfig::get_tcp_nodelay(string vhost) return SRS_CONF_PERFER_FALSE(conf->arg0()); } -int SrsConfig::get_send_min_interval(string vhost) +double SrsConfig::get_send_min_interval(string vhost) { - static int DEFAULT = 0; + static double DEFAULT = 0.0; SrsConfDirective* conf = get_vhost(vhost); if (!conf) { @@ -2532,7 +2532,7 @@ int SrsConfig::get_send_min_interval(string vhost) return DEFAULT; } - return ::atoi(conf->arg0().c_str()); + return ::atof(conf->arg0().c_str()); } bool SrsConfig::get_reduce_sequence_header(string vhost) diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index efc805a3a..a8c6f76de 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -529,7 +529,7 @@ public: /** * the minimal send interval in ms. */ - virtual int get_send_min_interval(std::string vhost); + virtual double get_send_min_interval(std::string vhost); /** * whether reduce the sequence header. */ diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 43c3951a9..a24e98030 100755 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -256,9 +256,9 @@ int SrsRtmpConn::on_reload_vhost_smi(string vhost) return ret; } - int smi = _srs_config->get_send_min_interval(vhost); + double smi = _srs_config->get_send_min_interval(vhost); if (smi != send_min_interval) { - srs_trace("apply smi %d=>%d", send_min_interval, smi); + srs_trace("apply smi %.2f=>%.2f", send_min_interval, smi); send_min_interval = smi; } @@ -615,7 +615,7 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe // set the sock options. set_sock_options(); - srs_trace("start play smi=%d, mw_sleep=%d, mw_enabled=%d, realtime=%d", + srs_trace("start play smi=%.2f, mw_sleep=%d, mw_enabled=%d, realtime=%d", send_min_interval, mw_sleep, mw_enabled, realtime); while (!disposed) { @@ -667,7 +667,7 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe // get messages from consumer. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. // @remark when enable send_min_interval, only fetch one message a time. - int count = send_min_interval? 1 : 0; + int count = (send_min_interval > 0)? 1 : 0; if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) { srs_error("get messages from consumer failed. ret=%d", ret); return ret; @@ -745,7 +745,7 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe // apply the minimal interval for delivery stream in ms. if (send_min_interval > 0) { - st_usleep(send_min_interval * 1000); + st_usleep((int64_t)(send_min_interval * 1000)); } } diff --git a/trunk/src/app/srs_app_rtmp_conn.hpp b/trunk/src/app/srs_app_rtmp_conn.hpp index 36f311a07..869efde6b 100755 --- a/trunk/src/app/srs_app_rtmp_conn.hpp +++ b/trunk/src/app/srs_app_rtmp_conn.hpp @@ -86,7 +86,7 @@ private: // @see https://github.com/simple-rtmp-server/srs/issues/257 bool realtime; // the minimal interval in ms for delivery stream. - int send_min_interval; + double send_min_interval; public: SrsRtmpConn(SrsServer* svr, st_netfd_t c); virtual ~SrsRtmpConn(); diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 0acec6134..1584bdfbe 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -1454,6 +1454,7 @@ int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata bool drop_for_reduce = false; if (cache_metadata && _srs_config->get_reduce_sequence_header(_req->vhost)) { drop_for_reduce = true; + srs_warn("drop for reduce sh metadata, size=%d", msg->size); } // create a shared ptr message. diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index d1aab230e..78b253e1f 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -2324,9 +2324,20 @@ int srs_human_print_rtmp_packet2(char type, u_int32_t timestamp, char* data, int } int srs_human_print_rtmp_packet3(char type, u_int32_t timestamp, char* data, int size, u_int32_t pre_timestamp, int64_t pre_now) +{ + return srs_human_print_rtmp_packet4(type, timestamp, data, size, pre_timestamp, pre_now, 0, 0); +} + +int srs_human_print_rtmp_packet4(char type, u_int32_t timestamp, char* data, int size, u_int32_t pre_timestamp, int64_t pre_now, int64_t starttime, int64_t nb_packets) { int ret = ERROR_SUCCESS; + // packets interval in milliseconds. + double pi = 0; + if (pre_now > starttime) { + pi = (pre_now - starttime) / (double)nb_packets; + } + int diff = 0; if (pre_timestamp > 0) { diff = (int)timestamp - (int)pre_timestamp; @@ -2339,22 +2350,22 @@ int srs_human_print_rtmp_packet3(char type, u_int32_t timestamp, char* data, int u_int32_t pts; if (srs_utils_parse_timestamp(timestamp, type, data, size, &pts) != 0) { - srs_human_trace("Rtmp packet type=%s, dts=%d, diff=%d, ndiff=%d, size=%d, DecodeError", - srs_human_flv_tag_type2string(type), timestamp, diff, ndiff, size + srs_human_trace("Rtmp packet id=%"PRId64"/%.1f, type=%s, dts=%d, diff=%d, ndiff=%d, size=%d, DecodeError", + nb_packets, pi, srs_human_flv_tag_type2string(type), timestamp, diff, ndiff, size ); return ret; } if (type == SRS_RTMP_TYPE_VIDEO) { - srs_human_trace("Video packet type=%s, dts=%d, pts=%d, diff=%d, ndiff=%d, size=%d, %s(%s,%s)", - srs_human_flv_tag_type2string(type), timestamp, pts, diff, ndiff, size, + srs_human_trace("Video packet id=%"PRId64"/%.1f, type=%s, dts=%d, pts=%d, diff=%d, ndiff=%d, size=%d, %s(%s,%s)", + nb_packets, pi, srs_human_flv_tag_type2string(type), timestamp, pts, diff, ndiff, size, srs_human_flv_video_codec_id2string(srs_utils_flv_video_codec_id(data, size)), srs_human_flv_video_avc_packet_type2string(srs_utils_flv_video_avc_packet_type(data, size)), srs_human_flv_video_frame_type2string(srs_utils_flv_video_frame_type(data, size)) ); } else if (type == SRS_RTMP_TYPE_AUDIO) { - srs_human_trace("Audio packet type=%s, dts=%d, pts=%d, diff=%d, ndiff=%d, size=%d, %s(%s,%s,%s,%s)", - srs_human_flv_tag_type2string(type), timestamp, pts, diff, ndiff, size, + srs_human_trace("Audio packet id=%"PRId64"/%.1f, type=%s, dts=%d, pts=%d, diff=%d, ndiff=%d, size=%d, %s(%s,%s,%s,%s)", + nb_packets, pi, srs_human_flv_tag_type2string(type), timestamp, pts, diff, ndiff, size, srs_human_flv_audio_sound_format2string(srs_utils_flv_audio_sound_format(data, size)), srs_human_flv_audio_sound_rate2string(srs_utils_flv_audio_sound_rate(data, size)), srs_human_flv_audio_sound_size2string(srs_utils_flv_audio_sound_size(data, size)), @@ -2362,8 +2373,8 @@ int srs_human_print_rtmp_packet3(char type, u_int32_t timestamp, char* data, int srs_human_flv_audio_aac_packet_type2string(srs_utils_flv_audio_aac_packet_type(data, size)) ); } else if (type == SRS_RTMP_TYPE_SCRIPT) { - srs_human_verbose("Data packet type=%s, time=%d, diff=%d, ndiff=%d, size=%d", - srs_human_flv_tag_type2string(type), timestamp, diff, ndiff, size); + srs_human_verbose("Data packet id=%"PRId64"/%.1f, type=%s, time=%d, diff=%d, ndiff=%d, size=%d", + nb_packets, pi, srs_human_flv_tag_type2string(type), timestamp, diff, ndiff, size); int nparsed = 0; while (nparsed < size) { int nb_parsed_this = 0; @@ -2379,8 +2390,8 @@ int srs_human_print_rtmp_packet3(char type, u_int32_t timestamp, char* data, int srs_freep(amf0_str); } } else { - srs_human_trace("Rtmp packet type=%#x, dts=%d, pts=%d, diff=%d, ndiff=%d, size=%d", - type, timestamp, pts, diff, ndiff, size); + srs_human_trace("Rtmp packet id=%"PRId64"/%.1f, type=%#x, dts=%d, pts=%d, diff=%d, ndiff=%d, size=%d", + nb_packets, pi, type, timestamp, pts, diff, ndiff, size); } return ret; diff --git a/trunk/src/libs/srs_librtmp.hpp b/trunk/src/libs/srs_librtmp.hpp index fbbe42e56..a39f44510 100644 --- a/trunk/src/libs/srs_librtmp.hpp +++ b/trunk/src/libs/srs_librtmp.hpp @@ -904,8 +904,19 @@ extern const char* srs_human_flv_audio_aac_packet_type2string(char aac_packet_ty * @return an error code for parse the timetstamp to dts and pts. */ extern int srs_human_print_rtmp_packet(char type, u_int32_t timestamp, char* data, int size); +/** + * @param pre_timestamp the previous timestamp in ms to calc the diff. + */ extern int srs_human_print_rtmp_packet2(char type, u_int32_t timestamp, char* data, int size, u_int32_t pre_timestamp); +/** + * @param pre_now the previous system time in ms to calc the ndiff. + */ extern int srs_human_print_rtmp_packet3(char type, u_int32_t timestamp, char* data, int size, u_int32_t pre_timestamp, int64_t pre_now); +/** + * @param starttime the rtmpdump starttime in ms. + * @param nb_packets the number of packets received, to calc the packets interval in ms. + */ +extern int srs_human_print_rtmp_packet4(char type, u_int32_t timestamp, char* data, int size, u_int32_t pre_timestamp, int64_t pre_now, int64_t starttime, int64_t nb_packets); // log to console, for use srs-librtmp application. extern const char* srs_human_format_time();