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:
137 static constexpr size_t DefaultInputQueueDepth = 64;
138
153 RtpDepacketizerThread(const String &name, uint32_t clockRateHz,
154 size_t depth = DefaultInputQueueDepth);
155
158 ~RtpDepacketizerThread() override;
159
160 RtpDepacketizerThread(const RtpDepacketizerThread &) = delete;
161 RtpDepacketizerThread &operator=(const RtpDepacketizerThread &) = delete;
162
167 RtpPacket::Queue &inputQueue() { return _inputQueue; }
168
170 const RtpPacket::Queue &inputQueue() const { return _inputQueue; }
171
180 void requestStop();
181
184 bool isStopRequested() const { return _stopRequested.value(); }
185
188 uint32_t clockRateHz() const { return _clockRateHz; }
189
194 const StreamAnchor &anchor() const { return _anchor; }
195
196 protected:
210 virtual void handlePacket(const RtpPacket &pkt) = 0;
211
215 virtual void onStart() {}
216
221 virtual void onStop() {}
222
233 void ensureAnchor(uint32_t rtpTs, const TimeStamp &arrivalSteady);
234
237 void resetAnchor();
238
245 TimeStamp captureTimeForRtpTs(uint32_t rtpTs) const;
246
252 static constexpr unsigned int PopTimeoutMs = 50;
253
254 void run() override;
255
256 private:
257 Atomic<bool> _stopRequested;
258 RtpPacket::Queue _inputQueue;
259 uint32_t _clockRateHz;
260 StreamAnchor _anchor;
261};
262
263PROMEKI_NAMESPACE_END
264
265#endif // PROMEKI_ENABLE_NETWORK