Commit 339f0e62 authored by François Grisez's avatar François Grisez
Browse files

Adds H264NalToFuaSpliter class

parent 8607fdc9
......@@ -28,6 +28,33 @@ using namespace std;
namespace mediastreamer2 {
//=================================================
// H264NalToFuaSpliter class
//=================================================
void H264NalToFuaSpliter::feedNalu(mblk_t *nalu) {
mblk_t *m;
int payload_max_size = _maxsize - 2; /*minus FUA header*/
uint8_t fu_indicator;
uint8_t type = ms_h264_nalu_get_type(nalu);
uint8_t nri = ms_h264_nalu_get_nri(nalu);
bool start = true;
H264Tools::nalHeaderInit(&fu_indicator, nri, MSH264NaluTypeFUA);
while (nalu->b_wptr - nalu->b_rptr > payload_max_size) {
m = dupb(nalu);
nalu->b_rptr += payload_max_size;
m->b_wptr = nalu->b_rptr;
m = H264Tools::prependFuIndicatorAndHeader(m, fu_indicator, start, FALSE, type);
ms_queue_put(&_q, m);
start = false;
}
/*send last packet */
m = H264Tools::prependFuIndicatorAndHeader(nalu, fu_indicator, FALSE, TRUE, type);
ms_queue_put(&_q, m);
}
//=================================================
// Rfc3984Pacer class
//=================================================
......@@ -127,17 +154,17 @@ void Rfc3984Packer::fragNaluAndSend(MSQueue *rtpq, uint32_t ts, mblk_t *nalu, bo
uint8_t nri = ms_h264_nalu_get_nri(nalu);
bool_t start = TRUE;
nalHeaderInit(&fu_indicator, nri, MSH264NaluTypeFUA);
H264Tools::nalHeaderInit(&fu_indicator, nri, MSH264NaluTypeFUA);
while (nalu->b_wptr - nalu->b_rptr > payload_max_size) {
m = dupb(nalu);
nalu->b_rptr += payload_max_size;
m->b_wptr = nalu->b_rptr;
m = prependFuIndicatorAndHeader(m, fu_indicator, start, FALSE, type);
m = H264Tools::prependFuIndicatorAndHeader(m, fu_indicator, start, FALSE, type);
sendPacket(rtpq, ts, m, FALSE);
start = FALSE;
}
/*send last packet */
m = prependFuIndicatorAndHeader(nalu, fu_indicator, FALSE, TRUE, type);
m = H264Tools::prependFuIndicatorAndHeader(nalu, fu_indicator, FALSE, TRUE, type);
sendPacket(rtpq, ts, m, marker);
}
......@@ -163,7 +190,7 @@ mblk_t *Rfc3984Packer::concatNalus(mblk_t *m1, mblk_t *m2) {
mblk_t *Rfc3984Packer::prependStapA(mblk_t *m) {
mblk_t *hm = allocb(3, 0);
nalHeaderInit(hm->b_wptr, ms_h264_nalu_get_nri(m), MSH264NaluTypeSTAPA);
H264Tools::nalHeaderInit(hm->b_wptr, ms_h264_nalu_get_nri(m), MSH264NaluTypeSTAPA);
hm->b_wptr += 1;
putNalSize(hm, msgdsize(m));
hm->b_cont = m;
......@@ -176,16 +203,6 @@ void Rfc3984Packer::putNalSize(mblk_t *m, size_t sz) {
m->b_wptr += 2;
}
mblk_t *Rfc3984Packer::prependFuIndicatorAndHeader(mblk_t *m, uint8_t indicator, bool_t start, bool_t end, uint8_t type) {
mblk_t *h = allocb(2, 0);
h->b_wptr[0] = indicator;
h->b_wptr[1] = ((start & 0x1) << 7) | ((end & 0x1) << 6) | type;
h->b_wptr += 2;
h->b_cont = m;
if (start) m->b_rptr++;/*skip original nalu header */
return h;
}
// ================
// AbstractUnpacker
// ================
......@@ -278,6 +295,19 @@ void Unpacker::storeNal(mblk_t *nal) {
ms_queue_put(&_q, nal);
}
// =======================
// H264Tools class
// =======================
mblk_t *H264Tools::prependFuIndicatorAndHeader(mblk_t *m, uint8_t indicator, bool_t start, bool_t end, uint8_t type) {
mblk_t *h = allocb(2, 0);
h->b_wptr[0] = indicator;
h->b_wptr[1] = ((start & 0x1) << 7) | ((end & 0x1) << 6) | type;
h->b_wptr += 2;
h->b_cont = m;
if (start) m->b_rptr++;/*skip original nalu header */
return h;
}
// =======================
// H264FUAAggregator class
// =======================
......@@ -304,7 +334,7 @@ mblk_t *H264FUAAggregator::feedNalu(mblk_t *im) {
im->b_rptr += 2; /*skip the nal header and the fu header*/
new_header = allocb(1, 0); /* allocate small fragment to put the correct nal header, this is to avoid to write on the buffer
which can break processing of other users of the buffers */
nalHeaderInit(new_header->b_wptr, nri, type);
H264Tools::nalHeaderInit(new_header->b_wptr, nri, type);
new_header->b_wptr++;
mblk_meta_copy(im, new_header);
concatb(new_header, im);
......
......@@ -33,6 +33,34 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
namespace mediastreamer2 {
class NaluAggregatorInterface {
public:
virtual ~NaluAggregatorInterface() = default;
virtual mblk_t *feedNalu(mblk_t *nalu) = 0;
virtual bool isAggregating() const = 0;
virtual void reset() = 0;
};
class NaluSpliterInterface {
public:
virtual ~NaluSpliterInterface() = default;
virtual void feedNalu(mblk_t *nalu) = 0;
virtual MSQueue *getNalus() = 0;
};
class H264NalToFuaSpliter: public NaluSpliterInterface {
public:
H264NalToFuaSpliter(size_t maxsize): _maxsize(maxsize) {ms_queue_init(&_q);}
~H264NalToFuaSpliter() {ms_queue_flush(&_q);}
void feedNalu(mblk_t *nalu) override;
MSQueue *getNalus() override {return &_q;};
private:
size_t _maxsize;
MSQueue _q;
};
class Rfc3984Packer {
public:
enum PacketizationMode {
......@@ -64,9 +92,7 @@ private:
static mblk_t *concatNalus(mblk_t *m1, mblk_t *m2);
static mblk_t *prependStapA(mblk_t *m);
static void nalHeaderInit(uint8_t *h, uint8_t nri, uint8_t type) {*h=((nri&0x3)<<5) | (type & ((1<<5)-1));}
static void putNalSize(mblk_t *m, size_t sz);
static mblk_t *prependFuIndicatorAndHeader(mblk_t *m, uint8_t indicator, bool_t start, bool_t end, uint8_t type);
int _maxSize = MS_DEFAULT_MAX_PAYLOAD_SIZE;
uint16_t _refCSeq = 0;
......@@ -74,21 +100,6 @@ private:
bool _aggregationEnabled = false;
};
class NaluAggregatorInterface {
public:
virtual ~NaluAggregatorInterface() = default;
virtual mblk_t *feedNalu(mblk_t *nalu) = 0;
virtual bool isAggregating() const = 0;
virtual void reset() = 0;
};
class NaluSpliterInterface {
public:
virtual ~NaluSpliterInterface() = default;
virtual void feedNalu(mblk_t *nalu) = 0;
virtual MSQueue *getNalus() = 0;
};
class Unpacker {
public:
class StatusFlag {
......@@ -133,6 +144,12 @@ protected:
std::unique_ptr<NaluSpliterInterface> _naluSpliter;
};
class H264Tools {
public:
static void nalHeaderInit(uint8_t *h, uint8_t nri, uint8_t type) {*h=((nri&0x3)<<5) | (type & ((1<<5)-1));}
static mblk_t *prependFuIndicatorAndHeader(mblk_t *m, uint8_t indicator, bool_t start, bool_t end, uint8_t type);
};
class H264FUAAggregator: public NaluAggregatorInterface {
public:
~H264FUAAggregator() {if (_m) freemsg(_m);}
......@@ -141,8 +158,6 @@ public:
void reset() override;
private:
static void nalHeaderInit(uint8_t *h, uint8_t nri, uint8_t type) {*h=((nri&0x3)<<5) | (type & ((1<<5)-1));}
mblk_t *_m = nullptr;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment