2
0

srs_bandwidth_check.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2013-2018 Winlin
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "../../objs/include/srs_librtmp.h"
  26. // srs debug info.
  27. char* ip = NULL;
  28. char* sig = NULL;
  29. int pid = 0, cid = 0;
  30. int major = 0, minor = 0, revision= 0, build = 0;
  31. // bandwidth test data.
  32. int64_t start_time = 0;
  33. int64_t end_time = 0;
  34. int play_kbps = 0;
  35. int publish_kbps = 0;
  36. int play_bytes = 0;
  37. int publish_bytes = 0;
  38. int play_duration = 0;
  39. int publish_duration = 0;
  40. int do_check(srs_rtmp_t rtmp)
  41. {
  42. int ret = 0;
  43. if ((ret = srs_rtmp_handshake(rtmp)) != 0) {
  44. srs_human_trace("simple handshake failed.");
  45. return ret;
  46. }
  47. srs_human_trace("simple handshake success");
  48. if ((ret = srs_rtmp_connect_app(rtmp)) != 0) {
  49. srs_human_trace("connect vhost/app failed.");
  50. return ret;
  51. }
  52. srs_human_trace("connect vhost/app success");
  53. if ((ret = srs_rtmp_get_server_sig(rtmp, &sig)) != 0) {
  54. srs_human_trace("Retrieve server ID failed, ret=%d", ret);
  55. return ret;
  56. }
  57. if ((ret = srs_rtmp_get_server_id(rtmp, &ip, &pid, &cid)) != 0) {
  58. srs_human_trace("Retrieve server ID failed, ret=%d", ret);
  59. return ret;
  60. }
  61. if ((ret = srs_rtmp_get_server_version(rtmp, &major, &minor, &revision, &build)) != 0) {
  62. srs_human_trace("Retrieve server version failed, ret=%d", ret);
  63. return ret;
  64. }
  65. if ((ret = srs_rtmp_bandwidth_check(rtmp,
  66. &start_time, &end_time, &play_kbps, &publish_kbps, &play_bytes, &publish_bytes,
  67. &play_duration, &publish_duration)) != 0
  68. ) {
  69. srs_human_trace("bandwidth check/test failed.");
  70. return ret;
  71. }
  72. srs_human_trace("bandwidth check/test success");
  73. return ret;
  74. }
  75. int main(int argc, char** argv)
  76. {
  77. int ret = 0;
  78. srs_rtmp_t rtmp;
  79. printf("RTMP bandwidth check/test with server.\n");
  80. printf("srs(ossrs) client librtmp library.\n");
  81. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  82. if (argc <= 1) {
  83. printf("RTMP bandwidth check/test with server.\n"
  84. "Usage: %s <rtmp_url>\n"
  85. " rtmp_url RTMP bandwidth url to check. format: rtmp://server:port/app?key=xxx,vhost=xxx\n"
  86. "For example:\n"
  87. " %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com\n"
  88. " %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com>/dev/null\n"
  89. "@remark, output text to stdout, while json to stderr.\n",
  90. argv[0], argv[0], argv[0]);
  91. exit(-1);
  92. }
  93. char url[512];
  94. snprintf(url, sizeof(url), "%s/%s", argv[1], "livestream");
  95. rtmp = srs_rtmp_create((const char*)url);
  96. srs_human_trace("bandwidth check/test url: %s", argv[1]);
  97. if ((ret = do_check(rtmp)) != 0) {
  98. goto rtmp_destroy;
  99. }
  100. srs_human_trace("\n%s, %s, %d.%d.%d.%d, srs_pid=%d, srs_id=%d\n"
  101. "duration: %dms(%d+%d), play: %dkbps, publish: %dkbps",
  102. (char*)sig, (char*)ip, major, minor, revision, build, pid, cid,
  103. (int)(end_time - start_time), play_duration, publish_duration, play_kbps, publish_kbps);
  104. rtmp_destroy:
  105. fprintf(stderr, "{\"code\":%d,"
  106. "\"srs_server\":\"%s\", \"srs_primary\":\"\", \"srs_authors\":\"\", \"srs_server_ip\":\"%s\", "
  107. "\"srs_version\":\"%d.%d.%d.%d\", "
  108. "\"srs_pid\":%d, "
  109. "\"srs_id\":%d, "
  110. "\"duration\":%d, "
  111. "\"play_duration\":%d, "
  112. "\"publish_duration\":%d,"
  113. "\"play_kbps\":%d, "
  114. "\"publish_kbps\":%d"
  115. "}",
  116. ret,
  117. (char*)sig, (char*)ip,
  118. major, minor, revision, build, pid, cid,
  119. (int)(end_time - start_time), play_duration, publish_duration,
  120. play_kbps, publish_kbps);
  121. srs_rtmp_destroy(rtmp);
  122. srs_human_trace(" ");
  123. srs_human_trace("completed");
  124. return ret;
  125. }