Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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, 49 : const std::string& streamId, 50 : const std::shared_ptr<MediaRecorder>& rec); 51 : virtual ~AudioRtpSession(); 52 : 53 : void start(std::unique_ptr<dhtnet::IceSocket> rtp_sock, std::unique_ptr<dhtnet::IceSocket> rtcp_sock) override; 54 : void restartSender() override; 55 : void stop() override; 56 : void setMuted(bool muted, Direction dir = Direction::SEND) override; 57 : 58 : void initRecorder() override; 59 : void deinitRecorder() override; 60 : 61 226 : std::shared_ptr<AudioInput>& getAudioLocal() { return audioInput_; } 62 234 : std::unique_ptr<AudioReceiveThread>& getAudioReceive() { return receiveThread_; } 63 : 64 : void setVoiceCallback(std::function<void(bool)> cb); 65 : 66 : private: 67 : void startSender(); 68 : void startReceiver(); 69 : bool check_RCTP_Info_RR(RTCPInfo& rtcpi); 70 : void adaptQualityAndBitrate(); 71 : void dropProcessing(RTCPInfo* rtcpi); 72 : void setNewPacketLoss(unsigned int newPL); 73 : float getPonderateLoss(float lastLoss); 74 : 75 : std::unique_ptr<AudioSender> sender_; 76 : std::unique_ptr<AudioReceiveThread> receiveThread_; 77 : std::shared_ptr<AudioInput> audioInput_; 78 : std::shared_ptr<RingBuffer> ringbuffer_; 79 : uint16_t initSeqVal_ {0}; 80 : bool muteState_ {false}; 81 : unsigned packetLoss_ {10}; 82 : DeviceParams localAudioParams_; 83 : 84 : InterruptedThreadLoop rtcpCheckerThread_; 85 : void processRtcpChecker(); 86 : 87 : // Interval in seconds between RTCP checking 88 397 : std::chrono::seconds rtcp_checking_interval {4}; 89 : 90 : std::function<void(bool)> voiceCallback_; 91 : 92 : void attachRemoteRecorder(const MediaStream& ms); 93 : void attachLocalRecorder(const MediaStream& ms); 94 : }; 95 : 96 : } // namespace jami