libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
rtpdepacketizerthread.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_NETWORK
13#include <cstdint>
14#include <promeki/atomic.h>
15#include <promeki/duration.h>
16#include <promeki/error.h>
17#include <promeki/namespace.h>
18#include <promeki/queue.h>
19#include <promeki/rtppacket.h>
20#include <promeki/string.h>
21#include <promeki/thread.h>
22#include <promeki/timestamp.h>
23
24PROMEKI_NAMESPACE_BEGIN
25
50struct StreamAnchor {
55 TimeStamp arrivalT0;
56
58 uint32_t rtpTs0 = 0;
59
62 uint32_t clockRate = 0;
63
65 bool valid = false;
66
69 void reset() {
70 *this = StreamAnchor{};
71 }
72
75 bool isValid() const { return valid && clockRate > 0; }
76
87 TimeStamp captureTimeFor(uint32_t rtpTs) const {
88 if (!isValid()) return TimeStamp();
89 const uint32_t delta = rtpTs - rtpTs0;
90 const int64_t ns = (static_cast<int64_t>(delta) * 1'000'000'000) /
91 static_cast<int64_t>(clockRate);
92 return arrivalT0 + Duration::fromNanoseconds(ns);
93 }
94};
95
127class RtpDepacketizerThread : public Thread {
128 public:
144 static constexpr size_t DefaultInputQueueDepth = 64;
145
160 RtpDepacketizerThread(const String &name, uint32_t clockRateHz,
161 size_t depth = DefaultInputQueueDepth);
162
165 ~RtpDepacketizerThread() override;
166
167 RtpDepacketizerThread(const RtpDepacketizerThread &) = delete;
168 RtpDepacketizerThread &operator=(const RtpDepacketizerThread &) = delete;
169
174 RtpPacket::Queue &inputQueue() { return _inputQueue; }
175
177 const RtpPacket::Queue &inputQueue() const { return _inputQueue; }
178
187 void requestStop();
188
191 bool isStopRequested() const { return _stopRequested.value(); }
192
195 uint32_t clockRateHz() const { return _clockRateHz; }
196
201 const StreamAnchor &anchor() const { return _anchor; }
202
203 protected:
217 virtual void handlePacket(const RtpPacket &pkt) = 0;
218
222 virtual void onStart() {}
223
228 virtual void onStop() {}
229
240 void ensureAnchor(uint32_t rtpTs, const TimeStamp &arrivalSteady);
241
244 void resetAnchor();
245
252 TimeStamp captureTimeForRtpTs(uint32_t rtpTs) const;
253
259 static constexpr unsigned int PopTimeoutMs = 50;
260
261 void run() override;
262
263 private:
264 Atomic<bool> _stopRequested;
265 RtpPacket::Queue _inputQueue;
266 uint32_t _clockRateHz;
267 StreamAnchor _anchor;
268};
269
270PROMEKI_NAMESPACE_END
271
272#endif // PROMEKI_ENABLE_NETWORK