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 : #pragma once 18 : 19 : #include "noncopyable.h" 20 : #include "threadloop.h" 21 : #include "media/media_stream.h" 22 : #include "media/media_device.h" // DeviceParams 23 : #include "media/video/video_base.h" 24 : #include "media/media_codec.h" 25 : 26 : #include <map> 27 : #include <atomic> 28 : #include <future> 29 : #include <string> 30 : #include <mutex> 31 : #include <condition_variable> 32 : #include <array> 33 : 34 : #if __APPLE__ 35 : #import "TargetConditionals.h" 36 : #endif 37 : 38 : namespace jami { 39 : class MediaDecoder; 40 : class MediaDemuxer; 41 : } // namespace jami 42 : 43 : namespace jami { 44 : namespace video { 45 : 46 : class SinkClient; 47 : 48 : enum class VideoInputMode { ManagedByClient, ManagedByDaemon, Undefined }; 49 : 50 : class VideoInput : public VideoGenerator 51 : { 52 : public: 53 : VideoInput(VideoInputMode inputMode = VideoInputMode::Undefined, 54 : const std::string& resource = "local", 55 : const std::string& sink = ""); 56 : ~VideoInput(); 57 : 58 : // as VideoGenerator 59 : const std::string& getName() const { return resource_; } 60 : int getWidth() const; 61 : int getHeight() const; 62 : AVPixelFormat getPixelFormat() const; 63 : 64 : const DeviceParams& getConfig() const { return decOpts_; } 65 108 : std::shared_future<DeviceParams> getParams() const { return futureDecOpts_; } 66 : 67 : MediaStream getInfo() const; 68 : 69 : void setSink(const std::string& sinkId); 70 : void updateStartTime(int64_t startTime); 71 : void configureFilePlayback(const std::string& path, std::shared_ptr<MediaDemuxer>& demuxer, int index); 72 : void flushBuffers(); 73 10 : void setPaused(bool paused) { paused_ = paused; } 74 : void setSeekTime(int64_t time); 75 : void setupSink(const int width, const int height); 76 : void stopSink(); 77 : 78 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb); 79 : 80 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) { onSuccessfulSetup_ = cb; } 81 : 82 : /** 83 : * Restart stopped video input 84 : * @note if a media is removed, then re-added in a conference, the loop will be stopped 85 : * and this input must be restarted 86 : */ 87 : void restart(); 88 : 89 : private: 90 : NON_COPYABLE(VideoInput); 91 : 92 : std::shared_future<DeviceParams> switchInput(const std::string& resource); 93 : 94 : std::string resource_; 95 : std::atomic<bool> switchPending_ = {false}; 96 : std::atomic_bool isStopped_ = {false}; 97 : 98 : DeviceParams decOpts_; 99 : std::promise<DeviceParams> foundDecOpts_; 100 : std::shared_future<DeviceParams> futureDecOpts_; 101 : bool emulateRate_ = false; 102 : 103 : std::atomic_bool decOptsFound_ {false}; 104 : void foundDecOpts(const DeviceParams& params); 105 : 106 : void clearOptions(); 107 : 108 : // true if decOpts_ is ready to use, false if using promise/future 109 : bool initCamera(const std::string& device); 110 : bool initFile(std::string path); 111 : 112 : #ifdef __APPLE__ 113 : bool initAVFoundation(const std::string& display); 114 : #elif defined(WIN32) 115 : bool initWindowsGrab(const std::string& display); 116 : #else 117 : bool initLinuxGrab(const std::string& display); 118 : #endif 119 : 120 : bool isCapturing() const noexcept; 121 : void startLoop(); 122 : 123 : void switchDevice(); 124 : bool capturing_ {false}; 125 : void createDecoder(); 126 : void deleteDecoder(); 127 : std::unique_ptr<MediaDecoder> decoder_; 128 : std::shared_ptr<SinkClient> sink_; 129 : ThreadLoop loop_; 130 : 131 : // for ThreadLoop 132 : bool setup(); 133 : void process(); 134 : void cleanup(); 135 : 136 : bool captureFrame(); 137 : 138 : int rotation_ {0}; 139 : std::shared_ptr<AVBufferRef> displayMatrix_; 140 : void setRotation(int angle); 141 : VideoInputMode inputMode_; 142 457 : inline bool videoManagedByClient() const { return inputMode_ == VideoInputMode::ManagedByClient; } 143 : bool playingFile_ = false; 144 : std::atomic_bool paused_ {true}; 145 : 146 : std::function<void(MediaType, bool)> onSuccessfulSetup_; 147 : std::function<void(const MediaStream& ms)> recorderCallback_; 148 : }; 149 : 150 : } // namespace video 151 : } // namespace jami