| 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 "h264-utils.h" |
| 22 |
|
| 23 |
#include "h264-nal-packer.h" |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
|
| 27 |
namespace mediastreamer { |
| 28 |
|
| 29 |
// ======================== |
| 30 |
// H264NaluToStapAggregator |
| 31 |
// ======================== |
| 32 |
|
| 33 |
mblk_t *H264NalPacker::NaluAggregator::feed(mblk_t *nalu) { |
| 34 |
size_t size = msgdsize(nalu); |
| 35 |
if (_stap == nullptr) { |
| 36 |
_stap = nalu; |
| 37 |
_size = size + 3; /* STAP-A header + size */ |
| 38 |
} else { |
| 39 |
if ((_size + size) < (_maxSize - 2)) { |
| 40 |
_stap = concatNalus(_stap, nalu); |
| 41 |
_size += (size + 2); /* +2 for the STAP-A size field */ |
| 42 |
} else { |
| 43 |
return completeAggregation(); |
| 44 |
} |
| 45 |
} |
| 46 |
return nullptr; |
| 47 |
} |
| 48 |
|
| 49 |
void H264NalPacker::NaluAggregator::reset() { |
| 50 |
if (_stap) freemsg(_stap); |
| 51 |
_size = 0; |
| 52 |
} |
| 53 |
|
| 54 |
mblk_t *H264NalPacker::NaluAggregator::completeAggregation() { |
| 55 |
mblk_t *res = _stap; |
| 56 |
_stap = nullptr; |
| 57 |
reset(); |
| 58 |
return res; |
| 59 |
} |
| 60 |
|
| 61 |
mblk_t *H264NalPacker::NaluAggregator::concatNalus(mblk_t *m1, mblk_t *m2) { |
| 62 |
mblk_t *l = allocb(2, 0); |
| 63 |
/*eventually append a STAP-A header to m1, if not already done*/ |
| 64 |
if (ms_h264_nalu_get_type(m1) != MSH264NaluTypeSTAPA) { |
| 65 |
m1 = prependStapA(m1); |
| 66 |
} |
| 67 |
putNalSize(l, msgdsize(m2)); |
| 68 |
l->b_cont = m2; |
| 69 |
concatb(m1, l); |
| 70 |
return m1; |
| 71 |
} |
| 72 |
|
| 73 |
mblk_t *H264NalPacker::NaluAggregator::prependStapA(mblk_t *m) { |
| 74 |
mblk_t *hm = allocb(3, 0); |
| 75 |
H264Tools::nalHeaderInit(hm->b_wptr, ms_h264_nalu_get_nri(m), MSH264NaluTypeSTAPA); |
| 76 |
hm->b_wptr += 1; |
| 77 |
putNalSize(hm, msgdsize(m)); |
| 78 |
hm->b_cont = m; |
| 79 |
return hm; |
| 80 |
} |
| 81 |
|
| 82 |
void H264NalPacker::NaluAggregator::putNalSize(mblk_t *m, size_t sz) { |
| 83 |
uint16_t size = htons((uint16_t)sz); |
| 84 |
*(uint16_t *)m->b_wptr = size; |
| 85 |
m->b_wptr += 2; |
| 86 |
} |
| 87 |
|
| 88 |
// ========================= |
| 89 |
// H264NalToFuaSpliter class |
| 90 |
// ========================= |
| 91 |
|
| 92 |
void H264NalPacker::NaluSpliter::feed(mblk_t *nalu) { |
| 93 |
mblk_t *m; |
| 94 |
size_t payload_max_size = _maxSize - 2; /*minus FU-A header*/ |
| 95 |
uint8_t fu_indicator; |
| 96 |
uint8_t type = ms_h264_nalu_get_type(nalu); |
| 97 |
uint8_t nri = ms_h264_nalu_get_nri(nalu); |
| 98 |
bool start = true; |
| 99 |
|
| 100 |
H264Tools::nalHeaderInit(&fu_indicator, nri, MSH264NaluTypeFUA); |
| 101 |
while ((size_t)(nalu->b_wptr - nalu->b_rptr) > payload_max_size) { |
| 102 |
m = dupb(nalu); |
| 103 |
nalu->b_rptr += payload_max_size; |
| 104 |
m->b_wptr = nalu->b_rptr; |
| 105 |
m = H264Tools::prependFuIndicatorAndHeader(m, fu_indicator, start, FALSE, type); |
| 106 |
ms_queue_put(&_q, m); |
| 107 |
start = false; |
| 108 |
} |
| 109 |
/*send last packet */ |
| 110 |
m = H264Tools::prependFuIndicatorAndHeader(nalu, fu_indicator, FALSE, TRUE, type); |
| 111 |
ms_queue_put(&_q, m); |
| 112 |
} |
| 113 |
|
| 114 |
} // namespace mediastreamer |