Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 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 :
20 : #include "video_mixer.h"
21 : #include "media_buffer.h"
22 : #include "client/videomanager.h"
23 : #include "manager.h"
24 : #include "media_filter.h"
25 : #include "sinkclient.h"
26 : #include "logger.h"
27 : #include "filter_transpose.h"
28 : #ifdef ENABLE_HWACCEL
29 : #include "accel.h"
30 : #endif
31 : #include "connectivity/sip_utils.h"
32 :
33 : #include <cmath>
34 : #include <unistd.h>
35 : #include <mutex>
36 :
37 : #include "videomanager_interface.h"
38 : #include <opendht/thread_pool.h>
39 :
40 : static constexpr auto MIN_LINE_ZOOM
41 : = 6; // Used by the ONE_BIG_WITH_SMALL layout for the small previews
42 :
43 : namespace jami {
44 : namespace video {
45 :
46 : struct VideoMixer::VideoMixerSource
47 : {
48 : Observable<std::shared_ptr<MediaFrame>>* source {nullptr};
49 : int rotation {0};
50 : std::unique_ptr<MediaFilter> rotationFilter {nullptr};
51 : std::shared_ptr<VideoFrame> render_frame;
52 0 : void atomic_copy(const VideoFrame& other)
53 : {
54 0 : std::lock_guard lock(mutex_);
55 0 : auto newFrame = std::make_shared<VideoFrame>();
56 0 : newFrame->copyFrom(other);
57 0 : render_frame = newFrame;
58 0 : }
59 :
60 9357 : std::shared_ptr<VideoFrame> getRenderFrame()
61 : {
62 9357 : std::lock_guard lock(mutex_);
63 18714 : return render_frame;
64 9357 : }
65 :
66 : // Current render information
67 : int x {};
68 : int y {};
69 : int w {};
70 : int h {};
71 : bool hasVideo {true};
72 :
73 : private:
74 : std::mutex mutex_;
75 : };
76 :
77 : static constexpr const auto MIXER_FRAMERATE = 30;
78 : static constexpr const auto FRAME_DURATION = std::chrono::duration<double>(1. / MIXER_FRAMERATE);
79 :
80 38 : VideoMixer::VideoMixer(const std::string& id, const std::string& localInput, bool attachHost)
81 : : VideoGenerator::VideoGenerator()
82 38 : , id_(id)
83 38 : , sink_(Manager::instance().createSinkClient(id, true))
84 152 : , loop_([] { return true; }, std::bind(&VideoMixer::process, this), [] {})
85 : {
86 : // Local video camera is the main participant
87 38 : if (not localInput.empty() && attachHost) {
88 0 : auto videoInput = getVideoInput(localInput);
89 0 : localInputs_.emplace_back(videoInput);
90 0 : attachVideo(videoInput.get(),
91 : "",
92 0 : sip_utils::streamId("", sip_utils::DEFAULT_VIDEO_STREAMID));
93 0 : }
94 38 : loop_.start();
95 38 : nextProcess_ = std::chrono::steady_clock::now();
96 :
97 38 : JAMI_DBG("[mixer:%s] New instance created", id_.c_str());
98 38 : }
99 :
100 76 : VideoMixer::~VideoMixer()
101 : {
102 38 : stopSink();
103 38 : stopInputs();
104 :
105 38 : loop_.join();
106 :
107 38 : JAMI_DBG("[mixer:%s] Instance destroyed", id_.c_str());
108 38 : }
109 :
110 : void
111 34 : VideoMixer::switchInputs(const std::vector<std::string>& inputs)
112 : {
113 : // Do not stop video inputs that are already there
114 : // But only detach it to get new index
115 34 : std::lock_guard lk(localInputsMtx_);
116 34 : decltype(localInputs_) newInputs;
117 34 : newInputs.reserve(inputs.size());
118 69 : for (const auto& input : inputs) {
119 70 : auto videoInput = getVideoInput(input);
120 : // Note, video can be a previously stopped device (eg. restart a screen sharing)
121 : // in this case, the videoInput will be found and must be restarted
122 35 : videoInput->restart();
123 35 : auto it = std::find(localInputs_.cbegin(), localInputs_.cend(), videoInput);
124 35 : auto onlyDetach = it != localInputs_.cend();
125 35 : if (onlyDetach) {
126 1 : videoInput->detach(this);
127 1 : localInputs_.erase(it);
128 : }
129 35 : newInputs.emplace_back(std::move(videoInput));
130 35 : }
131 : // Stop other video inputs
132 34 : stopInputs();
133 34 : localInputs_ = std::move(newInputs);
134 :
135 : // Re-attach videoInput to mixer
136 69 : for (size_t i = 0; i < localInputs_.size(); ++i) {
137 35 : auto& input = localInputs_[i];
138 35 : attachVideo(input.get(), "", sip_utils::streamId("", fmt::format("video_{}", i)));
139 : }
140 34 : }
141 :
142 : void
143 34 : VideoMixer::stopInput(const std::shared_ptr<VideoFrameActiveWriter>& input)
144 : {
145 : // Detach videoInputs from mixer
146 34 : input->detach(this);
147 34 : }
148 :
149 : void
150 99 : VideoMixer::stopInputs()
151 : {
152 133 : for (auto& input : localInputs_)
153 34 : stopInput(input);
154 99 : localInputs_.clear();
155 99 : }
156 :
157 : void
158 1 : VideoMixer::setActiveStream(const std::string& id)
159 : {
160 1 : activeStream_ = id;
161 1 : updateLayout();
162 1 : }
163 :
164 : void
165 414 : VideoMixer::updateLayout()
166 : {
167 414 : if (activeStream_ == "")
168 413 : currentLayout_ = Layout::GRID;
169 414 : layoutUpdated_ += 1;
170 414 : }
171 :
172 : void
173 86 : VideoMixer::attachVideo(Observable<std::shared_ptr<MediaFrame>>* frame,
174 : const std::string& callId,
175 : const std::string& streamId)
176 : {
177 86 : if (!frame)
178 0 : return;
179 86 : JAMI_DBG("Attaching video with streamId %s", streamId.c_str());
180 : {
181 86 : std::lock_guard lk(videoToStreamInfoMtx_);
182 86 : videoToStreamInfo_[frame] = StreamInfo {callId, streamId};
183 86 : }
184 86 : frame->attach(this);
185 : }
186 :
187 : void
188 50 : VideoMixer::detachVideo(Observable<std::shared_ptr<MediaFrame>>* frame)
189 : {
190 50 : if (!frame)
191 0 : return;
192 50 : bool detach = false;
193 50 : std::unique_lock lk(videoToStreamInfoMtx_);
194 50 : auto it = videoToStreamInfo_.find(frame);
195 50 : if (it != videoToStreamInfo_.end()) {
196 50 : JAMI_DBG("Detaching video of call %s", it->second.callId.c_str());
197 50 : detach = true;
198 : // Handle the case where the current shown source leave the conference
199 : // Note, do not call resetActiveStream() to avoid multiple updates
200 50 : if (verifyActive(it->second.streamId))
201 0 : activeStream_ = {};
202 50 : videoToStreamInfo_.erase(it);
203 : }
204 50 : lk.unlock();
205 50 : if (detach)
206 50 : frame->detach(this);
207 50 : }
208 :
209 : void
210 86 : VideoMixer::attached(Observable<std::shared_ptr<MediaFrame>>* ob)
211 : {
212 86 : std::unique_lock lock(rwMutex_);
213 :
214 86 : auto src = std::unique_ptr<VideoMixerSource>(new VideoMixerSource);
215 86 : src->render_frame = std::make_shared<VideoFrame>();
216 86 : src->source = ob;
217 86 : JAMI_DBG("Add new source [%p]", src.get());
218 86 : sources_.emplace_back(std::move(src));
219 258 : JAMI_DEBUG("Total sources: {:d}", sources_.size());
220 86 : updateLayout();
221 86 : }
222 :
223 : void
224 86 : VideoMixer::detached(Observable<std::shared_ptr<MediaFrame>>* ob)
225 : {
226 86 : std::unique_lock lock(rwMutex_);
227 :
228 135 : for (const auto& x : sources_) {
229 135 : if (x->source == ob) {
230 86 : JAMI_DBG("Remove source [%p]", x.get());
231 86 : sources_.remove(x);
232 258 : JAMI_DEBUG("Total sources: {:d}", sources_.size());
233 86 : updateLayout();
234 86 : break;
235 : }
236 : }
237 86 : }
238 :
239 : void
240 0 : VideoMixer::update(Observable<std::shared_ptr<MediaFrame>>* ob,
241 : const std::shared_ptr<MediaFrame>& frame_p)
242 : {
243 0 : std::shared_lock lock(rwMutex_);
244 :
245 0 : for (const auto& x : sources_) {
246 0 : if (x->source == ob) {
247 : #ifdef ENABLE_HWACCEL
248 0 : std::shared_ptr<VideoFrame> frame;
249 : try {
250 0 : frame = HardwareAccel::transferToMainMemory(*std::static_pointer_cast<VideoFrame>(
251 0 : frame_p),
252 0 : AV_PIX_FMT_NV12);
253 0 : x->atomic_copy(*std::static_pointer_cast<VideoFrame>(frame));
254 0 : } catch (const std::runtime_error& e) {
255 0 : JAMI_ERR("[mixer:%s] Accel failure: %s", id_.c_str(), e.what());
256 0 : return;
257 0 : }
258 : #else
259 : x->atomic_copy(*std::static_pointer_cast<VideoFrame>(frame_p));
260 : #endif
261 0 : return;
262 0 : }
263 : }
264 0 : }
265 :
266 : void
267 3783 : VideoMixer::process()
268 : {
269 3783 : nextProcess_ += std::chrono::duration_cast<std::chrono::microseconds>(FRAME_DURATION);
270 3783 : const auto delay = nextProcess_ - std::chrono::steady_clock::now();
271 3783 : if (delay.count() > 0)
272 3783 : std::this_thread::sleep_for(delay);
273 :
274 : // Nothing to do.
275 3783 : if (width_ == 0 or height_ == 0) {
276 0 : return;
277 : }
278 :
279 3783 : VideoFrame& output = getNewFrame();
280 : try {
281 3783 : output.reserve(format_, width_, height_);
282 0 : } catch (const std::bad_alloc& e) {
283 0 : JAMI_ERR("[mixer:%s] VideoFrame::allocBuffer() failed", id_.c_str());
284 0 : return;
285 0 : }
286 :
287 3783 : libav_utils::fillWithBlack(output.pointer());
288 :
289 : {
290 3783 : std::lock_guard lk(audioOnlySourcesMtx_);
291 3783 : std::shared_lock lock(rwMutex_);
292 :
293 3783 : int i = 0;
294 3783 : bool activeFound = false;
295 3783 : bool needsUpdate = layoutUpdated_ > 0;
296 3783 : bool successfullyRendered = audioOnlySources_.size() != 0 && sources_.size() == 0;
297 3783 : std::vector<SourceInfo> sourcesInfo;
298 3783 : sourcesInfo.reserve(sources_.size() + audioOnlySources_.size());
299 : // add all audioonlysources
300 4435 : for (auto& [callId, streamId] : audioOnlySources_) {
301 652 : auto active = verifyActive(streamId);
302 652 : if (currentLayout_ != Layout::ONE_BIG or active) {
303 652 : sourcesInfo.emplace_back(SourceInfo {{}, 0, 0, 10, 10, false, callId, streamId});
304 : }
305 652 : if (currentLayout_ == Layout::ONE_BIG) {
306 0 : if (active)
307 0 : successfullyRendered = true;
308 : else
309 0 : sourcesInfo.emplace_back(SourceInfo {{}, 0, 0, 0, 0, false, callId, streamId});
310 : // Add all participants info even in ONE_BIG layout.
311 : // The width and height set to 0 here will led the peer to filter them out.
312 : }
313 : }
314 : // add video sources
315 13140 : for (auto& x : sources_) {
316 : /* thread stop pending? */
317 9357 : if (!loop_.isRunning())
318 0 : return;
319 :
320 9357 : auto sinfo = streamInfo(x->source);
321 9357 : auto activeSource = verifyActive(sinfo.streamId);
322 9357 : if (currentLayout_ != Layout::ONE_BIG or activeSource) {
323 : // make rendered frame temporarily unavailable for update()
324 : // to avoid concurrent access.
325 9357 : std::shared_ptr<VideoFrame> input = x->getRenderFrame();
326 9357 : std::shared_ptr<VideoFrame> fooInput = std::make_shared<VideoFrame>();
327 :
328 9357 : auto wantedIndex = i;
329 9357 : if (currentLayout_ == Layout::ONE_BIG) {
330 0 : wantedIndex = 0;
331 0 : activeFound = true;
332 9357 : } else if (currentLayout_ == Layout::ONE_BIG_WITH_SMALL) {
333 0 : if (activeSource) {
334 0 : wantedIndex = 0;
335 0 : activeFound = true;
336 0 : } else if (not activeFound) {
337 0 : wantedIndex += 1;
338 : }
339 : }
340 :
341 9357 : auto hasVideo = x->hasVideo;
342 9357 : bool blackFrame = false;
343 :
344 9357 : if (!input->height() or !input->width()) {
345 9357 : successfullyRendered = true;
346 9357 : fooInput->reserve(format_, width_, height_);
347 9357 : blackFrame = true;
348 : } else {
349 0 : fooInput.swap(input);
350 : }
351 :
352 : // If orientation changed or if the first valid frame for source
353 : // is received -> trigger layout calculation and confInfo update
354 9357 : if (x->rotation != fooInput->getOrientation() or !x->w or !x->h) {
355 82 : updateLayout();
356 82 : needsUpdate = true;
357 : }
358 :
359 9357 : if (needsUpdate)
360 680 : calc_position(x, fooInput, wantedIndex);
361 :
362 9357 : if (!blackFrame) {
363 0 : if (fooInput)
364 0 : successfullyRendered |= render_frame(output, fooInput, x);
365 : else
366 0 : JAMI_WARN("[mixer:%s] Nothing to render for %p", id_.c_str(), x->source);
367 : }
368 :
369 9357 : x->hasVideo = !blackFrame && successfullyRendered;
370 9357 : if (hasVideo != x->hasVideo) {
371 82 : updateLayout();
372 82 : needsUpdate = true;
373 : }
374 9357 : } else if (needsUpdate) {
375 0 : x->x = 0;
376 0 : x->y = 0;
377 0 : x->w = 0;
378 0 : x->h = 0;
379 0 : x->hasVideo = false;
380 : }
381 :
382 9357 : ++i;
383 9357 : }
384 3783 : if (needsUpdate and successfullyRendered) {
385 308 : layoutUpdated_ -= 1;
386 308 : if (layoutUpdated_ == 0) {
387 243 : for (auto& x : sources_) {
388 169 : auto sinfo = streamInfo(x->source);
389 338 : sourcesInfo.emplace_back(SourceInfo {x->source,
390 169 : x->x,
391 169 : x->y,
392 169 : x->w,
393 169 : x->h,
394 169 : x->hasVideo,
395 : sinfo.callId,
396 : sinfo.streamId});
397 169 : }
398 74 : if (onSourcesUpdated_)
399 74 : onSourcesUpdated_(std::move(sourcesInfo));
400 : }
401 : }
402 3783 : }
403 :
404 3783 : output.pointer()->pts = av_rescale_q_rnd(av_gettime() - startTime_,
405 : {1, AV_TIME_BASE},
406 : {1, MIXER_FRAMERATE},
407 : static_cast<AVRounding>(AV_ROUND_NEAR_INF
408 : | AV_ROUND_PASS_MINMAX));
409 3783 : lastTimestamp_ = output.pointer()->pts;
410 3783 : publishFrame();
411 : }
412 :
413 : bool
414 0 : VideoMixer::render_frame(VideoFrame& output,
415 : const std::shared_ptr<VideoFrame>& input,
416 : std::unique_ptr<VideoMixerSource>& source)
417 : {
418 0 : if (!width_ or !height_ or !input->pointer() or input->pointer()->format == -1)
419 0 : return false;
420 :
421 0 : int cell_width = source->w;
422 0 : int cell_height = source->h;
423 0 : int xoff = source->x;
424 0 : int yoff = source->y;
425 :
426 0 : int angle = input->getOrientation();
427 0 : const constexpr char filterIn[] = "mixin";
428 0 : if (angle != source->rotation) {
429 0 : source->rotationFilter = video::getTransposeFilter(angle,
430 : filterIn,
431 : input->width(),
432 : input->height(),
433 : input->format(),
434 0 : false);
435 0 : source->rotation = angle;
436 : }
437 0 : std::shared_ptr<VideoFrame> frame;
438 0 : if (source->rotationFilter) {
439 0 : source->rotationFilter->feedInput(input->pointer(), filterIn);
440 0 : frame = std::static_pointer_cast<VideoFrame>(
441 0 : std::shared_ptr<MediaFrame>(source->rotationFilter->readOutput()));
442 : } else {
443 0 : frame = input;
444 : }
445 :
446 0 : scaler_.scale_and_pad(*frame, output, xoff, yoff, cell_width, cell_height, true);
447 0 : return true;
448 0 : }
449 :
450 : void
451 680 : VideoMixer::calc_position(std::unique_ptr<VideoMixerSource>& source,
452 : const std::shared_ptr<VideoFrame>& input,
453 : int index)
454 : {
455 680 : if (!width_ or !height_)
456 0 : return;
457 :
458 : // Compute cell size/position
459 : int cell_width, cell_height, cellW_off, cellH_off;
460 680 : const int n = currentLayout_ == Layout::ONE_BIG ? 1 : sources_.size();
461 0 : const int zoom = currentLayout_ == Layout::ONE_BIG_WITH_SMALL ? std::max(MIN_LINE_ZOOM, n)
462 680 : : ceil(sqrt(n));
463 680 : if (currentLayout_ == Layout::ONE_BIG_WITH_SMALL && index == 0) {
464 : // In ONE_BIG_WITH_SMALL, the first line at the top is the previews
465 : // The rest is the active source
466 0 : cell_width = width_;
467 0 : cell_height = height_ - height_ / zoom;
468 : } else {
469 680 : cell_width = width_ / zoom;
470 680 : cell_height = height_ / zoom;
471 :
472 680 : if (n == 1) {
473 : // On some platforms (at least macOS/android) - Having one frame at the same
474 : // size of the mixer cause it to be grey.
475 : // Removing some pixels solve this. We use 16 because it's a multiple of 8
476 : // (value that we prefer for video management)
477 62 : cell_width -= 16;
478 62 : cell_height -= 16;
479 : }
480 : }
481 680 : if (currentLayout_ == Layout::ONE_BIG_WITH_SMALL) {
482 0 : if (index == 0) {
483 0 : cellW_off = 0;
484 0 : cellH_off = height_ / zoom; // First line height
485 : } else {
486 0 : cellW_off = (index - 1) * cell_width;
487 : // Show sources in center
488 0 : cellW_off += (width_ - (n - 1) * cell_width) / 2;
489 0 : cellH_off = 0;
490 : }
491 : } else {
492 680 : cellW_off = (index % zoom) * cell_width;
493 680 : if (currentLayout_ == Layout::GRID && n % zoom != 0 && index >= (zoom * ((n - 1) / zoom))) {
494 : // Last line, center participants if not full
495 76 : cellW_off += (width_ - (n % zoom) * cell_width) / 2;
496 : }
497 680 : cellH_off = (index / zoom) * cell_height;
498 680 : if (n == 1) {
499 : // Centerize (cellwidth = width_ - 16)
500 62 : cellW_off += 8;
501 62 : cellH_off += 8;
502 : }
503 : }
504 :
505 : // Compute frame size/position
506 : float zoomW, zoomH;
507 : int frameW, frameH, frameW_off, frameH_off;
508 :
509 680 : if (input->getOrientation() % 180) {
510 : // Rotated frame
511 0 : zoomW = (float) input->height() / cell_width;
512 0 : zoomH = (float) input->width() / cell_height;
513 0 : frameH = std::round(input->width() / std::max(zoomW, zoomH));
514 0 : frameW = std::round(input->height() / std::max(zoomW, zoomH));
515 : } else {
516 680 : zoomW = (float) input->width() / cell_width;
517 680 : zoomH = (float) input->height() / cell_height;
518 680 : frameW = std::round(input->width() / std::max(zoomW, zoomH));
519 680 : frameH = std::round(input->height() / std::max(zoomW, zoomH));
520 : }
521 :
522 : // Center the frame in the cell
523 680 : frameW_off = cellW_off + (cell_width - frameW) / 2;
524 680 : frameH_off = cellH_off + (cell_height - frameH) / 2;
525 :
526 : // Update source's cache
527 680 : source->w = frameW;
528 680 : source->h = frameH;
529 680 : source->x = frameW_off;
530 680 : source->y = frameH_off;
531 : }
532 :
533 : void
534 38 : VideoMixer::setParameters(int width, int height, AVPixelFormat format)
535 : {
536 38 : std::unique_lock lock(rwMutex_);
537 :
538 38 : width_ = width;
539 38 : height_ = height;
540 38 : format_ = format;
541 :
542 : // cleanup the previous frame to have a nice copy in rendering method
543 38 : std::shared_ptr<VideoFrame> previous_p(obtainLastFrame());
544 38 : if (previous_p)
545 0 : libav_utils::fillWithBlack(previous_p->pointer());
546 :
547 38 : startSink();
548 38 : updateLayout();
549 38 : startTime_ = av_gettime();
550 38 : }
551 :
552 : void
553 38 : VideoMixer::startSink()
554 : {
555 38 : stopSink();
556 :
557 38 : if (width_ == 0 or height_ == 0) {
558 0 : JAMI_WARN("[mixer:%s] MX: unable to start with zero-sized output", id_.c_str());
559 0 : return;
560 : }
561 :
562 38 : if (not sink_->start()) {
563 0 : JAMI_ERR("[mixer:%s] MX: sink startup failed", id_.c_str());
564 0 : return;
565 : }
566 :
567 38 : if (this->attach(sink_.get()))
568 38 : sink_->setFrameSize(width_, height_);
569 : }
570 :
571 : void
572 76 : VideoMixer::stopSink()
573 : {
574 76 : this->detach(sink_.get());
575 76 : sink_->stop();
576 76 : }
577 :
578 : int
579 74 : VideoMixer::getWidth() const
580 : {
581 74 : return width_;
582 : }
583 :
584 : int
585 74 : VideoMixer::getHeight() const
586 : {
587 74 : return height_;
588 : }
589 :
590 : AVPixelFormat
591 0 : VideoMixer::getPixelFormat() const
592 : {
593 0 : return format_;
594 : }
595 :
596 : MediaStream
597 50 : VideoMixer::getStream(const std::string& name) const
598 : {
599 50 : MediaStream ms;
600 50 : ms.name = name;
601 50 : ms.format = format_;
602 50 : ms.isVideo = true;
603 50 : ms.height = height_;
604 50 : ms.width = width_;
605 50 : ms.frameRate = {MIXER_FRAMERATE, 1};
606 50 : ms.timeBase = {1, MIXER_FRAMERATE};
607 50 : ms.firstTimestamp = lastTimestamp_;
608 :
609 50 : return ms;
610 0 : }
611 :
612 : } // namespace video
613 : } // namespace jami
|