Replace uses of strcpy() and sprintf()

This commit is contained in:
Cameron Gutman 2023-09-23 11:15:21 -04:00
parent e1e3d8e710
commit f7bb4611e9
5 changed files with 87 additions and 46 deletions

View file

@ -190,13 +190,9 @@ void Session::clConnectionStatusUpdate(int connectionStatus)
switch (connectionStatus)
{
case CONN_STATUS_POOR:
if (s_ActiveSession->m_StreamConfig.bitrate > 5000) {
strcpy(s_ActiveSession->m_OverlayManager.getOverlayText(Overlay::OverlayStatusUpdate), "Slow connection to PC\nReduce your bitrate");
}
else {
strcpy(s_ActiveSession->m_OverlayManager.getOverlayText(Overlay::OverlayStatusUpdate), "Poor connection to PC");
}
s_ActiveSession->m_OverlayManager.setOverlayTextUpdated(Overlay::OverlayStatusUpdate);
s_ActiveSession->m_OverlayManager.updateOverlayText(Overlay::OverlayStatusUpdate,
s_ActiveSession->m_StreamConfig.bitrate > 5000 ?
"Slow connection to PC\nReduce your bitrate" : "Poor connection to PC");
s_ActiveSession->m_OverlayManager.setOverlayState(Overlay::OverlayStatusUpdate, true);
break;
case CONN_STATUS_OKAY:
@ -1288,8 +1284,7 @@ void Session::notifyMouseEmulationMode(bool enabled)
// We re-use the status update overlay for mouse mode notification
if (m_MouseEmulationRefCount > 0) {
strcpy(m_OverlayManager.getOverlayText(Overlay::OverlayStatusUpdate), "Gamepad mouse mode active\nLong press Start to deactivate");
m_OverlayManager.setOverlayTextUpdated(Overlay::OverlayStatusUpdate);
m_OverlayManager.updateOverlayText(Overlay::OverlayStatusUpdate, "Gamepad mouse mode active\nLong press Start to deactivate");
m_OverlayManager.setOverlayState(Overlay::OverlayStatusUpdate, true);
}
else {

View file

@ -604,10 +604,11 @@ void FFmpegVideoDecoder::addVideoStats(VIDEO_STATS& src, VIDEO_STATS& dst)
dst.renderedFps = (float)dst.renderedFrames / ((float)(now - dst.measurementStartTimestamp) / 1000);
}
void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output)
void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output, ssize_t length)
{
int offset = 0;
const char* codecString;
int ret;
// Start with an empty string
output[offset] = 0;
@ -652,42 +653,64 @@ void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output)
if (stats.receivedFps > 0) {
if (m_VideoDecoderCtx != nullptr) {
offset += sprintf(&output[offset],
ret = snprintf(&output[offset],
length - offset,
"Video stream: %dx%d %.2f FPS (Codec: %s)\n",
m_VideoDecoderCtx->width,
m_VideoDecoderCtx->height,
stats.totalFps,
codecString);
if (ret < 0 || ret >= length - offset) {
SDL_assert(false);
return;
}
offset += sprintf(&output[offset],
offset += ret;
}
ret = snprintf(&output[offset],
length - offset,
"Incoming frame rate from network: %.2f FPS\n"
"Decoding frame rate: %.2f FPS\n"
"Rendering frame rate: %.2f FPS\n",
stats.receivedFps,
stats.decodedFps,
stats.renderedFps);
if (ret < 0 || ret >= length - offset) {
SDL_assert(false);
return;
}
offset += ret;
}
if (stats.framesWithHostProcessingLatency > 0) {
offset += sprintf(&output[offset],
ret = snprintf(&output[offset],
length - offset,
"Host processing latency min/max/average: %.1f/%.1f/%.1f ms\n",
(float)stats.minHostProcessingLatency / 10,
(float)stats.maxHostProcessingLatency / 10,
(float)stats.totalHostProcessingLatency / 10 / stats.framesWithHostProcessingLatency);
if (ret < 0 || ret >= length - offset) {
SDL_assert(false);
return;
}
offset += ret;
}
if (stats.renderedFrames != 0) {
char rttString[32];
if (stats.lastRtt != 0) {
sprintf(rttString, "%u ms (variance: %u ms)", stats.lastRtt, stats.lastRttVariance);
snprintf(rttString, sizeof(rttString), "%u ms (variance: %u ms)", stats.lastRtt, stats.lastRttVariance);
}
else {
sprintf(rttString, "N/A");
snprintf(rttString, sizeof(rttString), "N/A");
}
offset += sprintf(&output[offset],
ret = snprintf(&output[offset],
length - offset,
"Frames dropped by your network connection: %.2f%%\n"
"Frames dropped due to network jitter: %.2f%%\n"
"Average network latency: %s\n"
@ -700,6 +723,12 @@ void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output)
(float)stats.totalDecodeTime / stats.decodedFrames,
(float)stats.totalPacerTime / stats.renderedFrames,
(float)stats.totalRenderTime / stats.renderedFrames);
if (ret < 0 || ret >= length - offset) {
SDL_assert(false);
return;
}
offset += ret;
}
}
@ -707,7 +736,7 @@ void FFmpegVideoDecoder::logVideoStats(VIDEO_STATS& stats, const char* title)
{
if (stats.renderedFps > 0 || stats.renderedFrames != 0) {
char videoStatsStr[512];
stringifyVideoStats(stats, videoStatsStr);
stringifyVideoStats(stats, videoStatsStr, sizeof(videoStatsStr));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"%s", title);
@ -1393,7 +1422,9 @@ int FFmpegVideoDecoder::submitDecodeUnit(PDECODE_UNIT du)
addVideoStats(m_LastWndVideoStats, lastTwoWndStats);
addVideoStats(m_ActiveWndVideoStats, lastTwoWndStats);
stringifyVideoStats(lastTwoWndStats, Session::get()->getOverlayManager().getOverlayText(Overlay::OverlayDebug));
stringifyVideoStats(lastTwoWndStats,
Session::get()->getOverlayManager().getOverlayText(Overlay::OverlayDebug),
Session::get()->getOverlayManager().getOverlayMaxTextLength());
Session::get()->getOverlayManager().setOverlayTextUpdated(Overlay::OverlayDebug);
}

View file

@ -32,7 +32,7 @@ public:
private:
bool completeInitialization(const AVCodec* decoder, PDECODER_PARAMETERS params, bool testFrame, bool useAlternateFrontend);
void stringifyVideoStats(VIDEO_STATS& stats, char* output);
void stringifyVideoStats(VIDEO_STATS& stats, char* output, ssize_t length);
void logVideoStats(VIDEO_STATS& stats, const char* title);

View file

@ -58,6 +58,19 @@ char* OverlayManager::getOverlayText(OverlayType type)
return m_Overlays[type].text;
}
void OverlayManager::updateOverlayText(OverlayType type, const char* text)
{
strncpy(m_Overlays[type].text, text, sizeof(m_Overlays[0].text));
m_Overlays[type].text[getOverlayMaxTextLength() - 1] = '\0';
setOverlayTextUpdated(type);
}
int OverlayManager::getOverlayMaxTextLength()
{
return sizeof(m_Overlays[0].text);
}
int OverlayManager::getOverlayFontSize(OverlayType type)
{
return m_Overlays[type].fontSize;

View file

@ -29,6 +29,8 @@ public:
bool isOverlayEnabled(OverlayType type);
char* getOverlayText(OverlayType type);
void updateOverlayText(OverlayType type, const char* text);
int getOverlayMaxTextLength();
void setOverlayTextUpdated(OverlayType type);
void setOverlayState(OverlayType type, bool enabled);
SDL_Color getOverlayColor(OverlayType type);