SdpMediaSection.cpp (5503B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "sdp/SdpMediaSection.h" 8 9 namespace mozilla { 10 const SdpFmtpAttributeList::Parameters* SdpMediaSection::FindFmtp( 11 const std::string& pt) const { 12 const SdpAttributeList& attrs = GetAttributeList(); 13 14 if (attrs.HasAttribute(SdpAttribute::kFmtpAttribute)) { 15 for (auto& fmtpAttr : attrs.GetFmtp().mFmtps) { 16 if (fmtpAttr.format == pt && fmtpAttr.parameters) { 17 return fmtpAttr.parameters.get(); 18 } 19 } 20 } 21 return nullptr; 22 } 23 24 void SdpMediaSection::SetFmtp(const SdpFmtpAttributeList::Fmtp& fmtpToSet) { 25 UniquePtr<SdpFmtpAttributeList> fmtps(new SdpFmtpAttributeList); 26 27 if (GetAttributeList().HasAttribute(SdpAttribute::kFmtpAttribute)) { 28 *fmtps = GetAttributeList().GetFmtp(); 29 } 30 31 bool found = false; 32 for (SdpFmtpAttributeList::Fmtp& fmtp : fmtps->mFmtps) { 33 if (fmtp.format == fmtpToSet.format) { 34 fmtp = fmtpToSet; 35 found = true; 36 } 37 } 38 39 if (!found) { 40 fmtps->mFmtps.push_back(fmtpToSet); 41 } 42 43 GetAttributeList().SetAttribute(fmtps.release()); 44 } 45 46 void SdpMediaSection::RemoveFmtp(const std::string& pt) { 47 UniquePtr<SdpFmtpAttributeList> fmtps(new SdpFmtpAttributeList); 48 49 SdpAttributeList& attrList = GetAttributeList(); 50 if (attrList.HasAttribute(SdpAttribute::kFmtpAttribute)) { 51 *fmtps = attrList.GetFmtp(); 52 } 53 54 for (size_t i = 0; i < fmtps->mFmtps.size(); ++i) { 55 if (pt == fmtps->mFmtps[i].format) { 56 fmtps->mFmtps.erase(fmtps->mFmtps.begin() + i); 57 break; 58 } 59 } 60 61 attrList.SetAttribute(fmtps.release()); 62 } 63 64 const SdpRtpmapAttributeList::Rtpmap* SdpMediaSection::FindRtpmap( 65 const std::string& pt) const { 66 auto& attrs = GetAttributeList(); 67 if (!attrs.HasAttribute(SdpAttribute::kRtpmapAttribute)) { 68 return nullptr; 69 } 70 71 const SdpRtpmapAttributeList& rtpmap = attrs.GetRtpmap(); 72 if (!rtpmap.HasEntry(pt)) { 73 return nullptr; 74 } 75 76 return &rtpmap.GetEntry(pt); 77 } 78 79 const SdpSctpmapAttributeList::Sctpmap* SdpMediaSection::GetSctpmap() const { 80 auto& attrs = GetAttributeList(); 81 if (!attrs.HasAttribute(SdpAttribute::kSctpmapAttribute)) { 82 return nullptr; 83 } 84 85 const SdpSctpmapAttributeList& sctpmap = attrs.GetSctpmap(); 86 if (sctpmap.mSctpmaps.empty()) { 87 return nullptr; 88 } 89 90 return &sctpmap.GetFirstEntry(); 91 } 92 93 uint32_t SdpMediaSection::GetSctpPort() const { 94 auto& attrs = GetAttributeList(); 95 if (!attrs.HasAttribute(SdpAttribute::kSctpPortAttribute)) { 96 return 0; 97 } 98 99 return attrs.GetSctpPort(); 100 } 101 102 bool SdpMediaSection::GetMaxMessageSize(uint32_t* size) const { 103 *size = 0; 104 105 auto& attrs = GetAttributeList(); 106 if (!attrs.HasAttribute(SdpAttribute::kMaxMessageSizeAttribute)) { 107 return false; 108 } 109 110 *size = attrs.GetMaxMessageSize(); 111 return true; 112 } 113 114 bool SdpMediaSection::HasRtcpFb(const std::string& pt, 115 SdpRtcpFbAttributeList::Type type, 116 const std::string& subType) const { 117 const SdpAttributeList& attrs(GetAttributeList()); 118 119 if (!attrs.HasAttribute(SdpAttribute::kRtcpFbAttribute)) { 120 return false; 121 } 122 123 for (auto& rtcpfb : attrs.GetRtcpFb().mFeedbacks) { 124 if (rtcpfb.type == type) { 125 if (rtcpfb.pt == "*" || rtcpfb.pt == pt) { 126 if (rtcpfb.parameter == subType) { 127 return true; 128 } 129 } 130 } 131 } 132 133 return false; 134 } 135 136 SdpRtcpFbAttributeList SdpMediaSection::GetRtcpFbs() const { 137 SdpRtcpFbAttributeList result; 138 if (GetAttributeList().HasAttribute(SdpAttribute::kRtcpFbAttribute)) { 139 result = GetAttributeList().GetRtcpFb(); 140 } 141 return result; 142 } 143 144 void SdpMediaSection::SetRtcpFbs(const SdpRtcpFbAttributeList& rtcpfbs) { 145 if (rtcpfbs.mFeedbacks.empty()) { 146 GetAttributeList().RemoveAttribute(SdpAttribute::kRtcpFbAttribute); 147 return; 148 } 149 150 GetAttributeList().SetAttribute(new SdpRtcpFbAttributeList(rtcpfbs)); 151 } 152 153 void SdpMediaSection::SetSsrcs(const std::vector<uint32_t>& ssrcs, 154 const std::string& cname) { 155 if (ssrcs.empty()) { 156 GetAttributeList().RemoveAttribute(SdpAttribute::kSsrcAttribute); 157 return; 158 } 159 160 UniquePtr<SdpSsrcAttributeList> ssrcAttr(new SdpSsrcAttributeList); 161 for (auto ssrc : ssrcs) { 162 // When using ssrc attributes, we are required to at least have a cname. 163 // (See https://tools.ietf.org/html/rfc5576#section-6.1) 164 std::string cnameAttr("cname:"); 165 cnameAttr += cname; 166 ssrcAttr->PushEntry(ssrc, cnameAttr); 167 } 168 169 GetAttributeList().SetAttribute(ssrcAttr.release()); 170 } 171 172 void SdpMediaSection::AddMsid(const std::string& id, 173 const std::string& appdata) { 174 UniquePtr<SdpMsidAttributeList> msids(new SdpMsidAttributeList); 175 if (GetAttributeList().HasAttribute(SdpAttribute::kMsidAttribute)) { 176 msids->mMsids = GetAttributeList().GetMsid().mMsids; 177 } 178 msids->PushEntry(id, appdata); 179 GetAttributeList().SetAttribute(msids.release()); 180 } 181 182 const SdpRidAttributeList::Rid* SdpMediaSection::FindRid( 183 const std::string& id) const { 184 if (!GetAttributeList().HasAttribute(SdpAttribute::kRidAttribute)) { 185 return nullptr; 186 } 187 188 for (const auto& rid : GetAttributeList().GetRid().mRids) { 189 if (rid.id == id) { 190 return &rid; 191 } 192 } 193 194 return nullptr; 195 } 196 197 } // namespace mozilla