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

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

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

Powered by Google App Engine
This is Rietveld 408576698