| 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 "h26x-utils.h" |
| 22 |
|
| 23 |
#include "h26x-encoder-filter.h" |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
|
| 27 |
namespace mediastreamer { |
| 28 |
|
| 29 |
H26xEncoderFilter::H26xEncoderFilter(MSFilter *f, H26xEncoder *encoder, const MSVideoConfiguration *vconfList) |
| 30 |
: EncoderFilter(f), _encoder(encoder), _vconfList(vconfList) { |
| 31 |
|
| 32 |
_vconf = |
| 33 |
ms_video_find_best_configuration_for_size(_vconfList, MS_VIDEO_SIZE_CIF, ms_factory_get_cpu_count(f->factory)); |
| 34 |
ms_video_starter_init(&_starter); |
| 35 |
|
| 36 |
_packer.reset( |
| 37 |
H26xToolFactory::get(_encoder->getMime()).createNalPacker(ms_factory_get_payload_max_size(f->factory))); |
| 38 |
_packer->setPacketizationMode(NalPacker::NonInterleavedMode); |
| 39 |
_packer->enableAggregation(false); |
| 40 |
} |
| 41 |
|
| 42 |
void H26xEncoderFilter::preprocess() { |
| 43 |
_encoder->start(); |
| 44 |
ms_video_starter_init(&_starter); |
| 45 |
ms_iframe_requests_limiter_init(&_iframeLimiter, 1000); |
| 46 |
} |
| 47 |
|
| 48 |
void H26xEncoderFilter::process() { |
| 49 |
/*First queue input image*/ |
| 50 |
if (mblk_t *im = ms_queue_peek_last(getInput(0))) { |
| 51 |
bool requestIFrame = false; |
| 52 |
if (ms_iframe_requests_limiter_iframe_requested(&_iframeLimiter, getTime()) || |
| 53 |
(!_avpfEnabled && ms_video_starter_need_i_frame(&_starter, getTime()))) { |
| 54 |
ms_message("H26xEncoder: requesting I-frame to the encoder."); |
| 55 |
requestIFrame = true; |
| 56 |
ms_iframe_requests_limiter_notify_iframe_sent(&_iframeLimiter, getTime()); |
| 57 |
} |
| 58 |
_encoder->feed(dupmsg(im), getTime(), requestIFrame); |
| 59 |
} |
| 60 |
ms_queue_flush(getInput(0)); |
| 61 |
|
| 62 |
/*Second, dequeue possibly pending encoded frames*/ |
| 63 |
MSQueue nalus; |
| 64 |
ms_queue_init(&nalus); |
| 65 |
while (_encoder->fetch(&nalus)) { |
| 66 |
if (!_firstFrameDecoded) { |
| 67 |
_firstFrameDecoded = true; |
| 68 |
ms_video_starter_first_frame(&_starter, getTime()); |
| 69 |
} |
| 70 |
_packer->pack(&nalus, getOutput(0), static_cast<uint32_t>(getTime() * 90LL)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void H26xEncoderFilter::postprocess() { |
| 75 |
_packer->flush(); |
| 76 |
_encoder->stop(); |
| 77 |
_firstFrameDecoded = false; |
| 78 |
} |
| 79 |
|
| 80 |
void H26xEncoderFilter::setVideoConfiguration(MSVideoConfiguration vconf) { |
| 81 |
char videoSettingsStr[255]; |
| 82 |
snprintf(videoSettingsStr, sizeof(videoSettingsStr), "bitrate=%db/s, fps=%f, vsize=%dx%d", _vconf.required_bitrate, |
| 83 |
_vconf.fps, _vconf.vsize.width, _vconf.vsize.height); |
| 84 |
try { |
| 85 |
if (_encoder->isRunning()) { |
| 86 |
ms_warning("H26xEncoderFilter: ignoring video size change because the encoder is started"); |
| 87 |
vconf.vsize = _encoder->getVideoSize(); |
| 88 |
} else { |
| 89 |
_encoder->setVideoSize(vconf.vsize); |
| 90 |
} |
| 91 |
_encoder->setFps(vconf.fps); |
| 92 |
_encoder->setBitrate(vconf.required_bitrate); |
| 93 |
_vconf = vconf; |
| 94 |
ms_message("H26xEncoder: video configuration set (%s)", videoSettingsStr); |
| 95 |
} catch (const runtime_error &e) { |
| 96 |
ms_error("H26xEncoder: video configuration failed (%s): %s", videoSettingsStr, e.what()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void H26xEncoderFilter::enableAvpf(bool enable) { |
| 101 |
ms_message("H26xEncoder: AVPF %s", enable ? "enabled" : "disabled"); |
| 102 |
_avpfEnabled = enable; |
| 103 |
} |
| 104 |
|
| 105 |
void H26xEncoderFilter::requestVfu() { |
| 106 |
ms_message("H26xEncoder: VFU requested"); |
| 107 |
ms_iframe_requests_limiter_request_iframe(&_iframeLimiter); |
| 108 |
} |
| 109 |
|
| 110 |
void H26xEncoderFilter::notifyPli() { |
| 111 |
ms_message("H26xEncoder: PLI requested"); |
| 112 |
ms_iframe_requests_limiter_request_iframe(&_iframeLimiter); |
| 113 |
} |
| 114 |
|
| 115 |
void H26xEncoderFilter::notifyFir() { |
| 116 |
ms_message("H26xEncoder: FIR requested"); |
| 117 |
ms_iframe_requests_limiter_request_iframe(&_iframeLimiter); |
| 118 |
} |
| 119 |
|
| 120 |
void H26xEncoderFilter::notifySli() { |
| 121 |
ms_message("H26xEncoder: SLI requested"); |
| 122 |
ms_iframe_requests_limiter_request_iframe(&_iframeLimiter); |
| 123 |
} |
| 124 |
|
| 125 |
const MSVideoConfiguration *H26xEncoderFilter::getVideoConfigurations() const { |
| 126 |
return _vconfList; |
| 127 |
} |
| 128 |
|
| 129 |
} // namespace mediastreamer |