srs_bandwidth_check.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_bandwidth_check.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_bandwidth_check
  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. // packet data
  30. int size;
  31. char type;
  32. char* data;
  33. u_int32_t timestamp;
  34. // srs debug info.
  35. char srs_server_ip[128];
  36. char srs_server[128];
  37. char srs_primary[128];
  38. char srs_authors[128];
  39. char srs_version[32];
  40. int srs_id = 0;
  41. int srs_pid = 0;
  42. // bandwidth test data.
  43. int64_t start_time = 0;
  44. int64_t end_time = 0;
  45. int play_kbps = 0;
  46. int publish_kbps = 0;
  47. int play_bytes = 0;
  48. int publish_bytes = 0;
  49. int play_duration = 0;
  50. int publish_duration = 0;
  51. // set to zero.
  52. srs_server_ip[0] = 0;
  53. srs_server[0] = 0;
  54. srs_primary[0] = 0;
  55. srs_authors[0] = 0;
  56. srs_version[0] = 0;
  57. printf("RTMP bandwidth check/test with server.\n");
  58. printf("srs(ossrs) client librtmp library.\n");
  59. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  60. if (argc <= 1) {
  61. printf("RTMP bandwidth check/test with server.\n"
  62. "Usage: %s <rtmp_url>\n"
  63. " rtmp_url RTMP bandwidth url to check. format: rtmp://server:port/app?key=xxx,vhost=xxx\n"
  64. "For example:\n"
  65. " %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com\n"
  66. " %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com>/dev/null\n"
  67. "@remark, output text to stdout, while json to stderr.\n",
  68. argv[0], argv[0], argv[0]);
  69. exit(-1);
  70. }
  71. rtmp = srs_rtmp_create2(argv[1]);
  72. srs_human_trace("bandwidth check/test url: %s", argv[1]);
  73. if ((ret = srs_rtmp_handshake(rtmp)) != 0) {
  74. srs_human_trace("simple handshake failed.");
  75. goto rtmp_destroy;
  76. }
  77. srs_human_trace("simple handshake success");
  78. if ((ret = srs_rtmp_connect_app2(rtmp,
  79. srs_server_ip, srs_server, srs_primary, srs_authors, srs_version,
  80. &srs_id, &srs_pid)) != 0) {
  81. srs_human_trace("connect vhost/app failed.");
  82. goto rtmp_destroy;
  83. }
  84. srs_human_trace("connect vhost/app success");
  85. if ((ret = srs_rtmp_bandwidth_check(rtmp,
  86. &start_time, &end_time, &play_kbps, &publish_kbps,
  87. &play_bytes, &publish_bytes, &play_duration, &publish_duration)) != 0
  88. ) {
  89. srs_human_trace("bandwidth check/test failed.");
  90. goto rtmp_destroy;
  91. }
  92. srs_human_trace("bandwidth check/test success");
  93. srs_human_trace("\n%s, %s, %s\n"
  94. "%s, %s, srs_pid=%d, srs_id=%d\n"
  95. "duration: %dms(%d+%d)\n"
  96. "play: %dkbps\n"
  97. "publish: %dkbps",
  98. (char*)srs_server, (char*)srs_primary, (char*)srs_authors,
  99. (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id,
  100. (int)(end_time - start_time), play_duration, publish_duration,
  101. play_kbps,
  102. publish_kbps);
  103. rtmp_destroy:
  104. srs_rtmp_destroy(rtmp);
  105. fprintf(stderr, "{\"code\":%d,"
  106. "\"srs_server\":\"%s\", "
  107. "\"srs_primary\":\"%s\", "
  108. "\"srs_authors\":\"%s\", "
  109. "\"srs_server_ip\":\"%s\", "
  110. "\"srs_version\":\"%s\", "
  111. "\"srs_pid\":%d, "
  112. "\"srs_id\":%d, "
  113. "\"duration\":%d, "
  114. "\"play_duration\":%d, "
  115. "\"publish_duration\":%d,"
  116. "\"play_kbps\":%d, "
  117. "\"publish_kbps\":%d"
  118. "}",
  119. ret,
  120. (char*)srs_server, (char*)srs_primary, (char*)srs_authors,
  121. (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id,
  122. (int)(end_time - start_time), play_duration, publish_duration,
  123. play_kbps, publish_kbps);
  124. srs_human_trace(" ");
  125. srs_human_trace("completed");
  126. return ret;
  127. }