|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Paul-Louis Ageneau |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | + |
| 9 | +#if RTC_ENABLE_MEDIA |
| 10 | + |
| 11 | +#include "h264rtpdepacketizer.hpp" |
| 12 | +#include "nalunit.hpp" |
| 13 | +#include "track.hpp" |
| 14 | + |
| 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 |
| 25 | + |
| 26 | +namespace rtc { |
| 27 | + |
| 28 | +const unsigned long stapaHeaderSize = 1; |
| 29 | +const auto fuaHeaderSize = 2; |
| 30 | + |
| 31 | +const uint8_t naluTypeSTAPA = 24; |
| 32 | +const uint8_t naluTypeFUA = 28; |
| 33 | + |
| 34 | +message_vector H264RtpDepacketizer::buildFrames(message_vector::iterator begin, |
| 35 | + message_vector::iterator end) { |
| 36 | + message_vector out = {}; |
| 37 | + auto fua_buffer = std::vector<std::byte>{}; |
| 38 | + |
| 39 | + for (auto it = begin; it != end; it++) { |
| 40 | + auto pkt = it->get(); |
| 41 | + auto pktParsed = reinterpret_cast<const rtc::RtpHeader *>(pkt->data()); |
| 42 | + auto headerSize = |
| 43 | + sizeof(rtc::RtpHeader) + pktParsed->csrcCount() + pktParsed->getExtensionHeaderSize(); |
| 44 | + auto nalUnitHeader = NalUnitHeader{std::to_integer<uint8_t>(pkt->at(headerSize))}; |
| 45 | + |
| 46 | + if (fua_buffer.size() != 0 || nalUnitHeader.unitType() == naluTypeFUA) { |
| 47 | + if (fua_buffer.size() == 0) { |
| 48 | + fua_buffer.push_back(std::byte(0)); |
| 49 | + } |
| 50 | + |
| 51 | + auto nalUnitFragmentHeader = |
| 52 | + NalUnitFragmentHeader{std::to_integer<uint8_t>(pkt->at(headerSize + 1))}; |
| 53 | + |
| 54 | + std::copy(pkt->begin() + headerSize + fuaHeaderSize, pkt->end(), |
| 55 | + std::back_inserter(fua_buffer)); |
| 56 | + |
| 57 | + if (nalUnitFragmentHeader.isEnd()) { |
| 58 | + fua_buffer.at(0) = |
| 59 | + std::byte(nalUnitHeader.idc() | nalUnitFragmentHeader.unitType()); |
| 60 | + |
| 61 | + out.push_back(make_message(std::move(fua_buffer))); |
| 62 | + fua_buffer.clear(); |
| 63 | + } |
| 64 | + } else if (nalUnitHeader.unitType() > 0 && nalUnitHeader.unitType() < 24) { |
| 65 | + out.push_back(make_message(pkt->begin() + headerSize, pkt->end())); |
| 66 | + } else if (nalUnitHeader.unitType() == naluTypeSTAPA) { |
| 67 | + auto currOffset = stapaHeaderSize + headerSize; |
| 68 | + |
| 69 | + while (currOffset < pkt->size()) { |
| 70 | + auto naluSize = |
| 71 | + uint16_t(pkt->at(currOffset)) << 8 | uint8_t(pkt->at(currOffset + 1)); |
| 72 | + |
| 73 | + currOffset += 2; |
| 74 | + |
| 75 | + if (pkt->size() < currOffset + naluSize) { |
| 76 | + throw std::runtime_error("STAP-A declared size is larger then buffer"); |
| 77 | + } |
| 78 | + |
| 79 | + out.push_back( |
| 80 | + make_message(pkt->begin() + currOffset, pkt->begin() + currOffset + naluSize)); |
| 81 | + currOffset += naluSize; |
| 82 | + } |
| 83 | + |
| 84 | + } else { |
| 85 | + throw std::runtime_error("Unknown H264 RTP Packetization"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return out; |
| 90 | +} |
| 91 | + |
| 92 | +void H264RtpDepacketizer::incoming(message_vector &messages, const message_callback &) { |
| 93 | + for (auto message : messages) { |
| 94 | + if (message->type == Message::Control) { |
| 95 | + continue; // RTCP |
| 96 | + } |
| 97 | + |
| 98 | + if (message->size() < sizeof(RtpHeader)) { |
| 99 | + PLOG_VERBOSE << "RTP packet is too small, size=" << message->size(); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + mRtpBuffer.push_back(message); |
| 104 | + } |
| 105 | + |
| 106 | + messages.clear(); |
| 107 | + |
| 108 | + while (mRtpBuffer.size() != 0) { |
| 109 | + uint32_t current_timestamp = 0; |
| 110 | + size_t packets_in_timestamp = 0; |
| 111 | + |
| 112 | + for (const auto &pkt : mRtpBuffer) { |
| 113 | + auto p = reinterpret_cast<const rtc::RtpHeader *>(pkt->data()); |
| 114 | + |
| 115 | + if (current_timestamp == 0) { |
| 116 | + current_timestamp = p->timestamp(); |
| 117 | + } else if (current_timestamp != p->timestamp()) { |
| 118 | + break; |
| 119 | + } |
| 120 | + |
| 121 | + packets_in_timestamp++; |
| 122 | + } |
| 123 | + |
| 124 | + if (packets_in_timestamp == mRtpBuffer.size()) { |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + auto begin = mRtpBuffer.begin(); |
| 129 | + auto end = mRtpBuffer.begin() + (packets_in_timestamp - 1); |
| 130 | + |
| 131 | + auto frames = buildFrames(begin, end + 1); |
| 132 | + messages.insert(messages.end(), frames.begin(), frames.end()); |
| 133 | + mRtpBuffer.erase(mRtpBuffer.begin(), mRtpBuffer.begin() + packets_in_timestamp); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace rtc |
| 138 | + |
| 139 | +#endif // RTC_ENABLE_MEDIA |
0 commit comments