| 6 |
| 7 | #include "logging.h" |
| 8 | #include "PrivateDefines.h" |
| 9 | #include "VoIPGroupController.h" |
| 10 | #include "VoIPServerConfig.h" |
| 11 |
| 12 | #include <cassert> |
| 13 | #include <cmath> |
| 14 | #include <cstdint> |
| 15 | #include <cstring> |
| 16 |
| 17 | using namespace tgvoip; |
| 18 |
| 19 | VoIPGroupController::VoIPGroupController(std::int32_t timeDifference) |
| 20 | : m_audioMixer(new AudioMixer()) |
| 21 | , m_timeDifference(timeDifference) |
| 22 | , m_userSelfID(0) |
| 23 | { |
| 24 | std::memset(&m_callbacks, 0, sizeof(m_callbacks)); |
| 25 | LOGV("Created VoIPGroupController; timeDifference=%d", timeDifference); |
| 26 | } |
| 27 |
| 28 | VoIPGroupController::~VoIPGroupController() |
| 29 | { |
| 30 | if (m_audioOutput != nullptr) |
| 31 | { |
| 32 | m_audioOutput->Stop(); |
| 33 | } |
| 34 |
| 35 | LOGD("before stop audio mixer"); |
| 36 | m_audioMixer->Stop(); |
| 37 | delete m_audioMixer; |
| 38 |
| 39 | for (GroupCallParticipant& participant : m_participants) |
| 40 | { |
| 41 | delete participant.levelMeter; |
| 42 | } |
| 43 | } |
| 44 |
| 45 | void VoIPGroupController::SetGroupCallInfo(std::uint8_t* encryptionKey, std::uint8_t* reflectorGroupTag, std::uint8_t* reflectorSelfTag, |
| 46 | std::uint8_t* reflectorSelfSecret, std::uint8_t* reflectorSelfTagHash, std::int32_t selfUserID, |
| 47 | const NetworkAddress& reflectorAddress, const NetworkAddress& reflectorAddressV6, std::uint16_t reflectorPort) |
| 48 | { |
| 49 | Endpoint e; |
| 50 | e.address = reflectorAddress; |
| 51 | e.v6address = reflectorAddressV6; |
| 52 | e.port = reflectorPort; |
| 53 | std::memcpy(e.peerTag, reflectorGroupTag, 16); |
| 54 | e.type = Endpoint::Type::UDP_RELAY; |
| 55 | e.id = FOURCC('G', 'R', 'P', 'R'); |
| 56 | m_endpoints[e.id] = e; |
| 57 | m_groupReflector = e; |
| 58 | m_currentEndpoint = e.id; |
| 59 |
| 60 | std::memcpy(m_encryptionKey, encryptionKey, 256); |
| 61 | std::memcpy(m_reflectorSelfTag, reflectorSelfTag, 16); |
| 62 | std::memcpy(m_reflectorSelfSecret, reflectorSelfSecret, 16); |
| 63 | std::memcpy(m_reflectorSelfTagHash, reflectorSelfTagHash, 16); |
| 64 | std::uint8_t sha256[SHA256_LENGTH]; |
| 65 | crypto.sha256(encryptionKey, 256, sha256); |
| 66 | std::memcpy(m_callID, sha256 + (SHA256_LENGTH - 16), 16); |
| 67 | std::memcpy(m_keyFingerprint, sha256 + (SHA256_LENGTH - 16), 8); |
| 68 | m_userSelfID = selfUserID; |
| 69 | } |
| 70 |
| 71 | void VoIPGroupController::AddGroupCallParticipant(std::int32_t userID, std::uint8_t* memberTagHash, std::uint8_t* serializedStreams, std::size_t streamsLength) |
| 72 | { |
| 73 | if (userID == m_userSelfID) |
| 74 | return; |
| 75 | if (m_userSelfID == 0) |
| 76 | return; |
| 77 |
| 78 | MutexGuard m(m_participantsMutex); |
| 79 | LOGV("Adding group call user %d, streams length %u", userID, static_cast<unsigned int>(streamsLength)); |
| 80 |
| 81 | for (const GroupCallParticipant& participant : m_participants) |
| 82 | { |
| 83 | if (participant.userID == userID) |
| 84 | { |
| 85 | LOGE("user %d already added", userID); |
| 86 | std::abort(); |
| 87 | } |
| 88 | } |
| 89 |
| 90 | GroupCallParticipant participant; |
| 91 | participant.userID = userID; |
| 92 | std::memcpy(participant.memberTagHash, memberTagHash, sizeof(participant.memberTagHash)); |
| 93 | participant.levelMeter = new AudioLevelMeter(); |
| 94 |
| 95 | BufferInputStream ss(serializedStreams, streamsLength); |
| 96 | std::vector<std::shared_ptr<Stream>> streams = DeserializeStreams(ss); |
| 97 |
| 98 | std::uint8_t audioStreamID = 0; |
| 99 |
| 100 | for (std::shared_ptr<Stream>& stream : streams) |
| 101 | { |
| 102 | stream->userID = userID; |
| 103 | if (stream->type == StreamType::AUDIO && stream->codec == CODEC_OPUS && !audioStreamID) |
| 104 | { |
| 105 | audioStreamID = stream->id; |
| 106 | stream->jitterBuffer = std::make_shared<JitterBuffer>(nullptr, stream->frameDuration); |
| 107 | if (stream->frameDuration > 50) |
| 108 | stream->jitterBuffer->SetMinPacketCount(static_cast<std::uint32_t>(ServerConfig::GetSharedInstance()->GetInt("jitter_initial_delay_60", 2))); |
| 109 | else if (stream->frameDuration > 30) |
| 110 | stream->jitterBuffer->SetMinPacketCount(static_cast<std::uint32_t>(ServerConfig::GetSharedInstance()->GetInt("jitter_initial_delay_40", 4))); |
| 111 | else |
| 112 | stream->jitterBuffer->SetMinPacketCount(static_cast<std::uint32_t>(ServerConfig::GetSharedInstance()->GetInt("jitter_initial_delay_20", 6))); |
| 113 | stream->callbackWrapper = std::make_shared<CallbackWrapper>(); |
| 114 | stream->decoder = std::make_shared<OpusDecoder>(stream->callbackWrapper.get(), false, false); |
| 115 | stream->decoder->SetJitterBuffer(stream->jitterBuffer); |
| 116 | stream->decoder->SetFrameDuration(stream->frameDuration); |
| 117 | stream->decoder->SetDTX(true); |
| 118 | stream->decoder->SetLevelMeter(participant.levelMeter); |
| 119 | m_audioMixer->AddInput(stream->callbackWrapper); |
| 120 | } |
| 121 | m_incomingStreams.emplace_back(stream); |
| 122 | } |
| 123 |
| 124 | if (audioStreamID == 0) |
| 125 | { |
| 126 | LOGW("User %d has no usable audio stream", userID); |
| 127 | } |
| 128 |
| 129 | participant.streams.insert(participant.streams.end(), streams.begin(), streams.end()); |
| 130 | m_participants.emplace_back(participant); |
| 131 | LOGI("Added group call participant %d", userID); |
| 132 | } |
| 133 |
| 134 | void VoIPGroupController::RemoveGroupCallParticipant(std::int32_t userID) |
| 135 | { |
| 136 | MutexGuard m(m_participantsMutex); |
| 137 | for (auto it = m_incomingStreams.begin(); it != m_incomingStreams.end();) |
| 138 | { |
| 139 | if ((*it)->userID == userID) |
| 140 | { |
| 141 | LOGI("Removed stream %d belonging to user %d", (*it)->id, userID); |
| 142 | m_audioMixer->RemoveInput((*it)->callbackWrapper); |
| 143 | (*it)->decoder->Stop(); |
| 144 | it = m_incomingStreams.erase(it); |
| 145 | continue; |
| 146 | } |
| 147 | ++it; |
| 148 | } |
| 149 | for (auto it = m_participants.begin(); it != m_participants.end(); ++it) |
| 150 | { |
| 151 | if (it->userID == userID) |
| 152 | { |
| 153 | delete it->levelMeter; |
| 154 | m_participants.erase(it); |
| 155 | LOGI("Removed group call participant %d", userID); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 |
| 161 | std::vector<std::shared_ptr<VoIPController::Stream>> VoIPGroupController::DeserializeStreams(BufferInputStream& in) |
| 162 | { |
| 163 | std::vector<std::shared_ptr<Stream>> res; |
| 164 | try |
| 165 | { |
| 166 | std::uint8_t count = in.ReadUInt8(); |
| 167 | for (int i = 0; i < count; ++i) |
| 168 | { |
| 169 | std::uint16_t len = in.ReadUInt16(); |
| 170 | BufferInputStream inner = in.GetPartBuffer(len, true); |
| 171 | std::shared_ptr<Stream> s = std::make_shared<Stream>(); |
| 172 | s->id = inner.ReadUInt8(); |
| 173 | s->type = static_cast<StreamType>(inner.ReadUInt8()); |
| 174 | s->codec = inner.ReadUInt32(); |
| 175 | std::uint32_t flags = inner.ReadUInt32(); |
| 176 | s->enabled = (flags & STREAM_FLAG_ENABLED) == STREAM_FLAG_ENABLED; |
| 177 | s->frameDuration = inner.ReadUInt16(); |
| 178 | res.emplace_back(s); |
| 179 | } |
| 180 | } |
| 181 | catch (const std::out_of_range& exception) |
| 182 | { |
| 183 | LOGW("Error deserializing streams.\nwhat():\n%s", exception.what()); |
| 184 | } |
| 185 | return res; |
| 186 | } |
| 187 |
| 188 | void VoIPGroupController::SetParticipantStreams(std::int32_t userID, std::uint8_t* serializedStreams, std::size_t length) |
| 189 | { |
| 190 | LOGD("Set participant streams for %d", userID); |
| 191 | MutexGuard m(m_participantsMutex); |
| 192 | for (const GroupCallParticipant& participant : m_participants) |
| 193 | { |
| 194 | if (participant.userID != userID) |
| 195 | continue; |
| 196 | BufferInputStream in(serializedStreams, length); |
| 197 | std::vector<std::shared_ptr<Stream>> streams = DeserializeStreams(in); |
| 198 | for (const std::shared_ptr<Stream>& ns : streams) |
| 199 | { |
| 200 | bool found = false; |
| 201 | for (const std::shared_ptr<Stream>& s : participant.streams) |
| 202 | { |
| 203 | if (s->id == ns->id) |
| 204 | { |
| 205 | s->enabled = ns->enabled; |
| 206 | if (m_groupCallbacks.participantAudioStateChanged) |
| 207 | m_groupCallbacks.participantAudioStateChanged(this, userID, s->enabled); |
| 208 | found = true; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | if (!found) |
| 213 | { |
| 214 | LOGW("Tried to add stream %d for user %d but adding/removing streams is not supported", ns->id, userID); |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 |
| 221 | std::size_t VoIPGroupController::GetInitialStreams(std::uint8_t* buf, std::size_t size) |
| 222 | { |
| 223 | BufferOutputStream s(buf, size); |
| 224 | s.WriteUInt8(1); // streams count |
| 225 |
| 226 | s.WriteInt16(12); // this object length |
| 227 | s.WriteUInt8(1); // stream id |
| 228 | s.WriteUInt8(static_cast<std::uint8_t>(StreamType::AUDIO)); |
| 229 | s.WriteUInt32(CODEC_OPUS); |
| 230 | s.WriteInt32(STREAM_FLAG_ENABLED | STREAM_FLAG_DTX); // flags |
| 231 | s.WriteInt16(60); // frame duration |
| 232 |
| 233 | return s.GetLength(); |
| 234 | } |
| 235 |
| 236 | void VoIPGroupController::SendInit() |
| 237 | { |
| 238 | SendRecentPacketsRequest(); |
| 239 | } |
| 240 |
| 241 | void VoIPGroupController::ProcessIncomingPacket(NetworkPacket& packet, Endpoint& srcEndpoint) |
| 242 | { |
| 243 | } |
| 244 |
| 245 | void VoIPGroupController::SendUdpPing(Endpoint& endpoint) |
| 246 | { |
| 247 | } |
| 248 |
| 249 | void VoIPGroupController::SetNetworkType(NetType type) |
| 250 | { |
| 251 | m_networkType = type; |
| 252 | UpdateDataSavingState(); |
| 253 | UpdateAudioBitrateLimit(); |
| 254 | std::string itfName = m_udpSocket->GetLocalInterfaceInfo(nullptr, nullptr); |
| 255 | if (itfName != m_activeNetItfName) |
| 256 | { |
| 257 | m_udpSocket->OnActiveInterfaceChanged(); |
| 258 | LOGI("Active network interface changed: %s -> %s", m_activeNetItfName.c_str(), itfName.c_str()); |
| 259 | bool isFirstChange = m_activeNetItfName.length() == 0; |
| 260 | m_activeNetItfName = itfName; |
| 261 | if (isFirstChange) |
| 262 | return; |
| 263 | m_udpConnectivityState = UdpState::UNKNOWN; |
| 264 | m_udpPingCount = 0; |
| 265 | m_lastUdpPingTime = 0; |
| 266 | if (m_proxyProtocol == Proxy::SOCKS5) |
| 267 | InitUDPProxy(); |
| 268 | m_selectCanceller->CancelSelect(); |
| 269 | } |
| 270 | } |
| 271 |
| 272 | void VoIPGroupController::SendRecentPacketsRequest() |
| 273 | { |
| 274 | BufferOutputStream out(1024); |
| 275 | out.WriteInt32(TLID_UDP_REFLECTOR_REQUEST_PACKETS_INFO); // TL function |
| 276 | out.WriteInt32(GetCurrentUnixtime()); // date:int |
| 277 | out.WriteInt64(0); // query_id:long |
| 278 | out.WriteInt32(64); // recv_num:int |
| 279 | out.WriteInt32(0); // sent_num:int |
| 280 | SendSpecialReflectorRequest(out.GetBuffer(), out.GetLength()); |
| 281 | } |
| 282 |
| 283 | void VoIPGroupController::SendSpecialReflectorRequest(std::uint8_t* data, std::size_t len) |
| 284 | { |
| 285 | } |
| 286 |
| 287 | void VoIPGroupController::SendRelayPings() |
| 288 | { |
| 289 | double currentTime = GetCurrentTime(); |
| 290 | if (currentTime - m_groupReflector.m_lastPingTime >= 0.25) |
| 291 | { |
| 292 | SendRecentPacketsRequest(); |
| 293 | m_groupReflector.m_lastPingTime = currentTime; |
| 294 | } |
| 295 | } |
| 296 |
| 297 | void VoIPGroupController::OnAudioOutputReady() |
| 298 | { |
| 299 | m_encoder->SetDTX(true); |
| 300 | m_audioMixer->SetOutput(m_audioOutput); |
| 301 | m_audioMixer->SetEchoCanceller(m_echoCanceller); |
| 302 | m_audioMixer->Start(); |
| 303 | m_audioOutput->Start(); |
| 304 | m_audioOutStarted = true; |
| 305 | m_encoder->SetLevelMeter(&m_selfLevelMeter); |
| 306 | } |
| 307 |
| 308 | void VoIPGroupController::WritePacketHeader(std::uint32_t seq, BufferOutputStream* s, PktType type, std::uint32_t length, PacketSender* source) |
| 309 | { |
| 310 | s->WriteUInt32(TLID_DECRYPTED_AUDIO_BLOCK); |
| 311 | std::int64_t randomID; |
| 312 | crypto.rand_bytes(reinterpret_cast<std::uint8_t*>(&randomID), 8); |
| 313 | s->WriteInt64(randomID); |
| 314 | std::uint8_t randBytes[7]; |
| 315 | crypto.rand_bytes(randBytes, 7); |
| 316 | s->WriteUInt8(std::uint8_t{7}); |
| 317 | s->WriteBytes(randBytes, 7); |
| 318 | std::uint32_t pflags = PFLAG_HAS_SEQ | PFLAG_HAS_SENDER_TAG_HASH; |
| 319 | if (length > 0) |
| 320 | pflags |= PFLAG_HAS_DATA; |
| 321 | pflags |= static_cast<std::uint32_t>(type) << 24; |
| 322 | s->WriteUInt32(pflags); |
| 323 |
| 324 | if (type == PktType::STREAM_DATA || type == PktType::STREAM_DATA_X2 || type == PktType::STREAM_DATA_X3) |
| 325 | { |
| 326 | m_congestionControl->PacketSent(seq, length); |
| 327 | } |
| 328 |
| 329 | s->WriteUInt32(seq); |
| 330 | s->WriteBytes(m_reflectorSelfTagHash, 16); |
| 331 | if (length > 0) |
| 332 | { |
| 333 | if (length <= 253) |
| 334 | { |
| 335 | s->WriteUInt8(static_cast<std::uint8_t>(length)); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | s->WriteUInt8(254); |
| 340 | s->WriteUInt8(static_cast<std::uint8_t>(length & 0xFF)); |
| 341 | s->WriteUInt8(static_cast<std::uint8_t>((length >> 8) & 0xFF)); |
| 342 | s->WriteUInt8(static_cast<std::uint8_t>((length >> 16) & 0xFF)); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 |
| 347 | void VoIPGroupController::SendPacket(std::uint8_t* data, std::size_t len, Endpoint& ep, PendingOutgoingPacket& srcPacket) |
| 348 | { |
| 349 | if (m_stopping) |
| 350 | return; |
| 351 | if (ep.type == Endpoint::Type::TCP_RELAY && !m_useTCP) |
| 352 | return; |
| 353 | BufferOutputStream out(len + 128); |
| 354 |
| 355 | out.WriteBytes(m_reflectorSelfTag, 16); |
| 356 |
| 357 | if (len > 0) |
| 358 | { |
| 359 | BufferOutputStream inner(len + 128); |
| 360 | inner.WriteUInt32(static_cast<std::uint32_t>(len)); |
| 361 | inner.WriteBytes(data, len); |
| 362 | std::size_t padLen = 16 - inner.GetLength() % 16; |
| 363 | if (padLen < 12) |
| 364 | padLen += 16; |
| 365 | std::uint8_t padding[28]; |
| 366 | crypto.rand_bytes(padding, padLen); |
| 367 | inner.WriteBytes(padding, padLen); |
| 368 | assert(inner.GetLength() % 16 == 0); |
| 369 |
| 370 | std::uint8_t key[32], iv[32], msgKey[16]; |
| 371 | out.WriteBytes(m_keyFingerprint, 8); |
| 372 | BufferOutputStream buf(len + 32); |
| 373 | std::size_t x = 0; |
| 374 | buf.WriteBytes(m_encryptionKey + 88 + x, 32); |
| 375 | buf.WriteBytes(inner.GetBuffer() + 4, inner.GetLength() - 4); |
| 376 | std::uint8_t msgKeyLarge[32]; |
| 377 | crypto.sha256(buf.GetBuffer(), buf.GetLength(), msgKeyLarge); |
| 378 | std::memcpy(msgKey, msgKeyLarge + 8, 16); |
| 379 | KDF2(msgKey, 0, key, iv); |
| 380 | out.WriteBytes(msgKey, 16); |
| 381 |
| 382 | std::vector<std::uint8_t> aesOut(MSC_STACK_FALLBACK(inner.GetLength(), 1500)); |
| 383 | crypto.aes_ige_encrypt(inner.GetBuffer(), aesOut.data(), inner.GetLength(), key, iv); |
| 384 | out.WriteBytes(aesOut.data(), inner.GetLength()); |
| 385 | } |
| 386 |
| 387 | // relay signature |
| 388 | out.WriteBytes(m_reflectorSelfSecret, 16); |
| 389 | std::uint8_t sig[32]; |
| 390 | crypto.sha256(out.GetBuffer(), out.GetLength(), sig); |
| 391 | out.Rewind(16); |
| 392 | out.WriteBytes(sig, 16); |
| 393 |
| 394 | if (srcPacket.type == PktType::STREAM_DATA || srcPacket.type == PktType::STREAM_DATA_X2 || srcPacket.type == PktType::STREAM_DATA_X3) |
| 395 | { |
| 396 | PacketIdMapping mapping = |
| 397 | { |
| 398 | .ackTime = 0.0, |
| 399 | .seq = srcPacket.seq, |
| 400 | .id = *reinterpret_cast<std::uint16_t*>(sig + 14), |
| 401 | }; |
| 402 | MutexGuard m(m_sentPacketsMutex); |
| 403 | m_recentSentPackets.emplace_back(mapping); |
| 404 | while (m_recentSentPackets.size() > 64) |
| 405 | m_recentSentPackets.erase(m_recentSentPackets.begin()); |
| 406 | } |
| 407 | m_lastSentSeq = srcPacket.seq; |
| 408 |
| 409 | if (IS_MOBILE_NETWORK(m_networkType)) |
| 410 | m_stats.bytesSentMobile += static_cast<std::uint64_t>(out.GetLength()); |
| 411 | else |
| 412 | m_stats.bytesSentWifi += static_cast<std::uint64_t>(out.GetLength()); |
| 413 | } |
| 414 |
| 415 | void VoIPGroupController::SetCallbacks(VoIPGroupController::Callbacks callbacks) |
| 416 | { |
| 417 | VoIPController::SetCallbacks(static_cast<VoIPController::Callbacks&>(callbacks)); |
| 418 | this->m_groupCallbacks = callbacks; |
| 419 | } |
| 420 |
| 421 | std::int32_t VoIPGroupController::GetCurrentUnixtime() |
| 422 | { |
| 423 | return static_cast<std::int32_t>(time(nullptr)) + m_timeDifference; |
| 424 | } |
| 425 |
| 426 | float VoIPGroupController::GetParticipantAudioLevel(std::int32_t userID) |
| 427 | { |
| 428 | if (userID == m_userSelfID) |
| 429 | return m_selfLevelMeter.GetLevel(); |
| 430 | MutexGuard m(m_participantsMutex); |
| 431 | for (const GroupCallParticipant& participant : m_participants) |
| 432 | { |
| 433 | if (participant.userID == userID) |
| 434 | { |
| 435 | return participant.levelMeter->GetLevel(); |
| 436 | } |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 |
| 441 | void VoIPGroupController::SetMicMute(bool mute) |
| 442 | { |
| 443 | m_micMuted = mute; |
| 444 | if (m_audioInput) |
| 445 | { |
| 446 | if (mute) |
| 447 | m_audioInput->Stop(); |
| 448 | else |
| 449 | m_audioInput->Start(); |
| 450 | if (!m_audioInput->IsInitialized()) |
| 451 | { |
| 452 | m_lastError = Error::AUDIO_IO; |
| 453 | SetState(State::FAILED); |
| 454 | return; |
| 455 | } |
| 456 | } |
| 457 | m_outgoingStreams[0]->enabled = !mute; |
| 458 | SerializeAndUpdateOutgoingStreams(); |
| 459 | } |
| 460 |
| 461 | void VoIPGroupController::SetParticipantVolume(std::int32_t userID, float volume) |
| 462 | { |
| 463 | MutexGuard m(m_participantsMutex); |
| 464 | for (const GroupCallParticipant& participant : m_participants) |
| 465 | { |
| 466 | if (participant.userID != userID) |
| 467 | continue; |
| 468 | for (const std::shared_ptr<Stream>& stream : participant.streams) |
| 469 | { |
| 470 | if (stream->type == StreamType::AUDIO) |
| 471 | { |
| 472 | if (stream->decoder != nullptr) |
| 473 | { |
| 474 | float db; |
| 475 | if (volume == 0.0f) |
| 476 | db = -INFINITY; |
| 477 | else if (volume < 1.0f) |
| 478 | db = -50.0f * (1.0f - volume); |
| 479 | else if (volume > 1.0f && volume <= 2.0f) |
| 480 | db = 10.0f * (volume - 1.0f); |
| 481 | else |
| 482 | db = 0.0f; |
| 483 | m_audioMixer->SetInputVolume(stream->callbackWrapper, db); |
| 484 | } |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 |
| 492 | void VoIPGroupController::SerializeAndUpdateOutgoingStreams() |
| 493 | { |
| 494 | BufferOutputStream out(1024); |
| 495 | out.WriteUInt8(static_cast<std::uint8_t>(m_outgoingStreams.size())); |
| 496 |
| 497 | for (const std::shared_ptr<Stream>& stream : m_outgoingStreams) |
| 498 | { |
| 499 | BufferOutputStream o(128); |
| 500 | o.WriteUInt8(stream->id); |
| 501 | o.WriteUInt8(static_cast<std::uint8_t>(stream->type)); |
| 502 | o.WriteUInt32(stream->codec); |
| 503 | o.WriteInt32(static_cast<std::uint8_t>((stream->enabled ? STREAM_FLAG_ENABLED : 0) | STREAM_FLAG_DTX)); |
| 504 | o.WriteUInt16(stream->frameDuration); |
| 505 | out.WriteUInt16(static_cast<std::uint16_t>(o.GetLength())); |
| 506 | out.WriteBytes(o.GetBuffer(), o.GetLength()); |
| 507 | } |
| 508 | if (m_groupCallbacks.updateStreams) |
| 509 | m_groupCallbacks.updateStreams(this, out.GetBuffer(), out.GetLength()); |
| 510 | } |
| 511 |
| 512 | std::string VoIPGroupController::GetDebugString() |
| 513 | { |
| 514 | std::string result = "Remote endpoints: \n"; |
| 515 | char buffer[2048]; |
| 516 | for (const auto& [_, endpoint] : m_endpoints) |
| 517 | { |
| 518 | const char* type; |
| 519 | switch (endpoint.type) |
| 520 | { |
| 521 | case Endpoint::Type::UDP_P2P_INET: |
| 522 | type = "UDP_P2P_INET"; |
| 523 | break; |
| 524 | case Endpoint::Type::UDP_P2P_LAN: |
| 525 | type = "UDP_P2P_LAN"; |
| 526 | break; |
| 527 | case Endpoint::Type::UDP_RELAY: |
| 528 | type = "UDP_RELAY"; |
| 529 | break; |
| 530 | case Endpoint::Type::TCP_RELAY: |
| 531 | type = "TCP_RELAY"; |
| 532 | break; |
| 533 | // default: |
| 534 | // type = "UNKNOWN"; |
| 535 | // break; |
| 536 | } |
| 537 | std::snprintf(buffer, sizeof(buffer), "%s:%u %dms [%s%s]\n", endpoint.address.ToString().c_str(), endpoint.port, |
| 538 | static_cast<int>(endpoint.m_averageRTT * 1000), type, m_currentEndpoint == endpoint.id ? ", IN_USE" : ""); |
| 539 | result += buffer; |
| 540 | } |
| 541 | double avgLate[3]; |
| 542 | std::shared_ptr<JitterBuffer> jitterBuffer = m_incomingStreams.size() == 1 ? m_incomingStreams[0]->jitterBuffer : nullptr; |
| 543 | if (jitterBuffer != nullptr) |
| 544 | jitterBuffer->GetAverageLateCount(avgLate); |
| 545 | else |
| 546 | std::fill(std::begin(avgLate), std::end(avgLate), 0); |
| 547 | std::snprintf( |
| 548 | buffer, |
| 549 | sizeof(buffer), |
| 550 | "RTT avg/min: %d/%d\n" |
| 551 | "Congestion window: %d/%d bytes\n" |
| 552 | "Key fingerprint: %02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n" |
| 553 | "Last sent/ack'd seq: %u/%u\n" |
| 554 | "Send/recv losses: %u/%u (%d%%)\n" |
| 555 | "Audio bitrate: %d kbit\n" |
| 556 | "Bytes sent/recvd: %llu/%llu\n\n", |
| 557 | static_cast<int>(m_congestionControl->GetAverageRTT() * 1000), |
| 558 | static_cast<int>(m_congestionControl->GetMinimumRTT() * 1000), |
| 559 | static_cast<int>(m_congestionControl->GetInflightDataSize()), |
| 560 | static_cast<int>(m_congestionControl->GetCongestionWindow()), |
| 561 | m_keyFingerprint[0], |
| 562 | m_keyFingerprint[1], |
| 563 | m_keyFingerprint[2], |
| 564 | m_keyFingerprint[3], |
| 565 | m_keyFingerprint[4], |
| 566 | m_keyFingerprint[5], |
| 567 | m_keyFingerprint[6], |
| 568 | m_keyFingerprint[7], |
| 569 | m_lastSentSeq, |
| 570 | m_lastRemoteAckSeq, |
| 571 | m_congestionControl->GetSendLossCount(), |
| 572 | m_recvLossCount, |
| 573 | m_encoder ? m_encoder->GetPacketLoss() : 0, |
| 574 | m_encoder ? (m_encoder->GetBitrate() / 1000) : 0, |
| 575 | static_cast<unsigned long long>(m_stats.bytesSentMobile + m_stats.bytesSentWifi), |
| 576 | static_cast<unsigned long long>(m_stats.bytesRecvdMobile + m_stats.bytesRecvdWifi)); |
| 577 |
| 578 | MutexGuard m(m_participantsMutex); |
| 579 | for (const GroupCallParticipant& participant : m_participants) |
| 580 | { |
| 581 | std::snprintf(buffer, sizeof(buffer), "Participant id: %d\n", participant.userID); |
| 582 | result += buffer; |
| 583 | for (const std::shared_ptr<Stream>& stream : participant.streams) |
| 584 | { |
| 585 | char* codec = reinterpret_cast<char*>(&stream->codec); |
| 586 | std::snprintf(buffer, sizeof(buffer), "Stream %d (type %u, codec '%c%c%c%c', %sabled)\n", |
| 587 | stream->id, static_cast<std::uint8_t>(stream->type), codec[3], codec[2], codec[1], codec[0], stream->enabled ? "en" : "dis"); |
| 588 | result += buffer; |
| 589 | if (stream->enabled) |
| 590 | { |
| 591 | if (stream->jitterBuffer != nullptr) |
| 592 | { |
| 593 | std::snprintf(buffer, sizeof(buffer), "Jitter buffer: %d/%.2f\n", |
| 594 | stream->jitterBuffer->GetMinPacketCount(), stream->jitterBuffer->GetAverageDelay()); |
| 595 | result += buffer; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | result += "\n"; |
| 600 | } |
| 601 | return result; |
| 602 | } |
| 603 |