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