Skip to content

Commit 091badc

Browse files
authored
fix: Revert changes of SetRates method (#186)
* revert changes of SetRates method * fixed bug * [skip ci] Update plugins
1 parent 3300a1f commit 091badc

File tree

7 files changed

+20
-56
lines changed

7 files changed

+20
-56
lines changed

Plugin~/WebRTCPlugin/Codec/NvCodec/NvEncoder.cpp

+10-27
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ namespace webrtc
9090
nvEncConfig.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
9191
nvEncConfig.gopLength = nvEncInitializeParams.frameRateNum;
9292
nvEncConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
93-
nvEncConfig.rcParams.averageBitRate = (static_cast<unsigned int>(5.0f *
93+
nvEncConfig.rcParams.averageBitRate =
94+
(static_cast<unsigned int>(5.0f *
9495
nvEncInitializeParams.encodeWidth *
9596
nvEncInitializeParams.encodeHeight) / (m_width * m_height)) * 100000;
9697
nvEncConfig.encodeCodecConfig.h264Config.idrPeriod = nvEncConfig.gopLength;
@@ -107,19 +108,18 @@ namespace webrtc
107108
capsParam.capsToQuery = NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT;
108109
int32 asyncMode = 0;
109110
errorCode = pNvEncodeAPI->nvEncGetEncodeCaps(pEncoderInterface, nvEncInitializeParams.encodeGUID, &capsParam, &asyncMode);
110-
checkf(NV_RESULT(errorCode), StringFormat("Failded to get NVEncoder capability params %d", errorCode).c_str());
111+
checkf(NV_RESULT(errorCode), StringFormat("Failed to get NVEncoder capability params %d", errorCode).c_str());
111112
nvEncInitializeParams.enableEncodeAsync = 0;
112113
#pragma endregion
113114
#pragma region initialize hardware encoder session
114115
errorCode = pNvEncodeAPI->nvEncInitializeEncoder(pEncoderInterface, &nvEncInitializeParams);
115116
result = NV_RESULT(errorCode);
116117
checkf(result, StringFormat("Failed to initialize NVEncoder %d", errorCode).c_str());
117118
#pragma endregion
118-
119119
InitEncoderResources();
120120
m_isNvEncoderSupported = true;
121-
122121
}
122+
123123
NvEncoder::~NvEncoder()
124124
{
125125
ReleaseEncoderResources();
@@ -205,7 +205,7 @@ namespace webrtc
205205
{
206206
if (s_hModule)
207207
{
208-
#if _WIN32
208+
#if defined(_WIN32)
209209
FreeLibrary((HMODULE)s_hModule);
210210
#else
211211
dlclose(s_hModule);
@@ -242,10 +242,6 @@ namespace webrtc
242242
}
243243
}
244244

245-
constexpr double kLowRateFactor = 1.0;
246-
constexpr double kHighRateFactor = 2.0;
247-
248-
249245
void NvEncoder::SetRates(uint32_t bitRate, int64_t frameRate)
250246
{
251247
m_frameRate = frameRate;
@@ -269,14 +265,6 @@ namespace webrtc
269265
UpdateSettings();
270266
uint32 bufferIndexToWrite = frameCount % bufferedFrameNum;
271267
Frame& frame = bufferedFrames[bufferIndexToWrite];
272-
#pragma region set frame params
273-
//no free buffer, skip this frame
274-
if (frame.isEncoding)
275-
{
276-
return false;
277-
}
278-
frame.isEncoding = true;
279-
#pragma endregion
280268
#pragma region configure per-frame encode parameters
281269
NV_ENC_PIC_PARAMS picParams = { 0 };
282270
picParams.version = NV_ENC_PIC_PARAMS_VER;
@@ -305,12 +293,6 @@ namespace webrtc
305293
//get encoded frame
306294
void NvEncoder::ProcessEncodedFrame(Frame& frame)
307295
{
308-
//The frame hasn't been encoded, something wrong
309-
if (!frame.isEncoding)
310-
{
311-
return;
312-
}
313-
frame.isEncoding = false;
314296
#pragma region retrieve encoded frame from output buffer
315297
NV_ENC_LOCK_BITSTREAM lockBitStream = { 0 };
316298
lockBitStream.version = NV_ENC_LOCK_BITSTREAM_VER;
@@ -325,14 +307,15 @@ namespace webrtc
325307
}
326308
errorCode = pNvEncodeAPI->nvEncUnlockBitstream(pEncoderInterface, frame.outputFrame);
327309
checkf(NV_RESULT(errorCode), StringFormat("Failed to unlock bit stream, error is %d", errorCode).c_str());
328-
frame.isIdrFrame = lockBitStream.pictureType == NV_ENC_PIC_TYPE_IDR;
329310
#pragma endregion
330-
rtc::scoped_refptr<FrameBuffer> buffer = new rtc::RefCountedObject<FrameBuffer>(m_width, m_height, frame.encodedFrame, m_encoderId);
331-
311+
const rtc::scoped_refptr<FrameBuffer> buffer =
312+
new rtc::RefCountedObject<FrameBuffer>(
313+
m_width, m_height, frame.encodedFrame, m_encoderId);
332314
const int64_t timestamp_us = m_clock->TimeInMicroseconds();
333315
const int64_t now_us = rtc::TimeMicros();
334316
const int64_t translated_camera_time_us =
335-
timestamp_aligner_.TranslateTimestamp(timestamp_us,
317+
timestamp_aligner_.TranslateTimestamp(
318+
timestamp_us,
336319
now_us);
337320

338321
webrtc::VideoFrame::Builder builder =

Plugin~/WebRTCPlugin/Codec/NvCodec/NvEncoder.h

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22
#include <vector>
33
#include <thread>
4-
#include <atomic>
54
#include <rtc_base/timestamp_aligner.h>
65

76
#include "nvEncodeAPI.h"
@@ -30,8 +29,6 @@ namespace webrtc
3029
InputFrame inputFrame = {nullptr, nullptr, NV_ENC_BUFFER_FORMAT_UNDEFINED };
3130
OutputFrame outputFrame = nullptr;
3231
std::vector<uint8> encodedFrame = {};
33-
bool isIdrFrame = false;
34-
std::atomic<bool> isEncoding = { false };
3532
};
3633
public:
3734
NvEncoder(

Plugin~/WebRTCPlugin/DummyVideoEncoder.cpp

+9-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "pch.h"
22
#include "DummyVideoEncoder.h"
3+
#include "modules/video_coding/utility/simulcast_rate_allocator.h"
34

45
namespace unity
56
{
@@ -34,7 +35,13 @@ namespace webrtc
3435
{
3536
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
3637
}
37-
m_codec = codec_settings;
38+
39+
m_codec = *codec_settings;
40+
webrtc::SimulcastRateAllocator init_allocator(m_codec);
41+
webrtc::VideoBitrateAllocation allocation =
42+
init_allocator.Allocate(webrtc::VideoBitrateAllocationParameters(
43+
webrtc::DataRate::KilobitsPerSec(m_codec.startBitrate), m_codec.maxFramerate));
44+
SetRates(RateControlParameters(allocation, m_codec.maxFramerate));
3845

3946
return WEBRTC_VIDEO_CODEC_OK;
4047
}
@@ -125,33 +132,10 @@ namespace webrtc
125132

126133
void DummyVideoEncoder::SetRates(const RateControlParameters& parameters)
127134
{
135+
int64_t frameRate = parameters.framerate_fps;
128136

129-
//
130-
// "parameters.framerate_fps" which the parameter of the argument of
131-
// "SetRates" method, in many cases this parameter is higher than
132-
// the frequency of the encoding.
133-
// Need to determine the right framerate to set to the hardware encoder,
134-
// so collect timestamp of encoding and get stats.
135-
//
136-
int64_t now_ms = m_clock->TimeInMilliseconds();
137-
absl::optional<int64_t> encodeFrameRate = m_encode_fps.Rate(now_ms);
138-
int64_t frameRate = encodeFrameRate.value_or(30);
139-
140-
//
141-
// The bitrate adjuster is using for avoiding overshoot the bitrate.
142-
// But, when a frame rate is low, estimation of bitrate may be low
143-
// so it can not recover video quality.
144-
// If it determine the low frame rate (defined as 15fps),
145-
// use original bps to avoid bitrate undershoot.
146-
//
147137
m_bitrateAdjuster->SetTargetBitrateBps(parameters.bitrate.get_sum_bps());
148138
uint32_t bitRate = m_bitrateAdjuster->GetAdjustedBitrateBps();
149-
const uint32_t kLowFrameRate = 15;
150-
if (frameRate < kLowFrameRate)
151-
{
152-
bitRate = parameters.bitrate.get_sum_bps();
153-
}
154-
155139

156140
m_setRates(m_encoderId, bitRate, frameRate);
157141
}

Plugin~/WebRTCPlugin/DummyVideoEncoder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace webrtc
3737
webrtc::EncodedImage m_encodedImage;
3838
webrtc::RTPFragmentationHeader m_fragHeader;
3939
webrtc::H264BitstreamParser m_h264BitstreamParser;
40-
const webrtc::VideoCodec* m_codec;
40+
webrtc::VideoCodec m_codec;
4141

4242
webrtc::RateStatistics m_encode_fps;
4343
webrtc::Clock* m_clock;

Runtime/Plugins/x86_64/libwebrtc.so

24 Bytes
Binary file not shown.
Binary file not shown.

Runtime/Plugins/x86_64/webrtc.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)