test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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('80 08'),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('80080106b387a72000000194'));
  44. console.log(parseRtpHeader('80080108b387a86000000194'));
  45. console.log(parseRtpHeader('8008010bb387aa4000000194'));
  46. console.log(parseRtpHeader('8008010cb387aae000000194'));
  47. console.log(parseRtpHeader('8008010db387ab8000000194'));
  48. console.log(parseRtpHeader('8008010eb387ac2000000194'));
  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'));