srs_ingest_flv.c 8.2 KB

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