Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "libav_deps.h" // MUST BE INCLUDED FIRST 19 : #include "video_base.h" 20 : #include "media_buffer.h" 21 : #include "string_utils.h" 22 : #include "logger.h" 23 : 24 : #include <cassert> 25 : 26 : namespace jami { 27 : namespace video { 28 : 29 : /*=== VideoGenerator =========================================================*/ 30 : 31 : VideoFrame& 32 3765 : VideoGenerator::getNewFrame() 33 : { 34 3765 : std::lock_guard lk(mutex_); 35 3765 : writableFrame_.reset(new VideoFrame()); 36 7530 : return *writableFrame_.get(); 37 3765 : } 38 : 39 : void 40 3765 : VideoGenerator::publishFrame() 41 : { 42 3765 : std::lock_guard lk(mutex_); 43 3765 : lastFrame_ = std::move(writableFrame_); 44 3765 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_)); 45 3765 : } 46 : 47 : void 48 5435 : VideoGenerator::publishFrame(std::shared_ptr<VideoFrame> frame) 49 : { 50 5435 : std::lock_guard lk(mutex_); 51 5435 : lastFrame_ = std::move(frame); 52 5435 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_)); 53 5435 : } 54 : 55 : void 56 10 : VideoGenerator::flushFrames() 57 : { 58 10 : std::lock_guard lk(mutex_); 59 10 : writableFrame_.reset(); 60 10 : lastFrame_.reset(); 61 10 : } 62 : 63 : std::shared_ptr<VideoFrame> 64 38 : VideoGenerator::obtainLastFrame() 65 : { 66 38 : std::lock_guard lk(mutex_); 67 76 : return lastFrame_; 68 38 : } 69 : 70 : /*=== VideoSettings =========================================================*/ 71 : 72 : static std::string 73 0 : extractString(const std::map<std::string, std::string>& settings, const std::string& key) 74 : { 75 0 : auto i = settings.find(key); 76 0 : if (i != settings.cend()) 77 0 : return i->second; 78 0 : return {}; 79 : } 80 : 81 0 : VideoSettings::VideoSettings(const std::map<std::string, std::string>& settings) 82 : { 83 0 : name = extractString(settings, "name"); 84 0 : unique_id = extractString(settings, "id"); 85 0 : input = extractString(settings, "input"); 86 0 : if (input.empty()) { 87 0 : input = unique_id; 88 : } 89 0 : channel = extractString(settings, "channel"); 90 0 : video_size = extractString(settings, "size"); 91 0 : framerate = extractString(settings, "rate"); 92 0 : } 93 : 94 : std::map<std::string, std::string> 95 0 : VideoSettings::to_map() const 96 : { 97 0 : return {{"name", name}, 98 0 : {"id", unique_id}, 99 0 : {"input", input}, 100 0 : {"size", video_size}, 101 0 : {"channel", channel}, 102 0 : {"rate", framerate}}; 103 : } 104 : 105 : } // namespace video 106 : } // namespace jami 107 : 108 : namespace YAML { 109 : 110 : Node 111 1738 : convert<jami::video::VideoSettings>::encode(const jami::video::VideoSettings& rhs) 112 : { 113 1738 : Node node; 114 1738 : node["name"] = rhs.name; 115 1738 : node["id"] = rhs.unique_id; 116 1738 : node["input"] = rhs.input; 117 1738 : node["video_size"] = rhs.video_size; 118 1738 : node["channel"] = rhs.channel; 119 1738 : node["framerate"] = rhs.framerate; 120 1738 : return node; 121 0 : } 122 : 123 : bool 124 30 : convert<jami::video::VideoSettings>::decode(const Node& node, jami::video::VideoSettings& rhs) 125 : { 126 30 : if (not node.IsMap()) { 127 0 : JAMI_WARN("Unable to decode VideoSettings YAML node"); 128 0 : return false; 129 : } 130 30 : rhs.name = node["name"].as<std::string>(); 131 30 : rhs.unique_id = node["id"].as<std::string>(); 132 30 : rhs.input = node["input"].as<std::string>(); 133 30 : rhs.video_size = node["video_size"].as<std::string>(); 134 30 : rhs.channel = node["channel"].as<std::string>(); 135 30 : rhs.framerate = node["framerate"].as<std::string>(); 136 30 : return true; 137 : } 138 : 139 : Emitter& 140 1738 : operator<<(Emitter& out, const jami::video::VideoSettings& v) 141 : { 142 1738 : out << convert<jami::video::VideoSettings>::encode(v); 143 1738 : return out; 144 : } 145 : 146 : } // namespace YAML