rtpCheck.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // 读取 rtpPack1.txt 文件,解析其中的 rtp 包,检查其中的序列号是否连续
  2. const path = require("path");
  3. const loadTxt = require("./loadTxt");
  4. const rtp = require("./rtpParse");
  5. function handle(promise){
  6. return promise.then(data=>[null,data]).catch(err=>[err,null])
  7. }
  8. let filePath1 = path.join(__dirname, 'rtpPack1.txt');
  9. let filePath2 = path.join(__dirname, 'rtpPack2.txt');
  10. async function parseRtpPackTxt(filePath) {
  11. let rtpPacks = [];
  12. let [err,lines] = await handle( loadTxt.loadFileLines(filePath) );
  13. if (err){
  14. return console.log(err);
  15. }
  16. // 读取一行,跳过一行
  17. for (let i = 0; i < lines.length; i+=2) {
  18. let line = lines[i];
  19. if (line === "***end***"){
  20. console.log("文件解析结束");
  21. return rtpPacks;
  22. }
  23. rtpPacks.push(line);
  24. }
  25. return rtpPacks;
  26. }
  27. // 格式化输出内容 用于打印表格 例如
  28. // 表单长度 用于打印表格 例如 [20,20] 则再打印表格时,每个单元格的长度为 20
  29. let tbLen = [50, 50];
  30. let lineLen = 2;
  31. function printf() {
  32. let str = "";
  33. str += "|";
  34. for (let i = 0; i < lineLen; i++) {
  35. let arg = arguments[i] || "";
  36. let len = tbLen[i];
  37. let argLen = arg.length;
  38. let spaceLen = len - argLen;
  39. let space = "";
  40. for (let j = 0; j < spaceLen; j++) {
  41. space += " ";
  42. }
  43. //
  44. // 如果长度超过了表单长度,则截取字符串
  45. if (argLen > len){
  46. arg = arg.substr(0,len);
  47. }
  48. str += arg + space + "|";
  49. }
  50. console.log(str);
  51. }
  52. // 解析剩余数据
  53. let last_data = ["",""];
  54. let bitLen = 2;
  55. // rtp over tcp 包的前两个字节为rtp包的长度
  56. function parseRtpOverTcp(no, package){
  57. // 合并旧数据
  58. if(last_data[no] !== ""){
  59. package = last_data[no] + package;
  60. last_data[no] = "";
  61. }
  62. let allLen = package.length;
  63. console.log(`---*-*-*-*-*------ allLen: ${allLen} ------*-*-*-*-*-*---`)
  64. let len = 0;
  65. // 读取包长度
  66. let packageLenStr = package.substr(0,4);
  67. let packageLen = parseInt(packageLenStr,16);
  68. console.log(`packageLenStr: ${packageLenStr} >>> ${packageLen}`);
  69. // package_len + i <= left_len + data_len
  70. while ( (len + packageLen) * 2 <= allLen ){
  71. console.log('----')
  72. // 解析rtp包
  73. let rtpPackage = package.substr(len * 2 + 4, packageLen * bitLen);
  74. let rtpHeaderInfo = rtp.parseRtpHeader(rtpPackage.substr(0,24));
  75. console.log(rtpHeaderInfo)
  76. // i += package_len + 2;
  77. // 读取下一个包长度
  78. len += packageLen + 2;
  79. console.log(`len: ${len}`)
  80. packageLenStr = package.substr(len * 2, 4);
  81. packageLen = parseInt(packageLenStr,16);
  82. console.log(`packageLenStr: ${packageLenStr} >>> ${packageLen} `);
  83. // 如果剩余数据不足以解析一个包,则跳出循环
  84. }
  85. // 读取剩余数据
  86. last_data[no] = package.substr(len * 2 );
  87. console.log(`last_data: ${last_data[no].length}`);
  88. }
  89. async function main(){
  90. let rtpPackages_1 = await parseRtpPackTxt(filePath2);
  91. let rtpPackages_2 = await parseRtpPackTxt(filePath2);
  92. printf(" black"," ok");
  93. printf("------------------------------","------------------------------");
  94. // 开始解析rtp数据
  95. for (let i = 0; i < rtpPackages_1.length; i++) {
  96. console.log(`---*-*-*-*-*------ ${i} ------*-*-*-*-*-*---`)
  97. let rtpPackage_1 = rtpPackages_1[i];
  98. // 输出前10个字节
  99. console.log(rtpPackage_1.substr(0,20));
  100. let rtpPackage_2 = rtpPackages_2[i];
  101. // rtpPackage_1 = rtpPackage_1.substr(4,24);
  102. // rtpPackage_2 = rtpPackage_2.substr(4,24);
  103. // printf(
  104. // ` ${rtpPackage_1}`,
  105. // ` ${rtpPackage_2}`,
  106. // );
  107. let rtpHeaderInfo_1 = parseRtpOverTcp(0, rtpPackage_1);
  108. // let rtpHeaderInfo_2 = rtp.parseRtpOverTcp(1, rtpPackage_2);
  109. let rtpHeaderInfo_2 = {};
  110. // printf(
  111. // `${i} version: ${rtpHeaderInfo_1.version}`,
  112. // `${i} version: ${rtpHeaderInfo_2.version}`,
  113. // );
  114. //
  115. // printf(
  116. // `${i} padding: ${rtpHeaderInfo_1.padding}`,
  117. // `${i} padding: ${rtpHeaderInfo_2.padding}`,
  118. // );
  119. //
  120. // printf(
  121. // `${i} extension: ${rtpHeaderInfo_1.extension}`,
  122. // `${i} extension: ${rtpHeaderInfo_2.extension}`,
  123. // );
  124. //
  125. // printf(
  126. // `${i} csrcCount: ${rtpHeaderInfo_1.csrcCount}`,
  127. // `${i} csrcCount: ${rtpHeaderInfo_2.csrcCount}`,
  128. // );
  129. //
  130. // printf(
  131. // `${i} csrcCount: ${rtpHeaderInfo_1.csrcCount}`,
  132. // `${i} csrcCount: ${rtpHeaderInfo_2.csrcCount}`,
  133. // );
  134. //
  135. // printf(
  136. // `${i} marker: ${rtpHeaderInfo_1.marker}`,
  137. // `${i} marker: ${rtpHeaderInfo_2.marker}`,
  138. // );
  139. //
  140. // printf(
  141. // `${i} payloadType: ${rtpHeaderInfo_1.payloadType}`,
  142. // `${i} payloadType: ${rtpHeaderInfo_2.payloadType}`,
  143. // );
  144. //
  145. // printf(
  146. // `${i} sequenceNumber: ${rtpHeaderInfo_1.sequenceNumber}`,
  147. // `${i} sequenceNumber: ${rtpHeaderInfo_2.sequenceNumber}`,
  148. // );
  149. //
  150. // printf(
  151. // `${i} timestamp: ${rtpHeaderInfo_1.timestamp}`,
  152. // `${i} timestamp: ${rtpHeaderInfo_2.timestamp}`,
  153. // );
  154. //
  155. // printf(
  156. // `${i} ssrc: ${rtpHeaderInfo_1.ssrc}`,
  157. // `${i} ssrc: ${rtpHeaderInfo_2.ssrc}`,
  158. // );
  159. //
  160. // printf(
  161. // `${i} csrc: ${rtpHeaderInfo_1.csrc}`,
  162. // `${i} csrc: ${rtpHeaderInfo_2.csrc}`,
  163. // );
  164. //
  165. // printf(
  166. // `${i} time: ${rtpHeaderInfo_1.time}`,
  167. // `${i} time: ${rtpHeaderInfo_2.time}`,
  168. // );
  169. //
  170. // printf("------------------------------","------------------------------");
  171. // printf("------------------------------","------------------------------");
  172. // printf("------------------------------","------------------------------");
  173. }
  174. }
  175. main()