2
0

srs_ingest_rtmp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_ingest_rtmp.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_ingest_rtmp
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include "../../objs/include/srs_librtmp.h"
  26. int connect_ic(srs_rtmp_t irtmp);
  27. int connect_oc(srs_rtmp_t ortmp);
  28. int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp);
  29. int main(int argc, char** argv)
  30. {
  31. int ret = 0;
  32. // user option parse index.
  33. int opt = 0;
  34. // user options.
  35. char* in_rtmp_url = NULL;
  36. char* out_rtmp_url = NULL;
  37. // rtmp handler
  38. srs_rtmp_t irtmp, ortmp;
  39. printf("ingest RTMP and publish to RTMP server like edge.\n");
  40. printf("srs(ossrs) client librtmp library.\n");
  41. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  42. if (argc <= 2) {
  43. printf("ingest RTMP and publish to RTMP server\n"
  44. "Usage: %s <-i in_rtmp_url> <-y out_rtmp_url>\n"
  45. " in_rtmp_url input rtmp url, ingest from this url.\n"
  46. " out_rtmp_url output rtmp url, publish to this url.\n"
  47. "For example:\n"
  48. " %s -i rtmp://127.0.0.1/live/livestream -y rtmp://127.0.0.1/live/demo\n",
  49. argv[0], argv[0]);
  50. exit(-1);
  51. }
  52. // fill the options for mac
  53. for (opt = 0; opt < argc - 1; opt++) {
  54. // ignore all options except -i and -y.
  55. char* p = argv[opt];
  56. // only accept -x
  57. if (p[0] != '-' || p[1] == 0 || p[2] != 0) {
  58. continue;
  59. }
  60. // parse according the option name.
  61. switch (p[1]) {
  62. case 'i': in_rtmp_url = argv[opt + 1]; break;
  63. case 'y': out_rtmp_url = argv[opt + 1]; break;
  64. default: break;
  65. }
  66. }
  67. if (!in_rtmp_url) {
  68. srs_human_trace("input invalid, use -i <input>");
  69. return -1;
  70. }
  71. if (!out_rtmp_url) {
  72. srs_human_trace("output invalid, use -y <output>");
  73. return -1;
  74. }
  75. srs_human_trace("input: %s", in_rtmp_url);
  76. srs_human_trace("output: %s", out_rtmp_url);
  77. irtmp = srs_rtmp_create(in_rtmp_url);
  78. ortmp = srs_rtmp_create(out_rtmp_url);
  79. ret = proxy(irtmp, ortmp);
  80. srs_human_trace("proxy completed");
  81. srs_rtmp_destroy(irtmp);
  82. srs_rtmp_destroy(ortmp);
  83. return ret;
  84. }
  85. int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp)
  86. {
  87. int ret = 0;
  88. // packet data
  89. int size;
  90. char type;
  91. char* data;
  92. u_int32_t timestamp;
  93. if ((ret = connect_ic(irtmp)) != 0) {
  94. return ret;
  95. }
  96. if ((ret = connect_oc(ortmp)) != 0) {
  97. return ret;
  98. }
  99. srs_human_trace("start proxy RTMP stream");
  100. for (;;) {
  101. if ((ret = srs_rtmp_read_packet(irtmp, &type, &timestamp, &data, &size)) != 0) {
  102. srs_human_trace("irtmp get packet failed. ret=%d", ret);
  103. return ret;
  104. }
  105. if (!srs_utils_flv_tag_is_ok(type)) {
  106. srs_human_trace("ignore invalid flv tag=%d, dts=%d, %d bytes", type, timestamp, size);
  107. free(data);
  108. continue;
  109. }
  110. if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) != 0) {
  111. srs_human_trace("print packet failed. ret=%d", ret);
  112. return ret;
  113. }
  114. if ((ret = srs_rtmp_write_packet(ortmp, type, timestamp, data, size)) != 0) {
  115. srs_human_trace("irtmp get packet failed. ret=%d", ret);
  116. return ret;
  117. }
  118. srs_human_verbose("ortmp sent packet: type=%s, time=%d, size=%d",
  119. srs_human_flv_tag_type2string(type), timestamp, size);
  120. }
  121. return ret;
  122. }
  123. int connect_ic(srs_rtmp_t irtmp)
  124. {
  125. int ret = 0;
  126. if ((ret = srs_rtmp_handshake(irtmp)) != 0) {
  127. srs_human_trace("irtmp simple handshake failed. ret=%d", ret);
  128. return ret;
  129. }
  130. srs_human_trace("irtmp simple handshake success");
  131. if ((ret = srs_rtmp_connect_app(irtmp)) != 0) {
  132. srs_human_trace("irtmp connect vhost/app failed. ret=%d", ret);
  133. return ret;
  134. }
  135. srs_human_trace("irtmp connect vhost/app success");
  136. if ((ret = srs_rtmp_play_stream(irtmp)) != 0) {
  137. srs_human_trace("irtmp play stream failed. ret=%d", ret);
  138. return ret;
  139. }
  140. srs_human_trace("irtmp play stream success");
  141. return ret;
  142. }
  143. int connect_oc(srs_rtmp_t ortmp)
  144. {
  145. int ret = 0;
  146. if ((ret = srs_rtmp_handshake(ortmp)) != 0) {
  147. srs_human_trace("ortmp simple handshake failed. ret=%d", ret);
  148. return ret;
  149. }
  150. srs_human_trace("ortmp simple handshake success");
  151. if ((ret = srs_rtmp_connect_app(ortmp)) != 0) {
  152. srs_human_trace("ortmp connect vhost/app failed. ret=%d", ret);
  153. return ret;
  154. }
  155. srs_human_trace("ortmp connect vhost/app success");
  156. if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) {
  157. srs_human_trace("ortmp publish stream failed. ret=%d", ret);
  158. return ret;
  159. }
  160. srs_human_trace("ortmp publish stream success");
  161. return ret;
  162. }