Line data Source code
1 : /* 2 : * Copyright (C) 2004-2026 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 "logger.h" 22 : 23 : #include <cassert> 24 : 25 : namespace jami { 26 : namespace video { 27 : 28 : /*=== VideoGenerator =========================================================*/ 29 : 30 : VideoFrame& 31 3576 : VideoGenerator::getNewFrame() 32 : { 33 3576 : std::lock_guard lk(mutex_); 34 3576 : writableFrame_.reset(new VideoFrame()); 35 7152 : return *writableFrame_.get(); 36 3576 : } 37 : 38 : void 39 3575 : VideoGenerator::publishFrame() 40 : { 41 3575 : std::lock_guard lk(mutex_); 42 3575 : lastFrame_ = std::move(writableFrame_); 43 3575 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_)); 44 3575 : } 45 : 46 : void 47 5308 : VideoGenerator::publishFrame(std::shared_ptr<VideoFrame> frame) 48 : { 49 5308 : std::lock_guard lk(mutex_); 50 5308 : lastFrame_ = std::move(frame); 51 5308 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_)); 52 5308 : } 53 : 54 : void 55 5 : VideoGenerator::flushFrames() 56 : { 57 5 : std::lock_guard lk(mutex_); 58 5 : writableFrame_.reset(); 59 5 : lastFrame_.reset(); 60 5 : } 61 : 62 : std::shared_ptr<VideoFrame> 63 37 : VideoGenerator::obtainLastFrame() 64 : { 65 37 : std::lock_guard lk(mutex_); 66 74 : return lastFrame_; 67 37 : } 68 : 69 : /*=== VideoSettings =========================================================*/ 70 : 71 : static std::string 72 0 : extractString(const std::map<std::string, std::string>& settings, const std::string& key) 73 : { 74 0 : auto i = settings.find(key); 75 0 : if (i != settings.cend()) 76 0 : return i->second; 77 0 : return {}; 78 : } 79 : 80 0 : VideoSettings::VideoSettings(const std::map<std::string, std::string>& settings) 81 : { 82 0 : name = extractString(settings, "name"); 83 0 : unique_id = extractString(settings, "id"); 84 0 : input = extractString(settings, "input"); 85 0 : if (input.empty()) { 86 0 : input = unique_id; 87 : } 88 0 : channel = extractString(settings, "channel"); 89 0 : video_size = extractString(settings, "size"); 90 0 : framerate = extractString(settings, "rate"); 91 0 : } 92 : 93 : std::map<std::string, std::string> 94 0 : VideoSettings::to_map() const 95 : { 96 0 : return {{"name", name}, 97 0 : {"id", unique_id}, 98 0 : {"input", input}, 99 0 : {"size", video_size}, 100 0 : {"channel", channel}, 101 0 : {"rate", framerate}}; 102 : } 103 : 104 : } // namespace video 105 : } // namespace jami 106 : 107 : namespace YAML { 108 : 109 : Node 110 2393 : convert<jami::video::VideoSettings>::encode(const jami::video::VideoSettings& rhs) 111 : { 112 2393 : Node node; 113 2393 : node["name"] = rhs.name; 114 2393 : node["id"] = rhs.unique_id; 115 2393 : node["input"] = rhs.input; 116 2393 : node["video_size"] = rhs.video_size; 117 2393 : node["channel"] = rhs.channel; 118 2393 : node["framerate"] = rhs.framerate; 119 2393 : return node; 120 0 : } 121 : 122 : bool 123 28 : convert<jami::video::VideoSettings>::decode(const Node& node, jami::video::VideoSettings& rhs) 124 : { 125 28 : if (not node.IsMap()) { 126 0 : JAMI_WARN("Unable to decode VideoSettings YAML node"); 127 0 : return false; 128 : } 129 28 : rhs.name = node["name"].as<std::string>(); 130 28 : rhs.unique_id = node["id"].as<std::string>(); 131 28 : rhs.input = node["input"].as<std::string>(); 132 28 : rhs.video_size = node["video_size"].as<std::string>(); 133 28 : rhs.channel = node["channel"].as<std::string>(); 134 28 : rhs.framerate = node["framerate"].as<std::string>(); 135 28 : return true; 136 : } 137 : 138 : Emitter& 139 2393 : operator<<(Emitter& out, const jami::video::VideoSettings& v) 140 : { 141 2393 : out << convert<jami::video::VideoSettings>::encode(v); 142 2393 : return out; 143 : } 144 : 145 : } // namespace YAML