Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

116 wiersze
2.9KB

  1. #ifndef ZLMEDIAKIT_SRT_COMMON_H
  2. #define ZLMEDIAKIT_SRT_COMMON_H
  3. #if defined(_WIN32)
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #include <Iphlpapi.h>
  7. #pragma comment(lib, "Ws2_32.lib")
  8. #pragma comment(lib, "Iphlpapi.lib")
  9. #else
  10. #include <netdb.h>
  11. #include <sys/socket.h>
  12. #endif // defined(_WIN32)
  13. #include <chrono>
  14. #define MAX_SEQ 0x7fffffff
  15. #define SEQ_NONE 0xffffffff
  16. #define MAX_TS 0xffffffff
  17. namespace SRT {
  18. using SteadyClock = std::chrono::steady_clock;
  19. using TimePoint = std::chrono::time_point<SteadyClock>;
  20. using Microseconds = std::chrono::microseconds;
  21. using Milliseconds = std::chrono::milliseconds;
  22. static inline int64_t DurationCountMicroseconds(SteadyClock::duration dur) {
  23. return std::chrono::duration_cast<std::chrono::microseconds>(dur).count();
  24. }
  25. static inline uint32_t loadUint32(uint8_t *ptr) {
  26. return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
  27. }
  28. static inline uint16_t loadUint16(uint8_t *ptr) {
  29. return ptr[0] << 8 | ptr[1];
  30. }
  31. inline static int64_t seqCmp(uint32_t seq1, uint32_t seq2) {
  32. if(seq1 > seq2){
  33. if((seq1 - seq2) >(MAX_SEQ>>1)){
  34. return (int64_t)seq1 - (int64_t)(seq2+MAX_SEQ);
  35. }else{
  36. return (int64_t)seq1 - (int64_t)seq2;
  37. }
  38. }else{
  39. if((seq2-seq1) >(MAX_SEQ>>1)){
  40. return (int64_t)(seq1+MAX_SEQ) - (int64_t)seq2;
  41. }else{
  42. return (int64_t)seq1 - (int64_t)seq2;
  43. }
  44. }
  45. }
  46. inline static uint32_t incSeq(int32_t seq) {
  47. return (seq == MAX_SEQ) ? 0 : seq + 1;
  48. }
  49. static inline void storeUint32(uint8_t *buf, uint32_t val) {
  50. buf[0] = val >> 24;
  51. buf[1] = (val >> 16) & 0xff;
  52. buf[2] = (val >> 8) & 0xff;
  53. buf[3] = val & 0xff;
  54. }
  55. static inline void storeUint16(uint8_t *buf, uint16_t val) {
  56. buf[0] = (val >> 8) & 0xff;
  57. buf[1] = val & 0xff;
  58. }
  59. static inline void storeUint32LE(uint8_t *buf, uint32_t val) {
  60. buf[0] = val & 0xff;
  61. buf[1] = (val >> 8) & 0xff;
  62. buf[2] = (val >> 16) & 0xff;
  63. buf[3] = (val >> 24) & 0xff;
  64. }
  65. static inline void storeUint16LE(uint8_t *buf, uint16_t val) {
  66. buf[0] = val & 0xff;
  67. buf[1] = (val >> 8) & 0xff;
  68. }
  69. static inline uint32_t srtVersion(int major, int minor, int patch) {
  70. return patch + minor * 0x100 + major * 0x10000;
  71. }
  72. static inline uint32_t genExpectedSeq(uint32_t seq) {
  73. return MAX_SEQ & seq;
  74. }
  75. class UTicker {
  76. public:
  77. UTicker() { _created = _begin = SteadyClock::now(); }
  78. ~UTicker() = default;
  79. /**
  80. * 获取创建时间,单位微妙
  81. */
  82. int64_t elapsedTime(TimePoint now) const { return DurationCountMicroseconds(now - _begin); }
  83. /**
  84. * 获取上次resetTime后至今的时间,单位毫秒
  85. */
  86. int64_t createdTime(TimePoint now) const { return DurationCountMicroseconds(now - _created); }
  87. /**
  88. * 重置计时器
  89. */
  90. void resetTime(TimePoint now) { _begin = now; }
  91. private:
  92. TimePoint _begin;
  93. TimePoint _created;
  94. };
  95. } // namespace SRT
  96. #endif // ZLMEDIAKIT_SRT_COMMON_H