test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const time = require('./time.js');
  2. function hexStrToBitStr(str){
  3. // 移除0x 以及空格
  4. str = str.replace(/0x/g,'').replace(/\s/g,'');
  5. // 转换为二进制
  6. return parseInt(str,16).toString(2);
  7. }
  8. function parseRtpHeader(hexStr){
  9. let bitStr = hexStrToBitStr(hexStr);
  10. let version = parseInt(bitStr.substr(0,2),2);
  11. let padding = bitStr.substr(2,1);
  12. let extension = bitStr.substr(3,1);
  13. let csrcCount = bitStr.substr(4,4);
  14. let marker = bitStr.substr(8,1);
  15. let payloadType = parseInt(bitStr.substr(9,7),2);
  16. let sequenceNumber = parseInt(bitStr.substr(16,16),2);
  17. let timestamp = parseInt(bitStr.substr(32,32),2);
  18. // 格式化时间戳
  19. let _time = time.timestr(timestamp )
  20. let ssrc = bitStr.substr(64,32);
  21. let csrc = bitStr.substr(96,32);
  22. return {
  23. version,
  24. padding,
  25. extension,
  26. csrcCount,
  27. marker,
  28. payloadType,
  29. sequenceNumber,
  30. timestamp,
  31. ssrc,
  32. csrc,
  33. time: _time
  34. }
  35. }
  36. console.log(parseInt(hexStrToBitStr('00 ac'),2));
  37. console.log(parseInt(hexStrToBitStr('00 5b'),2));
  38. console.log(parseInt(hexStrToBitStr('00 5d'),2));
  39. console.log(parseInt(hexStrToBitStr('01 e2'),2));
  40. console.log(parseInt(hexStrToBitStr('00 5a'),2));
  41. console.log(parseInt(hexStrToBitStr('00 5a'),2));
  42. console.log(parseInt(hexStrToBitStr('00 5a'),2));
  43. console.log(parseRtpHeader('80 08 00 04 d4 91 5e 08 07 27 11 e8'));
  44. console.log(parseRtpHeader('80 08 00 05 d4 91 5e a8 07 27 11 e8'));
  45. console.log(parseRtpHeader('80 08 00 06 d4 91 5f 48 07 27 11 e8'));
  46. console.log(parseRtpHeader('80 08 00 15 00 41 ad c0 00 00 27 66'));
  47. console.log(parseRtpHeader('80 08 00 17 00 41 af 00 00 00 27 66'));
  48. console.log(parseRtpHeader('80 08 00 18 00 41 af a0 00 00 27 66'));
  49. // console.log(parseRtpHeader('00 04 d4 91 5e 08 07 27 11 e8 d6 d4'));
  50. // console.log(parseRtpHeader('00 05 d4 91 5e a8 07 27 11 e8 d5 57'));
  51. // console.log(parseRtpHeader('00 06 d4 91 5f 48 07 27 11 e8 54 d4'));