Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: webrtc/pc/mediasession.cc

Issue 2991693002: Adding support for Unified Plan offer/answer negotiation. (Closed)
Patch Set: Review comments. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 for (std::vector<std::string>::const_iterator it = crypto_suites.begin(); 181 for (std::vector<std::string>::const_iterator it = crypto_suites.begin();
182 it != crypto_suites.end(); ++it) { 182 it != crypto_suites.end(); ++it) {
183 if (!AddCryptoParams(*it, &cryptos)) { 183 if (!AddCryptoParams(*it, &cryptos)) {
184 return false; 184 return false;
185 } 185 }
186 } 186 }
187 AddMediaCryptos(cryptos, media); 187 AddMediaCryptos(cryptos, media);
188 return true; 188 return true;
189 } 189 }
190 190
191 const CryptoParamsVec* GetCryptos(const MediaContentDescription* media) { 191 const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
192 if (!media) { 192 if (!content) {
193 return NULL; 193 return nullptr;
194 } 194 }
195 return &media->cryptos(); 195
196 RTC_DCHECK(IsMediaContent(content));
197 return &(static_cast<const MediaContentDescription*>(content->description)
198 ->cryptos());
196 } 199 }
197 200
198 bool FindMatchingCrypto(const CryptoParamsVec& cryptos, 201 bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
199 const CryptoParams& crypto, 202 const CryptoParams& crypto,
200 CryptoParams* out) { 203 CryptoParams* out) {
201 for (CryptoParamsVec::const_iterator it = cryptos.begin(); 204 for (CryptoParamsVec::const_iterator it = cryptos.begin();
202 it != cryptos.end(); ++it) { 205 it != cryptos.end(); ++it) {
203 if (crypto.Matches(*it)) { 206 if (crypto.Matches(*it)) {
204 *out = *it; 207 *out = *it;
205 return true; 208 return true;
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 // audio and video extensions. 424 // audio and video extensions.
422 class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> { 425 class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
423 public: 426 public:
424 UsedRtpHeaderExtensionIds() 427 UsedRtpHeaderExtensionIds()
425 : UsedIds<webrtc::RtpExtension>(webrtc::RtpExtension::kMinId, 428 : UsedIds<webrtc::RtpExtension>(webrtc::RtpExtension::kMinId,
426 webrtc::RtpExtension::kMaxId) {} 429 webrtc::RtpExtension::kMaxId) {}
427 430
428 private: 431 private:
429 }; 432 };
430 433
431 // Adds a StreamParams for each Stream in Streams with media type 434 // Adds a StreamParams for each SenderOptions in |sender_options| to
432 // media_type to content_description. 435 // content_description.
433 // |current_params| - All currently known StreamParams of any media type. 436 // |current_params| - All currently known StreamParams of any media type.
434 template <class C> 437 template <class C>
435 static bool AddStreamParams(MediaType media_type, 438 static bool AddStreamParams(
436 const MediaSessionOptions& options, 439 const std::vector<SenderOptions>& sender_options,
437 StreamParamsVec* current_streams, 440 const std::string& rtcp_cname,
438 MediaContentDescriptionImpl<C>* content_description, 441 StreamParamsVec* current_streams,
439 const bool add_legacy_stream) { 442 MediaContentDescriptionImpl<C>* content_description) {
440 // SCTP streams are not negotiated using SDP/ContentDescriptions. 443 // SCTP streams are not negotiated using SDP/ContentDescriptions.
441 if (IsSctp(content_description->protocol())) { 444 if (IsSctp(content_description->protocol())) {
442 return true; 445 return true;
443 } 446 }
444 447
445 const bool include_rtx_streams = 448 const bool include_rtx_streams =
446 ContainsRtxCodec(content_description->codecs()); 449 ContainsRtxCodec(content_description->codecs());
447 450
448 const MediaSessionOptions::Streams& streams = options.streams;
449 if (streams.empty() && add_legacy_stream) {
450 // TODO(perkj): Remove this legacy stream when all apps use StreamParams.
451 std::vector<uint32_t> ssrcs;
452 int num_ssrcs = include_rtx_streams ? 2 : 1;
453 GenerateSsrcs(*current_streams, num_ssrcs, &ssrcs);
454 if (include_rtx_streams) {
455 content_description->AddLegacyStream(ssrcs[0], ssrcs[1]);
456 content_description->set_multistream(true);
457 } else {
458 content_description->AddLegacyStream(ssrcs[0]);
459 }
460 return true;
461 }
462 451
463 const bool include_flexfec_stream = 452 const bool include_flexfec_stream =
464 ContainsFlexfecCodec(content_description->codecs()); 453 ContainsFlexfecCodec(content_description->codecs());
465 454
466 MediaSessionOptions::Streams::const_iterator stream_it; 455 for (const SenderOptions& sender : sender_options) {
467 for (stream_it = streams.begin();
468 stream_it != streams.end(); ++stream_it) {
469 if (stream_it->type != media_type)
470 continue; // Wrong media type.
471
472 StreamParams* param = GetStreamByIds(*current_streams, "", stream_it->id);
473 // groupid is empty for StreamParams generated using 456 // groupid is empty for StreamParams generated using
474 // MediaSessionDescriptionFactory. 457 // MediaSessionDescriptionFactory.
458 StreamParams* param =
459 GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
475 if (!param) { 460 if (!param) {
476 // This is a new stream. 461 // This is a new sender.
477 std::vector<uint32_t> ssrcs; 462 std::vector<uint32_t> ssrcs;
478 GenerateSsrcs(*current_streams, stream_it->num_sim_layers, &ssrcs); 463 GenerateSsrcs(*current_streams, sender.num_sim_layers, &ssrcs);
479 StreamParams stream_param; 464 StreamParams stream_param;
480 stream_param.id = stream_it->id; 465 stream_param.id = sender.track_id;
481 // Add the generated ssrc. 466 // Add the generated ssrc.
482 for (size_t i = 0; i < ssrcs.size(); ++i) { 467 for (size_t i = 0; i < ssrcs.size(); ++i) {
483 stream_param.ssrcs.push_back(ssrcs[i]); 468 stream_param.ssrcs.push_back(ssrcs[i]);
484 } 469 }
485 if (stream_it->num_sim_layers > 1) { 470 if (sender.num_sim_layers > 1) {
486 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs); 471 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs);
487 stream_param.ssrc_groups.push_back(group); 472 stream_param.ssrc_groups.push_back(group);
488 } 473 }
489 // Generate extra ssrcs for include_rtx_streams case. 474 // Generate extra ssrcs for include_rtx_streams case.
490 if (include_rtx_streams) { 475 if (include_rtx_streams) {
491 // Generate an RTX ssrc for every ssrc in the group. 476 // Generate an RTX ssrc for every ssrc in the group.
492 std::vector<uint32_t> rtx_ssrcs; 477 std::vector<uint32_t> rtx_ssrcs;
493 GenerateSsrcs(*current_streams, static_cast<int>(ssrcs.size()), 478 GenerateSsrcs(*current_streams, static_cast<int>(ssrcs.size()),
494 &rtx_ssrcs); 479 &rtx_ssrcs);
495 for (size_t i = 0; i < ssrcs.size(); ++i) { 480 for (size_t i = 0; i < ssrcs.size(); ++i) {
496 stream_param.AddFidSsrc(ssrcs[i], rtx_ssrcs[i]); 481 stream_param.AddFidSsrc(ssrcs[i], rtx_ssrcs[i]);
497 } 482 }
498 content_description->set_multistream(true); 483 content_description->set_multistream(true);
499 } 484 }
500 // Generate extra ssrc for include_flexfec_stream case. 485 // Generate extra ssrc for include_flexfec_stream case.
501 if (include_flexfec_stream) { 486 if (include_flexfec_stream) {
502 // TODO(brandtr): Update when we support multistream protection. 487 // TODO(brandtr): Update when we support multistream protection.
503 if (ssrcs.size() == 1) { 488 if (ssrcs.size() == 1) {
504 std::vector<uint32_t> flexfec_ssrcs; 489 std::vector<uint32_t> flexfec_ssrcs;
505 GenerateSsrcs(*current_streams, 1, &flexfec_ssrcs); 490 GenerateSsrcs(*current_streams, 1, &flexfec_ssrcs);
506 stream_param.AddFecFrSsrc(ssrcs[0], flexfec_ssrcs[0]); 491 stream_param.AddFecFrSsrc(ssrcs[0], flexfec_ssrcs[0]);
507 content_description->set_multistream(true); 492 content_description->set_multistream(true);
508 } else if (!ssrcs.empty()) { 493 } else if (!ssrcs.empty()) {
509 LOG(LS_WARNING) 494 LOG(LS_WARNING)
510 << "Our FlexFEC implementation only supports protecting " 495 << "Our FlexFEC implementation only supports protecting "
511 << "a single media streams. This session has multiple " 496 << "a single media streams. This session has multiple "
512 << "media streams however, so no FlexFEC SSRC will be generated."; 497 << "media streams however, so no FlexFEC SSRC will be generated.";
513 } 498 }
514 } 499 }
515 stream_param.cname = options.rtcp_cname; 500 stream_param.cname = rtcp_cname;
516 stream_param.sync_label = stream_it->sync_label; 501 stream_param.sync_label = sender.stream_id;
517 content_description->AddStream(stream_param); 502 content_description->AddStream(stream_param);
518 503
519 // Store the new StreamParams in current_streams. 504 // Store the new StreamParams in current_streams.
520 // This is necessary so that we can use the CNAME for other media types. 505 // This is necessary so that we can use the CNAME for other media types.
521 current_streams->push_back(stream_param); 506 current_streams->push_back(stream_param);
522 } else { 507 } else {
523 // Use existing generated SSRCs/groups, but update the sync_label if 508 // Use existing generated SSRCs/groups, but update the sync_label if
524 // necessary. This may be needed if a MediaStreamTrack was moved from one 509 // necessary. This may be needed if a MediaStreamTrack was moved from one
525 // MediaStream to another. 510 // MediaStream to another.
526 param->sync_label = stream_it->sync_label; 511 param->sync_label = sender.stream_id;
527 content_description->AddStream(*param); 512 content_description->AddStream(*param);
528 } 513 }
529 } 514 }
530 return true; 515 return true;
531 } 516 }
532 517
533 // Updates the transport infos of the |sdesc| according to the given 518 // Updates the transport infos of the |sdesc| according to the given
534 // |bundle_group|. The transport infos of the content names within the 519 // |bundle_group|. The transport infos of the content names within the
535 // |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the 520 // |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
536 // first content within the |bundle_group|. 521 // first content within the |bundle_group|.
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 } 713 }
729 } 714 }
730 return false; 715 return false;
731 } 716 }
732 717
733 template <class C> 718 template <class C>
734 static bool IsFlexfecCodec(const C& codec) { 719 static bool IsFlexfecCodec(const C& codec) {
735 return STR_CASE_CMP(codec.name.c_str(), kFlexfecCodecName) == 0; 720 return STR_CASE_CMP(codec.name.c_str(), kFlexfecCodecName) == 0;
736 } 721 }
737 722
738 static TransportOptions GetTransportOptions(const MediaSessionOptions& options, 723 // Create a media content to be offered for the given |sender_options|,
739 const std::string& content_name) { 724 // according to the given options.rtcp_mux, session_options.is_muc, codecs,
740 TransportOptions transport_options; 725 // secure_transport, crypto, and current_streams. If we don't currently have
741 auto it = options.transport_options.find(content_name); 726 // crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
742 if (it != options.transport_options.end()) { 727 // created (according to crypto_suites). The created content is added to the
743 transport_options = it->second; 728 // offer.
744 }
745 transport_options.enable_ice_renomination = options.enable_ice_renomination;
746 return transport_options;
747 }
748
749 // Create a media content to be offered in a session-initiate,
750 // according to the given options.rtcp_mux, options.is_muc,
751 // options.streams, codecs, secure_transport, crypto, and streams. If we don't
752 // currently have crypto (in current_cryptos) and it is enabled (in
753 // secure_policy), crypto is created (according to crypto_suites). If
754 // add_legacy_stream is true, and current_streams is empty, a legacy
755 // stream is created. The created content is added to the offer.
756 template <class C> 729 template <class C>
757 static bool CreateMediaContentOffer( 730 static bool CreateMediaContentOffer(
758 const MediaSessionOptions& options, 731 const std::vector<SenderOptions>& sender_options,
732 const MediaSessionOptions& session_options,
759 const std::vector<C>& codecs, 733 const std::vector<C>& codecs,
760 const SecurePolicy& secure_policy, 734 const SecurePolicy& secure_policy,
761 const CryptoParamsVec* current_cryptos, 735 const CryptoParamsVec* current_cryptos,
762 const std::vector<std::string>& crypto_suites, 736 const std::vector<std::string>& crypto_suites,
763 const RtpHeaderExtensions& rtp_extensions, 737 const RtpHeaderExtensions& rtp_extensions,
764 bool add_legacy_stream,
765 StreamParamsVec* current_streams, 738 StreamParamsVec* current_streams,
766 MediaContentDescriptionImpl<C>* offer) { 739 MediaContentDescriptionImpl<C>* offer) {
767 offer->AddCodecs(codecs); 740 offer->AddCodecs(codecs);
768 741
769 offer->set_rtcp_mux(options.rtcp_mux_enabled); 742 offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
770 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) { 743 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
771 offer->set_rtcp_reduced_size(true); 744 offer->set_rtcp_reduced_size(true);
772 } 745 }
773 offer->set_multistream(options.is_muc); 746 offer->set_multistream(session_options.is_muc);
774 offer->set_rtp_header_extensions(rtp_extensions); 747 offer->set_rtp_header_extensions(rtp_extensions);
775 748
776 if (!AddStreamParams(offer->type(), options, current_streams, offer, 749 if (!AddStreamParams(sender_options, session_options.rtcp_cname,
777 add_legacy_stream)) { 750 current_streams, offer)) {
778 return false; 751 return false;
779 } 752 }
780 753
781 if (secure_policy != SEC_DISABLED) { 754 if (secure_policy != SEC_DISABLED) {
782 if (current_cryptos) { 755 if (current_cryptos) {
783 AddMediaCryptos(*current_cryptos, offer); 756 AddMediaCryptos(*current_cryptos, offer);
784 } 757 }
785 if (offer->cryptos().empty()) { 758 if (offer->cryptos().empty()) {
786 if (!CreateMediaCryptos(crypto_suites, offer)) { 759 if (!CreateMediaCryptos(crypto_suites, offer)) {
787 return false; 760 return false;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 } 848 }
876 if (found_codec) { 849 if (found_codec) {
877 *found_codec = potential_match; 850 *found_codec = potential_match;
878 } 851 }
879 return true; 852 return true;
880 } 853 }
881 } 854 }
882 return false; 855 return false;
883 } 856 }
884 857
885 // Adds all codecs from |reference_codecs| to |offered_codecs| that dont' 858 // Find the codec in |codec_list| that |rtx_codec| is associated with.
859 template <class C>
860 static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
861 const C& rtx_codec) {
862 std::string associated_pt_str;
863 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
864 &associated_pt_str)) {
865 LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
866 << " is missing an associated payload type.";
867 return nullptr;
868 }
869
870 int associated_pt;
871 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
872 LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
873 << " of RTX codec " << rtx_codec.name << " to an integer.";
874 return nullptr;
875 }
876
877 // Find the associated reference codec for the reference RTX codec.
878 const C* associated_codec = FindCodecById(codec_list, associated_pt);
879 if (!associated_codec) {
880 LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
881 << associated_pt << " for RTX codec " << rtx_codec.name
882 << ".";
883 }
884 return associated_codec;
885 }
886
887 // Adds all codecs from |reference_codecs| to |offered_codecs| that don't
886 // already exist in |offered_codecs| and ensure the payload types don't 888 // already exist in |offered_codecs| and ensure the payload types don't
887 // collide. 889 // collide.
888 template <class C> 890 template <class C>
889 static void FindCodecsToOffer( 891 static void MergeCodecs(const std::vector<C>& reference_codecs,
890 const std::vector<C>& reference_codecs, 892 std::vector<C>* offered_codecs,
891 std::vector<C>* offered_codecs, 893 UsedPayloadTypes* used_pltypes) {
892 UsedPayloadTypes* used_pltypes) {
893
894 // Add all new codecs that are not RTX codecs. 894 // Add all new codecs that are not RTX codecs.
895 for (const C& reference_codec : reference_codecs) { 895 for (const C& reference_codec : reference_codecs) {
896 if (!IsRtxCodec(reference_codec) && 896 if (!IsRtxCodec(reference_codec) &&
897 !FindMatchingCodec<C>(reference_codecs, *offered_codecs, 897 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
898 reference_codec, nullptr)) { 898 reference_codec, nullptr)) {
899 C codec = reference_codec; 899 C codec = reference_codec;
900 used_pltypes->FindAndSetIdUsed(&codec); 900 used_pltypes->FindAndSetIdUsed(&codec);
901 offered_codecs->push_back(codec); 901 offered_codecs->push_back(codec);
902 } 902 }
903 } 903 }
904 904
905 // Add all new RTX codecs. 905 // Add all new RTX codecs.
906 for (const C& reference_codec : reference_codecs) { 906 for (const C& reference_codec : reference_codecs) {
907 if (IsRtxCodec(reference_codec) && 907 if (IsRtxCodec(reference_codec) &&
908 !FindMatchingCodec<C>(reference_codecs, *offered_codecs, 908 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
909 reference_codec, nullptr)) { 909 reference_codec, nullptr)) {
910 C rtx_codec = reference_codec; 910 C rtx_codec = reference_codec;
911 911 const C* associated_codec =
912 std::string associated_pt_str; 912 GetAssociatedCodec(reference_codecs, rtx_codec);
913 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType, 913 if (!associated_codec) {
914 &associated_pt_str)) {
915 LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
916 << " is missing an associated payload type.";
917 continue; 914 continue;
918 } 915 }
919
920 int associated_pt;
921 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
922 LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
923 << " of RTX codec " << rtx_codec.name
924 << " to an integer.";
925 continue;
926 }
927
928 // Find the associated reference codec for the reference RTX codec.
929 const C* associated_codec =
930 FindCodecById(reference_codecs, associated_pt);
931 if (!associated_codec) {
932 LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
933 << associated_pt << " for RTX codec " << rtx_codec.name
934 << ".";
935 continue;
936 }
937
938 // Find a codec in the offered list that matches the reference codec. 916 // Find a codec in the offered list that matches the reference codec.
939 // Its payload type may be different than the reference codec. 917 // Its payload type may be different than the reference codec.
940 C matching_codec; 918 C matching_codec;
941 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs, 919 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
942 *associated_codec, &matching_codec)) { 920 *associated_codec, &matching_codec)) {
943 LOG(LS_WARNING) << "Couldn't find matching " << associated_codec->name 921 LOG(LS_WARNING) << "Couldn't find matching " << associated_codec->name
944 << " codec."; 922 << " codec.";
945 continue; 923 continue;
946 } 924 }
947 925
948 rtx_codec.params[kCodecParamAssociatedPayloadType] = 926 rtx_codec.params[kCodecParamAssociatedPayloadType] =
949 rtc::ToString(matching_codec.id); 927 rtc::ToString(matching_codec.id);
950 used_pltypes->FindAndSetIdUsed(&rtx_codec); 928 used_pltypes->FindAndSetIdUsed(&rtx_codec);
951 offered_codecs->push_back(rtx_codec); 929 offered_codecs->push_back(rtx_codec);
952 } 930 }
953 } 931 }
954 } 932 }
955 933
934 static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
935 const webrtc::RtpExtension& ext_to_match,
936 webrtc::RtpExtension* found_extension) {
937 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
938 it != extensions.end(); ++it) {
939 // We assume that all URIs are given in a canonical format.
940 if (it->uri == ext_to_match.uri && it->encrypt == ext_to_match.encrypt) {
941 if (found_extension) {
942 *found_extension = *it;
943 }
944 return true;
945 }
946 }
947 return false;
948 }
949
956 static bool FindByUri(const RtpHeaderExtensions& extensions, 950 static bool FindByUri(const RtpHeaderExtensions& extensions,
957 const webrtc::RtpExtension& ext_to_match, 951 const webrtc::RtpExtension& ext_to_match,
958 webrtc::RtpExtension* found_extension) { 952 webrtc::RtpExtension* found_extension) {
959 // We assume that all URIs are given in a canonical format. 953 // We assume that all URIs are given in a canonical format.
960 const webrtc::RtpExtension* found = 954 const webrtc::RtpExtension* found =
961 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions, 955 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
962 ext_to_match.uri); 956 ext_to_match.uri);
963 if (!found) { 957 if (!found) {
964 return false; 958 return false;
965 } 959 }
(...skipping 23 matching lines...) Expand all
989 } 983 }
990 if (unencrypted_extension) { 984 if (unencrypted_extension) {
991 if (found_extension) { 985 if (found_extension) {
992 *found_extension = *unencrypted_extension; 986 *found_extension = *unencrypted_extension;
993 } 987 }
994 return true; 988 return true;
995 } 989 }
996 return false; 990 return false;
997 } 991 }
998 992
999 // Iterates through |offered_extensions|, adding each one to 993 // Adds all extensions from |reference_extensions| to |offered_extensions| that
1000 // |regular_extensions| (or |encrypted_extensions| if encrypted) and |used_ids|, 994 // don't already exist in |offered_extensions| and ensure the IDs don't
1001 // and resolving ID conflicts. 995 // collide. If an extension is added, it's also added to |regular_extensions| or
1002 // If an offered extension has the same URI as one in |regular_extensions| or 996 // |encrypted_extensions|, and if the extension is in |regular_extensions| or
1003 // |encrypted_extensions|, it will re-use the same ID and won't be treated as 997 // |encrypted_extensions|, its ID is marked as used in |used_ids|.
1004 // a conflict. 998 // |offered_extensions| is for either audio or video while |regular_extensions|
1005 static void FindAndSetRtpHdrExtUsed(RtpHeaderExtensions* offered_extensions, 999 // and |encrypted_extensions| are used for both audio and video. There could be
1006 RtpHeaderExtensions* regular_extensions, 1000 // overlap between audio extensions and video extensions.
1007 RtpHeaderExtensions* encrypted_extensions, 1001 static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
1008 UsedRtpHeaderExtensionIds* used_ids) { 1002 RtpHeaderExtensions* offered_extensions,
1009 for (auto& extension : *offered_extensions) { 1003 RtpHeaderExtensions* regular_extensions,
1010 webrtc::RtpExtension existing; 1004 RtpHeaderExtensions* encrypted_extensions,
1011 if ((extension.encrypt && 1005 UsedRtpHeaderExtensionIds* used_ids) {
1012 FindByUri(*encrypted_extensions, extension, &existing)) || 1006 for (auto reference_extension : reference_extensions) {
1013 (!extension.encrypt && 1007 if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
1014 FindByUri(*regular_extensions, extension, &existing))) { 1008 nullptr)) {
1015 extension.id = existing.id; 1009 webrtc::RtpExtension existing;
1016 } else { 1010 if (reference_extension.encrypt) {
1017 used_ids->FindAndSetIdUsed(&extension); 1011 if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
1018 if (extension.encrypt) { 1012 &existing)) {
1019 encrypted_extensions->push_back(extension); 1013 offered_extensions->push_back(existing);
1014 } else {
1015 used_ids->FindAndSetIdUsed(&reference_extension);
1016 encrypted_extensions->push_back(reference_extension);
1017 offered_extensions->push_back(reference_extension);
1018 }
1020 } else { 1019 } else {
1021 regular_extensions->push_back(extension); 1020 if (FindByUriAndEncryption(*regular_extensions, reference_extension,
1021 &existing)) {
1022 offered_extensions->push_back(existing);
1023 } else {
1024 used_ids->FindAndSetIdUsed(&reference_extension);
1025 regular_extensions->push_back(reference_extension);
1026 offered_extensions->push_back(reference_extension);
1027 }
1022 } 1028 }
1023 } 1029 }
1024 } 1030 }
1025 }
1026
1027 // Adds |reference_extensions| to |offered_extensions|, while updating
1028 // |all_extensions| and |used_ids|.
1029 static void FindRtpHdrExtsToOffer(
1030 const RtpHeaderExtensions& reference_extensions,
1031 RtpHeaderExtensions* offered_extensions,
1032 RtpHeaderExtensions* all_extensions,
1033 UsedRtpHeaderExtensionIds* used_ids) {
1034 for (auto reference_extension : reference_extensions) {
1035 if (!FindByUri(*offered_extensions, reference_extension, NULL)) {
1036 webrtc::RtpExtension existing;
1037 if (FindByUri(*all_extensions, reference_extension, &existing)) {
1038 offered_extensions->push_back(existing);
1039 } else {
1040 used_ids->FindAndSetIdUsed(&reference_extension);
1041 all_extensions->push_back(reference_extension);
1042 offered_extensions->push_back(reference_extension);
1043 }
1044 }
1045 }
1046 } 1031 }
1047 1032
1048 static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions, 1033 static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
1049 RtpHeaderExtensions* all_extensions, 1034 RtpHeaderExtensions* all_extensions,
1050 UsedRtpHeaderExtensionIds* used_ids) { 1035 UsedRtpHeaderExtensionIds* used_ids) {
1051 RtpHeaderExtensions encrypted_extensions; 1036 RtpHeaderExtensions encrypted_extensions;
1052 for (const webrtc::RtpExtension& extension : *extensions) { 1037 for (const webrtc::RtpExtension& extension : *extensions) {
1053 webrtc::RtpExtension existing; 1038 webrtc::RtpExtension existing;
1054 // Don't add encrypted extensions again that were already included in a 1039 // Don't add encrypted extensions again that were already included in a
1055 // previous offer or regular extensions that are also included as encrypted 1040 // previous offer or regular extensions that are also included as encrypted
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1096 AudioCodecs::iterator iter = audio_codecs->begin(); 1081 AudioCodecs::iterator iter = audio_codecs->begin();
1097 while (iter != audio_codecs->end()) { 1082 while (iter != audio_codecs->end()) {
1098 if (STR_CASE_CMP(iter->name.c_str(), kComfortNoiseCodecName) == 0) { 1083 if (STR_CASE_CMP(iter->name.c_str(), kComfortNoiseCodecName) == 0) {
1099 iter = audio_codecs->erase(iter); 1084 iter = audio_codecs->erase(iter);
1100 } else { 1085 } else {
1101 ++iter; 1086 ++iter;
1102 } 1087 }
1103 } 1088 }
1104 } 1089 }
1105 1090
1106 // Create a media content to be answered in a session-accept, 1091 // Create a media content to be answered for the given |sender_options|
1107 // according to the given options.rtcp_mux, options.streams, codecs, 1092 // according to the given session_options.rtcp_mux, session_options.streams,
1108 // crypto, and streams. If we don't currently have crypto (in 1093 // codecs, crypto, and current_streams. If we don't currently have crypto (in
1109 // current_cryptos) and it is enabled (in secure_policy), crypto is 1094 // current_cryptos) and it is enabled (in secure_policy), crypto is created
1110 // created (according to crypto_suites). If add_legacy_stream is 1095 // (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
1111 // true, and current_streams is empty, a legacy stream is created. 1096 // negotiated with the offer. If the negotiation fails, this method returns
1112 // The codecs, rtcp_mux, and crypto are all negotiated with the offer 1097 // false. The created content is added to the offer.
1113 // from the incoming session-initiate. If the negotiation fails, this
1114 // method returns false. The created content is added to the offer.
1115 template <class C> 1098 template <class C>
1116 static bool CreateMediaContentAnswer( 1099 static bool CreateMediaContentAnswer(
1117 const MediaContentDescriptionImpl<C>* offer, 1100 const MediaContentDescriptionImpl<C>* offer,
1118 const MediaSessionOptions& options, 1101 const MediaDescriptionOptions& media_description_options,
1102 const MediaSessionOptions& session_options,
1119 const std::vector<C>& local_codecs, 1103 const std::vector<C>& local_codecs,
1120 const SecurePolicy& sdes_policy, 1104 const SecurePolicy& sdes_policy,
1121 const CryptoParamsVec* current_cryptos, 1105 const CryptoParamsVec* current_cryptos,
1122 const RtpHeaderExtensions& local_rtp_extenstions, 1106 const RtpHeaderExtensions& local_rtp_extenstions,
1123 bool enable_encrypted_rtp_header_extensions, 1107 bool enable_encrypted_rtp_header_extensions,
1124 StreamParamsVec* current_streams, 1108 StreamParamsVec* current_streams,
1125 bool add_legacy_stream,
1126 bool bundle_enabled, 1109 bool bundle_enabled,
1127 MediaContentDescriptionImpl<C>* answer) { 1110 MediaContentDescriptionImpl<C>* answer) {
1128 std::vector<C> negotiated_codecs; 1111 std::vector<C> negotiated_codecs;
1129 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs); 1112 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1130 answer->AddCodecs(negotiated_codecs); 1113 answer->AddCodecs(negotiated_codecs);
1131 answer->set_protocol(offer->protocol()); 1114 answer->set_protocol(offer->protocol());
1132 RtpHeaderExtensions negotiated_rtp_extensions; 1115 RtpHeaderExtensions negotiated_rtp_extensions;
1133 NegotiateRtpHeaderExtensions(local_rtp_extenstions, 1116 NegotiateRtpHeaderExtensions(local_rtp_extenstions,
1134 offer->rtp_header_extensions(), 1117 offer->rtp_header_extensions(),
1135 enable_encrypted_rtp_header_extensions, 1118 enable_encrypted_rtp_header_extensions,
1136 &negotiated_rtp_extensions); 1119 &negotiated_rtp_extensions);
1137 answer->set_rtp_header_extensions(negotiated_rtp_extensions); 1120 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1138 1121
1139 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux()); 1122 answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
1140 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) { 1123 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1141 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size()); 1124 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1142 } 1125 }
1143 1126
1144 if (sdes_policy != SEC_DISABLED) { 1127 if (sdes_policy != SEC_DISABLED) {
1145 CryptoParams crypto; 1128 CryptoParams crypto;
1146 if (SelectCrypto(offer, bundle_enabled, options.crypto_options, &crypto)) { 1129 if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
1130 &crypto)) {
1147 if (current_cryptos) { 1131 if (current_cryptos) {
1148 FindMatchingCrypto(*current_cryptos, crypto, &crypto); 1132 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1149 } 1133 }
1150 answer->AddCrypto(crypto); 1134 answer->AddCrypto(crypto);
1151 } 1135 }
1152 } 1136 }
1153 1137
1154 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) { 1138 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
1155 return false; 1139 return false;
1156 } 1140 }
1157 1141
1158 if (!AddStreamParams(answer->type(), options, current_streams, answer, 1142 if (!AddStreamParams(media_description_options.sender_options,
1159 add_legacy_stream)) { 1143 session_options.rtcp_cname, current_streams, answer)) {
1160 return false; // Something went seriously wrong. 1144 return false; // Something went seriously wrong.
1161 } 1145 }
1162 1146
1163 // Make sure the answer media content direction is per default set as
1164 // described in RFC3264 section 6.1.
1165 const bool is_data = !IsRtpProtocol(answer->protocol());
1166 const bool has_send_streams = !answer->streams().empty();
1167 const bool wants_send = has_send_streams || is_data;
1168 const bool recv_audio =
1169 answer->type() == cricket::MEDIA_TYPE_AUDIO && options.recv_audio;
1170 const bool recv_video =
1171 answer->type() == cricket::MEDIA_TYPE_VIDEO && options.recv_video;
1172 const bool recv_data =
1173 answer->type() == cricket::MEDIA_TYPE_DATA;
1174 const bool wants_receive = recv_audio || recv_video || recv_data;
1175
1176 auto offer_rtd = 1147 auto offer_rtd =
1177 RtpTransceiverDirection::FromMediaContentDirection(offer->direction()); 1148 RtpTransceiverDirection::FromMediaContentDirection(offer->direction());
1178 auto wants_rtd = RtpTransceiverDirection(wants_send, wants_receive); 1149
1179 answer->set_direction(NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd) 1150 answer->set_direction(NegotiateRtpTransceiverDirection(
1180 .ToMediaContentDirection()); 1151 offer_rtd, media_description_options.direction)
1152 .ToMediaContentDirection());
1181 return true; 1153 return true;
1182 } 1154 }
1183 1155
1184 static bool IsMediaProtocolSupported(MediaType type, 1156 static bool IsMediaProtocolSupported(MediaType type,
1185 const std::string& protocol, 1157 const std::string& protocol,
1186 bool secure_transport) { 1158 bool secure_transport) {
1187 // Since not all applications serialize and deserialize the media protocol, 1159 // Since not all applications serialize and deserialize the media protocol,
1188 // we will have to accept |protocol| to be empty. 1160 // we will have to accept |protocol| to be empty.
1189 if (protocol.empty()) { 1161 if (protocol.empty()) {
1190 return true; 1162 return true;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1232 const TransportInfo* info = 1204 const TransportInfo* info =
1233 current_description->GetTransportInfoByName(content_name); 1205 current_description->GetTransportInfoByName(content_name);
1234 if (info) { 1206 if (info) {
1235 desc = &info->description; 1207 desc = &info->description;
1236 } 1208 }
1237 } 1209 }
1238 return desc; 1210 return desc;
1239 } 1211 }
1240 1212
1241 // Gets the current DTLS state from the transport description. 1213 // Gets the current DTLS state from the transport description.
1242 static bool IsDtlsActive( 1214 static bool IsDtlsActive(const ContentInfo* content,
1243 const std::string& content_name, 1215 const SessionDescription* current_description) {
1244 const SessionDescription* current_description) { 1216 if (!content) {
1245 if (!current_description)
1246 return false; 1217 return false;
1218 }
1247 1219
1248 const ContentInfo* content = 1220 if (current_description->transport_infos().size() <=
1249 current_description->GetContentByName(content_name); 1221 content->msection_index) {
1250 if (!content)
1251 return false; 1222 return false;
1223 }
1252 1224
1253 const TransportDescription* current_tdesc = 1225 return current_description->transport_infos()[content->msection_index]
1254 GetTransportDescription(content_name, current_description); 1226 .description.secure();
1255 if (!current_tdesc)
1256 return false;
1257
1258 return current_tdesc->secure();
1259 } 1227 }
1260 1228
1261 std::string MediaContentDirectionToString(MediaContentDirection direction) { 1229 std::string MediaContentDirectionToString(MediaContentDirection direction) {
1262 std::string dir_str; 1230 std::string dir_str;
1263 switch (direction) { 1231 switch (direction) {
1264 case MD_INACTIVE: 1232 case MD_INACTIVE:
1265 dir_str = "inactive"; 1233 dir_str = "inactive";
1266 break; 1234 break;
1267 case MD_SENDONLY: 1235 case MD_SENDONLY:
1268 dir_str = "sendonly"; 1236 dir_str = "sendonly";
1269 break; 1237 break;
1270 case MD_RECVONLY: 1238 case MD_RECVONLY:
1271 dir_str = "recvonly"; 1239 dir_str = "recvonly";
1272 break; 1240 break;
1273 case MD_SENDRECV: 1241 case MD_SENDRECV:
1274 dir_str = "sendrecv"; 1242 dir_str = "sendrecv";
1275 break; 1243 break;
1276 default: 1244 default:
1277 RTC_NOTREACHED(); 1245 RTC_NOTREACHED();
1278 break; 1246 break;
1279 } 1247 }
1280 1248
1281 return dir_str; 1249 return dir_str;
1282 } 1250 }
1283 1251
1284 void MediaSessionOptions::AddSendStream(MediaType type, 1252 void MediaDescriptionOptions::AddAudioSender(const std::string& track_id,
1285 const std::string& id, 1253 const std::string& stream_id) {
1286 const std::string& sync_label) { 1254 RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
1287 AddSendStreamInternal(type, id, sync_label, 1); 1255 AddSenderInternal(track_id, stream_id, 1);
1288 } 1256 }
1289 1257
1290 void MediaSessionOptions::AddSendVideoStream( 1258 void MediaDescriptionOptions::AddVideoSender(const std::string& track_id,
1291 const std::string& id, 1259 const std::string& stream_id,
1292 const std::string& sync_label, 1260 int num_sim_layers) {
1293 int num_sim_layers) { 1261 RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
1294 AddSendStreamInternal(MEDIA_TYPE_VIDEO, id, sync_label, num_sim_layers); 1262 AddSenderInternal(track_id, stream_id, num_sim_layers);
1295 } 1263 }
1296 1264
1297 void MediaSessionOptions::AddSendStreamInternal( 1265 void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
1298 MediaType type, 1266 const std::string& stream_id) {
1299 const std::string& id, 1267 RTC_DCHECK(type == MEDIA_TYPE_DATA);
1300 const std::string& sync_label, 1268 AddSenderInternal(track_id, stream_id, 1);
1301 int num_sim_layers) {
1302 streams.push_back(Stream(type, id, sync_label, num_sim_layers));
1303
1304 // If we haven't already set the data_channel_type, and we add a
1305 // stream, we assume it's an RTP data stream.
1306 if (type == MEDIA_TYPE_DATA && data_channel_type == DCT_NONE)
1307 data_channel_type = DCT_RTP;
1308 } 1269 }
1309 1270
1310 void MediaSessionOptions::RemoveSendStream(MediaType type, 1271 void MediaDescriptionOptions::AddSenderInternal(const std::string& track_id,
1311 const std::string& id) { 1272 const std::string& stream_id,
1312 Streams::iterator stream_it = streams.begin(); 1273 int num_sim_layers) {
1313 for (; stream_it != streams.end(); ++stream_it) { 1274 sender_options.push_back(SenderOptions{track_id, stream_id, num_sim_layers});
1314 if (stream_it->type == type && stream_it->id == id) {
1315 streams.erase(stream_it);
1316 return;
1317 }
1318 }
1319 RTC_NOTREACHED();
1320 } 1275 }
1321 1276
1322 bool MediaSessionOptions::HasSendMediaStream(MediaType type) const { 1277 bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
1323 Streams::const_iterator stream_it = streams.begin(); 1278 return std::find_if(media_description_options.begin(),
1324 for (; stream_it != streams.end(); ++stream_it) { 1279 media_description_options.end(),
1325 if (stream_it->type == type) { 1280 [type](const MediaDescriptionOptions& t) {
1326 return true; 1281 return t.type == type;
1327 } 1282 }) != media_description_options.end();
1328 }
1329 return false;
1330 } 1283 }
1331 1284
1332 MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( 1285 MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1333 const TransportDescriptionFactory* transport_desc_factory) 1286 const TransportDescriptionFactory* transport_desc_factory)
1334 : secure_(SEC_DISABLED), 1287 : transport_desc_factory_(transport_desc_factory) {}
1335 add_legacy_(true),
1336 transport_desc_factory_(transport_desc_factory) {
1337 }
1338 1288
1339 MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( 1289 MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1340 ChannelManager* channel_manager, 1290 ChannelManager* channel_manager,
1341 const TransportDescriptionFactory* transport_desc_factory) 1291 const TransportDescriptionFactory* transport_desc_factory)
1342 : secure_(SEC_DISABLED), 1292 : transport_desc_factory_(transport_desc_factory) {
1343 add_legacy_(true),
1344 transport_desc_factory_(transport_desc_factory) {
1345 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_); 1293 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
1346 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_); 1294 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
1347 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_); 1295 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
1348 channel_manager->GetSupportedVideoCodecs(&video_codecs_); 1296 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
1349 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_); 1297 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1350 channel_manager->GetSupportedDataCodecs(&data_codecs_); 1298 channel_manager->GetSupportedDataCodecs(&data_codecs_);
1351 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_, 1299 ComputeAudioCodecsIntersectionAndUnion();
1352 &audio_sendrecv_codecs_);
1353 } 1300 }
1354 1301
1355 const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs() 1302 const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
1356 const { 1303 const {
1357 return audio_sendrecv_codecs_; 1304 return audio_sendrecv_codecs_;
1358 } 1305 }
1359 1306
1360 const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const { 1307 const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
1361 return audio_send_codecs_; 1308 return audio_send_codecs_;
1362 } 1309 }
1363 1310
1364 const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const { 1311 const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
1365 return audio_recv_codecs_; 1312 return audio_recv_codecs_;
1366 } 1313 }
1367 1314
1368 void MediaSessionDescriptionFactory::set_audio_codecs( 1315 void MediaSessionDescriptionFactory::set_audio_codecs(
1369 const AudioCodecs& send_codecs, const AudioCodecs& recv_codecs) { 1316 const AudioCodecs& send_codecs, const AudioCodecs& recv_codecs) {
1370 audio_send_codecs_ = send_codecs; 1317 audio_send_codecs_ = send_codecs;
1371 audio_recv_codecs_ = recv_codecs; 1318 audio_recv_codecs_ = recv_codecs;
1372 audio_sendrecv_codecs_.clear(); 1319 ComputeAudioCodecsIntersectionAndUnion();
1373 // Use NegotiateCodecs to merge our codec lists, since the operation is
1374 // essentially the same. Put send_codecs as the offered_codecs, which is the
1375 // order we'd like to follow. The reasoning is that encoding is usually more
1376 // expensive than decoding, and prioritizing a codec in the send list probably
1377 // means it's a codec we can handle efficiently.
1378 NegotiateCodecs(recv_codecs, send_codecs, &audio_sendrecv_codecs_);
1379 } 1320 }
1380 1321
1381 SessionDescription* MediaSessionDescriptionFactory::CreateOffer( 1322 SessionDescription* MediaSessionDescriptionFactory::CreateOffer(
1382 const MediaSessionOptions& options, 1323 const MediaSessionOptions& session_options,
1383 const SessionDescription* current_description) const { 1324 const SessionDescription* current_description) const {
1384 std::unique_ptr<SessionDescription> offer(new SessionDescription()); 1325 std::unique_ptr<SessionDescription> offer(new SessionDescription());
1385 1326
1386 StreamParamsVec current_streams; 1327 StreamParamsVec current_streams;
1387 GetCurrentStreamParams(current_description, &current_streams); 1328 GetCurrentStreamParams(current_description, &current_streams);
1388 1329
1389 const bool wants_send = 1330 AudioCodecs offer_audio_codecs;
1390 options.HasSendMediaStream(MEDIA_TYPE_AUDIO) || add_legacy_; 1331 VideoCodecs offer_video_codecs;
1391 const AudioCodecs& supported_audio_codecs = 1332 DataCodecs offer_data_codecs;
1392 GetAudioCodecsForOffer({wants_send, options.recv_audio}); 1333 GetCodecsForOffer(current_description, &offer_audio_codecs,
1334 &offer_video_codecs, &offer_data_codecs);
1393 1335
1394 AudioCodecs audio_codecs; 1336 if (!session_options.vad_enabled) {
1395 VideoCodecs video_codecs;
1396 DataCodecs data_codecs;
1397 GetCodecsToOffer(current_description, supported_audio_codecs,
1398 video_codecs_, data_codecs_,
1399 &audio_codecs, &video_codecs, &data_codecs);
1400
1401 if (!options.vad_enabled) {
1402 // If application doesn't want CN codecs in offer. 1337 // If application doesn't want CN codecs in offer.
1403 StripCNCodecs(&audio_codecs); 1338 StripCNCodecs(&offer_audio_codecs);
1404 } 1339 }
1340 FilterDataCodecs(&offer_data_codecs,
1341 session_options.data_channel_type == DCT_SCTP);
1405 1342
1406 RtpHeaderExtensions audio_rtp_extensions; 1343 RtpHeaderExtensions audio_rtp_extensions;
1407 RtpHeaderExtensions video_rtp_extensions; 1344 RtpHeaderExtensions video_rtp_extensions;
1408 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions, 1345 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions,
1409 &video_rtp_extensions); 1346 &video_rtp_extensions);
1410 1347
1411 bool audio_added = false; 1348 // Must have options for each existing section.
1412 bool video_added = false;
1413 bool data_added = false;
1414
1415 // Iterate through the contents of |current_description| to maintain the order
1416 // of the m-lines in the new offer.
1417 if (current_description) { 1349 if (current_description) {
1418 ContentInfos::const_iterator it = current_description->contents().begin(); 1350 RTC_DCHECK(current_description->contents().size() <=
1419 for (; it != current_description->contents().end(); ++it) { 1351 session_options.media_description_options.size());
1420 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
1421 if (!AddAudioContentForOffer(options, current_description,
1422 audio_rtp_extensions, audio_codecs,
1423 &current_streams, offer.get())) {
1424 return NULL;
1425 }
1426 audio_added = true;
1427 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
1428 if (!AddVideoContentForOffer(options, current_description,
1429 video_rtp_extensions, video_codecs,
1430 &current_streams, offer.get())) {
1431 return NULL;
1432 }
1433 video_added = true;
1434 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)) {
1435 MediaSessionOptions options_copy(options);
1436 if (IsSctp(static_cast<const MediaContentDescription*>(it->description)
1437 ->protocol())) {
1438 options_copy.data_channel_type = DCT_SCTP;
1439 }
1440 if (!AddDataContentForOffer(options_copy, current_description,
1441 &data_codecs, &current_streams,
1442 offer.get())) {
1443 return NULL;
1444 }
1445 data_added = true;
1446 } else {
1447 RTC_NOTREACHED();
1448 }
1449 }
1450 } 1352 }
1451 1353
1452 // Append contents that are not in |current_description|. 1354 // Iterate through the media description options, matching with existing media
1453 if (!audio_added && options.has_audio() && 1355 // descriptions in |current_description|.
1454 !AddAudioContentForOffer(options, current_description, 1356 int msection_index = 0;
1455 audio_rtp_extensions, audio_codecs, 1357 for (const MediaDescriptionOptions& media_description_options :
1456 &current_streams, offer.get())) { 1358 session_options.media_description_options) {
1457 return NULL; 1359 const ContentInfo* current_content = nullptr;
1458 } 1360 if (current_description &&
1459 if (!video_added && options.has_video() && 1361 msection_index <
1460 !AddVideoContentForOffer(options, current_description, 1362 static_cast<int>(current_description->contents().size())) {
1461 video_rtp_extensions, video_codecs, 1363 current_content = &current_description->contents()[msection_index];
1462 &current_streams, offer.get())) { 1364 // Media type must match.
1463 return NULL; 1365 RTC_DCHECK(IsMediaContentOfType(current_content,
1464 } 1366 media_description_options.type));
1465 if (!data_added && options.has_data() && 1367 }
1466 !AddDataContentForOffer(options, current_description, &data_codecs, 1368 switch (media_description_options.type) {
1467 &current_streams, offer.get())) { 1369 case MEDIA_TYPE_AUDIO:
1468 return NULL; 1370 if (!AddAudioContentForOffer(media_description_options, session_options,
1371 current_content, current_description,
1372 audio_rtp_extensions, offer_audio_codecs,
1373 &current_streams, offer.get())) {
1374 return nullptr;
1375 }
1376 break;
1377 case MEDIA_TYPE_VIDEO:
1378 if (!AddVideoContentForOffer(media_description_options, session_options,
1379 current_content, current_description,
1380 video_rtp_extensions, offer_video_codecs,
1381 &current_streams, offer.get())) {
1382 return nullptr;
1383 }
1384 break;
1385 case MEDIA_TYPE_DATA:
1386 if (!AddDataContentForOffer(media_description_options, session_options,
1387 current_content, current_description,
1388 offer_data_codecs, &current_streams,
1389 offer.get())) {
1390 return nullptr;
1391 }
1392 break;
1393 default:
1394 RTC_NOTREACHED();
1395 }
1396 ++msection_index;
1469 } 1397 }
1470 1398
1471 // Bundle the contents together, if we've been asked to do so, and update any 1399 // Bundle the contents together, if we've been asked to do so, and update any
1472 // parameters that need to be tweaked for BUNDLE. 1400 // parameters that need to be tweaked for BUNDLE.
1473 if (options.bundle_enabled) { 1401 if (session_options.bundle_enabled && offer->contents().size() > 0u) {
1474 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE); 1402 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
1475 for (ContentInfos::const_iterator content = offer->contents().begin(); 1403 for (const ContentInfo& content : offer->contents()) {
1476 content != offer->contents().end(); ++content) { 1404 // TODO(deadbeef): There are conditions that make bundling two media
1477 offer_bundle.AddContentName(content->name); 1405 // descriptions together illegal. For example, they use the same payload
1406 // type to represent different codecs, or same IDs for different header
1407 // extensions. We need to detect this and not try to bundle those media
1408 // descriptions together.
1409 offer_bundle.AddContentName(content.name);
1478 } 1410 }
1479 offer->AddGroup(offer_bundle); 1411 offer->AddGroup(offer_bundle);
1480 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) { 1412 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1481 LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle."; 1413 LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle.";
1482 return NULL; 1414 return nullptr;
1483 } 1415 }
1484 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) { 1416 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1485 LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle."; 1417 LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1486 return NULL; 1418 return nullptr;
1487 } 1419 }
1488 } 1420 }
1489
1490 return offer.release(); 1421 return offer.release();
1491 } 1422 }
1492 1423
1493 SessionDescription* MediaSessionDescriptionFactory::CreateAnswer( 1424 SessionDescription* MediaSessionDescriptionFactory::CreateAnswer(
1494 const SessionDescription* offer, const MediaSessionOptions& options, 1425 const SessionDescription* offer,
1426 const MediaSessionOptions& session_options,
1495 const SessionDescription* current_description) const { 1427 const SessionDescription* current_description) const {
1496 if (!offer) { 1428 if (!offer) {
1497 return nullptr; 1429 return nullptr;
1498 } 1430 }
1499 // The answer contains the intersection of the codecs in the offer with the 1431 // The answer contains the intersection of the codecs in the offer with the
1500 // codecs we support. As indicated by XEP-0167, we retain the same payload ids 1432 // codecs we support. As indicated by XEP-0167, we retain the same payload ids
1501 // from the offer in the answer. 1433 // from the offer in the answer.
1502 std::unique_ptr<SessionDescription> answer(new SessionDescription()); 1434 std::unique_ptr<SessionDescription> answer(new SessionDescription());
1503 1435
1504 StreamParamsVec current_streams; 1436 StreamParamsVec current_streams;
1505 GetCurrentStreamParams(current_description, &current_streams); 1437 GetCurrentStreamParams(current_description, &current_streams);
1506 1438
1507 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE 1439 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1508 // group in the answer with the appropriate content names. 1440 // group in the answer with the appropriate content names.
1509 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE); 1441 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1510 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE); 1442 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1511 // Transport info shared by the bundle group. 1443 // Transport info shared by the bundle group.
1512 std::unique_ptr<TransportInfo> bundle_transport; 1444 std::unique_ptr<TransportInfo> bundle_transport;
1513 1445
1514 ContentInfos::const_iterator it = offer->contents().begin(); 1446 // Get list of all possible codecs that respects existing payload type
1515 for (; it != offer->contents().end(); ++it) { 1447 // mappings and uses a single payload type space.
1516 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) { 1448 //
1517 if (!AddAudioContentForAnswer(offer, options, current_description, 1449 // Note that these lists may be further filtered for each m= section; this
1518 bundle_transport.get(), &current_streams, 1450 // step is done just to establish the payload type mappings shared by all
1519 answer.get())) { 1451 // sections.
1520 return NULL; 1452 AudioCodecs answer_audio_codecs;
1521 } 1453 VideoCodecs answer_video_codecs;
1522 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) { 1454 DataCodecs answer_data_codecs;
1523 if (!AddVideoContentForAnswer(offer, options, current_description, 1455 GetCodecsForAnswer(current_description, offer, &answer_audio_codecs,
1524 bundle_transport.get(), &current_streams, 1456 &answer_video_codecs, &answer_data_codecs);
1525 answer.get())) { 1457
1526 return NULL; 1458 if (!session_options.vad_enabled) {
1527 } 1459 // If application doesn't want CN codecs in answer.
1528 } else { 1460 StripCNCodecs(&answer_audio_codecs);
1529 RTC_DCHECK(IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)); 1461 }
1530 if (!AddDataContentForAnswer(offer, options, current_description, 1462 FilterDataCodecs(&answer_data_codecs,
1531 bundle_transport.get(), &current_streams, 1463 session_options.data_channel_type == DCT_SCTP);
1532 answer.get())) { 1464
1533 return NULL; 1465 // Must have options for exactly as many sections as in the offer.
1534 } 1466 RTC_DCHECK(offer->contents().size() ==
1467 session_options.media_description_options.size());
1468 // Iterate through the media description options, matching with existing
1469 // media descriptions in |current_description|.
1470 int msection_index = 0;
1471 for (const MediaDescriptionOptions& media_description_options :
1472 session_options.media_description_options) {
1473 const ContentInfo* offer_content = &offer->contents()[msection_index];
1474 // Media types and MIDs must match between the remote offer and the
1475 // MediaDescriptionOptions.
1476 RTC_DCHECK(
1477 IsMediaContentOfType(offer_content, media_description_options.type));
1478 RTC_DCHECK(media_description_options.mid == offer_content->name);
1479 const ContentInfo* current_content = nullptr;
1480 if (current_description &&
1481 msection_index <
1482 static_cast<int>(current_description->contents().size())) {
1483 current_content = &current_description->contents()[msection_index];
1535 } 1484 }
1485 switch (media_description_options.type) {
1486 case MEDIA_TYPE_AUDIO:
1487 if (!AddAudioContentForAnswer(
1488 media_description_options, session_options, offer_content,
1489 offer, current_content, current_description,
1490 bundle_transport.get(), answer_audio_codecs, &current_streams,
1491 answer.get())) {
1492 return nullptr;
1493 }
1494 break;
1495 case MEDIA_TYPE_VIDEO:
1496 if (!AddVideoContentForAnswer(
1497 media_description_options, session_options, offer_content,
1498 offer, current_content, current_description,
1499 bundle_transport.get(), answer_video_codecs, &current_streams,
1500 answer.get())) {
1501 return nullptr;
1502 }
1503 break;
1504 case MEDIA_TYPE_DATA:
1505 if (!AddDataContentForAnswer(media_description_options, session_options,
1506 offer_content, offer, current_content,
1507 current_description,
1508 bundle_transport.get(), answer_data_codecs,
1509 &current_streams, answer.get())) {
1510 return nullptr;
1511 }
1512 break;
1513 default:
1514 RTC_NOTREACHED();
1515 }
1516 ++msection_index;
1536 // See if we can add the newly generated m= section to the BUNDLE group in 1517 // See if we can add the newly generated m= section to the BUNDLE group in
1537 // the answer. 1518 // the answer.
1538 ContentInfo& added = answer->contents().back(); 1519 ContentInfo& added = answer->contents().back();
1539 if (!added.rejected && options.bundle_enabled && offer_bundle && 1520 if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
1540 offer_bundle->HasContentName(added.name)) { 1521 offer_bundle->HasContentName(added.name)) {
1541 answer_bundle.AddContentName(added.name); 1522 answer_bundle.AddContentName(added.name);
1542 bundle_transport.reset( 1523 bundle_transport.reset(
1543 new TransportInfo(*answer->GetTransportInfoByName(added.name))); 1524 new TransportInfo(*answer->GetTransportInfoByName(added.name)));
1544 } 1525 }
1545 } 1526 }
1546 1527
1547 // Only put BUNDLE group in answer if nonempty. 1528 // Only put BUNDLE group in answer if nonempty.
1548 if (answer_bundle.FirstContentName()) { 1529 if (answer_bundle.FirstContentName()) {
1549 answer->AddGroup(answer_bundle); 1530 answer->AddGroup(answer_bundle);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1589 } else { 1570 } else {
1590 return audio_send_codecs_; 1571 return audio_send_codecs_;
1591 } 1572 }
1592 } else if (answer.send) { 1573 } else if (answer.send) {
1593 return audio_send_codecs_; 1574 return audio_send_codecs_;
1594 } else { 1575 } else {
1595 return audio_recv_codecs_; 1576 return audio_recv_codecs_;
1596 } 1577 }
1597 } 1578 }
1598 1579
1599 void MediaSessionDescriptionFactory::GetCodecsToOffer( 1580 void MergeCodecsFromDescription(const SessionDescription* description,
1581 AudioCodecs* audio_codecs,
1582 VideoCodecs* video_codecs,
1583 DataCodecs* data_codecs,
1584 UsedPayloadTypes* used_pltypes) {
1585 RTC_DCHECK(description);
1586 for (const ContentInfo& content : description->contents()) {
1587 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1588 const AudioContentDescription* audio =
1589 static_cast<AudioContentDescription*>(content.description);
1590 MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
1591 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1592 const VideoContentDescription* video =
1593 static_cast<VideoContentDescription*>(content.description);
1594 MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
1595 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1596 const DataContentDescription* data =
1597 static_cast<DataContentDescription*>(content.description);
1598 MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes);
1599 }
1600 }
1601 }
1602
1603 // Getting codecs for an offer involves these steps:
1604 //
1605 // 1. Construct payload type -> codec mappings for current description.
1606 // 2. Add any reference codecs that weren't already present
1607 // 3. For each individual media description (m= section), filter codecs based
1608 // on the directional attribute (happens in another method).
1609 void MediaSessionDescriptionFactory::GetCodecsForOffer(
1600 const SessionDescription* current_description, 1610 const SessionDescription* current_description,
1601 const AudioCodecs& supported_audio_codecs,
1602 const VideoCodecs& supported_video_codecs,
1603 const DataCodecs& supported_data_codecs,
1604 AudioCodecs* audio_codecs, 1611 AudioCodecs* audio_codecs,
1605 VideoCodecs* video_codecs, 1612 VideoCodecs* video_codecs,
1606 DataCodecs* data_codecs) const { 1613 DataCodecs* data_codecs) const {
1607 UsedPayloadTypes used_pltypes; 1614 UsedPayloadTypes used_pltypes;
1608 audio_codecs->clear(); 1615 audio_codecs->clear();
1609 video_codecs->clear(); 1616 video_codecs->clear();
1610 data_codecs->clear(); 1617 data_codecs->clear();
1611 1618
1619 // First - get all codecs from the current description if the media type
1620 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1621 // new media type is added.
1622 if (current_description) {
1623 MergeCodecsFromDescription(current_description, audio_codecs, video_codecs,
1624 data_codecs, &used_pltypes);
1625 }
1626
1627 // Add our codecs that are not in |current_description|.
1628 MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
1629 MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1630 MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1631 }
1632
1633 // Getting codecs for an answer involves these steps:
1634 //
1635 // 1. Construct payload type -> codec mappings for current description.
1636 // 2. Add any codecs from the offer that weren't already present.
1637 // 3. Add any remaining codecs that weren't already present.
1638 // 4. For each individual media description (m= section), filter codecs based
1639 // on the directional attribute (happens in another method).
1640 void MediaSessionDescriptionFactory::GetCodecsForAnswer(
1641 const SessionDescription* current_description,
1642 const SessionDescription* remote_offer,
1643 AudioCodecs* audio_codecs,
1644 VideoCodecs* video_codecs,
1645 DataCodecs* data_codecs) const {
1646 UsedPayloadTypes used_pltypes;
1647 audio_codecs->clear();
1648 video_codecs->clear();
1649 data_codecs->clear();
1612 1650
1613 // First - get all codecs from the current description if the media type 1651 // First - get all codecs from the current description if the media type
1614 // is used. 1652 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1615 // Add them to |used_pltypes| so the payloadtype is not reused if a new media 1653 // new media type is added.
1616 // type is added.
1617 if (current_description) { 1654 if (current_description) {
1618 const AudioContentDescription* audio = 1655 MergeCodecsFromDescription(current_description, audio_codecs, video_codecs,
1619 GetFirstAudioContentDescription(current_description); 1656 data_codecs, &used_pltypes);
1620 if (audio) { 1657 }
1621 *audio_codecs = audio->codecs(); 1658
1622 used_pltypes.FindAndSetIdUsed<AudioCodec>(audio_codecs); 1659 // Second - filter out codecs that we don't support at all and should ignore.
1623 } 1660 AudioCodecs filtered_offered_audio_codecs;
1624 const VideoContentDescription* video = 1661 VideoCodecs filtered_offered_video_codecs;
1625 GetFirstVideoContentDescription(current_description); 1662 DataCodecs filtered_offered_data_codecs;
1626 if (video) { 1663 for (const ContentInfo& content : remote_offer->contents()) {
1627 *video_codecs = video->codecs(); 1664 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1628 used_pltypes.FindAndSetIdUsed<VideoCodec>(video_codecs); 1665 const AudioContentDescription* audio =
1629 } 1666 static_cast<AudioContentDescription*>(content.description);
1630 const DataContentDescription* data = 1667 for (const AudioCodec& offered_audio_codec : audio->codecs()) {
1631 GetFirstDataContentDescription(current_description); 1668 if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
1632 if (data) { 1669 filtered_offered_audio_codecs,
1633 *data_codecs = data->codecs(); 1670 offered_audio_codec, nullptr) &&
1634 used_pltypes.FindAndSetIdUsed<DataCodec>(data_codecs); 1671 FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
1672 offered_audio_codec, nullptr)) {
1673 filtered_offered_audio_codecs.push_back(offered_audio_codec);
1674 }
1675 }
1676 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1677 const VideoContentDescription* video =
1678 static_cast<VideoContentDescription*>(content.description);
1679 for (const VideoCodec& offered_video_codec : video->codecs()) {
1680 if (!FindMatchingCodec<VideoCodec>(video->codecs(),
1681 filtered_offered_video_codecs,
1682 offered_video_codec, nullptr) &&
1683 FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
1684 offered_video_codec, nullptr)) {
1685 filtered_offered_video_codecs.push_back(offered_video_codec);
1686 }
1687 }
1688 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1689 const DataContentDescription* data =
1690 static_cast<DataContentDescription*>(content.description);
1691 for (const DataCodec& offered_data_codec : data->codecs()) {
1692 if (!FindMatchingCodec<DataCodec>(data->codecs(),
1693 filtered_offered_data_codecs,
1694 offered_data_codec, nullptr) &&
1695 FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_,
1696 offered_data_codec, nullptr)) {
1697 filtered_offered_data_codecs.push_back(offered_data_codec);
1698 }
1699 }
1635 } 1700 }
1636 } 1701 }
1637 1702
1638 // Add our codecs that are not in |current_description|. 1703 // Add codecs that are not in |current_description| but were in
1639 FindCodecsToOffer<AudioCodec>(supported_audio_codecs, audio_codecs, 1704 // |remote_offer|.
1640 &used_pltypes); 1705 MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
1641 FindCodecsToOffer<VideoCodec>(supported_video_codecs, video_codecs, 1706 &used_pltypes);
1642 &used_pltypes); 1707 MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
1643 FindCodecsToOffer<DataCodec>(supported_data_codecs, data_codecs, 1708 &used_pltypes);
1644 &used_pltypes); 1709 MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs,
1710 &used_pltypes);
1645 } 1711 }
1646 1712
1647 void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer( 1713 void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
1648 const SessionDescription* current_description, 1714 const SessionDescription* current_description,
1649 RtpHeaderExtensions* audio_extensions, 1715 RtpHeaderExtensions* offer_audio_extensions,
1650 RtpHeaderExtensions* video_extensions) const { 1716 RtpHeaderExtensions* offer_video_extensions) const {
1651 // All header extensions allocated from the same range to avoid potential 1717 // All header extensions allocated from the same range to avoid potential
1652 // issues when using BUNDLE. 1718 // issues when using BUNDLE.
1653 UsedRtpHeaderExtensionIds used_ids; 1719 UsedRtpHeaderExtensionIds used_ids;
1654 RtpHeaderExtensions all_regular_extensions; 1720 RtpHeaderExtensions all_regular_extensions;
1655 RtpHeaderExtensions all_encrypted_extensions; 1721 RtpHeaderExtensions all_encrypted_extensions;
1656 audio_extensions->clear(); 1722 offer_audio_extensions->clear();
1657 video_extensions->clear(); 1723 offer_video_extensions->clear();
1658 1724
1659 // First - get all extensions from the current description if the media type 1725 // First - get all extensions from the current description if the media type
1660 // is used. 1726 // is used.
1661 // Add them to |used_ids| so the local ids are not reused if a new media 1727 // Add them to |used_ids| so the local ids are not reused if a new media
1662 // type is added. 1728 // type is added.
1663 if (current_description) { 1729 if (current_description) {
1664 const AudioContentDescription* audio = 1730 for (const ContentInfo& content : current_description->contents()) {
1665 GetFirstAudioContentDescription(current_description); 1731 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1666 if (audio) { 1732 const AudioContentDescription* audio =
1667 *audio_extensions = audio->rtp_header_extensions(); 1733 static_cast<const AudioContentDescription*>(content.description);
1668 FindAndSetRtpHdrExtUsed(audio_extensions, &all_regular_extensions, 1734 MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
1669 &all_encrypted_extensions, &used_ids); 1735 &all_regular_extensions, &all_encrypted_extensions,
1670 } 1736 &used_ids);
1671 const VideoContentDescription* video = 1737 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1672 GetFirstVideoContentDescription(current_description); 1738 const VideoContentDescription* video =
1673 if (video) { 1739 static_cast<const VideoContentDescription*>(content.description);
1674 *video_extensions = video->rtp_header_extensions(); 1740 MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
1675 FindAndSetRtpHdrExtUsed(video_extensions, &all_regular_extensions, 1741 &all_regular_extensions, &all_encrypted_extensions,
1676 &all_encrypted_extensions, &used_ids); 1742 &used_ids);
1743 }
1677 } 1744 }
1678 } 1745 }
1679 1746
1680 // Add our default RTP header extensions that are not in 1747 // Add our default RTP header extensions that are not in
1681 // |current_description|. 1748 // |current_description|.
1682 FindRtpHdrExtsToOffer(audio_rtp_header_extensions(), audio_extensions, 1749 MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
1683 &all_regular_extensions, &used_ids); 1750 &all_regular_extensions, &all_encrypted_extensions,
1684 FindRtpHdrExtsToOffer(video_rtp_header_extensions(), video_extensions, 1751 &used_ids);
1685 &all_regular_extensions, &used_ids); 1752 MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
1753 &all_regular_extensions, &all_encrypted_extensions,
1754 &used_ids);
1755
1686 // TODO(jbauch): Support adding encrypted header extensions to existing 1756 // TODO(jbauch): Support adding encrypted header extensions to existing
1687 // sessions. 1757 // sessions.
1688 if (enable_encrypted_rtp_header_extensions_ && !current_description) { 1758 if (enable_encrypted_rtp_header_extensions_ && !current_description) {
1689 AddEncryptedVersionsOfHdrExts(audio_extensions, &all_encrypted_extensions, 1759 AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
1690 &used_ids); 1760 &all_encrypted_extensions, &used_ids);
1691 AddEncryptedVersionsOfHdrExts(video_extensions, &all_encrypted_extensions, 1761 AddEncryptedVersionsOfHdrExts(offer_video_extensions,
1692 &used_ids); 1762 &all_encrypted_extensions, &used_ids);
1693 } 1763 }
1694 } 1764 }
1695 1765
1696 bool MediaSessionDescriptionFactory::AddTransportOffer( 1766 bool MediaSessionDescriptionFactory::AddTransportOffer(
1697 const std::string& content_name, 1767 const std::string& content_name,
1698 const TransportOptions& transport_options, 1768 const TransportOptions& transport_options,
1699 const SessionDescription* current_desc, 1769 const SessionDescription* current_desc,
1700 SessionDescription* offer_desc) const { 1770 SessionDescription* offer_desc) const {
1701 if (!transport_desc_factory_) 1771 if (!transport_desc_factory_)
1702 return false; 1772 return false;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1737 if (!answer_desc->AddTransportInfo(TransportInfo(content_name, 1807 if (!answer_desc->AddTransportInfo(TransportInfo(content_name,
1738 transport_desc))) { 1808 transport_desc))) {
1739 LOG(LS_ERROR) 1809 LOG(LS_ERROR)
1740 << "Failed to AddTransportAnswer, content name=" << content_name; 1810 << "Failed to AddTransportAnswer, content name=" << content_name;
1741 return false; 1811 return false;
1742 } 1812 }
1743 return true; 1813 return true;
1744 } 1814 }
1745 1815
1746 bool MediaSessionDescriptionFactory::AddAudioContentForOffer( 1816 bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
1747 const MediaSessionOptions& options, 1817 const MediaDescriptionOptions& media_description_options,
1818 const MediaSessionOptions& session_options,
1819 const ContentInfo* current_content,
1748 const SessionDescription* current_description, 1820 const SessionDescription* current_description,
1749 const RtpHeaderExtensions& audio_rtp_extensions, 1821 const RtpHeaderExtensions& audio_rtp_extensions,
1750 const AudioCodecs& audio_codecs, 1822 const AudioCodecs& audio_codecs,
Taylor Brandstetter 2017/08/09 17:05:12 As we talked about earlier, it would be helpful to
Zhi Huang 2017/08/09 21:51:02 Done.
1751 StreamParamsVec* current_streams, 1823 StreamParamsVec* current_streams,
1752 SessionDescription* desc) const { 1824 SessionDescription* desc) const {
1753 const ContentInfo* current_audio_content = 1825 // Filter audio_codecs (which includes all codecs, with correctly remapped
1754 GetFirstAudioContent(current_description); 1826 // payload types) based on transceiver direction.
1755 std::string content_name = 1827 const AudioCodecs& supported_audio_codecs =
1756 current_audio_content ? current_audio_content->name : CN_AUDIO; 1828 GetAudioCodecsForOffer(media_description_options.direction);
1829
1830 AudioCodecs filtered_codecs;
1831 AudioCodec existing_codec;
1832 // Add the codecs from current content if exists.
1833 if (current_content) {
1834 RTC_DCHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
1835 const AudioContentDescription* acd =
1836 static_cast<const AudioContentDescription*>(
1837 current_content->description);
1838 for (const AudioCodec& codec : acd->codecs()) {
1839 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
1840 codec, &existing_codec)) {
Taylor Brandstetter 2017/08/09 17:05:12 Based on the description of FindMatchingCodec, I t
Zhi Huang 2017/08/09 21:51:02 Done.
1841 if (current_description) {
Taylor Brandstetter 2017/08/09 17:05:12 I still don't understand the "if (current_descript
Zhi Huang 2017/08/09 21:51:02 Done.
1842 filtered_codecs.push_back(existing_codec);
1843 } else {
1844 filtered_codecs.push_back(codec);
1845 }
1846 }
1847 }
1848 }
1849 // Add other supported audio codecs.
1850 for (const AudioCodec& codec : supported_audio_codecs) {
1851 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
1852 codec, &existing_codec) &&
Taylor Brandstetter 2017/08/09 17:05:12 nit: "existing_codec" isn't a great name, since it
Zhi Huang 2017/08/09 21:51:02 Maybe just call it "found_codec"? I'm not good at
1853 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
1854 codec, nullptr)) {
1855 if (current_description) {
1856 filtered_codecs.push_back(existing_codec);
Taylor Brandstetter 2017/08/09 17:05:12 Again, why is the "if (current_description)" neede
Zhi Huang 2017/08/09 21:51:02 Done.
1857 } else {
1858 filtered_codecs.push_back(codec);
1859 }
1860 }
1861 }
1757 1862
1758 cricket::SecurePolicy sdes_policy = 1863 cricket::SecurePolicy sdes_policy =
1759 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED 1864 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
1760 : secure(); 1865 : secure();
1761 1866
1762 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription()); 1867 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
1763 std::vector<std::string> crypto_suites; 1868 std::vector<std::string> crypto_suites;
1764 GetSupportedAudioSdesCryptoSuiteNames(options.crypto_options, &crypto_suites); 1869 GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
1870 &crypto_suites);
1765 if (!CreateMediaContentOffer( 1871 if (!CreateMediaContentOffer(
1766 options, 1872 media_description_options.sender_options, session_options,
1767 audio_codecs, 1873 filtered_codecs, sdes_policy, GetCryptos(current_content),
1768 sdes_policy, 1874 crypto_suites, audio_rtp_extensions, current_streams, audio.get())) {
1769 GetCryptos(GetFirstAudioContentDescription(current_description)),
1770 crypto_suites,
1771 audio_rtp_extensions,
1772 add_legacy_,
1773 current_streams,
1774 audio.get())) {
1775 return false; 1875 return false;
1776 } 1876 }
1777 audio->set_lang(lang_); 1877 audio->set_lang(lang_);
1778 1878
1779 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); 1879 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1780 SetMediaProtocol(secure_transport, audio.get()); 1880 SetMediaProtocol(secure_transport, audio.get());
1781 1881
1782 auto offer_rtd = 1882 audio->set_direction(
1783 RtpTransceiverDirection(!audio->streams().empty(), options.recv_audio); 1883 media_description_options.direction.ToMediaContentDirection());
1784 audio->set_direction(offer_rtd.ToMediaContentDirection());
1785 1884
1786 desc->AddContent(content_name, NS_JINGLE_RTP, audio.release()); 1885 desc->AddContent(media_description_options.mid, NS_JINGLE_RTP,
1787 if (!AddTransportOffer(content_name, 1886 media_description_options.stopped, audio.release());
1788 GetTransportOptions(options, content_name), 1887 if (!AddTransportOffer(media_description_options.mid,
1888 media_description_options.transport_options,
1789 current_description, desc)) { 1889 current_description, desc)) {
1790 return false; 1890 return false;
1791 } 1891 }
1792 1892
1793 return true; 1893 return true;
1794 } 1894 }
1795 1895
1796 bool MediaSessionDescriptionFactory::AddVideoContentForOffer( 1896 bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
1797 const MediaSessionOptions& options, 1897 const MediaDescriptionOptions& media_description_options,
1898 const MediaSessionOptions& session_options,
1899 const ContentInfo* current_content,
1798 const SessionDescription* current_description, 1900 const SessionDescription* current_description,
1799 const RtpHeaderExtensions& video_rtp_extensions, 1901 const RtpHeaderExtensions& video_rtp_extensions,
1800 const VideoCodecs& video_codecs, 1902 const VideoCodecs& video_codecs,
1801 StreamParamsVec* current_streams, 1903 StreamParamsVec* current_streams,
1802 SessionDescription* desc) const { 1904 SessionDescription* desc) const {
1803 const ContentInfo* current_video_content =
1804 GetFirstVideoContent(current_description);
1805 std::string content_name =
1806 current_video_content ? current_video_content->name : CN_VIDEO;
1807
1808 cricket::SecurePolicy sdes_policy = 1905 cricket::SecurePolicy sdes_policy =
1809 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED 1906 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
1810 : secure(); 1907 : secure();
1811 1908
1812 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription()); 1909 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
1813 std::vector<std::string> crypto_suites; 1910 std::vector<std::string> crypto_suites;
1814 GetSupportedVideoSdesCryptoSuiteNames(options.crypto_options, &crypto_suites); 1911 GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
1912 &crypto_suites);
1815 if (!CreateMediaContentOffer( 1913 if (!CreateMediaContentOffer(
1816 options, 1914 media_description_options.sender_options, session_options,
1817 video_codecs, 1915 video_codecs, sdes_policy, GetCryptos(current_content), crypto_suites,
1818 sdes_policy, 1916 video_rtp_extensions, current_streams, video.get())) {
1819 GetCryptos(GetFirstVideoContentDescription(current_description)),
1820 crypto_suites,
1821 video_rtp_extensions,
1822 add_legacy_,
1823 current_streams,
1824 video.get())) {
1825 return false; 1917 return false;
1826 } 1918 }
1827 1919
1828 video->set_bandwidth(options.video_bandwidth); 1920 video->set_bandwidth(kAutoBandwidth);
1829 1921
1830 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); 1922 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1831 SetMediaProtocol(secure_transport, video.get()); 1923 SetMediaProtocol(secure_transport, video.get());
1832 1924
1833 if (!video->streams().empty()) { 1925 video->set_direction(
1834 if (options.recv_video) { 1926 media_description_options.direction.ToMediaContentDirection());
1835 video->set_direction(MD_SENDRECV);
1836 } else {
1837 video->set_direction(MD_SENDONLY);
1838 }
1839 } else {
1840 if (options.recv_video) {
1841 video->set_direction(MD_RECVONLY);
1842 } else {
1843 video->set_direction(MD_INACTIVE);
1844 }
1845 }
1846 1927
1847 desc->AddContent(content_name, NS_JINGLE_RTP, video.release()); 1928 desc->AddContent(media_description_options.mid, NS_JINGLE_RTP,
1848 if (!AddTransportOffer(content_name, 1929 media_description_options.stopped, video.release());
1849 GetTransportOptions(options, content_name), 1930 if (!AddTransportOffer(media_description_options.mid,
1931 media_description_options.transport_options,
1850 current_description, desc)) { 1932 current_description, desc)) {
1851 return false; 1933 return false;
1852 } 1934 }
1853
1854 return true; 1935 return true;
1855 } 1936 }
1856 1937
1857 bool MediaSessionDescriptionFactory::AddDataContentForOffer( 1938 bool MediaSessionDescriptionFactory::AddDataContentForOffer(
1858 const MediaSessionOptions& options, 1939 const MediaDescriptionOptions& media_description_options,
1940 const MediaSessionOptions& session_options,
1941 const ContentInfo* current_content,
1859 const SessionDescription* current_description, 1942 const SessionDescription* current_description,
1860 DataCodecs* data_codecs, 1943 const DataCodecs& data_codecs,
1861 StreamParamsVec* current_streams, 1944 StreamParamsVec* current_streams,
1862 SessionDescription* desc) const { 1945 SessionDescription* desc) const {
1863 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); 1946 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1864 1947
1865 std::unique_ptr<DataContentDescription> data(new DataContentDescription()); 1948 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
1866 bool is_sctp = (options.data_channel_type == DCT_SCTP); 1949 bool is_sctp = (session_options.data_channel_type == DCT_SCTP);
1867 1950 // If the DataChannel type is not specified, use the DataChannel type in
1868 FilterDataCodecs(data_codecs, is_sctp); 1951 // the current description.
1869 1952 if (session_options.data_channel_type == DCT_NONE && current_content) {
1870 const ContentInfo* current_data_content = 1953 is_sctp = (static_cast<const DataContentDescription*>(
1871 GetFirstDataContent(current_description); 1954 current_content->description)
1872 std::string content_name = 1955 ->protocol() == kMediaProtocolSctp);
1873 current_data_content ? current_data_content->name : CN_DATA; 1956 }
1874 1957
1875 cricket::SecurePolicy sdes_policy = 1958 cricket::SecurePolicy sdes_policy =
1876 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED 1959 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
1877 : secure(); 1960 : secure();
1878 std::vector<std::string> crypto_suites; 1961 std::vector<std::string> crypto_suites;
1879 if (is_sctp) { 1962 if (is_sctp) {
1880 // SDES doesn't make sense for SCTP, so we disable it, and we only 1963 // SDES doesn't make sense for SCTP, so we disable it, and we only
1881 // get SDES crypto suites for RTP-based data channels. 1964 // get SDES crypto suites for RTP-based data channels.
1882 sdes_policy = cricket::SEC_DISABLED; 1965 sdes_policy = cricket::SEC_DISABLED;
1883 // Unlike SetMediaProtocol below, we need to set the protocol 1966 // Unlike SetMediaProtocol below, we need to set the protocol
1884 // before we call CreateMediaContentOffer. Otherwise, 1967 // before we call CreateMediaContentOffer. Otherwise,
1885 // CreateMediaContentOffer won't know this is SCTP and will 1968 // CreateMediaContentOffer won't know this is SCTP and will
1886 // generate SSRCs rather than SIDs. 1969 // generate SSRCs rather than SIDs.
1887 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once 1970 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once
1888 // it's safe to do so. Older versions of webrtc would reject these 1971 // it's safe to do so. Older versions of webrtc would reject these
1889 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706. 1972 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706.
1890 data->set_protocol( 1973 data->set_protocol(
1891 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp); 1974 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp);
1892 } else { 1975 } else {
1893 GetSupportedDataSdesCryptoSuiteNames(options.crypto_options, 1976 GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
1894 &crypto_suites); 1977 &crypto_suites);
1895 } 1978 }
1896 1979
1980 // Even SCTP uses a "codec".
1897 if (!CreateMediaContentOffer( 1981 if (!CreateMediaContentOffer(
1898 options, 1982 media_description_options.sender_options, session_options,
1899 *data_codecs, 1983 data_codecs, sdes_policy, GetCryptos(current_content), crypto_suites,
1900 sdes_policy, 1984 RtpHeaderExtensions(), current_streams, data.get())) {
1901 GetCryptos(GetFirstDataContentDescription(current_description)),
1902 crypto_suites,
1903 RtpHeaderExtensions(),
1904 add_legacy_,
1905 current_streams,
1906 data.get())) {
1907 return false; 1985 return false;
1908 } 1986 }
1909 1987
1910 if (is_sctp) { 1988 if (is_sctp) {
1911 desc->AddContent(content_name, NS_JINGLE_DRAFT_SCTP, data.release()); 1989 desc->AddContent(media_description_options.mid, NS_JINGLE_DRAFT_SCTP,
1990 data.release());
1912 } else { 1991 } else {
1913 data->set_bandwidth(options.data_bandwidth); 1992 data->set_bandwidth(kDataMaxBandwidth);
1914 SetMediaProtocol(secure_transport, data.get()); 1993 SetMediaProtocol(secure_transport, data.get());
1915 desc->AddContent(content_name, NS_JINGLE_RTP, data.release()); 1994 desc->AddContent(media_description_options.mid, NS_JINGLE_RTP,
1995 media_description_options.stopped, data.release());
1916 } 1996 }
1917 if (!AddTransportOffer(content_name, 1997 if (!AddTransportOffer(media_description_options.mid,
1918 GetTransportOptions(options, content_name), 1998 media_description_options.transport_options,
1919 current_description, desc)) { 1999 current_description, desc)) {
1920 return false; 2000 return false;
1921 } 2001 }
1922 return true; 2002 return true;
1923 } 2003 }
1924 2004
1925 bool MediaSessionDescriptionFactory::AddAudioContentForAnswer( 2005 bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
1926 const SessionDescription* offer, 2006 const MediaDescriptionOptions& media_description_options,
1927 const MediaSessionOptions& options, 2007 const MediaSessionOptions& session_options,
2008 const ContentInfo* offer_content,
2009 const SessionDescription* offer_description,
2010 const ContentInfo* current_content,
1928 const SessionDescription* current_description, 2011 const SessionDescription* current_description,
1929 const TransportInfo* bundle_transport, 2012 const TransportInfo* bundle_transport,
2013 const AudioCodecs& audio_codecs,
1930 StreamParamsVec* current_streams, 2014 StreamParamsVec* current_streams,
1931 SessionDescription* answer) const { 2015 SessionDescription* answer) const {
1932 const ContentInfo* audio_content = GetFirstAudioContent(offer); 2016 const AudioContentDescription* offer_audio_description =
1933 const AudioContentDescription* offer_audio = 2017 static_cast<const AudioContentDescription*>(offer_content->description);
1934 static_cast<const AudioContentDescription*>(audio_content->description);
1935 2018
1936 std::unique_ptr<TransportDescription> audio_transport( 2019 std::unique_ptr<TransportDescription> audio_transport(
1937 CreateTransportAnswer(audio_content->name, offer, 2020 CreateTransportAnswer(media_description_options.mid, offer_description,
1938 GetTransportOptions(options, audio_content->name), 2021 media_description_options.transport_options,
1939 current_description, bundle_transport != nullptr)); 2022 current_description, bundle_transport != nullptr));
1940 if (!audio_transport) { 2023 if (!audio_transport) {
1941 return false; 2024 return false;
1942 } 2025 }
1943 2026
1944 // Pick codecs based on the requested communications direction in the offer. 2027 // Pick codecs based on the requested communications direction in the offer
1945 const bool wants_send = 2028 // and the selected direction in the answer.
1946 options.HasSendMediaStream(MEDIA_TYPE_AUDIO) || add_legacy_; 2029 // Note these will be filtered one final time in CreateMediaContentAnswer.
1947 auto wants_rtd = RtpTransceiverDirection(wants_send, options.recv_audio); 2030 auto wants_rtd = media_description_options.direction;
1948 auto offer_rtd = 2031 auto offer_rtd = RtpTransceiverDirection::FromMediaContentDirection(
1949 RtpTransceiverDirection::FromMediaContentDirection( 2032 offer_audio_description->direction());
1950 offer_audio->direction());
1951 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd); 2033 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
1952 AudioCodecs audio_codecs = GetAudioCodecsForAnswer(offer_rtd, answer_rtd); 2034 AudioCodecs supported_audio_codecs =
1953 if (!options.vad_enabled) { 2035 GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
1954 StripCNCodecs(&audio_codecs); 2036
1955 } 2037 AudioCodecs filtered_codecs;
1956 2038 AudioCodec existing_codec;
1957 bool bundle_enabled = 2039 // Add the codecs from current content if exists.
1958 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled; 2040 if (current_content) {
2041 RTC_DCHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
2042 const AudioContentDescription* acd =
2043 static_cast<const AudioContentDescription*>(
2044 current_content->description);
2045 for (const AudioCodec& codec : acd->codecs()) {
2046 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
2047 codec, &existing_codec)) {
2048 if (current_description) {
2049 filtered_codecs.push_back(existing_codec);
2050 } else {
2051 filtered_codecs.push_back(codec);
2052 }
2053 }
2054 }
2055 }
2056
2057 // Add other supported audio codecs.
2058 for (const AudioCodec& codec : supported_audio_codecs) {
2059 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
2060 codec, &existing_codec) &&
2061 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2062 codec, nullptr)) {
2063 if (current_description) {
2064 filtered_codecs.push_back(existing_codec);
2065 } else {
2066 filtered_codecs.push_back(codec);
2067 }
2068 }
2069 }
2070
2071 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2072 session_options.bundle_enabled;
1959 std::unique_ptr<AudioContentDescription> audio_answer( 2073 std::unique_ptr<AudioContentDescription> audio_answer(
1960 new AudioContentDescription()); 2074 new AudioContentDescription());
1961 // Do not require or create SDES cryptos if DTLS is used. 2075 // Do not require or create SDES cryptos if DTLS is used.
1962 cricket::SecurePolicy sdes_policy = 2076 cricket::SecurePolicy sdes_policy =
1963 audio_transport->secure() ? cricket::SEC_DISABLED : secure(); 2077 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
1964 if (!CreateMediaContentAnswer( 2078 if (!CreateMediaContentAnswer(
1965 offer_audio, 2079 offer_audio_description, media_description_options, session_options,
1966 options, 2080 filtered_codecs, sdes_policy, GetCryptos(current_content),
1967 audio_codecs, 2081 audio_rtp_extensions_, enable_encrypted_rtp_header_extensions_,
1968 sdes_policy, 2082 current_streams, bundle_enabled, audio_answer.get())) {
1969 GetCryptos(GetFirstAudioContentDescription(current_description)),
1970 audio_rtp_extensions_,
1971 enable_encrypted_rtp_header_extensions_,
1972 current_streams,
1973 add_legacy_,
1974 bundle_enabled,
1975 audio_answer.get())) {
1976 return false; // Fails the session setup. 2083 return false; // Fails the session setup.
1977 } 2084 }
1978 2085
1979 bool secure = bundle_transport ? bundle_transport->description.secure() 2086 bool secure = bundle_transport ? bundle_transport->description.secure()
1980 : audio_transport->secure(); 2087 : audio_transport->secure();
1981 bool rejected = !options.has_audio() || audio_content->rejected || 2088 bool rejected = media_description_options.stopped ||
2089 offer_content->rejected ||
1982 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO, 2090 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
1983 audio_answer->protocol(), secure); 2091 audio_answer->protocol(), secure);
1984 if (!rejected) { 2092 if (!rejected) {
1985 AddTransportAnswer(audio_content->name, *(audio_transport.get()), answer); 2093 AddTransportAnswer(media_description_options.mid, *(audio_transport.get()),
2094 answer);
1986 } else { 2095 } else {
1987 // RFC 3264 2096 LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
1988 // The answer MUST contain the same number of m-lines as the offer. 2097 << "' being rejected in answer.";
1989 LOG(LS_INFO) << "Audio is not supported in the answer."; 2098 }
1990 } 2099
1991 2100 answer->AddContent(media_description_options.mid, offer_content->type,
1992 answer->AddContent(audio_content->name, audio_content->type, rejected, 2101 rejected, audio_answer.release());
1993 audio_answer.release());
1994 return true; 2102 return true;
1995 } 2103 }
1996 2104
1997 bool MediaSessionDescriptionFactory::AddVideoContentForAnswer( 2105 bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
1998 const SessionDescription* offer, 2106 const MediaDescriptionOptions& media_description_options,
1999 const MediaSessionOptions& options, 2107 const MediaSessionOptions& session_options,
2108 const ContentInfo* offer_content,
2109 const SessionDescription* offer_description,
2110 const ContentInfo* current_content,
2000 const SessionDescription* current_description, 2111 const SessionDescription* current_description,
2001 const TransportInfo* bundle_transport, 2112 const TransportInfo* bundle_transport,
2113 const VideoCodecs& video_codecs,
2002 StreamParamsVec* current_streams, 2114 StreamParamsVec* current_streams,
2003 SessionDescription* answer) const { 2115 SessionDescription* answer) const {
2004 const ContentInfo* video_content = GetFirstVideoContent(offer); 2116 const VideoContentDescription* offer_video_description =
2117 static_cast<const VideoContentDescription*>(offer_content->description);
2118
2005 std::unique_ptr<TransportDescription> video_transport( 2119 std::unique_ptr<TransportDescription> video_transport(
2006 CreateTransportAnswer(video_content->name, offer, 2120 CreateTransportAnswer(media_description_options.mid, offer_description,
2007 GetTransportOptions(options, video_content->name), 2121 media_description_options.transport_options,
2008 current_description, bundle_transport != nullptr)); 2122 current_description, bundle_transport != nullptr));
2009 if (!video_transport) { 2123 if (!video_transport) {
2010 return false; 2124 return false;
2011 } 2125 }
2012 2126
2127 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2128 session_options.bundle_enabled;
2129
2013 std::unique_ptr<VideoContentDescription> video_answer( 2130 std::unique_ptr<VideoContentDescription> video_answer(
2014 new VideoContentDescription()); 2131 new VideoContentDescription());
2015 // Do not require or create SDES cryptos if DTLS is used. 2132 // Do not require or create SDES cryptos if DTLS is used.
2016 cricket::SecurePolicy sdes_policy = 2133 cricket::SecurePolicy sdes_policy =
2017 video_transport->secure() ? cricket::SEC_DISABLED : secure(); 2134 video_transport->secure() ? cricket::SEC_DISABLED : secure();
2018 bool bundle_enabled =
2019 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
2020 if (!CreateMediaContentAnswer( 2135 if (!CreateMediaContentAnswer(
2021 static_cast<const VideoContentDescription*>( 2136 offer_video_description, media_description_options, session_options,
2022 video_content->description), 2137 video_codecs, sdes_policy, GetCryptos(current_content),
2023 options, 2138 video_rtp_extensions_, enable_encrypted_rtp_header_extensions_,
2024 video_codecs_, 2139 current_streams, bundle_enabled, video_answer.get())) {
2025 sdes_policy, 2140 return false; // Failed the sessin setup.
2026 GetCryptos(GetFirstVideoContentDescription(current_description)),
2027 video_rtp_extensions_,
2028 enable_encrypted_rtp_header_extensions_,
2029 current_streams,
2030 add_legacy_,
2031 bundle_enabled,
2032 video_answer.get())) {
2033 return false;
2034 } 2141 }
2035 bool secure = bundle_transport ? bundle_transport->description.secure() 2142 bool secure = bundle_transport ? bundle_transport->description.secure()
2036 : video_transport->secure(); 2143 : video_transport->secure();
2037 bool rejected = !options.has_video() || video_content->rejected || 2144 bool rejected = media_description_options.stopped ||
2145 offer_content->rejected ||
2038 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO, 2146 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
2039 video_answer->protocol(), secure); 2147 video_answer->protocol(), secure);
2040 if (!rejected) { 2148 if (!rejected) {
2041 if (!AddTransportAnswer(video_content->name, *(video_transport.get()), 2149 if (!AddTransportAnswer(media_description_options.mid,
2042 answer)) { 2150 *(video_transport.get()), answer)) {
2043 return false; 2151 return false;
2044 } 2152 }
2045 video_answer->set_bandwidth(options.video_bandwidth); 2153 video_answer->set_bandwidth(kAutoBandwidth);
2046 } else { 2154 } else {
2047 // RFC 3264 2155 LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
2048 // The answer MUST contain the same number of m-lines as the offer. 2156 << "' being rejected in answer.";
2049 LOG(LS_INFO) << "Video is not supported in the answer."; 2157 }
2050 } 2158 answer->AddContent(media_description_options.mid, offer_content->type,
2051 answer->AddContent(video_content->name, video_content->type, rejected, 2159 rejected, video_answer.release());
2052 video_answer.release());
2053 return true; 2160 return true;
2054 } 2161 }
2055 2162
2056 bool MediaSessionDescriptionFactory::AddDataContentForAnswer( 2163 bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
2057 const SessionDescription* offer, 2164 const MediaDescriptionOptions& media_description_options,
2058 const MediaSessionOptions& options, 2165 const MediaSessionOptions& session_options,
2166 const ContentInfo* offer_content,
2167 const SessionDescription* offer_description,
2168 const ContentInfo* current_content,
2059 const SessionDescription* current_description, 2169 const SessionDescription* current_description,
2060 const TransportInfo* bundle_transport, 2170 const TransportInfo* bundle_transport,
2171 const DataCodecs& data_codecs,
2061 StreamParamsVec* current_streams, 2172 StreamParamsVec* current_streams,
2062 SessionDescription* answer) const { 2173 SessionDescription* answer) const {
2063 const ContentInfo* data_content = GetFirstDataContent(offer);
2064 std::unique_ptr<TransportDescription> data_transport( 2174 std::unique_ptr<TransportDescription> data_transport(
2065 CreateTransportAnswer(data_content->name, offer, 2175 CreateTransportAnswer(media_description_options.mid, offer_description,
2066 GetTransportOptions(options, data_content->name), 2176 media_description_options.transport_options,
2067 current_description, bundle_transport != nullptr)); 2177 current_description, bundle_transport != nullptr));
2068 if (!data_transport) { 2178 if (!data_transport) {
2069 return false; 2179 return false;
2070 } 2180 }
2071 bool is_sctp = (options.data_channel_type == DCT_SCTP);
2072 std::vector<DataCodec> data_codecs(data_codecs_);
2073 FilterDataCodecs(&data_codecs, is_sctp);
2074 2181
2075 std::unique_ptr<DataContentDescription> data_answer( 2182 std::unique_ptr<DataContentDescription> data_answer(
2076 new DataContentDescription()); 2183 new DataContentDescription());
2077 // Do not require or create SDES cryptos if DTLS is used. 2184 // Do not require or create SDES cryptos if DTLS is used.
2078 cricket::SecurePolicy sdes_policy = 2185 cricket::SecurePolicy sdes_policy =
2079 data_transport->secure() ? cricket::SEC_DISABLED : secure(); 2186 data_transport->secure() ? cricket::SEC_DISABLED : secure();
2080 bool bundle_enabled = 2187 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2081 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled; 2188 session_options.bundle_enabled;
2082 if (!CreateMediaContentAnswer( 2189 if (!CreateMediaContentAnswer(
2083 static_cast<const DataContentDescription*>( 2190 static_cast<const DataContentDescription*>(
2084 data_content->description), 2191 offer_content->description),
2085 options, 2192 media_description_options, session_options, data_codecs, sdes_policy,
2086 data_codecs_, 2193 GetCryptos(current_content), RtpHeaderExtensions(),
2087 sdes_policy, 2194 enable_encrypted_rtp_header_extensions_, current_streams,
2088 GetCryptos(GetFirstDataContentDescription(current_description)), 2195 bundle_enabled, data_answer.get())) {
2089 RtpHeaderExtensions(),
2090 enable_encrypted_rtp_header_extensions_,
2091 current_streams,
2092 add_legacy_,
2093 bundle_enabled,
2094 data_answer.get())) {
2095 return false; // Fails the session setup. 2196 return false; // Fails the session setup.
2096 } 2197 }
2097 2198
2098 // Respond with sctpmap if the offer uses sctpmap. 2199 // Respond with sctpmap if the offer uses sctpmap.
2099 const DataContentDescription* offer_data_description = 2200 const DataContentDescription* offer_data_description =
2100 static_cast<const DataContentDescription*>(data_content->description); 2201 static_cast<const DataContentDescription*>(offer_content->description);
2101 bool offer_uses_sctpmap = offer_data_description->use_sctpmap(); 2202 bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
2102 data_answer->set_use_sctpmap(offer_uses_sctpmap); 2203 data_answer->set_use_sctpmap(offer_uses_sctpmap);
2103 2204
2104 bool secure = bundle_transport ? bundle_transport->description.secure() 2205 bool secure = bundle_transport ? bundle_transport->description.secure()
2105 : data_transport->secure(); 2206 : data_transport->secure();
2106 2207
2107 bool rejected = !options.has_data() || data_content->rejected || 2208 bool rejected = media_description_options.stopped ||
2209 offer_content->rejected ||
2108 !IsMediaProtocolSupported(MEDIA_TYPE_DATA, 2210 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
2109 data_answer->protocol(), secure); 2211 data_answer->protocol(), secure);
2110 if (!rejected) { 2212 if (!rejected) {
2111 data_answer->set_bandwidth(options.data_bandwidth); 2213 data_answer->set_bandwidth(kDataMaxBandwidth);
2112 if (!AddTransportAnswer(data_content->name, *(data_transport.get()), 2214 if (!AddTransportAnswer(media_description_options.mid,
2113 answer)) { 2215 *(data_transport.get()), answer)) {
2114 return false; 2216 return false;
2115 } 2217 }
2116 } else { 2218 } else {
2117 // RFC 3264 2219 // RFC 3264
2118 // The answer MUST contain the same number of m-lines as the offer. 2220 // The answer MUST contain the same number of m-lines as the offer.
2119 LOG(LS_INFO) << "Data is not supported in the answer."; 2221 LOG(LS_INFO) << "Data is not supported in the answer.";
2120 } 2222 }
2121 answer->AddContent(data_content->name, data_content->type, rejected, 2223 answer->AddContent(media_description_options.mid, offer_content->type,
2122 data_answer.release()); 2224 rejected, data_answer.release());
2123 return true; 2225 return true;
2124 } 2226 }
2125 2227
2228 void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
2229 audio_sendrecv_codecs_.clear();
2230 all_audio_codecs_.clear();
2231 // Compute the audio codecs union.
2232 for (const AudioCodec& send : audio_send_codecs_) {
2233 all_audio_codecs_.push_back(send);
2234 if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
2235 send, nullptr)) {
2236 // It doesn't make sense to have an RTX codec we support sending but not
2237 // receiving.
2238 RTC_DCHECK(!IsRtxCodec(send));
2239 }
2240 }
2241 for (const AudioCodec& recv : audio_recv_codecs_) {
2242 if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
2243 recv, nullptr)) {
2244 all_audio_codecs_.push_back(recv);
2245 }
2246 }
2247 // Use NegotiateCodecs to merge our codec lists, since the operation is
2248 // essentially the same. Put send_codecs as the offered_codecs, which is the
2249 // order we'd like to follow. The reasoning is that encoding is usually more
2250 // expensive than decoding, and prioritizing a codec in the send list probably
2251 // means it's a codec we can handle efficiently.
2252 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
2253 &audio_sendrecv_codecs_);
2254 }
2255
2126 bool IsMediaContent(const ContentInfo* content) { 2256 bool IsMediaContent(const ContentInfo* content) {
2127 return (content && 2257 return (content &&
2128 (content->type == NS_JINGLE_RTP || 2258 (content->type == NS_JINGLE_RTP ||
2129 content->type == NS_JINGLE_DRAFT_SCTP)); 2259 content->type == NS_JINGLE_DRAFT_SCTP));
2130 } 2260 }
2131 2261
2132 bool IsAudioContent(const ContentInfo* content) { 2262 bool IsAudioContent(const ContentInfo* content) {
2133 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO); 2263 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
2134 } 2264 }
2135 2265
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
2276 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); 2406 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2277 } 2407 }
2278 2408
2279 DataContentDescription* GetFirstDataContentDescription( 2409 DataContentDescription* GetFirstDataContentDescription(
2280 SessionDescription* sdesc) { 2410 SessionDescription* sdesc) {
2281 return static_cast<DataContentDescription*>( 2411 return static_cast<DataContentDescription*>(
2282 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); 2412 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2283 } 2413 }
2284 2414
2285 } // namespace cricket 2415 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698