|
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) : separator(separator) { |
| 25 | + switch (separator) { |
| 26 | + case Separator::StartSequence: [[fallthrough]]; |
| 27 | + case Separator::LongStartSequence: [[fallthrough]]; |
| 28 | + case Separator::ShortStartSequence: |
| 29 | + break; |
| 30 | + case Separator::Length: [[fallthrough]]; |
| 31 | + default: |
| 32 | + throw std::invalid_argument("Invalid separator"); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void H264RtpDepacketizer::addSeparator(binary& accessUnit) |
| 37 | +{ |
| 38 | + switch (separator) { |
| 39 | + case Separator::StartSequence: [[fallthrough]]; |
| 40 | + case Separator::LongStartSequence: |
| 41 | + accessUnit.insert(accessUnit.end(), |
| 42 | + naluLongStartCode.begin(), |
| 43 | + naluLongStartCode.end()); |
| 44 | + break; |
| 45 | + case Separator::ShortStartSequence: |
| 46 | + accessUnit.insert(accessUnit.end(), |
| 47 | + naluShortStartCode.begin(), |
| 48 | + naluShortStartCode.end()); |
| 49 | + break; |
| 50 | + case Separator::Length: [[fallthrough]]; |
| 51 | + default: |
| 52 | + throw std::invalid_argument("Invalid separator"); |
| 53 | + } |
| 54 | +} |
| 55 | + |
34 | 56 | message_vector H264RtpDepacketizer::buildFrames(message_vector::iterator begin,
|
35 | 57 | message_vector::iterator end, uint32_t timestamp) {
|
36 | 58 | message_vector out = {};
|
37 |
| - auto fua_buffer = std::vector<std::byte>{}; |
| 59 | + auto accessUnit = binary{}; |
38 | 60 | auto frameInfo = std::make_shared<FrameInfo>(timestamp);
|
| 61 | + auto nFrags = 0; |
39 | 62 |
|
40 |
| - for (auto it = begin; it != end; it++) { |
| 63 | + for (auto it = begin; it != end; ++it) { |
41 | 64 | auto pkt = it->get();
|
42 | 65 | auto pktParsed = reinterpret_cast<const rtc::RtpHeader *>(pkt->data());
|
43 |
| - auto headerSize = |
44 |
| - sizeof(rtc::RtpHeader) + pktParsed->csrcCount() + pktParsed->getExtensionHeaderSize(); |
45 |
| - auto paddingSize = 0; |
| 66 | + auto rtpHeaderSize = pktParsed->getSize() + pktParsed->getExtensionHeaderSize(); |
| 67 | + auto rtpPaddingSize = 0; |
46 | 68 |
|
47 | 69 | if (pktParsed->padding()) {
|
48 |
| - paddingSize = std::to_integer<uint8_t>(pkt->at(pkt->size() - 1)); |
| 70 | + rtpPaddingSize = std::to_integer<uint8_t>(pkt->at(pkt->size() - 1)); |
49 | 71 | }
|
50 | 72 |
|
51 |
| - if (pkt->size() == headerSize + paddingSize) { |
| 73 | + if (pkt->size() == rtpHeaderSize + rtpPaddingSize) { |
52 | 74 | PLOG_VERBOSE << "H.264 RTP packet has empty payload";
|
53 | 75 | continue;
|
54 | 76 | }
|
55 | 77 |
|
56 |
| - auto nalUnitHeader = NalUnitHeader{std::to_integer<uint8_t>(pkt->at(headerSize))}; |
| 78 | + auto nalUnitHeader = NalUnitHeader{std::to_integer<uint8_t>(pkt->at(rtpHeaderSize))}; |
57 | 79 |
|
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()); |
| 80 | + if (nalUnitHeader.unitType() == naluTypeFUA) { |
| 81 | + auto nalUnitFragmentHeader = NalUnitFragmentHeader{ |
| 82 | + std::to_integer<uint8_t>(pkt->at(rtpHeaderSize + sizeof(NalUnitHeader)))}; |
72 | 83 |
|
73 |
| - out.push_back( |
74 |
| - make_message(std::move(fua_buffer), Message::Binary, 0, nullptr, frameInfo)); |
75 |
| - fua_buffer.clear(); |
| 84 | + if (nFrags++ == 0) { |
| 85 | + addSeparator(accessUnit); |
| 86 | + accessUnit.emplace_back( |
| 87 | + byte(nalUnitHeader.idc() | nalUnitFragmentHeader.unitType())); |
76 | 88 | }
|
| 89 | + |
| 90 | + accessUnit.insert(accessUnit.end(), |
| 91 | + pkt->begin() + rtpHeaderSize + sizeof(NalUnitHeader) + |
| 92 | + sizeof(NalUnitFragmentHeader), |
| 93 | + pkt->end()); |
77 | 94 | } 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)); |
| 95 | + addSeparator(accessUnit); |
| 96 | + accessUnit.insert(accessUnit.end(), pkt->begin() + rtpHeaderSize, pkt->end()); |
80 | 97 | } else if (nalUnitHeader.unitType() == naluTypeSTAPA) {
|
81 |
| - auto currOffset = stapaHeaderSize + headerSize; |
| 98 | + auto currOffset = rtpHeaderSize + sizeof(NalUnitHeader); |
82 | 99 |
|
83 |
| - while (currOffset < pkt->size()) { |
84 |
| - auto naluSize = |
85 |
| - uint16_t(pkt->at(currOffset)) << 8 | uint8_t(pkt->at(currOffset + 1)); |
| 100 | + while (currOffset + sizeof(uint16_t) < pkt->size()) { |
| 101 | + auto naluSize = std::to_integer<uint16_t>(pkt->at(currOffset)) << 8 | |
| 102 | + std::to_integer<uint16_t>(pkt->at(currOffset + 1)); |
86 | 103 |
|
87 |
| - currOffset += 2; |
| 104 | + currOffset += sizeof(uint16_t); |
88 | 105 |
|
89 | 106 | if (pkt->size() < currOffset + naluSize) {
|
90 |
| - throw std::runtime_error("STAP-A declared size is larger then buffer"); |
| 107 | + throw std::runtime_error("H264 STAP-A declared size is larger than buffer"); |
91 | 108 | }
|
92 | 109 |
|
93 |
| - out.push_back(make_message(pkt->begin() + currOffset, |
94 |
| - pkt->begin() + currOffset + naluSize, Message::Binary, 0, |
95 |
| - nullptr, frameInfo)); |
| 110 | + addSeparator(accessUnit); |
| 111 | + accessUnit.insert(accessUnit.end(), pkt->begin() + currOffset, |
| 112 | + pkt->begin() + currOffset + naluSize); |
| 113 | + |
96 | 114 | currOffset += naluSize;
|
97 | 115 | }
|
98 | 116 | } else {
|
99 | 117 | throw std::runtime_error("Unknown H264 RTP Packetization");
|
100 | 118 | }
|
101 | 119 | }
|
102 | 120 |
|
| 121 | + if (!accessUnit.empty()) { |
| 122 | + out.emplace_back(make_message(accessUnit.begin(), accessUnit.end(), Message::Binary, 0, |
| 123 | + nullptr, frameInfo)); |
| 124 | + } |
| 125 | + |
103 | 126 | return out;
|
104 | 127 | }
|
105 | 128 |
|
|
0 commit comments