|
10 | 10 |
|
11 | 11 | #include "h264rtpdepacketizer.hpp"
|
12 | 12 | #include "nalunit.hpp"
|
13 |
| -#include "track.hpp" |
14 | 13 |
|
15 |
| -#include "impl/logcounter.hpp" |
16 |
| - |
17 |
| -#include <cmath> |
18 |
| -#include <utility> |
19 |
| - |
20 |
| -#ifdef _WIN32 |
21 |
| -#include <winsock2.h> |
22 |
| -#else |
23 |
| -#include <arpa/inet.h> |
24 |
| -#endif |
| 14 | +#include "impl/internals.hpp" |
25 | 15 |
|
26 | 16 | namespace rtc {
|
27 | 17 |
|
28 |
| -const unsigned long stapaHeaderSize = 1; |
29 |
| -const auto fuaHeaderSize = 2; |
| 18 | +const binary naluLongStartCode = {byte{0}, byte{0}, byte{0}, byte{1}}; |
| 19 | +const binary naluShortStartCode = {byte{0}, byte{0}, byte{1}}; |
30 | 20 |
|
31 | 21 | const uint8_t naluTypeSTAPA = 24;
|
32 | 22 | const uint8_t naluTypeFUA = 28;
|
33 | 23 |
|
| 24 | +H264RtpDepacketizer::H264RtpDepacketizer(Separator separator) : mSeparator(separator) { |
| 25 | + if (separator != Separator::StartSequence && separator != Separator::LongStartSequence && |
| 26 | + separator != Separator::ShortStartSequence) { |
| 27 | + throw std::invalid_argument("Invalid separator"); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void H264RtpDepacketizer::addSeparator(binary &accessUnit) { |
| 32 | + if (mSeparator == Separator::StartSequence || mSeparator == Separator::LongStartSequence) { |
| 33 | + accessUnit.insert(accessUnit.end(), naluLongStartCode.begin(), naluLongStartCode.end()); |
| 34 | + } else if (mSeparator == Separator::ShortStartSequence) { |
| 35 | + accessUnit.insert(accessUnit.end(), naluShortStartCode.begin(), naluShortStartCode.end()); |
| 36 | + } else { |
| 37 | + throw std::invalid_argument("Invalid separator"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
34 | 41 | message_vector H264RtpDepacketizer::buildFrames(message_vector::iterator begin,
|
35 |
| - message_vector::iterator end, uint8_t payloadType, uint32_t timestamp) { |
| 42 | + message_vector::iterator end, uint8_t payloadType, |
| 43 | + uint32_t timestamp) { |
36 | 44 | message_vector out = {};
|
37 |
| - auto fua_buffer = std::vector<std::byte>{}; |
| 45 | + auto accessUnit = binary{}; |
38 | 46 | auto frameInfo = std::make_shared<FrameInfo>(payloadType, timestamp);
|
| 47 | + auto nFrags = 0; |
39 | 48 |
|
40 |
| - for (auto it = begin; it != end; it++) { |
| 49 | + for (auto it = begin; it != end; ++it) { |
41 | 50 | auto pkt = it->get();
|
42 | 51 | auto pktParsed = reinterpret_cast<const rtc::RtpHeader *>(pkt->data());
|
43 |
| - auto headerSize = |
44 |
| - sizeof(rtc::RtpHeader) + pktParsed->csrcCount() + pktParsed->getExtensionHeaderSize(); |
45 |
| - auto paddingSize = 0; |
| 52 | + auto rtpHeaderSize = pktParsed->getSize() + pktParsed->getExtensionHeaderSize(); |
| 53 | + auto rtpPaddingSize = 0; |
46 | 54 |
|
47 | 55 | if (pktParsed->padding()) {
|
48 |
| - paddingSize = std::to_integer<uint8_t>(pkt->at(pkt->size() - 1)); |
| 56 | + rtpPaddingSize = std::to_integer<uint8_t>(pkt->at(pkt->size() - 1)); |
49 | 57 | }
|
50 | 58 |
|
51 |
| - if (pkt->size() == headerSize + paddingSize) { |
| 59 | + if (pkt->size() == rtpHeaderSize + rtpPaddingSize) { |
52 | 60 | PLOG_VERBOSE << "H.264 RTP packet has empty payload";
|
53 | 61 | continue;
|
54 | 62 | }
|
55 | 63 |
|
56 |
| - auto nalUnitHeader = NalUnitHeader{std::to_integer<uint8_t>(pkt->at(headerSize))}; |
| 64 | + auto nalUnitHeader = NalUnitHeader{std::to_integer<uint8_t>(pkt->at(rtpHeaderSize))}; |
57 | 65 |
|
58 |
| - if (fua_buffer.size() != 0 || nalUnitHeader.unitType() == naluTypeFUA) { |
59 |
| - if (fua_buffer.size() == 0) { |
60 |
| - fua_buffer.push_back(std::byte(0)); |
61 |
| - } |
62 |
| - |
63 |
| - auto nalUnitFragmentHeader = |
64 |
| - NalUnitFragmentHeader{std::to_integer<uint8_t>(pkt->at(headerSize + 1))}; |
65 |
| - |
66 |
| - std::copy(pkt->begin() + headerSize + fuaHeaderSize, pkt->end(), |
67 |
| - std::back_inserter(fua_buffer)); |
68 |
| - |
69 |
| - if (nalUnitFragmentHeader.isEnd()) { |
70 |
| - fua_buffer.at(0) = |
71 |
| - std::byte(nalUnitHeader.idc() | nalUnitFragmentHeader.unitType()); |
| 66 | + if (nalUnitHeader.unitType() == naluTypeFUA) { |
| 67 | + auto nalUnitFragmentHeader = NalUnitFragmentHeader{ |
| 68 | + std::to_integer<uint8_t>(pkt->at(rtpHeaderSize + sizeof(NalUnitHeader)))}; |
72 | 69 |
|
73 |
| - out.push_back( |
74 |
| - make_message(std::move(fua_buffer), Message::Binary, 0, nullptr, frameInfo)); |
75 |
| - fua_buffer.clear(); |
| 70 | + if (nFrags++ == 0) { |
| 71 | + addSeparator(accessUnit); |
| 72 | + accessUnit.emplace_back( |
| 73 | + byte(nalUnitHeader.idc() | nalUnitFragmentHeader.unitType())); |
76 | 74 | }
|
| 75 | + |
| 76 | + accessUnit.insert(accessUnit.end(), |
| 77 | + pkt->begin() + rtpHeaderSize + sizeof(NalUnitHeader) + |
| 78 | + sizeof(NalUnitFragmentHeader), |
| 79 | + pkt->end()); |
77 | 80 | } else if (nalUnitHeader.unitType() > 0 && nalUnitHeader.unitType() < 24) {
|
78 |
| - out.push_back(make_message(pkt->begin() + headerSize, pkt->end(), Message::Binary, 0, |
79 |
| - nullptr, frameInfo)); |
| 81 | + addSeparator(accessUnit); |
| 82 | + accessUnit.insert(accessUnit.end(), pkt->begin() + rtpHeaderSize, pkt->end()); |
80 | 83 | } else if (nalUnitHeader.unitType() == naluTypeSTAPA) {
|
81 |
| - auto currOffset = stapaHeaderSize + headerSize; |
| 84 | + auto currOffset = rtpHeaderSize + sizeof(NalUnitHeader); |
82 | 85 |
|
83 |
| - while (currOffset < pkt->size()) { |
84 |
| - auto naluSize = |
85 |
| - uint16_t(pkt->at(currOffset)) << 8 | uint8_t(pkt->at(currOffset + 1)); |
| 86 | + while (currOffset + sizeof(uint16_t) < pkt->size()) { |
| 87 | + auto naluSize = std::to_integer<uint16_t>(pkt->at(currOffset)) << 8 | |
| 88 | + std::to_integer<uint16_t>(pkt->at(currOffset + 1)); |
86 | 89 |
|
87 |
| - currOffset += 2; |
| 90 | + currOffset += sizeof(uint16_t); |
88 | 91 |
|
89 | 92 | if (pkt->size() < currOffset + naluSize) {
|
90 |
| - throw std::runtime_error("STAP-A declared size is larger then buffer"); |
| 93 | + throw std::runtime_error("H264 STAP-A declared size is larger than buffer"); |
91 | 94 | }
|
92 | 95 |
|
93 |
| - out.push_back(make_message(pkt->begin() + currOffset, |
94 |
| - pkt->begin() + currOffset + naluSize, Message::Binary, 0, |
95 |
| - nullptr, frameInfo)); |
| 96 | + addSeparator(accessUnit); |
| 97 | + accessUnit.insert(accessUnit.end(), pkt->begin() + currOffset, |
| 98 | + pkt->begin() + currOffset + naluSize); |
| 99 | + |
96 | 100 | currOffset += naluSize;
|
97 | 101 | }
|
98 | 102 | } else {
|
99 | 103 | throw std::runtime_error("Unknown H264 RTP Packetization");
|
100 | 104 | }
|
101 | 105 | }
|
102 | 106 |
|
| 107 | + if (!accessUnit.empty()) { |
| 108 | + out.emplace_back(make_message(accessUnit.begin(), accessUnit.end(), Message::Binary, 0, |
| 109 | + nullptr, frameInfo)); |
| 110 | + } |
| 111 | + |
103 | 112 | return out;
|
104 | 113 | }
|
105 | 114 |
|
@@ -131,7 +140,8 @@ void H264RtpDepacketizer::incoming(message_vector &messages, const message_callb
|
131 | 140 |
|
132 | 141 | if (current_timestamp == 0) {
|
133 | 142 | current_timestamp = p->timestamp();
|
134 |
| - payload_type = p->payloadType(); // should all be the same for data of the same codec |
| 143 | + payload_type = |
| 144 | + p->payloadType(); // should all be the same for data of the same codec |
135 | 145 | } else if (current_timestamp != p->timestamp()) {
|
136 | 146 | break;
|
137 | 147 | }
|
|
0 commit comments