2
0

srs_flv_injecter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_flv_injecter.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_flv_injecter
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.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. #define ERROR_INJECTED 10000
  31. int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc);
  32. int build_keyframes(srs_flv_t ic, srs_amf0_t *pname, srs_amf0_t* pdata, srs_amf0_t* pfilepositions, int64_t* pmetadata_end_offset);
  33. int do_inject_flv(srs_flv_t ic, srs_flv_t oc, srs_amf0_t amf0_name, srs_amf0_t amf0_data, srs_amf0_t filepositions, int64_t metadata_end_offset);
  34. int main(int argc, char** argv)
  35. {
  36. int ret = 0;
  37. // user options.
  38. char* in_flv_file;
  39. char* out_flv_file;
  40. // flv handler
  41. srs_flv_t ic = NULL;
  42. srs_flv_t oc = NULL;
  43. // temp variables.
  44. int tmp_file_size = 0;
  45. char* tmp_file;
  46. printf("inject flv file keyframes to metadata.\n");
  47. printf("srs(ossrs) client librtmp library.\n");
  48. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  49. if (argc <= 2) {
  50. printf("inject flv file keyframes to metadata\n"
  51. "Usage: %s in_flv_file out_flv_file\n"
  52. " in_flv_file input flv file to inject.\n"
  53. " out_flv_file the inject output file, can be in_flv_file.\n"
  54. "For example:\n"
  55. " %s doc/source.200kbps.768x320.flv injected.flv\n"
  56. " %s ../../doc/source.200kbps.768x320.flv injected.flv\n",
  57. argv[0], argv[0], argv[0]);
  58. exit(-1);
  59. }
  60. in_flv_file = argv[1];
  61. out_flv_file = argv[2];
  62. tmp_file_size = strlen(out_flv_file) + strlen(".tmp") + 1;
  63. tmp_file = (char*)malloc(tmp_file_size);
  64. snprintf(tmp_file, tmp_file_size, "%s.tmp", out_flv_file);
  65. srs_human_trace("input: %s", in_flv_file);
  66. srs_human_trace("output: %s", out_flv_file);
  67. srs_human_trace("tmp_file: %s", tmp_file);
  68. ret = process(in_flv_file, tmp_file, &ic, &oc);
  69. srs_flv_close(ic);
  70. srs_flv_close(oc);
  71. if (ret != 0) {
  72. unlink(tmp_file);
  73. if (ret == ERROR_INJECTED) {
  74. ret = 0;
  75. srs_human_trace("file already injected.");
  76. } else {
  77. srs_human_trace("error, remove tmp file.");
  78. }
  79. } else {
  80. rename(tmp_file, out_flv_file);
  81. srs_human_trace("completed, rename to %s", out_flv_file);
  82. }
  83. free(tmp_file);
  84. return ret;
  85. }
  86. int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc)
  87. {
  88. int ret = 0;
  89. srs_flv_t ic;
  90. srs_flv_t oc;
  91. // to adjust metadata.
  92. // the ic metadata end offset, the next tag start offset.
  93. // all oc metadata must adjust according to:
  94. // adjust = new_metadata_end_offset - metadata_end_offset
  95. int64_t metadata_end_offset = 0;
  96. // metadata
  97. srs_amf0_t amf0_name = NULL;
  98. srs_amf0_t amf0_data = NULL;
  99. srs_amf0_t filepositions = NULL;
  100. if ((ic = srs_flv_open_read(in_flv_file)) == NULL) {
  101. ret = 2;
  102. srs_human_trace("open input flv file failed. ret=%d", ret);
  103. return ret;
  104. }
  105. *pic = ic;
  106. if ((oc = srs_flv_open_write(out_flv_file)) == NULL) {
  107. ret = 2;
  108. srs_human_trace("open output flv file failed. ret=%d", ret);
  109. return ret;
  110. }
  111. *poc = oc;
  112. /**
  113. * we use two roundtrip to avoid the paddings of metadata,
  114. * to support large keyframes videos without padding fields.
  115. */
  116. // build keyframes offset to metadata.
  117. if ((ret = build_keyframes(ic, &amf0_name, &amf0_data, &filepositions, &metadata_end_offset)) != 0) {
  118. return ret;
  119. }
  120. // inject the metadata to oc.
  121. if ((ret = do_inject_flv(ic, oc, amf0_name, amf0_data, filepositions, metadata_end_offset)) != 0) {
  122. return ret;
  123. }
  124. // TODO: FIXME: mem leak when error.
  125. srs_amf0_free(amf0_name);
  126. srs_amf0_free(amf0_data);
  127. return ret;
  128. }
  129. int parse_metadata(char* data, int size, srs_amf0_t* pname, srs_amf0_t* pdata)
  130. {
  131. int ret = 0;
  132. int nparsed = 0;
  133. *pname = srs_amf0_parse(data, size, &nparsed);
  134. if (*pname == NULL || nparsed >= size) {
  135. srs_human_trace("invalid amf0 name data.");
  136. return -1;
  137. }
  138. *pdata = srs_amf0_parse(data + nparsed, size - nparsed, &nparsed);
  139. if (*pdata == NULL || nparsed > size) {
  140. srs_human_trace("invalid amf0 value data");
  141. return -1;
  142. }
  143. return ret;
  144. }
  145. int build_keyframes(srs_flv_t ic, srs_amf0_t *pname, srs_amf0_t* pdata, srs_amf0_t* pfilepositions, int64_t* pmetadata_end_offset)
  146. {
  147. int ret = 0;
  148. // flv header
  149. char header[13];
  150. // packet data
  151. char type;
  152. u_int32_t timestamp = 0;
  153. char* data = NULL;
  154. int32_t size;
  155. int64_t offset = 0;
  156. // metadata
  157. srs_amf0_t amf0_name = NULL;
  158. srs_amf0_t amf0_data = NULL;
  159. srs_amf0_t keyframes = NULL;
  160. srs_amf0_t filepositions = NULL;
  161. srs_amf0_t times = NULL;
  162. // reset to generate metadata
  163. srs_flv_lseek(ic, 0);
  164. if ((ret = srs_flv_read_header(ic, header)) != 0) {
  165. return ret;
  166. }
  167. srs_human_trace("build keyframe infos from flv");
  168. for (;;) {
  169. offset = srs_flv_tellg(ic);
  170. // tag header
  171. if ((ret = srs_flv_read_tag_header(ic, &type, &size, &timestamp)) != 0) {
  172. if (srs_flv_is_eof(ret)) {
  173. srs_human_trace("parse completed.");
  174. return 0;
  175. }
  176. srs_human_trace("flv get packet failed. ret=%d", ret);
  177. return ret;
  178. }
  179. if (size <= 0) {
  180. srs_human_trace("invalid size=%d", size);
  181. return ret;
  182. }
  183. // TODO: FIXME: mem leak when error.
  184. data = (char*)malloc(size);
  185. if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) {
  186. return ret;
  187. }
  188. // data tag
  189. if (type == SRS_RTMP_TYPE_VIDEO) {
  190. if (!srs_flv_is_sequence_header(data, size) && srs_flv_is_keyframe(data, size)) {
  191. srs_amf0_strict_array_append(filepositions, srs_amf0_create_number(offset));
  192. srs_amf0_strict_array_append(times, srs_amf0_create_number(((double)timestamp)/ 1000));
  193. }
  194. } else if (type == SRS_RTMP_TYPE_SCRIPT) {
  195. *pmetadata_end_offset = srs_flv_tellg(ic);
  196. if ((ret = parse_metadata(data, size, &amf0_name, &amf0_data)) != 0) {
  197. return ret;
  198. }
  199. *pname = amf0_name;
  200. *pdata = amf0_data;
  201. if (srs_amf0_is_object(amf0_data)) {
  202. keyframes = srs_amf0_object_property(amf0_data, "keyframes");
  203. if (keyframes == NULL) {
  204. keyframes = srs_amf0_create_object();
  205. srs_amf0_object_property_set(amf0_data, "keyframes", keyframes);
  206. }
  207. // always clear the old keyframes.
  208. srs_amf0_object_clear(keyframes);
  209. *pfilepositions = filepositions = srs_amf0_create_strict_array();
  210. srs_amf0_object_property_set(keyframes, "filepositions", filepositions);
  211. times = srs_amf0_create_strict_array();
  212. srs_amf0_object_property_set(keyframes, "times", times);
  213. } else if (srs_amf0_is_ecma_array(amf0_data)) {
  214. keyframes = srs_amf0_ecma_array_property(amf0_data, "keyframes");
  215. if (keyframes == NULL) {
  216. keyframes = srs_amf0_create_object();
  217. srs_amf0_ecma_array_property_set(amf0_data, "keyframes", keyframes);
  218. }
  219. // always clear the old keyframes.
  220. srs_amf0_object_clear(keyframes);
  221. *pfilepositions = filepositions = srs_amf0_create_strict_array();
  222. srs_amf0_object_property_set(keyframes, "filepositions", filepositions);
  223. times = srs_amf0_create_strict_array();
  224. srs_amf0_object_property_set(keyframes, "times", times);
  225. }
  226. }
  227. free(data);
  228. }
  229. return ret;
  230. }
  231. int do_inject_flv(srs_flv_t ic, srs_flv_t oc, srs_amf0_t amf0_name, srs_amf0_t amf0_data, srs_amf0_t filepositions, int64_t metadata_end_offset)
  232. {
  233. int ret = 0;
  234. // flv header
  235. char header[13];
  236. // packet data
  237. char type;
  238. u_int32_t timestamp = 0;
  239. char* data = NULL;
  240. int32_t size;
  241. // metadata
  242. srs_amf0_t fileposition = NULL;
  243. int amf0_name_size = 0;
  244. int i;
  245. // the metadata end offset, the next tag start offset.
  246. int64_t new_metadata_end_offset = 0;
  247. int offset_adjust = 0;
  248. // reset to write injected file
  249. srs_flv_lseek(ic, 0);
  250. if ((ret = srs_flv_read_header(ic, header)) != 0) {
  251. return ret;
  252. }
  253. if ((ret = srs_flv_write_header(oc, header)) != 0) {
  254. return ret;
  255. }
  256. // write metadata
  257. if (amf0_name != NULL && amf0_data != NULL) {
  258. amf0_name_size = srs_amf0_size(amf0_name);
  259. size = amf0_name_size + srs_amf0_size(amf0_data);
  260. // adjust all offset of keyframes.
  261. new_metadata_end_offset = srs_flv_tellg(oc) + srs_flv_size_tag(size);
  262. // the adjust is new offset sub the old offset of metadata end.
  263. offset_adjust = new_metadata_end_offset - metadata_end_offset;
  264. for (i = 0; i < srs_amf0_strict_array_property_count(filepositions); i++) {
  265. fileposition = srs_amf0_strict_array_property_at(filepositions, i);
  266. srs_amf0_set_number(fileposition, srs_amf0_to_number(fileposition) + offset_adjust);
  267. }
  268. data = (char*)malloc(size);
  269. memset(data, 0, size);
  270. if ((ret = srs_amf0_serialize(amf0_name, data, amf0_name_size)) != 0) {
  271. return ret;
  272. }
  273. if ((ret = srs_amf0_serialize(amf0_data, data + amf0_name_size, size - amf0_name_size)) != 0) {
  274. return ret;
  275. }
  276. if ((ret = srs_flv_write_tag(oc, SRS_RTMP_TYPE_SCRIPT, 0, data, size)) != 0) {
  277. return ret;
  278. }
  279. free(data);
  280. }
  281. srs_human_trace("build keyframe infos from flv");
  282. for (;;) {
  283. // tag header
  284. if ((ret = srs_flv_read_tag_header(ic, &type, &size, &timestamp)) != 0) {
  285. if (srs_flv_is_eof(ret)) {
  286. srs_human_trace("parse completed.");
  287. return 0;
  288. }
  289. srs_human_trace("flv get packet failed. ret=%d", ret);
  290. return ret;
  291. }
  292. if (size <= 0) {
  293. srs_human_trace("invalid size=%d", size);
  294. break;
  295. }
  296. // TODO: FIXME: mem leak when error.
  297. data = (char*)malloc(size);
  298. if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) {
  299. return ret;
  300. }
  301. // data tag
  302. if (type == SRS_RTMP_TYPE_SCRIPT) {
  303. continue;
  304. }
  305. // copy
  306. if ((ret = srs_flv_write_tag(oc, type, timestamp, data, size)) != 0) {
  307. return ret;
  308. }
  309. free(data);
  310. }
  311. return ret;
  312. }