From 40a4e63b7a76b41935e5a8173ce45fec5a97f494 Mon Sep 17 00:00:00 2001
From: Pierre Lespagnol <pierre.lespagnol@savoirfairelinux.com>
Date: Wed, 28 Aug 2019 17:12:00 -0400
Subject: [PATCH] encoder: fix preset for nvenc

Change-Id: Ie365e0012c80783459d59df9114f78135cd7bfba
---
 src/media/media_encoder.cpp | 30 ++++++++++++++++++++----------
 src/media/media_encoder.h   |  2 +-
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/media/media_encoder.cpp b/src/media/media_encoder.cpp
index d239501056..4849facc7a 100644
--- a/src/media/media_encoder.cpp
+++ b/src/media/media_encoder.cpp
@@ -597,18 +597,28 @@ MediaEncoder::prepareEncoderContext(AVCodec* outputCodec, bool is_video)
 }
 
 void
-MediaEncoder::forcePresetX264_X265(AVCodecContext* encoderCtx)
+MediaEncoder::forcePresetX2645(AVCodecContext* encoderCtx)
 {
+    if(accel_ && accel_->getName() == "nvenc") {
+        if (av_opt_set(encoderCtx, "preset", "fast", AV_OPT_SEARCH_CHILDREN))
+            JAMI_WARN("Failed to set preset to 'fast'");
+        if (av_opt_set(encoderCtx, "level", "auto", AV_OPT_SEARCH_CHILDREN))
+            JAMI_WARN("Failed to set level to 'auto'");
+        if (av_opt_set_int(encoderCtx, "zerolatency", 1, AV_OPT_SEARCH_CHILDREN))
+            JAMI_WARN("Failed to set zerolatency to '1'");
+    }
+    else {
 #if (defined(TARGET_OS_IOS) && TARGET_OS_IOS)
-    const char *speedPreset = "ultrafast";
+        const char *speedPreset = "ultrafast";
 #else
-    const char *speedPreset = "veryfast";
+        const char *speedPreset = "veryfast";
 #endif
-    if (av_opt_set(encoderCtx, "preset", speedPreset, AV_OPT_SEARCH_CHILDREN))
-        JAMI_WARN("Failed to set x264 preset '%s'", speedPreset);
-    const char *tune = "zerolatency";
-    if (av_opt_set(encoderCtx, "tune", tune, AV_OPT_SEARCH_CHILDREN))
-        JAMI_WARN("Failed to set x264 tune '%s'", tune);
+        if (av_opt_set(encoderCtx, "preset", speedPreset, AV_OPT_SEARCH_CHILDREN))
+            JAMI_WARN("Failed to set preset '%s'", speedPreset);
+        const char *tune = "zerolatency";
+        if (av_opt_set(encoderCtx, "tune", tune, AV_OPT_SEARCH_CHILDREN))
+            JAMI_WARN("Failed to set tune '%s'", tune);
+    }
 }
 
 void
@@ -748,11 +758,11 @@ MediaEncoder::initCodec(AVMediaType mediaType, AVCodecID avcodecId, uint64_t br)
     if (avcodecId == AV_CODEC_ID_H264) {
         auto profileLevelId = libav_utils::getDictValue(options_, "parameters");
         extractProfileLevelID(profileLevelId, encoderCtx);
-        forcePresetX264_X265(encoderCtx);
+        forcePresetX2645(encoderCtx);
         initH264(encoderCtx, br);
     } else if (avcodecId == AV_CODEC_ID_HEVC) {
         encoderCtx->profile = FF_PROFILE_HEVC_MAIN;
-        forcePresetX264_X265(encoderCtx);
+        forcePresetX2645(encoderCtx);
         initH265(encoderCtx, br);
     } else if (avcodecId == AV_CODEC_ID_VP8) {
         initVP8(encoderCtx, br);
diff --git a/src/media/media_encoder.h b/src/media/media_encoder.h
index b578a8a673..bba38a3b47 100644
--- a/src/media/media_encoder.h
+++ b/src/media/media_encoder.h
@@ -114,7 +114,7 @@ public:
 private:
     NON_COPYABLE(MediaEncoder);
     AVCodecContext* prepareEncoderContext(AVCodec* outputCodec, bool is_video);
-    void forcePresetX264_X265(AVCodecContext* encoderCtx);
+    void forcePresetX2645(AVCodecContext* encoderCtx);
     void extractProfileLevelID(const std::string &parameters, AVCodecContext *ctx);
     int initStream(const std::string& codecName, AVBufferRef* framesCtx);
     int initStream(const SystemCodecInfo& systemCodecInfo, AVBufferRef* framesCtx);
-- 
GitLab