Common.hpp 2.92 KB
Newer Older
1 2
#ifndef ZLMEDIAKIT_SRT_COMMON_H
#define ZLMEDIAKIT_SRT_COMMON_H
3 4 5
#if defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
6 7

#include <Iphlpapi.h>
8 9
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Iphlpapi.lib")
10 11 12 13 14
#else
#include <netdb.h>
#include <sys/socket.h>
#endif // defined(_WIN32)

15
#include <chrono>
16 17 18
#define MAX_SEQ  0x7fffffff
#define SEQ_NONE 0xffffffff
#define MAX_TS   0xffffffff
19

ziyue committed
20
namespace SRT {
21 22 23 24 25 26
using SteadyClock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<SteadyClock>;

using Microseconds = std::chrono::microseconds;
using Milliseconds = std::chrono::milliseconds;

ziyue committed
27
static inline int64_t DurationCountMicroseconds(SteadyClock::duration dur) {
28 29 30
    return std::chrono::duration_cast<std::chrono::microseconds>(dur).count();
}

ziyue committed
31
static inline uint32_t loadUint32(uint8_t *ptr) {
32 33
    return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
}
ziyue committed
34 35

static inline uint16_t loadUint16(uint8_t *ptr) {
36 37 38
    return ptr[0] << 8 | ptr[1];
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
inline static int64_t seqCmp(uint32_t seq1, uint32_t seq2) {
    if(seq1 > seq2){
        if((seq1 - seq2) >(MAX_SEQ>>1)){
            return (int64_t)seq1 - (int64_t)(seq2+MAX_SEQ);
        }else{
            return (int64_t)seq1 - (int64_t)seq2;
        }
    }else{
        if((seq2-seq1) >(MAX_SEQ>>1)){
            return (int64_t)(seq1+MAX_SEQ) - (int64_t)seq2;
        }else{
            return (int64_t)seq1 - (int64_t)seq2;
        }
    }
}

inline static uint32_t incSeq(int32_t seq) {
    return (seq == MAX_SEQ) ? 0 : seq + 1;
}
ziyue committed
58
static inline void storeUint32(uint8_t *buf, uint32_t val) {
59 60 61 62 63 64
    buf[0] = val >> 24;
    buf[1] = (val >> 16) & 0xff;
    buf[2] = (val >> 8) & 0xff;
    buf[3] = val & 0xff;
}

ziyue committed
65
static inline void storeUint16(uint8_t *buf, uint16_t val) {
66 67 68 69
    buf[0] = (val >> 8) & 0xff;
    buf[1] = val & 0xff;
}

ziyue committed
70
static inline void storeUint32LE(uint8_t *buf, uint32_t val) {
71 72 73
    buf[0] = val & 0xff;
    buf[1] = (val >> 8) & 0xff;
    buf[2] = (val >> 16) & 0xff;
ziyue committed
74
    buf[3] = (val >> 24) & 0xff;
75 76
}

ziyue committed
77
static inline void storeUint16LE(uint8_t *buf, uint16_t val) {
78
    buf[0] = val & 0xff;
ziyue committed
79
    buf[1] = (val >> 8) & 0xff;
80 81
}

ziyue committed
82 83
static inline uint32_t srtVersion(int major, int minor, int patch) {
    return patch + minor * 0x100 + major * 0x10000;
84
}
85 86 87
static inline uint32_t genExpectedSeq(uint32_t seq) {
    return MAX_SEQ & seq;
}
88 89 90

class UTicker {
public:
ziyue committed
91 92
    UTicker() { _created = _begin = SteadyClock::now(); }
    ~UTicker() = default;
93 94 95 96

    /**
     * 获取创建时间,单位微妙
     */
ziyue committed
97
    int64_t elapsedTime(TimePoint now) const { return DurationCountMicroseconds(now - _begin); }
98 99 100 101

    /**
     * 获取上次resetTime后至今的时间,单位毫秒
     */
ziyue committed
102
    int64_t createdTime(TimePoint now) const { return DurationCountMicroseconds(now - _created); }
103 104 105 106

    /**
     * 重置计时器
     */
ziyue committed
107
    void resetTime(TimePoint now) { _begin = now; }
108 109 110 111 112 113

private:
    TimePoint _begin;
    TimePoint _created;
};

114 115
} // namespace SRT

ziyue committed
116
#endif // ZLMEDIAKIT_SRT_COMMON_H