Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com> 5 : * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> 6 : * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> 7 : * 8 : * This program is free software; you can redistribute it and/or modify 9 : * it under the terms of the GNU General Public License as published by 10 : * the Free Software Foundation; either version 3 of the License, or 11 : * (at your option) any later version. 12 : * 13 : * This program is distributed in the hope that it will be useful, 14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : * GNU General Public License for more details. 17 : * 18 : * You should have received a copy of the GNU General Public License 19 : * along with this program; if not, write to the Free Software 20 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 : */ 22 : 23 : #pragma once 24 : 25 : #ifdef HAVE_CONFIG_H 26 : #include "config.h" 27 : #endif 28 : 29 : #include "video_base.h" 30 : #include <videomanager_interface.h> 31 : 32 : #include <string> 33 : #include <vector> 34 : #include <memory> 35 : 36 : //#define DEBUG_FPS 37 : 38 : namespace jami { 39 : class MediaFilter; 40 : } 41 : 42 : namespace jami { 43 : namespace video { 44 : 45 : #ifdef ENABLE_SHM 46 : class ShmHolder; 47 : #endif // ENABLE_SHM 48 : 49 : class VideoScaler; 50 : 51 : class SinkClient : public VideoFramePassiveReader, public VideoFrameActiveWriter 52 : { 53 : public: 54 : SinkClient(const std::string& id = "", bool mixer = false); 55 : 56 392 : const std::string& getId() const noexcept { return id_; } 57 : 58 : std::string openedName() const noexcept; 59 : 60 0 : int getWidth() const noexcept { return width_; } 61 : 62 0 : int getHeight() const noexcept { return height_; } 63 : 64 : AVPixelFormat getPreferredFormat() const noexcept 65 : { 66 : return (AVPixelFormat) target_.preferredFormat; 67 : } 68 : 69 : // as VideoFramePassiveReader 70 : void update(Observable<std::shared_ptr<jami::MediaFrame>>*, 71 : const std::shared_ptr<jami::MediaFrame>&) override; 72 : 73 : bool start() noexcept; 74 : bool stop() noexcept; 75 : 76 : void setFrameSize(int width, int height); 77 : void setCrop(int x, int y, int w, int h); 78 : 79 0 : void registerTarget(libjami::SinkTarget target) noexcept 80 : { 81 0 : std::lock_guard lock(mtx_); 82 0 : target_ = std::move(target); 83 0 : } 84 : 85 : #ifdef ENABLE_SHM 86 0 : void enableShm(bool value) { doShmTransfer_.store(value); } 87 : #endif 88 : 89 : private: 90 : const std::string id_; 91 : // True if the instance is used by a mixer. 92 : const bool mixer_ {false}; 93 : int width_ {0}; 94 : int height_ {0}; 95 : 96 : struct Rect 97 : { 98 : int x {0}, y {0}, w {0}, h {0}; 99 : }; 100 : Rect crop_ {}; 101 : 102 : bool started_ {false}; // used to arbitrate client's stop signal. 103 : int rotation_ {0}; 104 : libjami::SinkTarget target_; 105 : std::unique_ptr<VideoScaler> scaler_; 106 : std::unique_ptr<MediaFilter> filter_; 107 : std::mutex mtx_; 108 : 109 : void sendFrameDirect(const std::shared_ptr<jami::MediaFrame>&); 110 : void sendFrameTransformed(AVFrame* frame); 111 : 112 : /** 113 : * Apply required transformations before sending frames to clients/observers: 114 : * - Transfer the frame from gpu to main memory, if needed. 115 : * - Rotate the frame as needed. 116 : * - Apply cropping as needed 117 : */ 118 : std::shared_ptr<VideoFrame> applyTransform(VideoFrame& frame); 119 : 120 : #ifdef DEBUG_FPS 121 : unsigned frameCount_; 122 : std::chrono::steady_clock::time_point lastFrameDebug_; 123 : #endif 124 : 125 : #ifdef ENABLE_SHM 126 : // using shared_ptr and not unique_ptr as ShmHolder is forwared only 127 : std::shared_ptr<ShmHolder> shm_; 128 : std::atomic_bool doShmTransfer_ {false}; 129 : #endif // ENABLE_SHM 130 : }; 131 : 132 : } // namespace video 133 : } // namespace jami