| 6 | #include <sstream> |
| 7 |
| 8 | namespace tgcalls { |
| 9 |
| 10 | namespace { |
| 11 |
| 12 | signaling::MediaContent convertContentInfoToSingalingContent(cricket::ContentInfo const &content) { |
| 13 | signaling::MediaContent mappedContent; |
| 14 |
| 15 | switch (content.media_description()->type()) { |
| 16 | case cricket::MediaType::MEDIA_TYPE_AUDIO: { |
| 17 | mappedContent.type = signaling::MediaContent::Type::Audio; |
| 18 |
| 19 | for (const auto &codec : content.media_description()->as_audio()->codecs()) { |
| 20 | signaling::PayloadType mappedPayloadType; |
| 21 | mappedPayloadType.id = codec.id; |
| 22 | mappedPayloadType.name = codec.name; |
| 23 | mappedPayloadType.clockrate = codec.clockrate; |
| 24 | mappedPayloadType.channels = (uint32_t)codec.channels; |
| 25 |
| 26 | for (const auto &feedbackType : codec.feedback_params.params()) { |
| 27 | signaling::FeedbackType mappedFeedbackType; |
| 28 | mappedFeedbackType.type = feedbackType.id(); |
| 29 | mappedFeedbackType.subtype = feedbackType.param(); |
| 30 | mappedPayloadType.feedbackTypes.push_back(std::move(mappedFeedbackType)); |
| 31 | } |
| 32 |
| 33 | for (const auto ¶meter : codec.params) { |
| 34 | mappedPayloadType.parameters.push_back(std::make_pair(parameter.first, parameter.second)); |
| 35 | } |
| 36 | std::sort(mappedPayloadType.parameters.begin(), mappedPayloadType.parameters.end(), [](std::pair<std::string, std::string> const &lhs, std::pair<std::string, std::string> const &rhs) -> bool { |
| 37 | return lhs.first < rhs.first; |
| 38 | }); |
| 39 |
| 40 | mappedContent.payloadTypes.push_back(std::move(mappedPayloadType)); |
| 41 | } |
| 42 | break; |
| 43 | } |
| 44 | case cricket::MediaType::MEDIA_TYPE_VIDEO: { |
| 45 | mappedContent.type = signaling::MediaContent::Type::Video; |
| 46 |
| 47 | for (const auto &codec : content.media_description()->as_video()->codecs()) { |
| 48 | signaling::PayloadType mappedPayloadType; |
| 49 | mappedPayloadType.id = codec.id; |
| 50 | mappedPayloadType.name = codec.name; |
| 51 | mappedPayloadType.clockrate = codec.clockrate; |
| 52 | mappedPayloadType.channels = 0; |
| 53 |
| 54 | for (const auto &feedbackType : codec.feedback_params.params()) { |
| 55 | signaling::FeedbackType mappedFeedbackType; |
| 56 | mappedFeedbackType.type = feedbackType.id(); |
| 57 | mappedFeedbackType.subtype = feedbackType.param(); |
| 58 | mappedPayloadType.feedbackTypes.push_back(std::move(mappedFeedbackType)); |
| 59 | } |
| 60 |
| 61 | for (const auto ¶meter : codec.params) { |
| 62 | mappedPayloadType.parameters.push_back(std::make_pair(parameter.first, parameter.second)); |
| 63 | } |
| 64 | std::sort(mappedPayloadType.parameters.begin(), mappedPayloadType.parameters.end(), [](std::pair<std::string, std::string> const &lhs, std::pair<std::string, std::string> const &rhs) -> bool { |
| 65 | return lhs.first < rhs.first; |
| 66 | }); |
| 67 |
| 68 | mappedContent.payloadTypes.push_back(std::move(mappedPayloadType)); |
| 69 | } |
| 70 | break; |
| 71 | } |
| 72 | default: { |
| 73 | RTC_FATAL() << "Unknown media type"; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 |
| 78 | if (!content.media_description()->streams().empty()) { |
| 79 | mappedContent.ssrc = content.media_description()->streams()[0].first_ssrc(); |
| 80 | for (const auto &ssrcGroup : content.media_description()->streams()[0].ssrc_groups) { |
| 81 | signaling::SsrcGroup mappedSsrcGroup; |
| 82 | mappedSsrcGroup.semantics = ssrcGroup.semantics; |
| 83 | mappedSsrcGroup.ssrcs = ssrcGroup.ssrcs; |
| 84 | mappedContent.ssrcGroups.push_back(std::move(mappedSsrcGroup)); |
| 85 | } |
| 86 | } |
| 87 |
| 88 | for (const auto &extension : content.media_description()->rtp_header_extensions()) { |
| 89 | mappedContent.rtpExtensions.push_back(extension); |
| 90 | } |
| 91 |
| 92 | return mappedContent; |
| 93 | } |
| 94 |
| 95 | cricket::ContentInfo convertSingalingContentToContentInfo(std::string const &contentId, signaling::MediaContent const &content, webrtc::RtpTransceiverDirection direction) { |
| 96 | std::unique_ptr<cricket::MediaContentDescription> contentDescription; |
| 97 |
| 98 | switch (content.type) { |
| 99 | case signaling::MediaContent::Type::Audio: { |
| 100 | auto audioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 101 |
| 102 | for (const auto &payloadType : content.payloadTypes) { |
| 103 | cricket::AudioCodec mappedCodec((int)payloadType.id, payloadType.name, (int)payloadType.clockrate, 0, payloadType.channels); |
| 104 | for (const auto ¶meter : payloadType.parameters) { |
| 105 | mappedCodec.params.insert(parameter); |
| 106 | } |
| 107 | for (const auto &feedbackParam : payloadType.feedbackTypes) { |
| 108 | mappedCodec.AddFeedbackParam(cricket::FeedbackParam(feedbackParam.type, feedbackParam.subtype)); |
| 109 | } |
| 110 | audioDescription->AddCodec(mappedCodec); |
| 111 | } |
| 112 |
| 113 | contentDescription = std::move(audioDescription); |
| 114 |
| 115 | break; |
| 116 | } |
| 117 | case signaling::MediaContent::Type::Video: { |
| 118 | auto videoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 119 |
| 120 | for (const auto &payloadType : content.payloadTypes) { |
| 121 | cricket::VideoCodec mappedCodec((int)payloadType.id, payloadType.name); |
| 122 | for (const auto ¶meter : payloadType.parameters) { |
| 123 | mappedCodec.params.insert(parameter); |
| 124 | } |
| 125 | for (const auto &feedbackParam : payloadType.feedbackTypes) { |
| 126 | mappedCodec.AddFeedbackParam(cricket::FeedbackParam(feedbackParam.type, feedbackParam.subtype)); |
| 127 | } |
| 128 | videoDescription->AddCodec(mappedCodec); |
| 129 | } |
| 130 |
| 131 | contentDescription = std::move(videoDescription); |
| 132 |
| 133 | break; |
| 134 | } |
| 135 | default: { |
| 136 | RTC_FATAL() << "Unknown media type"; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 |
| 141 | cricket::StreamParams streamParams; |
| 142 | streamParams.id = contentId; |
| 143 | streamParams.set_stream_ids({ contentId }); |
| 144 | streamParams.add_ssrc(content.ssrc); |
| 145 | for (const auto &ssrcGroup : content.ssrcGroups) { |
| 146 | streamParams.ssrc_groups.push_back(cricket::SsrcGroup(ssrcGroup.semantics, ssrcGroup.ssrcs)); |
| 147 | for (const auto &ssrc : ssrcGroup.ssrcs) { |
| 148 | if (!streamParams.has_ssrc(ssrc)) { |
| 149 | streamParams.add_ssrc(ssrc); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | contentDescription->AddStream(streamParams); |
| 154 |
| 155 | for (const auto &extension : content.rtpExtensions) { |
| 156 | contentDescription->AddRtpHeaderExtension(extension); |
| 157 | } |
| 158 |
| 159 | contentDescription->set_direction(direction); |
| 160 | contentDescription->set_rtcp_mux(true); |
| 161 |
| 162 | cricket::ContentInfo mappedContentInfo(cricket::MediaProtocolType::kRtp); |
| 163 | mappedContentInfo.name = contentId; |
| 164 | mappedContentInfo.rejected = false; |
| 165 | mappedContentInfo.bundle_only = false; |
| 166 | mappedContentInfo.set_media_description(std::move(contentDescription)); |
| 167 |
| 168 | return mappedContentInfo; |
| 169 | } |
| 170 |
| 171 | cricket::ContentInfo createInactiveContentInfo(std::string const &contentId) { |
| 172 | std::unique_ptr<cricket::MediaContentDescription> contentDescription; |
| 173 |
| 174 | auto audioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 175 | contentDescription = std::move(audioDescription); |
| 176 |
| 177 | contentDescription->set_direction(webrtc::RtpTransceiverDirection::kInactive); |
| 178 | contentDescription->set_rtcp_mux(true); |
| 179 |
| 180 | cricket::ContentInfo mappedContentInfo(cricket::MediaProtocolType::kRtp); |
| 181 | mappedContentInfo.name = contentId; |
| 182 | mappedContentInfo.rejected = false; |
| 183 | mappedContentInfo.bundle_only = false; |
| 184 | mappedContentInfo.set_media_description(std::move(contentDescription)); |
| 185 |
| 186 | return mappedContentInfo; |
| 187 | } |
| 188 |
| 189 | std::string contentIdBySsrc(uint32_t ssrc) { |
| 190 | std::ostringstream contentIdString; |
| 191 |
| 192 | contentIdString << ssrc; |
| 193 |
| 194 | return contentIdString.str(); |
| 195 | } |
| 196 |
| 197 | } |
| 198 |
| 199 | ContentNegotiationContext::ContentNegotiationContext(const webrtc::WebRtcKeyValueConfig& fieldTrials, bool isOutgoing, rtc::UniqueRandomIdGenerator *uniqueRandomIdGenerator) : |
| 200 | _isOutgoing(isOutgoing), |
| 201 | _uniqueRandomIdGenerator(uniqueRandomIdGenerator) { |
| 202 | _transportDescriptionFactory = std::make_unique<cricket::TransportDescriptionFactory>(fieldTrials); |
| 203 |
| 204 | // tempCertificate is only used to fill in the local SDP |
| 205 | auto tempCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 206 | _transportDescriptionFactory->set_secure(cricket::SecurePolicy::SEC_REQUIRED); |
| 207 | _transportDescriptionFactory->set_certificate(tempCertificate); |
| 208 |
| 209 | _sessionDescriptionFactory = std::make_unique<cricket::MediaSessionDescriptionFactory>(_transportDescriptionFactory.get(), uniqueRandomIdGenerator); |
| 210 |
| 211 | _needNegotiation = true; |
| 212 | } |
| 213 |
| 214 | ContentNegotiationContext::~ContentNegotiationContext() { |
| 215 |
| 216 | } |
| 217 |
| 218 | void ContentNegotiationContext::copyCodecsFromChannelManager(cricket::MediaEngineInterface *mediaEngine, bool randomize) { |
| 219 | cricket::AudioCodecs audioSendCodecs = mediaEngine->voice().send_codecs(); |
| 220 | cricket::AudioCodecs audioRecvCodecs = mediaEngine->voice().recv_codecs(); |
| 221 | cricket::VideoCodecs videoSendCodecs = mediaEngine->video().send_codecs(); |
| 222 | cricket::VideoCodecs videoRecvCodecs = mediaEngine->video().recv_codecs(); |
| 223 |
| 224 | for (const auto &codec : audioSendCodecs) { |
| 225 | if (codec.name == "opus") { |
| 226 | audioSendCodecs = { codec }; |
| 227 | audioRecvCodecs = { codec }; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 |
| 232 | if (randomize) { |
| 233 | for (auto &codec : audioSendCodecs) { |
| 234 | codec.id += 3; |
| 235 | } |
| 236 | for (auto &codec : videoSendCodecs) { |
| 237 | codec.id += 3; |
| 238 | } |
| 239 | for (auto &codec : audioRecvCodecs) { |
| 240 | codec.id += 3; |
| 241 | } |
| 242 | for (auto &codec : videoRecvCodecs) { |
| 243 | codec.id += 3; |
| 244 | } |
| 245 | } |
| 246 |
| 247 | _sessionDescriptionFactory->set_audio_codecs(audioSendCodecs, audioRecvCodecs); |
| 248 | _sessionDescriptionFactory->set_video_codecs(videoSendCodecs, videoRecvCodecs); |
| 249 |
| 250 | int absSendTimeUriId = 2; |
| 251 | int transportSequenceNumberUriId = 3; |
| 252 | int videoRotationUri = 13; |
| 253 |
| 254 | if (randomize) { |
| 255 | absSendTimeUriId = 3; |
| 256 | transportSequenceNumberUriId = 2; |
| 257 | videoRotationUri = 4; |
| 258 | } |
| 259 |
| 260 | _rtpAudioExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, absSendTimeUriId); |
| 261 | _rtpAudioExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, transportSequenceNumberUriId); |
| 262 |
| 263 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, absSendTimeUriId); |
| 264 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, transportSequenceNumberUriId); |
| 265 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kVideoRotationUri, videoRotationUri); |
| 266 | } |
| 267 |
| 268 | std::string ContentNegotiationContext::addOutgoingChannel(signaling::MediaContent::Type mediaType) { |
| 269 | std::string channelId = takeNextOutgoingChannelId(); |
| 270 |
| 271 | cricket::MediaType mappedMediaType; |
| 272 | std::vector<webrtc::RtpHeaderExtensionCapability> rtpExtensions; |
| 273 | switch (mediaType) { |
| 274 | case signaling::MediaContent::Type::Audio: { |
| 275 | mappedMediaType = cricket::MediaType::MEDIA_TYPE_AUDIO; |
| 276 | rtpExtensions = _rtpAudioExtensions; |
| 277 | break; |
| 278 | } |
| 279 | case signaling::MediaContent::Type::Video: { |
| 280 | mappedMediaType = cricket::MediaType::MEDIA_TYPE_VIDEO; |
| 281 | rtpExtensions = _rtpVideoExtensions; |
| 282 | break; |
| 283 | } |
| 284 | default: { |
| 285 | RTC_FATAL() << "Unknown media type"; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | cricket::MediaDescriptionOptions offerDescription(mappedMediaType, channelId, webrtc::RtpTransceiverDirection::kSendOnly, false); |
| 290 | offerDescription.header_extensions = rtpExtensions; |
| 291 |
| 292 | switch (mediaType) { |
| 293 | case signaling::MediaContent::Type::Audio: { |
| 294 | offerDescription.AddAudioSender(channelId, { channelId }); |
| 295 | break; |
| 296 | } |
| 297 | case signaling::MediaContent::Type::Video: { |
| 298 | cricket::SimulcastLayerList simulcastLayers; |
| 299 | offerDescription.AddVideoSender(channelId, { channelId }, {}, simulcastLayers, 1); |
| 300 | break; |
| 301 | } |
| 302 | default: { |
| 303 | RTC_FATAL() << "Unknown media type"; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 |
| 308 | _outgoingChannelDescriptions.emplace_back(std::move(offerDescription)); |
| 309 | _needNegotiation = true; |
| 310 |
| 311 | return channelId; |
| 312 | } |
| 313 |
| 314 | void ContentNegotiationContext::removeOutgoingChannel(std::string const &id) { |
| 315 | for (size_t i = 0; i < _outgoingChannels.size(); i++) { |
| 316 | if (_outgoingChannelDescriptions[i].description.mid == id) { |
| 317 | _outgoingChannelDescriptions.erase(_outgoingChannelDescriptions.begin() + i); |
| 318 |
| 319 | _needNegotiation = true; |
| 320 |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 |
| 326 | std::unique_ptr<cricket::SessionDescription> ContentNegotiationContext::currentSessionDescriptionFromCoordinatedState() { |
| 327 | if (_channelIdOrder.empty()) { |
| 328 | return nullptr; |
| 329 | } |
| 330 |
| 331 | auto sessionDescription = std::make_unique<cricket::SessionDescription>(); |
| 332 |
| 333 | for (const auto &id : _channelIdOrder) { |
| 334 | bool found = false; |
| 335 |
| 336 | for (const auto &channel : _incomingChannels) { |
| 337 | if (contentIdBySsrc(channel.ssrc) == id) { |
| 338 | found = true; |
| 339 |
| 340 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(channel.ssrc), channel, webrtc::RtpTransceiverDirection::kRecvOnly); |
| 341 |
| 342 | cricket::TransportDescription transportDescription; |
| 343 | cricket::TransportInfo transportInfo(contentIdBySsrc(channel.ssrc), transportDescription); |
| 344 | sessionDescription->AddTransportInfo(transportInfo); |
| 345 |
| 346 | sessionDescription->AddContent(std::move(mappedContent)); |
| 347 |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 |
| 352 | for (const auto &channel : _outgoingChannels) { |
| 353 | if (channel.id == id) { |
| 354 | found = true; |
| 355 |
| 356 | auto mappedContent = convertSingalingContentToContentInfo(channel.id, channel.content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 357 |
| 358 | cricket::TransportDescription transportDescription; |
| 359 | cricket::TransportInfo transportInfo(mappedContent.name, transportDescription); |
| 360 | sessionDescription->AddTransportInfo(transportInfo); |
| 361 |
| 362 | sessionDescription->AddContent(std::move(mappedContent)); |
| 363 |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 |
| 368 | if (!found) { |
| 369 | auto mappedContent = createInactiveContentInfo("_" + id); |
| 370 |
| 371 | cricket::TransportDescription transportDescription; |
| 372 | cricket::TransportInfo transportInfo(mappedContent.name, transportDescription); |
| 373 | sessionDescription->AddTransportInfo(transportInfo); |
| 374 |
| 375 | sessionDescription->AddContent(std::move(mappedContent)); |
| 376 | } |
| 377 | } |
| 378 |
| 379 | return sessionDescription; |
| 380 | } |
| 381 |
| 382 | static cricket::MediaDescriptionOptions getIncomingContentDescription(signaling::MediaContent const &content) { |
| 383 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 384 |
| 385 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kRecvOnly, false); |
| 386 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 387 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 388 | } |
| 389 |
| 390 | return contentDescription; |
| 391 | } |
| 392 |
| 393 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::getPendingOffer() { |
| 394 | if (!_needNegotiation) { |
| 395 | return nullptr; |
| 396 | } |
| 397 | if (_pendingOutgoingOffer) { |
| 398 | return nullptr; |
| 399 | } |
| 400 |
| 401 | _pendingOutgoingOffer = std::make_unique<PendingOutgoingOffer>(); |
| 402 | _pendingOutgoingOffer->exchangeId = _uniqueRandomIdGenerator->GenerateId(); |
| 403 |
| 404 | auto currentSessionDescription = currentSessionDescriptionFromCoordinatedState(); |
| 405 |
| 406 | cricket::MediaSessionOptions offerOptions; |
| 407 | offerOptions.offer_extmap_allow_mixed = true; |
| 408 | offerOptions.bundle_enabled = true; |
| 409 |
| 410 | for (const auto &id : _channelIdOrder) { |
| 411 | bool found = false; |
| 412 |
| 413 | for (const auto &channel : _outgoingChannelDescriptions) { |
| 414 | if (channel.description.mid == id) { |
| 415 | found = true; |
| 416 | offerOptions.media_description_options.push_back(channel.description); |
| 417 |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 |
| 422 | for (const auto &content : _incomingChannels) { |
| 423 | if (contentIdBySsrc(content.ssrc) == id) { |
| 424 | found = true; |
| 425 | offerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 426 |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 |
| 431 | if (!found) { |
| 432 | cricket::MediaDescriptionOptions contentDescription(cricket::MediaType::MEDIA_TYPE_AUDIO, "_" + id, webrtc::RtpTransceiverDirection::kInactive, false); |
| 433 | offerOptions.media_description_options.push_back(contentDescription); |
| 434 | } |
| 435 | } |
| 436 |
| 437 | for (const auto &channel : _outgoingChannelDescriptions) { |
| 438 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), channel.description.mid) == _channelIdOrder.end()) { |
| 439 | _channelIdOrder.push_back(channel.description.mid); |
| 440 |
| 441 | offerOptions.media_description_options.push_back(channel.description); |
| 442 | } |
| 443 |
| 444 | for (const auto &content : _incomingChannels) { |
| 445 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), contentIdBySsrc(content.ssrc)) == _channelIdOrder.end()) { |
| 446 | _channelIdOrder.push_back(contentIdBySsrc(content.ssrc)); |
| 447 |
| 448 | offerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 |
| 453 | std::unique_ptr<cricket::SessionDescription> offer = _sessionDescriptionFactory->CreateOffer(offerOptions, currentSessionDescription.get()); |
| 454 |
| 455 | auto mappedOffer = std::make_unique<ContentNegotiationContext::NegotiationContents>(); |
| 456 |
| 457 | mappedOffer->exchangeId = _pendingOutgoingOffer->exchangeId; |
| 458 |
| 459 | for (const auto &content : offer->contents()) { |
| 460 | auto mappedContent = convertContentInfoToSingalingContent(content); |
| 461 |
| 462 | if (content.media_description()->direction() == webrtc::RtpTransceiverDirection::kSendOnly) { |
| 463 | mappedOffer->contents.push_back(std::move(mappedContent)); |
| 464 |
| 465 | for (auto &channel : _outgoingChannelDescriptions) { |
| 466 | if (channel.description.mid == content.mid()) { |
| 467 | channel.ssrc = mappedContent.ssrc; |
| 468 | channel.ssrcGroups = mappedContent.ssrcGroups; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 |
| 474 | return mappedOffer; |
| 475 | } |
| 476 |
| 477 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::setRemoteNegotiationContent(std::unique_ptr<NegotiationContents> &&remoteNegotiationContent) { |
| 478 | if (!remoteNegotiationContent) { |
| 479 | return nullptr; |
| 480 | } |
| 481 |
| 482 | if (_pendingOutgoingOffer) { |
| 483 | if (remoteNegotiationContent->exchangeId == _pendingOutgoingOffer->exchangeId) { |
| 484 | setAnswer(std::move(remoteNegotiationContent)); |
| 485 | return nullptr; |
| 486 | } else { |
| 487 | // race condition detected — call initiator wins |
| 488 | if (!_isOutgoing) { |
| 489 | _pendingOutgoingOffer.reset(); |
| 490 | return getAnswer(std::move(remoteNegotiationContent)); |
| 491 | } else { |
| 492 | return nullptr; |
| 493 | } |
| 494 | } |
| 495 | } else { |
| 496 | return getAnswer(std::move(remoteNegotiationContent)); |
| 497 | } |
| 498 | } |
| 499 |
| 500 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::getAnswer(std::unique_ptr<ContentNegotiationContext::NegotiationContents> &&offer) { |
| 501 | auto currentSessionDescription = currentSessionDescriptionFromCoordinatedState(); |
| 502 |
| 503 | auto mappedOffer = std::make_unique<cricket::SessionDescription>(); |
| 504 |
| 505 | cricket::MediaSessionOptions answerOptions; |
| 506 | answerOptions.offer_extmap_allow_mixed = true; |
| 507 | answerOptions.bundle_enabled = true; |
| 508 |
| 509 | for (const auto &id : _channelIdOrder) { |
| 510 | bool found = false; |
| 511 |
| 512 | for (const auto &channel : _outgoingChannels) { |
| 513 | if (channel.id == id) { |
| 514 | found = true; |
| 515 |
| 516 | auto mappedContent = convertSingalingContentToContentInfo(channel.id, channel.content, webrtc::RtpTransceiverDirection::kRecvOnly); |
| 517 |
| 518 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kSendOnly, false); |
| 519 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 520 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 521 | } |
| 522 | answerOptions.media_description_options.push_back(contentDescription); |
| 523 |
| 524 | cricket::TransportDescription transportDescription; |
| 525 | cricket::TransportInfo transportInfo(channel.id, transportDescription); |
| 526 | mappedOffer->AddTransportInfo(transportInfo); |
| 527 |
| 528 | mappedOffer->AddContent(std::move(mappedContent)); |
| 529 |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 |
| 534 | for (const auto &content : offer->contents) { |
| 535 | if (contentIdBySsrc(content.ssrc) == id) { |
| 536 | found = true; |
| 537 |
| 538 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 539 |
| 540 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kRecvOnly, false); |
| 541 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 542 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 543 | } |
| 544 | answerOptions.media_description_options.push_back(contentDescription); |
| 545 |
| 546 | cricket::TransportDescription transportDescription; |
| 547 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 548 | mappedOffer->AddTransportInfo(transportInfo); |
| 549 |
| 550 | mappedOffer->AddContent(std::move(mappedContent)); |
| 551 |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 |
| 556 | if (!found) { |
| 557 | auto mappedContent = createInactiveContentInfo("_" + id); |
| 558 |
| 559 | cricket::MediaDescriptionOptions contentDescription(cricket::MediaType::MEDIA_TYPE_AUDIO, "_" + id, webrtc::RtpTransceiverDirection::kInactive, false); |
| 560 | answerOptions.media_description_options.push_back(contentDescription); |
| 561 |
| 562 | cricket::TransportDescription transportDescription; |
| 563 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 564 | mappedOffer->AddTransportInfo(transportInfo); |
| 565 |
| 566 | mappedOffer->AddContent(std::move(mappedContent)); |
| 567 | } |
| 568 | } |
| 569 |
| 570 | for (const auto &content : offer->contents) { |
| 571 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), contentIdBySsrc(content.ssrc)) == _channelIdOrder.end()) { |
| 572 | _channelIdOrder.push_back(contentIdBySsrc(content.ssrc)); |
| 573 |
| 574 | answerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 575 |
| 576 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 577 |
| 578 | cricket::TransportDescription transportDescription; |
| 579 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 580 | mappedOffer->AddTransportInfo(transportInfo); |
| 581 |
| 582 | mappedOffer->AddContent(std::move(mappedContent)); |
| 583 | } |
| 584 | } |
| 585 |
| 586 | std::unique_ptr<cricket::SessionDescription> answer = _sessionDescriptionFactory->CreateAnswer(mappedOffer.get(), answerOptions, currentSessionDescription.get()); |
| 587 |
| 588 | auto mappedAnswer = std::make_unique<NegotiationContents>(); |
| 589 |
| 590 | mappedAnswer->exchangeId = offer->exchangeId; |
| 591 |
| 592 | std::vector<signaling::MediaContent> incomingChannels; |
| 593 |
| 594 | for (const auto &content : answer->contents()) { |
| 595 | auto mappedContent = convertContentInfoToSingalingContent(content); |
| 596 |
| 597 | if (content.media_description()->direction() == webrtc::RtpTransceiverDirection::kRecvOnly) { |
| 598 | for (const auto &offerContent : offer->contents) { |
| 599 | if (contentIdBySsrc(offerContent.ssrc) == content.mid()) { |
| 600 | mappedContent.ssrc = offerContent.ssrc; |
| 601 | mappedContent.ssrcGroups = offerContent.ssrcGroups; |
| 602 |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 |
| 607 | incomingChannels.push_back(mappedContent); |
| 608 | mappedAnswer->contents.push_back(std::move(mappedContent)); |
| 609 | } |
| 610 | } |
| 611 |
| 612 | _incomingChannels = incomingChannels; |
| 613 |
| 614 | return mappedAnswer; |
| 615 | } |
| 616 |
| 617 | void ContentNegotiationContext::setAnswer(std::unique_ptr<ContentNegotiationContext::NegotiationContents> &&answer) { |
| 618 | if (!_pendingOutgoingOffer) { |
| 619 | return; |
| 620 | } |
| 621 | if (_pendingOutgoingOffer->exchangeId != answer->exchangeId) { |
| 622 | return; |
| 623 | } |
| 624 |
| 625 | _pendingOutgoingOffer.reset(); |
| 626 | _needNegotiation = false; |
| 627 |
| 628 | _outgoingChannels.clear(); |
| 629 |
| 630 | for (const auto &content : answer->contents) { |
| 631 | for (const auto &pendingChannel : _outgoingChannelDescriptions) { |
| 632 | if (pendingChannel.ssrc != 0 && content.ssrc == pendingChannel.ssrc) { |
| 633 | _outgoingChannels.emplace_back(pendingChannel.description.mid, content); |
| 634 |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 |
| 641 | std::string ContentNegotiationContext::takeNextOutgoingChannelId() { |
| 642 | std::ostringstream result; |
| 643 | result << "m" << _nextOutgoingChannelId; |
| 644 | _nextOutgoingChannelId++; |
| 645 |
| 646 | return result.str(); |
| 647 | } |
| 648 |
| 649 | std::unique_ptr<ContentNegotiationContext::CoordinatedState> ContentNegotiationContext::coordinatedState() const { |
| 650 | auto result = std::make_unique<ContentNegotiationContext::CoordinatedState>(); |
| 651 |
| 652 | result->incomingContents = _incomingChannels; |
| 653 | for (const auto &channel : _outgoingChannels) { |
| 654 | bool found = false; |
| 655 |
| 656 | for (const auto &channelDescription : _outgoingChannelDescriptions) { |
| 657 | if (channelDescription.description.mid == channel.id) { |
| 658 | found = true; |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 |
| 663 | if (found) { |
| 664 | result->outgoingContents.push_back(channel.content); |
| 665 | } |
| 666 | } |
| 667 |
| 668 | return result; |
| 669 | } |
| 670 |
| 671 | absl::optional<uint32_t> ContentNegotiationContext::outgoingChannelSsrc(std::string const &id) const { |
| 672 | for (const auto &channel : _outgoingChannels) { |
| 673 | bool found = false; |
| 674 |
| 675 | for (const auto &channelDescription : _outgoingChannelDescriptions) { |
| 676 | if (channelDescription.description.mid == channel.id) { |
| 677 | found = true; |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 |
| 682 | if (found && channel.id == id) { |
| 683 | if (channel.content.ssrc != 0) { |
| 684 | return channel.content.ssrc; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 |
| 689 | return absl::nullopt; |
| 690 | } |
| 691 |
| 692 | } // namespace tgcalls |
| 693 |