srs_ingest_flv.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include "../../objs/include/srs_librtmp.h"
  30. int proxy(srs_flv_t flv, srs_rtmp_t ortmp);
  31. int connect_oc(srs_rtmp_t ortmp);
  32. #define RE_PULSE_MS 300
  33. #define RE_PULSE_JITTER_MS 3000
  34. int64_t re_create();
  35. void re_update(int64_t re, int32_t starttime, uint32_t time);
  36. void re_cleanup(int64_t re, int32_t starttime, uint32_t time);
  37. int64_t tools_main_entrance_startup_time;
  38. int main(int argc, char** argv)
  39. {
  40. int ret = 0;
  41. // main function
  42. tools_main_entrance_startup_time = srs_utils_time_ms();
  43. // user option parse index.
  44. int opt = 0;
  45. // user options.
  46. char* in_flv_file = NULL;
  47. char* out_rtmp_url = NULL;
  48. // rtmp handler
  49. srs_rtmp_t ortmp;
  50. // flv handler
  51. srs_flv_t flv;
  52. printf("ingest flv file and publish to RTMP server like FFMPEG.\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 <= 2) {
  56. printf("ingest flv file and publish to RTMP server\n"
  57. "Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n"
  58. " in_flv_file input flv file, ingest from this file.\n"
  59. " out_rtmp_url output rtmp url, publish to this url.\n"
  60. "For example:\n"
  61. " %s -i doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/livestream\n"
  62. " %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/livestream\n",
  63. argv[0], argv[0], argv[0]);
  64. exit(-1);
  65. }
  66. for (opt = 0; opt < argc; opt++) {
  67. srs_human_trace("argv[%d]=%s", opt, argv[opt]);
  68. }
  69. // fill the options for mac
  70. for (opt = 0; opt < argc - 1; opt++) {
  71. // ignore all options except -i and -y.
  72. char* p = argv[opt];
  73. // only accept -x
  74. if (p[0] != '-' || p[1] == 0 || p[2] != 0) {
  75. continue;
  76. }
  77. // parse according the option name.
  78. switch (p[1]) {
  79. case 'i': in_flv_file = argv[opt + 1]; break;
  80. case 'y': out_rtmp_url = argv[opt + 1]; break;
  81. default: break;
  82. }
  83. }
  84. if (!in_flv_file) {
  85. srs_human_trace("input invalid, use -i <input>");
  86. return -1;
  87. }
  88. if (!out_rtmp_url) {
  89. srs_human_trace("output invalid, use -y <output>");
  90. return -1;
  91. }
  92. srs_human_trace("input: %s", in_flv_file);
  93. srs_human_trace("output: %s", out_rtmp_url);
  94. if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
  95. ret = 2;
  96. srs_human_trace("open flv file failed. ret=%d", ret);
  97. return ret;
  98. }
  99. ortmp = srs_rtmp_create(out_rtmp_url);
  100. ret = proxy(flv, ortmp);
  101. srs_human_trace("ingest flv to RTMP completed");
  102. srs_rtmp_destroy(ortmp);
  103. srs_flv_close(flv);
  104. return ret;
  105. }
  106. int do_proxy(srs_flv_t flv, srs_rtmp_t ortmp, int64_t re, int32_t* pstarttime, uint32_t* ptimestamp)
  107. {
  108. int ret = 0;
  109. // packet data
  110. char type;
  111. int size;
  112. char* data = NULL;
  113. srs_human_trace("start ingest flv to RTMP stream");
  114. char buffer[1024];
  115. for (;;) {
  116. // tag header
  117. if ((ret = srs_flv_read_tag_header(flv, &type, &size, ptimestamp)) != 0) {
  118. if (srs_flv_is_eof(ret)) {
  119. srs_human_trace("parse completed.");
  120. return 0;
  121. }
  122. srs_human_trace("flv get packet failed. ret=%d", ret);
  123. return ret;
  124. }
  125. if (size <= 0) {
  126. srs_human_trace("invalid size=%d", size);
  127. break;
  128. }
  129. // TODO: FIXME: mem leak when error.
  130. data = (char*)malloc(size);
  131. if ((ret = srs_flv_read_tag_data(flv, data, size)) != 0) {
  132. return ret;
  133. }
  134. uint32_t timestamp = *ptimestamp;
  135. if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) != 0) {
  136. srs_human_trace("print packet failed. ret=%d", ret);
  137. return ret;
  138. }
  139. srs_human_trace("%s", buffer);
  140. if ((ret = srs_rtmp_write_packet(ortmp, type, *ptimestamp, data, size)) != 0) {
  141. srs_human_trace("irtmp get packet failed. ret=%d", ret);
  142. return ret;
  143. }
  144. if (*pstarttime < 0 && srs_utils_flv_tag_is_av(type)) {
  145. *pstarttime = *ptimestamp;
  146. }
  147. re_update(re, *pstarttime, *ptimestamp);
  148. }
  149. return ret;
  150. }
  151. int proxy(srs_flv_t flv, srs_rtmp_t ortmp)
  152. {
  153. int ret = 0;
  154. uint32_t timestamp = 0;
  155. int32_t starttime = -1;
  156. char header[13];
  157. if ((ret = srs_flv_read_header(flv, header)) != 0) {
  158. return ret;
  159. }
  160. if ((ret = connect_oc(ortmp)) != 0) {
  161. return ret;
  162. }
  163. int64_t re = re_create();
  164. ret = do_proxy(flv, ortmp, re, &starttime, &timestamp);
  165. // for the last pulse, always sleep.
  166. re_cleanup(re, starttime, timestamp);
  167. return ret;
  168. }
  169. int connect_oc(srs_rtmp_t ortmp)
  170. {
  171. int ret = 0;
  172. if ((ret = srs_rtmp_handshake(ortmp)) != 0) {
  173. srs_human_trace("ortmp simple handshake failed. ret=%d", ret);
  174. return ret;
  175. }
  176. srs_human_trace("ortmp simple handshake success");
  177. if ((ret = srs_rtmp_connect_app(ortmp)) != 0) {
  178. srs_human_trace("ortmp connect vhost/app failed. ret=%d", ret);
  179. return ret;
  180. }
  181. srs_human_trace("ortmp connect vhost/app success");
  182. if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) {
  183. srs_human_trace("ortmp publish stream failed. ret=%d", ret);
  184. return ret;
  185. }
  186. srs_human_trace("ortmp publish stream success");
  187. return ret;
  188. }
  189. int64_t re_create()
  190. {
  191. // if not very precise, we can directly use this as re.
  192. int64_t re = srs_utils_time_ms();
  193. // use the starttime to get the deviation
  194. int64_t deviation = re - tools_main_entrance_startup_time;
  195. srs_human_trace("deviation is %d ms, pulse is %d ms", (int)(deviation), (int)(RE_PULSE_MS));
  196. // so, we adjust time to max(0, deviation)
  197. // because the last pulse, we already sleeped
  198. int adjust = (int)(deviation);
  199. if (adjust > 0) {
  200. srs_human_trace("adjust re time for %d ms", adjust);
  201. re -= adjust;
  202. } else {
  203. srs_human_trace("no need to adjust re time");
  204. }
  205. return re;
  206. }
  207. void re_update(int64_t re, int32_t starttime, uint32_t time)
  208. {
  209. // send by pulse algorithm.
  210. int64_t now = srs_utils_time_ms();
  211. int64_t diff = time - starttime - (now -re);
  212. if (diff > RE_PULSE_MS && diff < RE_PULSE_JITTER_MS) {
  213. usleep((useconds_t)(diff * 1000));
  214. }
  215. }
  216. void re_cleanup(int64_t re, int32_t starttime, uint32_t time)
  217. {
  218. // for the last pulse, always sleep.
  219. // for the virtual live encoder long time publishing.
  220. int64_t now = srs_utils_time_ms();
  221. int64_t diff = time - starttime - (now -re);
  222. if (diff > 0) {
  223. srs_human_trace("re_cleanup, diff=%d, start=%d, last=%d ms", (int)diff, starttime, time);
  224. usleep((useconds_t)(diff * 1000));
  225. }
  226. }