Line data Source code
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 : #pragma once
18 :
19 : #include "media/media_device.h"
20 : #include "media/rtp_session.h"
21 : #include "media/media_stream.h"
22 :
23 : #include "threadloop.h"
24 :
25 : #include <string>
26 : #include <memory>
27 :
28 : namespace jami {
29 :
30 : class AudioInput;
31 : class AudioReceiveThread;
32 : class AudioSender;
33 : class IceSocket;
34 : class MediaRecorder;
35 : class RingBuffer;
36 :
37 : struct RTCPInfo
38 : {
39 : float packetLoss;
40 : unsigned int jitter;
41 : unsigned int nb_sample;
42 : float latency;
43 : };
44 :
45 : class AudioRtpSession : public RtpSession, public std::enable_shared_from_this<AudioRtpSession>
46 : {
47 : public:
48 : AudioRtpSession(const std::string& callId, const std::string& streamId, const std::shared_ptr<MediaRecorder>& rec);
49 : virtual ~AudioRtpSession();
50 :
51 : void start(std::unique_ptr<dhtnet::IceSocket> rtp_sock, std::unique_ptr<dhtnet::IceSocket> rtcp_sock) override;
52 : void restartSender() override;
53 : void stop() override;
54 : void setMuted(bool muted, Direction dir = Direction::SEND) override;
55 :
56 : void initRecorder() override;
57 : void deinitRecorder() override;
58 :
59 329 : std::shared_ptr<AudioInput>& getAudioLocal() { return audioInput_; }
60 337 : std::unique_ptr<AudioReceiveThread>& getAudioReceive() { return receiveThread_; }
61 :
62 : void setVoiceCallback(std::function<void(bool)> cb);
63 :
64 : private:
65 : void startSender();
66 : void startReceiver();
67 : bool check_RCTP_Info_RR(RTCPInfo& rtcpi);
68 : void adaptQualityAndBitrate();
69 : void dropProcessing(RTCPInfo* rtcpi);
70 : void setNewPacketLoss(unsigned int newPL);
71 : float getPonderateLoss(float lastLoss);
72 :
73 : std::unique_ptr<AudioSender> sender_;
74 : std::unique_ptr<AudioReceiveThread> receiveThread_;
75 : std::shared_ptr<AudioInput> audioInput_;
76 : std::shared_ptr<RingBuffer> ringbuffer_;
77 : uint16_t initSeqVal_ {0};
78 : bool muteState_ {false};
79 : unsigned packetLoss_ {10};
80 : DeviceParams localAudioParams_;
81 :
82 : InterruptedThreadLoop rtcpCheckerThread_;
83 : void processRtcpChecker();
84 :
85 : // Interval in seconds between RTCP checking
86 373 : std::chrono::seconds rtcp_checking_interval {4};
87 :
88 : std::function<void(bool)> voiceCallback_;
89 :
90 : void attachRemoteRecorder(const MediaStream& ms);
91 : void attachLocalRecorder(const MediaStream& ms);
92 : };
93 :
94 : } // namespace jami
|