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 3755 : VideoGenerator::getNewFrame()
32 : {
33 3755 : std::lock_guard lk(mutex_);
34 3755 : writableFrame_.reset(new VideoFrame());
35 7510 : return *writableFrame_.get();
36 3755 : }
37 :
38 : void
39 3754 : VideoGenerator::publishFrame()
40 : {
41 3754 : std::lock_guard lk(mutex_);
42 3754 : lastFrame_ = std::move(writableFrame_);
43 3754 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_));
44 3754 : }
45 :
46 : void
47 5302 : VideoGenerator::publishFrame(std::shared_ptr<VideoFrame> frame)
48 : {
49 5302 : std::lock_guard lk(mutex_);
50 5302 : lastFrame_ = std::move(frame);
51 5302 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_));
52 5302 : }
53 :
54 : void
55 10 : VideoGenerator::flushFrames()
56 : {
57 10 : std::lock_guard lk(mutex_);
58 10 : writableFrame_.reset();
59 10 : lastFrame_.reset();
60 10 : }
61 :
62 : std::shared_ptr<VideoFrame>
63 38 : VideoGenerator::obtainLastFrame()
64 : {
65 38 : std::lock_guard lk(mutex_);
66 76 : return lastFrame_;
67 38 : }
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 : auto n = extractString(settings, "passthrough");
92 0 : passthrough = n == "true" || n == "1";
93 0 : }
94 :
95 : std::map<std::string, std::string>
96 0 : VideoSettings::to_map() const
97 : {
98 0 : return {{"name", name},
99 0 : {"id", unique_id},
100 0 : {"input", input},
101 0 : {"size", video_size},
102 0 : {"channel", channel},
103 0 : {"rate", framerate},
104 0 : {"passthrough", passthrough ? "true" : "false"}};
105 0 : }
106 :
107 : } // namespace video
108 : } // namespace jami
109 :
110 : namespace YAML {
111 :
112 : Node
113 2528 : convert<jami::video::VideoSettings>::encode(const jami::video::VideoSettings& rhs)
114 : {
115 2528 : Node node;
116 2528 : node["name"] = rhs.name;
117 2528 : node["id"] = rhs.unique_id;
118 2528 : node["input"] = rhs.input;
119 2528 : node["video_size"] = rhs.video_size;
120 2528 : node["channel"] = rhs.channel;
121 2528 : node["framerate"] = rhs.framerate;
122 2528 : node["passthrough"] = rhs.passthrough;
123 2528 : return node;
124 0 : }
125 :
126 : bool
127 30 : convert<jami::video::VideoSettings>::decode(const Node& node, jami::video::VideoSettings& rhs)
128 : {
129 30 : if (not node.IsMap()) {
130 0 : JAMI_WARNING("Unable to decode VideoSettings YAML node");
131 0 : return false;
132 : }
133 30 : rhs.name = node["name"].as<std::string>();
134 30 : rhs.unique_id = node["id"].as<std::string>();
135 30 : rhs.input = node["input"].as<std::string>();
136 30 : rhs.video_size = node["video_size"].as<std::string>();
137 30 : rhs.channel = node["channel"].as<std::string>();
138 30 : rhs.framerate = node["framerate"].as<std::string>();
139 30 : if (node["passthrough"])
140 30 : rhs.passthrough = node["passthrough"].as<bool>();
141 30 : return true;
142 : }
143 :
144 : Emitter&
145 2528 : operator<<(Emitter& out, const jami::video::VideoSettings& v)
146 : {
147 2528 : out << convert<jami::video::VideoSettings>::encode(v);
148 2528 : return out;
149 : }
150 :
151 : } // namespace YAML
|