srs_detect_rtmp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013-2015 SRS(ossrs)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. /**
  20. gcc srs_detect_rtmp.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_detect_rtmp
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "../../objs/include/srs_librtmp.h"
  25. int main(int argc, char** argv)
  26. {
  27. int ret = 0;
  28. srs_rtmp_t rtmp;
  29. // time
  30. int64_t time_startup = srs_utils_time_ms();
  31. int64_t time_dns_resolve = 0;
  32. int64_t time_socket_connect = 0;
  33. int64_t time_play_stream = 0;
  34. int64_t time_first_packet = 0;
  35. int64_t time_cleanup = 0;
  36. // delay = actual - expect time when quit.
  37. int delay = 0;
  38. // bytes
  39. int64_t bytes_nsend = 0;
  40. int time_duration = 0;
  41. int64_t bytes_nrecv = 0;
  42. // packet data
  43. int size;
  44. char type;
  45. char* data;
  46. u_int32_t timestamp;
  47. u_int32_t basetime = 0;
  48. // user options
  49. const char* rtmp_url = NULL;
  50. int duration = 0;
  51. int timeout = 0;
  52. printf("detect rtmp stream\n");
  53. printf("srs(ossrs) client librtmp library.\n");
  54. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  55. if (argc <= 3) {
  56. printf("detect stream on RTMP server, print result to stderr.\n"
  57. "Usage: %s <rtmp_url> <duration> <timeout>\n"
  58. " rtmp_url RTMP stream url to play\n"
  59. " duration how long to play, in seconds, stream time.\n"
  60. " timeout how long to timeout, in seconds, system time.\n"
  61. "For example:\n"
  62. " %s rtmp://127.0.0.1:1935/live/livestream 3 10\n",
  63. argv[0], argv[0]);
  64. exit(-1);
  65. }
  66. rtmp_url = argv[1];
  67. duration = atoi(argv[2]);
  68. timeout = atoi(argv[3]);
  69. srs_human_trace("rtmp url: %s", rtmp_url);
  70. srs_human_trace("duration: %ds, timeout:%ds", duration, timeout);
  71. if (duration <= 0 || timeout <= 0) {
  72. srs_human_trace("duration and timeout must be positive.");
  73. exit(-2);
  74. }
  75. if ((rtmp = srs_rtmp_create(rtmp_url)) == NULL) {
  76. srs_human_trace("create rtmp failed");
  77. ret = -1;
  78. goto rtmp_destroy;
  79. }
  80. if ((ret = srs_rtmp_set_timeout(rtmp, timeout * 1000, timeout * 1000)) != 0) {
  81. srs_human_trace("set timeout for rtmp failed. errno=%d", ret);
  82. goto rtmp_destroy;
  83. }
  84. if ((ret = srs_rtmp_dns_resolve(rtmp)) != 0) {
  85. srs_human_trace("dns resolve failed. ret=%d", ret);
  86. goto rtmp_destroy;
  87. }
  88. srs_human_trace("dns resolve success");
  89. time_dns_resolve = srs_utils_time_ms();
  90. if ((ret = srs_rtmp_connect_server(rtmp)) != 0) {
  91. srs_human_trace("socket connect failed. ret=%d", ret);
  92. goto rtmp_destroy;
  93. }
  94. srs_human_trace("socket connect success");
  95. time_socket_connect = srs_utils_time_ms();
  96. if ((ret = srs_rtmp_do_simple_handshake(rtmp)) != 0) {
  97. srs_human_trace("do simple handshake failed. ret=%d", ret);
  98. goto rtmp_destroy;
  99. }
  100. srs_human_trace("do simple handshake success");
  101. if ((ret = srs_rtmp_connect_app(rtmp)) != 0) {
  102. srs_human_trace("connect vhost/app failed. ret=%d", ret);
  103. goto rtmp_destroy;
  104. }
  105. srs_human_trace("connect vhost/app success");
  106. if ((ret = srs_rtmp_play_stream(rtmp)) != 0) {
  107. srs_human_trace("play stream failed. ret=%d", ret);
  108. goto rtmp_destroy;
  109. }
  110. srs_human_trace("play stream success");
  111. time_play_stream = srs_utils_time_ms();
  112. for (;;) {
  113. if ((ret = srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size)) != 0) {
  114. srs_human_trace("read packet failed. ret=%d", ret);
  115. goto rtmp_destroy;
  116. }
  117. srs_human_trace("got packet: type=%s, time=%d, size=%d",
  118. srs_human_flv_tag_type2string(type), timestamp, size);
  119. if (SRS_RTMP_TYPE_VIDEO == type || SRS_RTMP_TYPE_AUDIO == type) {
  120. if (time_first_packet <= 0) {
  121. time_first_packet = srs_utils_time_ms();
  122. }
  123. if (basetime <= 0) {
  124. basetime = timestamp;
  125. }
  126. }
  127. free(data);
  128. if (srs_utils_time_ms() - time_startup > timeout * 1000) {
  129. srs_human_trace("timeout, terminate.");
  130. goto rtmp_destroy;
  131. }
  132. if ((timestamp - basetime) > duration * 1000) {
  133. srs_human_trace("duration exceed, terminate.");
  134. goto rtmp_destroy;
  135. }
  136. }
  137. rtmp_destroy:
  138. bytes_nsend = srs_utils_send_bytes(rtmp);
  139. bytes_nrecv = srs_utils_recv_bytes(rtmp);
  140. srs_rtmp_destroy(rtmp);
  141. time_cleanup = srs_utils_time_ms();
  142. time_duration = (int)(time_cleanup - time_startup);
  143. // print result to stderr.
  144. fprintf(stderr, "{"
  145. "\"%s\":%d, " //#0
  146. "\"%s\":%d, " //#1
  147. "\"%s\":%d, " // #2
  148. "\"%s\":%d, " // #3
  149. "\"%s\":%d, " // #4
  150. "\"%s\":%d, " // #5
  151. "\"%s\":%d, " // #6
  152. "\"%s\":%d, " // #7
  153. "\"%s\":%d, " // #8
  154. "\"%s\":%d, " // #9
  155. "\"%s\":%d, " // #10
  156. "%s,%s,%s,%s}",
  157. "code", ret, //#0
  158. // total = dns + tcp_connect + start_play + first_packet + last_packet
  159. "total", time_duration, //#1
  160. "dns", (int)(time_dns_resolve - time_startup), //#2
  161. "tcp_connect", (int)(time_socket_connect - time_dns_resolve), //#3
  162. "start_play", (int)(time_play_stream - time_socket_connect), //#4
  163. "first_packet", (int)(time_first_packet - time_play_stream), //#5
  164. "last_packet", (int)(time_cleanup - time_first_packet), //#6
  165. "stream", (int)(timestamp - basetime), //#7
  166. // expect = time_cleanup - time_first_packet
  167. // actual = stream
  168. // delay = actual - expect
  169. "delay", (int)(timestamp - basetime - (time_cleanup - time_first_packet)), //#8
  170. "publish_kbps", (int)((time_duration <= 0)? 0:(bytes_nsend * 8 / time_duration)), //#9
  171. "play_kbps", (int)((time_duration <= 0)? 0:(bytes_nrecv * 8 / time_duration)), //#10
  172. // unit in ms.
  173. "\"unit\": \"ms\"",
  174. "\"remark0\": \"total = dns + tcp_connect + start_play + first_packet + last_packet\"",
  175. "\"remark1\": \"delay = stream - (time_cleanup - time_first_packet)\"",
  176. "\"remark2\": \"if code is not 0, user must ignore all data\""
  177. );
  178. srs_human_trace(" ");
  179. srs_human_trace("completed");
  180. return ret;
  181. }