| 1 |
/* |
| 2 |
* Copyright (c) 2010-2022 Belledonne Communications SARL. |
| 3 |
* |
| 4 |
* This file is part of mediastreamer2 |
| 5 |
* (see https://gitlab.linphone.org/BC/public/mediastreamer2). |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "nal-packer.h" |
| 22 |
|
| 23 |
using namespace std; |
| 24 |
|
| 25 |
namespace mediastreamer { |
| 26 |
|
| 27 |
void NalPacker::NaluAggregatorInterface::setMaxSize(size_t maxSize) { |
| 28 |
if (isAggregating()) { |
| 29 |
throw logic_error("changing payload size while aggregating NALus"); |
| 30 |
} |
| 31 |
_maxSize = maxSize; |
| 32 |
} |
| 33 |
|
| 34 |
NalPacker::NalPacker(NaluAggregatorInterface *naluAggregator, NaluSpliterInterface *naluSpliter, size_t maxPayloadSize) |
| 35 |
: _naluSpliter(naluSpliter), _naluAggregator(naluAggregator) { |
| 36 |
setMaxPayloadSize(maxPayloadSize); |
| 37 |
} |
| 38 |
|
| 39 |
void NalPacker::setMaxPayloadSize(size_t size) { |
| 40 |
_maxSize = size; |
| 41 |
_naluSpliter->setMaxSize(size); |
| 42 |
_naluAggregator->setMaxSize(size); |
| 43 |
ms_message("H26xNalPacker: max payload size set to %zu bytes", size); |
| 44 |
} |
| 45 |
|
| 46 |
void NalPacker::pack(MSQueue *naluq, MSQueue *rtpq, uint32_t ts) { |
| 47 |
switch (_packMode) { |
| 48 |
case SingleNalUnitMode: |
| 49 |
packInSingleNalUnitMode(naluq, rtpq, ts); |
| 50 |
break; |
| 51 |
case NonInterleavedMode: |
| 52 |
packInNonInterleavedMode(naluq, rtpq, ts); |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void NalPacker::flush() { |
| 58 |
_naluAggregator->reset(); |
| 59 |
ms_queue_flush(_naluSpliter->getPackets()); |
| 60 |
} |
| 61 |
|
| 62 |
// Private methods |
| 63 |
void NalPacker::packInSingleNalUnitMode(MSQueue *naluq, MSQueue *rtpq, uint32_t ts) { |
| 64 |
while (mblk_t *m = ms_queue_get(naluq)) { |
| 65 |
bool end = !!ms_queue_empty(naluq); |
| 66 |
size_t size = msgdsize(m); |
| 67 |
if (size > _maxSize) { |
| 68 |
ms_warning("This H264 packet does not fit into MTU: size=%u", static_cast<unsigned int>(size)); |
| 69 |
} |
| 70 |
sendPacket(rtpq, ts, m, end); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void NalPacker::packInNonInterleavedMode(MSQueue *naluq, MSQueue *rtpq, uint32_t ts) { |
| 75 |
while (mblk_t *m = ms_queue_get(naluq)) { |
| 76 |
bool end = !!ms_queue_empty(naluq); |
| 77 |
size_t sz = msgdsize(m); |
| 78 |
if (_aggregationEnabled) { |
| 79 |
if (_naluAggregator->isAggregating()) { |
| 80 |
mblk_t *stapPacket = _naluAggregator->feed(m); |
| 81 |
if (stapPacket) { |
| 82 |
sendPacket(rtpq, ts, stapPacket, false); |
| 83 |
} else continue; |
| 84 |
} |
| 85 |
if (sz < (_maxSize / 2)) { |
| 86 |
_naluAggregator->feed(m); |
| 87 |
} else { |
| 88 |
/*send as single NAL or FU-A*/ |
| 89 |
if (sz > _maxSize) { |
| 90 |
ms_debug("Sending FU-A packets"); |
| 91 |
fragNaluAndSend(rtpq, ts, m, end); |
| 92 |
} else { |
| 93 |
ms_debug("Sending Single NAL"); |
| 94 |
sendPacket(rtpq, ts, m, end); |
| 95 |
} |
| 96 |
} |
| 97 |
} else { |
| 98 |
if (sz > _maxSize) { |
| 99 |
ms_debug("Sending FU-A packets"); |
| 100 |
fragNaluAndSend(rtpq, ts, m, end); |
| 101 |
} else { |
| 102 |
ms_debug("Sending Single NAL"); |
| 103 |
sendPacket(rtpq, ts, m, end); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
if (_naluAggregator->isAggregating()) { |
| 108 |
ms_debug("Sending Single NAL (2)"); |
| 109 |
sendPacket(rtpq, ts, _naluAggregator->completeAggregation(), true); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void NalPacker::fragNaluAndSend(MSQueue *rtpq, uint32_t ts, mblk_t *nalu, bool_t marker) { |
| 114 |
_naluSpliter->feed(nalu); |
| 115 |
MSQueue *nalus = _naluSpliter->getPackets(); |
| 116 |
while (mblk_t *m = ms_queue_get(nalus)) { |
| 117 |
sendPacket(rtpq, ts, m, ms_queue_empty(nalus) ? marker : false); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void NalPacker::sendPacket(MSQueue *rtpq, uint32_t ts, mblk_t *m, bool_t marker) { |
| 122 |
mblk_set_timestamp_info(m, ts); |
| 123 |
mblk_set_marker_info(m, marker); |
| 124 |
mblk_set_cseq(m, _refCSeq++); |
| 125 |
ms_queue_put(rtpq, m); |
| 126 |
} |
| 127 |
|
| 128 |
} // namespace mediastreamer |