2
0

srs_flv_parser.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <string.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 parse_flv(srs_flv_t flv);
  31. int main(int argc, char** argv)
  32. {
  33. int ret = 0;
  34. // user options.
  35. char* in_flv_file;
  36. // flv handler
  37. srs_flv_t flv;
  38. printf("parse and show flv file detail.\n");
  39. printf("srs(ossrs) client librtmp library.\n");
  40. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  41. if (argc <= 1) {
  42. printf("parse and show flv file detail\n"
  43. "Usage: %s in_flv_file\n"
  44. " in_flv_file flv file to parse and show.\n"
  45. "For example:\n"
  46. " %s doc/source.200kbps.768x320.flv\n"
  47. " %s ../../doc/source.200kbps.768x320.flv\n",
  48. argv[0], argv[0], argv[0]);
  49. exit(-1);
  50. }
  51. in_flv_file = argv[1];
  52. srs_human_trace("input: %s", in_flv_file);
  53. if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
  54. ret = 2;
  55. srs_human_trace("open flv file failed. ret=%d", ret);
  56. return ret;
  57. }
  58. ret = parse_flv(flv);
  59. srs_flv_close(flv);
  60. return ret;
  61. }
  62. void digit_to_char(char* src, int ssize, char* dst, int dsize)
  63. {
  64. int i, j;
  65. char v;
  66. for (i = 0, j = 0; i < ssize && j < dsize; i++) {
  67. if (j >= dsize) {
  68. break;
  69. }
  70. v = (src[i] >> 4) & 0x0F;
  71. if (v < 10) {
  72. dst[j++] = '0' + v;
  73. } else {
  74. dst[j++] = 'A' + (v - 10);
  75. }
  76. if (j >= dsize) {
  77. break;
  78. }
  79. v = src[i] & 0x0F;
  80. if (v < 10) {
  81. dst[j++] = '0' + v;
  82. } else {
  83. dst[j++] = 'A' + (v - 10);
  84. }
  85. if (j >= dsize) {
  86. break;
  87. }
  88. if (i < ssize - 1) {
  89. dst[j++] = ' ';
  90. }
  91. }
  92. }
  93. int parse_bytes(char* data, int size, char* hbuf, int hsize, char* tbuf, int tsize, int print_size)
  94. {
  95. memset(hbuf, 0, hsize);
  96. memset(tbuf, 0, tsize);
  97. if (size > 0) {
  98. digit_to_char(data, size, hbuf, hsize - 1);
  99. }
  100. if (size > print_size * 2) {
  101. digit_to_char(data + size - print_size, size, tbuf, tsize - 1);
  102. }
  103. return 0;
  104. }
  105. int parse_flv(srs_flv_t flv)
  106. {
  107. int ret = 0;
  108. // flv header
  109. char header[13];
  110. // packet data
  111. char type;
  112. u_int32_t timestamp = 0;
  113. char* data = NULL;
  114. int32_t size;
  115. int64_t offset = 0;
  116. if ((ret = srs_flv_read_header(flv, header)) != 0) {
  117. return ret;
  118. }
  119. srs_human_trace("start parse flv");
  120. for (;;) {
  121. offset = srs_flv_tellg(flv);
  122. // tag header
  123. if ((ret = srs_flv_read_tag_header(flv, &type, &size, &timestamp)) != 0) {
  124. if (srs_flv_is_eof(ret)) {
  125. srs_human_trace("parse completed.");
  126. return 0;
  127. }
  128. srs_human_trace("flv get packet failed. ret=%d", ret);
  129. return ret;
  130. }
  131. if (size <= 0) {
  132. srs_human_trace("invalid size=%d", size);
  133. break;
  134. }
  135. data = (char*)malloc(size);
  136. if ((ret = srs_flv_read_tag_data(flv, data, size)) == 0) {
  137. if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) == 0) {
  138. char hbuf[48]; char tbuf[48];
  139. parse_bytes(data, size, hbuf, sizeof(hbuf), tbuf, sizeof(tbuf), 16);
  140. srs_human_raw("offset=%d, first and last 16 bytes:\n"
  141. "[+00, +15] %s\n[-15, EOF] %s\n", (int)offset, hbuf, tbuf);
  142. } else {
  143. srs_human_trace("print packet failed. ret=%d", ret);
  144. }
  145. } else {
  146. srs_human_trace("read flv failed. ret=%d", ret);
  147. }
  148. free(data);
  149. if (ret != 0) {
  150. srs_human_trace("parse failed, ret=%d", ret);
  151. return ret;
  152. }
  153. }
  154. return ret;
  155. }