| 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 <cstdint> |
| 24 |
#include <map> |
| 25 |
#include <memory> |
| 26 |
#include <unordered_map> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "mediastreamer2/msqueue.h" |
| 30 |
|
| 31 |
#include "nal-packer.h" |
| 32 |
#include "nal-unpacker.h" |
| 33 |
|
| 34 |
namespace mediastreamer { |
| 35 |
|
| 36 |
class H26xNaluType { |
| 37 |
public: |
| 38 |
H26xNaluType() = default; |
| 39 |
H26xNaluType(uint8_t value); |
| 40 |
virtual ~H26xNaluType() = default; |
| 41 |
|
| 42 |
operator uint8_t() const { |
| 43 |
return _value; |
| 44 |
} |
| 45 |
|
| 46 |
virtual bool isVcl() const = 0; |
| 47 |
virtual bool isParameterSet() const = 0; |
| 48 |
virtual bool isKeyFramePart() const = 0; |
| 49 |
|
| 50 |
protected: |
| 51 |
uint8_t _value = 0; |
| 52 |
}; |
| 53 |
|
| 54 |
class H26xNaluHeader { |
| 55 |
public: |
| 56 |
H26xNaluHeader() = default; |
| 57 |
virtual ~H26xNaluHeader() = default; |
| 58 |
|
| 59 |
void setFBit(bool val) { |
| 60 |
_fBit = val; |
| 61 |
} |
| 62 |
bool getFBit() const { |
| 63 |
return _fBit; |
| 64 |
} |
| 65 |
|
| 66 |
virtual const H26xNaluType &getAbsType() const = 0; |
| 67 |
|
| 68 |
virtual void parse(const uint8_t *header) = 0; |
| 69 |
virtual mblk_t *forge() const = 0; |
| 70 |
|
| 71 |
protected: |
| 72 |
bool _fBit = false; |
| 73 |
}; |
| 74 |
|
| 75 |
class MS2_PUBLIC H26xUtils { |
| 76 |
public: |
| 77 |
H26xUtils() = delete; |
| 78 |
|
| 79 |
static void naluStreamToNalus(const std::vector<uint8_t> &byteStream, MSQueue *out); |
| 80 |
static void naluStreamToNalus(const uint8_t *byteStream, size_t size, MSQueue *out); |
| 81 |
|
| 82 |
static void |
| 83 |
byteStreamToNalus(const std::vector<uint8_t> &byteStream, MSQueue *out, bool removePreventionBytes = true); |
| 84 |
static void |
| 85 |
byteStreamToNalus(const uint8_t *byteStream, size_t size, MSQueue *out, bool removePreventionBytes = true); |
| 86 |
|
| 87 |
/* Convert nalus to byte stream. If byteStream buffer is not large enough std::invalid_argument is thrown.*/ |
| 88 |
static size_t nalusToByteStream(MSQueue *nalus, uint8_t *byteStream, size_t size); |
| 89 |
|
| 90 |
private: |
| 91 |
static mblk_t * |
| 92 |
makeNalu(const uint8_t *byteStream, size_t naluSize, bool removePreventionBytes, int *preventionBytesRemoved); |
| 93 |
}; |
| 94 |
|
| 95 |
class H26xParameterSetsInserter { |
| 96 |
public: |
| 97 |
virtual ~H26xParameterSetsInserter() = default; |
| 98 |
virtual void process(MSQueue *in, MSQueue *out) = 0; |
| 99 |
virtual void flush() = 0; |
| 100 |
|
| 101 |
protected: |
| 102 |
static void replaceParameterSet(mblk_t *&ps, mblk_t *newPs); |
| 103 |
}; |
| 104 |
|
| 105 |
class H26xParameterSetsStore { |
| 106 |
public: |
| 107 |
virtual ~H26xParameterSetsStore(); |
| 108 |
|
| 109 |
bool hasNewParameters() const { |
| 110 |
return _newParameters; |
| 111 |
} |
| 112 |
void acknowlege() { |
| 113 |
_newParameters = false; |
| 114 |
} |
| 115 |
|
| 116 |
void extractAllPs(MSQueue *frame); |
| 117 |
bool psGatheringCompleted() const; |
| 118 |
void fetchAllPs(MSQueue *outq) const; |
| 119 |
|
| 120 |
protected: |
| 121 |
H26xParameterSetsStore(const std::string &mime, const std::initializer_list<int> &psCodes); |
| 122 |
void addPs(int naluType, mblk_t *nalu); |
| 123 |
|
| 124 |
std::map<int, mblk_t *> _ps; |
| 125 |
std::unique_ptr<H26xNaluHeader> _naluHeader; |
| 126 |
bool _newParameters = false; |
| 127 |
}; |
| 128 |
|
| 129 |
class H26xToolFactory { |
| 130 |
public: |
| 131 |
H26xToolFactory() = default; |
| 132 |
virtual ~H26xToolFactory() = default; |
| 133 |
|
| 134 |
MS2_PUBLIC static const H26xToolFactory &get(const std::string &mime); |
| 135 |
|
| 136 |
virtual H26xNaluHeader *createNaluHeader() const = 0; |
| 137 |
virtual NalPacker *createNalPacker(size_t maxPayloadSize) const = 0; |
| 138 |
virtual NalUnpacker *createNalUnpacker() const = 0; |
| 139 |
virtual H26xParameterSetsInserter *createParameterSetsInserter() const = 0; |
| 140 |
virtual H26xParameterSetsStore *createParameterSetsStore() const = 0; |
| 141 |
|
| 142 |
private: |
| 143 |
static std::unordered_map<std::string, std::unique_ptr<H26xToolFactory>> _instances; |
| 144 |
}; |
| 145 |
|
| 146 |
class TimeReport { |
| 147 |
public: |
| 148 |
TimeReport(const std::string &what, int threshold_ms = 10) : mWhat(what) { |
| 149 |
mCurTime = bctbx_get_cur_time_ms(); |
| 150 |
mThreshold = threshold_ms; |
| 151 |
} |
| 152 |
void finished() { |
| 153 |
uint64_t diff = bctbx_get_cur_time_ms() - mCurTime; |
| 154 |
if ((int)diff >= mThreshold) ms_warning("%s took %i ms !", mWhat.c_str(), (int)diff); |
| 155 |
} |
| 156 |
|
| 157 |
private: |
| 158 |
uint64_t mCurTime; |
| 159 |
int mThreshold; |
| 160 |
std::string mWhat; |
| 161 |
}; |
| 162 |
|
| 163 |
} // namespace mediastreamer |