Ring Daemon 16.0.0
Loading...
Searching...
No Matches
video_sender.cpp
Go to the documentation of this file.
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 "video_sender.h"
19#include "video_mixer.h"
20#include "socket_pair.h"
21#include "client/videomanager.h"
22#include "logger.h"
23#include "manager.h"
24#include "media_device.h"
25#include "sip/sipcall.h"
26#ifdef RING_ACCEL
27#include "accel.h"
28#endif
29
30#include <map>
31#include <unistd.h>
32
33namespace jami {
34namespace video {
35
36using std::string;
37
38VideoSender::VideoSender(const std::string& dest,
39 const MediaStream& opts,
42 const uint16_t seqVal,
44 bool enableHwAccel)
45 : muxContext_(socketPair.createIOContext(mtu))
46 , videoEncoder_(new MediaEncoder)
47{
48 keyFrameFreq_ = opts.frameRate.numerator() * KEY_FRAME_PERIOD;
49 videoEncoder_->openOutput(dest, "rtp");
50 videoEncoder_->setOptions(opts);
51 videoEncoder_->setOptions(args);
52#ifdef RING_ACCEL
53 videoEncoder_->enableAccel(enableHwAccel
54 and Manager::instance().videoPreferences.getEncodingAccelerated());
55#endif
56 videoEncoder_->addStream(*args.codec);
57 videoEncoder_->setInitSeqVal(seqVal);
58 videoEncoder_->setIOContext(muxContext_->getContext());
59}
60
61void
62VideoSender::encodeAndSendVideo(const std::shared_ptr<VideoFrame>& input_frame)
63{
64 int angle = input_frame->getOrientation();
65 if (rotation_ != angle) {
66 rotation_ = angle;
67 if (changeOrientationCallback_)
68 changeOrientationCallback_(rotation_);
69 }
70
71 if (auto packet = input_frame->packet()) {
72 videoEncoder_->send(*packet);
73 } else {
74 bool is_keyframe = forceKeyFrame_ > 0
75 or (keyFrameFreq_ > 0 and (frameNumber_ % keyFrameFreq_) == 0);
76
77 if (is_keyframe)
78 --forceKeyFrame_;
79
80 if (videoEncoder_->encode(input_frame, is_keyframe, frameNumber_++) < 0)
81 JAMI_ERR("encoding failed");
82 }
83#ifdef DEBUG_SDP
84 if (frameNumber_ == 1) // video stream is lazy initialized, wait for first frame
85 videoEncoder_->print_sdp();
86#endif
87}
88
89void
90VideoSender::update(Observable<std::shared_ptr<MediaFrame>>* /*obs*/,
91 const std::shared_ptr<MediaFrame>& frame_p)
92{
93 encodeAndSendVideo(std::dynamic_pointer_cast<VideoFrame>(frame_p));
94}
95
96void
98{
99 JAMI_DBG("Key frame requested");
100 ++forceKeyFrame_;
101}
102
105{
106 return videoEncoder_->getLastSeqValue();
107}
108
109void
111{
112 changeOrientationCallback_ = std::move(cb);
113}
114
115int
117{
118 // The encoder may be destroy during a bitrate change
119 // when a codec parameter like auto quality change
120 if (!videoEncoder_)
121 return -1; // NOK
122
123 return videoEncoder_->setBitrate(br);
124}
125
126} // namespace video
127} // namespace jami
static LIBJAMI_TEST_EXPORT Manager & instance()
Definition manager.cpp:676
VideoSender(const std::string &dest, const MediaStream &opts, const MediaDescription &args, SocketPair &socketPair, const uint16_t seqVal, uint16_t mtu, bool allowHwAccel=true)
void update(Observable< std::shared_ptr< MediaFrame > > *obs, const std::shared_ptr< MediaFrame > &frame_p) override
void setChangeOrientationCallback(std::function< void(int)> cb)
int setBitrate(uint64_t br)
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_DBG(...)
Definition logger.h:216
void emitSignal(Args... args)
Definition ring_signal.h:64
SIPCall are SIP implementation of a normal Call.
MediaDescription Negotiated RTP media slot.