From 7a1d95be0d626d3094cb1555f307ab7759d53946 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 19 Jul 2021 16:47:53 -0400
Subject: [PATCH] media filter: cleanup

Change-Id: I72147bfa7fcf06d69a9cd911300ce3dada1f2649
---
 src/media/media_filter.cpp | 24 +++++++++++-------------
 src/media/media_filter.h   |  8 ++++----
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/media/media_filter.cpp b/src/media/media_filter.cpp
index 693e7d7407..0a962d278f 100644
--- a/src/media/media_filter.cpp
+++ b/src/media/media_filter.cpp
@@ -49,7 +49,7 @@ MediaFilter::getFilterDesc() const
 }
 
 int
-MediaFilter::initialize(const std::string& filterDesc, std::vector<MediaStream> msps)
+MediaFilter::initialize(const std::string& filterDesc, const std::vector<MediaStream>& msps)
 {
     int ret = 0;
     desc_ = filterDesc;
@@ -88,18 +88,16 @@ MediaFilter::initialize(const std::string& filterDesc, std::vector<MediaStream>
     for (AVFilterInOut* current = inputs.get(); current; current = current->next) {
         if (!current->name)
             return fail("Filters require non empty names", AVERROR(EINVAL));
-        std::string name = current->name;
-        const auto& it = std::find_if(msps.begin(), msps.end(), [name](const MediaStream& msp) {
+        std::string_view name = current->name;
+        auto it = std::find_if(msps.begin(), msps.end(), [name](const MediaStream& msp) {
             return msp.name == name;
         });
         if (it != msps.end()) {
             if ((ret = initInputFilter(current, *it)) < 0) {
-                std::string msg = "Failed to initialize input: " + name;
-                return fail(msg, ret);
+                return fail(fmt::format("Failed to initialize input: {}", name), ret);
             }
         } else {
-            std::string msg = "Failed to find matching parameters for: " + name;
-            return fail(msg, ret);
+            return fail(fmt::format("Failed to find matching parameters for: {}", name), ret);
         }
     }
 
@@ -111,10 +109,10 @@ MediaFilter::initialize(const std::string& filterDesc, std::vector<MediaStream>
     return 0;
 }
 
-MediaStream
+const MediaStream&
 MediaFilter::getInputParams(const std::string& inputName) const
 {
-    for (auto ms : inputParams_)
+    for (const auto& ms : inputParams_)
         if (ms.name == inputName)
             return ms;
     return {};
@@ -265,7 +263,7 @@ MediaFilter::initOutputFilter(AVFilterInOut* out)
 }
 
 int
-MediaFilter::initInputFilter(AVFilterInOut* in, MediaStream msp)
+MediaFilter::initInputFilter(AVFilterInOut* in, const MediaStream& msp)
 {
     int ret = 0;
     AVBufferSrcParameters* params = av_buffersrc_parameters_alloc();
@@ -309,8 +307,8 @@ MediaFilter::initInputFilter(AVFilterInOut* in, MediaStream msp)
         return fail("Failed to link buffer source to graph", ret);
 
     inputs_.push_back(buffersrcCtx);
-    msp.name = in->name;
-    inputParams_.push_back(msp);
+    inputParams_.emplace_back(msp);
+    inputParams_.back().name = in->name;
     return ret;
 }
 
@@ -328,7 +326,7 @@ MediaFilter::reinitialize()
 }
 
 int
-MediaFilter::fail(std::string msg, int err) const
+MediaFilter::fail(std::string_view msg, int err) const
 {
     if (!msg.empty())
         JAMI_ERR() << msg << ": " << libav_utils::getError(err);
diff --git a/src/media/media_filter.h b/src/media/media_filter.h
index 6e88615eff..0f87228560 100644
--- a/src/media/media_filter.h
+++ b/src/media/media_filter.h
@@ -73,12 +73,12 @@ public:
      * @brief Initializes the filter graph with one or more inputs and one output. Returns a
      * negative code on error.
      */
-    int initialize(const std::string& filterDesc, std::vector<MediaStream> msps);
+    int initialize(const std::string& filterDesc, const std::vector<MediaStream>& msps);
 
     /**
      * @brief Returns a MediaStream object describing the input specified by @inputName.
      */
-    MediaStream getInputParams(const std::string& inputName) const;
+    const MediaStream& getInputParams(const std::string& inputName) const;
 
     /**
      * @brief Returns a MediaStream struct describing the frames that will be output.
@@ -121,7 +121,7 @@ private:
     /**
      * @brief Initializes an input of filter graph.
      */
-    int initInputFilter(AVFilterInOut* in, MediaStream msp);
+    int initInputFilter(AVFilterInOut* in, const MediaStream& msp);
 
     /**
      * @brief Reinitializes the filter graph.
@@ -135,7 +135,7 @@ private:
      *
      * NOTE @msg should not be null.
      */
-    int fail(std::string msg, int err) const;
+    int fail(std::string_view msg, int err) const;
 
     /**
      * @brief Frees resources used by MediaFilter.
-- 
GitLab