Nagram/TMessagesProj/jni/voip/tgcalls/group/GroupInstanceCustomImpl.cpp

3619 lines
142 KiB
C++
Raw Normal View History

2021-03-19 10:25:58 +00:00
#include "GroupInstanceCustomImpl.h"
#include <memory>
#include <iomanip>
#include "Instance.h"
#include "VideoCaptureInterfaceImpl.h"
#include "VideoCapturerInterface.h"
#include "CodecSelectHelper.h"
#include "Message.h"
#include "platform/PlatformInterface.h"
#include "StaticThreads.h"
#include "GroupNetworkManager.h"
#include "api/audio_codecs/audio_decoder_factory_template.h"
#include "api/audio_codecs/audio_encoder_factory_template.h"
#include "api/audio_codecs/opus/audio_decoder_opus.h"
#include "api/audio_codecs/opus/audio_decoder_multi_channel_opus.h"
#include "api/audio_codecs/opus/audio_encoder_opus.h"
#include "api/audio_codecs/L16/audio_decoder_L16.h"
#include "api/audio_codecs/L16/audio_encoder_L16.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "media/engine/webrtc_media_engine.h"
#include "system_wrappers/include/field_trial.h"
#include "api/video/builtin_video_bitrate_allocator_factory.h"
#include "call/call.h"
2022-03-11 16:49:54 +00:00
#include "media/base/rtp_utils.h"
2021-03-19 10:25:58 +00:00
#include "api/call/audio_sink.h"
#include "modules/audio_processing/audio_buffer.h"
#include "absl/strings/match.h"
2021-06-25 00:43:10 +00:00
#include "modules/audio_processing/agc2/cpu_features.h"
2022-03-11 16:49:54 +00:00
#include "modules/audio_processing/agc2/vad_wrapper.h"
2021-03-19 10:25:58 +00:00
#include "pc/channel_manager.h"
#include "audio/audio_state.h"
#include "modules/audio_coding/neteq/default_neteq_factory.h"
#include "modules/audio_coding/include/audio_coding_module.h"
2021-07-15 14:24:57 +00:00
#include "common_audio/include/audio_util.h"
2021-08-31 19:06:39 +00:00
#include "modules/audio_device/include/audio_device_data_observer.h"
2022-03-11 16:49:54 +00:00
#include "common_audio/resampler/include/resampler.h"
#include "modules/rtp_rtcp/source/rtp_util.h"
2021-03-19 10:25:58 +00:00
#include "AudioFrame.h"
#include "ThreadLocalObject.h"
#include "Manager.h"
#include "NetworkManager.h"
#include "VideoCaptureInterfaceImpl.h"
#include "platform/PlatformInterface.h"
#include "LogSinkImpl.h"
#include "CodecSelectHelper.h"
2021-08-31 19:06:39 +00:00
#include "AudioStreamingPart.h"
#include "VideoStreamingPart.h"
2021-03-19 10:25:58 +00:00
#include "AudioDeviceHelper.h"
2021-07-15 14:24:57 +00:00
#include "FakeAudioDeviceModule.h"
2021-08-31 19:06:39 +00:00
#include "StreamingMediaContext.h"
2022-03-11 16:49:54 +00:00
#ifdef WEBRTC_IOS
#include "platform/darwin/iOS/tgcalls_audio_device_module_ios.h"
#endif
2021-06-25 00:43:10 +00:00
#include <mutex>
2021-03-19 10:25:58 +00:00
#include <random>
#include <sstream>
#include <iostream>
2021-06-25 00:43:10 +00:00
#ifndef USE_RNNOISE
#define USE_RNNOISE 1
#endif
#if USE_RNNOISE
#include "rnnoise.h"
#endif
#include "GroupJoinPayloadInternal.h"
#include "third-party/json11.hpp"
2021-03-19 10:25:58 +00:00
namespace tgcalls {
namespace {
2021-08-05 20:35:18 +00:00
template <typename Out>
void splitString(const std::string &s, char delim, Out result) {
std::istringstream iss(s);
std::string item;
while (std::getline(iss, item, delim)) {
*result++ = item;
}
}
std::vector<std::string> splitString(const std::string &s, char delim) {
std::vector<std::string> elems;
splitString(s, delim, std::back_inserter(elems));
return elems;
}
2021-03-19 10:25:58 +00:00
static int stringToInt(std::string const &string) {
std::stringstream stringStream(string);
int value = 0;
stringStream >> value;
return value;
}
static std::string intToString(int value) {
std::ostringstream stringStream;
stringStream << value;
return stringStream.str();
}
static std::string uint32ToString(uint32_t value) {
std::ostringstream stringStream;
stringStream << value;
return stringStream.str();
}
static uint32_t stringToUInt32(std::string const &string) {
std::stringstream stringStream(string);
uint32_t value = 0;
stringStream >> value;
return value;
}
static uint16_t stringToUInt16(std::string const &string) {
std::stringstream stringStream(string);
uint16_t value = 0;
stringStream >> value;
return value;
}
static std::string formatTimestampMillis(int64_t timestamp) {
std::ostringstream stringStream;
stringStream << std::fixed << std::setprecision(3) << (double)timestamp / 1000.0;
return stringStream.str();
}
static VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInterface *videoCapture) {
return videoCapture
? static_cast<VideoCaptureInterfaceImpl*>(videoCapture)->object()->getSyncAssumingSameThread()
: nullptr;
}
struct OutgoingVideoFormat {
cricket::VideoCodec videoCodec;
cricket::VideoCodec rtxCodec;
};
static void addDefaultFeedbackParams(cricket::VideoCodec *codec) {
// Don't add any feedback params for RED and ULPFEC.
if (codec->name == cricket::kRedCodecName || codec->name == cricket::kUlpfecCodecName) {
return;
}
codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamRemb, cricket::kParamValueEmpty));
codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc, cricket::kParamValueEmpty));
// Don't add any more feedback params for FLEXFEC.
if (codec->name == cricket::kFlexfecCodecName) {
return;
}
codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamCcm, cricket::kRtcpFbCcmParamFir));
codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamNack, cricket::kParamValueEmpty));
codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamNack, cricket::kRtcpFbNackParamPli));
}
2021-07-15 14:24:57 +00:00
struct H264FormatParameters {
std::string profileLevelId;
std::string packetizationMode;
std::string levelAssymetryAllowed;
};
H264FormatParameters parseH264FormatParameters(webrtc::SdpVideoFormat const &format) {
H264FormatParameters result;
for (const auto &parameter : format.parameters) {
if (parameter.first == "profile-level-id") {
result.profileLevelId = parameter.second;
} else if (parameter.first == "packetization-mode") {
result.packetizationMode = parameter.second;
} else if (parameter.first == "level-asymmetry-allowed") {
result.levelAssymetryAllowed = parameter.second;
}
}
return result;
}
static int getH264ProfileLevelIdPriority(std::string const &profileLevelId) {
if (profileLevelId == cricket::kH264ProfileLevelConstrainedHigh) {
return 0;
} else if (profileLevelId == cricket::kH264ProfileLevelConstrainedBaseline) {
return 1;
} else {
return 2;
}
}
static int getH264PacketizationModePriority(std::string const &packetizationMode) {
if (packetizationMode == "1") {
return 0;
} else {
return 1;
}
}
static int getH264LevelAssymetryAllowedPriority(std::string const &levelAssymetryAllowed) {
if (levelAssymetryAllowed == "1") {
return 0;
} else {
return 1;
}
}
static std::vector<webrtc::SdpVideoFormat> filterSupportedVideoFormats(std::vector<webrtc::SdpVideoFormat> const &formats) {
std::vector<webrtc::SdpVideoFormat> filteredFormats;
std::vector<std::string> filterCodecNames = {
cricket::kVp8CodecName,
cricket::kVp9CodecName,
cricket::kH264CodecName
};
std::vector<webrtc::SdpVideoFormat> vp9Formats;
std::vector<webrtc::SdpVideoFormat> h264Formats;
for (const auto &format : formats) {
if (std::find(filterCodecNames.begin(), filterCodecNames.end(), format.name) == filterCodecNames.end()) {
continue;
}
if (format.name == cricket::kVp9CodecName) {
vp9Formats.push_back(format);
} else if (format.name == cricket::kH264CodecName) {
h264Formats.push_back(format);
} else {
filteredFormats.push_back(format);
}
}
if (!vp9Formats.empty()) {
bool added = false;
for (const auto &format : vp9Formats) {
if (added) {
break;
}
for (const auto &parameter : format.parameters) {
if (parameter.first == "profile-id") {
if (parameter.second == "0") {
filteredFormats.push_back(format);
added = true;
break;
}
}
}
}
if (!added) {
filteredFormats.push_back(vp9Formats[0]);
}
}
if (!h264Formats.empty()) {
std::sort(h264Formats.begin(), h264Formats.end(), [](const webrtc::SdpVideoFormat &lhs, const webrtc::SdpVideoFormat &rhs) {
auto lhsParameters = parseH264FormatParameters(lhs);
auto rhsParameters = parseH264FormatParameters(rhs);
int lhsLevelIdPriority = getH264ProfileLevelIdPriority(lhsParameters.profileLevelId);
int lhsPacketizationModePriority = getH264PacketizationModePriority(lhsParameters.packetizationMode);
int lhsLevelAssymetryAllowedPriority = getH264LevelAssymetryAllowedPriority(lhsParameters.levelAssymetryAllowed);
int rhsLevelIdPriority = getH264ProfileLevelIdPriority(rhsParameters.profileLevelId);
int rhsPacketizationModePriority = getH264PacketizationModePriority(rhsParameters.packetizationMode);
int rhsLevelAssymetryAllowedPriority = getH264LevelAssymetryAllowedPriority(rhsParameters.levelAssymetryAllowed);
if (lhsLevelIdPriority != rhsLevelIdPriority) {
return lhsLevelIdPriority < rhsLevelIdPriority;
}
if (lhsPacketizationModePriority != rhsPacketizationModePriority) {
return lhsPacketizationModePriority < rhsPacketizationModePriority;
}
if (lhsLevelAssymetryAllowedPriority != rhsLevelAssymetryAllowedPriority) {
return lhsLevelAssymetryAllowedPriority < rhsLevelAssymetryAllowedPriority;
}
2022-03-11 16:49:54 +00:00
return false;
2021-07-15 14:24:57 +00:00
});
filteredFormats.push_back(h264Formats[0]);
}
return filteredFormats;
}
2021-06-25 00:43:10 +00:00
static std::vector<OutgoingVideoFormat> assignPayloadTypes(std::vector<webrtc::SdpVideoFormat> const &formats) {
2021-03-19 10:25:58 +00:00
if (formats.empty()) {
2021-06-25 00:43:10 +00:00
return {};
2021-03-19 10:25:58 +00:00
}
constexpr int kFirstDynamicPayloadType = 100;
constexpr int kLastDynamicPayloadType = 127;
int payload_type = kFirstDynamicPayloadType;
2021-06-25 00:43:10 +00:00
std::vector<OutgoingVideoFormat> result;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
std::vector<std::string> filterCodecNames = {
cricket::kVp8CodecName,
2021-07-15 14:24:57 +00:00
cricket::kVp9CodecName,
cricket::kH264CodecName,
2021-06-25 00:43:10 +00:00
};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
for (const auto &codecName : filterCodecNames) {
for (const auto &format : formats) {
if (format.name != codecName) {
continue;
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
cricket::VideoCodec codec(format);
codec.id = payload_type;
addDefaultFeedbackParams(&codec);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
OutgoingVideoFormat resultFormat;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
resultFormat.videoCodec = codec;
2021-03-19 10:25:58 +00:00
// Increment payload type.
++payload_type;
if (payload_type > kLastDynamicPayloadType) {
RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest.";
break;
}
2021-06-25 00:43:10 +00:00
// Add associated RTX codec for non-FEC codecs.
if (!absl::EqualsIgnoreCase(codec.name, cricket::kUlpfecCodecName) &&
!absl::EqualsIgnoreCase(codec.name, cricket::kFlexfecCodecName)) {
resultFormat.rtxCodec = cricket::VideoCodec::CreateRtxCodec(payload_type, codec.id);
// Increment payload type.
++payload_type;
if (payload_type > kLastDynamicPayloadType) {
RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest.";
break;
}
}
result.push_back(std::move(resultFormat));
2021-03-19 10:25:58 +00:00
}
}
2021-06-25 00:43:10 +00:00
2021-03-19 10:25:58 +00:00
return result;
}
struct VideoSsrcs {
struct SimulcastLayer {
uint32_t ssrc = 0;
uint32_t fidSsrc = 0;
SimulcastLayer(uint32_t ssrc_, uint32_t fidSsrc_) :
ssrc(ssrc_), fidSsrc(fidSsrc_) {
}
SimulcastLayer(const SimulcastLayer &other) :
ssrc(other.ssrc), fidSsrc(other.fidSsrc) {
}
};
std::vector<SimulcastLayer> simulcastLayers;
VideoSsrcs() {
}
VideoSsrcs(const VideoSsrcs &other) :
simulcastLayers(other.simulcastLayers) {
}
};
2021-07-15 14:24:57 +00:00
struct InternalGroupLevelValue {
GroupLevelValue value;
int64_t timestamp = 0;
};
2021-03-19 10:25:58 +00:00
struct ChannelId {
uint32_t networkSsrc = 0;
uint32_t actualSsrc = 0;
ChannelId(uint32_t networkSsrc_, uint32_t actualSsrc_) :
networkSsrc(networkSsrc_),
actualSsrc(actualSsrc_) {
}
explicit ChannelId(uint32_t networkSsrc_) :
networkSsrc(networkSsrc_),
actualSsrc(networkSsrc_) {
}
bool operator <(const ChannelId& rhs) const {
if (networkSsrc != rhs.networkSsrc) {
return networkSsrc < rhs.networkSsrc;
}
return actualSsrc < rhs.actualSsrc;
}
std::string name() {
if (networkSsrc == actualSsrc) {
return uint32ToString(networkSsrc);
} else {
return uint32ToString(networkSsrc) + "to" + uint32ToString(actualSsrc);
}
}
};
2021-06-25 00:43:10 +00:00
struct VideoChannelId {
std::string endpointId;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
explicit VideoChannelId(std::string const &endpointId_) :
endpointId(endpointId_) {
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
bool operator <(const VideoChannelId& rhs) const {
return endpointId < rhs.endpointId;
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
struct ChannelSsrcInfo {
enum class Type {
Audio,
Video
};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
Type type = Type::Audio;
std::vector<uint32_t> allSsrcs;
std::string videoEndpointId;
};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
struct RequestedMediaChannelDescriptions {
std::shared_ptr<RequestMediaChannelDescriptionTask> task;
std::vector<uint32_t> ssrcs;
RequestedMediaChannelDescriptions(std::shared_ptr<RequestMediaChannelDescriptionTask> task_, std::vector<uint32_t> ssrcs_) :
task(task_), ssrcs(std::move(ssrcs_)) {
}
2021-03-19 10:25:58 +00:00
};
2021-04-09 13:17:32 +00:00
static const int kVadResultHistoryLength = 6;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
class VadHistory {
2021-03-19 10:25:58 +00:00
private:
float _vadResultHistory[kVadResultHistoryLength];
public:
2021-06-25 00:43:10 +00:00
VadHistory() {
2021-03-19 10:25:58 +00:00
for (float & i : _vadResultHistory) {
i = 0.0f;
}
2021-06-25 00:43:10 +00:00
}
~VadHistory() = default;
bool update(float vadProbability) {
if (vadProbability >= 0.0f) {
for (int i = 1; i < kVadResultHistoryLength; i++) {
_vadResultHistory[i - 1] = _vadResultHistory[i];
}
_vadResultHistory[kVadResultHistoryLength - 1] = vadProbability;
}
float movingAverage = 0.0f;
for (float i : _vadResultHistory) {
movingAverage += i;
}
movingAverage /= (float)kVadResultHistoryLength;
return movingAverage > 0.6f;
}
};
class CombinedVad {
private:
2022-03-11 16:49:54 +00:00
webrtc::VoiceActivityDetectorWrapper _vadWithLevel;
2021-06-25 00:43:10 +00:00
VadHistory _history;
bool _countFrames;
std::atomic<int32_t> _waitingFramesToProcess{0};
public:
2022-03-11 16:49:54 +00:00
CombinedVad() :
_vadWithLevel(500, webrtc::GetAvailableCpuFeatures(), webrtc::AudioProcessing::kSampleRate48kHz) {
2021-03-19 10:25:58 +00:00
}
~CombinedVad() = default;
2021-04-09 13:17:32 +00:00
bool incWaitingFrames() {
if (_waitingFramesToProcess > 5) {
return false;
}
_waitingFramesToProcess++;
return true;
}
2021-03-19 10:25:58 +00:00
bool update(webrtc::AudioBuffer *buffer) {
2022-03-11 16:49:54 +00:00
if (buffer->num_channels() <= 0) {
return _history.update(0.0f);
}
webrtc::AudioFrameView<float> frameView(buffer->channels(), (int)(buffer->num_channels()), (int)(buffer->num_frames()));
float peak = 0.0f;
for (const auto &x : frameView.channel(0)) {
peak = std::max(std::fabs(x), peak);
2021-03-19 10:25:58 +00:00
}
2022-03-11 16:49:54 +00:00
if (peak <= 0.01f) {
return _history.update(false);
}
auto result = _vadWithLevel.Analyze(frameView);
return _history.update(result);
}
bool update() {
return _history.update(0.0f);
2021-06-25 00:43:10 +00:00
}
};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
class SparseVad {
public:
SparseVad() {
}
bool update(webrtc::AudioBuffer *buffer) {
_sampleCount += buffer->num_frames();
if (_sampleCount < 400) {
return _currentValue;
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
_sampleCount = 0;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_currentValue = _vad.update(buffer);
return _currentValue;
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
private:
CombinedVad _vad;
bool _currentValue = false;
size_t _sampleCount = 0;
2021-03-19 10:25:58 +00:00
};
class AudioSinkImpl: public webrtc::AudioSinkInterface {
public:
struct Update {
float level = 0.0f;
bool hasSpeech = false;
2021-03-19 10:25:58 +00:00
Update(float level_, bool hasSpech_) :
level(level_), hasSpeech(hasSpech_) {
}
Update(const Update &other) :
level(other.level), hasSpeech(other.hasSpeech) {
2021-03-19 10:25:58 +00:00
}
};
public:
AudioSinkImpl(std::function<void(Update)> update,
ChannelId channel_id, std::function<void(uint32_t, const AudioFrame &)> onAudioFrame) :
_update(update), _channel_id(channel_id), _onAudioFrame(std::move(onAudioFrame)) {
2022-03-11 16:49:54 +00:00
//_vad = std::make_shared<CombinedVad>(true);
2021-03-19 10:25:58 +00:00
}
virtual ~AudioSinkImpl() {
}
virtual void OnData(const Data& audio) override {
if (_onAudioFrame) {
AudioFrame frame;
frame.audio_samples = audio.data;
frame.num_samples = audio.samples_per_channel;
frame.bytes_per_sample = 2;
frame.num_channels = audio.channels;
frame.samples_per_sec = audio.sample_rate;
frame.elapsed_time_ms = 0;
frame.ntp_time_ms = 0;
_onAudioFrame(_channel_id.actualSsrc, frame);
}
if (_update && audio.channels == 1) {
2021-04-09 13:17:32 +00:00
const int16_t *samples = (const int16_t *)audio.data;
2021-03-19 10:25:58 +00:00
int numberOfSamplesInFrame = (int)audio.samples_per_channel;
2021-06-25 00:43:10 +00:00
int16_t currentPeak = 0;
2021-03-19 10:25:58 +00:00
for (int i = 0; i < numberOfSamplesInFrame; i++) {
int16_t sample = samples[i];
if (sample < 0) {
sample = -sample;
}
if (_peak < sample) {
_peak = sample;
}
2021-06-25 00:43:10 +00:00
if (currentPeak < sample) {
currentPeak = sample;
}
2021-03-19 10:25:58 +00:00
_peakCount += 1;
}
2021-06-25 00:43:10 +00:00
if (_peakCount >= 4400) {
float level = ((float)(_peak)) / 8000.0f;
2021-03-19 10:25:58 +00:00
_peak = 0;
_peakCount = 0;
_update(Update(level, level >= 1.0f));
2021-03-19 10:25:58 +00:00
}
}
}
private:
std::function<void(Update)> _update;
ChannelId _channel_id;
std::function<void(uint32_t, const AudioFrame &)> _onAudioFrame;
std::shared_ptr<CombinedVad> _vad;
int _peakCount = 0;
uint16_t _peak = 0;
};
class VideoSinkImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
public:
2021-07-15 14:24:57 +00:00
VideoSinkImpl(std::string const &endpointId) :
_endpointId(endpointId) {
2021-03-19 10:25:58 +00:00
}
virtual ~VideoSinkImpl() {
}
virtual void OnFrame(const webrtc::VideoFrame& frame) override {
2021-06-25 00:43:10 +00:00
std::unique_lock<std::mutex> lock{ _mutex };
2022-03-11 16:49:54 +00:00
int64_t timestamp = rtc::TimeMillis();
2021-07-15 14:24:57 +00:00
if (_lastFrame) {
if (_lastFrame->video_frame_buffer()->width() != frame.video_frame_buffer()->width()) {
int64_t deltaTime = std::abs(_lastFrameSizeChangeTimestamp - timestamp);
if (deltaTime < 200) {
RTC_LOG(LS_WARNING) << "VideoSinkImpl: frequent frame size change detected for " << _endpointId << ": " << _lastFrameSizeChangeHeight << " -> " << _lastFrame->video_frame_buffer()->height() << " -> " << frame.video_frame_buffer()->height() << " in " << deltaTime << " ms";
}
_lastFrameSizeChangeHeight = _lastFrame->video_frame_buffer()->height();
_lastFrameSizeChangeTimestamp = timestamp;
}
} else {
_lastFrameSizeChangeHeight = 0;
_lastFrameSizeChangeTimestamp = timestamp;
}
2022-03-11 16:49:54 +00:00
_lastFrame = frame;
2021-03-19 10:25:58 +00:00
for (int i = (int)(_sinks.size()) - 1; i >= 0; i--) {
auto strong = _sinks[i].lock();
if (!strong) {
_sinks.erase(_sinks.begin() + i);
} else {
strong->OnFrame(frame);
}
}
}
virtual void OnDiscardedFrame() override {
2021-06-25 00:43:10 +00:00
std::unique_lock<std::mutex> lock{ _mutex };
2021-03-19 10:25:58 +00:00
for (int i = (int)(_sinks.size()) - 1; i >= 0; i--) {
auto strong = _sinks[i].lock();
if (!strong) {
_sinks.erase(_sinks.begin() + i);
} else {
strong->OnDiscardedFrame();
}
}
}
void addSink(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) {
2022-03-11 16:49:54 +00:00
if (const auto strong = impl.lock()) {
std::unique_lock<std::mutex> lock{ _mutex };
_sinks.push_back(impl);
if (_lastFrame) {
2021-03-19 10:25:58 +00:00
strong->OnFrame(_lastFrame.value());
}
}
}
2021-07-30 14:49:55 +00:00
std::vector<std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>> getSinks() {
return _sinks;
}
2021-03-19 10:25:58 +00:00
private:
std::vector<std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>> _sinks;
absl::optional<webrtc::VideoFrame> _lastFrame;
2021-06-25 00:43:10 +00:00
std::mutex _mutex;
2021-07-15 14:24:57 +00:00
int64_t _lastFrameSizeChangeTimestamp = 0;
int _lastFrameSizeChangeHeight = 0;
std::string _endpointId;
2021-06-25 00:43:10 +00:00
2021-03-19 10:25:58 +00:00
};
2021-06-25 00:43:10 +00:00
struct NoiseSuppressionConfiguration {
NoiseSuppressionConfiguration(bool isEnabled_) :
isEnabled(isEnabled_) {
}
bool isEnabled = false;
};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
#if USE_RNNOISE
class AudioCapturePostProcessor : public webrtc::CustomProcessing {
public:
2021-07-15 14:24:57 +00:00
AudioCapturePostProcessor(std::function<void(GroupLevelValue const &)> updated, std::shared_ptr<NoiseSuppressionConfiguration> noiseSuppressionConfiguration, std::vector<float> *externalAudioSamples, webrtc::Mutex *externalAudioSamplesMutex) :
2021-06-25 00:43:10 +00:00
_updated(updated),
2021-07-15 14:24:57 +00:00
_noiseSuppressionConfiguration(noiseSuppressionConfiguration),
_externalAudioSamples(externalAudioSamples),
_externalAudioSamplesMutex(externalAudioSamplesMutex) {
2021-06-25 00:43:10 +00:00
int frameSize = rnnoise_get_frame_size();
_frameSamples.resize(frameSize);
_denoiseState = rnnoise_create(nullptr);
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
virtual ~AudioCapturePostProcessor() {
if (_denoiseState) {
rnnoise_destroy(_denoiseState);
}
}
private:
virtual void Initialize(int sample_rate_hz, int num_channels) override {
}
virtual void Process(webrtc::AudioBuffer *buffer) override {
2021-03-19 10:25:58 +00:00
if (!buffer) {
return;
}
if (buffer->num_channels() != 1) {
return;
}
2021-06-25 00:43:10 +00:00
if (!_denoiseState) {
return;
}
if (buffer->num_frames() != _frameSamples.size()) {
return;
}
float sourcePeak = 0.0f;
float *sourceSamples = buffer->channels()[0];
for (int i = 0; i < _frameSamples.size(); i++) {
sourcePeak = std::max(std::fabs(sourceSamples[i]), sourcePeak);
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
if (_noiseSuppressionConfiguration->isEnabled) {
float vadProbability = 0.0f;
if (sourcePeak >= 0.01f) {
vadProbability = rnnoise_process_frame(_denoiseState, _frameSamples.data(), buffer->channels()[0]);
if (_noiseSuppressionConfiguration->isEnabled) {
memcpy(buffer->channels()[0], _frameSamples.data(), _frameSamples.size() * sizeof(float));
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
float peak = 0;
int peakCount = 0;
const float *samples = buffer->channels_const()[0];
for (int i = 0; i < buffer->num_frames(); i++) {
float sample = samples[i];
if (sample < 0) {
sample = -sample;
}
if (peak < sample) {
peak = sample;
}
peakCount += 1;
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
bool vadStatus = _history.update(vadProbability);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_peakCount += peakCount;
if (_peak < peak) {
_peak = peak;
}
if (_peakCount >= 4400) {
float level = _peak / 4000.0f;
_peak = 0;
_peakCount = 0;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_updated(GroupLevelValue{
level,
vadStatus,
});
}
} else {
float peak = 0;
int peakCount = 0;
const float *samples = buffer->channels_const()[0];
for (int i = 0; i < buffer->num_frames(); i++) {
float sample = samples[i];
if (sample < 0) {
sample = -sample;
}
if (peak < sample) {
peak = sample;
}
peakCount += 1;
}
_peakCount += peakCount;
if (_peak < peak) {
_peak = peak;
}
if (_peakCount >= 1200) {
float level = _peak / 8000.0f;
_peak = 0;
_peakCount = 0;
_updated(GroupLevelValue{
level,
level >= 1.0f,
});
}
2021-03-19 10:25:58 +00:00
}
2021-07-15 14:24:57 +00:00
2021-07-30 14:49:55 +00:00
if (_externalAudioSamplesMutex && _externalAudioSamples) {
_externalAudioSamplesMutex->Lock();
if (!_externalAudioSamples->empty()) {
float *bufferData = buffer->channels()[0];
int takenSamples = 0;
for (int i = 0; i < _externalAudioSamples->size() && i < _frameSamples.size(); i++) {
float sample = (*_externalAudioSamples)[i];
sample += bufferData[i];
sample = std::min(sample, 32768.f);
sample = std::max(sample, -32768.f);
bufferData[i] = sample;
takenSamples++;
}
if (takenSamples != 0) {
_externalAudioSamples->erase(_externalAudioSamples->begin(), _externalAudioSamples->begin() + takenSamples);
}
2021-07-15 14:24:57 +00:00
}
2021-07-30 14:49:55 +00:00
_externalAudioSamplesMutex->Unlock();
2021-07-15 14:24:57 +00:00
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
virtual std::string ToString() const override {
return "CustomPostProcessing";
}
virtual void SetRuntimeSetting(webrtc::AudioProcessing::RuntimeSetting setting) override {
2021-03-19 10:25:58 +00:00
}
private:
std::function<void(GroupLevelValue const &)> _updated;
2021-06-25 00:43:10 +00:00
std::shared_ptr<NoiseSuppressionConfiguration> _noiseSuppressionConfiguration;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
DenoiseState *_denoiseState = nullptr;
std::vector<float> _frameSamples;
2021-03-19 10:25:58 +00:00
int32_t _peakCount = 0;
float _peak = 0;
2021-06-25 00:43:10 +00:00
VadHistory _history;
SparseVad _vad;
2021-07-15 14:24:57 +00:00
std::vector<float> *_externalAudioSamples = nullptr;
webrtc::Mutex *_externalAudioSamplesMutex = nullptr;
2021-03-19 10:25:58 +00:00
};
2021-06-25 00:43:10 +00:00
#endif
2021-07-15 14:24:57 +00:00
class ExternalAudioRecorder : public FakeAudioDeviceModule::Recorder {
public:
ExternalAudioRecorder(std::vector<float> *externalAudioSamples, webrtc::Mutex *externalAudioSamplesMutex) :
_externalAudioSamples(externalAudioSamples),
_externalAudioSamplesMutex(externalAudioSamplesMutex) {
_samples.resize(480);
}
virtual ~ExternalAudioRecorder() {
}
virtual AudioFrame Record() override {
AudioFrame result;
_externalAudioSamplesMutex->Lock();
if (!_externalAudioSamples->empty() && _externalAudioSamples->size() >= 480) {
size_t takenSamples = std::min(_samples.size(), _externalAudioSamples->size());
webrtc::FloatS16ToS16(_externalAudioSamples->data(), takenSamples, _samples.data());
result.num_samples = takenSamples;
if (takenSamples != 0) {
_externalAudioSamples->erase(_externalAudioSamples->begin(), _externalAudioSamples->begin() + takenSamples);
}
} else {
result.num_samples = 0;
}
_externalAudioSamplesMutex->Unlock();
result.audio_samples = _samples.data();
result.bytes_per_sample = 2;
result.num_channels = 1;
result.samples_per_sec = 48000;
result.elapsed_time_ms = 0;
result.ntp_time_ms = 0;
return result;
}
virtual int32_t WaitForUs() override {
_externalAudioSamplesMutex->Lock();
_externalAudioSamplesMutex->Unlock();
return 1000;
}
private:
std::vector<float> *_externalAudioSamples = nullptr;
webrtc::Mutex *_externalAudioSamplesMutex = nullptr;
std::vector<int16_t> _samples;
};
2021-03-19 10:25:58 +00:00
class IncomingAudioChannel : public sigslot::has_slots<> {
public:
IncomingAudioChannel(
cricket::ChannelManager *channelManager,
webrtc::Call *call,
webrtc::RtpTransport *rtpTransport,
rtc::UniqueRandomIdGenerator *randomIdGenerator,
bool isRawPcm,
ChannelId ssrc,
std::function<void(AudioSinkImpl::Update)> &&onAudioLevelUpdated,
std::function<void(uint32_t, const AudioFrame &)> onAudioFrame,
2021-06-25 00:43:10 +00:00
std::shared_ptr<Threads> threads) :
_threads(threads),
2021-03-19 10:25:58 +00:00
_ssrc(ssrc),
_channelManager(channelManager),
_call(call) {
_creationTimestamp = rtc::TimeMillis();
2021-06-25 00:43:10 +00:00
threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this, rtpTransport, ssrc, onAudioFrame = std::move(onAudioFrame), onAudioLevelUpdated = std::move(onAudioLevelUpdated), randomIdGenerator, isRawPcm]() mutable {
cricket::AudioOptions audioOptions;
audioOptions.audio_jitter_buffer_fast_accelerate = true;
audioOptions.audio_jitter_buffer_min_delay_ms = 50;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
std::string streamId = std::string("stream") + ssrc.name();
2021-03-19 10:25:58 +00:00
2022-03-13 01:58:00 +00:00
_audioChannel = _channelManager->CreateVoiceChannel(_call, cricket::MediaConfig(), rtpTransport, _threads->getWorkerThread(), std::string("audio") + uint32ToString(ssrc.networkSsrc), false, GroupNetworkManager::getDefaulCryptoOptions(), randomIdGenerator, audioOptions);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
const uint8_t opusPTimeMs = 120;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
cricket::AudioCodec opusCodec(111, "opus", 48000, 0, 2);
opusCodec.SetParam(cricket::kCodecParamUseInbandFec, 1);
opusCodec.SetParam(cricket::kCodecParamPTime, opusPTimeMs);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
cricket::AudioCodec pcmCodec(112, "l16", 48000, 0, 1);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
auto outgoingAudioDescription = std::make_unique<cricket::AudioContentDescription>();
if (!isRawPcm) {
outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, 1));
outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, 2));
outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, 3));
}
outgoingAudioDescription->set_rtcp_mux(true);
outgoingAudioDescription->set_rtcp_reduced_size(true);
outgoingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly);
outgoingAudioDescription->set_codecs({ opusCodec, pcmCodec });
outgoingAudioDescription->set_bandwidth(1300000);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
auto incomingAudioDescription = std::make_unique<cricket::AudioContentDescription>();
if (!isRawPcm) {
incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, 1));
incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, 2));
incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, 3));
}
incomingAudioDescription->set_rtcp_mux(true);
incomingAudioDescription->set_rtcp_reduced_size(true);
incomingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly);
incomingAudioDescription->set_codecs({ opusCodec, pcmCodec });
incomingAudioDescription->set_bandwidth(1300000);
cricket::StreamParams streamParams = cricket::StreamParams::CreateLegacy(ssrc.networkSsrc);
streamParams.set_stream_ids({ streamId });
incomingAudioDescription->AddStream(streamParams);
2021-03-19 10:25:58 +00:00
2022-03-13 01:58:00 +00:00
_audioChannel->SetLocalContent(outgoingAudioDescription.get(), webrtc::SdpType::kOffer, nullptr);
_audioChannel->SetRemoteContent(incomingAudioDescription.get(), webrtc::SdpType::kAnswer, nullptr);
2021-06-25 00:43:10 +00:00
_audioChannel->SetPayloadTypeDemuxingEnabled(false);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
outgoingAudioDescription.reset();
incomingAudioDescription.reset();
2021-03-19 10:25:58 +00:00
2021-07-15 14:24:57 +00:00
if (_ssrc.actualSsrc != 1) {
std::unique_ptr<AudioSinkImpl> audioLevelSink(new AudioSinkImpl(std::move(onAudioLevelUpdated), _ssrc, std::move(onAudioFrame)));
_audioChannel->media_channel()->SetRawAudioSink(ssrc.networkSsrc, std::move(audioLevelSink));
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_audioChannel->Enable(true);
});
2021-03-19 10:25:58 +00:00
}
~IncomingAudioChannel() {
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_channelManager->DestroyVoiceChannel(_audioChannel);
_audioChannel = nullptr;
});
2021-03-19 10:25:58 +00:00
}
void setVolume(double value) {
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this, value]() {
_audioChannel->media_channel()->SetOutputVolume(_ssrc.networkSsrc, value);
});
2021-03-19 10:25:58 +00:00
}
void updateActivity() {
_activityTimestamp = rtc::TimeMillis();
}
int64_t getActivity() {
return _activityTimestamp;
}
private:
2021-06-25 00:43:10 +00:00
std::shared_ptr<Threads> _threads;
2021-03-19 10:25:58 +00:00
ChannelId _ssrc;
// Memory is managed by _channelManager
cricket::VoiceChannel *_audioChannel = nullptr;
// Memory is managed externally
cricket::ChannelManager *_channelManager = nullptr;
webrtc::Call *_call = nullptr;
int64_t _creationTimestamp = 0;
int64_t _activityTimestamp = 0;
};
class IncomingVideoChannel : public sigslot::has_slots<> {
public:
IncomingVideoChannel(
cricket::ChannelManager *channelManager,
webrtc::Call *call,
webrtc::RtpTransport *rtpTransport,
rtc::UniqueRandomIdGenerator *randomIdGenerator,
std::vector<webrtc::SdpVideoFormat> const &availableVideoFormats,
2021-06-25 00:43:10 +00:00
GroupJoinVideoInformation sharedVideoInformation,
uint32_t audioSsrc,
VideoChannelDescription::Quality minQuality,
VideoChannelDescription::Quality maxQuality,
GroupParticipantVideoInformation const &description,
std::shared_ptr<Threads> threads) :
_threads(threads),
_endpointId(description.endpointId),
2021-03-19 10:25:58 +00:00
_channelManager(channelManager),
2021-06-25 00:43:10 +00:00
_call(call),
_requestedMinQuality(minQuality),
_requestedMaxQuality(maxQuality) {
2021-07-15 14:24:57 +00:00
_videoSink.reset(new VideoSinkImpl(_endpointId));
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this, rtpTransport, &availableVideoFormats, &description, randomIdGenerator]() mutable {
uint32_t mid = randomIdGenerator->GenerateId();
std::string streamId = std::string("video") + uint32ToString(mid);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory();
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
auto payloadTypes = assignPayloadTypes(availableVideoFormats);
std::vector<cricket::VideoCodec> codecs;
for (const auto &payloadType : payloadTypes) {
codecs.push_back(payloadType.videoCodec);
codecs.push_back(payloadType.rtxCodec);
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
auto outgoingVideoDescription = std::make_unique<cricket::VideoContentDescription>();
outgoingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, 2));
outgoingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, 3));
outgoingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kVideoRotationUri, 13));
outgoingVideoDescription->set_rtcp_mux(true);
outgoingVideoDescription->set_rtcp_reduced_size(true);
outgoingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly);
outgoingVideoDescription->set_codecs(codecs);
outgoingVideoDescription->set_bandwidth(1300000);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
cricket::StreamParams videoRecvStreamParams;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
std::vector<uint32_t> allSsrcs;
for (const auto &group : description.ssrcGroups) {
for (auto ssrc : group.ssrcs) {
if (std::find(allSsrcs.begin(), allSsrcs.end(), ssrc) == allSsrcs.end()) {
allSsrcs.push_back(ssrc);
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
if (group.semantics == "SIM") {
if (_mainVideoSsrc == 0) {
_mainVideoSsrc = group.ssrcs[0];
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
cricket::SsrcGroup parsedGroup(group.semantics, group.ssrcs);
videoRecvStreamParams.ssrc_groups.push_back(parsedGroup);
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
videoRecvStreamParams.ssrcs = allSsrcs;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
if (_mainVideoSsrc == 0) {
if (description.ssrcGroups.size() == 1) {
_mainVideoSsrc = description.ssrcGroups[0].ssrcs[0];
}
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
videoRecvStreamParams.cname = "cname";
videoRecvStreamParams.set_stream_ids({ streamId });
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
auto incomingVideoDescription = std::make_unique<cricket::VideoContentDescription>();
incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, 2));
incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, 3));
incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kVideoRotationUri, 13));
incomingVideoDescription->set_rtcp_mux(true);
incomingVideoDescription->set_rtcp_reduced_size(true);
incomingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly);
incomingVideoDescription->set_codecs(codecs);
incomingVideoDescription->set_bandwidth(1300000);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
incomingVideoDescription->AddStream(videoRecvStreamParams);
2021-03-19 10:25:58 +00:00
2022-03-13 01:58:00 +00:00
_videoChannel = _channelManager->CreateVideoChannel(_call, cricket::MediaConfig(), rtpTransport, _threads->getWorkerThread(), std::string("video") + uint32ToString(mid), false, GroupNetworkManager::getDefaulCryptoOptions(), randomIdGenerator, cricket::VideoOptions(), _videoBitrateAllocatorFactory.get());
_videoChannel->SetLocalContent(outgoingVideoDescription.get(), webrtc::SdpType::kOffer, nullptr);
_videoChannel->SetRemoteContent(incomingVideoDescription.get(), webrtc::SdpType::kAnswer, nullptr);
2021-06-25 00:43:10 +00:00
_videoChannel->SetPayloadTypeDemuxingEnabled(false);
_videoChannel->media_channel()->SetSink(_mainVideoSsrc, _videoSink.get());
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_videoChannel->Enable(true);
});
2021-03-19 10:25:58 +00:00
}
~IncomingVideoChannel() {
2021-06-25 00:43:10 +00:00
//_videoChannel->SignalSentPacket().disconnect(this);
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_videoChannel->Enable(false);
_channelManager->DestroyVideoChannel(_videoChannel);
_videoChannel = nullptr;
});
2021-03-19 10:25:58 +00:00
}
void addSink(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) {
_videoSink->addSink(impl);
}
2021-07-30 14:49:55 +00:00
std::vector<std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>> getSinks() {
return _videoSink->getSinks();
}
2021-06-25 00:43:10 +00:00
std::string const &endpointId() {
return _endpointId;
}
VideoChannelDescription::Quality requestedMinQuality() {
return _requestedMinQuality;
}
VideoChannelDescription::Quality requestedMaxQuality() {
return _requestedMaxQuality;
}
void setRequstedMinQuality(VideoChannelDescription::Quality quality) {
_requestedMinQuality = quality;
}
void setRequstedMaxQuality(VideoChannelDescription::Quality quality) {
_requestedMaxQuality = quality;
2021-03-19 10:25:58 +00:00
}
2021-08-05 20:35:18 +00:00
void setStats(absl::optional<GroupInstanceStats::IncomingVideoStats> stats) {
_stats = stats;
}
absl::optional<GroupInstanceStats::IncomingVideoStats> getStats() {
return _stats;
}
2021-03-19 10:25:58 +00:00
private:
2021-06-25 00:43:10 +00:00
std::shared_ptr<Threads> _threads;
2021-03-19 10:25:58 +00:00
uint32_t _mainVideoSsrc = 0;
2021-06-25 00:43:10 +00:00
std::string _endpointId;
2021-03-19 10:25:58 +00:00
std::unique_ptr<VideoSinkImpl> _videoSink;
std::vector<GroupJoinPayloadVideoSourceGroup> _ssrcGroups;
std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory;
// Memory is managed by _channelManager
cricket::VideoChannel *_videoChannel;
// Memory is managed externally
cricket::ChannelManager *_channelManager = nullptr;
webrtc::Call *_call = nullptr;
2021-06-25 00:43:10 +00:00
VideoChannelDescription::Quality _requestedMinQuality = VideoChannelDescription::Quality::Thumbnail;
VideoChannelDescription::Quality _requestedMaxQuality = VideoChannelDescription::Quality::Thumbnail;
2021-08-05 20:35:18 +00:00
absl::optional<GroupInstanceStats::IncomingVideoStats> _stats;
2021-03-19 10:25:58 +00:00
};
class MissingSsrcPacketBuffer {
public:
MissingSsrcPacketBuffer(int limit) :
_limit(limit) {
}
~MissingSsrcPacketBuffer() {
}
void add(uint32_t ssrc, rtc::CopyOnWriteBuffer const &packet) {
if (_packets.size() == _limit) {
_packets.erase(_packets.begin());
}
_packets.push_back(std::make_pair(ssrc, packet));
}
std::vector<rtc::CopyOnWriteBuffer> get(uint32_t ssrc) {
std::vector<rtc::CopyOnWriteBuffer> result;
for (auto it = _packets.begin(); it != _packets.end(); ) {
if (it->first == ssrc) {
result.push_back(it->second);
_packets.erase(it);
} else {
it++;
}
}
return result;
}
private:
int _limit = 0;
std::vector<std::pair<uint32_t, rtc::CopyOnWriteBuffer>> _packets;
};
class RequestedBroadcastPart {
public:
int64_t timestamp = 0;
std::shared_ptr<BroadcastPartTask> task;
explicit RequestedBroadcastPart(int64_t timestamp_, std::shared_ptr<BroadcastPartTask> task_) :
timestamp(timestamp_), task(task_) {
}
};
struct DecodedBroadcastPart {
struct DecodedBroadcastPartChannel {
uint32_t ssrc = 0;
std::vector<int16_t> pcmData;
};
DecodedBroadcastPart(int numSamples_, std::vector<DecodedBroadcastPartChannel> &&_channels) :
numSamples(numSamples_), channels(std::move(_channels)) {
}
int numSamples = 0;
std::vector<DecodedBroadcastPartChannel> channels;
};
2021-06-25 00:43:10 +00:00
std::function<webrtc::VideoTrackSourceInterface*()> videoCaptureToGetVideoSource(std::shared_ptr<VideoCaptureInterface> videoCapture) {
return [videoCapture]() {
VideoCaptureInterfaceObject *videoCaptureImpl = GetVideoCaptureAssumingSameThread(videoCapture.get());
return videoCaptureImpl ? videoCaptureImpl->source() : nullptr;
};
}
2021-08-31 19:06:39 +00:00
class AudioDeviceDataObserverShared {
public:
AudioDeviceDataObserverShared() {
}
~AudioDeviceDataObserverShared() {
}
void setStreamingContext(std::shared_ptr<StreamingMediaContext> streamingContext) {
_mutex.Lock();
_streamingContext = streamingContext;
_mutex.Unlock();
}
void mixAudio(int16_t *audio_samples, const size_t num_samples, const size_t num_channels, const uint32_t samples_per_sec) {
_mutex.Lock();
const auto context = _streamingContext;
_mutex.Unlock();
if (context) {
2022-03-11 16:49:54 +00:00
if (_samplesToResample.size() < 480 * num_channels) {
_samplesToResample.resize(480 * num_channels);
}
memset(_samplesToResample.data(), 0, _samplesToResample.size() * sizeof(int16_t));
context->getAudio(_samplesToResample.data(), 480, num_channels, 48000);
if (_resamplerFrequency != samples_per_sec || _resamplerNumChannels != num_channels) {
_resamplerFrequency = samples_per_sec;
_resamplerNumChannels = num_channels;
_resampler = std::make_unique<webrtc::Resampler>();
if (_resampler->Reset(48000, samples_per_sec, num_channels) == -1) {
_resampler = nullptr;
}
}
if (_resampler) {
size_t outLen = 0;
_resampler->Push(_samplesToResample.data(), _samplesToResample.size(), (int16_t *)audio_samples, num_samples * num_channels, outLen);
}
2021-08-31 19:06:39 +00:00
}
}
private:
webrtc::Mutex _mutex;
2022-03-11 16:49:54 +00:00
std::unique_ptr<webrtc::Resampler> _resampler;
uint32_t _resamplerFrequency = 0;
size_t _resamplerNumChannels = 0;
std::vector<int16_t> _samplesToResample;
2021-08-31 19:06:39 +00:00
std::shared_ptr<StreamingMediaContext> _streamingContext;
};
class AudioDeviceDataObserverImpl : public webrtc::AudioDeviceDataObserver {
public:
AudioDeviceDataObserverImpl(std::shared_ptr<AudioDeviceDataObserverShared> shared) :
_shared(shared) {
}
virtual ~AudioDeviceDataObserverImpl() {
}
virtual void OnCaptureData(const void* audio_samples,
const size_t num_samples,
const size_t bytes_per_sample,
const size_t num_channels,
const uint32_t samples_per_sec) override {
}
virtual void OnRenderData(const void* audio_samples,
const size_t num_samples,
const size_t bytes_per_sample,
const size_t num_channels,
const uint32_t samples_per_sec) override {
2022-03-11 16:49:54 +00:00
if (bytes_per_sample != num_channels * 2) {
2021-08-31 19:06:39 +00:00
return;
}
2022-03-11 16:49:54 +00:00
if (samples_per_sec % 100 != 0) {
return;
}
if (num_samples != samples_per_sec / 100) {
2021-08-31 19:06:39 +00:00
return;
}
2022-03-11 16:49:54 +00:00
2021-08-31 19:06:39 +00:00
if (_shared) {
_shared->mixAudio((int16_t *)audio_samples, num_samples, num_channels, samples_per_sec);
}
}
private:
std::shared_ptr<AudioDeviceDataObserverShared> _shared;
};
2022-03-11 16:49:54 +00:00
class CustomNetEqFactory: public webrtc::NetEqFactory {
public:
virtual ~CustomNetEqFactory() = default;
std::unique_ptr<webrtc::NetEq> CreateNetEq(
const webrtc::NetEq::Config& config,
const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory, webrtc::Clock* clock
) const override {
webrtc::NetEq::Config updatedConfig = config;
updatedConfig.sample_rate_hz = 48000;
return webrtc::DefaultNetEqFactory().CreateNetEq(updatedConfig, decoder_factory, clock);
}
};
std::unique_ptr<webrtc::NetEqFactory> createNetEqFactory() {
return std::make_unique<CustomNetEqFactory>();
}
class CustomEchoDetector : public webrtc::EchoDetector {
public:
// (Re-)Initializes the submodule.
virtual void Initialize(int capture_sample_rate_hz,
int num_capture_channels,
int render_sample_rate_hz,
int num_render_channels) override {
}
// Analysis (not changing) of the render signal.
virtual void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override {
}
// Analysis (not changing) of the capture signal.
virtual void AnalyzeCaptureAudio(
rtc::ArrayView<const float> capture_audio) override {
}
// Collect current metrics from the echo detector.
virtual Metrics GetMetrics() const override {
return webrtc::EchoDetector::Metrics();
}
};
2021-03-19 10:25:58 +00:00
} // namespace
class GroupInstanceCustomInternal : public sigslot::has_slots<>, public std::enable_shared_from_this<GroupInstanceCustomInternal> {
public:
GroupInstanceCustomInternal(GroupInstanceDescriptor &&descriptor, std::shared_ptr<Threads> threads) :
_threads(std::move(threads)),
_networkStateUpdated(descriptor.networkStateUpdated),
_audioLevelsUpdated(descriptor.audioLevelsUpdated),
_onAudioFrame(descriptor.onAudioFrame),
2021-06-25 00:43:10 +00:00
_requestMediaChannelDescriptions(descriptor.requestMediaChannelDescriptions),
2021-08-31 19:06:39 +00:00
_requestCurrentTime(descriptor.requestCurrentTime),
_requestAudioBroadcastPart(descriptor.requestAudioBroadcastPart),
_requestVideoBroadcastPart(descriptor.requestVideoBroadcastPart),
2021-03-19 10:25:58 +00:00
_videoCapture(descriptor.videoCapture),
2021-07-15 14:24:57 +00:00
_videoCaptureSink(new VideoSinkImpl("VideoCapture")),
2021-06-25 00:43:10 +00:00
_getVideoSource(descriptor.getVideoSource),
2021-04-09 13:17:32 +00:00
_disableIncomingChannels(descriptor.disableIncomingChannels),
_useDummyChannel(descriptor.useDummyChannel),
2021-06-25 00:43:10 +00:00
_outgoingAudioBitrateKbit(descriptor.outgoingAudioBitrateKbit),
_disableOutgoingAudioProcessing(descriptor.disableOutgoingAudioProcessing),
2022-03-11 16:49:54 +00:00
#ifdef WEBRTC_IOS
_disableAudioInput(descriptor.disableAudioInput),
#endif
2021-06-25 00:43:10 +00:00
_minOutgoingVideoBitrateKbit(descriptor.minOutgoingVideoBitrateKbit),
_videoContentType(descriptor.videoContentType),
_videoCodecPreferences(std::move(descriptor.videoCodecPreferences)),
2021-03-19 10:25:58 +00:00
_eventLog(std::make_unique<webrtc::RtcEventLogNull>()),
_taskQueueFactory(webrtc::CreateDefaultTaskQueueFactory()),
2022-03-11 16:49:54 +00:00
_netEqFactory(createNetEqFactory()),
2021-06-25 00:43:10 +00:00
_createAudioDeviceModule(descriptor.createAudioDeviceModule),
2021-03-19 10:25:58 +00:00
_initialInputDeviceId(std::move(descriptor.initialInputDeviceId)),
_initialOutputDeviceId(std::move(descriptor.initialOutputDeviceId)),
2021-06-25 00:43:10 +00:00
_missingPacketBuffer(50),
2021-03-19 10:25:58 +00:00
_platformContext(descriptor.platformContext) {
assert(_threads->getMediaThread()->IsCurrent());
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this] {
_workerThreadSafery = webrtc::PendingTaskSafetyFlag::Create();
});
2021-07-15 14:24:57 +00:00
_threads->getNetworkThread()->Invoke<void>(RTC_FROM_HERE, [this] {
_networkThreadSafery = webrtc::PendingTaskSafetyFlag::Create();
});
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
if (_videoCapture) {
assert(!_getVideoSource);
_getVideoSource = videoCaptureToGetVideoSource(std::move(descriptor.videoCapture));
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
generateSsrcs();
_noiseSuppressionConfiguration = std::make_shared<NoiseSuppressionConfiguration>(descriptor.initialEnableNoiseSuppression);
2021-07-15 14:24:57 +00:00
_externalAudioRecorder.reset(new ExternalAudioRecorder(&_externalAudioSamples, &_externalAudioSamplesMutex));
2021-03-19 10:25:58 +00:00
}
~GroupInstanceCustomInternal() {
_incomingAudioChannels.clear();
_incomingVideoChannels.clear();
2021-06-25 00:43:10 +00:00
_serverBandwidthProbingVideoSsrc.reset();
2021-03-19 10:25:58 +00:00
destroyOutgoingAudioChannel();
2021-06-25 00:43:10 +00:00
destroyOutgoingVideoChannel();
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_threads->getNetworkThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_rtpTransport->SignalSentPacket.disconnect(this);
_rtpTransport->SignalRtcpPacketReceived.disconnect(this);
});
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_channelManager = nullptr;
if (_audioDeviceModule) {
_audioDeviceModule->Stop();
_audioDeviceModule = nullptr;
}
_call.reset();
});
2021-03-19 10:25:58 +00:00
}
void start() {
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
webrtc::field_trial::InitFieldTrialsFromString(
"WebRTC-Audio-Allocation/min:32kbps,max:32kbps/"
"WebRTC-Audio-OpusMinPacketLossRate/Enabled-1/"
2021-06-25 00:43:10 +00:00
"WebRTC-TaskQueuePacer/Enabled/"
"WebRTC-VP8ConferenceTemporalLayers/1/"
"WebRTC-Audio-MinimizeResamplingOnMobile/Enabled/"
2022-03-11 16:49:54 +00:00
"WebRTC-BweLossExperiment/Enabled/"
2021-03-19 10:25:58 +00:00
);
_networkManager.reset(new ThreadLocalObject<GroupNetworkManager>(_threads->getNetworkThread(), [weak, threads = _threads] () mutable {
return new GroupNetworkManager(
[=](const GroupNetworkManager::State &state) {
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] {
2021-03-19 10:25:58 +00:00
const auto strong = weak.lock();
if (!strong) {
return;
}
strong->setIsRtcConnected(state.isReadyToSendData);
});
},
[=](rtc::CopyOnWriteBuffer const &message, bool isUnresolved) {
2021-06-25 00:43:10 +00:00
if (!isUnresolved) {
return;
}
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, message, isUnresolved]() mutable {
2021-03-19 10:25:58 +00:00
if (const auto strong = weak.lock()) {
strong->receivePacket(message, isUnresolved);
}
});
},
[=](bool isDataChannelOpen) {
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, isDataChannelOpen]() mutable {
2021-03-19 10:25:58 +00:00
if (const auto strong = weak.lock()) {
strong->updateIsDataChannelOpen(isDataChannelOpen);
}
});
},
[=](std::string const &message) {
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, message]() {
2021-03-19 10:25:58 +00:00
if (const auto strong = weak.lock()) {
2021-06-25 00:43:10 +00:00
strong->receiveDataChannelMessage(message);
2021-03-19 10:25:58 +00:00
}
});
2021-07-15 14:24:57 +00:00
},
[=](uint32_t ssrc, uint8_t audioLevel, bool isSpeech) {
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, ssrc, audioLevel, isSpeech]() {
2021-07-15 14:24:57 +00:00
if (const auto strong = weak.lock()) {
strong->updateSsrcAudioLevel(ssrc, audioLevel, isSpeech);
}
});
2021-03-19 10:25:58 +00:00
}, threads);
}));
2021-07-30 14:49:55 +00:00
#if USE_RNNOISE
2021-07-15 14:24:57 +00:00
std::unique_ptr<AudioCapturePostProcessor> audioProcessor = nullptr;
2021-07-30 14:49:55 +00:00
#endif
2021-06-25 00:43:10 +00:00
if (_videoContentType != VideoContentType::Screencast) {
2022-04-16 14:43:17 +00:00
int numChannels = 1;
#ifdef WEBRTC_IOS
if (_disableAudioInput) {
numChannels = 2;
}
#endif
PlatformInterface::SharedInstance()->configurePlatformAudio(numChannels);
2021-03-19 10:25:58 +00:00
2021-07-15 14:24:57 +00:00
#if USE_RNNOISE
audioProcessor = std::make_unique<AudioCapturePostProcessor>([weak, threads = _threads](GroupLevelValue const &level) {
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, level](){
2021-07-15 14:24:57 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
strong->_myAudioLevel = level;
});
2021-07-30 14:49:55 +00:00
}, _noiseSuppressionConfiguration, nullptr, nullptr);
2021-07-15 14:24:57 +00:00
#endif
}
2021-03-19 10:25:58 +00:00
2021-07-30 14:49:55 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this
#if USE_RNNOISE
, audioProcessor = std::move(audioProcessor)
#endif
2021-06-25 00:43:10 +00:00
]() mutable {
cricket::MediaEngineDependencies mediaDeps;
mediaDeps.task_queue_factory = _taskQueueFactory.get();
mediaDeps.audio_encoder_factory = webrtc::CreateAudioEncoderFactory<webrtc::AudioEncoderOpus, webrtc::AudioEncoderL16>();
mediaDeps.audio_decoder_factory = webrtc::CreateAudioDecoderFactory<webrtc::AudioDecoderOpus, webrtc::AudioDecoderL16>();
2021-03-19 10:25:58 +00:00
2022-03-11 16:49:54 +00:00
mediaDeps.video_encoder_factory = PlatformInterface::SharedInstance()->makeVideoEncoderFactory(_platformContext, false, _videoContentType == VideoContentType::Screencast);
2021-06-25 00:43:10 +00:00
mediaDeps.video_decoder_factory = PlatformInterface::SharedInstance()->makeVideoDecoderFactory(_platformContext);
2021-03-19 10:25:58 +00:00
2021-07-30 14:49:55 +00:00
#if USE_RNNOISE
2021-07-15 14:24:57 +00:00
if (_audioLevelsUpdated && audioProcessor) {
2021-06-25 00:43:10 +00:00
webrtc::AudioProcessingBuilder builder;
2021-07-15 14:24:57 +00:00
builder.SetCapturePostProcessing(std::move(audioProcessor));
2021-03-19 10:25:58 +00:00
2022-03-11 16:49:54 +00:00
builder.SetEchoDetector(rtc::make_ref_counted<CustomEchoDetector>());
2021-06-25 00:43:10 +00:00
mediaDeps.audio_processing = builder.Create();
}
2021-07-30 14:49:55 +00:00
#endif
2021-03-19 10:25:58 +00:00
2021-08-31 19:06:39 +00:00
_audioDeviceDataObserverShared = std::make_shared<AudioDeviceDataObserverShared>();
2021-06-25 00:43:10 +00:00
_audioDeviceModule = createAudioDeviceModule();
if (!_audioDeviceModule) {
return;
}
mediaDeps.adm = _audioDeviceModule;
2021-03-19 10:25:58 +00:00
2021-07-15 14:24:57 +00:00
_availableVideoFormats = filterSupportedVideoFormats(mediaDeps.video_encoder_factory->GetSupportedFormats());
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
std::unique_ptr<cricket::MediaEngineInterface> mediaEngine = cricket::CreateMediaEngine(std::move(mediaDeps));
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_channelManager = cricket::ChannelManager::Create(
std::move(mediaEngine),
true,
_threads->getWorkerThread(),
_threads->getNetworkThread()
);
});
2021-03-19 10:25:58 +00:00
setAudioInputDevice(_initialInputDeviceId);
setAudioOutputDevice(_initialOutputDeviceId);
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
webrtc::Call::Config callConfig(_eventLog.get(), _threads->getNetworkThread());
2022-03-11 16:49:54 +00:00
callConfig.neteq_factory = _netEqFactory.get();
2021-06-25 00:43:10 +00:00
callConfig.task_queue_factory = _taskQueueFactory.get();
callConfig.trials = &_fieldTrials;
callConfig.audio_state = _channelManager->media_engine()->voice().GetAudioState();
2022-03-11 16:49:54 +00:00
_call.reset(webrtc::Call::Create(callConfig, webrtc::Clock::GetRealTimeClock(), _threads->getSharedModuleThread(), webrtc::ProcessThread::Create("PacerThread")));
2021-06-25 00:43:10 +00:00
});
2021-03-19 10:25:58 +00:00
_uniqueRandomIdGenerator.reset(new rtc::UniqueRandomIdGenerator());
_threads->getNetworkThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_rtpTransport = _networkManager->getSyncAssumingSameThread()->getRtpTransport();
2021-06-25 00:43:10 +00:00
_rtpTransport->SignalSentPacket.connect(this, &GroupInstanceCustomInternal::OnSentPacket_w);
_rtpTransport->SignalRtcpPacketReceived.connect(this, &GroupInstanceCustomInternal::OnRtcpPacketReceived_n);
2021-03-19 10:25:58 +00:00
});
_videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory();
2021-06-25 00:43:10 +00:00
if (_audioLevelsUpdated) {
beginLevelsTimer(100);
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
if (_getVideoSource) {
setVideoSource(_getVideoSource, true);
2021-04-09 13:17:32 +00:00
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
if (_useDummyChannel && _videoContentType != VideoContentType::Screencast) {
addIncomingAudioChannel(ChannelId(1), true);
2021-03-19 10:25:58 +00:00
}
2021-07-15 14:24:57 +00:00
if (_videoContentType == VideoContentType::Screencast) {
setIsMuted(false);
}
2021-06-25 00:43:10 +00:00
/*if (_videoContentType != VideoContentType::Screencast) {
createOutgoingAudioChannel();
}*/
beginNetworkStatusTimer(0);
//beginAudioChannelCleanupTimer(0);
2021-03-19 10:25:58 +00:00
adjustBitratePreferences(true);
2021-06-25 00:43:10 +00:00
beginRemoteConstraintsUpdateTimer(5000);
}
void destroyOutgoingVideoChannel() {
if (!_outgoingVideoChannel) {
return;
2021-04-09 13:17:32 +00:00
}
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_outgoingVideoChannel->Enable(false);
_outgoingVideoChannel->media_channel()->SetVideoSend(_outgoingVideoSsrcs.simulcastLayers[0].ssrc, nullptr, nullptr);
_channelManager->DestroyVideoChannel(_outgoingVideoChannel);
});
_outgoingVideoChannel = nullptr;
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
void createOutgoingVideoChannel() {
if (_outgoingVideoChannel
|| _videoContentType == VideoContentType::None) {
return;
}
configureVideoParams();
if (!_selectedPayloadType) {
RTC_LOG(LS_ERROR) << "Could not select payload type.";
return;
}
cricket::VideoOptions videoOptions;
if (_videoContentType == VideoContentType::Screencast) {
videoOptions.is_screencast = true;
}
2022-03-13 01:58:00 +00:00
_outgoingVideoChannel = _channelManager->CreateVideoChannel(_call.get(), cricket::MediaConfig(), _rtpTransport, _threads->getWorkerThread(), "1", false, GroupNetworkManager::getDefaulCryptoOptions(), _uniqueRandomIdGenerator.get(), videoOptions, _videoBitrateAllocatorFactory.get());
2021-06-25 00:43:10 +00:00
if (!_outgoingVideoChannel) {
RTC_LOG(LS_ERROR) << "Could not create outgoing video channel.";
return;
}
cricket::StreamParams videoSendStreamParams;
std::vector<uint32_t> simulcastGroupSsrcs;
std::vector<cricket::SsrcGroup> fidGroups;
for (const auto &layer : _outgoingVideoSsrcs.simulcastLayers) {
simulcastGroupSsrcs.push_back(layer.ssrc);
videoSendStreamParams.ssrcs.push_back(layer.ssrc);
videoSendStreamParams.ssrcs.push_back(layer.fidSsrc);
cricket::SsrcGroup fidGroup(cricket::kFidSsrcGroupSemantics, { layer.ssrc, layer.fidSsrc });
fidGroups.push_back(fidGroup);
}
if (simulcastGroupSsrcs.size() > 1) {
cricket::SsrcGroup simulcastGroup(cricket::kSimSsrcGroupSemantics, simulcastGroupSsrcs);
videoSendStreamParams.ssrc_groups.push_back(simulcastGroup);
GroupJoinPayloadVideoSourceGroup payloadSimulcastGroup;
payloadSimulcastGroup.semantics = "SIM";
payloadSimulcastGroup.ssrcs = simulcastGroupSsrcs;
}
for (auto fidGroup : fidGroups) {
videoSendStreamParams.ssrc_groups.push_back(fidGroup);
GroupJoinPayloadVideoSourceGroup payloadFidGroup;
payloadFidGroup.semantics = "FID";
payloadFidGroup.ssrcs = fidGroup.ssrcs;
}
videoSendStreamParams.cname = "cname";
auto outgoingVideoDescription = std::make_shared<cricket::VideoContentDescription>();
for (const auto &extension : _videoExtensionMap) {
outgoingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(extension.second, extension.first));
}
outgoingVideoDescription->set_rtcp_mux(true);
outgoingVideoDescription->set_rtcp_reduced_size(true);
outgoingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly);
outgoingVideoDescription->set_codecs({ _selectedPayloadType->videoCodec, _selectedPayloadType->rtxCodec });
outgoingVideoDescription->set_bandwidth(1300000);
outgoingVideoDescription->AddStream(videoSendStreamParams);
auto incomingVideoDescription = std::make_shared<cricket::VideoContentDescription>();
for (const auto &extension : _videoExtensionMap) {
incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(extension.second, extension.first));
}
incomingVideoDescription->set_rtcp_mux(true);
incomingVideoDescription->set_rtcp_reduced_size(true);
incomingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly);
incomingVideoDescription->set_codecs({ _selectedPayloadType->videoCodec, _selectedPayloadType->rtxCodec });
incomingVideoDescription->set_bandwidth(1300000);
2022-03-11 16:49:54 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() {
2022-03-13 01:58:00 +00:00
_outgoingVideoChannel->SetRemoteContent(incomingVideoDescription.get(), webrtc::SdpType::kAnswer, nullptr);
_outgoingVideoChannel->SetLocalContent(outgoingVideoDescription.get(), webrtc::SdpType::kOffer, nullptr);
2022-03-11 16:49:54 +00:00
_outgoingVideoChannel->SetPayloadTypeDemuxingEnabled(false);
});
2021-06-25 00:43:10 +00:00
adjustVideoSendParams();
updateVideoSend();
}
void adjustVideoSendParams() {
if (!_outgoingVideoChannel) {
return;
}
if (_videoContentType == VideoContentType::Screencast) {
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
webrtc::RtpParameters rtpParameters = _outgoingVideoChannel->media_channel()->GetRtpSendParameters(_outgoingVideoSsrcs.simulcastLayers[0].ssrc);
if (rtpParameters.encodings.size() == 3) {
for (int i = 0; i < (int)rtpParameters.encodings.size(); i++) {
if (i == 0) {
rtpParameters.encodings[i].min_bitrate_bps = 50000;
rtpParameters.encodings[i].max_bitrate_bps = 100000;
rtpParameters.encodings[i].scale_resolution_down_by = 4.0;
rtpParameters.encodings[i].active = _outgoingVideoConstraint >= 180;
} else if (i == 1) {
2021-07-30 14:49:55 +00:00
rtpParameters.encodings[i].min_bitrate_bps = 150000;
2021-06-25 00:43:10 +00:00
rtpParameters.encodings[i].max_bitrate_bps = 200000;
rtpParameters.encodings[i].scale_resolution_down_by = 2.0;
rtpParameters.encodings[i].active = _outgoingVideoConstraint >= 360;
} else if (i == 2) {
rtpParameters.encodings[i].min_bitrate_bps = 300000;
rtpParameters.encodings[i].max_bitrate_bps = 800000 + 100000;
rtpParameters.encodings[i].active = _outgoingVideoConstraint >= 720;
}
}
2021-07-15 14:24:57 +00:00
} else if (rtpParameters.encodings.size() == 2) {
for (int i = 0; i < (int)rtpParameters.encodings.size(); i++) {
if (i == 0) {
rtpParameters.encodings[i].min_bitrate_bps = 50000;
rtpParameters.encodings[i].max_bitrate_bps = 100000;
rtpParameters.encodings[i].scale_resolution_down_by = 2.0;
} else if (i == 1) {
rtpParameters.encodings[i].min_bitrate_bps = 200000;
rtpParameters.encodings[i].max_bitrate_bps = 900000 + 100000;
}
}
} else {
rtpParameters.encodings[0].max_bitrate_bps = (800000 + 100000) * 2;
}
_outgoingVideoChannel->media_channel()->SetRtpSendParameters(_outgoingVideoSsrcs.simulcastLayers[0].ssrc, rtpParameters);
});
} else {
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
webrtc::RtpParameters rtpParameters = _outgoingVideoChannel->media_channel()->GetRtpSendParameters(_outgoingVideoSsrcs.simulcastLayers[0].ssrc);
if (rtpParameters.encodings.size() == 3) {
for (int i = 0; i < (int)rtpParameters.encodings.size(); i++) {
if (i == 0) {
rtpParameters.encodings[i].min_bitrate_bps = 50000;
rtpParameters.encodings[i].max_bitrate_bps = 60000;
rtpParameters.encodings[i].scale_resolution_down_by = 4.0;
rtpParameters.encodings[i].active = _outgoingVideoConstraint >= 180;
} else if (i == 1) {
2021-07-30 14:49:55 +00:00
rtpParameters.encodings[i].min_bitrate_bps = 100000;
2021-07-15 14:24:57 +00:00
rtpParameters.encodings[i].max_bitrate_bps = 110000;
rtpParameters.encodings[i].scale_resolution_down_by = 2.0;
rtpParameters.encodings[i].active = _outgoingVideoConstraint >= 360;
} else if (i == 2) {
rtpParameters.encodings[i].min_bitrate_bps = 300000;
rtpParameters.encodings[i].max_bitrate_bps = 800000 + 100000;
rtpParameters.encodings[i].active = _outgoingVideoConstraint >= 720;
}
}
2021-06-25 00:43:10 +00:00
} else if (rtpParameters.encodings.size() == 2) {
for (int i = 0; i < (int)rtpParameters.encodings.size(); i++) {
if (i == 0) {
rtpParameters.encodings[i].min_bitrate_bps = 50000;
rtpParameters.encodings[i].max_bitrate_bps = 100000;
rtpParameters.encodings[i].scale_resolution_down_by = 4.0;
} else if (i == 1) {
rtpParameters.encodings[i].min_bitrate_bps = 200000;
rtpParameters.encodings[i].max_bitrate_bps = 900000 + 100000;
}
}
} else {
rtpParameters.encodings[0].max_bitrate_bps = (800000 + 100000) * 2;
}
_outgoingVideoChannel->media_channel()->SetRtpSendParameters(_outgoingVideoSsrcs.simulcastLayers[0].ssrc, rtpParameters);
});
}
}
void updateVideoSend() {
if (!_outgoingVideoChannel) {
return;
}
webrtc::VideoTrackSourceInterface *videoSource = _getVideoSource ? _getVideoSource() : nullptr;
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this, videoSource]() {
if (_getVideoSource) {
_outgoingVideoChannel->Enable(true);
_outgoingVideoChannel->media_channel()->SetVideoSend(_outgoingVideoSsrcs.simulcastLayers[0].ssrc, nullptr, videoSource);
} else {
_outgoingVideoChannel->Enable(false);
_outgoingVideoChannel->media_channel()->SetVideoSend(_outgoingVideoSsrcs.simulcastLayers[0].ssrc, nullptr, nullptr);
}
});
2021-03-19 10:25:58 +00:00
}
void destroyOutgoingAudioChannel() {
if (!_outgoingAudioChannel) {
return;
}
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_outgoingAudioChannel->media_channel()->SetAudioSend(_outgoingAudioSsrc, false, nullptr, &_audioSource);
_outgoingAudioChannel->Enable(false);
_channelManager->DestroyVoiceChannel(_outgoingAudioChannel);
});
2021-03-19 10:25:58 +00:00
_outgoingAudioChannel = nullptr;
}
void createOutgoingAudioChannel() {
2021-07-15 14:24:57 +00:00
if (_outgoingAudioChannel) {
2021-03-19 10:25:58 +00:00
return;
}
cricket::AudioOptions audioOptions;
2021-07-15 14:24:57 +00:00
if (_disableOutgoingAudioProcessing || _videoContentType == VideoContentType::Screencast) {
2021-06-25 00:43:10 +00:00
audioOptions.echo_cancellation = false;
audioOptions.noise_suppression = false;
audioOptions.auto_gain_control = false;
audioOptions.highpass_filter = false;
audioOptions.typing_detection = false;
2022-03-13 01:58:00 +00:00
audioOptions.experimental_agc = false;
audioOptions.experimental_ns = false;
2021-06-25 00:43:10 +00:00
audioOptions.residual_echo_detector = false;
} else {
audioOptions.echo_cancellation = true;
audioOptions.noise_suppression = true;
2022-03-13 01:58:00 +00:00
audioOptions.experimental_ns = true;
2021-06-25 00:43:10 +00:00
audioOptions.residual_echo_detector = true;
}
2021-03-19 10:25:58 +00:00
std::vector<std::string> streamIds;
streamIds.push_back("1");
2022-03-13 01:58:00 +00:00
_outgoingAudioChannel = _channelManager->CreateVoiceChannel(_call.get(), cricket::MediaConfig(), _rtpTransport, _threads->getWorkerThread(), "0", false, GroupNetworkManager::getDefaulCryptoOptions(), _uniqueRandomIdGenerator.get(), audioOptions);
2021-06-25 00:43:10 +00:00
const uint8_t opusMinBitrateKbps = _outgoingAudioBitrateKbit;
const uint8_t opusMaxBitrateKbps = _outgoingAudioBitrateKbit;
const uint8_t opusStartBitrateKbps = _outgoingAudioBitrateKbit;
2021-03-19 10:25:58 +00:00
const uint8_t opusPTimeMs = 120;
cricket::AudioCodec opusCodec(111, "opus", 48000, 0, 2);
opusCodec.AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc));
opusCodec.SetParam(cricket::kCodecParamMinBitrate, opusMinBitrateKbps);
opusCodec.SetParam(cricket::kCodecParamStartBitrate, opusStartBitrateKbps);
opusCodec.SetParam(cricket::kCodecParamMaxBitrate, opusMaxBitrateKbps);
opusCodec.SetParam(cricket::kCodecParamUseInbandFec, 1);
opusCodec.SetParam(cricket::kCodecParamPTime, opusPTimeMs);
2021-06-25 00:43:10 +00:00
auto outgoingAudioDescription = std::make_shared<cricket::AudioContentDescription>();
2021-03-19 10:25:58 +00:00
outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, 1));
outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, 2));
outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, 3));
outgoingAudioDescription->set_rtcp_mux(true);
outgoingAudioDescription->set_rtcp_reduced_size(true);
outgoingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly);
outgoingAudioDescription->set_codecs({ opusCodec });
2021-06-25 00:43:10 +00:00
outgoingAudioDescription->set_bandwidth(1300000);
2021-03-19 10:25:58 +00:00
outgoingAudioDescription->AddStream(cricket::StreamParams::CreateLegacy(_outgoingAudioSsrc));
2021-06-25 00:43:10 +00:00
auto incomingAudioDescription = std::make_shared<cricket::AudioContentDescription>();
2021-03-19 10:25:58 +00:00
incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, 1));
incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, 2));
incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri, 3));
incomingAudioDescription->set_rtcp_mux(true);
incomingAudioDescription->set_rtcp_reduced_size(true);
incomingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly);
incomingAudioDescription->set_codecs({ opusCodec });
2021-06-25 00:43:10 +00:00
incomingAudioDescription->set_bandwidth(1300000);
2021-03-19 10:25:58 +00:00
2022-03-11 16:49:54 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() {
2022-03-13 01:58:00 +00:00
_outgoingAudioChannel->SetLocalContent(outgoingAudioDescription.get(), webrtc::SdpType::kOffer, nullptr);
_outgoingAudioChannel->SetRemoteContent(incomingAudioDescription.get(), webrtc::SdpType::kAnswer, nullptr);
2021-06-25 00:43:10 +00:00
_outgoingAudioChannel->SetPayloadTypeDemuxingEnabled(false);
_outgoingAudioChannel->Enable(true);
});
2021-03-19 10:25:58 +00:00
onUpdatedIsMuted();
2021-06-25 00:43:10 +00:00
adjustBitratePreferences(false);
2021-03-19 10:25:58 +00:00
}
void stop() {
}
2021-07-15 14:24:57 +00:00
void updateSsrcAudioLevel(uint32_t ssrc, uint8_t audioLevel, bool isSpeech) {
2021-08-31 19:06:39 +00:00
float mappedLevelDb = ((float)audioLevel) / (float)(0x7f);
//mappedLevelDb = fabs(1.0f - mappedLevelDb);
//float mappedLevel = pow(10.0f, mappedLevelDb * 0.1f);
//printf("mappedLevelDb: %f, mappedLevel: %f\n", mappedLevelDb, mappedLevel);
float mappedLevel = (fabs(1.0f - mappedLevelDb)) * 1.0f;
2021-07-15 14:24:57 +00:00
auto it = _audioLevels.find(ChannelId(ssrc));
if (it != _audioLevels.end()) {
it->second.value.level = fmax(it->second.value.level, mappedLevel);
if (isSpeech) {
it->second.value.voice = true;
}
it->second.timestamp = rtc::TimeMillis();
} else {
InternalGroupLevelValue updated;
updated.value.level = mappedLevel;
updated.value.voice = isSpeech;
updated.timestamp = rtc::TimeMillis();
_audioLevels.insert(std::make_pair(ChannelId(ssrc), std::move(updated)));
}
auto audioChannel = _incomingAudioChannels.find(ChannelId(ssrc));
if (audioChannel != _incomingAudioChannels.end()) {
audioChannel->second->updateActivity();
}
}
2021-03-19 10:25:58 +00:00
void beginLevelsTimer(int timeoutMs) {
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
2022-03-13 01:58:00 +00:00
_threads->getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak]() {
2021-03-19 10:25:58 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
2021-08-31 19:06:39 +00:00
//int64_t timestamp = rtc::TimeMillis();
//int64_t maxSampleTimeout = 400;
2021-07-15 14:24:57 +00:00
2021-03-19 10:25:58 +00:00
GroupLevelsUpdate levelsUpdate;
levelsUpdate.updates.reserve(strong->_audioLevels.size() + 1);
for (auto &it : strong->_audioLevels) {
2021-08-31 19:06:39 +00:00
/*if (it.second.value.level < 0.001f) {
2021-08-05 20:35:18 +00:00
continue;
}
if (it.second.timestamp <= timestamp - maxSampleTimeout) {
continue;
2021-08-31 19:06:39 +00:00
}*/
2021-07-15 14:24:57 +00:00
2021-08-05 20:35:18 +00:00
uint32_t effectiveSsrc = it.first.actualSsrc;
if (std::find_if(levelsUpdate.updates.begin(), levelsUpdate.updates.end(), [&](GroupLevelUpdate const &item) {
return item.ssrc == effectiveSsrc;
}) != levelsUpdate.updates.end()) {
continue;
}
levelsUpdate.updates.push_back(GroupLevelUpdate{
effectiveSsrc,
it.second.value,
});
if (it.second.value.level > 0.001f) {
auto audioChannel = strong->_incomingAudioChannels.find(it.first);
if (audioChannel != strong->_incomingAudioChannels.end()) {
audioChannel->second->updateActivity();
}
2021-03-19 10:25:58 +00:00
}
2021-08-05 20:35:18 +00:00
2021-08-31 19:06:39 +00:00
//it.second.value.level *= 0.5f;
//it.second.value.voice = false;
2021-03-19 10:25:58 +00:00
}
2021-07-15 14:24:57 +00:00
2021-08-31 19:06:39 +00:00
strong->_audioLevels.clear();
2021-03-19 10:25:58 +00:00
auto myAudioLevel = strong->_myAudioLevel;
2021-06-25 00:43:10 +00:00
myAudioLevel.isMuted = strong->_isMuted;
2021-03-19 10:25:58 +00:00
levelsUpdate.updates.push_back(GroupLevelUpdate{ 0, myAudioLevel });
if (strong->_audioLevelsUpdated) {
strong->_audioLevelsUpdated(levelsUpdate);
}
2021-07-15 14:24:57 +00:00
bool isSpeech = myAudioLevel.voice && !myAudioLevel.isMuted;
strong->_networkManager->perform(RTC_FROM_HERE, [isSpeech = isSpeech](GroupNetworkManager *networkManager) {
networkManager->setOutgoingVoiceActivity(isSpeech);
});
2021-06-25 00:43:10 +00:00
strong->beginLevelsTimer(100);
2021-03-19 10:25:58 +00:00
}, timeoutMs);
}
2021-06-25 00:43:10 +00:00
void beginAudioChannelCleanupTimer(int delayMs) {
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
2022-03-13 01:58:00 +00:00
_threads->getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak]() {
2021-06-25 00:43:10 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
auto timestamp = rtc::TimeMillis();
std::vector<ChannelId> removeChannels;
for (const auto &it : strong->_incomingAudioChannels) {
if (it.first.networkSsrc == 1) {
continue;
}
auto activity = it.second->getActivity();
if (activity < timestamp - 1000) {
removeChannels.push_back(it.first);
}
}
for (const auto &channelId : removeChannels) {
strong->removeIncomingAudioChannel(channelId);
}
strong->beginAudioChannelCleanupTimer(500);
}, delayMs);
}
void beginRemoteConstraintsUpdateTimer(int delayMs) {
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
2022-03-13 01:58:00 +00:00
_threads->getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak]() {
2021-06-25 00:43:10 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
strong->maybeUpdateRemoteVideoConstraints();
strong->beginRemoteConstraintsUpdateTimer(5000);
}, delayMs);
}
2021-03-19 10:25:58 +00:00
void beginNetworkStatusTimer(int delayMs) {
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
2022-03-13 01:58:00 +00:00
_threads->getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak]() {
2021-03-19 10:25:58 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
if (strong->_connectionMode == GroupConnectionMode::GroupConnectionModeBroadcast || strong->_broadcastEnabledUntilRtcIsConnectedAtTimestamp) {
strong->updateBroadcastNetworkStatus();
}
strong->beginNetworkStatusTimer(500);
}, delayMs);
}
void updateBroadcastNetworkStatus() {
bool isBroadcastConnected = true;
if (isBroadcastConnected != _isBroadcastConnected) {
_isBroadcastConnected = isBroadcastConnected;
updateIsConnected();
}
}
2021-06-25 00:43:10 +00:00
void configureVideoParams() {
2021-07-15 14:24:57 +00:00
if (!_sharedVideoInformation) {
return;
}
2021-06-25 00:43:10 +00:00
if (_selectedPayloadType) {
// Already configured.
2021-03-19 10:25:58 +00:00
return;
}
2021-06-25 00:43:10 +00:00
_availablePayloadTypes = assignPayloadTypes(_availableVideoFormats);
if (_availablePayloadTypes.empty()) {
2021-03-19 10:25:58 +00:00
return;
}
2021-06-25 00:43:10 +00:00
for (const auto &payloadType : _availablePayloadTypes) {
GroupJoinPayloadVideoPayloadType payload;
payload.id = payloadType.videoCodec.id;
payload.name = payloadType.videoCodec.name;
payload.clockrate = payloadType.videoCodec.clockrate;
payload.channels = 0;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
std::vector<GroupJoinPayloadVideoPayloadType::FeedbackType> feedbackTypes;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoPayloadType::FeedbackType fbGoogRemb;
fbGoogRemb.type = "goog-remb";
feedbackTypes.push_back(fbGoogRemb);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoPayloadType::FeedbackType fbTransportCc;
fbTransportCc.type = "transport-cc";
feedbackTypes.push_back(fbTransportCc);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoPayloadType::FeedbackType fbCcmFir;
fbCcmFir.type = "ccm";
fbCcmFir.subtype = "fir";
feedbackTypes.push_back(fbCcmFir);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoPayloadType::FeedbackType fbNack;
fbNack.type = "nack";
feedbackTypes.push_back(fbNack);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoPayloadType::FeedbackType fbNackPli;
fbNackPli.type = "nack";
fbNackPli.subtype = "pli";
feedbackTypes.push_back(fbNackPli);
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
payload.feedbackTypes = feedbackTypes;
payload.parameters = {};
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_videoPayloadTypes.push_back(std::move(payload));
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoPayloadType rtxPayload;
rtxPayload.id = payloadType.rtxCodec.id;
rtxPayload.name = payloadType.rtxCodec.name;
rtxPayload.clockrate = payloadType.rtxCodec.clockrate;
rtxPayload.parameters.push_back(std::make_pair("apt", intToString(payloadType.videoCodec.id)));
_videoPayloadTypes.push_back(std::move(rtxPayload));
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
std::vector<std::string> codecPriorities;
for (const auto name : _videoCodecPreferences) {
std::string codecName;
switch (name) {
case VideoCodecName::VP8: {
codecName = cricket::kVp8CodecName;
break;
}
case VideoCodecName::VP9: {
codecName = cricket::kVp9CodecName;
break;
}
2021-07-15 14:24:57 +00:00
case VideoCodecName::H264: {
codecName = cricket::kH264CodecName;
break;
}
2021-06-25 00:43:10 +00:00
default: {
break;
}
}
if (codecName.size() != 0) {
codecPriorities.push_back(std::move(codecName));
}
}
std::vector<std::string> defaultCodecPriorities = {
cricket::kVp8CodecName,
cricket::kVp9CodecName
};
2021-07-15 14:24:57 +00:00
bool enableH264 = false;
for (const auto &payloadType : _sharedVideoInformation->payloadTypes) {
if (payloadType.name == cricket::kH264CodecName) {
enableH264 = true;
break;
}
}
if (enableH264) {
defaultCodecPriorities.insert(defaultCodecPriorities.begin(), cricket::kH264CodecName);
}
2021-06-25 00:43:10 +00:00
for (const auto &name : defaultCodecPriorities) {
if (std::find(codecPriorities.begin(), codecPriorities.end(), name) == codecPriorities.end()) {
codecPriorities.push_back(name);
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
for (const auto &codecName : codecPriorities) {
if (_selectedPayloadType) {
break;
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
for (const auto &payloadType : _availablePayloadTypes) {
if (payloadType.videoCodec.name == codecName) {
_selectedPayloadType = payloadType;
break;
2021-03-19 10:25:58 +00:00
}
}
}
2021-06-25 00:43:10 +00:00
if (!_selectedPayloadType) {
return;
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
_videoExtensionMap.emplace_back(2, webrtc::RtpExtension::kAbsSendTimeUri);
_videoExtensionMap.emplace_back(3, webrtc::RtpExtension::kTransportSequenceNumberUri);
_videoExtensionMap.emplace_back(13, webrtc::RtpExtension::kVideoRotationUri);
2021-03-19 10:25:58 +00:00
}
void OnSentPacket_w(const rtc::SentPacket& sent_packet) {
_call->OnSentPacket(sent_packet);
}
2021-06-25 00:43:10 +00:00
void OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *buffer, int64_t packet_time_us) {
rtc::CopyOnWriteBuffer packet = *buffer;
2022-03-11 16:49:54 +00:00
if (_call) {
_call->Receiver()->DeliverPacket(webrtc::MediaType::ANY, packet, packet_time_us);
}
2021-06-25 00:43:10 +00:00
}
2021-03-19 10:25:58 +00:00
void adjustBitratePreferences(bool resetStartBitrate) {
webrtc::BitrateConstraints preferences;
2021-06-25 00:43:10 +00:00
webrtc::BitrateSettings settings;
if (_getVideoSource) {
settings.min_bitrate_bps = _minOutgoingVideoBitrateKbit * 1024;
2021-03-19 10:25:58 +00:00
if (resetStartBitrate) {
2021-06-25 00:43:10 +00:00
preferences.start_bitrate_bps = std::max(preferences.min_bitrate_bps, 400 * 1000);
}
if (_videoContentType == VideoContentType::Screencast) {
preferences.max_bitrate_bps = std::max(preferences.min_bitrate_bps, (1020 + 32) * 1000);
} else {
2021-07-15 14:24:57 +00:00
preferences.max_bitrate_bps = std::max(preferences.min_bitrate_bps, (1020 + 32) * 1000);
2021-03-19 10:25:58 +00:00
}
} else {
preferences.min_bitrate_bps = 32000;
if (resetStartBitrate) {
preferences.start_bitrate_bps = 32000;
}
preferences.max_bitrate_bps = 32000;
}
2021-06-25 00:43:10 +00:00
settings.min_bitrate_bps = preferences.min_bitrate_bps;
settings.start_bitrate_bps = preferences.start_bitrate_bps;
settings.max_bitrate_bps = preferences.max_bitrate_bps;
2021-03-19 10:25:58 +00:00
_call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences);
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() {
_call->SetClientBitratePreferences(settings);
});
2021-03-19 10:25:58 +00:00
}
void setIsRtcConnected(bool isConnected) {
if (_isRtcConnected == isConnected) {
return;
}
_isRtcConnected = isConnected;
RTC_LOG(LS_INFO) << formatTimestampMillis(rtc::TimeMillis()) << ": " << "setIsRtcConnected: " << _isRtcConnected;
if (_broadcastEnabledUntilRtcIsConnectedAtTimestamp) {
_broadcastEnabledUntilRtcIsConnectedAtTimestamp = absl::nullopt;
2021-08-31 19:06:39 +00:00
if (_streamingContext) {
_streamingContext.reset();
_audioDeviceDataObserverShared->setStreamingContext(nullptr);
2021-03-19 10:25:58 +00:00
}
}
updateIsConnected();
}
void updateIsConnected() {
bool isEffectivelyConnected = false;
bool isTransitioningFromBroadcastToRtc = false;
switch (_connectionMode) {
case GroupConnectionMode::GroupConnectionModeNone: {
isEffectivelyConnected = false;
if (_broadcastEnabledUntilRtcIsConnectedAtTimestamp && _isBroadcastConnected) {
isEffectivelyConnected = true;
isTransitioningFromBroadcastToRtc = true;
}
break;
}
case GroupConnectionMode::GroupConnectionModeRtc: {
isEffectivelyConnected = _isRtcConnected;
if (_broadcastEnabledUntilRtcIsConnectedAtTimestamp && _isBroadcastConnected) {
isEffectivelyConnected = true;
isTransitioningFromBroadcastToRtc = true;
}
break;
}
case GroupConnectionMode::GroupConnectionModeBroadcast: {
isEffectivelyConnected = _isBroadcastConnected;
break;
}
}
GroupNetworkState effectiveNetworkState;
effectiveNetworkState.isConnected = isEffectivelyConnected;
effectiveNetworkState.isTransitioningFromBroadcastToRtc = isTransitioningFromBroadcastToRtc;
if (_effectiveNetworkState.isConnected != effectiveNetworkState.isConnected || _effectiveNetworkState.isTransitioningFromBroadcastToRtc != effectiveNetworkState.isTransitioningFromBroadcastToRtc) {
_effectiveNetworkState = effectiveNetworkState;
if (_networkStateUpdated) {
_networkStateUpdated(_effectiveNetworkState);
}
}
}
void updateIsDataChannelOpen(bool isDataChannelOpen) {
if (_isDataChannelOpen == isDataChannelOpen) {
return;
}
_isDataChannelOpen = isDataChannelOpen;
if (_isDataChannelOpen) {
2021-06-25 00:43:10 +00:00
maybeUpdateRemoteVideoConstraints();
2021-03-19 10:25:58 +00:00
}
}
void receivePacket(rtc::CopyOnWriteBuffer const &packet, bool isUnresolved) {
2021-06-25 00:43:10 +00:00
if (packet.size() >= 4) {
2021-03-19 10:25:58 +00:00
if (packet.data()[0] == 0x13 && packet.data()[1] == 0x88 && packet.data()[2] == 0x13 && packet.data()[3] == 0x88) {
// SCTP packet header (source port 5000, destination port 5000)
return;
}
}
2022-03-11 16:49:54 +00:00
if (webrtc::IsRtcpPacket(packet)) {
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this, packet]() {
_call->Receiver()->DeliverPacket(webrtc::MediaType::ANY, packet, -1);
});
2021-03-19 10:25:58 +00:00
} else {
2022-03-11 16:49:54 +00:00
uint32_t ssrc = webrtc::ParseRtpSsrc(packet);
int payloadType = webrtc::ParseRtpPayloadType(packet);
2021-03-19 10:25:58 +00:00
2022-03-11 16:49:54 +00:00
if (ssrc == _outgoingAudioSsrc) {
2021-03-19 10:25:58 +00:00
return;
}
2022-03-11 16:49:54 +00:00
auto ssrcInfo = _channelBySsrc.find(ssrc);
2021-06-25 00:43:10 +00:00
if (ssrcInfo == _channelBySsrc.end()) {
// opus
2022-03-11 16:49:54 +00:00
if (payloadType == 111) {
maybeRequestUnknownSsrc(ssrc);
_missingPacketBuffer.add(ssrc, packet);
2021-03-19 10:25:58 +00:00
}
} else {
2021-06-25 00:43:10 +00:00
switch (ssrcInfo->second.type) {
case ChannelSsrcInfo::Type::Audio: {
2022-03-11 16:49:54 +00:00
const auto it = _incomingAudioChannels.find(ChannelId(ssrc));
2021-06-25 00:43:10 +00:00
if (it != _incomingAudioChannels.end()) {
it->second->updateActivity();
}
break;
}
case ChannelSsrcInfo::Type::Video: {
break;
}
default: {
break;
}
2021-03-19 10:25:58 +00:00
}
}
}
}
void receiveRtcpPacket(rtc::CopyOnWriteBuffer const &packet, int64_t timestamp) {
2022-03-13 01:58:00 +00:00
_threads->getWorkerThread()->PostTask(RTC_FROM_HERE, [this, packet, timestamp]() {
2021-06-25 00:43:10 +00:00
_call->Receiver()->DeliverPacket(webrtc::MediaType::ANY, packet, timestamp);
});
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
void receiveDataChannelMessage(std::string const &message) {
std::string parsingError;
auto json = json11::Json::parse(message, parsingError);
if (json.type() != json11::Json::OBJECT) {
RTC_LOG(LS_WARNING) << "receiveDataChannelMessage: error parsing message: " << parsingError;
return;
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
if (json.is_object()) {
const auto colibriClass = json.object_items().find("colibriClass");
if (colibriClass != json.object_items().end() && colibriClass->second.is_string()) {
const auto messageType = colibriClass->second.string_value();
if (messageType == "SenderVideoConstraints") {
const auto videoConstraints = json.object_items().find("videoConstraints");
if (videoConstraints != json.object_items().end() && videoConstraints->second.is_object()) {
const auto idealHeight = videoConstraints->second.object_items().find("idealHeight");
if (idealHeight != videoConstraints->second.object_items().end() && idealHeight->second.is_number()) {
int outgoingVideoConstraint = idealHeight->second.int_value();
if (_outgoingVideoConstraint != outgoingVideoConstraint) {
if (_outgoingVideoConstraint > outgoingVideoConstraint) {
_pendingOutgoingVideoConstraint = outgoingVideoConstraint;
int requestId = _pendingOutgoingVideoConstraintRequestId;
_pendingOutgoingVideoConstraintRequestId += 1;
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
2022-03-13 01:58:00 +00:00
_threads->getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak, requestId]() {
2021-06-25 00:43:10 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
if (strong->_pendingOutgoingVideoConstraint != -1 && strong->_pendingOutgoingVideoConstraintRequestId == requestId) {
if (strong->_outgoingVideoConstraint != strong->_pendingOutgoingVideoConstraint) {
strong->_outgoingVideoConstraint = strong->_pendingOutgoingVideoConstraint;
strong->adjustVideoSendParams();
}
strong->_pendingOutgoingVideoConstraint = -1;
}
}, 2000);
} else {
_pendingOutgoingVideoConstraint = -1;
_pendingOutgoingVideoConstraintRequestId += 1;
_outgoingVideoConstraint = outgoingVideoConstraint;
adjustVideoSendParams();
}
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
}
2021-08-05 20:35:18 +00:00
} else if (messageType == "DebugMessage") {
const auto message = json.object_items().find("message");
if (message != json.object_items().end() && message->second.is_string()) {
std::vector<std::string> parts = splitString(message->second.string_value(), '\n');
for (const auto &part : parts) {
std::string cleanString = part;
std::size_t index = cleanString.find("=");
if (index == std::string::npos) {
continue;
}
cleanString.erase(cleanString.begin(), cleanString.begin() + index + 1);
index = cleanString.find("target=");
if (index == std::string::npos) {
continue;
}
std::string endpointId = cleanString.substr(0, index);
cleanString.erase(cleanString.begin(), cleanString.begin() + index + 7);
index = cleanString.find("p/");
if (index == std::string::npos) {
continue;
}
std::string targetQuality = cleanString.substr(0, index);
cleanString.erase(cleanString.begin(), cleanString.begin() + index + 2);
index = cleanString.find("ideal=");
if (index == std::string::npos) {
continue;
}
cleanString.erase(cleanString.begin(), cleanString.begin() + index + 6);
index = cleanString.find("p/");
if (index == std::string::npos) {
continue;
}
std::string availableQuality = cleanString.substr(0, index);
for (const auto &it : _incomingVideoChannels) {
if (it.second->endpointId() == endpointId) {
GroupInstanceStats::IncomingVideoStats incomingVideoStats;
incomingVideoStats.receivingQuality = stringToInt(targetQuality);
incomingVideoStats.availableQuality = stringToInt(availableQuality);
it.second->setStats(incomingVideoStats);
}
}
}
}
2021-03-19 10:25:58 +00:00
}
}
}
}
2021-06-25 00:43:10 +00:00
void maybeRequestUnknownSsrc(uint32_t ssrc) {
if (!_requestMediaChannelDescriptions) {
MediaChannelDescription description;
description.audioSsrc = ssrc;
processMediaChannelDescriptionsResponse(-1, {description});
return;
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
for (const auto &it : _requestedMediaChannelDescriptions) {
if (std::find(it.second.ssrcs.begin(), it.second.ssrcs.end(), ssrc) != it.second.ssrcs.end()) {
return;
}
}
int requestId = _nextMediaChannelDescriptionsRequestId;
_nextMediaChannelDescriptionsRequestId += 1;
std::vector<uint32_t> requestSsrcs = { ssrc };
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
auto task = _requestMediaChannelDescriptions(requestSsrcs, [weak, threads = _threads, requestId](std::vector<MediaChannelDescription> &&descriptions) {
2022-03-13 01:58:00 +00:00
threads->getWorkerThread()->PostTask(RTC_FROM_HERE, [weak, requestId, descriptions = std::move(descriptions)]() mutable {
2021-06-25 00:43:10 +00:00
auto strong = weak.lock();
if (!strong) {
return;
}
strong->processMediaChannelDescriptionsResponse(requestId, descriptions);
});
});
_requestedMediaChannelDescriptions.insert(std::make_pair(requestId, RequestedMediaChannelDescriptions(task, std::move(requestSsrcs))));
}
void processMediaChannelDescriptionsResponse(int requestId, std::vector<MediaChannelDescription> const &descriptions) {
_requestedMediaChannelDescriptions.erase(requestId);
if (_disableIncomingChannels) {
return;
}
for (const auto &description : descriptions) {
switch (description.type) {
case MediaChannelDescription::Type::Audio: {
if (description.audioSsrc != 0) {
addIncomingAudioChannel(ChannelId(description.audioSsrc));
}
break;
}
case MediaChannelDescription::Type::Video: {
break;
}
default: {
break;
}
}
2021-03-19 10:25:58 +00:00
}
}
void maybeDeliverBufferedPackets(uint32_t ssrc) {
// TODO: Re-enable after implementing custom transport
/*auto packets = _missingPacketBuffer.get(ssrc);
if (packets.size() != 0) {
auto it = _ssrcMapping.find(ssrc);
if (it != _ssrcMapping.end()) {
for (const auto &packet : packets) {
_threads->getNetworkThread()->Invoke<void>(RTC_FROM_HERE, [this, packet]() {
_rtpTransport->DemuxPacketInternal(packet, -1);
});
}
}
}*/
}
2021-06-25 00:43:10 +00:00
void maybeUpdateRemoteVideoConstraints() {
2021-03-19 10:25:58 +00:00
if (!_isDataChannelOpen) {
return;
}
std::string pinnedEndpoint;
2021-06-25 00:43:10 +00:00
json11::Json::object json;
json.insert(std::make_pair("colibriClass", json11::Json("ReceiverVideoConstraints")));
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
json11::Json::object defaultConstraints;
defaultConstraints.insert(std::make_pair("maxHeight", json11::Json(0)));
json.insert(std::make_pair("defaultConstraints", json11::Json(std::move(defaultConstraints))));
json11::Json::array onStageEndpoints;
json11::Json::object constraints;
for (const auto &incomingVideoChannel : _incomingVideoChannels) {
json11::Json::object selectedConstraint;
switch (incomingVideoChannel.second->requestedMinQuality()) {
case VideoChannelDescription::Quality::Full: {
selectedConstraint.insert(std::make_pair("minHeight", json11::Json(720)));
break;
}
case VideoChannelDescription::Quality::Medium: {
selectedConstraint.insert(std::make_pair("minHeight", json11::Json(360)));
break;
}
case VideoChannelDescription::Quality::Thumbnail: {
selectedConstraint.insert(std::make_pair("minHeight", json11::Json(180)));
break;
}
default: {
break;
}
}
switch (incomingVideoChannel.second->requestedMaxQuality()) {
case VideoChannelDescription::Quality::Full: {
onStageEndpoints.push_back(json11::Json(incomingVideoChannel.first.endpointId));
selectedConstraint.insert(std::make_pair("maxHeight", json11::Json(720)));
break;
}
case VideoChannelDescription::Quality::Medium: {
selectedConstraint.insert(std::make_pair("maxHeight", json11::Json(360)));
break;
}
case VideoChannelDescription::Quality::Thumbnail: {
selectedConstraint.insert(std::make_pair("maxHeight", json11::Json(180)));
break;
}
default: {
break;
2021-03-19 10:25:58 +00:00
}
}
2021-06-25 00:43:10 +00:00
constraints.insert(std::make_pair(incomingVideoChannel.first.endpointId, json11::Json(std::move(selectedConstraint))));
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
json.insert(std::make_pair("onStageEndpoints", json11::Json(std::move(onStageEndpoints))));
json.insert(std::make_pair("constraints", json11::Json(std::move(constraints))));
std::string result = json11::Json(std::move(json)).dump();
2021-03-19 10:25:58 +00:00
_networkManager->perform(RTC_FROM_HERE, [result = std::move(result)](GroupNetworkManager *networkManager) {
networkManager->sendDataChannelMessage(result);
});
}
2022-03-11 16:49:54 +00:00
void setConnectionMode(GroupConnectionMode connectionMode, bool keepBroadcastIfWasEnabled, bool isUnifiedBroadcast) {
2021-06-25 00:43:10 +00:00
if (_connectionMode != connectionMode || connectionMode == GroupConnectionMode::GroupConnectionModeNone) {
2021-03-19 10:25:58 +00:00
GroupConnectionMode previousMode = _connectionMode;
_connectionMode = connectionMode;
2022-03-11 16:49:54 +00:00
_isUnifiedBroadcast = isUnifiedBroadcast;
2021-03-19 10:25:58 +00:00
onConnectionModeUpdated(previousMode, keepBroadcastIfWasEnabled);
}
}
void onConnectionModeUpdated(GroupConnectionMode previousMode, bool keepBroadcastIfWasEnabled) {
2021-06-25 00:43:10 +00:00
RTC_CHECK(_connectionMode != previousMode || _connectionMode == GroupConnectionMode::GroupConnectionModeNone);
2021-03-19 10:25:58 +00:00
if (previousMode == GroupConnectionMode::GroupConnectionModeRtc) {
_networkManager->perform(RTC_FROM_HERE, [](GroupNetworkManager *networkManager) {
networkManager->stop();
});
} else if (previousMode == GroupConnectionMode::GroupConnectionModeBroadcast) {
if (keepBroadcastIfWasEnabled) {
_broadcastEnabledUntilRtcIsConnectedAtTimestamp = rtc::TimeMillis();
} else {
2021-08-31 19:06:39 +00:00
if (_streamingContext) {
_streamingContext.reset();
_audioDeviceDataObserverShared->setStreamingContext(nullptr);
2021-03-19 10:25:58 +00:00
}
}
}
if (_connectionMode == GroupConnectionMode::GroupConnectionModeNone) {
destroyOutgoingAudioChannel();
2021-06-25 00:43:10 +00:00
destroyOutgoingVideoChannel();
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
// Regenerate and reconfigure.
generateSsrcs();
2021-03-19 10:25:58 +00:00
if (!_isMuted) {
createOutgoingAudioChannel();
}
2021-06-25 00:43:10 +00:00
createOutgoingVideoChannel();
2021-03-19 10:25:58 +00:00
}
switch (_connectionMode) {
case GroupConnectionMode::GroupConnectionModeNone: {
break;
}
case GroupConnectionMode::GroupConnectionModeRtc: {
_networkManager->perform(RTC_FROM_HERE, [](GroupNetworkManager *networkManager) {
networkManager->start();
});
break;
}
case GroupConnectionMode::GroupConnectionModeBroadcast: {
_isBroadcastConnected = false;
2021-08-31 19:06:39 +00:00
if (!_streamingContext) {
StreamingMediaContext::StreamingMediaContextArguments arguments;
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
arguments.threads = _threads;
arguments.platformContext = _platformContext;
2022-03-11 16:49:54 +00:00
arguments.isUnifiedBroadcast = _isUnifiedBroadcast;
2021-08-31 19:06:39 +00:00
arguments.requestCurrentTime = _requestCurrentTime;
arguments.requestAudioBroadcastPart = _requestAudioBroadcastPart;
arguments.requestVideoBroadcastPart = _requestVideoBroadcastPart;
arguments.updateAudioLevel = [weak, threads = _threads](uint32_t ssrc, float level, bool isSpeech) {
assert(threads->getMediaThread()->IsCurrent());
auto strong = weak.lock();
if (!strong) {
return;
}
InternalGroupLevelValue updated;
updated.value.level = level;
updated.value.voice = isSpeech;
updated.timestamp = rtc::TimeMillis();
strong->_audioLevels.insert(std::make_pair(ChannelId(ssrc), std::move(updated)));
};
_streamingContext = std::make_shared<StreamingMediaContext>(std::move(arguments));
for (const auto &it : _pendingVideoSinks) {
for (const auto &sink : it.second) {
_streamingContext->addVideoSink(it.first.endpointId, sink);
}
}
for (const auto &it : _volumeBySsrc) {
_streamingContext->setVolume(it.first, it.second);
}
std::vector<StreamingMediaContext::VideoChannel> streamingVideoChannels;
for (const auto &it : _pendingRequestedVideo) {
streamingVideoChannels.emplace_back(it.maxQuality, it.endpointId);
}
_streamingContext->setActiveVideoChannels(streamingVideoChannels);
_audioDeviceDataObserverShared->setStreamingContext(_streamingContext);
}
2021-03-19 10:25:58 +00:00
break;
}
default: {
2022-03-11 16:49:54 +00:00
RTC_FATAL() << "Unknown connectionMode";
2021-03-19 10:25:58 +00:00
break;
}
}
updateIsConnected();
}
2021-06-25 00:43:10 +00:00
void generateSsrcs() {
auto generator = std::mt19937(std::random_device()());
auto distribution = std::uniform_int_distribution<uint32_t>();
do {
_outgoingAudioSsrc = distribution(generator) & 0x7fffffffU;
} while (!_outgoingAudioSsrc);
uint32_t outgoingVideoSsrcBase = _outgoingAudioSsrc + 1;
int numVideoSimulcastLayers = 3;
if (_videoContentType == VideoContentType::Screencast) {
numVideoSimulcastLayers = 2;
}
_outgoingVideoSsrcs.simulcastLayers.clear();
for (int layerIndex = 0; layerIndex < numVideoSimulcastLayers; layerIndex++) {
_outgoingVideoSsrcs.simulcastLayers.push_back(VideoSsrcs::SimulcastLayer(outgoingVideoSsrcBase + layerIndex * 2 + 0, outgoingVideoSsrcBase + layerIndex * 2 + 1));
}
2021-07-15 14:24:57 +00:00
_videoSourceGroups.clear();
std::vector<uint32_t> simulcastGroupSsrcs;
std::vector<cricket::SsrcGroup> fidGroups;
for (const auto &layer : _outgoingVideoSsrcs.simulcastLayers) {
simulcastGroupSsrcs.push_back(layer.ssrc);
cricket::SsrcGroup fidGroup(cricket::kFidSsrcGroupSemantics, { layer.ssrc, layer.fidSsrc });
fidGroups.push_back(fidGroup);
}
if (simulcastGroupSsrcs.size() > 1) {
cricket::SsrcGroup simulcastGroup(cricket::kSimSsrcGroupSemantics, simulcastGroupSsrcs);
GroupJoinPayloadVideoSourceGroup payloadSimulcastGroup;
payloadSimulcastGroup.semantics = "SIM";
payloadSimulcastGroup.ssrcs = simulcastGroupSsrcs;
_videoSourceGroups.push_back(payloadSimulcastGroup);
}
for (auto fidGroup : fidGroups) {
GroupJoinPayloadVideoSourceGroup payloadFidGroup;
payloadFidGroup.semantics = "FID";
payloadFidGroup.ssrcs = fidGroup.ssrcs;
_videoSourceGroups.push_back(payloadFidGroup);
}
2021-06-25 00:43:10 +00:00
}
void emitJoinPayload(std::function<void(GroupJoinPayload const &)> completion) {
_networkManager->perform(RTC_FROM_HERE, [outgoingAudioSsrc = _outgoingAudioSsrc, /*videoPayloadTypes = _videoPayloadTypes, videoExtensionMap = _videoExtensionMap, */videoSourceGroups = _videoSourceGroups, videoContentType = _videoContentType, completion](GroupNetworkManager *networkManager) {
GroupJoinInternalPayload payload;
payload.audioSsrc = outgoingAudioSsrc;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
if (videoContentType != VideoContentType::None) {
GroupParticipantVideoInformation videoInformation;
videoInformation.ssrcGroups = videoSourceGroups;
payload.videoInformation = std::move(videoInformation);
}
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinTransportDescription transportDescription;
2021-03-19 10:25:58 +00:00
auto localIceParameters = networkManager->getLocalIceParameters();
2021-06-25 00:43:10 +00:00
transportDescription.ufrag = localIceParameters.ufrag;
transportDescription.pwd = localIceParameters.pwd;
2021-03-19 10:25:58 +00:00
auto localFingerprint = networkManager->getLocalFingerprint();
if (localFingerprint) {
2021-06-25 00:43:10 +00:00
GroupJoinTransportDescription::Fingerprint serializedFingerprint;
2021-03-19 10:25:58 +00:00
serializedFingerprint.hash = localFingerprint->algorithm;
serializedFingerprint.fingerprint = localFingerprint->GetRfc4572Fingerprint();
serializedFingerprint.setup = "passive";
2021-06-25 00:43:10 +00:00
transportDescription.fingerprints.push_back(std::move(serializedFingerprint));
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
payload.transport = std::move(transportDescription);
GroupJoinPayload result;
result.audioSsrc = payload.audioSsrc;
result.json = payload.serialize();
completion(result);
2021-03-19 10:25:58 +00:00
});
}
2021-06-25 00:43:10 +00:00
void setVideoSource(std::function<webrtc::VideoTrackSourceInterface*()> getVideoSource, bool isInitializing) {
bool resetBitrate = (!_getVideoSource) != (!getVideoSource) && !isInitializing;
if (!isInitializing && _getVideoSource && getVideoSource && getVideoSource() == _getVideoSource()) {
2021-03-19 10:25:58 +00:00
return;
}
2021-06-25 00:43:10 +00:00
_getVideoSource = std::move(getVideoSource);
updateVideoSend();
2021-03-19 10:25:58 +00:00
if (resetBitrate) {
adjustBitratePreferences(true);
}
}
2021-06-25 00:43:10 +00:00
void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture, bool isInitializing) {
_videoCapture = videoCapture;
setVideoSource(videoCaptureToGetVideoSource(std::move(videoCapture)), isInitializing);
}
void setAudioOutputDevice(const std::string &id) {
2021-03-19 10:25:58 +00:00
#if not defined(WEBRTC_IOS) && not defined(WEBRTC_ANDROID)
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&] {
SetAudioOutputDeviceById(_audioDeviceModule.get(), id);
});
2021-03-19 10:25:58 +00:00
#endif // WEBRTC_IOS
}
void setAudioInputDevice(const std::string &id) {
#if not defined(WEBRTC_IOS) && not defined(WEBRTC_ANDROID)
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&] {
SetAudioInputDeviceById(_audioDeviceModule.get(), id);
});
2021-03-19 10:25:58 +00:00
#endif // WEBRTC_IOS
}
2021-07-15 14:24:57 +00:00
void addExternalAudioSamples(std::vector<uint8_t> &&samples) {
if (samples.size() % 2 != 0) {
return;
}
_externalAudioSamplesMutex.Lock();
size_t previousSize = _externalAudioSamples.size();
_externalAudioSamples.resize(_externalAudioSamples.size() + samples.size() / 2);
webrtc::S16ToFloatS16((const int16_t *)samples.data(), samples.size() / 2, _externalAudioSamples.data() + previousSize);
if (_externalAudioSamples.size() > 2 * 48000) {
_externalAudioSamples.erase(_externalAudioSamples.begin(), _externalAudioSamples.begin() + (_externalAudioSamples.size() - 2 * 48000));
}
_externalAudioSamplesMutex.Unlock();
}
2021-06-25 00:43:10 +00:00
void setJoinResponsePayload(std::string const &payload) {
2021-03-19 10:25:58 +00:00
RTC_LOG(LS_INFO) << formatTimestampMillis(rtc::TimeMillis()) << ": " << "setJoinResponsePayload";
2021-06-25 00:43:10 +00:00
auto parsedPayload = GroupJoinResponsePayload::parse(payload);
if (!parsedPayload) {
RTC_LOG(LS_ERROR) << "Could not parse json response payload";
return;
}
_sharedVideoInformation = parsedPayload->videoInformation;
_serverBandwidthProbingVideoSsrc.reset();
if (parsedPayload->videoInformation && parsedPayload->videoInformation->serverVideoBandwidthProbingSsrc) {
setServerBandwidthProbingChannelSsrc(parsedPayload->videoInformation->serverVideoBandwidthProbingSsrc);
}
_networkManager->perform(RTC_FROM_HERE, [parsedTransport = parsedPayload->transport](GroupNetworkManager *networkManager) {
2021-03-19 10:25:58 +00:00
PeerIceParameters remoteIceParameters;
2021-06-25 00:43:10 +00:00
remoteIceParameters.ufrag = parsedTransport.ufrag;
remoteIceParameters.pwd = parsedTransport.pwd;
2021-03-19 10:25:58 +00:00
std::vector<cricket::Candidate> iceCandidates;
2021-06-25 00:43:10 +00:00
for (auto const &candidate : parsedTransport.candidates) {
2021-03-19 10:25:58 +00:00
rtc::SocketAddress address(candidate.ip, stringToInt(candidate.port));
cricket::Candidate parsedCandidate(
/*component=*/stringToInt(candidate.component),
/*protocol=*/candidate.protocol,
/*address=*/address,
/*priority=*/stringToUInt32(candidate.priority),
2021-06-25 00:43:10 +00:00
/*username=*/parsedTransport.ufrag,
/*password=*/parsedTransport.pwd,
2021-03-19 10:25:58 +00:00
/*type=*/candidate.type,
/*generation=*/stringToUInt32(candidate.generation),
/*foundation=*/candidate.foundation,
/*network_id=*/stringToUInt16(candidate.network),
/*network_cost=*/0
);
iceCandidates.push_back(parsedCandidate);
}
std::unique_ptr<rtc::SSLFingerprint> fingerprint;
2021-06-25 00:43:10 +00:00
if (parsedTransport.fingerprints.size() != 0) {
fingerprint = rtc::SSLFingerprint::CreateUniqueFromRfc4572(parsedTransport.fingerprints[0].hash, parsedTransport.fingerprints[0].fingerprint);
2021-03-19 10:25:58 +00:00
}
networkManager->setRemoteParams(remoteIceParameters, iceCandidates, fingerprint.get());
});
2021-07-15 14:24:57 +00:00
configureVideoParams();
createOutgoingVideoChannel();
2021-06-25 00:43:10 +00:00
adjustBitratePreferences(true);
if (!_pendingRequestedVideo.empty()) {
setRequestedVideoChannels(std::move(_pendingRequestedVideo));
_pendingRequestedVideo.clear();
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
void setServerBandwidthProbingChannelSsrc(uint32_t probingSsrc) {
RTC_CHECK(probingSsrc);
if (!_sharedVideoInformation || _availablePayloadTypes.empty()) {
2021-03-19 10:25:58 +00:00
return;
}
2021-06-25 00:43:10 +00:00
GroupParticipantVideoInformation videoInformation;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
GroupJoinPayloadVideoSourceGroup sourceGroup;
sourceGroup.ssrcs.push_back(probingSsrc);
sourceGroup.semantics = "SIM";
videoInformation.ssrcGroups.push_back(std::move(sourceGroup));
_serverBandwidthProbingVideoSsrc.reset(new IncomingVideoChannel(
_channelManager.get(),
_call.get(),
_rtpTransport,
_uniqueRandomIdGenerator.get(),
_availableVideoFormats,
_sharedVideoInformation.value(),
123456,
VideoChannelDescription::Quality::Thumbnail,
VideoChannelDescription::Quality::Thumbnail,
videoInformation,
_threads
));
ChannelSsrcInfo mapping;
mapping.type = ChannelSsrcInfo::Type::Video;
mapping.allSsrcs.push_back(probingSsrc);
_channelBySsrc.insert(std::make_pair(probingSsrc, std::move(mapping)));
2021-03-19 10:25:58 +00:00
}
void removeSsrcs(std::vector<uint32_t> ssrcs) {
2021-06-25 00:43:10 +00:00
}
void removeIncomingVideoSource(uint32_t ssrc) {
2021-03-19 10:25:58 +00:00
}
void setIsMuted(bool isMuted) {
if (_isMuted == isMuted) {
return;
}
_isMuted = isMuted;
2021-06-25 00:43:10 +00:00
if (!_isMuted && !_outgoingAudioChannel) {
createOutgoingAudioChannel();
}
2021-03-19 10:25:58 +00:00
onUpdatedIsMuted();
}
void onUpdatedIsMuted() {
if (_outgoingAudioChannel) {
2021-06-25 00:43:10 +00:00
_threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [this]() {
_outgoingAudioChannel->media_channel()->SetAudioSend(_outgoingAudioSsrc, !_isMuted, nullptr, &_audioSource);
_outgoingAudioChannel->Enable(!_isMuted);
});
2021-03-19 10:25:58 +00:00
}
}
2021-06-25 00:43:10 +00:00
void setIsNoiseSuppressionEnabled(bool isNoiseSuppressionEnabled) {
_noiseSuppressionConfiguration->isEnabled = isNoiseSuppressionEnabled;
}
2022-03-11 16:49:54 +00:00
void addOutgoingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
_videoCaptureSink->addSink(sink);
if (_videoCapture) {
_videoCapture->setOutput(_videoCaptureSink);
}
}
2021-06-25 00:43:10 +00:00
void addIncomingVideoOutput(std::string const &endpointId, std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
if (_sharedVideoInformation && endpointId == _sharedVideoInformation->endpointId) {
if (_videoCapture) {
_videoCaptureSink->addSink(sink);
_videoCapture->setOutput(_videoCaptureSink);
}
} else {
auto it = _incomingVideoChannels.find(VideoChannelId(endpointId));
if (it != _incomingVideoChannels.end()) {
it->second->addSink(sink);
} else {
_pendingVideoSinks[VideoChannelId(endpointId)].push_back(sink);
}
2021-08-31 19:06:39 +00:00
if (_streamingContext) {
_streamingContext->addVideoSink(endpointId, sink);
}
2021-03-19 10:25:58 +00:00
}
}
2021-06-25 00:43:10 +00:00
void addIncomingAudioChannel(ChannelId ssrc, bool isRawPcm = false) {
2021-03-19 10:25:58 +00:00
if (_incomingAudioChannels.find(ssrc) != _incomingAudioChannels.end()) {
return;
}
2022-03-11 16:49:54 +00:00
if (_incomingAudioChannels.size() > 10) {
2021-06-25 00:43:10 +00:00
auto timestamp = rtc::TimeMillis();
2021-03-19 10:25:58 +00:00
int64_t minActivity = INT64_MAX;
ChannelId minActivityChannelId(0, 0);
for (const auto &it : _incomingAudioChannels) {
2021-06-25 00:43:10 +00:00
if (it.first.networkSsrc == 1) {
continue;
}
2021-03-19 10:25:58 +00:00
auto activity = it.second->getActivity();
2021-06-25 00:43:10 +00:00
if (activity < minActivity && activity < timestamp - 1000) {
2021-03-19 10:25:58 +00:00
minActivity = activity;
minActivityChannelId = it.first;
}
}
if (minActivityChannelId.networkSsrc != 0) {
2021-06-25 00:43:10 +00:00
removeIncomingAudioChannel(minActivityChannelId);
}
2022-03-11 16:49:54 +00:00
if (_incomingAudioChannels.size() > 10) {
2021-06-25 00:43:10 +00:00
// Wait until there is a channel that hasn't been active in 1 second
return;
2021-03-19 10:25:58 +00:00
}
}
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
2021-04-09 13:17:32 +00:00
std::function<void(AudioSinkImpl::Update)> onAudioSinkUpdate;
if (ssrc.actualSsrc != ssrc.networkSsrc) {
if (_audioLevelsUpdated) {
onAudioSinkUpdate = [weak, ssrc = ssrc, threads = _threads](AudioSinkImpl::Update update) {
2022-03-13 01:58:00 +00:00
threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, ssrc, update]() {
auto strong = weak.lock();
if (!strong) {
return;
}
2021-08-05 20:35:18 +00:00
auto it = strong->_audioLevels.find(ChannelId(ssrc));
if (it != strong->_audioLevels.end()) {
it->second.value.level = fmax(it->second.value.level, update.level);
if (update.hasSpeech) {
it->second.value.voice = true;
}
it->second.timestamp = rtc::TimeMillis();
} else {
InternalGroupLevelValue updated;
updated.value.level = update.level;
updated.value.voice = update.hasSpeech;
updated.timestamp = rtc::TimeMillis();
strong->_audioLevels.insert(std::make_pair(ChannelId(ssrc), std::move(updated)));
}
});
};
}
}
2021-04-09 13:17:32 +00:00
std::unique_ptr<IncomingAudioChannel> channel(new IncomingAudioChannel(
_channelManager.get(),
_call.get(),
_rtpTransport,
_uniqueRandomIdGenerator.get(),
isRawPcm,
ssrc,
std::move(onAudioSinkUpdate),
2021-03-19 10:25:58 +00:00
_onAudioFrame,
2021-06-25 00:43:10 +00:00
_threads
2021-03-19 10:25:58 +00:00
));
auto volume = _volumeBySsrc.find(ssrc.actualSsrc);
if (volume != _volumeBySsrc.end()) {
channel->setVolume(volume->second);
}
_incomingAudioChannels.insert(std::make_pair(ssrc, std::move(channel)));
2021-06-25 00:43:10 +00:00
auto currentMapping = _channelBySsrc.find(ssrc.networkSsrc);
if (currentMapping != _channelBySsrc.end()) {
if (currentMapping->second.type == ChannelSsrcInfo::Type::Audio) {
if (std::find(currentMapping->second.allSsrcs.begin(), currentMapping->second.allSsrcs.end(), ssrc.networkSsrc) == currentMapping->second.allSsrcs.end()) {
currentMapping->second.allSsrcs.push_back(ssrc.networkSsrc);
}
}
} else {
ChannelSsrcInfo mapping;
mapping.type = ChannelSsrcInfo::Type::Audio;
mapping.allSsrcs.push_back(ssrc.networkSsrc);
_channelBySsrc.insert(std::make_pair(ssrc.networkSsrc, std::move(mapping)));
}
2021-03-19 10:25:58 +00:00
maybeDeliverBufferedPackets(ssrc.networkSsrc);
2021-06-25 00:43:10 +00:00
adjustBitratePreferences(false);
}
void removeIncomingAudioChannel(ChannelId const &channelId) {
const auto it = _incomingAudioChannels.find(channelId);
if (it != _incomingAudioChannels.end()) {
_incomingAudioChannels.erase(it);
}
auto currentMapping = _channelBySsrc.find(channelId.networkSsrc);
if (currentMapping != _channelBySsrc.end()) {
if (currentMapping->second.type == ChannelSsrcInfo::Type::Audio) {
auto ssrcs = currentMapping->second.allSsrcs;
for (auto ssrc : ssrcs) {
auto it = _channelBySsrc.find(ssrc);
if (it != _channelBySsrc.end()) {
_channelBySsrc.erase(it);
}
}
}
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
void addIncomingVideoChannel(uint32_t audioSsrc, GroupParticipantVideoInformation const &videoInformation, VideoChannelDescription::Quality minQuality, VideoChannelDescription::Quality maxQuality) {
if (!_sharedVideoInformation) {
return;
}
if (_incomingVideoChannels.find(VideoChannelId(videoInformation.endpointId)) != _incomingVideoChannels.end()) {
2021-03-19 10:25:58 +00:00
return;
}
const auto weak = std::weak_ptr<GroupInstanceCustomInternal>(shared_from_this());
std::unique_ptr<IncomingVideoChannel> channel(new IncomingVideoChannel(
_channelManager.get(),
_call.get(),
_rtpTransport,
_uniqueRandomIdGenerator.get(),
_availableVideoFormats,
2021-06-25 00:43:10 +00:00
_sharedVideoInformation.value(),
audioSsrc,
minQuality,
maxQuality,
videoInformation,
_threads
2021-03-19 10:25:58 +00:00
));
2021-06-25 00:43:10 +00:00
const auto pendingSinks = _pendingVideoSinks.find(VideoChannelId(videoInformation.endpointId));
if (pendingSinks != _pendingVideoSinks.end()) {
for (const auto &sink : pendingSinks->second) {
channel->addSink(sink);
}
_pendingVideoSinks.erase(pendingSinks);
}
_incomingVideoChannels.insert(std::make_pair(VideoChannelId(videoInformation.endpointId), std::move(channel)));
2021-03-19 10:25:58 +00:00
std::vector<uint32_t> allSsrcs;
2021-06-25 00:43:10 +00:00
for (const auto &group : videoInformation.ssrcGroups) {
2021-03-19 10:25:58 +00:00
for (auto ssrc : group.ssrcs) {
2021-06-25 00:43:10 +00:00
if (std::find(allSsrcs.begin(), allSsrcs.end(), ssrc) == allSsrcs.end()) {
2021-03-19 10:25:58 +00:00
allSsrcs.push_back(ssrc);
}
}
}
2021-06-25 00:43:10 +00:00
for (auto ssrc : allSsrcs) {
ChannelSsrcInfo mapping;
mapping.type = ChannelSsrcInfo::Type::Video;
mapping.allSsrcs = allSsrcs;
mapping.videoEndpointId = videoInformation.endpointId;
_channelBySsrc.insert(std::make_pair(ssrc, std::move(mapping)));
}
2021-03-19 10:25:58 +00:00
for (auto ssrc : allSsrcs) {
maybeDeliverBufferedPackets(ssrc);
}
2021-06-25 00:43:10 +00:00
adjustBitratePreferences(false);
2021-03-19 10:25:58 +00:00
}
void setVolume(uint32_t ssrc, double volume) {
auto current = _volumeBySsrc.find(ssrc);
if (current != _volumeBySsrc.end() && std::abs(current->second - volume) < 0.0001) {
return;
}
_volumeBySsrc[ssrc] = volume;
auto it = _incomingAudioChannels.find(ChannelId(ssrc));
if (it != _incomingAudioChannels.end()) {
it->second->setVolume(volume);
}
it = _incomingAudioChannels.find(ChannelId(ssrc + 1000, ssrc));
if (it != _incomingAudioChannels.end()) {
it->second->setVolume(volume);
}
2021-08-31 19:06:39 +00:00
if (_streamingContext) {
_streamingContext->setVolume(ssrc, volume);
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
void setRequestedVideoChannels(std::vector<VideoChannelDescription> &&requestedVideoChannels) {
2021-08-31 19:06:39 +00:00
if (_streamingContext) {
std::vector<StreamingMediaContext::VideoChannel> streamingVideoChannels;
for (const auto &it : requestedVideoChannels) {
streamingVideoChannels.emplace_back(it.maxQuality, it.endpointId);
}
_streamingContext->setActiveVideoChannels(streamingVideoChannels);
}
2021-06-25 00:43:10 +00:00
if (!_sharedVideoInformation) {
_pendingRequestedVideo = std::move(requestedVideoChannels);
return;
}
bool updated = false;
std::vector<std::string> allEndpointIds;
for (const auto &description : requestedVideoChannels) {
if (_sharedVideoInformation && _sharedVideoInformation->endpointId == description.endpointId) {
continue;
}
GroupParticipantVideoInformation videoInformation;
videoInformation.endpointId = description.endpointId;
for (const auto &group : description.ssrcGroups) {
GroupJoinPayloadVideoSourceGroup parsedGroup;
parsedGroup.semantics = group.semantics;
parsedGroup.ssrcs = group.ssrcs;
videoInformation.ssrcGroups.push_back(std::move(parsedGroup));
}
allEndpointIds.push_back(videoInformation.endpointId);
auto current = _incomingVideoChannels.find(VideoChannelId(videoInformation.endpointId));
if (current != _incomingVideoChannels.end()) {
if (current->second->requestedMinQuality() != description.minQuality || current->second->requestedMaxQuality() != description.maxQuality) {
current->second->setRequstedMinQuality(description.minQuality);
current->second->setRequstedMaxQuality(description.maxQuality);
updated = true;
}
continue;
}
addIncomingVideoChannel(description.audioSsrc, videoInformation, description.minQuality, description.maxQuality);
updated = true;
}
std::vector<std::string> removeEndpointIds;
for (const auto &it : _incomingVideoChannels) {
if (std::find(allEndpointIds.begin(), allEndpointIds.end(), it.first.endpointId) == allEndpointIds.end()) {
removeEndpointIds.push_back(it.first.endpointId);
updated = true;
}
}
for (const auto &endpointId : removeEndpointIds) {
2021-07-30 14:49:55 +00:00
const auto it = _incomingVideoChannels.find(VideoChannelId(endpointId));
if (it != _incomingVideoChannels.end()) {
auto sinks = it->second->getSinks();
for (const auto &sink : sinks) {
_pendingVideoSinks[VideoChannelId(endpointId)].push_back(sink);
}
_incomingVideoChannels.erase(it);
}
2021-03-19 10:25:58 +00:00
}
2021-06-25 00:43:10 +00:00
if (updated) {
maybeUpdateRemoteVideoConstraints();
2021-03-19 10:25:58 +00:00
}
}
2021-08-05 20:35:18 +00:00
void getStats(std::function<void(GroupInstanceStats)> completion) {
GroupInstanceStats result;
for (const auto &it : _incomingVideoChannels) {
const auto videoStats = it.second->getStats();
if (videoStats) {
result.incomingVideoStats.push_back(std::make_pair(it.second->endpointId(), videoStats.value()));
}
}
completion(result);
}
2021-03-19 10:25:58 +00:00
private:
2021-06-25 00:43:10 +00:00
rtc::scoped_refptr<WrappedAudioDeviceModule> createAudioDeviceModule() {
2021-08-31 19:06:39 +00:00
auto audioDeviceDataObserverShared = _audioDeviceDataObserverShared;
2022-03-11 16:49:54 +00:00
#ifdef WEBRTC_IOS
bool disableRecording = _disableAudioInput;
#endif
2021-06-25 00:43:10 +00:00
const auto create = [&](webrtc::AudioDeviceModule::AudioLayer layer) {
2022-03-11 16:49:54 +00:00
#ifdef WEBRTC_IOS
2022-04-16 14:43:17 +00:00
return rtc::make_ref_counted<webrtc::tgcalls_ios_adm::AudioDeviceModuleIOS>(false, disableRecording, disableRecording ? 2 : 1);
2022-03-11 16:49:54 +00:00
#else
2021-06-25 00:43:10 +00:00
return webrtc::AudioDeviceModule::Create(
layer,
_taskQueueFactory.get());
2022-03-11 16:49:54 +00:00
#endif
2021-06-25 00:43:10 +00:00
};
const auto check = [&](const rtc::scoped_refptr<webrtc::AudioDeviceModule> &result) -> rtc::scoped_refptr<WrappedAudioDeviceModule> {
2021-08-31 19:06:39 +00:00
if (!result) {
return nullptr;
}
auto audioDeviceObserver = std::make_unique<AudioDeviceDataObserverImpl>(audioDeviceDataObserverShared);
auto module = webrtc::CreateAudioDeviceWithDataObserver(result, std::move(audioDeviceObserver));
if (module->Init() == 0) {
return PlatformInterface::SharedInstance()->wrapAudioDeviceModule(module);
2021-06-25 00:43:10 +00:00
} else {
return nullptr;
}
};
if (_createAudioDeviceModule) {
if (const auto result = check(_createAudioDeviceModule(_taskQueueFactory.get()))) {
return result;
}
2021-07-15 14:24:57 +00:00
} else if (_videoContentType == VideoContentType::Screencast) {
#ifdef WEBRTC_ANDROID
return check(create(webrtc::AudioDeviceModule::kAndroidScreenAudio));
#else
FakeAudioDeviceModule::Options options;
options.num_channels = 1;
return check(FakeAudioDeviceModule::Creator(nullptr, _externalAudioRecorder, options)(_taskQueueFactory.get()));
#endif
2021-06-25 00:43:10 +00:00
}
return check(create(webrtc::AudioDeviceModule::kPlatformDefaultAudio));
2021-03-19 10:25:58 +00:00
}
private:
std::shared_ptr<Threads> _threads;
GroupConnectionMode _connectionMode = GroupConnectionMode::GroupConnectionModeNone;
2022-03-11 16:49:54 +00:00
bool _isUnifiedBroadcast = false;
2021-03-19 10:25:58 +00:00
std::function<void(GroupNetworkState)> _networkStateUpdated;
std::function<void(GroupLevelsUpdate const &)> _audioLevelsUpdated;
std::function<void(uint32_t, const AudioFrame &)> _onAudioFrame;
2021-06-25 00:43:10 +00:00
std::function<std::shared_ptr<RequestMediaChannelDescriptionTask>(std::vector<uint32_t> const &, std::function<void(std::vector<MediaChannelDescription> &&)>)> _requestMediaChannelDescriptions;
2021-08-31 19:06:39 +00:00
std::function<std::shared_ptr<BroadcastPartTask>(std::function<void(int64_t)>)> _requestCurrentTime;
std::function<std::shared_ptr<BroadcastPartTask>(std::shared_ptr<PlatformContext>, int64_t, int64_t, std::function<void(BroadcastPart &&)>)> _requestAudioBroadcastPart;
std::function<std::shared_ptr<BroadcastPartTask>(std::shared_ptr<PlatformContext>, int64_t, int64_t, int32_t, VideoChannelDescription::Quality, std::function<void(BroadcastPart &&)>)> _requestVideoBroadcastPart;
2021-03-19 10:25:58 +00:00
std::shared_ptr<VideoCaptureInterface> _videoCapture;
2021-06-25 00:43:10 +00:00
std::shared_ptr<VideoSinkImpl> _videoCaptureSink;
std::function<webrtc::VideoTrackSourceInterface*()> _getVideoSource;
2021-03-19 10:25:58 +00:00
bool _disableIncomingChannels = false;
2021-04-09 13:17:32 +00:00
bool _useDummyChannel{true};
2021-06-25 00:43:10 +00:00
int _outgoingAudioBitrateKbit{32};
bool _disableOutgoingAudioProcessing{false};
2022-03-11 16:49:54 +00:00
#ifdef WEBRTC_IOS
bool _disableAudioInput{false};
#endif
2021-06-25 00:43:10 +00:00
int _minOutgoingVideoBitrateKbit{100};
VideoContentType _videoContentType{VideoContentType::None};
std::vector<VideoCodecName> _videoCodecPreferences;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
int _nextMediaChannelDescriptionsRequestId = 0;
std::map<int, RequestedMediaChannelDescriptions> _requestedMediaChannelDescriptions;
2021-03-19 10:25:58 +00:00
std::unique_ptr<ThreadLocalObject<GroupNetworkManager>> _networkManager;
std::unique_ptr<webrtc::RtcEventLogNull> _eventLog;
std::unique_ptr<webrtc::TaskQueueFactory> _taskQueueFactory;
2022-03-11 16:49:54 +00:00
std::unique_ptr<webrtc::NetEqFactory> _netEqFactory;
2021-03-19 10:25:58 +00:00
std::unique_ptr<cricket::MediaEngineInterface> _mediaEngine;
std::unique_ptr<webrtc::Call> _call;
webrtc::FieldTrialBasedConfig _fieldTrials;
webrtc::LocalAudioSinkAdapter _audioSource;
2021-08-31 19:06:39 +00:00
std::shared_ptr<AudioDeviceDataObserverShared> _audioDeviceDataObserverShared;
2021-06-25 00:43:10 +00:00
rtc::scoped_refptr<WrappedAudioDeviceModule> _audioDeviceModule;
std::function<rtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> _createAudioDeviceModule;
2021-03-19 10:25:58 +00:00
std::string _initialInputDeviceId;
std::string _initialOutputDeviceId;
// _outgoingAudioChannel memory is managed by _channelManager
cricket::VoiceChannel *_outgoingAudioChannel = nullptr;
uint32_t _outgoingAudioSsrc = 0;
std::vector<webrtc::SdpVideoFormat> _availableVideoFormats;
2021-06-25 00:43:10 +00:00
std::vector<OutgoingVideoFormat> _availablePayloadTypes;
absl::optional<OutgoingVideoFormat> _selectedPayloadType;
2021-03-19 10:25:58 +00:00
std::vector<GroupJoinPayloadVideoPayloadType> _videoPayloadTypes;
std::vector<std::pair<uint32_t, std::string>> _videoExtensionMap;
std::vector<GroupJoinPayloadVideoSourceGroup> _videoSourceGroups;
std::unique_ptr<rtc::UniqueRandomIdGenerator> _uniqueRandomIdGenerator;
webrtc::RtpTransport *_rtpTransport = nullptr;
std::unique_ptr<cricket::ChannelManager> _channelManager;
std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory;
// _outgoingVideoChannel memory is managed by _channelManager
cricket::VideoChannel *_outgoingVideoChannel = nullptr;
VideoSsrcs _outgoingVideoSsrcs;
2021-06-25 00:43:10 +00:00
int _outgoingVideoConstraint = 720;
int _pendingOutgoingVideoConstraint = -1;
int _pendingOutgoingVideoConstraintRequestId = 0;
2021-03-19 10:25:58 +00:00
2021-07-15 14:24:57 +00:00
std::map<ChannelId, InternalGroupLevelValue> _audioLevels;
2021-03-19 10:25:58 +00:00
GroupLevelValue _myAudioLevel;
bool _isMuted = true;
2021-06-25 00:43:10 +00:00
std::shared_ptr<NoiseSuppressionConfiguration> _noiseSuppressionConfiguration;
2021-03-19 10:25:58 +00:00
MissingSsrcPacketBuffer _missingPacketBuffer;
2021-06-25 00:43:10 +00:00
std::map<uint32_t, ChannelSsrcInfo> _channelBySsrc;
2021-03-19 10:25:58 +00:00
std::map<uint32_t, double> _volumeBySsrc;
std::map<ChannelId, std::unique_ptr<IncomingAudioChannel>> _incomingAudioChannels;
2021-06-25 00:43:10 +00:00
std::map<VideoChannelId, std::unique_ptr<IncomingVideoChannel>> _incomingVideoChannels;
std::map<VideoChannelId, std::vector<std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>>> _pendingVideoSinks;
std::vector<VideoChannelDescription> _pendingRequestedVideo;
2021-03-19 10:25:58 +00:00
2021-06-25 00:43:10 +00:00
std::unique_ptr<IncomingVideoChannel> _serverBandwidthProbingVideoSsrc;
absl::optional<GroupJoinVideoInformation> _sharedVideoInformation;
2021-03-19 10:25:58 +00:00
2021-07-15 14:24:57 +00:00
std::vector<float> _externalAudioSamples;
webrtc::Mutex _externalAudioSamplesMutex;
std::shared_ptr<ExternalAudioRecorder> _externalAudioRecorder;
2021-03-19 10:25:58 +00:00
bool _isRtcConnected = false;
bool _isBroadcastConnected = false;
absl::optional<int64_t> _broadcastEnabledUntilRtcIsConnectedAtTimestamp;
bool _isDataChannelOpen = false;
GroupNetworkState _effectiveNetworkState;
2021-08-31 19:06:39 +00:00
std::shared_ptr<StreamingMediaContext> _streamingContext;
2021-06-25 00:43:10 +00:00
rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> _workerThreadSafery;
2021-07-15 14:24:57 +00:00
rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> _networkThreadSafery;
2021-06-25 00:43:10 +00:00
2021-03-19 10:25:58 +00:00
std::shared_ptr<PlatformContext> _platformContext;
};
GroupInstanceCustomImpl::GroupInstanceCustomImpl(GroupInstanceDescriptor &&descriptor) {
if (descriptor.config.need_log) {
_logSink = std::make_unique<LogSinkImpl>(descriptor.config.logPath);
2021-06-25 00:43:10 +00:00
rtc::LogMessage::SetLogToStderr(true);
} else {
rtc::LogMessage::SetLogToStderr(false);
2021-03-19 10:25:58 +00:00
}
rtc::LogMessage::LogToDebug(rtc::LS_INFO);
if (_logSink) {
rtc::LogMessage::AddLogToStream(_logSink.get(), rtc::LS_INFO);
}
_threads = descriptor.threads;
_internal.reset(new ThreadLocalObject<GroupInstanceCustomInternal>(_threads->getMediaThread(), [descriptor = std::move(descriptor), threads = _threads]() mutable {
return new GroupInstanceCustomInternal(std::move(descriptor), threads);
}));
_internal->perform(RTC_FROM_HERE, [](GroupInstanceCustomInternal *internal) {
internal->start();
});
}
GroupInstanceCustomImpl::~GroupInstanceCustomImpl() {
if (_logSink) {
rtc::LogMessage::RemoveLogToStream(_logSink.get());
}
_internal.reset();
// Wait until _internal is destroyed
_threads->getMediaThread()->Invoke<void>(RTC_FROM_HERE, [] {});
}
void GroupInstanceCustomImpl::stop() {
_internal->perform(RTC_FROM_HERE, [](GroupInstanceCustomInternal *internal) {
internal->stop();
});
}
2022-03-11 16:49:54 +00:00
void GroupInstanceCustomImpl::setConnectionMode(GroupConnectionMode connectionMode, bool keepBroadcastIfWasEnabled, bool isUnifiedBroadcast) {
_internal->perform(RTC_FROM_HERE, [connectionMode, keepBroadcastIfWasEnabled, isUnifiedBroadcast](GroupInstanceCustomInternal *internal) {
internal->setConnectionMode(connectionMode, keepBroadcastIfWasEnabled, isUnifiedBroadcast);
2021-03-19 10:25:58 +00:00
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::emitJoinPayload(std::function<void(GroupJoinPayload const &)> completion) {
2021-03-19 10:25:58 +00:00
_internal->perform(RTC_FROM_HERE, [completion](GroupInstanceCustomInternal *internal) {
internal->emitJoinPayload(completion);
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::setJoinResponsePayload(std::string const &payload) {
_internal->perform(RTC_FROM_HERE, [payload](GroupInstanceCustomInternal *internal) {
internal->setJoinResponsePayload(payload);
2021-03-19 10:25:58 +00:00
});
}
void GroupInstanceCustomImpl::removeSsrcs(std::vector<uint32_t> ssrcs) {
_internal->perform(RTC_FROM_HERE, [ssrcs = std::move(ssrcs)](GroupInstanceCustomInternal *internal) mutable {
internal->removeSsrcs(ssrcs);
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::removeIncomingVideoSource(uint32_t ssrc) {
_internal->perform(RTC_FROM_HERE, [ssrc](GroupInstanceCustomInternal *internal) mutable {
internal->removeIncomingVideoSource(ssrc);
});
}
2021-03-19 10:25:58 +00:00
void GroupInstanceCustomImpl::setIsMuted(bool isMuted) {
_internal->perform(RTC_FROM_HERE, [isMuted](GroupInstanceCustomInternal *internal) {
internal->setIsMuted(isMuted);
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::setIsNoiseSuppressionEnabled(bool isNoiseSuppressionEnabled) {
_internal->perform(RTC_FROM_HERE, [isNoiseSuppressionEnabled](GroupInstanceCustomInternal *internal) {
internal->setIsNoiseSuppressionEnabled(isNoiseSuppressionEnabled);
});
}
void GroupInstanceCustomImpl::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) {
_internal->perform(RTC_FROM_HERE, [videoCapture](GroupInstanceCustomInternal *internal) {
internal->setVideoCapture(videoCapture, false);
2021-03-19 10:25:58 +00:00
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::setVideoSource(std::function<webrtc::VideoTrackSourceInterface*()> getVideoSource) {
_internal->perform(RTC_FROM_HERE, [getVideoSource](GroupInstanceCustomInternal *internal) {
internal->setVideoSource(getVideoSource, false);
});
}
2021-03-19 10:25:58 +00:00
void GroupInstanceCustomImpl::setAudioOutputDevice(std::string id) {
_internal->perform(RTC_FROM_HERE, [id](GroupInstanceCustomInternal *internal) {
internal->setAudioOutputDevice(id);
2021-06-25 00:43:10 +00:00
});
2021-03-19 10:25:58 +00:00
}
void GroupInstanceCustomImpl::setAudioInputDevice(std::string id) {
2021-06-25 00:43:10 +00:00
_internal->perform(RTC_FROM_HERE, [id](GroupInstanceCustomInternal *internal) {
internal->setAudioInputDevice(id);
});
2021-07-15 14:24:57 +00:00
}
void GroupInstanceCustomImpl::addExternalAudioSamples(std::vector<uint8_t> &&samples) {
_internal->perform(RTC_FROM_HERE, [samples = std::move(samples)](GroupInstanceCustomInternal *internal) mutable {
internal->addExternalAudioSamples(std::move(samples));
});
2021-03-19 10:25:58 +00:00
}
2022-03-11 16:49:54 +00:00
void GroupInstanceCustomImpl::addOutgoingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
_internal->perform(RTC_FROM_HERE, [sink](GroupInstanceCustomInternal *internal) mutable {
internal->addOutgoingVideoOutput(sink);
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::addIncomingVideoOutput(std::string const &endpointId, std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
_internal->perform(RTC_FROM_HERE, [endpointId, sink](GroupInstanceCustomInternal *internal) mutable {
internal->addIncomingVideoOutput(endpointId, sink);
2021-03-19 10:25:58 +00:00
});
}
void GroupInstanceCustomImpl::setVolume(uint32_t ssrc, double volume) {
_internal->perform(RTC_FROM_HERE, [ssrc, volume](GroupInstanceCustomInternal *internal) {
internal->setVolume(ssrc, volume);
});
}
2021-06-25 00:43:10 +00:00
void GroupInstanceCustomImpl::setRequestedVideoChannels(std::vector<VideoChannelDescription> &&requestedVideoChannels) {
_internal->perform(RTC_FROM_HERE, [requestedVideoChannels = std::move(requestedVideoChannels)](GroupInstanceCustomInternal *internal) mutable {
internal->setRequestedVideoChannels(std::move(requestedVideoChannels));
2021-03-19 10:25:58 +00:00
});
}
2021-06-25 00:43:10 +00:00
2021-08-05 20:35:18 +00:00
void GroupInstanceCustomImpl::getStats(std::function<void(GroupInstanceStats)> completion) {
_internal->perform(RTC_FROM_HERE, [completion = std::move(completion)](GroupInstanceCustomInternal *internal) mutable {
internal->getStats(completion);
});
}
2021-04-09 13:17:32 +00:00
std::vector<GroupInstanceInterface::AudioDevice> GroupInstanceInterface::getAudioDevices(AudioDevice::Type type) {
auto result = std::vector<AudioDevice>();
#ifdef WEBRTC_LINUX //Not needed for ios, and some crl::sync stuff is needed for windows
const auto resolve = [&] {
const auto queueFactory = webrtc::CreateDefaultTaskQueueFactory();
const auto info = webrtc::AudioDeviceModule::Create(
webrtc::AudioDeviceModule::kPlatformDefaultAudio,
queueFactory.get());
if (!info || info->Init() < 0) {
return;
}
const auto count = type == AudioDevice::Type::Input ? info->RecordingDevices() : info->PlayoutDevices();
if (count <= 0) {
return;
}
for (auto i = int16_t(); i != count; ++i) {
char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 };
char id[webrtc::kAdmMaxGuidSize + 1] = { 0 };
if (type == AudioDevice::Type::Input) {
info->RecordingDeviceName(i, name, id);
} else {
info->PlayoutDeviceName(i, name, id);
}
result.push_back({ id, name });
}
};
resolve();
#endif
return result;
}
2021-03-19 10:25:58 +00:00
} // namespace tgcalls