From cf5c24af4ac55c312742eb708b3f14e97c7fb0a3 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 8 Nov 2014 14:00:59 +0800 Subject: [PATCH] refine librtmp, unify all tools format and usage. --- trunk/research/librtmp/srs_detect_rtmp.c | 56 +++++++++++------------ trunk/research/librtmp/srs_flv_injecter.c | 14 +++--- trunk/research/librtmp/srs_flv_parser.c | 15 +++--- trunk/research/librtmp/srs_ingest_flv.c | 16 +++---- trunk/research/librtmp/srs_ingest_rtmp.c | 11 ++--- trunk/research/librtmp/srs_play.c | 26 ++++------- trunk/research/librtmp/srs_publish.c | 50 +++++++++++--------- trunk/src/libs/srs_librtmp.hpp | 6 +-- 8 files changed, 96 insertions(+), 98 deletions(-) diff --git a/trunk/research/librtmp/srs_detect_rtmp.c b/trunk/research/librtmp/srs_detect_rtmp.c index 0ebcb82d3..cfe235a31 100644 --- a/trunk/research/librtmp/srs_detect_rtmp.c +++ b/trunk/research/librtmp/srs_detect_rtmp.c @@ -58,9 +58,13 @@ int main(int argc, char** argv) const char* rtmp_url = NULL; int duration = 0; int timeout = 0; + + printf("detect rtmp stream\n"); + printf("srs(simple-rtmp-server) client librtmp library.\n"); + printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); if (argc <= 3) { - printf("detect stream on RTMP server\n" + printf("detect stream on RTMP server, print result to stderr.\n" "Usage: %s \n" " rtmp_url RTMP stream url to play\n" " duration how long to play, in seconds, stream time.\n" @@ -68,69 +72,63 @@ int main(int argc, char** argv) "For example:\n" " %s rtmp://127.0.0.1:1935/live/livestream 3 10\n", argv[0], argv[0]); - ret = 1; - exit(ret); - return ret; + exit(-1); } rtmp_url = argv[1]; duration = atoi(argv[2]); timeout = atoi(argv[3]); - printf("detect rtmp stream\n"); - printf("srs(simple-rtmp-server) client librtmp library.\n"); - printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); - printf("rtmp url: %s\n", rtmp_url); - printf("duration: %ds, timeout:%ds\n", duration, timeout); + srs_trace("rtmp url: %s", rtmp_url); + srs_trace("duration: %ds, timeout:%ds", duration, timeout); if (duration <= 0 || timeout <= 0) { - ret = 1; - fprintf(stderr, "duration and timeout must be positive. ret=%d\n", ret); - exit(ret); - return ret; + srs_trace("duration and timeout must be positive."); + exit(-1); } rtmp = srs_rtmp_create(rtmp_url); if ((ret = __srs_dns_resolve(rtmp)) != 0) { - fprintf(stderr, "dns resolve failed. ret=%d\n", ret); + srs_trace("dns resolve failed. ret=%d", ret); goto rtmp_destroy; } - printf("dns resolve success\n"); + srs_trace("dns resolve success"); time_dns_resolve = srs_get_time_ms(); if ((ret = __srs_connect_server(rtmp)) != 0) { - fprintf(stderr, "socket connect failed. ret=%d\n", ret); + srs_trace("socket connect failed. ret=%d", ret); goto rtmp_destroy; } - printf("socket connect success\n"); + srs_trace("socket connect success"); time_socket_connect = srs_get_time_ms(); if ((ret = __srs_do_simple_handshake(rtmp)) != 0) { - fprintf(stderr, "do simple handshake failed. ret=%d\n", ret); + srs_trace("do simple handshake failed. ret=%d", ret); goto rtmp_destroy; } - printf("do simple handshake success\n"); + srs_trace("do simple handshake success"); if ((ret = srs_connect_app(rtmp)) != 0) { - fprintf(stderr, "connect vhost/app failed. ret=%d\n", ret); + srs_trace("connect vhost/app failed. ret=%d", ret); goto rtmp_destroy; } - printf("connect vhost/app success\n"); + srs_trace("connect vhost/app success"); if ((ret = srs_play_stream(rtmp)) != 0) { - fprintf(stderr, "play stream failed. ret=%d\n", ret); + srs_trace("play stream failed. ret=%d", ret); goto rtmp_destroy; } - printf("play stream success\n"); + srs_trace("play stream success"); time_play_stream = srs_get_time_ms(); for (;;) { if ((ret = srs_read_packet(rtmp, &type, ×tamp, &data, &size)) != 0) { - fprintf(stderr, "read packet failed. ret=%d\n", ret); + srs_trace("read packet failed. ret=%d", ret); goto rtmp_destroy; } - printf("got packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size); + srs_trace("got packet: type=%s, time=%d, size=%d", + srs_type2string(type), timestamp, size); if (SRS_RTMP_TYPE_VIDEO == type || SRS_RTMP_TYPE_AUDIO == type) { if (time_first_packet <= 0) { @@ -144,12 +142,12 @@ int main(int argc, char** argv) free(data); if (srs_get_time_ms() - time_startup > timeout * 1000) { - printf("timeout, terminate.\n"); + srs_trace("timeout, terminate."); goto rtmp_destroy; } if ((timestamp - basetime) > duration * 1000) { - printf("duration exceed, terminate.\n"); + srs_trace("duration exceed, terminate."); goto rtmp_destroy; } } @@ -197,7 +195,9 @@ rtmp_destroy: "\"remark1\": \"delay = stream - (time_cleanup - time_first_packet)\"", "\"remark2\": \"if code is not 0, user must ignore all data\"" ); - printf("\n"); + + srs_trace(""); + srs_trace("completed"); return ret; } diff --git a/trunk/research/librtmp/srs_flv_injecter.c b/trunk/research/librtmp/srs_flv_injecter.c index 5b6d866c3..e053ee047 100644 --- a/trunk/research/librtmp/srs_flv_injecter.c +++ b/trunk/research/librtmp/srs_flv_injecter.c @@ -52,6 +52,10 @@ int main(int argc, char** argv) // temp variables. int tmp_file_size = 0; char* tmp_file; + + printf("inject flv file keyframes to metadata.\n"); + printf("srs(simple-rtmp-server) client librtmp library.\n"); + printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); if (argc <= 2) { printf("inject flv file keyframes to metadata\n" @@ -59,11 +63,10 @@ int main(int argc, char** argv) " in_flv_file input flv file to inject.\n" " out_flv_file the inject output file, can be in_flv_file.\n" "For example:\n" + " %s doc/source.200kbps.768x320.flv injected.flv\n" " %s ../../doc/source.200kbps.768x320.flv injected.flv\n", - argv[0], argv[0]); - ret = 1; - exit(ret); - return ret; + argv[0], argv[0], argv[0]); + exit(-1); } in_flv_file = argv[1]; @@ -73,9 +76,6 @@ int main(int argc, char** argv) tmp_file = (char*)malloc(tmp_file_size); snprintf(tmp_file, tmp_file_size, "%s.tmp", out_flv_file); - srs_trace("inject flv file keyframes to metadata."); - srs_trace("srs(simple-rtmp-server) client librtmp library."); - srs_trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); srs_trace("input: %s", in_flv_file); srs_trace("output: %s", out_flv_file); srs_trace("tmp_file: %s", tmp_file); diff --git a/trunk/research/librtmp/srs_flv_parser.c b/trunk/research/librtmp/srs_flv_parser.c index ce78e948a..abfca6a1b 100644 --- a/trunk/research/librtmp/srs_flv_parser.c +++ b/trunk/research/librtmp/srs_flv_parser.c @@ -45,23 +45,22 @@ int main(int argc, char** argv) // flv handler srs_flv_t flv; + printf("parse and show flv file detail.\n"); + printf("srs(simple-rtmp-server) client librtmp library.\n"); + printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); + if (argc <= 1) { printf("parse and show flv file detail\n" "Usage: %s in_flv_file\n" " in_flv_file flv file to parse and show.\n" "For example:\n" + " %s doc/source.200kbps.768x320.flv\n" " %s ../../doc/source.200kbps.768x320.flv\n", - argv[0], argv[0]); - ret = 1; - exit(ret); - return ret; + argv[0], argv[0], argv[0]); + exit(-1); } in_flv_file = argv[1]; - - srs_trace("parse and show flv file detail."); - srs_trace("srs(simple-rtmp-server) client librtmp library."); - srs_trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); srs_trace("input: %s", in_flv_file); if ((flv = srs_flv_open_read(in_flv_file)) == NULL) { diff --git a/trunk/research/librtmp/srs_ingest_flv.c b/trunk/research/librtmp/srs_ingest_flv.c index 6eca2bd05..669b960d2 100644 --- a/trunk/research/librtmp/srs_ingest_flv.c +++ b/trunk/research/librtmp/srs_ingest_flv.c @@ -59,17 +59,20 @@ int main(int argc, char** argv) // flv handler srs_flv_t flv; + printf("ingest flv file and publish to RTMP server like FFMPEG.\n"); + printf("srs(simple-rtmp-server) client librtmp library.\n"); + printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); + if (argc <= 2) { printf("ingest flv file and publish to RTMP server\n" "Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n" " in_flv_file input flv file, ingest from this file.\n" " out_rtmp_url output rtmp url, publish to this url.\n" "For example:\n" - " %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/demo\n", - argv[0], argv[0]); - ret = 1; - exit(ret); - return ret; + " %s -i doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/livestream\n" + " %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/livestream\n", + argv[0], argv[0], argv[0]); + exit(-1); } // parse options in FFMPEG format. @@ -86,9 +89,6 @@ int main(int argc, char** argv) } } - srs_trace("ingest flv file and publish to RTMP server like FFMPEG."); - srs_trace("srs(simple-rtmp-server) client librtmp library."); - srs_trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); srs_trace("input: %s", in_flv_file); srs_trace("output: %s", out_rtmp_url); diff --git a/trunk/research/librtmp/srs_ingest_rtmp.c b/trunk/research/librtmp/srs_ingest_rtmp.c index 9b2ccee93..1f3470480 100644 --- a/trunk/research/librtmp/srs_ingest_rtmp.c +++ b/trunk/research/librtmp/srs_ingest_rtmp.c @@ -45,6 +45,10 @@ int main(int argc, char** argv) // rtmp handler srs_rtmp_t irtmp, ortmp; + printf("ingest RTMP and publish to RTMP server like edge.\n"); + printf("srs(simple-rtmp-server) client librtmp library.\n"); + printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); + if (argc <= 2) { printf("ingest RTMP and publish to RTMP server\n" "Usage: %s <-i in_rtmp_url> <-y out_rtmp_url>\n" @@ -53,9 +57,7 @@ int main(int argc, char** argv) "For example:\n" " %s -i rtmp://127.0.0.1/live/livestream -y rtmp://127.0.0.1/live/demo\n", argv[0], argv[0]); - ret = 1; - exit(ret); - return ret; + exit(-1); } // parse options in FFMPEG format. @@ -72,9 +74,6 @@ int main(int argc, char** argv) } } - srs_trace("ingest RTMP and publish to RTMP server like edge."); - srs_trace("srs(simple-rtmp-server) client librtmp library."); - srs_trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); srs_trace("input: %s", in_rtmp_url); srs_trace("output: %s", out_rtmp_url); diff --git a/trunk/research/librtmp/srs_play.c b/trunk/research/librtmp/srs_play.c index 960523a3b..7bf431f58 100644 --- a/trunk/research/librtmp/srs_play.c +++ b/trunk/research/librtmp/srs_play.c @@ -31,31 +31,21 @@ gcc srs_play.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_play int main(int argc, char** argv) { - srs_rtmp_t rtmp; - - // packet data - int type, size; - u_int32_t timestamp = 0; - char* data; + printf("suck rtmp stream like rtmpdump\n"); + printf("srs(simple-rtmp-server) client librtmp library.\n"); + printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); if (argc <= 1) { - printf("play stream on RTMP server\n" - "Usage: %s \n" + printf("Usage: %s \n" " rtmp_url RTMP stream url to play\n" "For example:\n" " %s rtmp://127.0.0.1:1935/live/livestream\n", argv[0], argv[0]); - int ret = 1; - exit(ret); - return ret; + exit(-1); } - rtmp = srs_rtmp_create(argv[1]); - - srs_trace("suck rtmp stream like rtmpdump"); - srs_trace("srs(simple-rtmp-server) client librtmp library."); - srs_trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); srs_trace("rtmp url: %s", argv[1]); + srs_rtmp_t rtmp = srs_rtmp_create(argv[1]); if (srs_simple_handshake(rtmp) != 0) { srs_trace("simple handshake failed."); @@ -76,6 +66,10 @@ int main(int argc, char** argv) srs_trace("play stream success"); for (;;) { + int type, size; + u_int32_t timestamp = 0; + char* data; + if (srs_read_packet(rtmp, &type, ×tamp, &data, &size) != 0) { goto rtmp_destroy; } diff --git a/trunk/research/librtmp/srs_publish.c b/trunk/research/librtmp/srs_publish.c index 30c2af255..85e2d3d88 100644 --- a/trunk/research/librtmp/srs_publish.c +++ b/trunk/research/librtmp/srs_publish.c @@ -32,54 +32,60 @@ gcc srs_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_publish int main(int argc, char** argv) { - srs_rtmp_t rtmp; - - // packet data - int type, size; - u_int32_t timestamp = 0; - char* data; - printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\n"); printf("srs(simple-rtmp-server) client librtmp library.\n"); printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); + + if (argc <= 1) { + printf("Usage: %s \n" + " rtmp_url RTMP stream url to publish\n" + "For example:\n" + " %s rtmp://127.0.0.1:1935/live/livestream\n", + argv[0], argv[0]); + exit(-1); + } + // warn it . // @see: https://github.com/winlinvip/simple-rtmp-server/issues/126 - printf("\033[33m%s\033[0m", + srs_trace("\033[33m%s\033[0m", "[warning] it's only a sample to use librtmp. " "please never use it to publish and test forward/transcode/edge/HLS whatever. " - "you should refer to this tool to use the srs-librtmp to publish the real media stream."); - printf("\n"); - - rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream"); + "you should refer to this tool to use the srs-librtmp to publish the real media stream." + "read about: https://github.com/winlinvip/simple-rtmp-server/issues/126"); + srs_trace("rtmp url: %s", argv[1]); + srs_rtmp_t rtmp = srs_rtmp_create(argv[1]); if (srs_simple_handshake(rtmp) != 0) { - printf("simple handshake failed.\n"); + srs_trace("simple handshake failed."); goto rtmp_destroy; } - printf("simple handshake success\n"); + srs_trace("simple handshake success"); if (srs_connect_app(rtmp) != 0) { - printf("connect vhost/app failed.\n"); + srs_trace("connect vhost/app failed."); goto rtmp_destroy; } - printf("connect vhost/app success\n"); + srs_trace("connect vhost/app success"); if (srs_publish_stream(rtmp) != 0) { - printf("publish stream failed.\n"); + srs_trace("publish stream failed."); goto rtmp_destroy; } - printf("publish stream success\n"); + srs_trace("publish stream success"); + u_int32_t timestamp = 0; for (;;) { - type = SRS_RTMP_TYPE_VIDEO; + int type = SRS_RTMP_TYPE_VIDEO; + int size = 4096; + char* data = (char*)malloc(4096); + timestamp += 40; - size = 4096; - data = (char*)malloc(4096); if (srs_write_packet(rtmp, type, timestamp, data, size) != 0) { goto rtmp_destroy; } - printf("sent packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size); + srs_trace("sent packet: type=%s, time=%d, size=%d", + srs_type2string(type), timestamp, size); usleep(40 * 1000); } diff --git a/trunk/src/libs/srs_librtmp.hpp b/trunk/src/libs/srs_librtmp.hpp index 66b1e9087..9dbb82d39 100644 --- a/trunk/src/libs/srs_librtmp.hpp +++ b/trunk/src/libs/srs_librtmp.hpp @@ -237,8 +237,8 @@ extern int64_t srs_get_nrecv_bytes(srs_rtmp_t rtmp); // log to console, for use srs-librtmp application. extern const char* srs_format_time(); -#define srs_trace(msg, ...) printf("[%s]", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n") -#define srs_verbose(msg, ...) printf("[%s]", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n") +#define srs_trace(msg, ...) printf("[%s] ", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n") +#define srs_verbose(msg, ...) printf("[%s] ", srs_format_time());printf(msg, ##__VA_ARGS__);printf("\n") /************************************************************* ************************************************************** @@ -338,7 +338,7 @@ extern void srs_amf0_strict_array_append(srs_amf0_t amf0, srs_amf0_t value); /** * human readable print * @param pdata, output the heap data, NULL to ignore. -* user must use srs_amf0_free_bytes to free it. +* user must use srs_amf0_free_bytes to free it. * @return return the *pdata for print. NULL to ignore. */ extern char* srs_amf0_human_print(srs_amf0_t amf0, char** pdata, int* psize);