| 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 = cricket::CreateAudioCodec((int)payloadType.id, payloadType.name, (int)payloadType.clockrate, 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 | webrtc::SdpVideoFormat videoFormat(payloadType.name); |
| 122 | for (const auto ¶meter : payloadType.parameters) { |
| 123 | videoFormat.parameters.insert(parameter); |
| 124 | } |
| 125 | cricket::VideoCodec mappedCodec = cricket::CreateVideoCodec(videoFormat); |
| 126 | mappedCodec.id = (int)payloadType.id; |
| 127 | for (const auto &feedbackParam : payloadType.feedbackTypes) { |
| 128 | mappedCodec.AddFeedbackParam(cricket::FeedbackParam(feedbackParam.type, feedbackParam.subtype)); |
| 129 | } |
| 130 | videoDescription->AddCodec(mappedCodec); |
| 131 | } |
| 132 |
| 133 | contentDescription = std::move(videoDescription); |
| 134 |
| 135 | break; |
| 136 | } |
| 137 | default: { |
| 138 | RTC_FATAL() << "Unknown media type"; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 |
| 143 | cricket::StreamParams streamParams; |
| 144 | streamParams.id = contentId; |
| 145 | streamParams.set_stream_ids({ contentId }); |
| 146 | streamParams.add_ssrc(content.ssrc); |
| 147 | for (const auto &ssrcGroup : content.ssrcGroups) { |
| 148 | streamParams.ssrc_groups.push_back(cricket::SsrcGroup(ssrcGroup.semantics, ssrcGroup.ssrcs)); |
| 149 | for (const auto &ssrc : ssrcGroup.ssrcs) { |
| 150 | if (!streamParams.has_ssrc(ssrc)) { |
| 151 | streamParams.add_ssrc(ssrc); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | contentDescription->AddStream(streamParams); |
| 156 |
| 157 | for (const auto &extension : content.rtpExtensions) { |
| 158 | contentDescription->AddRtpHeaderExtension(extension); |
| 159 | } |
| 160 |
| 161 | contentDescription->set_direction(direction); |
| 162 | contentDescription->set_rtcp_mux(true); |
| 163 |
| 164 | cricket::ContentInfo mappedContentInfo(cricket::MediaProtocolType::kRtp); |
| 165 | mappedContentInfo.name = contentId; |
| 166 | mappedContentInfo.rejected = false; |
| 167 | mappedContentInfo.bundle_only = false; |
| 168 | mappedContentInfo.set_media_description(std::move(contentDescription)); |
| 169 |
| 170 | return mappedContentInfo; |
| 171 | } |
| 172 |
| 173 | cricket::ContentInfo createInactiveContentInfo(std::string const &contentId) { |
| 174 | std::unique_ptr<cricket::MediaContentDescription> contentDescription; |
| 175 |
| 176 | auto audioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 177 | contentDescription = std::move(audioDescription); |
| 178 |
| 179 | contentDescription->set_direction(webrtc::RtpTransceiverDirection::kInactive); |
| 180 | contentDescription->set_rtcp_mux(true); |
| 181 |
| 182 | cricket::ContentInfo mappedContentInfo(cricket::MediaProtocolType::kRtp); |
| 183 | mappedContentInfo.name = contentId; |
| 184 | mappedContentInfo.rejected = false; |
| 185 | mappedContentInfo.bundle_only = false; |
| 186 | mappedContentInfo.set_media_description(std::move(contentDescription)); |
| 187 |
| 188 | return mappedContentInfo; |
| 189 | } |
| 190 |
| 191 | std::string contentIdBySsrc(uint32_t ssrc) { |
| 192 | return std::to_string(ssrc); |
| 193 | } |
| 194 |
| 195 | } |
| 196 |
| 197 | ContentNegotiationContext::ContentNegotiationContext(const webrtc::FieldTrialsView& fieldTrials, bool isOutgoing, cricket::MediaEngineInterface *mediaEngine, rtc::UniqueRandomIdGenerator *uniqueRandomIdGenerator) : |
| 198 | _isOutgoing(isOutgoing), |
| 199 | _uniqueRandomIdGenerator(uniqueRandomIdGenerator) { |
| 200 | _transportDescriptionFactory = std::make_unique<cricket::TransportDescriptionFactory>(fieldTrials); |
| 201 |
| 202 | // tempCertificate is only used to fill in the local SDP |
| 203 | auto tempCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 204 | _transportDescriptionFactory->set_certificate(tempCertificate); |
| 205 |
| 206 | _sessionDescriptionFactory = std::make_unique<cricket::MediaSessionDescriptionFactory>(mediaEngine, true, uniqueRandomIdGenerator, _transportDescriptionFactory.get()); |
| 207 |
| 208 | _needNegotiation = true; |
| 209 | } |
| 210 |
| 211 | ContentNegotiationContext::~ContentNegotiationContext() { |
| 212 |
| 213 | } |
| 214 |
| 215 | void ContentNegotiationContext::copyCodecsFromChannelManager(cricket::MediaEngineInterface *mediaEngine, bool randomize) { |
| 216 | cricket::AudioCodecs audioSendCodecs = mediaEngine->voice().send_codecs(); |
| 217 | cricket::AudioCodecs audioRecvCodecs = mediaEngine->voice().recv_codecs(); |
| 218 | cricket::VideoCodecs videoSendCodecs = mediaEngine->video().send_codecs(); |
| 219 | cricket::VideoCodecs videoRecvCodecs = mediaEngine->video().recv_codecs(); |
| 220 |
| 221 | for (const auto &codec : audioSendCodecs) { |
| 222 | if (codec.name == "opus") { |
| 223 | audioSendCodecs = { codec }; |
| 224 | audioRecvCodecs = { codec }; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 |
| 229 | if (randomize) { |
| 230 | for (auto &codec : audioSendCodecs) { |
| 231 | codec.id += 3; |
| 232 | } |
| 233 | for (auto &codec : videoSendCodecs) { |
| 234 | codec.id += 3; |
| 235 | } |
| 236 | for (auto &codec : audioRecvCodecs) { |
| 237 | codec.id += 3; |
| 238 | } |
| 239 | for (auto &codec : videoRecvCodecs) { |
| 240 | codec.id += 3; |
| 241 | } |
| 242 | } |
| 243 |
| 244 | _sessionDescriptionFactory->set_audio_codecs(audioSendCodecs, audioRecvCodecs); |
| 245 | _sessionDescriptionFactory->set_video_codecs(videoSendCodecs, videoRecvCodecs); |
| 246 |
| 247 | int absSendTimeUriId = 2; |
| 248 | int transportSequenceNumberUriId = 3; |
| 249 | int videoRotationUri = 13; |
| 250 |
| 251 | if (randomize) { |
| 252 | absSendTimeUriId = 3; |
| 253 | transportSequenceNumberUriId = 2; |
| 254 | videoRotationUri = 4; |
| 255 | } |
| 256 |
| 257 | _rtpAudioExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, absSendTimeUriId); |
| 258 | _rtpAudioExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, transportSequenceNumberUriId); |
| 259 |
| 260 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, absSendTimeUriId); |
| 261 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, transportSequenceNumberUriId); |
| 262 | _rtpVideoExtensions.emplace_back(webrtc::RtpExtension::kVideoRotationUri, videoRotationUri); |
| 263 | } |
| 264 |
| 265 | std::string ContentNegotiationContext::addOutgoingChannel(signaling::MediaContent::Type mediaType) { |
| 266 | std::string channelId = takeNextOutgoingChannelId(); |
| 267 |
| 268 | cricket::MediaType mappedMediaType; |
| 269 | std::vector<webrtc::RtpHeaderExtensionCapability> rtpExtensions; |
| 270 | switch (mediaType) { |
| 271 | case signaling::MediaContent::Type::Audio: { |
| 272 | mappedMediaType = cricket::MediaType::MEDIA_TYPE_AUDIO; |
| 273 | rtpExtensions = _rtpAudioExtensions; |
| 274 | break; |
| 275 | } |
| 276 | case signaling::MediaContent::Type::Video: { |
| 277 | mappedMediaType = cricket::MediaType::MEDIA_TYPE_VIDEO; |
| 278 | rtpExtensions = _rtpVideoExtensions; |
| 279 | break; |
| 280 | } |
| 281 | default: { |
| 282 | RTC_FATAL() << "Unknown media type"; |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | cricket::MediaDescriptionOptions offerDescription(mappedMediaType, channelId, webrtc::RtpTransceiverDirection::kSendOnly, false); |
| 287 | offerDescription.header_extensions = rtpExtensions; |
| 288 |
| 289 | switch (mediaType) { |
| 290 | case signaling::MediaContent::Type::Audio: { |
| 291 | offerDescription.AddAudioSender(channelId, { channelId }); |
| 292 | break; |
| 293 | } |
| 294 | case signaling::MediaContent::Type::Video: { |
| 295 | cricket::SimulcastLayerList simulcastLayers; |
| 296 | offerDescription.AddVideoSender(channelId, { channelId }, {}, simulcastLayers, 1); |
| 297 | break; |
| 298 | } |
| 299 | default: { |
| 300 | RTC_FATAL() << "Unknown media type"; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 |
| 305 | _outgoingChannelDescriptions.emplace_back(std::move(offerDescription)); |
| 306 | _needNegotiation = true; |
| 307 |
| 308 | return channelId; |
| 309 | } |
| 310 |
| 311 | void ContentNegotiationContext::removeOutgoingChannel(std::string const &id) { |
| 312 | for (size_t i = 0; i < _outgoingChannelDescriptions.size(); i++) { |
| 313 | if (_outgoingChannelDescriptions[i].description.mid == id) { |
| 314 | _outgoingChannelDescriptions.erase(_outgoingChannelDescriptions.begin() + i); |
| 315 |
| 316 | _needNegotiation = true; |
| 317 |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 |
| 323 | std::unique_ptr<cricket::SessionDescription> ContentNegotiationContext::currentSessionDescriptionFromCoordinatedState() { |
| 324 | if (_channelIdOrder.empty()) { |
| 325 | return nullptr; |
| 326 | } |
| 327 |
| 328 | auto sessionDescription = std::make_unique<cricket::SessionDescription>(); |
| 329 |
| 330 | for (const auto &id : _channelIdOrder) { |
| 331 | bool found = false; |
| 332 |
| 333 | for (const auto &channel : _incomingChannels) { |
| 334 | if (contentIdBySsrc(channel.ssrc) == id) { |
| 335 | found = true; |
| 336 |
| 337 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(channel.ssrc), channel, webrtc::RtpTransceiverDirection::kRecvOnly); |
| 338 |
| 339 | // only required for content negotiation |
| 340 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 341 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 342 | if (localCertificate) { |
| 343 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 344 | } |
| 345 |
| 346 | std::vector<std::string> transportOptions; |
| 347 | cricket::TransportDescription transportDescription( |
| 348 | transportOptions, |
| 349 | "ufrag", |
| 350 | "pwd", |
| 351 | cricket::IceMode::ICEMODE_FULL, |
| 352 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 353 | fingerprint.get() |
| 354 | ); |
| 355 | cricket::TransportInfo transportInfo(contentIdBySsrc(channel.ssrc), transportDescription); |
| 356 | sessionDescription->AddTransportInfo(transportInfo); |
| 357 |
| 358 | sessionDescription->AddContent(std::move(mappedContent)); |
| 359 |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 |
| 364 | for (const auto &channel : _outgoingChannels) { |
| 365 | if (channel.id == id) { |
| 366 | found = true; |
| 367 |
| 368 | auto mappedContent = convertSingalingContentToContentInfo(channel.id, channel.content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 369 |
| 370 | // only required for content negotiation |
| 371 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 372 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 373 | if (localCertificate) { |
| 374 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 375 | } |
| 376 |
| 377 | std::vector<std::string> transportOptions; |
| 378 | cricket::TransportDescription transportDescription( |
| 379 | transportOptions, |
| 380 | "ufrag", |
| 381 | "pwd", |
| 382 | cricket::IceMode::ICEMODE_FULL, |
| 383 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 384 | fingerprint.get() |
| 385 | ); |
| 386 | cricket::TransportInfo transportInfo(mappedContent.name, transportDescription); |
| 387 | sessionDescription->AddTransportInfo(transportInfo); |
| 388 |
| 389 | sessionDescription->AddContent(std::move(mappedContent)); |
| 390 |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 |
| 395 | if (!found) { |
| 396 | auto mappedContent = createInactiveContentInfo("_" + id); |
| 397 |
| 398 | // only required for content negotiation |
| 399 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 400 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 401 | if (localCertificate) { |
| 402 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 403 | } |
| 404 |
| 405 | std::vector<std::string> transportOptions; |
| 406 | cricket::TransportDescription transportDescription( |
| 407 | transportOptions, |
| 408 | "ufrag", |
| 409 | "pwd", |
| 410 | cricket::IceMode::ICEMODE_FULL, |
| 411 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 412 | fingerprint.get() |
| 413 | ); |
| 414 | cricket::TransportInfo transportInfo(mappedContent.name, transportDescription); |
| 415 | sessionDescription->AddTransportInfo(transportInfo); |
| 416 |
| 417 | sessionDescription->AddContent(std::move(mappedContent)); |
| 418 | } |
| 419 | } |
| 420 |
| 421 | return sessionDescription; |
| 422 | } |
| 423 |
| 424 | static cricket::MediaDescriptionOptions getIncomingContentDescription(signaling::MediaContent const &content) { |
| 425 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 426 |
| 427 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kRecvOnly, false); |
| 428 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 429 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 430 | } |
| 431 |
| 432 | return contentDescription; |
| 433 | } |
| 434 |
| 435 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::getPendingOffer() { |
| 436 | if (!_needNegotiation) { |
| 437 | return nullptr; |
| 438 | } |
| 439 | if (_pendingOutgoingOffer) { |
| 440 | return nullptr; |
| 441 | } |
| 442 |
| 443 | _needNegotiation = false; |
| 444 |
| 445 | _pendingOutgoingOffer = std::make_unique<PendingOutgoingOffer>(); |
| 446 | _pendingOutgoingOffer->exchangeId = _uniqueRandomIdGenerator->GenerateId(); |
| 447 |
| 448 | auto currentSessionDescription = currentSessionDescriptionFromCoordinatedState(); |
| 449 |
| 450 | cricket::MediaSessionOptions offerOptions; |
| 451 | offerOptions.offer_extmap_allow_mixed = true; |
| 452 | offerOptions.bundle_enabled = true; |
| 453 |
| 454 | for (const auto &id : _channelIdOrder) { |
| 455 | bool found = false; |
| 456 |
| 457 | for (const auto &channel : _outgoingChannelDescriptions) { |
| 458 | if (channel.description.mid == id) { |
| 459 | found = true; |
| 460 | offerOptions.media_description_options.push_back(channel.description); |
| 461 |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 |
| 466 | for (const auto &content : _incomingChannels) { |
| 467 | if (contentIdBySsrc(content.ssrc) == id) { |
| 468 | found = true; |
| 469 | offerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 470 |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 |
| 475 | if (!found) { |
| 476 | cricket::MediaDescriptionOptions contentDescription(cricket::MediaType::MEDIA_TYPE_AUDIO, "_" + id, webrtc::RtpTransceiverDirection::kInactive, false); |
| 477 | offerOptions.media_description_options.push_back(contentDescription); |
| 478 | } |
| 479 | } |
| 480 |
| 481 | for (const auto &channel : _outgoingChannelDescriptions) { |
| 482 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), channel.description.mid) == _channelIdOrder.end()) { |
| 483 | _channelIdOrder.push_back(channel.description.mid); |
| 484 |
| 485 | offerOptions.media_description_options.push_back(channel.description); |
| 486 | } |
| 487 |
| 488 | for (const auto &content : _incomingChannels) { |
| 489 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), contentIdBySsrc(content.ssrc)) == _channelIdOrder.end()) { |
| 490 | _channelIdOrder.push_back(contentIdBySsrc(content.ssrc)); |
| 491 |
| 492 | offerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 |
| 497 | auto offerOrError = _sessionDescriptionFactory->CreateOfferOrError(offerOptions, currentSessionDescription.get()); |
| 498 | if (!offerOrError.ok()) { |
| 499 | return nullptr; |
| 500 | } |
| 501 | auto offer = offerOrError.MoveValue(); |
| 502 |
| 503 | auto mappedOffer = std::make_unique<ContentNegotiationContext::NegotiationContents>(); |
| 504 |
| 505 | mappedOffer->exchangeId = _pendingOutgoingOffer->exchangeId; |
| 506 |
| 507 | for (const auto &content : offer->contents()) { |
| 508 | auto mappedContent = convertContentInfoToSingalingContent(content); |
| 509 |
| 510 | if (content.media_description()->direction() == webrtc::RtpTransceiverDirection::kSendOnly) { |
| 511 | mappedOffer->contents.push_back(std::move(mappedContent)); |
| 512 |
| 513 | for (auto &channel : _outgoingChannelDescriptions) { |
| 514 | if (channel.description.mid == content.mid()) { |
| 515 | channel.ssrc = mappedContent.ssrc; |
| 516 | channel.ssrcGroups = mappedContent.ssrcGroups; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 |
| 522 | return mappedOffer; |
| 523 | } |
| 524 |
| 525 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::setRemoteNegotiationContent(std::unique_ptr<NegotiationContents> &&remoteNegotiationContent) { |
| 526 | if (!remoteNegotiationContent) { |
| 527 | return nullptr; |
| 528 | } |
| 529 |
| 530 | if (_pendingOutgoingOffer) { |
| 531 | if (remoteNegotiationContent->exchangeId == _pendingOutgoingOffer->exchangeId) { |
| 532 | setAnswer(std::move(remoteNegotiationContent)); |
| 533 | return nullptr; |
| 534 | } else { |
| 535 | // race condition detected — call initiator wins |
| 536 | if (!_isOutgoing) { |
| 537 | _pendingOutgoingOffer.reset(); |
| 538 | return getAnswer(std::move(remoteNegotiationContent)); |
| 539 | } else { |
| 540 | return nullptr; |
| 541 | } |
| 542 | } |
| 543 | } else { |
| 544 | return getAnswer(std::move(remoteNegotiationContent)); |
| 545 | } |
| 546 | } |
| 547 |
| 548 | std::unique_ptr<ContentNegotiationContext::NegotiationContents> ContentNegotiationContext::getAnswer(std::unique_ptr<ContentNegotiationContext::NegotiationContents> &&offer) { |
| 549 | auto currentSessionDescription = currentSessionDescriptionFromCoordinatedState(); |
| 550 |
| 551 | auto mappedOffer = std::make_unique<cricket::SessionDescription>(); |
| 552 |
| 553 | cricket::MediaSessionOptions answerOptions; |
| 554 | answerOptions.offer_extmap_allow_mixed = true; |
| 555 | answerOptions.bundle_enabled = true; |
| 556 |
| 557 | for (const auto &id : _channelIdOrder) { |
| 558 | bool found = false; |
| 559 |
| 560 | for (const auto &channel : _outgoingChannels) { |
| 561 | if (channel.id == id) { |
| 562 | found = true; |
| 563 |
| 564 | auto mappedContent = convertSingalingContentToContentInfo(channel.id, channel.content, webrtc::RtpTransceiverDirection::kRecvOnly); |
| 565 |
| 566 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kSendOnly, false); |
| 567 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 568 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 569 | } |
| 570 | answerOptions.media_description_options.push_back(contentDescription); |
| 571 |
| 572 | // only required for content negotiation |
| 573 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 574 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 575 | if (localCertificate) { |
| 576 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 577 | } |
| 578 |
| 579 | std::vector<std::string> transportOptions; |
| 580 | cricket::TransportDescription transportDescription( |
| 581 | transportOptions, |
| 582 | "ufrag", |
| 583 | "pwd", |
| 584 | cricket::IceMode::ICEMODE_FULL, |
| 585 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 586 | fingerprint.get() |
| 587 | ); |
| 588 | cricket::TransportInfo transportInfo(channel.id, transportDescription); |
| 589 | mappedOffer->AddTransportInfo(transportInfo); |
| 590 |
| 591 | mappedOffer->AddContent(std::move(mappedContent)); |
| 592 |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 |
| 597 | for (const auto &content : offer->contents) { |
| 598 | if (contentIdBySsrc(content.ssrc) == id) { |
| 599 | found = true; |
| 600 |
| 601 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 602 |
| 603 | cricket::MediaDescriptionOptions contentDescription(mappedContent.media_description()->type(), mappedContent.name, webrtc::RtpTransceiverDirection::kRecvOnly, false); |
| 604 | for (const auto &extension : mappedContent.media_description()->rtp_header_extensions()) { |
| 605 | contentDescription.header_extensions.emplace_back(extension.uri, extension.id); |
| 606 | } |
| 607 | answerOptions.media_description_options.push_back(contentDescription); |
| 608 |
| 609 | // only required for content negotiation |
| 610 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 611 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 612 | if (localCertificate) { |
| 613 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 614 | } |
| 615 |
| 616 | std::vector<std::string> transportOptions; |
| 617 | cricket::TransportDescription transportDescription( |
| 618 | transportOptions, |
| 619 | "ufrag", |
| 620 | "pwd", |
| 621 | cricket::IceMode::ICEMODE_FULL, |
| 622 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 623 | fingerprint.get() |
| 624 | ); |
| 625 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 626 | mappedOffer->AddTransportInfo(transportInfo); |
| 627 |
| 628 | mappedOffer->AddContent(std::move(mappedContent)); |
| 629 |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 |
| 634 | if (!found) { |
| 635 | auto mappedContent = createInactiveContentInfo("_" + id); |
| 636 |
| 637 | cricket::MediaDescriptionOptions contentDescription(cricket::MediaType::MEDIA_TYPE_AUDIO, "_" + id, webrtc::RtpTransceiverDirection::kInactive, false); |
| 638 | answerOptions.media_description_options.push_back(contentDescription); |
| 639 |
| 640 | // only required for content negotiation |
| 641 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 642 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 643 | if (localCertificate) { |
| 644 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 645 | } |
| 646 |
| 647 | std::vector<std::string> transportOptions; |
| 648 | cricket::TransportDescription transportDescription( |
| 649 | transportOptions, |
| 650 | "ufrag", |
| 651 | "pwd", |
| 652 | cricket::IceMode::ICEMODE_FULL, |
| 653 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 654 | fingerprint.get() |
| 655 | ); |
| 656 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 657 | mappedOffer->AddTransportInfo(transportInfo); |
| 658 |
| 659 | mappedOffer->AddContent(std::move(mappedContent)); |
| 660 | } |
| 661 | } |
| 662 |
| 663 | for (const auto &content : offer->contents) { |
| 664 | if (std::find(_channelIdOrder.begin(), _channelIdOrder.end(), contentIdBySsrc(content.ssrc)) == _channelIdOrder.end()) { |
| 665 | _channelIdOrder.push_back(contentIdBySsrc(content.ssrc)); |
| 666 |
| 667 | answerOptions.media_description_options.push_back(getIncomingContentDescription(content)); |
| 668 |
| 669 | auto mappedContent = convertSingalingContentToContentInfo(contentIdBySsrc(content.ssrc), content, webrtc::RtpTransceiverDirection::kSendOnly); |
| 670 |
| 671 | // only required for content negotiation |
| 672 | auto localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 673 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 674 | if (localCertificate) { |
| 675 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*localCertificate.get()); |
| 676 | } |
| 677 |
| 678 | std::vector<std::string> transportOptions; |
| 679 | cricket::TransportDescription transportDescription( |
| 680 | transportOptions, |
| 681 | "ufrag", |
| 682 | "pwd", |
| 683 | cricket::IceMode::ICEMODE_FULL, |
| 684 | cricket::ConnectionRole::CONNECTIONROLE_ACTPASS, |
| 685 | fingerprint.get() |
| 686 | ); |
| 687 | cricket::TransportInfo transportInfo(mappedContent.mid(), transportDescription); |
| 688 | mappedOffer->AddTransportInfo(transportInfo); |
| 689 |
| 690 | mappedOffer->AddContent(std::move(mappedContent)); |
| 691 | } |
| 692 | } |
| 693 |
| 694 | auto answerOrError = _sessionDescriptionFactory->CreateAnswerOrError(mappedOffer.get(), answerOptions, currentSessionDescription.get()); |
| 695 | if (!answerOrError.ok()) { |
| 696 | return nullptr; |
| 697 | } |
| 698 | auto answer = answerOrError.MoveValue(); |
| 699 |
| 700 | auto mappedAnswer = std::make_unique<NegotiationContents>(); |
| 701 |
| 702 | mappedAnswer->exchangeId = offer->exchangeId; |
| 703 |
| 704 | std::vector<signaling::MediaContent> incomingChannels; |
| 705 |
| 706 | for (const auto &content : answer->contents()) { |
| 707 | auto mappedContent = convertContentInfoToSingalingContent(content); |
| 708 |
| 709 | if (content.media_description()->direction() == webrtc::RtpTransceiverDirection::kRecvOnly) { |
| 710 | for (const auto &offerContent : offer->contents) { |
| 711 | if (contentIdBySsrc(offerContent.ssrc) == content.mid()) { |
| 712 | mappedContent.ssrc = offerContent.ssrc; |
| 713 | mappedContent.ssrcGroups = offerContent.ssrcGroups; |
| 714 |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 |
| 719 | incomingChannels.push_back(mappedContent); |
| 720 | mappedAnswer->contents.push_back(std::move(mappedContent)); |
| 721 | } |
| 722 | } |
| 723 |
| 724 | _incomingChannels = incomingChannels; |
| 725 |
| 726 | return mappedAnswer; |
| 727 | } |
| 728 |
| 729 | void ContentNegotiationContext::setAnswer(std::unique_ptr<ContentNegotiationContext::NegotiationContents> &&answer) { |
| 730 | if (!_pendingOutgoingOffer) { |
| 731 | return; |
| 732 | } |
| 733 | if (_pendingOutgoingOffer->exchangeId != answer->exchangeId) { |
| 734 | return; |
| 735 | } |
| 736 |
| 737 | _pendingOutgoingOffer.reset(); |
| 738 |
| 739 | _outgoingChannels.clear(); |
| 740 |
| 741 | for (const auto &content : answer->contents) { |
| 742 | for (const auto &pendingChannel : _outgoingChannelDescriptions) { |
| 743 | if (pendingChannel.ssrc != 0 && content.ssrc == pendingChannel.ssrc) { |
| 744 | _outgoingChannels.emplace_back(pendingChannel.description.mid, content); |
| 745 |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 |
| 752 | std::string ContentNegotiationContext::takeNextOutgoingChannelId() { |
| 753 | const auto result = "m" + std::to_string(_nextOutgoingChannelId); |
| 754 | _nextOutgoingChannelId++; |
| 755 |
| 756 | return result; |
| 757 | } |
| 758 |
| 759 | std::unique_ptr<ContentNegotiationContext::CoordinatedState> ContentNegotiationContext::coordinatedState() const { |
| 760 | auto result = std::make_unique<ContentNegotiationContext::CoordinatedState>(); |
| 761 |
| 762 | result->incomingContents = _incomingChannels; |
| 763 | for (const auto &channel : _outgoingChannels) { |
| 764 | bool found = false; |
| 765 |
| 766 | for (const auto &channelDescription : _outgoingChannelDescriptions) { |
| 767 | if (channelDescription.description.mid == channel.id) { |
| 768 | found = true; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 |
| 773 | if (found) { |
| 774 | result->outgoingContents.push_back(channel.content); |
| 775 | } |
| 776 | } |
| 777 |
| 778 | return result; |
| 779 | } |
| 780 |
| 781 | absl::optional<uint32_t> ContentNegotiationContext::outgoingChannelSsrc(std::string const &id) const { |
| 782 | for (const auto &channel : _outgoingChannels) { |
| 783 | bool found = false; |
| 784 |
| 785 | for (const auto &channelDescription : _outgoingChannelDescriptions) { |
| 786 | if (channelDescription.description.mid == channel.id) { |
| 787 | found = true; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 |
| 792 | if (found && channel.id == id) { |
| 793 | if (channel.content.ssrc != 0) { |
| 794 | return channel.content.ssrc; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 |
| 799 | return absl::nullopt; |
| 800 | } |
| 801 |
| 802 | } // namespace tgcalls |
| 803 |