2
0

srs_flv_parser.c 5.2 KB

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