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 3598 : VideoGenerator::getNewFrame()
32 : {
33 3598 : std::lock_guard lk(mutex_);
34 3598 : writableFrame_.reset(new VideoFrame());
35 7196 : return *writableFrame_.get();
36 3598 : }
37 :
38 : void
39 3597 : VideoGenerator::publishFrame()
40 : {
41 3597 : std::lock_guard lk(mutex_);
42 3597 : lastFrame_ = std::move(writableFrame_);
43 3597 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_));
44 3597 : }
45 :
46 : void
47 5359 : VideoGenerator::publishFrame(std::shared_ptr<VideoFrame> frame)
48 : {
49 5359 : std::lock_guard lk(mutex_);
50 5359 : lastFrame_ = std::move(frame);
51 5359 : notify(std::static_pointer_cast<MediaFrame>(lastFrame_));
52 5359 : }
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 : 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 2449 : convert<jami::video::VideoSettings>::encode(const jami::video::VideoSettings& rhs)
114 : {
115 2449 : Node node;
116 2449 : node["name"] = rhs.name;
117 2449 : node["id"] = rhs.unique_id;
118 2449 : node["input"] = rhs.input;
119 2449 : node["video_size"] = rhs.video_size;
120 2449 : node["channel"] = rhs.channel;
121 2449 : node["framerate"] = rhs.framerate;
122 2449 : node["passthrough"] = rhs.passthrough;
123 2449 : return node;
124 0 : }
125 :
126 : bool
127 29 : convert<jami::video::VideoSettings>::decode(const Node& node, jami::video::VideoSettings& rhs)
128 : {
129 29 : if (not node.IsMap()) {
130 0 : JAMI_WARNING("Unable to decode VideoSettings YAML node");
131 0 : return false;
132 : }
133 29 : rhs.name = node["name"].as<std::string>();
134 29 : rhs.unique_id = node["id"].as<std::string>();
135 29 : rhs.input = node["input"].as<std::string>();
136 29 : rhs.video_size = node["video_size"].as<std::string>();
137 29 : rhs.channel = node["channel"].as<std::string>();
138 29 : rhs.framerate = node["framerate"].as<std::string>();
139 29 : if (node["passthrough"])
140 29 : rhs.passthrough = node["passthrough"].as<bool>();
141 29 : return true;
142 : }
143 :
144 : Emitter&
145 2449 : operator<<(Emitter& out, const jami::video::VideoSettings& v)
146 : {
147 2449 : out << convert<jami::video::VideoSettings>::encode(v);
148 2449 : return out;
149 : }
150 :
151 : } // namespace YAML
|