Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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 : #pragma once 18 : 19 : #ifdef HAVE_CONFIG_H 20 : #include "config.h" 21 : #endif 22 : 23 : #include "video_base.h" 24 : #include <videomanager_interface.h> 25 : 26 : #include <string> 27 : #include <vector> 28 : #include <memory> 29 : 30 : //#define DEBUG_FPS 31 : 32 : namespace jami { 33 : class MediaFilter; 34 : } 35 : 36 : namespace jami { 37 : namespace video { 38 : 39 : #ifdef ENABLE_SHM 40 : class ShmHolder; 41 : #endif // ENABLE_SHM 42 : 43 : class VideoScaler; 44 : 45 : class SinkClient : public VideoFramePassiveReader, public VideoFrameActiveWriter 46 : { 47 : public: 48 : SinkClient(const std::string& id = "", bool mixer = false); 49 : 50 411 : const std::string& getId() const noexcept { return id_; } 51 : 52 : std::string openedName() const noexcept; 53 : 54 0 : int getWidth() const noexcept { return width_; } 55 : 56 0 : int getHeight() const noexcept { return height_; } 57 : 58 : AVPixelFormat getPreferredFormat() const noexcept 59 : { 60 : return (AVPixelFormat) target_.preferredFormat; 61 : } 62 : 63 : // as VideoFramePassiveReader 64 : void update(Observable<std::shared_ptr<jami::MediaFrame>>*, 65 : const std::shared_ptr<jami::MediaFrame>&) override; 66 : 67 : bool start() noexcept; 68 : bool stop() noexcept; 69 : 70 : void setFrameSize(int width, int height); 71 : void setCrop(int x, int y, int w, int h); 72 : 73 0 : void registerTarget(libjami::SinkTarget target) noexcept 74 : { 75 0 : std::lock_guard lock(mtx_); 76 0 : target_ = std::move(target); 77 0 : } 78 : 79 : #ifdef ENABLE_SHM 80 0 : void enableShm(bool value) { doShmTransfer_.store(value); } 81 : #endif 82 : 83 : private: 84 : const std::string id_; 85 : // True if the instance is used by a mixer. 86 : const bool mixer_ {false}; 87 : int width_ {0}; 88 : int height_ {0}; 89 : 90 : struct Rect 91 : { 92 : int x {0}, y {0}, w {0}, h {0}; 93 : }; 94 : Rect crop_ {}; 95 : 96 : bool started_ {false}; // used to arbitrate client's stop signal. 97 : int rotation_ {0}; 98 : libjami::SinkTarget target_; 99 : std::unique_ptr<VideoScaler> scaler_; 100 : std::unique_ptr<MediaFilter> filter_; 101 : std::mutex mtx_; 102 : 103 : void sendFrameDirect(const std::shared_ptr<jami::MediaFrame>&); 104 : void sendFrameTransformed(AVFrame* frame); 105 : 106 : /** 107 : * Apply required transformations before sending frames to clients/observers: 108 : * - Transfer the frame from gpu to main memory, if needed. 109 : * - Rotate the frame as needed. 110 : * - Apply cropping as needed 111 : */ 112 : std::shared_ptr<VideoFrame> applyTransform(VideoFrame& frame); 113 : 114 : #ifdef DEBUG_FPS 115 : unsigned frameCount_; 116 : std::chrono::steady_clock::time_point lastFrameDebug_; 117 : #endif 118 : 119 : #ifdef ENABLE_SHM 120 : // using shared_ptr and not unique_ptr as ShmHolder is forwared only 121 : std::shared_ptr<ShmHolder> shm_; 122 : std::atomic_bool doShmTransfer_ {false}; 123 : #endif // ENABLE_SHM 124 : }; 125 : 126 : } // namespace video 127 : } // namespace jami