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 : #ifndef _VIDEO_RECEIVE_THREAD_H_ 19 : #define _VIDEO_RECEIVE_THREAD_H_ 20 : 21 : #include "video_base.h" 22 : #include "media/media_codec.h" 23 : #include "media/media_io_handle.h" 24 : #include "media/media_codec.h" 25 : #include "media/media_device.h" 26 : #include "media/media_stream.h" 27 : #include "threadloop.h" 28 : #include "noncopyable.h" 29 : #include "media/libav_utils.h" 30 : 31 : #include <functional> 32 : #include <map> 33 : #include <string> 34 : #include <climits> 35 : #include <sstream> 36 : #include <memory> 37 : 38 : namespace jami { 39 : class SocketPair; 40 : class MediaDecoder; 41 : } // namespace jami 42 : 43 : namespace jami { 44 : namespace video { 45 : 46 : class SinkClient; 47 : 48 : class VideoReceiveThread : public VideoGenerator 49 : { 50 : public: 51 : VideoReceiveThread(const std::string& id, bool useSink, const std::string& sdp, uint16_t mtu); 52 : ~VideoReceiveThread(); 53 : 54 : void startLoop(); 55 : void stopLoop(); 56 : void decodeFrame(); 57 : void addIOContext(SocketPair& socketPair); 58 139 : void setRequestKeyFrameCallback(std::function<void(void)> cb) { keyFrameRequestCallback_ = std::move(cb); }; 59 : void startSink(); 60 : void stopSink(); 61 172 : std::shared_ptr<SinkClient>& getSink() { return sink_; } 62 : 63 : // as VideoGenerator 64 : int getWidth() const; 65 : int getHeight() const; 66 : AVPixelFormat getPixelFormat() const; 67 : MediaStream getInfo() const; 68 : 69 : /** 70 : * Set angle of rotation to apply to the video by the decoder 71 : * 72 : * @param angle Angle of rotation in degrees (counterclockwise) 73 : */ 74 : void setRotation(int angle); 75 : 76 139 : void setSuccessfulSetupCb(const std::function<void(MediaType, bool)>& cb) { onSuccessfulSetup_ = cb; } 77 : 78 : void setRecorderCallback(const std::function<void(const MediaStream& ms)>& cb); 79 : 80 : private: 81 : NON_COPYABLE(VideoReceiveThread); 82 : 83 : DeviceParams args_; 84 : 85 : /*-------------------------------------------------------------*/ 86 : /* These variables should be used in thread (i.e. run()) only! */ 87 : /*-------------------------------------------------------------*/ 88 : std::unique_ptr<MediaDecoder> videoDecoder_; 89 : int dstWidth_ {0}; 90 : int dstHeight_ {0}; 91 : const std::string id_; 92 : bool useSink_; 93 : std::istringstream stream_; 94 : MediaIOHandle sdpContext_; 95 : std::unique_ptr<MediaIOHandle> demuxContext_; 96 : std::shared_ptr<SinkClient> sink_; 97 : bool isVideoConfigured_ {false}; 98 : uint16_t mtu_; 99 : int rotation_ {0}; 100 : 101 : std::mutex rotationMtx_; 102 : libav_utils::AVBufferPtr displayMatrix_; 103 : 104 : static int interruptCb(void* ctx); 105 : static int readFunction(void* opaque, uint8_t* buf, int buf_size); 106 : bool configureVideoOutput(); 107 : 108 : ThreadLoop loop_; 109 : 110 : // used by ThreadLoop 111 : bool setup(); 112 : void process(); 113 : void cleanup(); 114 : 115 : std::function<void(void)> keyFrameRequestCallback_; 116 : std::function<void(MediaType, bool)> onSuccessfulSetup_; 117 : std::function<void(const MediaStream& ms)> recorderCallback_; 118 : }; 119 : 120 : } // namespace video 121 : } // namespace jami 122 : 123 : #endif // _VIDEO_RECEIVE_THREAD_H_