diff --git a/trunk/research/librtmp/srs_flv_parser.c b/trunk/research/librtmp/srs_flv_parser.c index b39d91cb4..c46000f65 100644 --- a/trunk/research/librtmp/srs_flv_parser.c +++ b/trunk/research/librtmp/srs_flv_parser.c @@ -141,6 +141,7 @@ int parse_flv(srs_flv_t flv) } srs_human_trace("start parse flv"); + char buffer[1024]; for (;;) { offset = srs_flv_tellg(flv); @@ -162,7 +163,9 @@ int parse_flv(srs_flv_t flv) data = (char*)malloc(size); if ((ret = srs_flv_read_tag_data(flv, data, size)) == 0) { - if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) == 0) { + if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) == 0) { + srs_human_trace("%s", buffer); + char hbuf[48]; char tbuf[48]; parse_bytes(data, size, hbuf, sizeof(hbuf), tbuf, sizeof(tbuf), 16); srs_human_raw("offset=%d, first and last 16 bytes:\n" diff --git a/trunk/research/librtmp/srs_ingest_flv.c b/trunk/research/librtmp/srs_ingest_flv.c index 88a8b981b..f48b495e5 100644 --- a/trunk/research/librtmp/srs_ingest_flv.c +++ b/trunk/research/librtmp/srs_ingest_flv.c @@ -135,6 +135,7 @@ int do_proxy(srs_flv_t flv, srs_rtmp_t ortmp, int64_t re, int32_t* pstarttime, u char* data = NULL; srs_human_trace("start ingest flv to RTMP stream"); + char buffer[1024]; for (;;) { // tag header if ((ret = srs_flv_read_tag_header(flv, &type, &size, ptimestamp)) != 0) { @@ -159,10 +160,11 @@ int do_proxy(srs_flv_t flv, srs_rtmp_t ortmp, int64_t re, int32_t* pstarttime, u uint32_t timestamp = *ptimestamp; - if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) != 0) { + if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) != 0) { srs_human_trace("print packet failed. ret=%d", ret); return ret; } + srs_human_trace("%s", buffer); if ((ret = srs_rtmp_write_packet(ortmp, type, *ptimestamp, data, size)) != 0) { srs_human_trace("irtmp get packet failed. ret=%d", ret); diff --git a/trunk/research/librtmp/srs_ingest_mp4.c b/trunk/research/librtmp/srs_ingest_mp4.c index ea4256a7b..e8d3e74c3 100644 --- a/trunk/research/librtmp/srs_ingest_mp4.c +++ b/trunk/research/librtmp/srs_ingest_mp4.c @@ -184,6 +184,7 @@ int do_proxy(srs_mp4_t mp4, srs_rtmp_t ortmp, int64_t re, int32_t* pstarttime, u int ret = 0; srs_human_trace("start ingest mp4 to RTMP stream"); + char buffer[1024]; for (;;) { // packet data char type; @@ -213,10 +214,11 @@ int do_proxy(srs_mp4_t mp4, srs_rtmp_t ortmp, int64_t re, int32_t* pstarttime, u } uint32_t timestamp = *ptimestamp; - if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) != 0) { + if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) != 0) { srs_human_trace("print packet failed. ret=%d", ret); return ret; } + srs_human_trace("%s", buffer); if ((ret = srs_rtmp_write_packet(ortmp, type, *ptimestamp, data, size)) != 0) { srs_human_trace("irtmp get packet failed. ret=%d", ret); diff --git a/trunk/research/librtmp/srs_ingest_rtmp.c b/trunk/research/librtmp/srs_ingest_rtmp.c index 995922968..6af36f5e5 100644 --- a/trunk/research/librtmp/srs_ingest_rtmp.c +++ b/trunk/research/librtmp/srs_ingest_rtmp.c @@ -129,6 +129,8 @@ int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp) } else { srs_human_verbose("start proxy RTMP stream"); } + + char buffer[1024]; for (;;) { if ((ret = srs_rtmp_read_packet(irtmp, &type, ×tamp, &data, &size)) != 0) { srs_human_trace("irtmp get packet failed. ret=%d", ret); @@ -146,10 +148,11 @@ int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp) } if (verbose || ((nb_msgs++ % PITHY_PRINT_EVERY_MSGS) == 0 && nb_msgs > 10)) { - if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) != 0) { + if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) != 0) { srs_human_trace("print packet failed. ret=%d", ret); return ret; } + srs_human_trace("%s", buffer); } if ((ret = srs_rtmp_write_packet(ortmp, type, timestamp, data, size)) != 0) { diff --git a/trunk/research/librtmp/srs_play.c b/trunk/research/librtmp/srs_play.c index ba24c4246..7ab987428 100644 --- a/trunk/research/librtmp/srs_play.c +++ b/trunk/research/librtmp/srs_play.c @@ -62,6 +62,7 @@ int main(int argc, char** argv) } srs_human_trace("play stream success"); + char buffer[1024]; for (;;) { int size; char type; @@ -72,9 +73,10 @@ int main(int argc, char** argv) goto rtmp_destroy; } - if (srs_human_print_rtmp_packet(type, timestamp, data, size) != 0) { + if (srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size) != 0) { goto rtmp_destroy; } + srs_human_trace("%s", buffer); free(data); } diff --git a/trunk/research/librtmp/srs_rtmp_dump.c b/trunk/research/librtmp/srs_rtmp_dump.c index bc8eb603e..eea2489aa 100644 --- a/trunk/research/librtmp/srs_rtmp_dump.c +++ b/trunk/research/librtmp/srs_rtmp_dump.c @@ -174,6 +174,7 @@ int do_proxy(srs_rtmp_t rtmp, srs_flv_t flv) uint32_t pre_timestamp = 0; int64_t pre_now = -1; int64_t start_time = -1; + char buffer[1024]; for (;;) { int size; char type; @@ -192,10 +193,12 @@ int do_proxy(srs_rtmp_t rtmp, srs_flv_t flv) start_time = srs_utils_time_ms(); } - if ((ret = srs_human_print_rtmp_packet4(type, timestamp, data, size, pre_timestamp, pre_now, start_time, nb_packets++)) != 0) { + if ((ret = srs_human_format_rtmp_packet2(buffer, sizeof(buffer), type, timestamp, data, size, pre_timestamp, pre_now, start_time, nb_packets++)) != 0) { srs_human_trace("print rtmp packet failed, ret=%d", ret); return ret; } + srs_human_trace("%s", buffer); + pre_timestamp = timestamp; pre_now = srs_utils_time_ms(); diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index ff7b16a8f..982643a33 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -123,6 +123,9 @@ struct Context int64_t stimeout; int64_t rtimeout; + // The RTMP handler level buffer, can used to format packet. + char buffer[1024]; + Context() { rtmp = NULL; skt = NULL; @@ -544,6 +547,8 @@ int srs_version_revision() srs_rtmp_t srs_rtmp_create(const char* url) { + int ret = ERROR_SUCCESS; + Context* context = new Context(); context->url = url; @@ -551,7 +556,8 @@ srs_rtmp_t srs_rtmp_create(const char* url) srs_freep(context->skt); context->skt = new SimpleSocketStream(); - if (context->skt->create_socket(context) != ERROR_SUCCESS) { + if ((ret = context->skt->create_socket(context)) != ERROR_SUCCESS) { + errno = ret; // free the context and return NULL srs_freep(context); return NULL; @@ -2536,25 +2542,19 @@ const char* srs_human_flv_audio_aac_packet_type2string(char aac_packet_type) return unknown; } -int srs_human_print_rtmp_packet(char type, uint32_t timestamp, char* data, int size) +int srs_human_format_rtmp_packet(char* buffer, int nb_buffer, char type, uint32_t timestamp, char* data, int size) { - return srs_human_print_rtmp_packet2(type, timestamp, data, size, 0); -} - -int srs_human_print_rtmp_packet2(char type, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp) -{ - return srs_human_print_rtmp_packet3(type, timestamp, data, size, pre_timestamp, 0); + return srs_human_format_rtmp_packet2(buffer, nb_buffer, type, timestamp, data, size, 0, 0, 0, 0); } -int srs_human_print_rtmp_packet3(char type, uint32_t timestamp, char* data, int size, uint32_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, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp, int64_t pre_now, int64_t starttime, int64_t nb_packets) -{ +int srs_human_format_rtmp_packet2(char* buffer, int nb_buffer, char type, uint32_t timestamp, char* data, int size, + uint32_t pre_timestamp, int64_t pre_now, int64_t starttime, int64_t nb_packets +) { int ret = ERROR_SUCCESS; + // Initialize to empty NULL terminated string. + buffer[0] = 0; + // packets interval in milliseconds. double pi = 0; if (pre_now > starttime) { @@ -2587,33 +2587,30 @@ int srs_human_print_rtmp_packet4(char type, uint32_t timestamp, char* data, int } uint32_t pts; - if (srs_utils_parse_timestamp(timestamp, type, data, size, &pts) != 0) { - srs_human_trace("Rtmp packet id=%"PRId64"/%.1f/%.1f, type=%s, dts=%d, ndiff=%d, diff=%d, size=%d, DecodeError, (%s)", - nb_packets, pi, gfps, srs_human_flv_tag_type2string(type), timestamp, ndiff, diff, size, sbytes - ); + if ((ret = srs_utils_parse_timestamp(timestamp, type, data, size, &pts)) != ERROR_SUCCESS) { + snprintf(buffer, nb_buffer, "Rtmp packet id=%"PRId64"/%.1f/%.1f, type=%s, dts=%d, ndiff=%d, diff=%d, size=%d, DecodeError, (%s), ret=%d", + nb_packets, pi, gfps, srs_human_flv_tag_type2string(type), timestamp, ndiff, diff, size, sbytes, ret); return ret; } if (type == SRS_RTMP_TYPE_VIDEO) { - srs_human_trace("Video packet id=%"PRId64"/%.1f/%.1f, type=%s, dts=%d, pts=%d, ndiff=%d, diff=%d, size=%d, %s(%s,%s), (%s)", + snprintf(buffer, nb_buffer, "Video packet id=%"PRId64"/%.1f/%.1f, type=%s, dts=%d, pts=%d, ndiff=%d, diff=%d, size=%d, %s(%s,%s), (%s)", nb_packets, pi, gfps, srs_human_flv_tag_type2string(type), timestamp, pts, ndiff, diff, 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)), - sbytes - ); + sbytes); } else if (type == SRS_RTMP_TYPE_AUDIO) { - srs_human_trace("Audio packet id=%"PRId64"/%.1f/%.1f, type=%s, dts=%d, pts=%d, ndiff=%d, diff=%d, size=%d, %s(%s,%s,%s,%s), (%s)", + snprintf(buffer, nb_buffer, "Audio packet id=%"PRId64"/%.1f/%.1f, type=%s, dts=%d, pts=%d, ndiff=%d, diff=%d, size=%d, %s(%s,%s,%s,%s), (%s)", nb_packets, pi, gfps, srs_human_flv_tag_type2string(type), timestamp, pts, ndiff, diff, 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)), srs_human_flv_audio_sound_type2string(srs_utils_flv_audio_sound_type(data, size)), srs_human_flv_audio_aac_packet_type2string(srs_utils_flv_audio_aac_packet_type(data, size)), - sbytes - ); + sbytes); } else if (type == SRS_RTMP_TYPE_SCRIPT) { - srs_human_trace("Data packet id=%"PRId64"/%.1f/%.1f, type=%s, time=%d, ndiff=%d, diff=%d, size=%d, (%s)", + int nb = snprintf(buffer, nb_buffer, "Data packet id=%"PRId64"/%.1f/%.1f, type=%s, time=%d, ndiff=%d, diff=%d, size=%d, (%s)", nb_packets, pi, gfps, srs_human_flv_tag_type2string(type), timestamp, ndiff, diff, size, sbytes); int nparsed = 0; while (nparsed < size) { @@ -2622,15 +2619,16 @@ int srs_human_print_rtmp_packet4(char type, uint32_t timestamp, char* data, int if (amf0 == NULL) { break; } - + nparsed += nb_parsed_this; char* amf0_str = NULL; - srs_human_raw("%s", srs_human_amf0_print(amf0, &amf0_str, NULL)); + nb += snprintf(buffer + nb, nb_buffer - nb, "\n%s", srs_human_amf0_print(amf0, &amf0_str, NULL)) - 1; srs_freepa(amf0_str); } + buffer[nb] = 0; } else { - srs_human_trace("Rtmp packet id=%"PRId64"/%.1f/%.1f, type=%#x, dts=%d, pts=%d, ndiff=%d, diff=%d, size=%d, (%s)", + snprintf(buffer, nb_buffer, "Rtmp packet id=%"PRId64"/%.1f/%.1f, type=%#x, dts=%d, pts=%d, ndiff=%d, diff=%d, size=%d, (%s)", nb_packets, pi, gfps, type, timestamp, pts, ndiff, diff, size, sbytes); } @@ -2735,6 +2733,29 @@ int srs_rtmp_connect_app2(srs_rtmp_t rtmp, return ret; } + +int srs_human_print_rtmp_packet(char type, uint32_t timestamp, char* data, int size) +{ + return srs_human_print_rtmp_packet2(type, timestamp, data, size, 0); +} + +int srs_human_print_rtmp_packet2(char type, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp) +{ + return srs_human_print_rtmp_packet3(type, timestamp, data, size, pre_timestamp, 0); +} + +int srs_human_print_rtmp_packet3(char type, uint32_t timestamp, char* data, int size, uint32_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, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp, int64_t pre_now, int64_t starttime, int64_t nb_packets) +{ + char buffer[1024]; + int ret = srs_human_format_rtmp_packet2(buffer, sizeof(buffer), type, timestamp, data, size, pre_timestamp, pre_now, starttime, nb_packets); + srs_human_trace("%s", buffer); + return ret; +} #ifdef __cplusplus } diff --git a/trunk/src/libs/srs_librtmp.hpp b/trunk/src/libs/srs_librtmp.hpp index 6c12634dd..16c698570 100644 --- a/trunk/src/libs/srs_librtmp.hpp +++ b/trunk/src/libs/srs_librtmp.hpp @@ -1020,28 +1020,19 @@ extern const char* srs_human_flv_audio_sound_type2string(char sound_type); * it's static shared const string. */ extern const char* srs_human_flv_audio_aac_packet_type2string(char aac_packet_type); + +/** + * Format the RTMP packet to human readable buffer. + * @return Whether parse RTMP packet ok. 0, success; otherwise, failed. + */ +extern int srs_human_format_rtmp_packet(char* buffer, int nb_buffer, char type, uint32_t timestamp, char* data, int size); +extern int srs_human_format_rtmp_packet2(char* buffer, int nb_buffer, char type, uint32_t timestamp, char* data, int size, + uint32_t pre_timestamp, int64_t pre_now, int64_t starttime, int64_t nb_packets); /** -* print the rtmp packet, use srs_human_trace/srs_human_verbose for packet, -* and use srs_human_raw for script data body. -* @return an error code for parse the timetstamp to dts and pts. + * Format current time to human readable string. + * @return A NULL terminated string. */ -extern int srs_human_print_rtmp_packet(char type, uint32_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, uint32_t timestamp, char* data, int size, uint32_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, uint32_t timestamp, char* data, int size, uint32_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, uint32_t timestamp, char* data, int size, uint32_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(); #ifndef _WIN32 @@ -1062,14 +1053,14 @@ extern const char* srs_human_format_time(); #define srs_human_raw(msg, ...) (void)0 #else #define srs_human_trace(msg, ...) \ - fprintf(stdout, "[Trace][%d][%s][%d] ", getpid(), srs_human_format_time(), getpid());\ + fprintf(stdout, "[T][%d][%s] ", getpid(), srs_human_format_time());\ fprintf(stdout, msg, ##__VA_ARGS__); fprintf(stdout, "\n") #define srs_human_warn(msg, ...) \ - fprintf(stdout, "[Warn][%d][%s][%d] ", getpid(), srs_human_format_time(), getpid()); \ + fprintf(stdout, "[W][%d][%s] ", getpid(), srs_human_format_time()); \ fprintf(stdout, msg, ##__VA_ARGS__); \ fprintf(stdout, "\n") #define srs_human_error(msg, ...) \ - fprintf(stderr, "[Error][%d][%s][%d] ", getpid(), srs_human_format_time(), getpid());\ + fprintf(stderr, "[E][%d][%s] ", getpid(), srs_human_format_time());\ fprintf(stderr, msg, ##__VA_ARGS__); \ fprintf(stderr, "\n") #define srs_human_verbose(msg, ...) (void)0 @@ -1234,12 +1225,12 @@ typedef void* srs_hijack_io_t; ************************************************************* *************************************************************/ /** - * @Deprecated For bandwidth test check only. + * Deprecated, for bandwidth test check only. */ extern srs_rtmp_t srs_rtmp_create2(const char* url); /** - * @Deprecated Use seperate function to retrieve information from rtmp, + * Deprecated, use seperate function to retrieve information from rtmp, * for example, use srs_rtmp_get_server_ip to get server ip. */ extern int srs_rtmp_connect_app2(srs_rtmp_t rtmp, @@ -1247,6 +1238,15 @@ extern int srs_rtmp_connect_app2(srs_rtmp_t rtmp, char srs_primary[128], char srs_authors[128], char srs_version[32], int* srs_id, int* srs_pid ); + +/** + * Deprecated, use srs_human_format_rtmp_packet instead. + */ +extern int srs_human_print_rtmp_packet(char type, uint32_t timestamp, char* data, int size); +extern int srs_human_print_rtmp_packet2(char type, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp); +extern int srs_human_print_rtmp_packet3(char type, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp, int64_t pre_now); +extern int srs_human_print_rtmp_packet4(char type, uint32_t timestamp, char* data, int size, uint32_t pre_timestamp, int64_t pre_now, + int64_t starttime, int64_t nb_packets); #ifdef __cplusplus }