2
0

srs_ingest_rtmp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <unistd.h>
  26. #include "../../objs/include/srs_librtmp.h"
  27. int connect_ic(srs_rtmp_t irtmp);
  28. int connect_oc(srs_rtmp_t ortmp);
  29. int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp);
  30. // whether use verbose log.
  31. int verbose = 0;
  32. // 2000 is about 30s.
  33. #define PITHY_PRINT_EVERY_MSGS 2000
  34. int main(int argc, char** argv)
  35. {
  36. int ret = 0;
  37. // user option parse index.
  38. int opt = 0;
  39. // user options.
  40. char* in_rtmp_url = NULL;
  41. char* out_rtmp_url = NULL;
  42. // rtmp handler
  43. srs_rtmp_t irtmp, ortmp;
  44. printf("Ingest RTMP to server like FFMPEG over srs-librtmp %d.%d.%d\n",
  45. srs_version_major(), srs_version_minor(), srs_version_revision());
  46. if (argc <= 2) {
  47. printf("ingest RTMP and publish to RTMP server\n"
  48. "Usage: %s <-i in_rtmp_url> <-y out_rtmp_url> [-v verbose]\n"
  49. " in_rtmp_url input rtmp url, ingest from this url.\n"
  50. " out_rtmp_url output rtmp url, publish to this url.\n"
  51. " verbose output verbose log.\n"
  52. "For example:\n"
  53. " %s -i rtmp://127.0.0.1/live/livestream -y rtmp://127.0.0.1/live/demo\n"
  54. " %s -i rtmp://127.0.0.1/live/livestream -y rtmp://127.0.0.1/live/demo -v verbose\n",
  55. argv[0], argv[0], argv[0]);
  56. exit(-1);
  57. }
  58. // fill the options for mac
  59. for (opt = 0; opt < argc - 1; opt++) {
  60. // ignore all options except -i and -y.
  61. char* p = argv[opt];
  62. // only accept -x
  63. if (p[0] != '-' || p[1] == 0 || p[2] != 0) {
  64. continue;
  65. }
  66. // parse according the option name.
  67. switch (p[1]) {
  68. case 'i': in_rtmp_url = argv[++opt]; break;
  69. case 'y': out_rtmp_url = argv[++opt]; break;
  70. case 'v': verbose=1; opt++; break;
  71. default: break;
  72. }
  73. }
  74. if (!in_rtmp_url) {
  75. srs_human_trace("input invalid, use -i <input>");
  76. return -1;
  77. }
  78. if (!out_rtmp_url) {
  79. srs_human_trace("output invalid, use -y <output>");
  80. return -1;
  81. }
  82. srs_human_trace("ingest %s to %s, verbose=%d", in_rtmp_url, out_rtmp_url, verbose);
  83. irtmp = srs_rtmp_create(in_rtmp_url);
  84. ortmp = srs_rtmp_create(out_rtmp_url);
  85. ret = proxy(irtmp, ortmp);
  86. srs_human_trace("proxy completed");
  87. srs_rtmp_destroy(irtmp);
  88. srs_rtmp_destroy(ortmp);
  89. return ret;
  90. }
  91. int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp)
  92. {
  93. int ret = 0;
  94. // packet data
  95. int size;
  96. char type;
  97. char* data;
  98. uint32_t timestamp;
  99. uint64_t nb_msgs = 0;
  100. if ((ret = connect_ic(irtmp)) != 0) {
  101. return ret;
  102. }
  103. if ((ret = connect_oc(ortmp)) != 0) {
  104. return ret;
  105. }
  106. if (verbose) {
  107. srs_human_trace("start proxy RTMP stream");
  108. } else {
  109. srs_human_verbose("start proxy RTMP stream");
  110. }
  111. char buffer[1024];
  112. for (;;) {
  113. if ((ret = srs_rtmp_read_packet(irtmp, &type, &timestamp, &data, &size)) != 0) {
  114. srs_human_trace("irtmp get packet failed. ret=%d", ret);
  115. return ret;
  116. }
  117. if (!srs_utils_flv_tag_is_ok(type)) {
  118. if (verbose) {
  119. srs_human_trace("ignore invalid flv tag=%d, dts=%d, %d bytes", type, timestamp, size);
  120. } else {
  121. srs_human_verbose("ignore invalid flv tag=%d, dts=%d, %d bytes", type, timestamp, size);
  122. }
  123. free(data);
  124. continue;
  125. }
  126. if (verbose || ((nb_msgs++ % PITHY_PRINT_EVERY_MSGS) == 0 && nb_msgs > 10)) {
  127. if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) != 0) {
  128. srs_human_trace("print packet failed. ret=%d", ret);
  129. return ret;
  130. }
  131. srs_human_trace("%s", buffer);
  132. }
  133. if ((ret = srs_rtmp_write_packet(ortmp, type, timestamp, data, size)) != 0) {
  134. srs_human_trace("irtmp get packet failed. ret=%d", ret);
  135. return ret;
  136. }
  137. if (verbose) {
  138. srs_human_trace("ortmp sent packet: type=%s, time=%d, size=%d",
  139. srs_human_flv_tag_type2string(type), timestamp, size);
  140. } else {
  141. srs_human_verbose("ortmp sent packet: type=%s, time=%d, size=%d",
  142. srs_human_flv_tag_type2string(type), timestamp, size);
  143. }
  144. }
  145. return ret;
  146. }
  147. int connect_ic(srs_rtmp_t irtmp)
  148. {
  149. int ret = 0;
  150. // srs debug info.
  151. char* ip = NULL;
  152. char* sig = NULL;
  153. int pid = 0, cid = 0;
  154. int major = 0, minor = 0, revision= 0, build = 0;
  155. if ((ret = srs_rtmp_handshake(irtmp)) != 0) {
  156. srs_human_trace("irtmp simple handshake failed. ret=%d", ret);
  157. return ret;
  158. }
  159. if (verbose) {
  160. srs_human_trace("irtmp simple handshake success");
  161. } else {
  162. srs_human_verbose("irtmp simple handshake success");
  163. }
  164. if (srs_rtmp_connect_app(irtmp) != 0) {
  165. srs_human_trace("irtmp connect vhost/app failed. ret=%d", ret);
  166. return ret;
  167. }
  168. if ((ret = srs_rtmp_get_server_sig(irtmp, &sig)) != 0) {
  169. srs_human_trace("Retrieve server ID failed, ret=%d", ret);
  170. return ret;
  171. }
  172. if ((ret = srs_rtmp_get_server_id(irtmp, &ip, &pid, &cid)) != 0) {
  173. srs_human_trace("Retrieve server ID failed, ret=%d", ret);
  174. return ret;
  175. }
  176. if ((ret = srs_rtmp_get_server_version(irtmp, &major, &minor, &revision, &build)) != 0) {
  177. srs_human_trace("Retrieve server version failed, ret=%d", ret);
  178. return ret;
  179. }
  180. srs_human_trace("irtmp connect ok, ip=%s, server=%s/%d.%d.%d.%d, pid=%d, cid=%d",
  181. ip, sig, major, minor, revision, build, pid, cid);
  182. if ((ret = srs_rtmp_play_stream(irtmp)) != 0) {
  183. srs_human_trace("irtmp play stream failed. ret=%d", ret);
  184. return ret;
  185. }
  186. if (verbose) {
  187. srs_human_trace("irtmp play stream success");
  188. } else {
  189. srs_human_verbose("irtmp play stream success");
  190. }
  191. return ret;
  192. }
  193. int connect_oc(srs_rtmp_t ortmp)
  194. {
  195. int ret = 0;
  196. // srs debug info.
  197. char* ip = NULL;
  198. char* sig = NULL;
  199. int pid = 0, cid = 0;
  200. int major = 0, minor = 0, revision= 0, build = 0;
  201. if ((ret = srs_rtmp_handshake(ortmp)) != 0) {
  202. srs_human_trace("ortmp simple handshake failed. ret=%d", ret);
  203. return ret;
  204. }
  205. if (verbose) {
  206. srs_human_trace("ortmp simple handshake success");
  207. } else {
  208. srs_human_verbose("ortmp simple handshake success");
  209. }
  210. if (srs_rtmp_connect_app(ortmp) != 0) {
  211. srs_human_trace("ortmp connect vhost/app failed. ret=%d", ret);
  212. return ret;
  213. }
  214. if ((ret = srs_rtmp_get_server_sig(ortmp, &sig)) != 0) {
  215. srs_human_trace("Retrieve server ID failed, ret=%d", ret);
  216. return ret;
  217. }
  218. if ((ret = srs_rtmp_get_server_id(ortmp, &ip, &pid, &cid)) != 0) {
  219. srs_human_trace("Retrieve server ID failed, ret=%d", ret);
  220. return ret;
  221. }
  222. if ((ret = srs_rtmp_get_server_version(ortmp, &major, &minor, &revision, &build)) != 0) {
  223. srs_human_trace("Retrieve server version failed, ret=%d", ret);
  224. return ret;
  225. }
  226. srs_human_trace("ortmp connect ok, ip=%s, server=%s/%d.%d.%d.%d, pid=%d, cid=%d",
  227. ip, sig, major, minor, revision, build, pid, cid);
  228. if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) {
  229. srs_human_trace("ortmp publish stream failed. ret=%d", ret);
  230. return ret;
  231. }
  232. if (verbose) {
  233. srs_human_trace("ortmp publish stream success");
  234. } else {
  235. srs_human_verbose("ortmp publish stream success");
  236. }
  237. return ret;
  238. }