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