| #include "VideoCapturerInterface.h" |
| 6 | #include "v2/NativeNetworkingImpl.h" |
| 7 | #include "v2/Signaling.h" |
| 8 |
| 9 | #include "CodecSelectHelper.h" |
| 10 | #include "platform/PlatformInterface.h" |
| 11 |
| 12 | #include "api/audio_codecs/audio_decoder_factory_template.h" |
| 13 | #include "api/audio_codecs/audio_encoder_factory_template.h" |
| 14 | #include "api/audio_codecs/opus/audio_decoder_opus.h" |
| 15 | #include "api/audio_codecs/opus/audio_decoder_multi_channel_opus.h" |
| 16 | #include "api/audio_codecs/opus/audio_encoder_opus.h" |
| 17 | #include "api/audio_codecs/L16/audio_decoder_L16.h" |
| 18 | #include "api/audio_codecs/L16/audio_encoder_L16.h" |
| 19 | #include "api/task_queue/default_task_queue_factory.h" |
| 20 | #include "media/engine/webrtc_media_engine.h" |
| 21 | #include "system_wrappers/include/field_trial.h" |
| 22 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
| 23 | #include "call/call.h" |
| 24 | #include "api/call/audio_sink.h" |
| 25 | #include "modules/audio_processing/audio_buffer.h" |
| 26 | #include "absl/strings/match.h" |
| 27 | #include "pc/channel_manager.h" |
| 28 | #include "audio/audio_state.h" |
| 29 | #include "modules/audio_coding/neteq/default_neteq_factory.h" |
| 30 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 31 | #include "api/candidate.h" |
| 32 | #include "api/jsep_ice_candidate.h" |
| 33 | #include "pc/used_ids.h" |
| 34 | #include "media/base/sdp_video_format_utils.h" |
| 35 |
| 36 | #include "AudioFrame.h" |
| 37 | #include "ThreadLocalObject.h" |
| 38 | #include "Manager.h" |
| 39 | #include "NetworkManager.h" |
| 40 | #include "VideoCaptureInterfaceImpl.h" |
| 41 | #include "platform/PlatformInterface.h" |
| 42 | #include "LogSinkImpl.h" |
| 43 | #include "CodecSelectHelper.h" |
| 44 | #include "AudioDeviceHelper.h" |
| 45 | #include "SignalingEncryption.h" |
| 46 | #ifdef WEBRTC_IOS |
| 47 | #include "platform/darwin/iOS/tgcalls_audio_device_module_ios.h" |
| 48 | #endif |
| 49 | #include <random> |
| 50 | #include <sstream> |
| 51 |
| 52 | namespace tgcalls { |
| 53 | namespace { |
| 54 |
| 55 | static std::string intToString(int value) { |
| 56 | std::ostringstream stringStream; |
| 57 | stringStream << value; |
| 58 | return stringStream.str(); |
| 59 | } |
| 60 |
| 61 | static VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInterface *videoCapture) { |
| 62 | return videoCapture |
| 63 | ? static_cast<VideoCaptureInterfaceImpl*>(videoCapture)->object()->getSyncAssumingSameThread() |
| 64 | : nullptr; |
| 65 | } |
| 66 |
| 67 | struct OutgoingVideoFormat { |
| 68 | cricket::VideoCodec videoCodec; |
| 69 | absl::optional<cricket::VideoCodec> rtxCodec; |
| 70 | }; |
| 71 |
| 72 | static void addDefaultFeedbackParams(cricket::VideoCodec *codec) { |
| 73 | // Don't add any feedback params for RED and ULPFEC. |
| 74 | if (codec->name == cricket::kRedCodecName || codec->name == cricket::kUlpfecCodecName) { |
| 75 | return; |
| 76 | } |
| 77 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamRemb, cricket::kParamValueEmpty)); |
| 78 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc, cricket::kParamValueEmpty)); |
| 79 | // Don't add any more feedback params for FLEXFEC. |
| 80 | if (codec->name == cricket::kFlexfecCodecName) { |
| 81 | return; |
| 82 | } |
| 83 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamCcm, cricket::kRtcpFbCcmParamFir)); |
| 84 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamNack, cricket::kParamValueEmpty)); |
| 85 | codec->AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamNack, cricket::kRtcpFbNackParamPli)); |
| 86 | } |
| 87 |
| 88 | template <class C> |
| 89 | static bool IsRtxCodec(const C& codec) { |
| 90 | return absl::EqualsIgnoreCase(codec.name, cricket::kRtxCodecName); |
| 91 | } |
| 92 |
| 93 | template <class C> |
| 94 | static bool ReferencedCodecsMatch(const std::vector<C>& codecs1, |
| 95 | const int codec1_id, |
| 96 | const std::vector<C>& codecs2, |
| 97 | const int codec2_id) { |
| 98 | const C* codec1 = FindCodecById(codecs1, codec1_id); |
| 99 | const C* codec2 = FindCodecById(codecs2, codec2_id); |
| 100 | return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2); |
| 101 | } |
| 102 |
| 103 | // Finds a codec in |codecs2| that matches |codec_to_match|, which is |
| 104 | // a member of |codecs1|. If |codec_to_match| is an RTX codec, both |
| 105 | // the codecs themselves and their associated codecs must match. |
| 106 | template <class C> |
| 107 | static bool FindMatchingCodec(const std::vector<C>& codecs1, |
| 108 | const std::vector<C>& codecs2, |
| 109 | const C& codec_to_match, |
| 110 | C* found_codec) { |
| 111 | // |codec_to_match| should be a member of |codecs1|, in order to look up RTX |
| 112 | // codecs' associated codecs correctly. If not, that's a programming error. |
| 113 | RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const C& codec) { |
| 114 | return &codec == &codec_to_match; |
| 115 | })); |
| 116 | for (const C& potential_match : codecs2) { |
| 117 | if (potential_match.Matches(codec_to_match)) { |
| 118 | if (IsRtxCodec(codec_to_match)) { |
| 119 | int apt_value_1 = 0; |
| 120 | int apt_value_2 = 0; |
| 121 | if (!codec_to_match.GetParam(cricket::kCodecParamAssociatedPayloadType, |
| 122 | &apt_value_1) || |
| 123 | !potential_match.GetParam(cricket::kCodecParamAssociatedPayloadType, |
| 124 | &apt_value_2)) { |
| 125 | RTC_LOG(LS_WARNING) << "RTX missing associated payload type."; |
| 126 | continue; |
| 127 | } |
| 128 | if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2, |
| 129 | apt_value_2)) { |
| 130 | continue; |
| 131 | } |
| 132 | } |
| 133 | if (found_codec) { |
| 134 | *found_codec = potential_match; |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 |
| 142 | template <class C> |
| 143 | static void NegotiatePacketization(const C& local_codec, |
| 144 | const C& remote_codec, |
| 145 | C* negotiated_codec) {} |
| 146 |
| 147 | template <> |
| 148 | void NegotiatePacketization(const cricket::VideoCodec& local_codec, |
| 149 | const cricket::VideoCodec& remote_codec, |
| 150 | cricket::VideoCodec* negotiated_codec) { |
| 151 | negotiated_codec->packetization = |
| 152 | cricket::VideoCodec::IntersectPacketization(local_codec, remote_codec); |
| 153 | } |
| 154 |
| 155 | template <class C> |
| 156 | static void NegotiateCodecs(const std::vector<C>& local_codecs, |
| 157 | const std::vector<C>& offered_codecs, |
| 158 | std::vector<C>* negotiated_codecs, |
| 159 | bool keep_offer_order) { |
| 160 | for (const C& ours : local_codecs) { |
| 161 | C theirs; |
| 162 | // Note that we intentionally only find one matching codec for each of our |
| 163 | // local codecs, in case the remote offer contains duplicate codecs. |
| 164 | if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) { |
| 165 | C negotiated = ours; |
| 166 | NegotiatePacketization(ours, theirs, &negotiated); |
| 167 | negotiated.IntersectFeedbackParams(theirs); |
| 168 | if (IsRtxCodec(negotiated)) { |
| 169 | const auto apt_it = |
| 170 | theirs.params.find(cricket::kCodecParamAssociatedPayloadType); |
| 171 | // FindMatchingCodec shouldn't return something with no apt value. |
| 172 | RTC_DCHECK(apt_it != theirs.params.end()); |
| 173 | negotiated.SetParam(cricket::kCodecParamAssociatedPayloadType, apt_it->second); |
| 174 | } |
| 175 | if (absl::EqualsIgnoreCase(ours.name, cricket::kH264CodecName)) { |
| 176 | webrtc::H264GenerateProfileLevelIdForAnswer( |
| 177 | ours.params, theirs.params, &negotiated.params); |
| 178 | } |
| 179 | negotiated.id = theirs.id; |
| 180 | negotiated.name = theirs.name; |
| 181 | negotiated_codecs->push_back(std::move(negotiated)); |
| 182 | } |
| 183 | } |
| 184 | if (keep_offer_order) { |
| 185 | // RFC3264: Although the answerer MAY list the formats in their desired |
| 186 | // order of preference, it is RECOMMENDED that unless there is a |
| 187 | // specific reason, the answerer list formats in the same relative order |
| 188 | // they were present in the offer. |
| 189 | // This can be skipped when the transceiver has any codec preferences. |
| 190 | std::unordered_map<int, int> payload_type_preferences; |
| 191 | int preference = static_cast<int>(offered_codecs.size() + 1); |
| 192 | for (const C& codec : offered_codecs) { |
| 193 | payload_type_preferences[codec.id] = preference--; |
| 194 | } |
| 195 | absl::c_sort(*negotiated_codecs, [&payload_type_preferences](const C& a, |
| 196 | const C& b) { |
| 197 | return payload_type_preferences[a.id] > payload_type_preferences[b.id]; |
| 198 | }); |
| 199 | } |
| 200 | } |
| 201 |
| 202 | // Find the codec in |codec_list| that |rtx_codec| is associated with. |
| 203 | template <class C> |
| 204 | static const C* GetAssociatedCodec(const std::vector<C>& codec_list, |
| 205 | const C& rtx_codec) { |
| 206 | std::string associated_pt_str; |
| 207 | if (!rtx_codec.GetParam(cricket::kCodecParamAssociatedPayloadType, |
| 208 | &associated_pt_str)) { |
| 209 | RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name |
| 210 | << " is missing an associated payload type."; |
| 211 | return nullptr; |
| 212 | } |
| 213 |
| 214 | int associated_pt; |
| 215 | if (!rtc::FromString(associated_pt_str, &associated_pt)) { |
| 216 | RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str |
| 217 | << " of RTX codec " << rtx_codec.name |
| 218 | << " to an integer."; |
| 219 | return nullptr; |
| 220 | } |
| 221 |
| 222 | // Find the associated reference codec for the reference RTX codec. |
| 223 | const C* associated_codec = FindCodecById(codec_list, associated_pt); |
| 224 | if (!associated_codec) { |
| 225 | RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type " |
| 226 | << associated_pt << " for RTX codec " << rtx_codec.name |
| 227 | << "."; |
| 228 | } |
| 229 | return associated_codec; |
| 230 | } |
| 231 |
| 232 | // Adds all codecs from |reference_codecs| to |offered_codecs| that don't |
| 233 | // already exist in |offered_codecs| and ensure the payload types don't |
| 234 | // collide. |
| 235 | template <class C> |
| 236 | static void MergeCodecs(const std::vector<C>& reference_codecs, |
| 237 | std::vector<C>* offered_codecs, |
| 238 | cricket::UsedPayloadTypes* used_pltypes) { |
| 239 | // Add all new codecs that are not RTX codecs. |
| 240 | for (const C& reference_codec : reference_codecs) { |
| 241 | if (!IsRtxCodec(reference_codec) && |
| 242 | !FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 243 | reference_codec, nullptr)) { |
| 244 | C codec = reference_codec; |
| 245 | used_pltypes->FindAndSetIdUsed(&codec); |
| 246 | offered_codecs->push_back(codec); |
| 247 | } |
| 248 | } |
| 249 |
| 250 | // Add all new RTX codecs. |
| 251 | for (const C& reference_codec : reference_codecs) { |
| 252 | if (IsRtxCodec(reference_codec) && |
| 253 | !FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 254 | reference_codec, nullptr)) { |
| 255 | C rtx_codec = reference_codec; |
| 256 | const C* associated_codec = |
| 257 | GetAssociatedCodec(reference_codecs, rtx_codec); |
| 258 | if (!associated_codec) { |
| 259 | continue; |
| 260 | } |
| 261 | // Find a codec in the offered list that matches the reference codec. |
| 262 | // Its payload type may be different than the reference codec. |
| 263 | C matching_codec; |
| 264 | if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 265 | *associated_codec, &matching_codec)) { |
| 266 | RTC_LOG(LS_WARNING) |
| 267 | << "Couldn't find matching " << associated_codec->name << " codec."; |
| 268 | continue; |
| 269 | } |
| 270 |
| 271 | rtx_codec.params[cricket::kCodecParamAssociatedPayloadType] = |
| 272 | rtc::ToString(matching_codec.id); |
| 273 | used_pltypes->FindAndSetIdUsed(&rtx_codec); |
| 274 | offered_codecs->push_back(rtx_codec); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 |
| 279 | static std::vector<OutgoingVideoFormat> generateAvailableVideoFormats(std::vector<webrtc::SdpVideoFormat> const &formats) { |
| 280 | if (formats.empty()) { |
| 281 | return {}; |
| 282 | } |
| 283 |
| 284 | constexpr int kFirstDynamicPayloadType = 100; |
| 285 | constexpr int kLastDynamicPayloadType = 127; |
| 286 |
| 287 | int payload_type = kFirstDynamicPayloadType; |
| 288 |
| 289 | std::vector<OutgoingVideoFormat> result; |
| 290 |
| 291 | //bool codecSelected = false; |
| 292 |
| 293 | for (const auto &format : formats) { |
| 294 | /*if (codecSelected) { |
| 295 | break; |
| 296 | }*/ |
| 297 |
| 298 | bool alreadyAdded = false; |
| 299 | for (const auto &it : result) { |
| 300 | if (it.videoCodec.name == format.name) { |
| 301 | alreadyAdded = true; |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | if (alreadyAdded) { |
| 306 | continue; |
| 307 | } |
| 308 |
| 309 | OutgoingVideoFormat resultFormat; |
| 310 |
| 311 | cricket::VideoCodec codec(format); |
| 312 | codec.id = payload_type; |
| 313 | addDefaultFeedbackParams(&codec); |
| 314 |
| 315 | resultFormat.videoCodec = codec; |
| 316 | //codecSelected = true; |
| 317 |
| 318 | // Increment payload type. |
| 319 | ++payload_type; |
| 320 | if (payload_type > kLastDynamicPayloadType) { |
| 321 | RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest."; |
| 322 | break; |
| 323 | } |
| 324 |
| 325 | // Add associated RTX codec for non-FEC codecs. |
| 326 | if (!absl::EqualsIgnoreCase(codec.name, cricket::kUlpfecCodecName) && |
| 327 | !absl::EqualsIgnoreCase(codec.name, cricket::kFlexfecCodecName)) { |
| 328 | resultFormat.rtxCodec = cricket::VideoCodec::CreateRtxCodec(payload_type, codec.id); |
| 329 |
| 330 | // Increment payload type. |
| 331 | ++payload_type; |
| 332 | if (payload_type > kLastDynamicPayloadType) { |
| 333 | RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest."; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 |
| 338 | result.push_back(std::move(resultFormat)); |
| 339 | } |
| 340 | return result; |
| 341 | } |
| 342 |
| 343 | static void getCodecsFromMediaContent(signaling::MediaContent const &content, std::vector<cricket::VideoCodec> &codecs) { |
| 344 | for (const auto &payloadType : content.payloadTypes) { |
| 345 | cricket::VideoCodec codec(payloadType.id, payloadType.name); |
| 346 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 347 | codec.AddFeedbackParam(cricket::FeedbackParam(feedbackType.type, feedbackType.subtype)); |
| 348 | } |
| 349 | for (const auto ¶meter : payloadType.parameters) { |
| 350 | codec.SetParam(parameter.first, parameter.second); |
| 351 | } |
| 352 | codecs.push_back(std::move(codec)); |
| 353 | } |
| 354 | } |
| 355 |
| 356 | static std::vector<signaling::PayloadType> getPayloadTypesFromVideoCodecs(std::vector<cricket::VideoCodec> const &codecs) { |
| 357 | std::vector<signaling::PayloadType> payloadTypes; |
| 358 |
| 359 | for (const auto &codec : codecs) { |
| 360 | signaling::PayloadType payloadType; |
| 361 |
| 362 | payloadType.id = codec.id; |
| 363 | payloadType.name = codec.name; |
| 364 | payloadType.clockrate = 90000; |
| 365 | payloadType.channels = 0; |
| 366 |
| 367 | for (const auto &feedbackParam : codec.feedback_params.params()) { |
| 368 | signaling::FeedbackType feedbackType; |
| 369 | feedbackType.type = feedbackParam.id(); |
| 370 | feedbackType.subtype = feedbackParam.param(); |
| 371 | payloadType.feedbackTypes.push_back(std::move(feedbackType)); |
| 372 | } |
| 373 |
| 374 | for (const auto ¶m : codec.params) { |
| 375 | payloadType.parameters.push_back(std::make_pair(param.first, param.second)); |
| 376 | } |
| 377 |
| 378 | payloadTypes.push_back(std::move(payloadType)); |
| 379 | } |
| 380 |
| 381 | return payloadTypes; |
| 382 | } |
| 383 |
| 384 | static void getCodecsFromMediaContent(signaling::MediaContent const &content, std::vector<cricket::AudioCodec> &codecs) { |
| 385 | for (const auto &payloadType : content.payloadTypes) { |
| 386 | cricket::AudioCodec codec(payloadType.id, payloadType.name, payloadType.clockrate, 0, payloadType.channels); |
| 387 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 388 | codec.AddFeedbackParam(cricket::FeedbackParam(feedbackType.type, feedbackType.subtype)); |
| 389 | } |
| 390 | for (const auto ¶meter : payloadType.parameters) { |
| 391 | codec.SetParam(parameter.first, parameter.second); |
| 392 | } |
| 393 | codecs.push_back(std::move(codec)); |
| 394 | } |
| 395 | } |
| 396 |
| 397 | static std::vector<signaling::PayloadType> getPayloadTypesFromAudioCodecs(std::vector<cricket::AudioCodec> const &codecs) { |
| 398 | std::vector<signaling::PayloadType> payloadTypes; |
| 399 |
| 400 | for (const auto &codec : codecs) { |
| 401 | signaling::PayloadType payloadType; |
| 402 |
| 403 | payloadType.id = codec.id; |
| 404 | payloadType.name = codec.name; |
| 405 | payloadType.clockrate = codec.clockrate; |
| 406 | payloadType.channels = (uint32_t)codec.channels; |
| 407 |
| 408 | for (const auto &feedbackParam : codec.feedback_params.params()) { |
| 409 | signaling::FeedbackType feedbackType; |
| 410 | feedbackType.type = feedbackParam.id(); |
| 411 | feedbackType.subtype = feedbackParam.param(); |
| 412 | payloadType.feedbackTypes.push_back(std::move(feedbackType)); |
| 413 | } |
| 414 |
| 415 | for (const auto ¶m : codec.params) { |
| 416 | payloadType.parameters.push_back(std::make_pair(param.first, param.second)); |
| 417 | } |
| 418 |
| 419 | payloadTypes.push_back(std::move(payloadType)); |
| 420 | } |
| 421 |
| 422 | return payloadTypes; |
| 423 | } |
| 424 |
| 425 | template <class C> |
| 426 | struct NegotiatedMediaContent { |
| 427 | uint32_t ssrc = 0; |
| 428 | std::vector<signaling::SsrcGroup> ssrcGroups; |
| 429 | std::vector<webrtc::RtpExtension> rtpExtensions; |
| 430 | std::vector<C> codecs; |
| 431 | }; |
| 432 |
| 433 | static bool FindByUri(const cricket::RtpHeaderExtensions& extensions, |
| 434 | const webrtc::RtpExtension& ext_to_match, |
| 435 | webrtc::RtpExtension* found_extension) { |
| 436 | // We assume that all URIs are given in a canonical format. |
| 437 | const webrtc::RtpExtension* found = |
| 438 | webrtc::RtpExtension::FindHeaderExtensionByUri( |
| 439 | extensions, |
| 440 | ext_to_match.uri, |
| 441 | webrtc::RtpExtension::Filter::kPreferEncryptedExtension |
| 442 | ); |
| 443 | if (!found) { |
| 444 | return false; |
| 445 | } |
| 446 | if (found_extension) { |
| 447 | *found_extension = *found; |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 |
| 452 | template <class C> |
| 453 | static NegotiatedMediaContent<C> negotiateMediaContent(signaling::MediaContent const &baseMediaContent, signaling::MediaContent const &localContent, signaling::MediaContent const &remoteContent, bool isAnswer) { |
| 454 | std::vector<C> localCodecs; |
| 455 | getCodecsFromMediaContent(localContent, localCodecs); |
| 456 |
| 457 | std::vector<C> remoteCodecs; |
| 458 | getCodecsFromMediaContent(remoteContent, remoteCodecs); |
| 459 |
| 460 | std::vector<C> negotiatedCodecs; |
| 461 |
| 462 | cricket::UsedPayloadTypes usedPayloadTypes; |
| 463 | NegotiateCodecs<C>(localCodecs, remoteCodecs, &negotiatedCodecs, true); |
| 464 |
| 465 | NegotiatedMediaContent<C> result; |
| 466 |
| 467 | result.ssrc = baseMediaContent.ssrc; |
| 468 | result.ssrcGroups = baseMediaContent.ssrcGroups; |
| 469 | result.codecs = std::move(negotiatedCodecs); |
| 470 |
| 471 | cricket::UsedRtpHeaderExtensionIds extensionIds(cricket::UsedRtpHeaderExtensionIds::IdDomain::kOneByteOnly); |
| 472 |
| 473 | for (const auto &extension : remoteContent.rtpExtensions) { |
| 474 | if (isAnswer) { |
| 475 | webrtc::RtpExtension found; |
| 476 | if (!FindByUri(localContent.rtpExtensions, extension, &found)) { |
| 477 | continue; |
| 478 | } |
| 479 | } |
| 480 |
| 481 | webrtc::RtpExtension mutableExtension = extension; |
| 482 | extensionIds.FindAndSetIdUsed(&mutableExtension); |
| 483 | result.rtpExtensions.push_back(std::move(mutableExtension)); |
| 484 | } |
| 485 |
| 486 | if (!isAnswer) { |
| 487 | for (const auto &extension : localContent.rtpExtensions) { |
| 488 | webrtc::RtpExtension found; |
| 489 | if (!FindByUri(result.rtpExtensions, extension, &found)) { |
| 490 | webrtc::RtpExtension mutableExtension = extension; |
| 491 | extensionIds.FindAndSetIdUsed(&mutableExtension); |
| 492 | result.rtpExtensions.push_back(std::move(mutableExtension)); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 |
| 497 | return result; |
| 498 | } |
| 499 |
| 500 | class OutgoingAudioChannel : public sigslot::has_slots<> { |
| 501 | public: |
| 502 | static absl::optional<signaling::MediaContent> createOutgoingContentDescription() { |
| 503 | signaling::MediaContent mediaContent; |
| 504 |
| 505 | auto generator = std::mt19937(std::random_device()()); |
| 506 | auto distribution = std::uniform_int_distribution<uint32_t>(); |
| 507 | do { |
| 508 | mediaContent.ssrc = distribution(generator) & 0x7fffffffU; |
| 509 | } while (!mediaContent.ssrc); |
| 510 |
| 511 | mediaContent.rtpExtensions.emplace_back(webrtc::RtpExtension::kAudioLevelUri, 1); |
| 512 | mediaContent.rtpExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, 2); |
| 513 | mediaContent.rtpExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 3); |
| 514 |
| 515 | cricket::AudioCodec opusCodec(109, "opus", 48000, 0, 2); |
| 516 | opusCodec.AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc)); |
| 517 | opusCodec.SetParam(cricket::kCodecParamUseInbandFec, 1); |
| 518 | opusCodec.SetParam(cricket::kCodecParamMinPTime, 60); |
| 519 |
| 520 | mediaContent.payloadTypes = getPayloadTypesFromAudioCodecs({ opusCodec }); |
| 521 |
| 522 | return mediaContent; |
| 523 | } |
| 524 |
| 525 | public: |
| 526 | OutgoingAudioChannel( |
| 527 | webrtc::Call *call, |
| 528 | cricket::ChannelManager *channelManager, |
| 529 | rtc::UniqueRandomIdGenerator *uniqueRandomIdGenerator, |
| 530 | webrtc::LocalAudioSinkAdapter *audioSource, |
| 531 | webrtc::RtpTransport *rtpTransport, |
| 532 | NegotiatedMediaContent<cricket::AudioCodec> const &mediaContent, |
| 533 | std::shared_ptr<Threads> threads |
| 534 | ) : |
| 535 | _threads(threads), |
| 536 | _ssrc(mediaContent.ssrc), |
| 537 | _call(call), |
| 538 | _channelManager(channelManager), |
| 539 | _audioSource(audioSource) { |
| 540 | cricket::AudioOptions audioOptions; |
| 541 | bool _disableOutgoingAudioProcessing = false; |
| 542 |
| 543 | if (_disableOutgoingAudioProcessing) { |
| 544 | audioOptions.echo_cancellation = false; |
| 545 | audioOptions.noise_suppression = false; |
| 546 | audioOptions.auto_gain_control = false; |
| 547 | audioOptions.highpass_filter = false; |
| 548 | audioOptions.typing_detection = false; |
| 549 | audioOptions.experimental_agc = false; |
| 550 | audioOptions.experimental_ns = false; |
| 551 | audioOptions.residual_echo_detector = false; |
| 552 | } else { |
| 553 | audioOptions.echo_cancellation = true; |
| 554 | audioOptions.noise_suppression = true; |
| 555 | } |
| 556 |
| 557 | std::vector<std::string> streamIds; |
| 558 | streamIds.push_back("1"); |
| 559 |
| 560 | _outgoingAudioChannel = _channelManager->CreateVoiceChannel(call, cricket::MediaConfig(), rtpTransport, threads->getMediaThread(), "audio0", false, NativeNetworkingImpl::getDefaulCryptoOptions(), uniqueRandomIdGenerator, audioOptions); |
| 561 |
| 562 | std::vector<cricket::AudioCodec> codecs; |
| 563 | for (const auto &codec : mediaContent.codecs) { |
| 564 | if (codec.name == "opus") { |
| 565 | auto mutableCodec = codec; |
| 566 |
| 567 | const uint8_t opusMinBitrateKbps = 16; |
| 568 | const uint8_t opusMaxBitrateKbps = 32; |
| 569 | const uint8_t opusStartBitrateKbps = 32; |
| 570 | const uint8_t opusPTimeMs = 60; |
| 571 |
| 572 | mutableCodec.SetParam(cricket::kCodecParamMinBitrate, opusMinBitrateKbps); |
| 573 | mutableCodec.SetParam(cricket::kCodecParamStartBitrate, opusStartBitrateKbps); |
| 574 | mutableCodec.SetParam(cricket::kCodecParamMaxBitrate, opusMaxBitrateKbps); |
| 575 | mutableCodec.SetParam(cricket::kCodecParamUseInbandFec, 1); |
| 576 | mutableCodec.SetParam(cricket::kCodecParamPTime, opusPTimeMs); |
| 577 |
| 578 | codecs.push_back(std::move(mutableCodec)); |
| 579 | } |
| 580 | } |
| 581 |
| 582 | auto outgoingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 583 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 584 | outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 585 | } |
| 586 | outgoingAudioDescription->set_rtcp_mux(true); |
| 587 | outgoingAudioDescription->set_rtcp_reduced_size(true); |
| 588 | outgoingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 589 | outgoingAudioDescription->set_codecs(codecs); |
| 590 | outgoingAudioDescription->set_bandwidth(-1); |
| 591 | outgoingAudioDescription->AddStream(cricket::StreamParams::CreateLegacy(_ssrc)); |
| 592 |
| 593 | auto incomingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 594 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 595 | incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 596 | } |
| 597 | incomingAudioDescription->set_rtcp_mux(true); |
| 598 | incomingAudioDescription->set_rtcp_reduced_size(true); |
| 599 | incomingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 600 | incomingAudioDescription->set_codecs(codecs); |
| 601 | incomingAudioDescription->set_bandwidth(-1); |
| 602 |
| 603 | _threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 604 | _outgoingAudioChannel->SetPayloadTypeDemuxingEnabled(false); |
| 605 | _outgoingAudioChannel->SetLocalContent(outgoingAudioDescription.get(), webrtc::SdpType::kOffer, nullptr); |
| 606 | _outgoingAudioChannel->SetRemoteContent(incomingAudioDescription.get(), webrtc::SdpType::kAnswer, nullptr); |
| 607 | }); |
| 608 |
| 609 | //_outgoingAudioChannel->SignalSentPacket().connect(this, &OutgoingAudioChannel::OnSentPacket_w); |
| 610 | //_outgoingAudioChannel->UpdateRtpTransport(nullptr); |
| 611 |
| 612 | setIsMuted(false); |
| 613 | } |
| 614 |
| 615 | ~OutgoingAudioChannel() { |
| 616 | //_outgoingAudioChannel->SignalSentPacket().disconnect(this); |
| 617 | _outgoingAudioChannel->Enable(false); |
| 618 | _channelManager->DestroyVoiceChannel(_outgoingAudioChannel); |
| 619 | _outgoingAudioChannel = nullptr; |
| 620 | } |
| 621 |
| 622 | void setIsMuted(bool isMuted) { |
| 623 | if (_isMuted != isMuted) { |
| 624 | _isMuted = isMuted; |
| 625 |
| 626 | _outgoingAudioChannel->Enable(!_isMuted); |
| 627 | _threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 628 | _outgoingAudioChannel->media_channel()->SetAudioSend(_ssrc, !_isMuted, nullptr, _audioSource); |
| 629 | }); |
| 630 | } |
| 631 | } |
| 632 |
| 633 | private: |
| 634 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 635 | _call->OnSentPacket(sent_packet); |
| 636 | } |
| 637 |
| 638 | private: |
| 639 | std::shared_ptr<Threads> _threads; |
| 640 | uint32_t _ssrc = 0; |
| 641 | webrtc::Call *_call = nullptr; |
| 642 | cricket::ChannelManager *_channelManager = nullptr; |
| 643 | webrtc::LocalAudioSinkAdapter *_audioSource = nullptr; |
| 644 | cricket::VoiceChannel *_outgoingAudioChannel = nullptr; |
| 645 |
| 646 | bool _isMuted = true; |
| 647 | }; |
| 648 |
| 649 | class IncomingV2AudioChannel : public sigslot::has_slots<> { |
| 650 | public: |
| 651 | IncomingV2AudioChannel( |
| 652 | cricket::ChannelManager *channelManager, |
| 653 | webrtc::Call *call, |
| 654 | webrtc::RtpTransport *rtpTransport, |
| 655 | rtc::UniqueRandomIdGenerator *randomIdGenerator, |
| 656 | NegotiatedMediaContent<cricket::AudioCodec> const &mediaContent, |
| 657 | std::shared_ptr<Threads> threads) : |
| 658 | _ssrc(mediaContent.ssrc), |
| 659 | _channelManager(channelManager), |
| 660 | _call(call) { |
| 661 | _creationTimestamp = rtc::TimeMillis(); |
| 662 |
| 663 | cricket::AudioOptions audioOptions; |
| 664 | audioOptions.audio_jitter_buffer_fast_accelerate = true; |
| 665 | audioOptions.audio_jitter_buffer_min_delay_ms = 50; |
| 666 |
| 667 | std::string streamId = std::string("stream1"); |
| 668 |
| 669 | _audioChannel = _channelManager->CreateVoiceChannel(call, cricket::MediaConfig(), rtpTransport, threads->getMediaThread(), "0", false, NativeNetworkingImpl::getDefaulCryptoOptions(), randomIdGenerator, audioOptions); |
| 670 |
| 671 | auto audioCodecs = mediaContent.codecs; |
| 672 |
| 673 | auto outgoingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 674 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 675 | outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 676 | } |
| 677 | outgoingAudioDescription->set_rtcp_mux(true); |
| 678 | outgoingAudioDescription->set_rtcp_reduced_size(true); |
| 679 | outgoingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 680 | outgoingAudioDescription->set_codecs(audioCodecs); |
| 681 | outgoingAudioDescription->set_bandwidth(-1); |
| 682 |
| 683 | auto incomingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 684 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 685 | incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 686 | } |
| 687 | incomingAudioDescription->set_rtcp_mux(true); |
| 688 | incomingAudioDescription->set_rtcp_reduced_size(true); |
| 689 | incomingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 690 | incomingAudioDescription->set_codecs(audioCodecs); |
| 691 | incomingAudioDescription->set_bandwidth(-1); |
| 692 | cricket::StreamParams streamParams = cricket::StreamParams::CreateLegacy(mediaContent.ssrc); |
| 693 | streamParams.set_stream_ids({ streamId }); |
| 694 | incomingAudioDescription->AddStream(streamParams); |
| 695 |
| 696 | threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 697 | _audioChannel->SetPayloadTypeDemuxingEnabled(false); |
| 698 | _audioChannel->SetLocalContent(outgoingAudioDescription.get(), webrtc::SdpType::kOffer, nullptr); |
| 699 | _audioChannel->SetRemoteContent(incomingAudioDescription.get(), webrtc::SdpType::kAnswer, nullptr); |
| 700 | }); |
| 701 |
| 702 | outgoingAudioDescription.reset(); |
| 703 | incomingAudioDescription.reset(); |
| 704 |
| 705 | //std::unique_ptr<AudioSinkImpl> audioLevelSink(new AudioSinkImpl(onAudioLevelUpdated, _ssrc, std::move(onAudioFrame))); |
| 706 | //_audioChannel->media_channel()->SetRawAudioSink(ssrc.networkSsrc, std::move(audioLevelSink)); |
| 707 |
| 708 | _audioChannel->Enable(true); |
| 709 | } |
| 710 |
| 711 | ~IncomingV2AudioChannel() { |
| 712 | _audioChannel->Enable(false); |
| 713 | _channelManager->DestroyVoiceChannel(_audioChannel); |
| 714 | _audioChannel = nullptr; |
| 715 | } |
| 716 |
| 717 | void setVolume(double value) { |
| 718 | _audioChannel->media_channel()->SetOutputVolume(_ssrc, value); |
| 719 | } |
| 720 |
| 721 | void updateActivity() { |
| 722 | _activityTimestamp = rtc::TimeMillis(); |
| 723 | } |
| 724 |
| 725 | int64_t getActivity() { |
| 726 | return _activityTimestamp; |
| 727 | } |
| 728 |
| 729 | private: |
| 730 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 731 | _call->OnSentPacket(sent_packet); |
| 732 | } |
| 733 |
| 734 | private: |
| 735 | uint32_t _ssrc = 0; |
| 736 | // Memory is managed by _channelManager |
| 737 | cricket::VoiceChannel *_audioChannel = nullptr; |
| 738 | // Memory is managed externally |
| 739 | cricket::ChannelManager *_channelManager = nullptr; |
| 740 | webrtc::Call *_call = nullptr; |
| 741 | int64_t _creationTimestamp = 0; |
| 742 | int64_t _activityTimestamp = 0; |
| 743 | }; |
| 744 |
| 745 | class OutgoingVideoChannel : public sigslot::has_slots<>, public std::enable_shared_from_this<OutgoingVideoChannel> { |
| 746 | public: |
| 747 | static absl::optional<signaling::MediaContent> createOutgoingContentDescription(std::vector<webrtc::SdpVideoFormat> const &availableVideoFormats, bool isScreencast) { |
| 748 | signaling::MediaContent mediaContent; |
| 749 |
| 750 | auto generator = std::mt19937(std::random_device()()); |
| 751 | auto distribution = std::uniform_int_distribution<uint32_t>(); |
| 752 | do { |
| 753 | mediaContent.ssrc = distribution(generator) & 0x7fffffffU; |
| 754 | } while (!mediaContent.ssrc); |
| 755 |
| 756 | mediaContent.rtpExtensions.emplace_back(webrtc::RtpExtension::kAbsSendTimeUri, 2); |
| 757 | mediaContent.rtpExtensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 3); |
| 758 | mediaContent.rtpExtensions.emplace_back(webrtc::RtpExtension::kVideoRotationUri, 13); |
| 759 |
| 760 | signaling::SsrcGroup fidGroup; |
| 761 | fidGroup.semantics = "FID"; |
| 762 | fidGroup.ssrcs.push_back(mediaContent.ssrc); |
| 763 | fidGroup.ssrcs.push_back(mediaContent.ssrc + 1); |
| 764 | mediaContent.ssrcGroups.push_back(std::move(fidGroup)); |
| 765 |
| 766 | auto unsortedVideoFormats = generateAvailableVideoFormats(availableVideoFormats); |
| 767 |
| 768 | std::vector<std::string> formatPreferences; |
| 769 | if (isScreencast) { |
| 770 | formatPreferences.push_back(cricket::kVp8CodecName); |
| 771 | } else { |
| 772 | #ifndef WEBRTC_DISABLE_H265 |
| 773 | formatPreferences.push_back(cricket::kH265CodecName); |
| 774 | #endif |
| 775 | formatPreferences.push_back(cricket::kH264CodecName); |
| 776 | } |
| 777 |
| 778 | std::vector<OutgoingVideoFormat> videoFormats; |
| 779 | for (const auto &name : formatPreferences) { |
| 780 | for (size_t i = 0; i < unsortedVideoFormats.size(); i++) { |
| 781 | if (absl::EqualsIgnoreCase(name, unsortedVideoFormats[i].videoCodec.name)) { |
| 782 | videoFormats.push_back(unsortedVideoFormats[i]); |
| 783 | unsortedVideoFormats.erase(unsortedVideoFormats.begin() + i); |
| 784 | break; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | for (const auto &format : unsortedVideoFormats) { |
| 789 | videoFormats.push_back(format); |
| 790 | } |
| 791 |
| 792 | for (const auto &format : videoFormats) { |
| 793 | signaling::PayloadType videoPayload; |
| 794 | videoPayload.id = format.videoCodec.id; |
| 795 | videoPayload.name = format.videoCodec.name; |
| 796 | videoPayload.clockrate = format.videoCodec.clockrate; |
| 797 | videoPayload.channels = 0; |
| 798 |
| 799 | std::vector<signaling::FeedbackType> videoFeedbackTypes; |
| 800 |
| 801 | signaling::FeedbackType fbGoogRemb; |
| 802 | fbGoogRemb.type = "goog-remb"; |
| 803 | videoFeedbackTypes.push_back(fbGoogRemb); |
| 804 |
| 805 | signaling::FeedbackType fbTransportCc; |
| 806 | fbTransportCc.type = "transport-cc"; |
| 807 | videoFeedbackTypes.push_back(fbTransportCc); |
| 808 |
| 809 | signaling::FeedbackType fbCcmFir; |
| 810 | fbCcmFir.type = "ccm"; |
| 811 | fbCcmFir.subtype = "fir"; |
| 812 | videoFeedbackTypes.push_back(fbCcmFir); |
| 813 |
| 814 | signaling::FeedbackType fbNack; |
| 815 | fbNack.type = "nack"; |
| 816 | videoFeedbackTypes.push_back(fbNack); |
| 817 |
| 818 | signaling::FeedbackType fbNackPli; |
| 819 | fbNackPli.type = "nack"; |
| 820 | fbNackPli.subtype = "pli"; |
| 821 | videoFeedbackTypes.push_back(fbNackPli); |
| 822 |
| 823 | videoPayload.feedbackTypes = videoFeedbackTypes; |
| 824 | videoPayload.parameters = {}; |
| 825 |
| 826 | mediaContent.payloadTypes.push_back(std::move(videoPayload)); |
| 827 |
| 828 | if (format.rtxCodec) { |
| 829 | signaling::PayloadType rtxPayload; |
| 830 | rtxPayload.id = format.rtxCodec->id; |
| 831 | rtxPayload.name = format.rtxCodec->name; |
| 832 | rtxPayload.clockrate = format.rtxCodec->clockrate; |
| 833 | rtxPayload.parameters.push_back(std::make_pair("apt", intToString(videoPayload.id))); |
| 834 | mediaContent.payloadTypes.push_back(std::move(rtxPayload)); |
| 835 | } |
| 836 | } |
| 837 |
| 838 | return mediaContent; |
| 839 | } |
| 840 |
| 841 | public: |
| 842 | OutgoingVideoChannel( |
| 843 | std::shared_ptr<Threads> threads, |
| 844 | cricket::ChannelManager *channelManager, |
| 845 | webrtc::Call *call, |
| 846 | webrtc::RtpTransport *rtpTransport, |
| 847 | rtc::UniqueRandomIdGenerator *randomIdGenerator, |
| 848 | webrtc::VideoBitrateAllocatorFactory *videoBitrateAllocatorFactory, |
| 849 | std::function<void()> rotationUpdated, |
| 850 | NegotiatedMediaContent<cricket::VideoCodec> const &mediaContent, |
| 851 | bool isScreencast |
| 852 | ) : |
| 853 | _threads(threads), |
| 854 | _mainSsrc(mediaContent.ssrc), |
| 855 | _call(call), |
| 856 | _channelManager(channelManager), |
| 857 | _rotationUpdated(rotationUpdated) { |
| 858 | cricket::VideoOptions videoOptions; |
| 859 | videoOptions.is_screencast = isScreencast; |
| 860 | _outgoingVideoChannel = _channelManager->CreateVideoChannel(call, cricket::MediaConfig(), rtpTransport, threads->getMediaThread(), "out" + intToString(mediaContent.ssrc), false, NativeNetworkingImpl::getDefaulCryptoOptions(), randomIdGenerator, videoOptions, videoBitrateAllocatorFactory); |
| 861 |
| 862 | auto videoCodecs = mediaContent.codecs; |
| 863 |
| 864 | auto outgoingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 865 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 866 | outgoingVideoDescription->AddRtpHeaderExtension(rtpExtension); |
| 867 | } |
| 868 |
| 869 | outgoingVideoDescription->set_rtcp_mux(true); |
| 870 | outgoingVideoDescription->set_rtcp_reduced_size(true); |
| 871 | outgoingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 872 | outgoingVideoDescription->set_codecs(videoCodecs); |
| 873 | outgoingVideoDescription->set_bandwidth(-1); |
| 874 |
| 875 | cricket::StreamParams videoSendStreamParams; |
| 876 |
| 877 | for (const auto &ssrcGroup : mediaContent.ssrcGroups) { |
| 878 | for (auto ssrc : ssrcGroup.ssrcs) { |
| 879 | videoSendStreamParams.ssrcs.push_back(ssrc); |
| 880 | } |
| 881 |
| 882 | cricket::SsrcGroup mappedGroup(ssrcGroup.semantics, ssrcGroup.ssrcs); |
| 883 | videoSendStreamParams.ssrc_groups.push_back(std::move(mappedGroup)); |
| 884 | } |
| 885 |
| 886 | videoSendStreamParams.cname = "cname"; |
| 887 |
| 888 | outgoingVideoDescription->AddStream(videoSendStreamParams); |
| 889 |
| 890 | auto incomingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 891 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 892 | incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 893 | } |
| 894 | incomingVideoDescription->set_rtcp_mux(true); |
| 895 | incomingVideoDescription->set_rtcp_reduced_size(true); |
| 896 | incomingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 897 | incomingVideoDescription->set_codecs(videoCodecs); |
| 898 | incomingVideoDescription->set_bandwidth(-1); |
| 899 |
| 900 | threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 901 | _outgoingVideoChannel->SetPayloadTypeDemuxingEnabled(false); |
| 902 | _outgoingVideoChannel->SetLocalContent(outgoingVideoDescription.get(), webrtc::SdpType::kOffer, nullptr); |
| 903 | _outgoingVideoChannel->SetRemoteContent(incomingVideoDescription.get(), webrtc::SdpType::kAnswer, nullptr); |
| 904 |
| 905 | webrtc::RtpParameters rtpParameters = _outgoingVideoChannel->media_channel()->GetRtpSendParameters(mediaContent.ssrc); |
| 906 |
| 907 | _outgoingVideoChannel->media_channel()->SetRtpSendParameters(mediaContent.ssrc, rtpParameters); |
| 908 | }); |
| 909 |
| 910 | _outgoingVideoChannel->Enable(false); |
| 911 |
| 912 | threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 913 | _outgoingVideoChannel->media_channel()->SetVideoSend(mediaContent.ssrc, NULL, nullptr); |
| 914 | }); |
| 915 | } |
| 916 |
| 917 | ~OutgoingVideoChannel() { |
| 918 | _outgoingVideoChannel->Enable(false); |
| 919 | _channelManager->DestroyVideoChannel(_outgoingVideoChannel); |
| 920 | _outgoingVideoChannel = nullptr; |
| 921 | } |
| 922 |
| 923 | void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 924 | _videoCapture = videoCapture; |
| 925 |
| 926 | if (_videoCapture) { |
| 927 | _outgoingVideoChannel->Enable(true); |
| 928 | auto videoCaptureImpl = GetVideoCaptureAssumingSameThread(_videoCapture.get()); |
| 929 |
| 930 | _threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 931 | _outgoingVideoChannel->media_channel()->SetVideoSend(_mainSsrc, NULL, videoCaptureImpl->source()); |
| 932 | }); |
| 933 |
| 934 | const auto weak = std::weak_ptr<OutgoingVideoChannel>(shared_from_this()); |
| 935 | videoCaptureImpl->setRotationUpdated([threads = _threads, weak](int angle) { |
| 936 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 937 | const auto strong = weak.lock(); |
| 938 | if (!strong) { |
| 939 | return; |
| 940 | } |
| 941 | signaling::MediaStateMessage::VideoRotation videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 942 | switch (angle) { |
| 943 | case 0: { |
| 944 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 945 | break; |
| 946 | } |
| 947 | case 90: { |
| 948 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation90; |
| 949 | break; |
| 950 | } |
| 951 | case 180: { |
| 952 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation180; |
| 953 | break; |
| 954 | } |
| 955 | case 270: { |
| 956 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation270; |
| 957 | break; |
| 958 | } |
| 959 | default: { |
| 960 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | if (strong->_videoRotation != videoRotation) { |
| 965 | strong->_videoRotation = videoRotation; |
| 966 | strong->_rotationUpdated(); |
| 967 | } |
| 968 | }); |
| 969 | }); |
| 970 |
| 971 | switch (videoCaptureImpl->getRotation()) { |
| 972 | case 0: { |
| 973 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 974 | break; |
| 975 | } |
| 976 | case 90: { |
| 977 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation90; |
| 978 | break; |
| 979 | } |
| 980 | case 180: { |
| 981 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation180; |
| 982 | break; |
| 983 | } |
| 984 | case 270: { |
| 985 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation270; |
| 986 | break; |
| 987 | } |
| 988 | default: { |
| 989 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 990 | break; |
| 991 | } |
| 992 | } |
| 993 | } else { |
| 994 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 995 | _outgoingVideoChannel->Enable(false); |
| 996 |
| 997 | _threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 998 | _outgoingVideoChannel->media_channel()->SetVideoSend(_mainSsrc, NULL, nullptr); |
| 999 | }); |
| 1000 | } |
| 1001 | } |
| 1002 |
| 1003 | public: |
| 1004 | std::shared_ptr<VideoCaptureInterface> videoCapture() { |
| 1005 | return _videoCapture; |
| 1006 | } |
| 1007 |
| 1008 | signaling::MediaStateMessage::VideoRotation getRotation() { |
| 1009 | return _videoRotation; |
| 1010 | } |
| 1011 |
| 1012 | private: |
| 1013 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 1014 | _call->OnSentPacket(sent_packet); |
| 1015 | } |
| 1016 |
| 1017 | private: |
| 1018 | std::shared_ptr<Threads> _threads; |
| 1019 |
| 1020 | uint32_t _mainSsrc = 0; |
| 1021 | webrtc::Call *_call = nullptr; |
| 1022 | cricket::ChannelManager *_channelManager = nullptr; |
| 1023 | cricket::VideoChannel *_outgoingVideoChannel = nullptr; |
| 1024 |
| 1025 | std::function<void()> _rotationUpdated; |
| 1026 |
| 1027 | std::shared_ptr<VideoCaptureInterface> _videoCapture; |
| 1028 | signaling::MediaStateMessage::VideoRotation _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 1029 | }; |
| 1030 |
| 1031 | class VideoSinkImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 1032 | public: |
| 1033 | VideoSinkImpl() { |
| 1034 | } |
| 1035 |
| 1036 | virtual ~VideoSinkImpl() { |
| 1037 | } |
| 1038 |
| 1039 | virtual void OnFrame(const webrtc::VideoFrame& frame) override { |
| 1040 | //_lastFrame = frame; |
| 1041 | for (int i = (int)(_sinks.size()) - 1; i >= 0; i--) { |
| 1042 | auto strong = _sinks[i].lock(); |
| 1043 | if (!strong) { |
| 1044 | _sinks.erase(_sinks.begin() + i); |
| 1045 | } else { |
| 1046 | strong->OnFrame(frame); |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 |
| 1051 | virtual void OnDiscardedFrame() override { |
| 1052 | for (int i = (int)(_sinks.size()) - 1; i >= 0; i--) { |
| 1053 | auto strong = _sinks[i].lock(); |
| 1054 | if (!strong) { |
| 1055 | _sinks.erase(_sinks.begin() + i); |
| 1056 | } else { |
| 1057 | strong->OnDiscardedFrame(); |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 |
| 1062 | void addSink(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) { |
| 1063 | _sinks.push_back(impl); |
| 1064 | if (_lastFrame) { |
| 1065 | auto strong = impl.lock(); |
| 1066 | if (strong) { |
| 1067 | strong->OnFrame(_lastFrame.value()); |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 |
| 1072 | private: |
| 1073 | std::vector<std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>> _sinks; |
| 1074 | absl::optional<webrtc::VideoFrame> _lastFrame; |
| 1075 | }; |
| 1076 |
| 1077 | class IncomingV2VideoChannel : public sigslot::has_slots<> { |
| 1078 | public: |
| 1079 | IncomingV2VideoChannel( |
| 1080 | cricket::ChannelManager *channelManager, |
| 1081 | webrtc::Call *call, |
| 1082 | webrtc::RtpTransport *rtpTransport, |
| 1083 | rtc::UniqueRandomIdGenerator *randomIdGenerator, |
| 1084 | NegotiatedMediaContent<cricket::VideoCodec> const &mediaContent, |
| 1085 | std::string const &streamId, |
| 1086 | std::shared_ptr<Threads> threads) : |
| 1087 | _channelManager(channelManager), |
| 1088 | _call(call) { |
| 1089 | _videoSink.reset(new VideoSinkImpl()); |
| 1090 |
| 1091 | _videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1092 |
| 1093 | _videoChannel = _channelManager->CreateVideoChannel(call, cricket::MediaConfig(), rtpTransport, threads->getMediaThread(), streamId, false, NativeNetworkingImpl::getDefaulCryptoOptions(), randomIdGenerator, cricket::VideoOptions(), _videoBitrateAllocatorFactory.get()); |
| 1094 |
| 1095 | std::vector<cricket::VideoCodec> videoCodecs = mediaContent.codecs; |
| 1096 |
| 1097 | auto outgoingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 1098 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 1099 | outgoingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 1100 | } |
| 1101 | outgoingVideoDescription->set_rtcp_mux(true); |
| 1102 | outgoingVideoDescription->set_rtcp_reduced_size(true); |
| 1103 | outgoingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 1104 | outgoingVideoDescription->set_codecs(videoCodecs); |
| 1105 | outgoingVideoDescription->set_bandwidth(-1); |
| 1106 |
| 1107 | cricket::StreamParams videoRecvStreamParams; |
| 1108 |
| 1109 | _mainVideoSsrc = mediaContent.ssrc; |
| 1110 |
| 1111 | std::vector<uint32_t> allSsrcs; |
| 1112 | for (const auto &group : mediaContent.ssrcGroups) { |
| 1113 | for (auto ssrc : group.ssrcs) { |
| 1114 | if (std::find(allSsrcs.begin(), allSsrcs.end(), ssrc) == allSsrcs.end()) { |
| 1115 | allSsrcs.push_back(ssrc); |
| 1116 | } |
| 1117 | } |
| 1118 |
| 1119 | cricket::SsrcGroup parsedGroup(group.semantics, group.ssrcs); |
| 1120 | videoRecvStreamParams.ssrc_groups.push_back(parsedGroup); |
| 1121 | } |
| 1122 | videoRecvStreamParams.ssrcs = allSsrcs; |
| 1123 |
| 1124 | videoRecvStreamParams.cname = "cname"; |
| 1125 | videoRecvStreamParams.set_stream_ids({ streamId }); |
| 1126 |
| 1127 | auto incomingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 1128 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 1129 | incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 1130 | } |
| 1131 | incomingVideoDescription->set_rtcp_mux(true); |
| 1132 | incomingVideoDescription->set_rtcp_reduced_size(true); |
| 1133 | incomingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 1134 | incomingVideoDescription->set_codecs(videoCodecs); |
| 1135 | incomingVideoDescription->set_bandwidth(-1); |
| 1136 |
| 1137 | incomingVideoDescription->AddStream(videoRecvStreamParams); |
| 1138 |
| 1139 | threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 1140 | _videoChannel->SetPayloadTypeDemuxingEnabled(false); |
| 1141 | _videoChannel->SetLocalContent(outgoingVideoDescription.get(), webrtc::SdpType::kOffer, nullptr); |
| 1142 | _videoChannel->SetRemoteContent(incomingVideoDescription.get(), webrtc::SdpType::kAnswer, nullptr); |
| 1143 |
| 1144 | _videoChannel->media_channel()->SetSink(_mainVideoSsrc, _videoSink.get()); |
| 1145 | }); |
| 1146 |
| 1147 | _videoChannel->Enable(true); |
| 1148 | } |
| 1149 |
| 1150 | ~IncomingV2VideoChannel() { |
| 1151 | _videoChannel->Enable(false); |
| 1152 | _channelManager->DestroyVideoChannel(_videoChannel); |
| 1153 | _videoChannel = nullptr; |
| 1154 | } |
| 1155 |
| 1156 | void addSink(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) { |
| 1157 | _videoSink->addSink(impl); |
| 1158 | } |
| 1159 |
| 1160 | private: |
| 1161 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 1162 | _call->OnSentPacket(sent_packet); |
| 1163 | } |
| 1164 |
| 1165 | private: |
| 1166 | uint32_t _mainVideoSsrc = 0; |
| 1167 | std::unique_ptr<VideoSinkImpl> _videoSink; |
| 1168 | std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory; |
| 1169 | // Memory is managed by _channelManager |
| 1170 | cricket::VideoChannel *_videoChannel; |
| 1171 | // Memory is managed externally |
| 1172 | cricket::ChannelManager *_channelManager = nullptr; |
| 1173 | webrtc::Call *_call = nullptr; |
| 1174 | }; |
| 1175 |
| 1176 | } // namespace |
| 1177 |
| 1178 | class InstanceV2ImplInternal : public std::enable_shared_from_this<InstanceV2ImplInternal> { |
| 1179 | public: |
| 1180 | InstanceV2ImplInternal(Descriptor &&descriptor, std::shared_ptr<Threads> threads) : |
| 1181 | _threads(threads), |
| 1182 | _rtcServers(descriptor.rtcServers), |
| 1183 | _encryptionKey(std::move(descriptor.encryptionKey)), |
| 1184 | _stateUpdated(descriptor.stateUpdated), |
| 1185 | _signalBarsUpdated(descriptor.signalBarsUpdated), |
| 1186 | _audioLevelUpdated(descriptor.audioLevelUpdated), |
| 1187 | _remoteBatteryLevelIsLowUpdated(descriptor.remoteBatteryLevelIsLowUpdated), |
| 1188 | _remoteMediaStateUpdated(descriptor.remoteMediaStateUpdated), |
| 1189 | _remotePrefferedAspectRatioUpdated(descriptor.remotePrefferedAspectRatioUpdated), |
| 1190 | _signalingDataEmitted(descriptor.signalingDataEmitted), |
| 1191 | _createAudioDeviceModule(descriptor.createAudioDeviceModule), |
| 1192 | _eventLog(std::make_unique<webrtc::RtcEventLogNull>()), |
| 1193 | _taskQueueFactory(webrtc::CreateDefaultTaskQueueFactory()), |
| 1194 | _videoCapture(descriptor.videoCapture) { |
| 1195 | } |
| 1196 |
| 1197 | ~InstanceV2ImplInternal() { |
| 1198 | _networking->perform(RTC_FROM_HERE, [](NativeNetworkingImpl *networking) { |
| 1199 | networking->stop(); |
| 1200 | }); |
| 1201 |
| 1202 | _incomingAudioChannel.reset(); |
| 1203 | _incomingVideoChannel.reset(); |
| 1204 | _incomingScreencastChannel.reset(); |
| 1205 | _outgoingAudioChannel.reset(); |
| 1206 | _outgoingVideoChannel.reset(); |
| 1207 | _outgoingScreencastChannel.reset(); |
| 1208 | _currentSink.reset(); |
| 1209 |
| 1210 | _threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 1211 | _channelManager.reset(); |
| 1212 | _call.reset(); |
| 1213 | _audioDeviceModule = nullptr; |
| 1214 | }); |
| 1215 | _threads->getNetworkThread()->Invoke<void>(RTC_FROM_HERE, []() { |
| 1216 | }); |
| 1217 | } |
| 1218 |
| 1219 | void start() { |
| 1220 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1221 |
| 1222 | _networking.reset(new ThreadLocalObject<NativeNetworkingImpl>(_threads->getNetworkThread(), [weak, threads = _threads, isOutgoing = _encryptionKey.isOutgoing, rtcServers = _rtcServers]() { |
| 1223 | return new NativeNetworkingImpl(NativeNetworkingImpl::Configuration{ |
| 1224 | .isOutgoing = isOutgoing, |
| 1225 | .enableStunMarking = false, |
| 1226 | .enableTCP = false, |
| 1227 | .enableP2P = true, |
| 1228 | .rtcServers = rtcServers, |
| 1229 | .stateUpdated = [threads, weak](const NativeNetworkingImpl::State &state) { |
| 1230 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1231 | const auto strong = weak.lock(); |
| 1232 | if (!strong) { |
| 1233 | return; |
| 1234 | } |
| 1235 | strong->onNetworkStateUpdated(state); |
| 1236 | }); |
| 1237 | }, |
| 1238 | .candidateGathered = [threads, weak](const cricket::Candidate &candidate) { |
| 1239 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1240 | const auto strong = weak.lock(); |
| 1241 | if (!strong) { |
| 1242 | return; |
| 1243 | } |
| 1244 |
| 1245 | strong->sendCandidate(candidate); |
| 1246 | }); |
| 1247 | }, |
| 1248 | .transportMessageReceived = [threads, weak](rtc::CopyOnWriteBuffer const &packet, bool isMissing) { |
| 1249 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1250 | const auto strong = weak.lock(); |
| 1251 | if (!strong) { |
| 1252 | return; |
| 1253 | } |
| 1254 | }); |
| 1255 | }, |
| 1256 | .rtcpPacketReceived = [threads, weak](rtc::CopyOnWriteBuffer const &packet, int64_t timestamp) { |
| 1257 | const auto strong = weak.lock(); |
| 1258 | if (!strong) { |
| 1259 | return; |
| 1260 | } |
| 1261 | strong->_call->Receiver()->DeliverPacket(webrtc::MediaType::ANY, packet, timestamp); |
| 1262 | }, |
| 1263 | .dataChannelStateUpdated = [threads, weak](bool isDataChannelOpen) { |
| 1264 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1265 | const auto strong = weak.lock(); |
| 1266 | if (!strong) { |
| 1267 | return; |
| 1268 | } |
| 1269 | strong->onDataChannelStateUpdated(isDataChannelOpen); |
| 1270 | }); |
| 1271 | }, |
| 1272 | .dataChannelMessageReceived = [threads, weak](std::string const &message) { |
| 1273 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1274 | const auto strong = weak.lock(); |
| 1275 | if (!strong) { |
| 1276 | return; |
| 1277 | } |
| 1278 | strong->onDataChannelMessage(message); |
| 1279 | }); |
| 1280 | }, |
| 1281 | .threads = threads |
| 1282 | }); |
| 1283 | })); |
| 1284 |
| 1285 | PlatformInterface::SharedInstance()->configurePlatformAudio(); |
| 1286 |
| 1287 | //setAudioInputDevice(_initialInputDeviceId); |
| 1288 | //setAudioOutputDevice(_initialOutputDeviceId); |
| 1289 |
| 1290 | _threads->getWorkerThread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 1291 | cricket::MediaEngineDependencies mediaDeps; |
| 1292 | mediaDeps.task_queue_factory = _taskQueueFactory.get(); |
| 1293 | mediaDeps.audio_encoder_factory = webrtc::CreateAudioEncoderFactory<webrtc::AudioEncoderOpus, webrtc::AudioEncoderL16>(); |
| 1294 | mediaDeps.audio_decoder_factory = webrtc::CreateAudioDecoderFactory<webrtc::AudioDecoderOpus, webrtc::AudioDecoderL16>(); |
| 1295 |
| 1296 | mediaDeps.video_encoder_factory = PlatformInterface::SharedInstance()->makeVideoEncoderFactory(true); |
| 1297 | mediaDeps.video_decoder_factory = PlatformInterface::SharedInstance()->makeVideoDecoderFactory(); |
| 1298 |
| 1299 | _audioDeviceModule = createAudioDeviceModule(); |
| 1300 | /*if (!_audioDeviceModule) { |
| 1301 | return; |
| 1302 | }*/ |
| 1303 | mediaDeps.adm = _audioDeviceModule; |
| 1304 |
| 1305 | _availableVideoFormats = mediaDeps.video_encoder_factory->GetSupportedFormats(); |
| 1306 |
| 1307 | std::unique_ptr<cricket::MediaEngineInterface> mediaEngine = cricket::CreateMediaEngine(std::move(mediaDeps)); |
| 1308 |
| 1309 | _channelManager = cricket::ChannelManager::Create( |
| 1310 | std::move(mediaEngine), |
| 1311 | true, |
| 1312 | _threads->getWorkerThread(), |
| 1313 | _threads->getNetworkThread() |
| 1314 | ); |
| 1315 |
| 1316 | webrtc::Call::Config callConfig(_eventLog.get()); |
| 1317 | callConfig.task_queue_factory = _taskQueueFactory.get(); |
| 1318 | callConfig.trials = &_fieldTrials; |
| 1319 | callConfig.audio_state = _channelManager->media_engine()->voice().GetAudioState(); |
| 1320 |
| 1321 | _call.reset(webrtc::Call::Create(callConfig, webrtc::Clock::GetRealTimeClock(), _threads->getSharedModuleThread(), webrtc::ProcessThread::Create("PacerThread"))); |
| 1322 | }); |
| 1323 |
| 1324 | _uniqueRandomIdGenerator.reset(new rtc::UniqueRandomIdGenerator()); |
| 1325 |
| 1326 | _threads->getNetworkThread()->Invoke<void>(RTC_FROM_HERE, [this]() { |
| 1327 | _rtpTransport = _networking->getSyncAssumingSameThread()->getRtpTransport(); |
| 1328 | }); |
| 1329 |
| 1330 | _videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1331 |
| 1332 | _networking->perform(RTC_FROM_HERE, [](NativeNetworkingImpl *networking) { |
| 1333 | networking->start(); |
| 1334 | }); |
| 1335 |
| 1336 | if (_videoCapture) { |
| 1337 | setVideoCapture(_videoCapture); |
| 1338 | } |
| 1339 |
| 1340 | beginSignaling(); |
| 1341 |
| 1342 | adjustBitratePreferences(true); |
| 1343 | } |
| 1344 |
| 1345 | void sendSignalingMessage(signaling::Message const &message) { |
| 1346 | auto data = message.serialize(); |
| 1347 |
| 1348 | RTC_LOG(LS_INFO) << "sendSignalingMessage: " << std::string(data.begin(), data.end()); |
| 1349 |
| 1350 | if (_signalingEncryption) { |
| 1351 | if (const auto encryptedData = _signalingEncryption->encryptOutgoing(data)) { |
| 1352 | _signalingDataEmitted(std::vector<uint8_t>(encryptedData->data(), encryptedData->data() + encryptedData->size())); |
| 1353 | } else { |
| 1354 | RTC_LOG(LS_ERROR) << "sendSignalingMessage: failed to encrypt payload"; |
| 1355 | } |
| 1356 | } else { |
| 1357 | _signalingDataEmitted(data); |
| 1358 | } |
| 1359 | } |
| 1360 |
| 1361 | void beginSignaling() { |
| 1362 | _signalingEncryption.reset(new SignalingEncryption(_encryptionKey)); |
| 1363 |
| 1364 | if (_encryptionKey.isOutgoing) { |
| 1365 | _outgoingAudioContent = OutgoingAudioChannel::createOutgoingContentDescription(); |
| 1366 | _outgoingVideoContent = OutgoingVideoChannel::createOutgoingContentDescription(_availableVideoFormats, false); |
| 1367 | _outgoingScreencastContent = OutgoingVideoChannel::createOutgoingContentDescription(_availableVideoFormats, true); |
| 1368 |
| 1369 | sendInitialSetup(); |
| 1370 | } |
| 1371 | } |
| 1372 |
| 1373 | void createNegotiatedChannels() { |
| 1374 | if (_negotiatedOutgoingVideoContent) { |
| 1375 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1376 |
| 1377 | _outgoingVideoChannel.reset(new OutgoingVideoChannel( |
| 1378 | _threads, |
| 1379 | _channelManager.get(), |
| 1380 | _call.get(), |
| 1381 | _rtpTransport, |
| 1382 | _uniqueRandomIdGenerator.get(), |
| 1383 | _videoBitrateAllocatorFactory.get(), |
| 1384 | [threads = _threads, weak]() { |
| 1385 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1386 | const auto strong = weak.lock(); |
| 1387 | if (!strong) { |
| 1388 | return; |
| 1389 | } |
| 1390 | strong->sendMediaState(); |
| 1391 | }); |
| 1392 | }, |
| 1393 | _negotiatedOutgoingVideoContent.value(), |
| 1394 | false |
| 1395 | )); |
| 1396 |
| 1397 | if (_videoCapture) { |
| 1398 | _outgoingVideoChannel->setVideoCapture(_videoCapture); |
| 1399 | } |
| 1400 | } |
| 1401 |
| 1402 | if (_negotiatedOutgoingScreencastContent) { |
| 1403 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1404 |
| 1405 | _outgoingScreencastChannel.reset(new OutgoingVideoChannel( |
| 1406 | _threads, |
| 1407 | _channelManager.get(), |
| 1408 | _call.get(), |
| 1409 | _rtpTransport, |
| 1410 | _uniqueRandomIdGenerator.get(), |
| 1411 | _videoBitrateAllocatorFactory.get(), |
| 1412 | [threads = _threads, weak]() { |
| 1413 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [=] { |
| 1414 | const auto strong = weak.lock(); |
| 1415 | if (!strong) { |
| 1416 | return; |
| 1417 | } |
| 1418 | strong->sendMediaState(); |
| 1419 | }); |
| 1420 | }, |
| 1421 | _negotiatedOutgoingScreencastContent.value(), |
| 1422 | true |
| 1423 | )); |
| 1424 |
| 1425 | if (_screencastCapture) { |
| 1426 | _outgoingScreencastChannel->setVideoCapture(_screencastCapture); |
| 1427 | } |
| 1428 | } |
| 1429 |
| 1430 | if (_negotiatedOutgoingAudioContent) { |
| 1431 | _outgoingAudioChannel.reset(new OutgoingAudioChannel( |
| 1432 | _call.get(), |
| 1433 | _channelManager.get(), |
| 1434 | _uniqueRandomIdGenerator.get(), |
| 1435 | &_audioSource, |
| 1436 | _rtpTransport, |
| 1437 | _negotiatedOutgoingAudioContent.value(), |
| 1438 | _threads |
| 1439 | )); |
| 1440 | } |
| 1441 |
| 1442 | adjustBitratePreferences(true); |
| 1443 | } |
| 1444 |
| 1445 | void sendInitialSetup() { |
| 1446 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1447 |
| 1448 | _networking->perform(RTC_FROM_HERE, [weak, threads = _threads, isOutgoing = _encryptionKey.isOutgoing](NativeNetworkingImpl *networking) { |
| 1449 | auto localFingerprint = networking->getLocalFingerprint(); |
| 1450 | std::string hash = localFingerprint->algorithm; |
| 1451 | std::string fingerprint = localFingerprint->GetRfc4572Fingerprint(); |
| 1452 | std::string setup; |
| 1453 | if (isOutgoing) { |
| 1454 | setup = "actpass"; |
| 1455 | } else { |
| 1456 | setup = "passive"; |
| 1457 | } |
| 1458 |
| 1459 | auto localIceParams = networking->getLocalIceParameters(); |
| 1460 | std::string ufrag = localIceParams.ufrag; |
| 1461 | std::string pwd = localIceParams.pwd; |
| 1462 |
| 1463 | threads->getMediaThread()->PostTask(RTC_FROM_HERE, [weak, ufrag, pwd, hash, fingerprint, setup, localIceParams]() { |
| 1464 | const auto strong = weak.lock(); |
| 1465 | if (!strong) { |
| 1466 | return; |
| 1467 | } |
| 1468 |
| 1469 | signaling::InitialSetupMessage data; |
| 1470 |
| 1471 | if (strong->_outgoingAudioContent) { |
| 1472 | data.audio = strong->_outgoingAudioContent.value(); |
| 1473 | } |
| 1474 | if (strong->_outgoingVideoContent) { |
| 1475 | data.video = strong->_outgoingVideoContent.value(); |
| 1476 | } |
| 1477 | if (strong->_outgoingScreencastContent) { |
| 1478 | data.screencast = strong->_outgoingScreencastContent.value(); |
| 1479 | } |
| 1480 |
| 1481 | data.ufrag = ufrag; |
| 1482 | data.pwd = pwd; |
| 1483 |
| 1484 | signaling::DtlsFingerprint dtlsFingerprint; |
| 1485 | dtlsFingerprint.hash = hash; |
| 1486 | dtlsFingerprint.fingerprint = fingerprint; |
| 1487 | dtlsFingerprint.setup = setup; |
| 1488 | data.fingerprints.push_back(std::move(dtlsFingerprint)); |
| 1489 |
| 1490 | signaling::Message message; |
| 1491 | message.data = std::move(data); |
| 1492 | strong->sendSignalingMessage(message); |
| 1493 | }); |
| 1494 | }); |
| 1495 | } |
| 1496 |
| 1497 | void receiveSignalingData(const std::vector<uint8_t> &data) { |
| 1498 | std::vector<uint8_t> decryptedData; |
| 1499 |
| 1500 | if (_signalingEncryption) { |
| 1501 | const auto rawDecryptedData = _signalingEncryption->decryptIncoming(data); |
| 1502 | if (!rawDecryptedData) { |
| 1503 | RTC_LOG(LS_ERROR) << "receiveSignalingData: could not decrypt payload"; |
| 1504 |
| 1505 | return; |
| 1506 | } |
| 1507 |
| 1508 | decryptedData = std::vector<uint8_t>(rawDecryptedData->data(), rawDecryptedData->data() + rawDecryptedData->size()); |
| 1509 | } else { |
| 1510 | decryptedData = data; |
| 1511 | } |
| 1512 |
| 1513 | processSignalingData(decryptedData); |
| 1514 | } |
| 1515 |
| 1516 | void processSignalingData(const std::vector<uint8_t> &data) { |
| 1517 | RTC_LOG(LS_INFO) << "processSignalingData: " << std::string(data.begin(), data.end()); |
| 1518 |
| 1519 | const auto message = signaling::Message::parse(data); |
| 1520 | if (!message) { |
| 1521 | return; |
| 1522 | } |
| 1523 | const auto messageData = &message->data; |
| 1524 | if (const auto initialSetup = absl::get_if<signaling::InitialSetupMessage>(messageData)) { |
| 1525 | PeerIceParameters remoteIceParameters; |
| 1526 | remoteIceParameters.ufrag = initialSetup->ufrag; |
| 1527 | remoteIceParameters.pwd = initialSetup->pwd; |
| 1528 |
| 1529 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 1530 | std::string sslSetup; |
| 1531 | if (initialSetup->fingerprints.size() != 0) { |
| 1532 | fingerprint = rtc::SSLFingerprint::CreateUniqueFromRfc4572(initialSetup->fingerprints[0].hash, initialSetup->fingerprints[0].fingerprint); |
| 1533 | sslSetup = initialSetup->fingerprints[0].setup; |
| 1534 | } |
| 1535 |
| 1536 | _networking->perform(RTC_FROM_HERE, [threads = _threads, remoteIceParameters = std::move(remoteIceParameters), fingerprint = std::move(fingerprint), sslSetup = std::move(sslSetup)](NativeNetworkingImpl *networking) { |
| 1537 | networking->setRemoteParams(remoteIceParameters, fingerprint.get(), sslSetup); |
| 1538 | }); |
| 1539 |
| 1540 | if (const auto audio = initialSetup->audio) { |
| 1541 | if (_encryptionKey.isOutgoing) { |
| 1542 | if (_outgoingAudioContent) { |
| 1543 | _negotiatedOutgoingAudioContent = negotiateMediaContent<cricket::AudioCodec>(_outgoingAudioContent.value(), _outgoingAudioContent.value(), audio.value(), false); |
| 1544 | const auto incomingAudioContent = negotiateMediaContent<cricket::AudioCodec>(audio.value(), _outgoingAudioContent.value(), audio.value(), false); |
| 1545 |
| 1546 | signaling::MediaContent outgoingAudioContent; |
| 1547 |
| 1548 | outgoingAudioContent.ssrc = _outgoingAudioContent->ssrc; |
| 1549 | outgoingAudioContent.ssrcGroups = _outgoingAudioContent->ssrcGroups; |
| 1550 | outgoingAudioContent.rtpExtensions = _negotiatedOutgoingAudioContent->rtpExtensions; |
| 1551 | outgoingAudioContent.payloadTypes = getPayloadTypesFromAudioCodecs(_negotiatedOutgoingAudioContent->codecs); |
| 1552 |
| 1553 | _outgoingAudioContent = std::move(outgoingAudioContent); |
| 1554 |
| 1555 | _incomingAudioChannel.reset(new IncomingV2AudioChannel( |
| 1556 | _channelManager.get(), |
| 1557 | _call.get(), |
| 1558 | _rtpTransport, |
| 1559 | _uniqueRandomIdGenerator.get(), |
| 1560 | incomingAudioContent, |
| 1561 | _threads |
| 1562 | )); |
| 1563 | } |
| 1564 | } else { |
| 1565 | const auto generatedOutgoingContent = OutgoingAudioChannel::createOutgoingContentDescription(); |
| 1566 |
| 1567 | if (generatedOutgoingContent) { |
| 1568 | _negotiatedOutgoingAudioContent = negotiateMediaContent<cricket::AudioCodec>(generatedOutgoingContent.value(), generatedOutgoingContent.value(), audio.value(), true); |
| 1569 | const auto incomingAudioContent = negotiateMediaContent<cricket::AudioCodec>(audio.value(), generatedOutgoingContent.value(), audio.value(), true); |
| 1570 |
| 1571 | if (_negotiatedOutgoingAudioContent) { |
| 1572 | signaling::MediaContent outgoingAudioContent; |
| 1573 |
| 1574 | outgoingAudioContent.ssrc = generatedOutgoingContent->ssrc; |
| 1575 | outgoingAudioContent.ssrcGroups = generatedOutgoingContent->ssrcGroups; |
| 1576 | outgoingAudioContent.rtpExtensions = _negotiatedOutgoingAudioContent->rtpExtensions; |
| 1577 | outgoingAudioContent.payloadTypes = getPayloadTypesFromAudioCodecs(_negotiatedOutgoingAudioContent->codecs); |
| 1578 |
| 1579 | _outgoingAudioContent = std::move(outgoingAudioContent); |
| 1580 |
| 1581 | _incomingAudioChannel.reset(new IncomingV2AudioChannel( |
| 1582 | _channelManager.get(), |
| 1583 | _call.get(), |
| 1584 | _rtpTransport, |
| 1585 | _uniqueRandomIdGenerator.get(), |
| 1586 | incomingAudioContent, |
| 1587 | _threads |
| 1588 | )); |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 |
| 1594 | if (const auto video = initialSetup->video) { |
| 1595 | if (_encryptionKey.isOutgoing) { |
| 1596 | if (_outgoingVideoContent) { |
| 1597 | _negotiatedOutgoingVideoContent = negotiateMediaContent<cricket::VideoCodec>(_outgoingVideoContent.value(), _outgoingVideoContent.value(), video.value(), false); |
| 1598 | const auto incomingVideoContent = negotiateMediaContent<cricket::VideoCodec>(video.value(), _outgoingVideoContent.value(), video.value(), false); |
| 1599 |
| 1600 | signaling::MediaContent outgoingVideoContent; |
| 1601 |
| 1602 | outgoingVideoContent.ssrc = _outgoingVideoContent->ssrc; |
| 1603 | outgoingVideoContent.ssrcGroups = _outgoingVideoContent->ssrcGroups; |
| 1604 | outgoingVideoContent.rtpExtensions = _negotiatedOutgoingVideoContent->rtpExtensions; |
| 1605 | outgoingVideoContent.payloadTypes = getPayloadTypesFromVideoCodecs(_negotiatedOutgoingVideoContent->codecs); |
| 1606 |
| 1607 | _outgoingVideoContent = std::move(outgoingVideoContent); |
| 1608 |
| 1609 | _incomingVideoChannel.reset(new IncomingV2VideoChannel( |
| 1610 | _channelManager.get(), |
| 1611 | _call.get(), |
| 1612 | _rtpTransport, |
| 1613 | _uniqueRandomIdGenerator.get(), |
| 1614 | incomingVideoContent, |
| 1615 | "1", |
| 1616 | _threads |
| 1617 | )); |
| 1618 | _incomingVideoChannel->addSink(_currentSink); |
| 1619 | } |
| 1620 | } else { |
| 1621 | const auto generatedOutgoingContent = OutgoingVideoChannel::createOutgoingContentDescription(_availableVideoFormats, false); |
| 1622 |
| 1623 | if (generatedOutgoingContent) { |
| 1624 | _negotiatedOutgoingVideoContent = negotiateMediaContent<cricket::VideoCodec>(generatedOutgoingContent.value(), generatedOutgoingContent.value(), video.value(), true); |
| 1625 | const auto incomingVideoContent = negotiateMediaContent<cricket::VideoCodec>(video.value(), generatedOutgoingContent.value(), video.value(), true); |
| 1626 |
| 1627 | if (_negotiatedOutgoingVideoContent) { |
| 1628 | signaling::MediaContent outgoingVideoContent; |
| 1629 |
| 1630 | outgoingVideoContent.ssrc = generatedOutgoingContent->ssrc; |
| 1631 | outgoingVideoContent.ssrcGroups = generatedOutgoingContent->ssrcGroups; |
| 1632 | outgoingVideoContent.rtpExtensions = _negotiatedOutgoingVideoContent->rtpExtensions; |
| 1633 | outgoingVideoContent.payloadTypes = getPayloadTypesFromVideoCodecs(_negotiatedOutgoingVideoContent->codecs); |
| 1634 |
| 1635 | _outgoingVideoContent = std::move(outgoingVideoContent); |
| 1636 |
| 1637 | _incomingVideoChannel.reset(new IncomingV2VideoChannel( |
| 1638 | _channelManager.get(), |
| 1639 | _call.get(), |
| 1640 | _rtpTransport, |
| 1641 | _uniqueRandomIdGenerator.get(), |
| 1642 | incomingVideoContent, |
| 1643 | "1", |
| 1644 | _threads |
| 1645 | )); |
| 1646 | _incomingVideoChannel->addSink(_currentSink); |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 |
| 1652 | if (const auto screencast = initialSetup->screencast) { |
| 1653 | if (_encryptionKey.isOutgoing) { |
| 1654 | if (_outgoingScreencastContent) { |
| 1655 | _negotiatedOutgoingScreencastContent = negotiateMediaContent<cricket::VideoCodec>(_outgoingScreencastContent.value(), _outgoingScreencastContent.value(), screencast.value(), false); |
| 1656 | const auto incomingScreencastContent = negotiateMediaContent<cricket::VideoCodec>(screencast.value(), _outgoingScreencastContent.value(), screencast.value(), false); |
| 1657 |
| 1658 | signaling::MediaContent outgoingScreencastContent; |
| 1659 |
| 1660 | outgoingScreencastContent.ssrc = _outgoingScreencastContent->ssrc; |
| 1661 | outgoingScreencastContent.ssrcGroups = _outgoingScreencastContent->ssrcGroups; |
| 1662 | outgoingScreencastContent.rtpExtensions = _negotiatedOutgoingScreencastContent->rtpExtensions; |
| 1663 | outgoingScreencastContent.payloadTypes = getPayloadTypesFromVideoCodecs(_negotiatedOutgoingScreencastContent->codecs); |
| 1664 |
| 1665 | _outgoingScreencastContent = std::move(outgoingScreencastContent); |
| 1666 |
| 1667 | _incomingScreencastChannel.reset(new IncomingV2VideoChannel( |
| 1668 | _channelManager.get(), |
| 1669 | _call.get(), |
| 1670 | _rtpTransport, |
| 1671 | _uniqueRandomIdGenerator.get(), |
| 1672 | incomingScreencastContent, |
| 1673 | "2", |
| 1674 | _threads |
| 1675 | )); |
| 1676 | _incomingScreencastChannel->addSink(_currentSink); |
| 1677 | } |
| 1678 | } else { |
| 1679 | const auto generatedOutgoingContent = OutgoingVideoChannel::createOutgoingContentDescription(_availableVideoFormats, true); |
| 1680 |
| 1681 | if (generatedOutgoingContent) { |
| 1682 | _negotiatedOutgoingScreencastContent = negotiateMediaContent<cricket::VideoCodec>(generatedOutgoingContent.value(), generatedOutgoingContent.value(), screencast.value(), true); |
| 1683 | const auto incomingScreencastContent = negotiateMediaContent<cricket::VideoCodec>(screencast.value(), generatedOutgoingContent.value(), screencast.value(), true); |
| 1684 |
| 1685 | if (_negotiatedOutgoingScreencastContent) { |
| 1686 | signaling::MediaContent outgoingScreencastContent; |
| 1687 |
| 1688 | outgoingScreencastContent.ssrc = generatedOutgoingContent->ssrc; |
| 1689 | outgoingScreencastContent.ssrcGroups = generatedOutgoingContent->ssrcGroups; |
| 1690 | outgoingScreencastContent.rtpExtensions = _negotiatedOutgoingScreencastContent->rtpExtensions; |
| 1691 | outgoingScreencastContent.payloadTypes = getPayloadTypesFromVideoCodecs(_negotiatedOutgoingScreencastContent->codecs); |
| 1692 |
| 1693 | _outgoingScreencastContent = std::move(outgoingScreencastContent); |
| 1694 |
| 1695 | _incomingScreencastChannel.reset(new IncomingV2VideoChannel( |
| 1696 | _channelManager.get(), |
| 1697 | _call.get(), |
| 1698 | _rtpTransport, |
| 1699 | _uniqueRandomIdGenerator.get(), |
| 1700 | incomingScreencastContent, |
| 1701 | "2", |
| 1702 | _threads |
| 1703 | )); |
| 1704 | _incomingScreencastChannel->addSink(_currentSink); |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | } |
| 1709 |
| 1710 | createNegotiatedChannels(); |
| 1711 |
| 1712 | if (!_encryptionKey.isOutgoing) { |
| 1713 | sendInitialSetup(); |
| 1714 | } |
| 1715 |
| 1716 | _handshakeCompleted = true; |
| 1717 | commitPendingIceCandidates(); |
| 1718 | } else if (const auto candidatesList = absl::get_if<signaling::CandidatesMessage>(messageData)) { |
| 1719 | for (const auto &candidate : candidatesList->iceCandidates) { |
| 1720 | webrtc::JsepIceCandidate parseCandidate{ std::string(), 0 }; |
| 1721 | if (!parseCandidate.Initialize(candidate.sdpString, nullptr)) { |
| 1722 | RTC_LOG(LS_ERROR) << "Could not parse candidate: " << candidate.sdpString; |
| 1723 | continue; |
| 1724 | } |
| 1725 | _pendingIceCandidates.push_back(parseCandidate.candidate()); |
| 1726 | } |
| 1727 |
| 1728 | if (_handshakeCompleted) { |
| 1729 | commitPendingIceCandidates(); |
| 1730 | } |
| 1731 | } else if (const auto mediaState = absl::get_if<signaling::MediaStateMessage>(messageData)) { |
| 1732 | AudioState mappedAudioState; |
| 1733 | if (mediaState->isMuted) { |
| 1734 | mappedAudioState = AudioState::Muted; |
| 1735 | } else { |
| 1736 | mappedAudioState = AudioState::Active; |
| 1737 | } |
| 1738 |
| 1739 | VideoState mappedVideoState; |
| 1740 | switch (mediaState->videoState) { |
| 1741 | case signaling::MediaStateMessage::VideoState::Inactive: { |
| 1742 | mappedVideoState = VideoState::Inactive; |
| 1743 | break; |
| 1744 | } |
| 1745 | case signaling::MediaStateMessage::VideoState::Suspended: { |
| 1746 | mappedVideoState = VideoState::Paused; |
| 1747 | break; |
| 1748 | } |
| 1749 | case signaling::MediaStateMessage::VideoState::Active: { |
| 1750 | mappedVideoState = VideoState::Active; |
| 1751 | break; |
| 1752 | } |
| 1753 | default: { |
| 1754 | RTC_FATAL() << "Unknown videoState"; |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 |
| 1759 | VideoState mappedScreencastState; |
| 1760 | switch (mediaState->screencastState) { |
| 1761 | case signaling::MediaStateMessage::VideoState::Inactive: { |
| 1762 | mappedScreencastState = VideoState::Inactive; |
| 1763 | break; |
| 1764 | } |
| 1765 | case signaling::MediaStateMessage::VideoState::Suspended: { |
| 1766 | mappedScreencastState = VideoState::Paused; |
| 1767 | break; |
| 1768 | } |
| 1769 | case signaling::MediaStateMessage::VideoState::Active: { |
| 1770 | mappedScreencastState = VideoState::Active; |
| 1771 | break; |
| 1772 | } |
| 1773 | default: { |
| 1774 | RTC_FATAL() << "Unknown videoState"; |
| 1775 | break; |
| 1776 | } |
| 1777 | } |
| 1778 |
| 1779 | VideoState effectiveVideoState = mappedVideoState; |
| 1780 | if (mappedScreencastState == VideoState::Active || mappedScreencastState == VideoState::Paused) { |
| 1781 | effectiveVideoState = mappedScreencastState; |
| 1782 | } |
| 1783 |
| 1784 | if (_remoteMediaStateUpdated) { |
| 1785 | _remoteMediaStateUpdated(mappedAudioState, effectiveVideoState); |
| 1786 | } |
| 1787 |
| 1788 | if (_remoteBatteryLevelIsLowUpdated) { |
| 1789 | _remoteBatteryLevelIsLowUpdated(mediaState->isBatteryLow); |
| 1790 | } |
| 1791 | } |
| 1792 | } |
| 1793 |
| 1794 | void commitPendingIceCandidates() { |
| 1795 | if (_pendingIceCandidates.size() == 0) { |
| 1796 | return; |
| 1797 | } |
| 1798 | _networking->perform(RTC_FROM_HERE, [threads = _threads, parsedCandidates = _pendingIceCandidates](NativeNetworkingImpl *networking) { |
| 1799 | networking->addCandidates(parsedCandidates); |
| 1800 | }); |
| 1801 | _pendingIceCandidates.clear(); |
| 1802 | } |
| 1803 |
| 1804 | void onNetworkStateUpdated(NativeNetworkingImpl::State const &state) { |
| 1805 | State mappedState; |
| 1806 | if (state.isReadyToSendData) { |
| 1807 | mappedState = State::Established; |
| 1808 | } else { |
| 1809 | mappedState = State::Reconnecting; |
| 1810 | } |
| 1811 | _stateUpdated(mappedState); |
| 1812 | } |
| 1813 |
| 1814 | void onDataChannelStateUpdated(bool isDataChannelOpen) { |
| 1815 | if (_isDataChannelOpen != isDataChannelOpen) { |
| 1816 | _isDataChannelOpen = isDataChannelOpen; |
| 1817 |
| 1818 | if (_isDataChannelOpen) { |
| 1819 | sendMediaState(); |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 |
| 1824 | void sendDataChannelMessage(signaling::Message const &message) { |
| 1825 | if (!_isDataChannelOpen) { |
| 1826 | RTC_LOG(LS_ERROR) << "sendDataChannelMessage called, but data channel is not open"; |
| 1827 | return; |
| 1828 | } |
| 1829 | auto data = message.serialize(); |
| 1830 | std::string stringData(data.begin(), data.end()); |
| 1831 | RTC_LOG(LS_INFO) << "sendDataChannelMessage: " << stringData; |
| 1832 | _networking->perform(RTC_FROM_HERE, [stringData = std::move(stringData)](NativeNetworkingImpl *networking) { |
| 1833 | networking->sendDataChannelMessage(stringData); |
| 1834 | }); |
| 1835 | } |
| 1836 |
| 1837 | void onDataChannelMessage(std::string const &message) { |
| 1838 | RTC_LOG(LS_INFO) << "dataChannelMessage received: " << message; |
| 1839 | std::vector<uint8_t> data(message.begin(), message.end()); |
| 1840 | processSignalingData(data); |
| 1841 | } |
| 1842 |
| 1843 | void sendMediaState() { |
| 1844 | if (!_isDataChannelOpen) { |
| 1845 | return; |
| 1846 | } |
| 1847 | signaling::Message message; |
| 1848 | signaling::MediaStateMessage data; |
| 1849 | data.isMuted = _isMicrophoneMuted; |
| 1850 | data.isBatteryLow = _isBatteryLow; |
| 1851 | if (_outgoingVideoChannel) { |
| 1852 | if (_outgoingVideoChannel->videoCapture()) { |
| 1853 | data.videoState = signaling::MediaStateMessage::VideoState::Active; |
| 1854 | } else{ |
| 1855 | data.videoState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1856 | } |
| 1857 | data.videoRotation = _outgoingVideoChannel->getRotation(); |
| 1858 | } else { |
| 1859 | data.videoState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1860 | data.videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 1861 | } |
| 1862 | if (_outgoingScreencastChannel) { |
| 1863 | if (_outgoingScreencastChannel->videoCapture()) { |
| 1864 | data.screencastState = signaling::MediaStateMessage::VideoState::Active; |
| 1865 | } else{ |
| 1866 | data.screencastState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1867 | } |
| 1868 | } else { |
| 1869 | data.screencastState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1870 | } |
| 1871 | message.data = std::move(data); |
| 1872 | sendDataChannelMessage(message); |
| 1873 | } |
| 1874 |
| 1875 | void sendCandidate(const cricket::Candidate &candidate) { |
| 1876 | cricket::Candidate patchedCandidate = candidate; |
| 1877 | patchedCandidate.set_component(1); |
| 1878 |
| 1879 | signaling::CandidatesMessage data; |
| 1880 |
| 1881 | signaling::IceCandidate serializedCandidate; |
| 1882 |
| 1883 | webrtc::JsepIceCandidate iceCandidate{ std::string(), 0 }; |
| 1884 | iceCandidate.SetCandidate(patchedCandidate); |
| 1885 | std::string serialized; |
| 1886 | const auto success = iceCandidate.ToString(&serialized); |
| 1887 | assert(success); |
| 1888 | (void)success; |
| 1889 |
| 1890 | serializedCandidate.sdpString = serialized; |
| 1891 |
| 1892 | data.iceCandidates.push_back(std::move(serializedCandidate)); |
| 1893 |
| 1894 | signaling::Message message; |
| 1895 | message.data = std::move(data); |
| 1896 | sendSignalingMessage(message); |
| 1897 | } |
| 1898 |
| 1899 | void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 1900 | auto videoCaptureImpl = GetVideoCaptureAssumingSameThread(videoCapture.get()); |
| 1901 | if (videoCaptureImpl) { |
| 1902 | if (videoCaptureImpl->isScreenCapture()) { |
| 1903 | _videoCapture = nullptr; |
| 1904 | _screencastCapture = videoCapture; |
| 1905 |
| 1906 | if (_outgoingVideoChannel) { |
| 1907 | _outgoingVideoChannel->setVideoCapture(nullptr); |
| 1908 | } |
| 1909 |
| 1910 | if (_outgoingScreencastChannel) { |
| 1911 | _outgoingScreencastChannel->setVideoCapture(videoCapture); |
| 1912 | } |
| 1913 |
| 1914 | sendMediaState(); |
| 1915 | adjustBitratePreferences(true); |
| 1916 | } else { |
| 1917 | _videoCapture = videoCapture; |
| 1918 | _screencastCapture = nullptr; |
| 1919 |
| 1920 | if (_outgoingVideoChannel) { |
| 1921 | _outgoingVideoChannel->setVideoCapture(videoCapture); |
| 1922 | } |
| 1923 |
| 1924 | if (_outgoingScreencastChannel) { |
| 1925 | _outgoingScreencastChannel->setVideoCapture(nullptr); |
| 1926 | } |
| 1927 |
| 1928 | sendMediaState(); |
| 1929 | adjustBitratePreferences(true); |
| 1930 | } |
| 1931 | } else { |
| 1932 | _videoCapture = nullptr; |
| 1933 | _screencastCapture = nullptr; |
| 1934 |
| 1935 | if (_outgoingVideoChannel) { |
| 1936 | _outgoingVideoChannel->setVideoCapture(nullptr); |
| 1937 | } |
| 1938 |
| 1939 | if (_outgoingScreencastChannel) { |
| 1940 | _outgoingScreencastChannel->setVideoCapture(nullptr); |
| 1941 | } |
| 1942 |
| 1943 | sendMediaState(); |
| 1944 | adjustBitratePreferences(true); |
| 1945 | } |
| 1946 | } |
| 1947 |
| 1948 | void setRequestedVideoAspect(float aspect) { |
| 1949 | } |
| 1950 |
| 1951 | void setNetworkType(NetworkType networkType) { |
| 1952 |
| 1953 | } |
| 1954 |
| 1955 | void setMuteMicrophone(bool muteMicrophone) { |
| 1956 | if (_isMicrophoneMuted != muteMicrophone) { |
| 1957 | _isMicrophoneMuted = muteMicrophone; |
| 1958 |
| 1959 | if (_outgoingAudioChannel) { |
| 1960 | _outgoingAudioChannel->setIsMuted(muteMicrophone); |
| 1961 | } |
| 1962 |
| 1963 | sendMediaState(); |
| 1964 | } |
| 1965 | } |
| 1966 |
| 1967 | void setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 1968 | _currentSink = sink; |
| 1969 | if (_incomingVideoChannel) { |
| 1970 | _incomingVideoChannel->addSink(sink); |
| 1971 | } |
| 1972 | if (_incomingScreencastChannel) { |
| 1973 | _incomingScreencastChannel->addSink(sink); |
| 1974 | } |
| 1975 | } |
| 1976 |
| 1977 | void setAudioInputDevice(std::string id) { |
| 1978 |
| 1979 | } |
| 1980 |
| 1981 | void setAudioOutputDevice(std::string id) { |
| 1982 |
| 1983 | } |
| 1984 |
| 1985 | void setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 1986 | if (_isBatteryLow != isLowBatteryLevel) { |
| 1987 | _isBatteryLow = isLowBatteryLevel; |
| 1988 | sendMediaState(); |
| 1989 | } |
| 1990 | } |
| 1991 |
| 1992 | void stop(std::function<void(FinalState)> completion) { |
| 1993 | completion({}); |
| 1994 | } |
| 1995 |
| 1996 | void adjustBitratePreferences(bool resetStartBitrate) { |
| 1997 | webrtc::BitrateConstraints preferences; |
| 1998 | if (_videoCapture || _screencastCapture) { |
| 1999 | preferences.min_bitrate_bps = 64000; |
| 2000 | if (resetStartBitrate) { |
| 2001 | preferences.start_bitrate_bps = (100 + 800 + 32 + 100) * 1000; |
| 2002 | } |
| 2003 | preferences.max_bitrate_bps = (100 + 200 + 800 + 32 + 100) * 1000; |
| 2004 | } else { |
| 2005 | preferences.min_bitrate_bps = 32000; |
| 2006 | if (resetStartBitrate) { |
| 2007 | preferences.start_bitrate_bps = 32000; |
| 2008 | } |
| 2009 | preferences.max_bitrate_bps = 32000; |
| 2010 | } |
| 2011 |
| 2012 | _call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences); |
| 2013 | } |
| 2014 |
| 2015 | private: |
| 2016 | rtc::scoped_refptr<webrtc::AudioDeviceModule> createAudioDeviceModule() { |
| 2017 | const auto create = [&](webrtc::AudioDeviceModule::AudioLayer layer) { |
| 2018 | #ifdef WEBRTC_IOS |
| 2019 | return rtc::make_ref_counted<webrtc::tgcalls_ios_adm::AudioDeviceModuleIOS>(false, false); |
| 2020 | #else |
| 2021 | return webrtc::AudioDeviceModule::Create( |
| 2022 | layer, |
| 2023 | _taskQueueFactory.get()); |
| 2024 | #endif |
| 2025 | }; |
| 2026 | const auto check = [&](const rtc::scoped_refptr<webrtc::AudioDeviceModule> &result) { |
| 2027 | return (result && result->Init() == 0) ? result : nullptr; |
| 2028 | }; |
| 2029 | if (_createAudioDeviceModule) { |
| 2030 | if (const auto result = check(_createAudioDeviceModule(_taskQueueFactory.get()))) { |
| 2031 | return result; |
| 2032 | } |
| 2033 | } |
| 2034 | return check(create(webrtc::AudioDeviceModule::kPlatformDefaultAudio)); |
| 2035 | } |
| 2036 |
| 2037 | private: |
| 2038 | std::shared_ptr<Threads> _threads; |
| 2039 | std::vector<RtcServer> _rtcServers; |
| 2040 | EncryptionKey _encryptionKey; |
| 2041 | std::function<void(State)> _stateUpdated; |
| 2042 | std::function<void(int)> _signalBarsUpdated; |
| 2043 | std::function<void(float)> _audioLevelUpdated; |
| 2044 | std::function<void(bool)> _remoteBatteryLevelIsLowUpdated; |
| 2045 | std::function<void(AudioState, VideoState)> _remoteMediaStateUpdated; |
| 2046 | std::function<void(float)> _remotePrefferedAspectRatioUpdated; |
| 2047 | std::function<void(const std::vector<uint8_t> &)> _signalingDataEmitted; |
| 2048 | std::function<rtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> _createAudioDeviceModule; |
| 2049 |
| 2050 | std::unique_ptr<SignalingEncryption> _signalingEncryption; |
| 2051 |
| 2052 | bool _handshakeCompleted = false; |
| 2053 | std::vector<cricket::Candidate> _pendingIceCandidates; |
| 2054 | bool _isDataChannelOpen = false; |
| 2055 |
| 2056 | std::unique_ptr<webrtc::RtcEventLogNull> _eventLog; |
| 2057 | std::unique_ptr<webrtc::TaskQueueFactory> _taskQueueFactory; |
| 2058 | std::unique_ptr<cricket::MediaEngineInterface> _mediaEngine; |
| 2059 | std::unique_ptr<webrtc::Call> _call; |
| 2060 | webrtc::FieldTrialBasedConfig _fieldTrials; |
| 2061 | webrtc::LocalAudioSinkAdapter _audioSource; |
| 2062 | rtc::scoped_refptr<webrtc::AudioDeviceModule> _audioDeviceModule; |
| 2063 |
| 2064 | std::unique_ptr<rtc::UniqueRandomIdGenerator> _uniqueRandomIdGenerator; |
| 2065 | webrtc::RtpTransport *_rtpTransport = nullptr; |
| 2066 | std::unique_ptr<cricket::ChannelManager> _channelManager; |
| 2067 | std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory; |
| 2068 |
| 2069 | std::shared_ptr<ThreadLocalObject<NativeNetworkingImpl>> _networking; |
| 2070 |
| 2071 | absl::optional<signaling::MediaContent> _outgoingAudioContent; |
| 2072 | absl::optional<NegotiatedMediaContent<cricket::AudioCodec>> _negotiatedOutgoingAudioContent; |
| 2073 |
| 2074 | std::unique_ptr<OutgoingAudioChannel> _outgoingAudioChannel; |
| 2075 | bool _isMicrophoneMuted = false; |
| 2076 |
| 2077 | std::vector<webrtc::SdpVideoFormat> _availableVideoFormats; |
| 2078 |
| 2079 | absl::optional<signaling::MediaContent> _outgoingVideoContent; |
| 2080 | absl::optional<NegotiatedMediaContent<cricket::VideoCodec>> _negotiatedOutgoingVideoContent; |
| 2081 |
| 2082 | absl::optional<signaling::MediaContent> _outgoingScreencastContent; |
| 2083 | absl::optional<NegotiatedMediaContent<cricket::VideoCodec>> _negotiatedOutgoingScreencastContent; |
| 2084 |
| 2085 | std::shared_ptr<OutgoingVideoChannel> _outgoingVideoChannel; |
| 2086 | std::shared_ptr<OutgoingVideoChannel> _outgoingScreencastChannel; |
| 2087 |
| 2088 | bool _isBatteryLow = false; |
| 2089 |
| 2090 | std::unique_ptr<IncomingV2AudioChannel> _incomingAudioChannel; |
| 2091 | std::unique_ptr<IncomingV2VideoChannel> _incomingVideoChannel; |
| 2092 | std::unique_ptr<IncomingV2VideoChannel> _incomingScreencastChannel; |
| 2093 |
| 2094 | std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> _currentSink; |
| 2095 |
| 2096 | std::shared_ptr<VideoCaptureInterface> _videoCapture; |
| 2097 | std::shared_ptr<VideoCaptureInterface> _screencastCapture; |
| 2098 | }; |
| 2099 |
| 2100 | InstanceV2Impl::InstanceV2Impl(Descriptor &&descriptor) { |
| 2101 | if (descriptor.config.logPath.data.size() != 0) { |
| 2102 | _logSink = std::make_unique<LogSinkImpl>(descriptor.config.logPath); |
| 2103 | } |
| 2104 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 2105 | rtc::LogMessage::SetLogToStderr(false); |
| 2106 | if (_logSink) { |
| 2107 | rtc::LogMessage::AddLogToStream(_logSink.get(), rtc::LS_INFO); |
| 2108 | } |
| 2109 |
| 2110 | _threads = StaticThreads::getThreads(); |
| 2111 | _internal.reset(new ThreadLocalObject<InstanceV2ImplInternal>(_threads->getMediaThread(), [descriptor = std::move(descriptor), threads = _threads]() mutable { |
| 2112 | return new InstanceV2ImplInternal(std::move(descriptor), threads); |
| 2113 | })); |
| 2114 | _internal->perform(RTC_FROM_HERE, [](InstanceV2ImplInternal *internal) { |
| 2115 | internal->start(); |
| 2116 | }); |
| 2117 | } |
| 2118 |
| 2119 | InstanceV2Impl::~InstanceV2Impl() { |
| 2120 | rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
| 2121 | } |
| 2122 |
| 2123 | void InstanceV2Impl::receiveSignalingData(const std::vector<uint8_t> &data) { |
| 2124 | _internal->perform(RTC_FROM_HERE, [data](InstanceV2ImplInternal *internal) { |
| 2125 | internal->receiveSignalingData(data); |
| 2126 | }); |
| 2127 | } |
| 2128 |
| 2129 | void InstanceV2Impl::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 2130 | _internal->perform(RTC_FROM_HERE, [videoCapture](InstanceV2ImplInternal *internal) { |
| 2131 | internal->setVideoCapture(videoCapture); |
| 2132 | }); |
| 2133 | } |
| 2134 |
| 2135 | void InstanceV2Impl::setRequestedVideoAspect(float aspect) { |
| 2136 | _internal->perform(RTC_FROM_HERE, [aspect](InstanceV2ImplInternal *internal) { |
| 2137 | internal->setRequestedVideoAspect(aspect); |
| 2138 | }); |
| 2139 | } |
| 2140 |
| 2141 | void InstanceV2Impl::setNetworkType(NetworkType networkType) { |
| 2142 | _internal->perform(RTC_FROM_HERE, [networkType](InstanceV2ImplInternal *internal) { |
| 2143 | internal->setNetworkType(networkType); |
| 2144 | }); |
| 2145 | } |
| 2146 |
| 2147 | void InstanceV2Impl::setMuteMicrophone(bool muteMicrophone) { |
| 2148 | _internal->perform(RTC_FROM_HERE, [muteMicrophone](InstanceV2ImplInternal *internal) { |
| 2149 | internal->setMuteMicrophone(muteMicrophone); |
| 2150 | }); |
| 2151 | } |
| 2152 |
| 2153 | void InstanceV2Impl::setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 2154 | _internal->perform(RTC_FROM_HERE, [sink](InstanceV2ImplInternal *internal) { |
| 2155 | internal->setIncomingVideoOutput(sink); |
| 2156 | }); |
| 2157 | } |
| 2158 |
| 2159 | void InstanceV2Impl::setAudioInputDevice(std::string id) { |
| 2160 | _internal->perform(RTC_FROM_HERE, [id](InstanceV2ImplInternal *internal) { |
| 2161 | internal->setAudioInputDevice(id); |
| 2162 | }); |
| 2163 | } |
| 2164 |
| 2165 | void InstanceV2Impl::setAudioOutputDevice(std::string id) { |
| 2166 | _internal->perform(RTC_FROM_HERE, [id](InstanceV2ImplInternal *internal) { |
| 2167 | internal->setAudioOutputDevice(id); |
| 2168 | }); |
| 2169 | } |
| 2170 |
| 2171 | void InstanceV2Impl::setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 2172 | _internal->perform(RTC_FROM_HERE, [isLowBatteryLevel](InstanceV2ImplInternal *internal) { |
| 2173 | internal->setIsLowBatteryLevel(isLowBatteryLevel); |
| 2174 | }); |
| 2175 | } |
| 2176 |
| 2177 | void InstanceV2Impl::setInputVolume(float level) { |
| 2178 | } |
| 2179 |
| 2180 | void InstanceV2Impl::setOutputVolume(float level) { |
| 2181 | } |
| 2182 |
| 2183 | void InstanceV2Impl::setAudioOutputDuckingEnabled(bool enabled) { |
| 2184 | } |
| 2185 |
| 2186 | void InstanceV2Impl::setAudioOutputGainControlEnabled(bool enabled) { |
| 2187 | } |
| 2188 |
| 2189 | void InstanceV2Impl::setEchoCancellationStrength(int strength) { |
| 2190 | } |
| 2191 |
| 2192 | std::vector<std::string> InstanceV2Impl::GetVersions() { |
| 2193 | std::vector<std::string> result; |
| 2194 | result.push_back("4.0.0"); |
| 2195 | return result; |
| 2196 | } |
| 2197 |
| 2198 | int InstanceV2Impl::GetConnectionMaxLayer() { |
| 2199 | return 92; |
| 2200 | } |
| 2201 |
| 2202 | std::string InstanceV2Impl::getLastError() { |
| 2203 | return ""; |
| 2204 | } |
| 2205 |
| 2206 | std::string InstanceV2Impl::getDebugInfo() { |
| 2207 | return ""; |
| 2208 | } |
| 2209 |
| 2210 | int64_t InstanceV2Impl::getPreferredRelayId() { |
| 2211 | return 0; |
| 2212 | } |
| 2213 |
| 2214 | TrafficStats InstanceV2Impl::getTrafficStats() { |
| 2215 | return {}; |
| 2216 | } |
| 2217 |
| 2218 | PersistentState InstanceV2Impl::getPersistentState() { |
| 2219 | return {}; |
| 2220 | } |
| 2221 |
| 2222 | void InstanceV2Impl::stop(std::function<void(FinalState)> completion) { |
| 2223 | std::string debugLog; |
| 2224 | if (_logSink) { |
| 2225 | debugLog = _logSink->result(); |
| 2226 | } |
| 2227 | _internal->perform(RTC_FROM_HERE, [completion, debugLog = std::move(debugLog)](InstanceV2ImplInternal *internal) mutable { |
| 2228 | internal->stop([completion, debugLog = std::move(debugLog)](FinalState finalState) mutable { |
| 2229 | finalState.debugLog = debugLog; |
| 2230 | completion(finalState); |
| 2231 | }); |
| 2232 | }); |
| 2233 | } |
| 2234 |
| 2235 | template <> |
| 2236 | bool Register<InstanceV2Impl>() { |
| 2237 | return Meta::RegisterOne<InstanceV2Impl>(); |
| 2238 | } |
| 2239 |
| 2240 | } // namespace tgcalls |
| 2241 |