From 68a5407b3a8b57d449f63e88a69f0b426799420a Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 28 Feb 2017 20:35:56 +0800 Subject: [PATCH] Remove using of connect_app2. --- trunk/research/librtmp/srs_bandwidth_check.c | 120 ++++---- trunk/research/librtmp/srs_ingest_rtmp.c | 76 ++--- trunk/research/librtmp/srs_rtmp_dump.c | 303 ++++++++++--------- 3 files changed, 263 insertions(+), 236 deletions(-) diff --git a/trunk/research/librtmp/srs_bandwidth_check.c b/trunk/research/librtmp/srs_bandwidth_check.c index 8432b6c1b..016fea7c0 100644 --- a/trunk/research/librtmp/srs_bandwidth_check.c +++ b/trunk/research/librtmp/srs_bandwidth_check.c @@ -26,36 +26,67 @@ #include "../../objs/include/srs_librtmp.h" +// srs debug info. +char* ip = NULL; +char* sig = NULL; +int pid = 0, cid = 0; +int major = 0, minor = 0, revision= 0, build = 0; +// bandwidth test data. +int64_t start_time = 0; +int64_t end_time = 0; +int play_kbps = 0; +int publish_kbps = 0; +int play_bytes = 0; +int publish_bytes = 0; +int play_duration = 0; +int publish_duration = 0; + +int do_check(srs_rtmp_t rtmp) +{ + int ret = 0; + + if ((ret = srs_rtmp_handshake(rtmp)) != 0) { + srs_human_trace("simple handshake failed."); + return ret; + } + srs_human_trace("simple handshake success"); + + if ((ret = srs_rtmp_connect_app(rtmp)) != 0) { + srs_human_trace("connect vhost/app failed."); + return ret; + } + srs_human_trace("connect vhost/app success"); + + if ((ret = srs_rtmp_get_server_sig(rtmp, &sig)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_id(rtmp, &ip, &pid, &cid)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_version(rtmp, &major, &minor, &revision, &build)) != 0) { + srs_human_trace("Retrieve server version failed, ret=%d", ret); + return ret; + } + + if ((ret = srs_rtmp_bandwidth_check(rtmp, + &start_time, &end_time, &play_kbps, &publish_kbps, + &play_bytes, &publish_bytes, &play_duration, &publish_duration)) != 0 + ) { + srs_human_trace("bandwidth check/test failed."); + return ret; + } + srs_human_trace("bandwidth check/test success"); + + return ret; +} + int main(int argc, char** argv) { int ret = 0; srs_rtmp_t rtmp; - // srs debug info. - char srs_server_ip[128]; - char srs_server[128]; - char srs_primary[128]; - char srs_authors[128]; - char srs_version[32]; - int srs_id = 0; - int srs_pid = 0; - // bandwidth test data. - int64_t start_time = 0; - int64_t end_time = 0; - int play_kbps = 0; - int publish_kbps = 0; - int play_bytes = 0; - int publish_bytes = 0; - int play_duration = 0; - int publish_duration = 0; - - // set to zero. - srs_server_ip[0] = 0; - srs_server[0] = 0; - srs_primary[0] = 0; - srs_authors[0] = 0; - srs_version[0] = 0; - printf("RTMP bandwidth check/test with server.\n"); printf("srs(ossrs) client librtmp library.\n"); printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); @@ -75,52 +106,30 @@ int main(int argc, char** argv) char url[512]; snprintf(url, sizeof(url), "%s/%s", argv[1], "livestream"); rtmp = srs_rtmp_create((const char*)url); - srs_human_trace("bandwidth check/test url: %s", argv[1]); - if ((ret = srs_rtmp_handshake(rtmp)) != 0) { - srs_human_trace("simple handshake failed."); + if ((ret = do_check(rtmp)) != 0) { goto rtmp_destroy; } - srs_human_trace("simple handshake success"); - - if ((ret = srs_rtmp_connect_app2(rtmp, - srs_server_ip, srs_server, srs_primary, srs_authors, srs_version, - &srs_id, &srs_pid)) != 0) { - srs_human_trace("connect vhost/app failed."); - goto rtmp_destroy; - } - srs_human_trace("connect vhost/app success"); - - if ((ret = srs_rtmp_bandwidth_check(rtmp, - &start_time, &end_time, &play_kbps, &publish_kbps, - &play_bytes, &publish_bytes, &play_duration, &publish_duration)) != 0 - ) { - srs_human_trace("bandwidth check/test failed."); - goto rtmp_destroy; - } - srs_human_trace("bandwidth check/test success"); srs_human_trace("\n%s, %s, %s\n" - "%s, %s, srs_pid=%d, srs_id=%d\n" + "%s, %d.%d.%d.%d, srs_pid=%d, srs_id=%d\n" "duration: %dms(%d+%d)\n" "play: %dkbps\n" - "publish: %dkbps", - (char*)srs_server, (char*)srs_primary, (char*)srs_authors, - (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id, + "publish: %dkbps", + (char*)sig, NULL, NULL, + (char*)ip, major, minor, revision, build, pid, cid, (int)(end_time - start_time), play_duration, publish_duration, play_kbps, publish_kbps); rtmp_destroy: - srs_rtmp_destroy(rtmp); - fprintf(stderr, "{\"code\":%d," "\"srs_server\":\"%s\", " "\"srs_primary\":\"%s\", " "\"srs_authors\":\"%s\", " "\"srs_server_ip\":\"%s\", " - "\"srs_version\":\"%s\", " + "\"srs_version\":\"%d.%d.%d.%d\", " "\"srs_pid\":%d, " "\"srs_id\":%d, " "\"duration\":%d, " @@ -130,10 +139,11 @@ rtmp_destroy: "\"publish_kbps\":%d" "}", ret, - (char*)srs_server, (char*)srs_primary, (char*)srs_authors, - (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id, + (char*)sig, NULL, NULL, + (char*)ip, major, minor, revision, build, pid, cid, (int)(end_time - start_time), play_duration, publish_duration, play_kbps, publish_kbps); + srs_rtmp_destroy(rtmp); srs_human_trace(" "); srs_human_trace("completed"); diff --git a/trunk/research/librtmp/srs_ingest_rtmp.c b/trunk/research/librtmp/srs_ingest_rtmp.c index d374d709c..995922968 100644 --- a/trunk/research/librtmp/srs_ingest_rtmp.c +++ b/trunk/research/librtmp/srs_ingest_rtmp.c @@ -174,20 +174,10 @@ int connect_ic(srs_rtmp_t irtmp) int ret = 0; // srs debug info. - char srs_server_ip[128]; - char srs_server[128]; - char srs_primary[128]; - char srs_authors[128]; - char srs_version[32]; - int srs_id = 0; - int srs_pid = 0; - - // set to zero. - srs_server_ip[0] = 0; - srs_server[0] = 0; - srs_primary[0] = 0; - srs_authors[0] = 0; - srs_version[0] = 0; + char* ip = NULL; + char* sig = NULL; + int pid = 0, cid = 0; + int major = 0, minor = 0, revision= 0, build = 0; if ((ret = srs_rtmp_handshake(irtmp)) != 0) { srs_human_trace("irtmp simple handshake failed. ret=%d", ret); @@ -199,13 +189,25 @@ int connect_ic(srs_rtmp_t irtmp) srs_human_verbose("irtmp simple handshake success"); } - if (srs_rtmp_connect_app2(irtmp, - srs_server_ip, srs_server, srs_primary, srs_authors, srs_version, - &srs_id, &srs_pid) != 0) { + if (srs_rtmp_connect_app(irtmp) != 0) { srs_human_trace("irtmp connect vhost/app failed. ret=%d", ret); return ret; } - srs_human_trace("irtmp connect ok, ip=%s, server=%s/%s, pid=%d, cid=%d", srs_server_ip, srs_server, srs_version, srs_pid, srs_id); + + if ((ret = srs_rtmp_get_server_sig(irtmp, &sig)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_id(irtmp, &ip, &pid, &cid)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_version(irtmp, &major, &minor, &revision, &build)) != 0) { + srs_human_trace("Retrieve server version failed, ret=%d", ret); + return ret; + } + srs_human_trace("irtmp connect ok, ip=%s, server=%s/%d.%d.%d.%d, pid=%d, cid=%d", + ip, sig, major, minor, revision, build, pid, cid); if ((ret = srs_rtmp_play_stream(irtmp)) != 0) { srs_human_trace("irtmp play stream failed. ret=%d", ret); @@ -225,20 +227,10 @@ int connect_oc(srs_rtmp_t ortmp) int ret = 0; // srs debug info. - char srs_server_ip[128]; - char srs_server[128]; - char srs_primary[128]; - char srs_authors[128]; - char srs_version[32]; - int srs_id = 0; - int srs_pid = 0; - - // set to zero. - srs_server_ip[0] = 0; - srs_server[0] = 0; - srs_primary[0] = 0; - srs_authors[0] = 0; - srs_version[0] = 0; + char* ip = NULL; + char* sig = NULL; + int pid = 0, cid = 0; + int major = 0, minor = 0, revision= 0, build = 0; if ((ret = srs_rtmp_handshake(ortmp)) != 0) { srs_human_trace("ortmp simple handshake failed. ret=%d", ret); @@ -250,13 +242,25 @@ int connect_oc(srs_rtmp_t ortmp) srs_human_verbose("ortmp simple handshake success"); } - if (srs_rtmp_connect_app2(ortmp, - srs_server_ip, srs_server, srs_primary, srs_authors, srs_version, - &srs_id, &srs_pid) != 0) { + if (srs_rtmp_connect_app(ortmp) != 0) { srs_human_trace("ortmp connect vhost/app failed. ret=%d", ret); return ret; } - srs_human_trace("ortmp connect ok, ip=%s, server=%s/%s, pid=%d, cid=%d", srs_server_ip, srs_server, srs_version, srs_pid, srs_id); + + if ((ret = srs_rtmp_get_server_sig(ortmp, &sig)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_id(ortmp, &ip, &pid, &cid)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_version(ortmp, &major, &minor, &revision, &build)) != 0) { + srs_human_trace("Retrieve server version failed, ret=%d", ret); + return ret; + } + srs_human_trace("ortmp connect ok, ip=%s, server=%s/%d.%d.%d.%d, pid=%d, cid=%d", + ip, sig, major, minor, revision, build, pid, cid); if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) { srs_human_trace("ortmp publish stream failed. ret=%d", ret); diff --git a/trunk/research/librtmp/srs_rtmp_dump.c b/trunk/research/librtmp/srs_rtmp_dump.c index f3c7f3e29..bc8eb603e 100644 --- a/trunk/research/librtmp/srs_rtmp_dump.c +++ b/trunk/research/librtmp/srs_rtmp_dump.c @@ -75,25 +75,170 @@ void parse_amf0_object(char* p, srs_amf0_t args) } } +// srs debug info. +char* ip = NULL; +char* sig = NULL; +int pid = 0, cid = 0; +int major = 0, minor = 0, revision= 0, build = 0; +// User options. +int complex_handshake = 0; +const char* rtmp_url = NULL; +const char* output_flv = NULL; +const char* swfUrl = NULL; +const char* tcUrl = NULL; +const char* pageUrl = NULL; +srs_amf0_t args = NULL; + +int do_proxy(srs_rtmp_t rtmp, srs_flv_t flv) +{ + int ret = 0; + + if ((ret = srs_rtmp_dns_resolve(rtmp)) != 0) { + srs_human_trace("dns resolve failed, ret=%d", ret); + return ret; + } + + if ((ret = srs_rtmp_connect_server(rtmp)) != 0) { + srs_human_trace("connect to server failed, ret=%d", ret); + return ret; + } + + if (complex_handshake) { + if ((ret = srs_rtmp_do_complex_handshake(rtmp)) != 0) { + srs_human_trace("complex handshake failed, ret=%d", ret); + return ret; + } + srs_human_trace("do complex handshake success"); + } else { + if ((ret = srs_rtmp_do_simple_handshake(rtmp)) != 0) { + srs_human_trace("simple handshake failed, ret=%d", ret); + return ret; + } + srs_human_trace("do simple handshake success"); + } + + if ((ret = srs_rtmp_set_connect_args(rtmp, tcUrl, swfUrl, pageUrl, args)) != 0) { + srs_human_trace("set connect args failed, ret=%d", ret); + return ret; + } + + if ((ret = srs_rtmp_connect_app(rtmp)) != 0) { + srs_human_trace("connect vhost/app failed, ret=%d", ret); + return ret; + } + + if ((ret = srs_rtmp_get_server_sig(rtmp, &sig)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_id(rtmp, &ip, &pid, &cid)) != 0) { + srs_human_trace("Retrieve server ID failed, ret=%d", ret); + return ret; + } + if ((ret = srs_rtmp_get_server_version(rtmp, &major, &minor, &revision, &build)) != 0) { + srs_human_trace("Retrieve server version failed, ret=%d", ret); + return ret; + } + srs_human_trace("connect ok, ip=%s, server=%s/%d.%d.%d.%d, pid=%d, cid=%d", + ip, sig, major, minor, revision, build, pid, cid); + + if ((ret = srs_rtmp_play_stream(rtmp)) != 0) { + srs_human_trace("play stream failed, ret=%d", ret); + return ret; + } + srs_human_trace("play stream success"); + + if (flv) { + // flv header + char header[9]; + // 3bytes, signature, "FLV", + header[0] = 'F'; + header[1] = 'L'; + header[2] = 'V'; + // 1bytes, version, 0x01, + header[3] = 0x01; + // 1bytes, flags, UB[5] 0, UB[1] audio present, UB[1] 0, UB[1] video present. + header[4] = 0x03; // audio + video. + // 4bytes, dataoffset + header[5] = 0x00; + header[6] = 0x00; + header[7] = 0x00; + header[8] = 0x09; + if ((ret = srs_flv_write_header(flv, header)) != 0) { + srs_human_trace("write flv header failed, ret=%d", ret); + return ret; + } + } + + int64_t nb_packets = 0; + uint32_t pre_timestamp = 0; + int64_t pre_now = -1; + int64_t start_time = -1; + for (;;) { + int size; + char type; + char* data; + uint32_t timestamp; + + if ((ret = srs_rtmp_read_packet(rtmp, &type, ×tamp, &data, &size)) != 0) { + srs_human_trace("read rtmp packet failed, ret=%d", ret); + return ret; + } + + if (pre_now == -1) { + pre_now = srs_utils_time_ms(); + } + if (start_time == -1) { + 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) { + srs_human_trace("print rtmp packet failed, ret=%d", ret); + return ret; + } + pre_timestamp = timestamp; + pre_now = srs_utils_time_ms(); + + // we only write some types of messages to flv file. + int is_flv_msg = type == SRS_RTMP_TYPE_AUDIO + || type == SRS_RTMP_TYPE_VIDEO || type == SRS_RTMP_TYPE_SCRIPT; + + // for script data, ignore except onMetaData + if (type == SRS_RTMP_TYPE_SCRIPT) { + if (!srs_rtmp_is_onMetaData(type, data, size)) { + is_flv_msg = 0; + } + } + + if (flv) { + if (is_flv_msg) { + if ((ret = srs_flv_write_tag(flv, type, timestamp, data, size)) != 0) { + srs_human_trace("dump rtmp packet failed, ret=%d", ret); + return ret; + } + } else { + srs_human_trace("drop message type=%#x, size=%dB", type, size); + } + } + + free(data); + } + + return ret; +} + int main(int argc, char** argv) { srs_flv_t flv = NULL; srs_rtmp_t rtmp = NULL; - // srs debug info. - char srs_server_ip[128]; - char srs_server[128]; - char srs_primary[128]; - char srs_authors[128]; - char srs_version[32]; - int srs_id = 0; - int srs_pid = 0; - printf("dump rtmp stream to flv file\n"); printf("srs(ossrs) client librtmp library.\n"); printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); printf("@refer to http://rtmpdump.mplayerhq.hu/rtmpdump.1.html\n"); + int show_help = 0; + const char* short_options = "hxr:o:s:t:p:C:"; struct option long_options[] = { {"rtmp", required_argument, 0, 'r'}, {"flv", required_argument, 0, 'o'}, @@ -106,25 +251,9 @@ int main(int argc, char** argv) {0, 0, 0, 0} }; - int show_help = 0; - int complex_handshake = 0; - const char* rtmp_url = NULL; - const char* output_flv = NULL; - const char* swfUrl = NULL; - const char* tcUrl = NULL; - const char* pageUrl = NULL; - srs_amf0_t args = NULL; - - // set to zero. - srs_server_ip[0] = 0; - srs_server[0] = 0; - srs_primary[0] = 0; - srs_authors[0] = 0; - srs_version[0] = 0; - int opt = 0; int option_index = 0; - while((opt = getopt_long(argc, argv, "hxr:o:s:t:p:C:", long_options, &option_index)) != -1){ + while((opt = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1){ switch(opt){ case 'r': rtmp_url = optarg; @@ -201,131 +330,15 @@ int main(int argc, char** argv) } rtmp = srs_rtmp_create(rtmp_url); - - if (srs_rtmp_dns_resolve(rtmp) != 0) { - srs_human_trace("dns resolve failed."); - goto rtmp_destroy; - } - - if (srs_rtmp_connect_server(rtmp) != 0) { - srs_human_trace("connect to server failed."); - goto rtmp_destroy; - } - - if (complex_handshake) { - if (srs_rtmp_do_complex_handshake(rtmp) != 0) { - srs_human_trace("complex handshake failed."); - goto rtmp_destroy; - } - srs_human_trace("do complex handshake success"); - } else { - if (srs_rtmp_do_simple_handshake(rtmp) != 0) { - srs_human_trace("simple handshake failed."); - goto rtmp_destroy; - } - srs_human_trace("do simple handshake success"); - } - - if (srs_rtmp_set_connect_args(rtmp, tcUrl, swfUrl, pageUrl, args) != 0) { - srs_human_trace("set connect args failed."); - goto rtmp_destroy; - } - - if (srs_rtmp_connect_app2(rtmp, - srs_server_ip, srs_server, srs_primary, srs_authors, srs_version, - &srs_id, &srs_pid) != 0) { - srs_human_trace("connect vhost/app failed."); - goto rtmp_destroy; - } - srs_human_trace("connect ok, ip=%s, server=%s/%s, pid=%d, cid=%d", srs_server_ip, srs_server, srs_version, srs_pid, srs_id); - - if (srs_rtmp_play_stream(rtmp) != 0) { - srs_human_trace("play stream failed."); - goto rtmp_destroy; - } - srs_human_trace("play stream success"); - if (output_flv) { flv = srs_flv_open_write(output_flv); } - if (flv) { - // flv header - char header[9]; - // 3bytes, signature, "FLV", - header[0] = 'F'; - header[1] = 'L'; - header[2] = 'V'; - // 1bytes, version, 0x01, - header[3] = 0x01; - // 1bytes, flags, UB[5] 0, UB[1] audio present, UB[1] 0, UB[1] video present. - header[4] = 0x03; // audio + video. - // 4bytes, dataoffset - header[5] = 0x00; - header[6] = 0x00; - header[7] = 0x00; - header[8] = 0x09; - if (srs_flv_write_header(flv, header) != 0) { - srs_human_trace("write flv header failed."); - goto rtmp_destroy; - } + int ret = 0; + if ((ret = do_proxy(rtmp, flv)) != 0) { + srs_human_trace("Dump RTMP failed, ret=%d", ret); } - int64_t nb_packets = 0; - uint32_t pre_timestamp = 0; - int64_t pre_now = -1; - int64_t start_time = -1; - for (;;) { - int size; - char type; - char* data; - uint32_t timestamp; - - if (srs_rtmp_read_packet(rtmp, &type, ×tamp, &data, &size) != 0) { - srs_human_trace("read rtmp packet failed."); - goto rtmp_destroy; - } - - if (pre_now == -1) { - pre_now = srs_utils_time_ms(); - } - if (start_time == -1) { - start_time = srs_utils_time_ms(); - } - - 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; - } - pre_timestamp = timestamp; - pre_now = srs_utils_time_ms(); - - // we only write some types of messages to flv file. - int is_flv_msg = type == SRS_RTMP_TYPE_AUDIO - || type == SRS_RTMP_TYPE_VIDEO || type == SRS_RTMP_TYPE_SCRIPT; - - // for script data, ignore except onMetaData - if (type == SRS_RTMP_TYPE_SCRIPT) { - if (!srs_rtmp_is_onMetaData(type, data, size)) { - is_flv_msg = 0; - } - } - - if (flv) { - if (is_flv_msg) { - if (srs_flv_write_tag(flv, type, timestamp, data, size) != 0) { - srs_human_trace("dump rtmp packet failed."); - goto rtmp_destroy; - } - } else { - srs_human_trace("drop message type=%#x, size=%dB", type, size); - } - } - - free(data); - } - -rtmp_destroy: srs_rtmp_destroy(rtmp); srs_flv_close(flv); srs_human_trace("completed");