agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 1 | /** |
Sébastien Blin | cb783e3 | 2021-02-12 11:34:10 -0500 | [diff] [blame] | 2 | * Copyright (C) 2020-2021 Savoir-faire Linux Inc. |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 | * USA. |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "pluginMediaHandler.h" |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 23 | |
| 24 | #include <pluglog.h> |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 25 | #include <string_view> |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 26 | const char sep = separator(); |
| 27 | const std::string TAG = "FORESEG"; |
| 28 | |
| 29 | #define NAME "Foreground Segmentation" |
| 30 | |
| 31 | namespace jami { |
| 32 | |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 33 | PluginMediaHandler::PluginMediaHandler(std::map<std::string, std::string>&& preferences, |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 34 | std::string&& datapath) |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 35 | : dataPath_ {datapath} |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 36 | , preferences_ {preferences} |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 37 | { |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 38 | setId(dataPath_); |
agsantos | 2c8525e | 2021-03-19 11:18:01 -0400 | [diff] [blame] | 39 | #ifdef __ANDROID__ |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 40 | mVS = std::make_shared<VideoSubscriber>(dataPath_ + sep + "model/mModel.ort", |
agsantos | 2c8525e | 2021-03-19 11:18:01 -0400 | [diff] [blame] | 41 | preferences_.at("acceleration") == "1"); |
| 42 | #else |
| 43 | #ifdef NVIDIA |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 44 | mVS = std::make_shared<VideoSubscriber>(dataPath_ + sep + "model/mModel.onnx", |
agsantos | 2c8525e | 2021-03-19 11:18:01 -0400 | [diff] [blame] | 45 | preferences_.at("acceleration") == "1"); |
| 46 | #else |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 47 | mVS = std::make_shared<VideoSubscriber>(dataPath_ + sep + "model/mModel.onnx"); |
agsantos | 2c8525e | 2021-03-19 11:18:01 -0400 | [diff] [blame] | 48 | #endif // NVIDIA |
| 49 | #endif // ANDROID |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 50 | mVS->setBackground(preferences_.at("background")); |
| 51 | mVS->setBlur(preferences_.at("blur") == "1"); |
| 52 | mVS->setBlurLevel(preferences_.at("blurlevel")); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
| 56 | PluginMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject) |
| 57 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 58 | std::ostringstream oss; |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 59 | std::string_view direction = data.direction ? "Receive" : "Preview"; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 60 | oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 61 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 62 | bool preferredStreamDirection = false; |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 63 | auto it = preferences_.find("streamslist"); |
| 64 | if (it != preferences_.end()) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 65 | preferredStreamDirection = it->second == "in"; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 66 | } |
| 67 | oss << "preferredStreamDirection " << preferredStreamDirection << std::endl; |
| 68 | if (data.type == StreamType::video && !data.direction |
| 69 | && data.direction == preferredStreamDirection) { |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 70 | detach(); |
| 71 | subject->attachPriorityObserver(mVS); // my image |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 72 | oss << "got my sent image attached" << std::endl; |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 73 | attached_ = '1'; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 74 | } else if (data.type == StreamType::video && data.direction |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 75 | && data.direction == preferredStreamDirection) { |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 76 | detach(); |
| 77 | subject->attachPriorityObserver(mVS); // the image I receive from the others on the call |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 78 | oss << "got my received image attached" << std::endl; |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 79 | attached_ = '1'; |
| 80 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 81 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 82 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | std::map<std::string, std::string> |
| 86 | PluginMediaHandler::getCallMediaHandlerDetails() |
| 87 | { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 88 | return {{"name", NAME}, |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 89 | {"iconPath", dataPath_ + sep + "icon.svg"}, |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 90 | {"pluginId", id()}, |
| 91 | {"attached", attached_}, |
| 92 | {"dataType", "1"}}; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value) |
| 97 | { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 98 | auto it = preferences_.find(key); |
| 99 | if (it != preferences_.end() && it->second != value) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 100 | it->second = value; |
| 101 | if (key == "background") { |
| 102 | mVS->setBackground(value); |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 103 | } |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 104 | if (key == "blur") { |
| 105 | mVS->setBlur(value == "1"); |
| 106 | } |
| 107 | if (key == "blurlevel") { |
| 108 | mVS->setBlurLevel(value); |
| 109 | } |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 110 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool |
| 114 | PluginMediaHandler::preferenceMapHasKey(const std::string& key) |
| 115 | { |
agsantos | dd6a62a | 2021-03-29 17:13:27 -0400 | [diff] [blame] | 116 | return (key == "background" || key == "blur" || key == "blurlevel"); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
| 120 | PluginMediaHandler::detach() |
| 121 | { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 122 | attached_ = '0'; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 123 | mVS->detach(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | PluginMediaHandler::~PluginMediaHandler() |
| 127 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 128 | std::ostringstream oss; |
| 129 | oss << " ~FORESEG Plugin" << std::endl; |
| 130 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 131 | detach(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 132 | } |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 133 | } // namespace jami |