| 1 |
/* |
| 2 |
* Copyright (c) 2010-2023 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 "obu-unpacker.h" |
| 22 |
|
| 23 |
#include <stdexcept> |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
|
| 27 |
namespace mediastreamer { |
| 28 |
|
| 29 |
ObuUnpacker::~ObuUnpacker() { |
| 30 |
if (mFrame) freemsg(mFrame); |
| 31 |
} |
| 32 |
|
| 33 |
ObuUnpacker::Status ObuUnpacker::unpack(mblk_t *im, MSQueue *output) { |
| 34 |
uint16_t cseq = mblk_get_cseq(im); |
| 35 |
Status ret = NoFrame; |
| 36 |
|
| 37 |
if (im->b_cont) msgpullup(im, -1); |
| 38 |
|
| 39 |
if (!mInitializedRefCSeq) { |
| 40 |
mInitializedRefCSeq = true; |
| 41 |
mRefCSeq = cseq; |
| 42 |
} else { |
| 43 |
mRefCSeq++; |
| 44 |
if (mRefCSeq != cseq) { |
| 45 |
ms_message("ObuUnpacker: Sequence inconsistency detected (diff=%i)", (int)(cseq - mRefCSeq)); |
| 46 |
mRefCSeq = cseq; |
| 47 |
ret = FrameCorrupted; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
mblk_t *om = feed(im); |
| 52 |
if (om) { |
| 53 |
ret = FrameAvailable; |
| 54 |
ms_queue_put(output, om); |
| 55 |
} |
| 56 |
|
| 57 |
return ret; |
| 58 |
} |
| 59 |
|
| 60 |
void ObuUnpacker::reset() { |
| 61 |
if (mFrame) freemsg(mFrame); |
| 62 |
mFrame = nullptr; |
| 63 |
mInitializedRefCSeq = false; |
| 64 |
} |
| 65 |
|
| 66 |
mblk_t *ObuUnpacker::feed(mblk_t *packet) { |
| 67 |
uint8_t *header = packet->b_rptr; |
| 68 |
bool isFirst = (*header >> 3) & 0x1; |
| 69 |
int marker = mblk_get_marker_info(packet); |
| 70 |
|
| 71 |
// Remove aggregation header |
| 72 |
packet->b_rptr += 1; |
| 73 |
|
| 74 |
if (isAggregating() && isFirst) { |
| 75 |
ms_error("ObuUnpacker: Received the first packet of a video sequence while already aggregating. Dropping the " |
| 76 |
"current frame."); |
| 77 |
if (mFrame) freemsg(mFrame); |
| 78 |
mFrame = packet; |
| 79 |
|
| 80 |
return marker ? completeAggregation() : nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
if (!isAggregating() && !isFirst) { |
| 84 |
ms_error("ObuUnpacker: Received a continuation packet while aggregation is not started. Dropping packet."); |
| 85 |
freemsg(packet); |
| 86 |
|
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
if (!isAggregating()) { |
| 91 |
mFrame = packet; |
| 92 |
} else { |
| 93 |
concatb(mFrame, packet); |
| 94 |
} |
| 95 |
|
| 96 |
return marker ? completeAggregation() : nullptr; |
| 97 |
} |
| 98 |
|
| 99 |
bool ObuUnpacker::isAggregating() const { |
| 100 |
return mFrame != nullptr; |
| 101 |
} |
| 102 |
|
| 103 |
mblk_t *ObuUnpacker::completeAggregation() { |
| 104 |
if (!isAggregating()) return nullptr; |
| 105 |
|
| 106 |
mblk_t *om = mFrame; |
| 107 |
msgpullup(om, -1); |
| 108 |
|
| 109 |
mFrame = nullptr; |
| 110 |
|
| 111 |
return om; |
| 112 |
} |
| 113 |
|
| 114 |
} // namespace mediastreamer |