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" |
| 23 | // Logger |
| 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) |
| 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 | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 38 | setId(datapath_); |
agsantos | 2c8525e | 2021-03-19 11:18:01 -0400 | [diff] [blame^] | 39 | #ifdef __ANDROID__ |
| 40 | mVS = std::make_shared<VideoSubscriber>(datapath_, |
| 41 | "mModel.ort", |
| 42 | preferences_.at("background"), |
| 43 | preferences_.at("acceleration") == "1"); |
| 44 | #else |
| 45 | #ifdef NVIDIA |
| 46 | mVS = std::make_shared<VideoSubscriber>(datapath_, |
| 47 | "mModel.onnx", |
| 48 | preferences_.at("background"), |
| 49 | preferences_.at("acceleration") == "1"); |
| 50 | #else |
| 51 | mVS = std::make_shared<VideoSubscriber>(datapath_, "mModel.onnx", preferences_.at("background")); |
| 52 | #endif // NVIDIA |
| 53 | #endif // ANDROID |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | PluginMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject) |
| 58 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 59 | std::ostringstream oss; |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 60 | std::string_view direction = data.direction ? "Receive" : "Preview"; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 61 | oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 62 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 63 | bool preferredStreamDirection = false; |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 64 | auto it = preferences_.find("streamslist"); |
| 65 | if (it != preferences_.end()) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 66 | preferredStreamDirection = it->second == "in"; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 67 | } |
| 68 | oss << "preferredStreamDirection " << preferredStreamDirection << std::endl; |
| 69 | if (data.type == StreamType::video && !data.direction |
| 70 | && data.direction == preferredStreamDirection) { |
| 71 | subject->attach(mVS.get()); // my image |
| 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 | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 76 | subject->attach(mVS.get()); // the image I receive from the others on the call |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 77 | oss << "got my received image attached" << std::endl; |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 78 | attached_ = '1'; |
| 79 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 80 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 81 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | std::map<std::string, std::string> |
| 85 | PluginMediaHandler::getCallMediaHandlerDetails() |
| 86 | { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 87 | return {{"name", NAME}, |
agsantos | d00bf41 | 2021-01-26 13:43:33 -0500 | [diff] [blame] | 88 | {"iconPath", datapath_ + sep + "icon.svg"}, |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 89 | {"pluginId", id()}, |
| 90 | {"attached", attached_}, |
| 91 | {"dataType", "1"}}; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
| 95 | PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value) |
| 96 | { |
agsantos | 82678f3 | 2020-12-09 15:03:24 -0500 | [diff] [blame] | 97 | auto it = preferences_.find(key); |
| 98 | if (it != preferences_.end() && it->second != value) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 99 | it->second = value; |
| 100 | if (key == "background") { |
| 101 | mVS->setBackground(value); |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool |
| 107 | PluginMediaHandler::preferenceMapHasKey(const std::string& key) |
| 108 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 109 | if (key == "background") { |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
| 116 | PluginMediaHandler::detach() |
| 117 | { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 118 | attached_ = '0'; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 119 | mVS->detach(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | PluginMediaHandler::~PluginMediaHandler() |
| 123 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 124 | std::ostringstream oss; |
| 125 | oss << " ~FORESEG Plugin" << std::endl; |
| 126 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 127 | detach(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 128 | } |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 129 | } // namespace jami |