-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathNvEncoderImpl.cpp
More file actions
633 lines (558 loc) · 23.7 KB
/
NvEncoderImpl.cpp
File metadata and controls
633 lines (558 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include "pch.h"
// todo::
// CMake doesn't support building CUDA kernel with Clang compiler on Windows.
// https://gitlab.kitware.com/cmake/cmake/-/issues/20776
#if !(_WIN32 && __clang__)
#define SUPPORT_CUDA_KERNEL 1
#endif
#include <absl/strings/match.h>
#include <api/video/video_codec_constants.h>
#include <api/video/video_codec_type.h>
#include <common_video/h264/h264_common.h>
#include <media/base/media_constants.h>
#include <modules/video_coding/include/video_codec_interface.h>
#include <modules/video_coding/utility/simulcast_utility.h>
#include "Codec/H264ProfileLevelId.h"
#include "Codec/NvCodec/NvEncoderCudaWithCUarray.h"
#include "GraphicsDevice/Cuda/GpuMemoryBufferCudaHandle.h"
#include "NvEncoder/NvEncoder.h"
#include "NvEncoder/NvEncoderCuda.h"
#include "NvEncoderImpl.h"
#include "ProfilerMarkerFactory.h"
#if SUPPORT_CUDA_KERNEL
#include "ResizeSurf.h"
#endif
#include "ScopedProfiler.h"
#include "UnityVideoTrackSource.h"
#include "VideoFrameAdapter.h"
namespace unity
{
namespace webrtc
{
inline bool operator==(const CUDA_ARRAY_DESCRIPTOR& lhs, const CUDA_ARRAY_DESCRIPTOR& rhs)
{
return lhs.Width == rhs.Width && lhs.Height == rhs.Height && lhs.NumChannels == rhs.NumChannels &&
lhs.Format == rhs.Format;
}
inline bool operator!=(const CUDA_ARRAY_DESCRIPTOR& lhs, const CUDA_ARRAY_DESCRIPTOR& rhs) { return !(lhs == rhs); }
inline absl::optional<webrtc::H264Level> NvEncSupportedLevel(std::vector<SdpVideoFormat>& formats, const GUID& guid)
{
for (const auto& format : formats)
{
const auto profileLevelId = webrtc::ParseSdpForH264ProfileLevelId(format.parameters);
if (!profileLevelId.has_value())
continue;
const auto guid2 = ProfileToGuid(profileLevelId.value().profile);
if (guid2.has_value() && guid == guid2.value())
{
return profileLevelId.value().level;
}
}
return absl::nullopt;
}
inline absl::optional<NV_ENC_LEVEL>
NvEncRequiredLevel(const VideoCodec& codec, std::vector<SdpVideoFormat>& formats, const GUID& guid)
{
auto requiredLevel = unity::webrtc::H264SupportedLevel(
codec.width, codec.height, static_cast<int>(codec.maxFramerate), static_cast<int>(codec.maxBitrate));
if (!requiredLevel)
{
return absl::nullopt;
}
// Check NvEnc supported level.
auto supportedLevel = NvEncSupportedLevel(formats, guid);
if (!supportedLevel)
{
return absl::nullopt;
}
// The supported level must be over the required level.
if (static_cast<int>(requiredLevel.value()) > static_cast<int>(supportedLevel.value()))
{
return absl::nullopt;
}
return static_cast<NV_ENC_LEVEL>(requiredLevel.value());
}
absl::optional<H264Level> NvEncoderImpl::s_maxSupportedH264Level;
std::vector<SdpVideoFormat> NvEncoderImpl::s_formats;
#if SUPPORT_CUDA_KERNEL
CUresult Resize(const CUarray& src, CUarray& dst, const Size& size)
{
CUDA_ARRAY_DESCRIPTOR srcDesc = {};
CUresult result = cuArrayGetDescriptor(&srcDesc, src);
if (result != CUDA_SUCCESS)
{
RTC_LOG(LS_ERROR) << "cuArrayGetDescriptor failed. error:" << result;
return result;
}
CUDA_ARRAY_DESCRIPTOR dstDesc = {};
dstDesc.Format = srcDesc.Format;
dstDesc.NumChannels = srcDesc.NumChannels;
dstDesc.Width = static_cast<size_t>(size.width());
dstDesc.Height = static_cast<size_t>(size.height());
bool create = false;
if (!dst)
{
create = true;
}
else
{
CUDA_ARRAY_DESCRIPTOR desc = {};
result = cuArrayGetDescriptor(&desc, dst);
if (result != CUDA_SUCCESS)
{
RTC_LOG(LS_ERROR) << "cuArrayGetDescriptor failed. error:" << result;
return result;
}
if (desc != dstDesc)
{
result = cuArrayDestroy(dst);
if (result != CUDA_SUCCESS)
{
RTC_LOG(LS_ERROR) << "cuArrayDestroy failed. error:" << result;
return result;
}
dst = nullptr;
create = true;
}
}
if (create)
{
CUresult result = cuArrayCreate(&dst, &dstDesc);
if (result != CUDA_SUCCESS)
{
RTC_LOG(LS_ERROR) << "cuArrayCreate failed. error:" << result;
return result;
}
}
return ResizeSurf(src, dst);
}
#endif
NvEncoderImpl::NvEncoderImpl(
const cricket::VideoCodec& codec,
CUcontext context,
CUmemorytype memoryType,
NV_ENC_BUFFER_FORMAT format,
ProfilerMarkerFactory* profiler)
: m_context(context)
, m_memoryType(memoryType)
, m_scaledArray(nullptr)
, m_encoder(nullptr)
, m_format(format)
, m_encodedCompleteCallback(nullptr)
, m_profiler(profiler)
{
RTC_CHECK(absl::EqualsIgnoreCase(codec.name, cricket::kH264CodecName));
// not implemented for host memory
RTC_CHECK_NE(memoryType, CU_MEMORYTYPE_HOST);
std::string profileLevelIdString;
RTC_CHECK(codec.GetParam(cricket::kH264FmtpProfileLevelId, &profileLevelIdString));
auto profileLevelId = ParseH264ProfileLevelId(profileLevelIdString.c_str());
m_profileGuid = ProfileToGuid(profileLevelId.value().profile).value();
m_level = static_cast<NV_ENC_LEVEL>(profileLevelId.value().level);
m_configurations.reserve(kMaxSimulcastStreams);
if (profiler)
m_marker = profiler->CreateMarker(
"NvEncoderImpl.CopyResource", kUnityProfilerCategoryOther, kUnityProfilerMarkerFlagDefault, 0);
// SupportedNvEncoderCodecs and SupportedMaxH264Level function consume the session of NvEnc and the number of
// the sessions is limited by NVIDIA device. So it caches the return value here.
if (s_formats.empty())
s_formats = SupportedNvEncoderCodecs(m_context);
if (!s_maxSupportedH264Level.has_value())
s_maxSupportedH264Level = SupportedMaxH264Level(m_context);
}
NvEncoderImpl::~NvEncoderImpl() { Release(); }
VideoEncoder::EncoderInfo NvEncoderImpl::GetEncoderInfo() const
{
VideoEncoder::EncoderInfo info;
info.implementation_name = "NvCodec";
info.is_hardware_accelerated = true;
info.supports_native_handle = true;
return info;
}
int NvEncoderImpl::InitEncode(const VideoCodec* codec, const VideoEncoder::Settings& settings)
{
if (!codec || codec->codecType != kVideoCodecH264)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (codec->maxFramerate == 0)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
if (codec->width < 1 || codec->height < 1)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
int number_of_streams = SimulcastUtility::NumberOfSimulcastStreams(*codec);
bool doing_simulcast = (number_of_streams > 1);
if (doing_simulcast && !SimulcastUtility::ValidSimulcastParameters(*codec, number_of_streams))
{
return WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED;
}
m_codec = *codec;
// Check required level.
auto requiredLevel = NvEncRequiredLevel(m_codec, s_formats, m_profileGuid);
if (!requiredLevel)
{
// workaround
// Use supported max framerate that calculated by h264 level define.
m_codec.maxFramerate = static_cast<uint32_t>(
SupportedMaxFramerate(s_maxSupportedH264Level.value(), m_codec.width, m_codec.height));
requiredLevel = NvEncRequiredLevel(m_codec, s_formats, m_profileGuid);
if (!requiredLevel)
{
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
}
// workaround
// Use required level if the profile level is lower than required level.
if (requiredLevel.value() > m_level)
{
m_level = requiredLevel.value();
}
int32_t ret = Release();
if (ret != WEBRTC_VIDEO_CODEC_OK)
{
return ret;
}
m_configurations.resize(number_of_streams);
const CUresult result = cuCtxSetCurrent(m_context);
if (result != CUDA_SUCCESS)
{
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
}
// Some NVIDIA GPUs have a limited Encode Session count.
// We can't get the Session count, so catching NvEncThrow to avoid the crash.
// refer: https://developer.nvidia.com/video-encode-and-decode-gpu-support-matrix-new
try
{
if (m_memoryType == CU_MEMORYTYPE_DEVICE)
{
m_encoder = std::make_unique<NvEncoderCuda>(m_context, m_codec.width, m_codec.height, m_format, 0);
}
else if (m_memoryType == CU_MEMORYTYPE_ARRAY)
{
m_encoder =
std::make_unique<NvEncoderCudaWithCUarray>(m_context, m_codec.width, m_codec.height, m_format, 0);
}
else
{
RTC_DCHECK_NOTREACHED();
}
}
catch (const NVENCException& e)
{
// todo: If Encoder initialization fails, need to notify for Managed side.
RTC_LOG(LS_ERROR) << "Failed Initialize NvEncoder " << e.what();
return WEBRTC_VIDEO_CODEC_ERROR;
}
// todo(kazuki): Add multiple configurations to support simulcast
m_configurations[0].width = m_codec.width;
m_configurations[0].height = m_codec.height;
m_configurations[0].sending = false;
m_configurations[0].max_frame_rate = static_cast<float>(m_codec.maxFramerate);
m_configurations[0].key_frame_interval = m_codec.H264()->keyFrameInterval;
m_configurations[0].max_bps = m_codec.maxBitrate * 1000;
m_configurations[0].target_bps = m_codec.startBitrate * 1000;
m_initializeParams.version = NV_ENC_INITIALIZE_PARAMS_VER;
m_encodeConfig.version = NV_ENC_CONFIG_VER;
m_initializeParams.encodeConfig = &m_encodeConfig;
GUID encodeGuid = NV_ENC_CODEC_H264_GUID;
GUID presetGuid = NV_ENC_PRESET_P4_GUID;
m_encoder->CreateDefaultEncoderParams(
&m_initializeParams, encodeGuid, presetGuid, NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY);
m_initializeParams.frameRateNum = static_cast<uint32_t>(m_configurations[0].max_frame_rate);
m_initializeParams.frameRateDen = 1;
m_encodeConfig.profileGUID = m_profileGuid;
m_encodeConfig.gopLength = NVENC_INFINITE_GOPLENGTH;
m_encodeConfig.frameIntervalP = 1;
m_encodeConfig.encodeCodecConfig.h264Config.level = m_level;
m_encodeConfig.encodeCodecConfig.h264Config.idrPeriod = NVENC_INFINITE_GOPLENGTH;
m_encodeConfig.rcParams.version = NV_ENC_RC_PARAMS_VER;
m_encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
m_encodeConfig.rcParams.averageBitRate = m_configurations[0].target_bps;
m_encodeConfig.rcParams.vbvBufferSize = (m_encodeConfig.rcParams.averageBitRate *
m_initializeParams.frameRateDen / m_initializeParams.frameRateNum) *
5;
m_encodeConfig.rcParams.vbvInitialDelay = m_encodeConfig.rcParams.vbvBufferSize;
try
{
m_encoder->CreateEncoder(&m_initializeParams);
}
catch (const NVENCException& e)
{
RTC_LOG(LS_ERROR) << "Failed Initialize NvEncoder " << e.what();
return WEBRTC_VIDEO_CODEC_ERROR;
}
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t NvEncoderImpl::RegisterEncodeCompleteCallback(EncodedImageCallback* callback)
{
m_encodedCompleteCallback = callback;
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t NvEncoderImpl::Release()
{
if (m_encoder)
{
m_encoder->DestroyEncoder();
m_encoder = nullptr;
}
if (m_scaledArray)
{
cuArrayDestroy(m_scaledArray);
m_scaledArray = nullptr;
}
m_configurations.clear();
return WEBRTC_VIDEO_CODEC_OK;
}
bool NvEncoderImpl::CopyResource(
const NvEncInputFrame* encoderInputFrame,
GpuMemoryBufferInterface* buffer,
Size& size,
CUcontext context,
CUmemorytype memoryType)
{
std::unique_ptr<const ScopedProfiler> profiler;
if (m_profiler)
profiler = m_profiler->CreateScopedProfiler(*m_marker);
const GpuMemoryBufferCudaHandle* handle = static_cast<const GpuMemoryBufferCudaHandle*>(buffer->handle());
if (!handle)
{
RTC_LOG(LS_INFO) << "GpuMemoryBufferCudaHandle is null";
return false;
}
if (memoryType == CU_MEMORYTYPE_DEVICE)
{
NvEncoderCuda::CopyToDeviceFrame(
context,
reinterpret_cast<void*>(handle->mappedPtr),
0,
reinterpret_cast<CUdeviceptr>(encoderInputFrame->inputPtr),
encoderInputFrame->pitch,
size.width(),
size.height(),
CU_MEMORYTYPE_DEVICE,
encoderInputFrame->bufferFormat,
encoderInputFrame->chromaOffsets,
encoderInputFrame->numChromaPlanes);
}
else if (memoryType == CU_MEMORYTYPE_ARRAY)
{
void* pSrcArray = static_cast<void*>(handle->mappedArray);
// Resize cuda array when the resolution of input buffer is different from output one.
// The output buffer named m_scaledArray is reused while the resolution is matched.
#if SUPPORT_CUDA_KERNEL
if (buffer->GetSize() != size)
{
CUresult result = Resize(handle->mappedArray, m_scaledArray, size);
if (result != CUDA_SUCCESS)
{
RTC_LOG(LS_INFO) << "Resize failed. original size=" << buffer->GetSize().width() << ","
<< buffer->GetSize().height() << " output size=" << size.width() << ","
<< size.height();
return false;
}
pSrcArray = static_cast<void*>(m_scaledArray);
}
#endif
NvEncoderCudaWithCUarray::CopyToDeviceFrame(
context,
pSrcArray,
0,
static_cast<CUarray>(encoderInputFrame->inputPtr),
encoderInputFrame->pitch,
size.width(),
size.height(),
CU_MEMORYTYPE_ARRAY,
encoderInputFrame->bufferFormat,
encoderInputFrame->chromaOffsets,
encoderInputFrame->numChromaPlanes);
}
return true;
}
int32_t NvEncoderImpl::Encode(const ::webrtc::VideoFrame& frame, const std::vector<VideoFrameType>* frameTypes)
{
if (!m_encoder)
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
if (!m_encodedCompleteCallback)
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
auto frameBuffer = frame.video_frame_buffer();
if (frameBuffer->type() != VideoFrameBuffer::Type::kNative)
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
auto videoFrameBuffer = static_cast<ScalableBufferInterface*>(frameBuffer.get());
rtc::scoped_refptr<VideoFrame> video_frame = videoFrameBuffer->scaled()
? static_cast<VideoFrameAdapter::ScaledBuffer*>(videoFrameBuffer)->GetVideoFrame()
: static_cast<VideoFrameAdapter*>(videoFrameBuffer)->GetVideoFrame();
if (!video_frame)
{
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
}
bool send_key_frame = false;
if (m_configurations[0].key_frame_request && m_configurations[0].sending)
send_key_frame = true;
if (!send_key_frame && frameTypes)
{
if (m_configurations[0].sending && (*frameTypes)[0] == VideoFrameType::kVideoFrameKey)
{
send_key_frame = true;
}
}
try
{
Size encodeSize(m_encoder->GetEncodeWidth(), m_encoder->GetEncodeHeight());
const NvEncInputFrame* encoderInputFrame = m_encoder->GetNextInputFrame();
// Copy CUDA buffer in VideoFrame to encoderInputFrame.
auto buffer = video_frame->GetGpuMemoryBuffer();
if (!CopyResource(encoderInputFrame, buffer, encodeSize, m_context, m_memoryType))
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
NV_ENC_PIC_PARAMS picParams = NV_ENC_PIC_PARAMS();
picParams.version = NV_ENC_PIC_PARAMS_VER;
picParams.encodePicFlags = 0;
if (send_key_frame)
{
picParams.encodePicFlags =
NV_ENC_PIC_FLAG_FORCEINTRA | NV_ENC_PIC_FLAG_FORCEIDR | NV_ENC_PIC_FLAG_OUTPUT_SPSPPS;
m_configurations[0].key_frame_request = false;
}
std::vector<std::vector<uint8_t>> vPacket;
m_encoder->EncodeFrame(vPacket, &picParams);
for (std::vector<uint8_t>& packet : vPacket)
{
int32_t result = ProcessEncodedFrame(packet, frame);
if (result != WEBRTC_VIDEO_CODEC_OK)
{
return result;
}
}
}
catch (const NVENCException& e)
{
RTC_LOG(LS_ERROR) << "Failed EncodeFrame NvEncoder " << e.what();
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
}
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t NvEncoderImpl::ProcessEncodedFrame(std::vector<uint8_t>& packet, const ::webrtc::VideoFrame& inputFrame)
{
m_encodedImage._encodedWidth = m_encoder->GetEncodeWidth();
m_encodedImage._encodedHeight = m_encoder->GetEncodeHeight();
m_encodedImage.SetTimestamp(inputFrame.timestamp());
m_encodedImage.SetSimulcastIndex(0);
m_encodedImage.ntp_time_ms_ = inputFrame.ntp_time_ms();
m_encodedImage.capture_time_ms_ = inputFrame.render_time_ms();
m_encodedImage.rotation_ = inputFrame.rotation();
m_encodedImage.content_type_ = VideoContentType::UNSPECIFIED;
m_encodedImage.timing_.flags = VideoSendTiming::kInvalid;
m_encodedImage._frameType = VideoFrameType::kVideoFrameDelta;
m_encodedImage.SetColorSpace(inputFrame.color_space());
std::vector<H264::NaluIndex> naluIndices = H264::FindNaluIndices(packet.data(), packet.size());
for (uint32_t i = 0; i < naluIndices.size(); i++)
{
const H264::NaluType naluType = H264::ParseNaluType(packet[naluIndices[i].payload_start_offset]);
if (naluType == H264::kIdr)
{
m_encodedImage._frameType = VideoFrameType::kVideoFrameKey;
break;
}
}
m_encodedImage.SetEncodedData(EncodedImageBuffer::Create(packet.data(), packet.size()));
m_encodedImage.set_size(packet.size());
m_h264BitstreamParser.ParseBitstream(m_encodedImage);
m_encodedImage.qp_ = m_h264BitstreamParser.GetLastSliceQp().value_or(-1);
CodecSpecificInfo codecInfo;
codecInfo.codecType = kVideoCodecH264;
codecInfo.codecSpecific.H264.packetization_mode = H264PacketizationMode::NonInterleaved;
const auto result = m_encodedCompleteCallback->OnEncodedImage(m_encodedImage, &codecInfo);
if (result.error != EncodedImageCallback::Result::OK)
{
RTC_LOG(LS_ERROR) << "Encode m_encodedCompleteCallback failed " << result.error;
return WEBRTC_VIDEO_CODEC_ERROR;
}
return WEBRTC_VIDEO_CODEC_OK;
}
void NvEncoderImpl::SetRates(const RateControlParameters& parameters)
{
if (m_encoder == nullptr)
{
RTC_LOG(LS_WARNING) << "while uninitialized.";
return;
}
if (parameters.framerate_fps < 1.0)
{
RTC_LOG(LS_WARNING) << "Invalid frame rate: " << parameters.framerate_fps;
return;
}
if (parameters.bitrate.get_sum_bps() == 0)
{
RTC_LOG(LS_WARNING) << "Encoder paused, turn off all encoding";
m_configurations[0].SetStreamState(false);
return;
}
m_codec.maxFramerate = static_cast<uint32_t>(parameters.framerate_fps);
m_codec.maxBitrate = parameters.bitrate.GetSpatialLayerSum(0);
// Check required level.
auto requiredLevel = NvEncRequiredLevel(m_codec, s_formats, m_profileGuid);
if (!requiredLevel)
{
// workaround
// Use supported max framerate that calculated by h264 level define.
m_codec.maxFramerate = static_cast<uint32_t>(
SupportedMaxFramerate(s_maxSupportedH264Level.value(), m_codec.width, m_codec.height));
requiredLevel = NvEncRequiredLevel(m_codec, s_formats, m_profileGuid);
if (!requiredLevel)
{
RTC_LOG(LS_WARNING) << "Not supported codec parameter "
<< "width:" << m_codec.width << " "
<< "height:" << m_codec.height << " "
<< "maxFramerate:" << m_codec.maxFramerate;
m_configurations[0].SetStreamState(false);
return;
}
}
// workaround:
// Use required level if the profile level is lower than required level.
if (requiredLevel.value() > m_level)
{
m_level = requiredLevel.value();
}
m_configurations[0].target_bps = m_codec.maxBitrate;
m_configurations[0].max_frame_rate = static_cast<float>(m_codec.maxFramerate);
NV_ENC_RECONFIGURE_PARAMS reconfigureParams = NV_ENC_RECONFIGURE_PARAMS();
reconfigureParams.version = NV_ENC_RECONFIGURE_PARAMS_VER;
std::memcpy(&reconfigureParams.reInitEncodeParams, &m_initializeParams, sizeof(m_initializeParams));
NV_ENC_CONFIG reInitCodecConfig = NV_ENC_CONFIG();
reInitCodecConfig.version = NV_ENC_CONFIG_VER;
std::memcpy(&reInitCodecConfig, m_initializeParams.encodeConfig, sizeof(reInitCodecConfig));
reconfigureParams.reInitEncodeParams.encodeConfig = &reInitCodecConfig;
// Change framerate and bitrate
reconfigureParams.reInitEncodeParams.frameRateNum = static_cast<uint32_t>(m_configurations[0].max_frame_rate);
reInitCodecConfig.encodeCodecConfig.h264Config.level = m_level;
reInitCodecConfig.rcParams.averageBitRate = m_configurations[0].target_bps;
reInitCodecConfig.rcParams.vbvBufferSize =
(reInitCodecConfig.rcParams.averageBitRate * reconfigureParams.reInitEncodeParams.frameRateDen /
reconfigureParams.reInitEncodeParams.frameRateNum) *
5;
reInitCodecConfig.rcParams.vbvInitialDelay = m_encodeConfig.rcParams.vbvBufferSize;
try
{
m_encoder->Reconfigure(&reconfigureParams);
}
catch (const NVENCException& e)
{
RTC_LOG(LS_ERROR) << "Failed Reconfigure NvEncoder " << e.what();
return;
}
// Force send Keyframe
m_configurations[0].SetStreamState(true);
}
void NvEncoderImpl::LayerConfig::SetStreamState(bool sendStream)
{
if (sendStream && !sending)
{
key_frame_request = true;
}
sending = sendStream;
}
} // end namespace webrtc
} // end namespace unity