forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtcpnackresponder.cpp
114 lines (90 loc) · 3.38 KB
/
rtcpnackresponder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Copyright (c) 2020 Filip Klembara (in2core)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#if RTC_ENABLE_MEDIA
#include "rtcpnackresponder.hpp"
#include "rtp.hpp"
#include "impl/internals.hpp"
#include <cassert>
namespace rtc {
RtcpNackResponder::RtcpNackResponder(size_t maxSize)
: mStorage(std::make_shared<Storage>(maxSize)) {}
void RtcpNackResponder::incoming(message_vector &messages, const message_callback &send) {
for (const auto &message : messages) {
if (message->type != Message::Control)
continue;
size_t p = 0;
while (p + sizeof(RtcpNack) <= message->size()) {
auto nack = reinterpret_cast<RtcpNack *>(message->data() + p);
p += nack->header.header.lengthInBytes();
if (p > message->size())
break;
// check if RTCP is NACK
if (nack->header.header.payloadType() != 205 || nack->header.header.reportCount() != 1)
continue;
unsigned int fieldsCount = nack->getSeqNoCount();
std::vector<uint16_t> missingSequenceNumbers;
for (unsigned int i = 0; i < fieldsCount; i++) {
auto field = nack->parts[i];
auto newMissingSeqenceNumbers = field.getSequenceNumbers();
missingSequenceNumbers.insert(missingSequenceNumbers.end(),
newMissingSeqenceNumbers.begin(),
newMissingSeqenceNumbers.end());
}
for (auto sequenceNumber : missingSequenceNumbers) {
if (auto optPacket = mStorage->get(sequenceNumber))
send(make_message(*optPacket.value()));
}
}
}
}
void RtcpNackResponder::outgoing(message_vector &messages,
[[maybe_unused]] const message_callback &send) {
for (const auto &message : messages)
if (message->type != Message::Control)
mStorage->store(message);
}
RtcpNackResponder::Storage::Element::Element(binary_ptr packet, uint16_t sequenceNumber,
shared_ptr<Element> next)
: packet(packet), sequenceNumber(sequenceNumber), next(next) {}
size_t RtcpNackResponder::Storage::size() { return storage.size(); }
RtcpNackResponder::Storage::Storage(size_t _maxSize) : maxSize(_maxSize) {
assert(maxSize > 0);
storage.reserve(maxSize);
}
optional<binary_ptr> RtcpNackResponder::Storage::get(uint16_t sequenceNumber) {
std::lock_guard lock(mutex);
auto position = storage.find(sequenceNumber);
return position != storage.end() ? std::make_optional(storage.at(sequenceNumber)->packet)
: nullopt;
}
void RtcpNackResponder::Storage::store(binary_ptr packet) {
if (!packet || packet->size() < sizeof(RtpHeader))
return;
auto rtp = reinterpret_cast<RtpHeader *>(packet->data());
auto sequenceNumber = rtp->seqNumber();
std::lock_guard lock(mutex);
assert((storage.empty() && !oldest && !newest) || (!storage.empty() && oldest && newest));
if (size() == 0) {
newest = std::make_shared<Element>(packet, sequenceNumber);
oldest = newest;
} else {
auto current = std::make_shared<Element>(packet, sequenceNumber);
newest->next = current;
newest = current;
}
storage.emplace(sequenceNumber, newest);
if (size() > maxSize) {
assert(oldest);
if (oldest) {
storage.erase(oldest->sequenceNumber);
oldest = oldest->next;
}
}
}
} // namespace rtc
#endif /* RTC_ENABLE_MEDIA */