| 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 | return std::to_string(ssrc); |
| 191 | } |
| 192 |
| 193 | } |
| 194 |
| 195 | ContentNegotiationContext::ContentNegotiationContext(const webrtc::WebRtcKeyValueConfig& fieldTrials, bool isOutgoing, rtc::UniqueRandomIdGenerator *uniqueRandomIdGenerator) : |
| 196 | _isOutgoing(isOutgoing), |
| 197 | _uniqueRandomIdGenerator(uniqueRandomIdGenerator) { |
| 198 | _transportDescriptionFactory = std::make_unique<cricket::TransportDescriptionFactory>(fieldTrials); |
| 199 |
| 200 | // tempCertificate is only used to fill in the local SDP |
| 201 | auto tempCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 202 | _transportDescriptionFactory->set_secure(cricket::SecurePolicy::SEC_REQUIRED); |
| 203 | _transportDescriptionFactory->set_certificate(tempCertificate); |
| 204 |
| 205 | _sessionDescriptionFactory = std::make_unique<cricket::MediaSessionDescriptionFactory>(_transportDescriptionFactory.get(), uniqueRandomIdGenerator); |
| 206 |
| 207 | _needNegotiation = true; |
| 208 | } |
| 209 |
| 210 | ContentNegotiationContext::~ContentNegotiationContext() { |
| 211 |
| 212 | } |
| 213 |
| 214 | void ContentNegotiationContext::copyCodecsFromChannelManager(cricket::MediaEngineInterface *mediaEngine, bool randomize) { |
| 215 | cricket::AudioCodecs audioSendCodecs = mediaEngine->voice().send_codecs(); |
| 216 | cricket::AudioCodecs audioRecvCodecs = mediaEngine->voice().recv_codecs(); |
| 217 | cricket::VideoCodecs videoSendCodecs = mediaEngine->video().send_codecs(); |
| 218 | cricket::VideoCodecs videoRecvCodecs = mediaEngine->video().recv_codecs(); |
| 219 |
| 220 | for (const auto &codec : audioSendCodecs) { |
| 221 | if (codec.name == "opus") { |
| 222 | audioSendCodecs = { codec }; |
| 223 | audioRecvCodecs = { codec }; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 |
| 228 | if (randomize) { |
| 229 | for (auto &codec : audioSendCodecs) { |
| 230 | codec.id += 3; |
| 231 | } |
| 232 | for (auto &codec : videoSendCodecs) { |
| 233 | codec.id += 3; |
| 234 | } |
| 235 | for (auto &codec : audioRecvCodecs) { |
| 236 | codec.id += 3; |
| 237 | } |
| 238 | for (auto &codec : videoRecvCodecs) { |
| 239 | codec.id += 3; |
| 240 | } |
| 241 | } |
| 242 |
| 243 | _sessionDescriptionFactory->set_audio_codecs(audioSendCodecs, audioRecvCodecs); |
| 244 | _sessionDescriptionFactory->set_video_codecs(videoSendCodecs, videoRecvCodecs); |
| 245 |
| 246 | int absSendTimeUriId = 2; |
| 247 | int transportSequenceNumberUriId = 3; |
| 248 | int videoRotationUri = 13; |
| 249 |
| 250 | if (randomize) { |
| 251 | absSendTimeUriId = 3; |
| 252 | transportSequenceNumberUriId = 2; |
| 253 | videoRotationUri = 4; |
| 254 | } |
| 255 |
| 256 | _rtpAudioExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, absSendTimeUriId); |
| 257 | _rtpAudioExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, transportSequenceNumberUriId); |
| 258 |
| 259 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, absSendTimeUriId); |
| 260 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, transportSequenceNumberUriId); |
| 261 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kVideoRotationUri, videoRotationUri); |
| 262 | } |
| 263 |
| 264 | std::string ContentNegotiationContext::addOutgoingChannel(signaling::MediaContent::Type mediaType) { |
| 265 | std::string channelId = takeNextOutgoingChannelId(); |
| 266 |
| 267 | cricket::MediaType mappedMediaType; |
| 268 | std::vector<webrtc::RtpHeaderExtensionCapability> rtpExtensions; |
| 269 | switch (mediaType) { |
| 270 | case signaling::MediaContent::Type::Audio: { |
| 271 | mappedMediaType = cricket::MediaType::MEDIA_TYPE_AUDIO; |
| 272 | rtpExtensions = _rtpAudioExtensions; |
| 273 | break; |
| 274 | } |
| 275 | case signaling::MediaContent::Type::Video: { |
| 276 | mappedMediaType = cricket::MediaType::MEDIA_TYPE_VIDEO; |
| 277 | rtpExtensions = _rtpVideoExtensions; |
| 278 | break; |
| 279 | } |
| 280 | default: { |
| 281 | RTC_FATAL() << "Unknown media type"; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | cricket::MediaDescriptionOptions offerDescription(mappedMediaType, channelId, webrtc::RtpTransceiverDirection::kSendOnly, false); |
| 286 | offerDescription.header_extensions = rtpExtensions; |
| 287 |
| 288 | switch (mediaType) { |
| 289 | case signaling::MediaContent::Type::Audio: { |
| 290 | offerDescription.AddAudioSender(channelId, { channelId }); |
| 291 | break; |
| 292 | } |
| 293 | case signaling::MediaContent::Type::Video: { |
| 294 | cricket::SimulcastLayerList simulcastLayers; |
| 295 | offerDescription.AddVideoSender(channelId, { channelId }, {}, simulcastLayers, 1); |
| 296 | break; |
| 297 | } |
| 298 | default: { |
| 299 | RTC_FATAL() << "Unknown media type"; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 |
| 304 | _outgoingChannelDescriptions.emplace_back(std::move(offerDescription)); |
| 305 | _needNegotiation = true; |
| 306 |
| 307 | return channelId; |
| 308 | } |
| 309 |
| 310 | void ContentNegotiationContext::removeOutgoingChannel(std::string const &id) { |
| 311 | for (size_t i = 0; i < _outgoingChannels.size(); i++) { |
| 312 | if (_outgoingChannelDescriptions[i].description.mid == id) { |
| 313 | _outgoingChannelDescriptions.erase(_outgoingChannelDescriptions.begin() + i); |
| 314 |
| 315 | _needNegotiation = true; |
| 316 |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 |
| 322 | std::unique_ptr<cricket::SessionDescription> ContentNegotiationContext::currentSessionDescriptionFromCoordinatedState() { |
| 323 | if (_channelIdOrder.empty()) { |
| 324 | return nullptr; |
| 325 | } |
| 326 |
| 327 | auto sessionDescription = std::make_unique<cricket::SessionDescription>(); |
| 328 |
| 329 | for (const auto &id : _channelIdOrder) { |
| 330 | bool found = false; |
| 331 |
| 332 | for (const auto &channel : _incomingChannels) { |
| 333 | if (contentIdBySsrc(channel.ssrc) == id) { |
| 334 | found = true; |
| 335 |
| 336 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(channel.ssrc), channel, webrtc::RtpTransceiverDirection::kRecvOnly); |
| 337 |
| 338 | cricket::TransportDescription transportDescription; |
| 339 | cricket::TransportInfo transportInfo(contentIdBySsrc(channel.ssrc), transportDescription); |
| 340 | sessionDescription->AddTransportInfo(transportInfo); |
| 341 |
| 342 | sessionDescription->AddContent(std::move(mappedContent)); |
| 343 |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 |
| 348 | for (const auto &channel : _outgoingChannels) { |
| 349 | if (channel.id == id) { |
| 350 | found = true; |
| 351 |
| 352 | auto mappedContent = convertSingalingContentToContentInfo(channel.id, channel.content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 353 |
| 354 | cricket::TransportDescription transportDescription; |
| 355 | cricket::TransportInfo transportInfo(mappedContent.name, transportDescription); |
| 356 | sessionDescription->AddTransportInfo(transportInfo); |
| 357 |
| 358 | sessionDescription->AddContent(std::move(mappedContent)); |
| 359 |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 |
| 364 | if (!found) { |
| 365 | auto mappedContent = createInactiveContentInfo("_" + id); |
| 366 |
| 367 | cricket::TransportDescription transportDescription; |
| 368 | cricket::TransportInfo transportInfo(mappedContent.name, transportDescription); |
| 369 | sessionDescription->AddTransportInfo(transportInfo); |
| 370 |
| 371 | sessionDescription->AddContent(std::move(mappedContent)); |
| 372 | } |
| 373 | } |
| 374 |
| 375 | return sessionDescription; |
| 376 | } |
| 377 |
| 378 | static cricket::MediaDescriptionOptions getIncomingContentDescription(signaling::MediaContent const &content) { |
| 379 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 380 |
| 381 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kRecvOnly, false); |
| 382 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 383 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 384 | } |
| 385 |
| 386 | return contentDescription; |
| 387 | } |
| 388 |
| 389 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::getPendingOffer() { |
| 390 | if (!_needNegotiation) { |
| 391 | return nullptr; |
| 392 | } |
| 393 | if (_pendingOutgoingOffer) { |
| 394 | return nullptr; |
| 395 | } |
| 396 |
| 397 | _needNegotiation = false; |
| 398 |
| 399 | _pendingOutgoingOffer = std::make_unique<PendingOutgoingOffer>(); |
| 400 | _pendingOutgoingOffer->exchangeId = _uniqueRandomIdGenerator->GenerateId(); |
| 401 |
| 402 | auto currentSessionDescription = currentSessionDescriptionFromCoordinatedState(); |
| 403 |
| 404 | cricket::MediaSessionOptions offerOptions; |
| 405 | offerOptions.offer_extmap_allow_mixed = true; |
| 406 | offerOptions.bundle_enabled = true; |
| 407 |
| 408 | for (const auto &id : _channelIdOrder) { |
| 409 | bool found = false; |
| 410 |
| 411 | for (const auto &channel : _outgoingChannelDescriptions) { |
| 412 | if (channel.description.mid == id) { |
| 413 | found = true; |
| 414 | offerOptions.media_description_options.push_back(channel.description); |
| 415 |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 |
| 420 | for (const auto &content : _incomingChannels) { |
| 421 | if (contentIdBySsrc(content.ssrc) == id) { |
| 422 | found = true; |
| 423 | offerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 424 |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 |
| 429 | if (!found) { |
| 430 | cricket::MediaDescriptionOptions contentDescription(cricket::MediaType::MEDIA_TYPE_AUDIO, "_" + id, webrtc::RtpTransceiverDirection::kInactive, false); |
| 431 | offerOptions.media_description_options.push_back(contentDescription); |
| 432 | } |
| 433 | } |
| 434 |
| 435 | for (const auto &channel : _outgoingChannelDescriptions) { |
| 436 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), channel.description.mid) == _channelIdOrder.end()) { |
| 437 | _channelIdOrder.push_back(channel.description.mid); |
| 438 |
| 439 | offerOptions.media_description_options.push_back(channel.description); |
| 440 | } |
| 441 |
| 442 | for (const auto &content : _incomingChannels) { |
| 443 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), contentIdBySsrc(content.ssrc)) == _channelIdOrder.end()) { |
| 444 | _channelIdOrder.push_back(contentIdBySsrc(content.ssrc)); |
| 445 |
| 446 | offerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 |
| 451 | std::unique_ptr<cricket::SessionDescription> offer = _sessionDescriptionFactory->CreateOffer(offerOptions, currentSessionDescription.get()); |
| 452 |
| 453 | auto mappedOffer = std::make_unique<ContentNegotiationContext::NegotiationContents>(); |
| 454 |
| 455 | mappedOffer->exchangeId = _pendingOutgoingOffer->exchangeId; |
| 456 |
| 457 | for (const auto &content : offer->contents()) { |
| 458 | auto mappedContent = convertContentInfoToSingalingContent(content); |
| 459 |
| 460 | if (content.media_description()->direction() == webrtc::RtpTransceiverDirection::kSendOnly) { |
| 461 | mappedOffer->contents.push_back(std::move(mappedContent)); |
| 462 |
| 463 | for (auto &channel : _outgoingChannelDescriptions) { |
| 464 | if (channel.description.mid == content.mid()) { |
| 465 | channel.ssrc = mappedContent.ssrc; |
| 466 | channel.ssrcGroups = mappedContent.ssrcGroups; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 |
| 472 | return mappedOffer; |
| 473 | } |
| 474 |
| 475 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::setRemoteNegotiationContent(std::unique_ptr<NegotiationContents> &&remoteNegotiationContent) { |
| 476 | if (!remoteNegotiationContent) { |
| 477 | return nullptr; |
| 478 | } |
| 479 |
| 480 | if (_pendingOutgoingOffer) { |
| 481 | if (remoteNegotiationContent->exchangeId == _pendingOutgoingOffer->exchangeId) { |
| 482 | setAnswer(std::move(remoteNegotiationContent)); |
| 483 | return nullptr; |
| 484 | } else { |
| 485 | // race condition detected — call initiator wins |
| 486 | if (!_isOutgoing) { |
| 487 | _pendingOutgoingOffer.reset(); |
| 488 | return getAnswer(std::move(remoteNegotiationContent)); |
| 489 | } else { |
| 490 | return nullptr; |
| 491 | } |
| 492 | } |
| 493 | } else { |
| 494 | return getAnswer(std::move(remoteNegotiationContent)); |
| 495 | } |
| 496 | } |
| 497 |
| 498 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::getAnswer(std::unique_ptr<ContentNegotiationContext::NegotiationContents> &&offer) { |
| 499 | auto currentSessionDescription = currentSessionDescriptionFromCoordinatedState(); |
| 500 |
| 501 | auto mappedOffer = std::make_unique<cricket::SessionDescription>(); |
| 502 |
| 503 | cricket::MediaSessionOptions answerOptions; |
| 504 | answerOptions.offer_extmap_allow_mixed = true; |
| 505 | answerOptions.bundle_enabled = true; |
| 506 |
| 507 | for (const auto &id : _channelIdOrder) { |
| 508 | bool found = false; |
| 509 |
| 510 | for (const auto &channel : _outgoingChannels) { |
| 511 | if (channel.id == id) { |
| 512 | found = true; |
| 513 |
| 514 | auto mappedContent = convertSingalingContentToContentInfo(channel.id, channel.content, webrtc::RtpTransceiverDirection::kRecvOnly); |
| 515 |
| 516 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kSendOnly, false); |
| 517 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 518 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 519 | } |
| 520 | answerOptions.media_description_options.push_back(contentDescription); |
| 521 |
| 522 | cricket::TransportDescription transportDescription; |
| 523 | cricket::TransportInfo transportInfo(channel.id, transportDescription); |
| 524 | mappedOffer->AddTransportInfo(transportInfo); |
| 525 |
| 526 | mappedOffer->AddContent(std::move(mappedContent)); |
| 527 |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 |
| 532 | for (const auto &content : offer->contents) { |
| 533 | if (contentIdBySsrc(content.ssrc) == id) { |
| 534 | found = true; |
| 535 |
| 536 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 537 |
| 538 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kRecvOnly, false); |
| 539 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 540 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 541 | } |
| 542 | answerOptions.media_description_options.push_back(contentDescription); |
| 543 |
| 544 | cricket::TransportDescription transportDescription; |
| 545 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 546 | mappedOffer->AddTransportInfo(transportInfo); |
| 547 |
| 548 | mappedOffer->AddContent(std::move(mappedContent)); |
| 549 |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 |
| 554 | if (!found) { |
| 555 | auto mappedContent = createInactiveContentInfo("_" + id); |
| 556 |
| 557 | cricket::MediaDescriptionOptions contentDescription(cricket::MediaType::MEDIA_TYPE_AUDIO, "_" + id, webrtc::RtpTransceiverDirection::kInactive, false); |
| 558 | answerOptions.media_description_options.push_back(contentDescription); |
| 559 |
| 560 | cricket::TransportDescription transportDescription; |
| 561 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 562 | mappedOffer->AddTransportInfo(transportInfo); |
| 563 |
| 564 | mappedOffer->AddContent(std::move(mappedContent)); |
| 565 | } |
| 566 | } |
| 567 |
| 568 | for (const auto &content : offer->contents) { |
| 569 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), contentIdBySsrc(content.ssrc)) == _channelIdOrder.end()) { |
| 570 | _channelIdOrder.push_back(contentIdBySsrc(content.ssrc)); |
| 571 |
| 572 | answerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 573 |
| 574 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 575 |
| 576 | cricket::TransportDescription transportDescription; |
| 577 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 578 | mappedOffer->AddTransportInfo(transportInfo); |
| 579 |
| 580 | mappedOffer->AddContent(std::move(mappedContent)); |
| 581 | } |
| 582 | } |
| 583 |
| 584 | std::unique_ptr<cricket::SessionDescription> answer = _sessionDescriptionFactory->CreateAnswer(mappedOffer.get(), answerOptions, currentSessionDescription.get()); |
| 585 |
| 586 | auto mappedAnswer = std::make_unique<NegotiationContents>(); |
| 587 |
| 588 | mappedAnswer->exchangeId = offer->exchangeId; |
| 589 |
| 590 | std::vector<signaling::MediaContent> incomingChannels; |
| 591 |
| 592 | for (const auto &content : answer->contents()) { |
| 593 | auto mappedContent = convertContentInfoToSingalingContent(content); |
| 594 |
| 595 | if (content.media_description()->direction() == webrtc::RtpTransceiverDirection::kRecvOnly) { |
| 596 | for (const auto &offerContent : offer->contents) { |
| 597 | if (contentIdBySsrc(offerContent.ssrc) == content.mid()) { |
| 598 | mappedContent.ssrc = offerContent.ssrc; |
| 599 | mappedContent.ssrcGroups = offerContent.ssrcGroups; |
| 600 |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 |
| 605 | incomingChannels.push_back(mappedContent); |
| 606 | mappedAnswer->contents.push_back(std::move(mappedContent)); |
| 607 | } |
| 608 | } |
| 609 |
| 610 | _incomingChannels = incomingChannels; |
| 611 |
| 612 | return mappedAnswer; |
| 613 | } |
| 614 |
| 615 | void ContentNegotiationContext::setAnswer(std::unique_ptr<ContentNegotiationContext::NegotiationContents> &&answer) { |
| 616 | if (!_pendingOutgoingOffer) { |
| 617 | return; |
| 618 | } |
| 619 | if (_pendingOutgoingOffer->exchangeId != answer->exchangeId) { |
| 620 | return; |
| 621 | } |
| 622 |
| 623 | _pendingOutgoingOffer.reset(); |
| 624 |
| 625 | _outgoingChannels.clear(); |
| 626 |
| 627 | for (const auto &content : answer->contents) { |
| 628 | for (const auto &pendingChannel : _outgoingChannelDescriptions) { |
| 629 | if (pendingChannel.ssrc != 0 && content.ssrc == pendingChannel.ssrc) { |
| 630 | _outgoingChannels.emplace_back(pendingChannel.description.mid, content); |
| 631 |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 |
| 638 | std::string ContentNegotiationContext::takeNextOutgoingChannelId() { |
| 639 | const auto result = "m" + std::to_string(_nextOutgoingChannelId); |
| 640 | _nextOutgoingChannelId++; |
| 641 |
| 642 | return result; |
| 643 | } |
| 644 |
| 645 | std::unique_ptr<ContentNegotiationContext::CoordinatedState> ContentNegotiationContext::coordinatedState() const { |
| 646 | auto result = std::make_unique<ContentNegotiationContext::CoordinatedState>(); |
| 647 |
| 648 | result->incomingContents = _incomingChannels; |
| 649 | for (const auto &channel : _outgoingChannels) { |
| 650 | bool found = false; |
| 651 |
| 652 | for (const auto &channelDescription : _outgoingChannelDescriptions) { |
| 653 | if (channelDescription.description.mid == channel.id) { |
| 654 | found = true; |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 |
| 659 | if (found) { |
| 660 | result->outgoingContents.push_back(channel.content); |
| 661 | } |
| 662 | } |
| 663 |
| 664 | return result; |
| 665 | } |
| 666 |
| 667 | absl::optional<uint32_t> ContentNegotiationContext::outgoingChannelSsrc(std::string const &id) const { |
| 668 | for (const auto &channel : _outgoingChannels) { |
| 669 | bool found = false; |
| 670 |
| 671 | for (const auto &channelDescription : _outgoingChannelDescriptions) { |
| 672 | if (channelDescription.description.mid == channel.id) { |
| 673 | found = true; |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 |
| 678 | if (found && channel.id == id) { |
| 679 | if (channel.content.ssrc != 0) { |
| 680 | return channel.content.ssrc; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 |
| 685 | return absl::nullopt; |
| 686 | } |
| 687 |
| 688 | } // namespace tgcalls |
| 689 |