|
| 1 | +/** |
| 2 | + * Copyright (c) 2023-2024 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 "h265rtpdepacketizer.hpp" |
| 12 | +#include "h265nalunit.hpp" |
| 13 | + |
| 14 | +#include "impl/internals.hpp" |
| 15 | + |
| 16 | +namespace rtc { |
| 17 | + |
| 18 | +const binary naluStartCode = {byte{0}, byte{0}, byte{0}, byte{1}}; |
| 19 | + |
| 20 | +const uint8_t naluTypeAP = 48; |
| 21 | +const uint8_t naluTypeFU = 49; |
| 22 | + |
| 23 | +message_vector H265RtpDepacketizer::buildFrames(message_vector::iterator begin, |
| 24 | + message_vector::iterator end, uint32_t timestamp) { |
| 25 | + message_vector out = {}; |
| 26 | + auto accessUnit = binary{}; |
| 27 | + auto frameInfo = std::make_shared<FrameInfo>(timestamp); |
| 28 | + auto nFrags = 0; |
| 29 | + |
| 30 | + for (auto it = begin; it != end; ++it) { |
| 31 | + auto pkt = it->get(); |
| 32 | + auto pktParsed = reinterpret_cast<const rtc::RtpHeader *>(pkt->data()); |
| 33 | + auto rtpHeaderSize = pktParsed->getSize() + pktParsed->getExtensionHeaderSize(); |
| 34 | + auto nalUnitHeader = |
| 35 | + H265NalUnitHeader{std::to_integer<uint8_t>(pkt->at(rtpHeaderSize)), |
| 36 | + std::to_integer<uint8_t>(pkt->at(rtpHeaderSize + 1))}; |
| 37 | + |
| 38 | + if (nalUnitHeader.unitType() == naluTypeFU) { |
| 39 | + auto nalUnitFragmentHeader = H265NalUnitFragmentHeader{ |
| 40 | + std::to_integer<uint8_t>(pkt->at(rtpHeaderSize + sizeof(H265NalUnitHeader)))}; |
| 41 | + |
| 42 | + if (nFrags++ == 0) { |
| 43 | + std::copy(naluStartCode.begin(), naluStartCode.end(), |
| 44 | + std::back_inserter(accessUnit)); |
| 45 | + |
| 46 | + nalUnitHeader.setUnitType(nalUnitFragmentHeader.unitType()); |
| 47 | + accessUnit.emplace_back(byte(nalUnitHeader._first)); |
| 48 | + accessUnit.emplace_back(byte(nalUnitHeader._second)); |
| 49 | + } |
| 50 | + |
| 51 | + std::copy(pkt->begin() + rtpHeaderSize + sizeof(H265NalUnitHeader) + |
| 52 | + sizeof(H265NalUnitFragmentHeader), |
| 53 | + pkt->end(), std::back_inserter(accessUnit)); |
| 54 | + } else if (nalUnitHeader.unitType() == naluTypeAP) { |
| 55 | + auto currOffset = rtpHeaderSize + sizeof(H265NalUnitHeader); |
| 56 | + |
| 57 | + while (currOffset + sizeof(uint16_t) < pkt->size()) { |
| 58 | + auto naluSize = std::to_integer<uint16_t>(pkt->at(currOffset)) << 8 | |
| 59 | + std::to_integer<uint16_t>(pkt->at(currOffset + 1)); |
| 60 | + |
| 61 | + currOffset += sizeof(uint16_t); |
| 62 | + |
| 63 | + if (pkt->size() < currOffset + naluSize) { |
| 64 | + throw std::runtime_error("H265 AP declared size is larger than buffer"); |
| 65 | + } |
| 66 | + |
| 67 | + std::copy(naluStartCode.begin(), naluStartCode.end(), |
| 68 | + std::back_inserter(accessUnit)); |
| 69 | + |
| 70 | + std::copy(pkt->begin() + currOffset, pkt->begin() + currOffset + naluSize, |
| 71 | + std::back_inserter(accessUnit)); |
| 72 | + |
| 73 | + currOffset += naluSize; |
| 74 | + } |
| 75 | + } else if (nalUnitHeader.unitType() < naluTypeAP) { |
| 76 | + // "NAL units with NAL unit type values in the range of 0 to 47, inclusive, may be |
| 77 | + // passed to the decoder." |
| 78 | + std::copy(naluStartCode.begin(), naluStartCode.end(), std::back_inserter(accessUnit)); |
| 79 | + std::copy(pkt->begin() + rtpHeaderSize, pkt->end(), std::back_inserter(accessUnit)); |
| 80 | + } else { |
| 81 | + // "NAL-unit-like structures with NAL unit type values in the range of 48 to 63, |
| 82 | + // inclusive, MUST NOT be passed to the decoder." |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!accessUnit.empty()) { |
| 87 | + out.emplace_back(make_message(accessUnit.begin(), accessUnit.end(), Message::Binary, 0, |
| 88 | + nullptr, frameInfo)); |
| 89 | + } |
| 90 | + |
| 91 | + return out; |
| 92 | +} |
| 93 | + |
| 94 | +void H265RtpDepacketizer::incoming(message_vector &messages, const message_callback &) { |
| 95 | + messages.erase(std::remove_if(messages.begin(), messages.end(), |
| 96 | + [&](message_ptr message) { |
| 97 | + if (message->type == Message::Control) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + if (message->size() < sizeof(RtpHeader)) { |
| 102 | + PLOG_VERBOSE << "RTP packet is too small, size=" |
| 103 | + << message->size(); |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + mRtpBuffer.push_back(std::move(message)); |
| 108 | + return true; |
| 109 | + }), |
| 110 | + messages.end()); |
| 111 | + |
| 112 | + while (mRtpBuffer.size() != 0) { |
| 113 | + uint32_t current_timestamp = 0; |
| 114 | + size_t packets_in_timestamp = 0; |
| 115 | + |
| 116 | + for (const auto &pkt : mRtpBuffer) { |
| 117 | + auto p = reinterpret_cast<const rtc::RtpHeader *>(pkt->data()); |
| 118 | + |
| 119 | + if (current_timestamp == 0) { |
| 120 | + current_timestamp = p->timestamp(); |
| 121 | + } else if (current_timestamp != p->timestamp()) { |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + packets_in_timestamp++; |
| 126 | + } |
| 127 | + |
| 128 | + if (packets_in_timestamp == mRtpBuffer.size()) { |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + auto begin = mRtpBuffer.begin(); |
| 133 | + auto end = mRtpBuffer.begin() + (packets_in_timestamp - 1); |
| 134 | + |
| 135 | + auto frames = buildFrames(begin, end + 1, current_timestamp); |
| 136 | + messages.insert(messages.end(), frames.begin(), frames.end()); |
| 137 | + mRtpBuffer.erase(mRtpBuffer.begin(), mRtpBuffer.begin() + packets_in_timestamp); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace rtc |
| 142 | + |
| 143 | +#endif // RTC_ENABLE_MEDIA |
0 commit comments