2022-06-03 13:25:32 +08:00
|
|
|
|
#ifndef ZLMEDIAKIT_SRT_COMMON_H
|
|
|
|
|
#define ZLMEDIAKIT_SRT_COMMON_H
|
2022-06-06 20:40:04 +08:00
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#include <ws2tcpip.h>
|
2022-06-06 22:13:53 +08:00
|
|
|
|
#include <Iphlpapi.h>
|
|
|
|
|
#pragma comment (lib, "Ws2_32.lib")
|
|
|
|
|
#pragma comment(lib,"Iphlpapi.lib")
|
2022-06-06 20:40:04 +08:00
|
|
|
|
#else
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#endif // defined(_WIN32)
|
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
#include <chrono>
|
|
|
|
|
|
2022-06-06 20:40:04 +08:00
|
|
|
|
namespace SRT {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
using SteadyClock = std::chrono::steady_clock;
|
|
|
|
|
using TimePoint = std::chrono::time_point<SteadyClock>;
|
|
|
|
|
|
|
|
|
|
using Microseconds = std::chrono::microseconds;
|
|
|
|
|
using Milliseconds = std::chrono::milliseconds;
|
|
|
|
|
|
2022-06-06 20:40:04 +08:00
|
|
|
|
inline int64_t DurationCountMicroseconds(SteadyClock::duration dur) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(dur).count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline uint32_t loadUint32(uint8_t *ptr) {
|
|
|
|
|
return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
|
|
|
|
|
}
|
|
|
|
|
inline uint16_t loadUint16(uint8_t *ptr) {
|
|
|
|
|
return ptr[0] << 8 | ptr[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void storeUint32(uint8_t *buf, uint32_t val) {
|
|
|
|
|
buf[0] = val >> 24;
|
|
|
|
|
buf[1] = (val >> 16) & 0xff;
|
|
|
|
|
buf[2] = (val >> 8) & 0xff;
|
|
|
|
|
buf[3] = val & 0xff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void storeUint16(uint8_t *buf, uint16_t val) {
|
|
|
|
|
buf[0] = (val >> 8) & 0xff;
|
|
|
|
|
buf[1] = val & 0xff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void storeUint32LE(uint8_t *buf, uint32_t val) {
|
|
|
|
|
buf[0] = val & 0xff;
|
|
|
|
|
buf[1] = (val >> 8) & 0xff;
|
|
|
|
|
buf[2] = (val >> 16) & 0xff;
|
2022-06-06 20:40:04 +08:00
|
|
|
|
buf[3] = (val >> 24) & 0xff;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void storeUint16LE(uint8_t *buf, uint16_t val) {
|
|
|
|
|
buf[0] = val & 0xff;
|
2022-06-06 20:40:04 +08:00
|
|
|
|
buf[1] = (val >> 8) & 0xff;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 20:40:04 +08:00
|
|
|
|
inline uint32_t srtVersion(int major, int minor, int patch) {
|
|
|
|
|
return patch + minor * 0x100 + major * 0x10000;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
2022-06-03 20:18:34 +08:00
|
|
|
|
|
|
|
|
|
class UTicker {
|
|
|
|
|
public:
|
2022-06-06 20:40:04 +08:00
|
|
|
|
UTicker() { _created = _begin = SteadyClock::now(); }
|
2022-06-03 20:18:34 +08:00
|
|
|
|
|
2022-06-06 20:40:04 +08:00
|
|
|
|
~UTicker() {}
|
2022-06-03 20:18:34 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取创建时间,单位微妙
|
|
|
|
|
*/
|
2022-06-06 20:40:04 +08:00
|
|
|
|
int64_t elapsedTime(TimePoint now) const { return DurationCountMicroseconds(now - _begin); }
|
2022-06-03 20:18:34 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取上次resetTime后至今的时间,单位毫秒
|
|
|
|
|
*/
|
2022-06-06 20:40:04 +08:00
|
|
|
|
int64_t createdTime(TimePoint now) const { return DurationCountMicroseconds(now - _created); }
|
2022-06-03 20:18:34 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置计时器
|
|
|
|
|
*/
|
2022-06-06 20:40:04 +08:00
|
|
|
|
void resetTime(TimePoint now) { _begin = now; }
|
2022-06-03 20:18:34 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
TimePoint _begin;
|
|
|
|
|
TimePoint _created;
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
} // namespace SRT
|
|
|
|
|
|
2022-06-06 20:40:04 +08:00
|
|
|
|
#endif // ZLMEDIAKIT_SRT_COMMON_H
|