2
0

srs_ingest_mp4.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. int64_t tools_main_entrance_startup_time;
  27. int proxy(srs_mp4_t mp4, srs_rtmp_t ortmp);
  28. int main(int argc, char** argv)
  29. {
  30. // main function
  31. tools_main_entrance_startup_time = srs_utils_time_ms();
  32. printf("Ingest mp4 file and publish to RTMP server like FFMPEG.\n");
  33. printf("SRS(OSSRS) client librtmp library.\n");
  34. printf("Version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  35. if (argc <= 2) {
  36. printf("Ingest mp4 file and publish to RTMP server\n"
  37. "Usage: %s <-i in_mp4_file> <-y out_rtmp_url>\n"
  38. " in_mp4_file input mp4 file, ingest from this file.\n"
  39. " out_rtmp_url output rtmp url, publish to this url.\n"
  40. "@remark Only support non-seek mp4, see https://github.com/ossrs/srs/issues/738#issuecomment-276343669\n"
  41. "For example:\n"
  42. " %s -i avatar.mp4 -y rtmp://127.0.0.1/live/livestream\n",
  43. argv[0], argv[0]);
  44. exit(-1);
  45. }
  46. int opt;
  47. for (opt = 0; opt < argc; opt++) {
  48. srs_human_trace("The argv[%d]=%s", opt, argv[opt]);
  49. }
  50. // fill the options for mac
  51. char* in_file = NULL;
  52. char* out_rtmp_url = NULL;
  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_file = argv[opt + 1]; break;
  63. case 'y': out_rtmp_url = argv[opt + 1]; break;
  64. default: break;
  65. }
  66. }
  67. if (!in_file) {
  68. srs_human_trace("Invalid input file, use -i <input>");
  69. return -1;
  70. }
  71. if (!out_rtmp_url) {
  72. srs_human_trace("Invalid output url, use -y <output>");
  73. return -1;
  74. }
  75. srs_human_trace("Input file: %s", in_file);
  76. srs_human_trace("Output url: %s", out_rtmp_url);
  77. int ret = 0;
  78. srs_mp4_t mp4 = NULL;
  79. if ((mp4 = srs_mp4_open_read(in_file)) == NULL) {
  80. ret = 2;
  81. srs_human_trace("open mp4 file failed. ret=%d", ret);
  82. return ret;
  83. }
  84. srs_rtmp_t ortmp = srs_rtmp_create(out_rtmp_url);
  85. ret = proxy(mp4, ortmp);
  86. srs_human_trace("Ingest mp4 to RTMP ok.");
  87. srs_rtmp_destroy(ortmp);
  88. srs_mp4_close(mp4);
  89. return ret;
  90. }
  91. int connect_oc(srs_rtmp_t ortmp)
  92. {
  93. int ret = 0;
  94. if ((ret = srs_rtmp_handshake(ortmp)) != 0) {
  95. srs_human_trace("ortmp simple handshake failed. ret=%d", ret);
  96. return ret;
  97. }
  98. srs_human_trace("ortmp simple handshake success");
  99. if ((ret = srs_rtmp_connect_app(ortmp)) != 0) {
  100. srs_human_trace("ortmp connect vhost/app failed. ret=%d", ret);
  101. return ret;
  102. }
  103. srs_human_trace("ortmp connect vhost/app success");
  104. if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) {
  105. srs_human_trace("ortmp publish stream failed. ret=%d", ret);
  106. return ret;
  107. }
  108. srs_human_trace("ortmp publish stream success");
  109. return ret;
  110. }
  111. #define RE_PULSE_MS 300
  112. #define RE_PULSE_JITTER_MS 3000
  113. int64_t re_create()
  114. {
  115. // if not very precise, we can directly use this as re.
  116. int64_t re = srs_utils_time_ms();
  117. // use the starttime to get the deviation
  118. int64_t deviation = re - tools_main_entrance_startup_time;
  119. srs_human_trace("deviation is %d ms, pulse is %d ms", (int)(deviation), (int)(RE_PULSE_MS));
  120. // so, we adjust time to max(0, deviation)
  121. // because the last pulse, we already sleeped
  122. int adjust = (int)(deviation);
  123. if (adjust > 0) {
  124. srs_human_trace("adjust re time for %d ms", adjust);
  125. re -= adjust;
  126. } else {
  127. srs_human_trace("no need to adjust re time");
  128. }
  129. return re;
  130. }
  131. void re_update(int64_t re, int32_t starttime, uint32_t time)
  132. {
  133. // send by pulse algorithm.
  134. int64_t now = srs_utils_time_ms();
  135. int64_t diff = time - starttime - (now -re);
  136. if (diff > RE_PULSE_MS && diff < RE_PULSE_JITTER_MS) {
  137. usleep((useconds_t)(diff * 1000));
  138. }
  139. }
  140. void re_cleanup(int64_t re, int32_t starttime, uint32_t time)
  141. {
  142. // for the last pulse, always sleep.
  143. // for the virtual live encoder long time publishing.
  144. int64_t now = srs_utils_time_ms();
  145. int64_t diff = time - starttime - (now -re);
  146. if (diff > 0) {
  147. srs_human_trace("re_cleanup, diff=%d, start=%d, last=%d ms",
  148. (int)diff, starttime, time);
  149. usleep((useconds_t)(diff * 1000));
  150. }
  151. }
  152. int do_proxy(srs_mp4_t mp4, srs_rtmp_t ortmp, int64_t re, int32_t* pstarttime, uint32_t* ptimestamp)
  153. {
  154. int ret = 0;
  155. srs_human_trace("start ingest mp4 to RTMP stream");
  156. char buffer[1024];
  157. for (;;) {
  158. // packet data
  159. char type;
  160. int32_t size;
  161. char* data = NULL;
  162. // Read a mp4 sample and convert to flv tag.
  163. if (1) {
  164. srs_mp4_sample_t sample;
  165. if ((ret = srs_mp4_read_sample(mp4, &sample)) != 0) {
  166. if (srs_mp4_is_eof(ret)) {
  167. srs_human_trace("parse completed.");
  168. return 0;
  169. }
  170. srs_human_trace("mp4 get sample failed. ret=%d", ret);
  171. return ret;
  172. }
  173. size = srs_mp4_sizeof(mp4, &sample);
  174. data = (char*)malloc(size);
  175. if ((ret = srs_mp4_to_flv_tag(mp4, &sample, &type, ptimestamp, data, size)) != 0) {
  176. return ret;
  177. }
  178. srs_mp4_free_sample(&sample);
  179. }
  180. uint32_t timestamp = *ptimestamp;
  181. if ((ret = srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size)) != 0) {
  182. srs_human_trace("print packet failed. ret=%d", ret);
  183. return ret;
  184. }
  185. srs_human_trace("%s", buffer);
  186. if ((ret = srs_rtmp_write_packet(ortmp, type, *ptimestamp, data, size)) != 0) {
  187. srs_human_trace("irtmp get packet failed. ret=%d", ret);
  188. return ret;
  189. }
  190. if (*pstarttime < 0 && srs_utils_flv_tag_is_av(type)) {
  191. *pstarttime = *ptimestamp;
  192. }
  193. re_update(re, *pstarttime, *ptimestamp);
  194. }
  195. return ret;
  196. }
  197. int proxy(srs_mp4_t mp4, srs_rtmp_t ortmp)
  198. {
  199. int ret = 0;
  200. if ((ret = srs_mp4_init_demuxer(mp4)) != 0) {
  201. srs_human_trace("init demuxer failed. ret=%d", ret);
  202. return ret;
  203. }
  204. if ((ret = connect_oc(ortmp)) != 0) {
  205. return ret;
  206. }
  207. int64_t re = re_create();
  208. uint32_t timestamp = 0;
  209. int32_t starttime = -1;
  210. ret = do_proxy(mp4, ortmp, re, &starttime, &timestamp);
  211. // for the last pulse, always sleep.
  212. re_cleanup(re, starttime, timestamp);
  213. return ret;
  214. }