| 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 |
#pragma once |
| 22 |
|
| 23 |
#include <memory> |
| 24 |
|
| 25 |
#include <ortp/str_utils.h> |
| 26 |
|
| 27 |
#include "mediastreamer2/msqueue.h" |
| 28 |
|
| 29 |
namespace mediastreamer { |
| 30 |
|
| 31 |
class NalPacker { |
| 32 |
public: |
| 33 |
enum PacketizationMode { SingleNalUnitMode, NonInterleavedMode }; |
| 34 |
|
| 35 |
class NaluAggregatorInterface { |
| 36 |
public: |
| 37 |
NaluAggregatorInterface(size_t maxSize) : _maxSize(maxSize) { |
| 38 |
} |
| 39 |
virtual ~NaluAggregatorInterface() = default; |
| 40 |
|
| 41 |
size_t getMaxSize() const { |
| 42 |
return _maxSize; |
| 43 |
} |
| 44 |
void setMaxSize(size_t maxSize); |
| 45 |
|
| 46 |
virtual mblk_t *feed(mblk_t *nalu) = 0; |
| 47 |
virtual bool isAggregating() const = 0; |
| 48 |
virtual void reset() = 0; |
| 49 |
virtual mblk_t *completeAggregation() = 0; |
| 50 |
|
| 51 |
protected: |
| 52 |
size_t _maxSize; |
| 53 |
}; |
| 54 |
|
| 55 |
class NaluSpliterInterface { |
| 56 |
public: |
| 57 |
NaluSpliterInterface(size_t maxSize) : _maxSize(maxSize) { |
| 58 |
ms_queue_init(&_q); |
| 59 |
} |
| 60 |
virtual ~NaluSpliterInterface() { |
| 61 |
ms_queue_flush(&_q); |
| 62 |
} |
| 63 |
|
| 64 |
size_t getMaxSize() const { |
| 65 |
return _maxSize; |
| 66 |
} |
| 67 |
void setMaxSize(size_t maxSize) { |
| 68 |
_maxSize = maxSize; |
| 69 |
} |
| 70 |
|
| 71 |
virtual void feed(mblk_t *nalu) = 0; |
| 72 |
MSQueue *getPackets() { |
| 73 |
return &_q; |
| 74 |
} |
| 75 |
|
| 76 |
protected: |
| 77 |
size_t _maxSize; |
| 78 |
MSQueue _q; |
| 79 |
}; |
| 80 |
|
| 81 |
void setPacketizationMode(PacketizationMode packMode) { |
| 82 |
_packMode = packMode; |
| 83 |
} |
| 84 |
PacketizationMode getPacketizationMode() const { |
| 85 |
return _packMode; |
| 86 |
} |
| 87 |
|
| 88 |
// some stupid phones don't decode STAP-A packets ... |
| 89 |
void enableAggregation(bool yesno) { |
| 90 |
_aggregationEnabled = yesno; |
| 91 |
} |
| 92 |
bool aggregationEnabled() const { |
| 93 |
return _aggregationEnabled; |
| 94 |
} |
| 95 |
|
| 96 |
void setMaxPayloadSize(size_t size); |
| 97 |
size_t getMaxPayloadSize() { |
| 98 |
return _maxSize; |
| 99 |
} |
| 100 |
|
| 101 |
// process NALus and pack them into RTP payloads |
| 102 |
MS2_PUBLIC void pack(MSQueue *naluq, MSQueue *rtpq, uint32_t ts); |
| 103 |
void flush(); |
| 104 |
|
| 105 |
protected: |
| 106 |
NalPacker(NaluAggregatorInterface *naluAggregator, NaluSpliterInterface *naluSpliter, size_t maxPayloadSize); |
| 107 |
|
| 108 |
void packInSingleNalUnitMode(MSQueue *naluq, MSQueue *rtpq, uint32_t ts); |
| 109 |
void packInNonInterleavedMode(MSQueue *naluq, MSQueue *rtpq, uint32_t ts); |
| 110 |
void fragNaluAndSend(MSQueue *rtpq, uint32_t ts, mblk_t *nalu, bool_t marker); |
| 111 |
void sendPacket(MSQueue *rtpq, uint32_t ts, mblk_t *m, bool_t marker); |
| 112 |
|
| 113 |
size_t _maxSize; |
| 114 |
uint16_t _refCSeq = 0; |
| 115 |
PacketizationMode _packMode = SingleNalUnitMode; |
| 116 |
bool _aggregationEnabled = false; |
| 117 |
std::unique_ptr<NaluSpliterInterface> _naluSpliter; |
| 118 |
std::unique_ptr<NaluAggregatorInterface> _naluAggregator; |
| 119 |
}; |
| 120 |
|
| 121 |
}; // namespace mediastreamer |