JsepSessionImpl.h (11307B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef _JSEPSESSIONIMPL_H_ 6 #define _JSEPSESSIONIMPL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "jsep/JsepCodecDescription.h" 14 #include "jsep/JsepSession.h" 15 #include "jsep/JsepTrack.h" 16 #include "jsep/JsepTransceiver.h" 17 #include "jsep/SsrcGenerator.h" 18 #include "sdp/HybridSdpParser.h" 19 #include "sdp/SdpHelper.h" 20 21 namespace mozilla { 22 23 // JsepSessionImpl members that have default copy c'tors, to simplify the 24 // implementation of the copy c'tor for JsepSessionImpl 25 class JsepSessionCopyableStuff { 26 protected: 27 struct JsepDtlsFingerprint { 28 std::string mAlgorithm; 29 std::vector<uint8_t> mValue; 30 }; 31 32 Maybe<bool> mIsPendingOfferer; 33 Maybe<bool> mIsCurrentOfferer; 34 bool mIceControlling = false; 35 std::string mIceUfrag; 36 std::string mIcePwd; 37 std::string mOldIceUfrag; 38 std::string mOldIcePwd; 39 bool mRemoteIsIceLite = false; 40 std::vector<std::string> mIceOptions; 41 JsepBundlePolicy mBundlePolicy = kBundleBalanced; 42 std::vector<JsepDtlsFingerprint> mDtlsFingerprints; 43 uint64_t mSessionId = 0; 44 uint64_t mSessionVersion = 0; 45 size_t mMidCounter = 0; 46 std::set<std::string> mUsedMids; 47 size_t mTransportIdCounter = 0; 48 std::vector<JsepExtmapMediaType> mRtpExtensions; 49 std::set<uint16_t> mExtmapEntriesEverUsed; 50 std::map<uint16_t, std::string> mExtmapEntriesEverNegotiated; 51 std::string mDefaultRemoteStreamId; 52 std::string mCNAME; 53 // Used to prevent duplicate local SSRCs. Not used to prevent local/remote or 54 // remote-only duplication, which will be important for EKT but not now. 55 std::set<uint32_t> mSsrcs; 56 std::string mLastError; 57 std::vector<std::pair<size_t, std::string>> mLastSdpParsingErrors; 58 bool mEncodeTrackId = true; 59 SsrcGenerator mSsrcGenerator; 60 // !!!NOT INDEXED BY LEVEL!!! The level mapping is done with 61 // JsepTransceiver::mLevel. The keys are UUIDs. 62 std::vector<JsepTransceiver> mTransceivers; 63 // So we can rollback. Not as simple as just going back to the old, though... 64 std::vector<JsepTransceiver> mOldTransceivers; 65 }; 66 67 class JsepSessionImpl : public JsepSession, public JsepSessionCopyableStuff { 68 public: 69 JsepSessionImpl(const std::string& name, UniquePtr<JsepUuidGenerator> uuidgen) 70 : JsepSession(name), 71 mUuidGen(std::move(uuidgen)), 72 mSdpHelper(&mLastError), 73 mParser(new HybridSdpParser()) {} 74 75 JsepSessionImpl(const JsepSessionImpl& aOrig); 76 77 JsepSession* Clone() const override { return new JsepSessionImpl(*this); } 78 79 // Implement JsepSession methods. 80 virtual nsresult Init() override; 81 82 nsresult SetBundlePolicy(JsepBundlePolicy policy) override; 83 84 virtual bool RemoteIsIceLite() const override { return mRemoteIsIceLite; } 85 86 virtual std::vector<std::string> GetIceOptions() const override { 87 return mIceOptions; 88 } 89 90 virtual nsresult AddDtlsFingerprint( 91 const nsACString& algorithm, const std::vector<uint8_t>& value) override; 92 93 virtual nsresult AddRtpExtension( 94 JsepMediaType mediaType, const std::string& extensionName, 95 SdpDirectionAttribute::Direction direction) override; 96 virtual nsresult AddAudioRtpExtension( 97 const std::string& extensionName, 98 SdpDirectionAttribute::Direction direction = 99 SdpDirectionAttribute::Direction::kSendrecv) override; 100 101 virtual nsresult AddVideoRtpExtension( 102 const std::string& extensionName, 103 SdpDirectionAttribute::Direction direction = 104 SdpDirectionAttribute::Direction::kSendrecv) override; 105 106 virtual nsresult AddAudioVideoRtpExtension( 107 const std::string& extensionName, 108 SdpDirectionAttribute::Direction direction = 109 SdpDirectionAttribute::Direction::kSendrecv) override; 110 111 virtual std::vector<UniquePtr<JsepCodecDescription>>& Codecs() override { 112 return mSupportedCodecs; 113 } 114 115 virtual Result CreateOffer(const JsepOfferOptions& options, 116 std::string* offer) override; 117 118 virtual Result CreateAnswer(const JsepAnswerOptions& options, 119 std::string* answer) override; 120 121 virtual std::string GetLocalDescription( 122 JsepDescriptionPendingOrCurrent type) const override; 123 124 virtual std::string GetRemoteDescription( 125 JsepDescriptionPendingOrCurrent type) const override; 126 127 virtual Result SetLocalDescription(JsepSdpType type, 128 const std::string& sdp) override; 129 130 virtual Result SetRemoteDescription(JsepSdpType type, 131 const std::string& sdp) override; 132 133 virtual Result AddRemoteIceCandidate(const std::string& candidate, 134 const std::string& mid, 135 const Maybe<uint16_t>& level, 136 const std::string& ufrag, 137 std::string* transportId) override; 138 139 virtual nsresult AddLocalIceCandidate(const std::string& candidate, 140 const std::string& transportId, 141 const std::string& ufrag, 142 uint16_t* level, std::string* mid, 143 bool* skipped) override; 144 145 virtual nsresult UpdateDefaultCandidate( 146 const std::string& defaultCandidateAddr, uint16_t defaultCandidatePort, 147 const std::string& defaultRtcpCandidateAddr, 148 uint16_t defaultRtcpCandidatePort, 149 const std::string& transportId) override; 150 151 virtual nsresult Close() override; 152 153 virtual const std::string GetLastError() const override; 154 155 virtual const std::vector<std::pair<size_t, std::string>>& 156 GetLastSdpParsingErrors() const override; 157 158 virtual bool IsIceControlling() const override { return mIceControlling; } 159 160 virtual Maybe<bool> IsPendingOfferer() const override { 161 return mIsPendingOfferer; 162 } 163 164 virtual Maybe<bool> IsCurrentOfferer() const override { 165 return mIsCurrentOfferer; 166 } 167 168 virtual bool IsIceRestarting() const override { 169 return !mOldIceUfrag.empty(); 170 } 171 172 virtual std::set<std::pair<std::string, std::string>> GetLocalIceCredentials() 173 const override; 174 175 virtual void AddTransceiver(const JsepTransceiver& transceiver) override; 176 177 virtual bool CheckNegotiationNeeded() const override; 178 179 virtual void SetDefaultCodecs( 180 const std::vector<UniquePtr<JsepCodecDescription>>& aPreferredCodecs) 181 override; 182 183 private: 184 friend class JsepSessionTest; 185 virtual const std::vector<JsepTransceiver>& GetTransceivers() const override { 186 return mTransceivers; 187 } 188 189 virtual std::vector<JsepTransceiver>& GetTransceivers() override { 190 return mTransceivers; 191 } 192 193 // Non-const so it can set mLastError 194 nsresult CreateGenericSDP(UniquePtr<Sdp>* sdp); 195 void AddExtmap(SdpMediaSection* msection); 196 std::vector<SdpExtmapAttributeList::Extmap> GetRtpExtensions( 197 const SdpMediaSection& msection); 198 std::string GetNewMid(); 199 200 void AddCommonExtmaps(const SdpMediaSection& remoteMsection, 201 SdpMediaSection* msection); 202 uint16_t GetNeverUsedExtmapEntry(); 203 nsresult SetupIds(); 204 void SetState(JsepSignalingState state); 205 // Non-const so it can set mLastError 206 nsresult ParseSdp(const std::string& sdp, UniquePtr<Sdp>* parsedp); 207 nsresult SetLocalDescriptionOffer(UniquePtr<Sdp> offer); 208 nsresult SetLocalDescriptionAnswer(JsepSdpType type, UniquePtr<Sdp> answer); 209 nsresult SetRemoteDescriptionOffer(UniquePtr<Sdp> offer); 210 nsresult SetRemoteDescriptionAnswer(JsepSdpType type, UniquePtr<Sdp> answer); 211 nsresult ValidateLocalDescription(const Sdp& description, JsepSdpType type); 212 nsresult ValidateRemoteDescription(const Sdp& description); 213 nsresult ValidateOffer(const Sdp& offer); 214 nsresult ValidateAnswer(const Sdp& offer, const Sdp& answer); 215 nsresult UpdateTransceiversFromRemoteDescription(const Sdp& remote); 216 Maybe<JsepTransceiver> GetTransceiverForLevel(size_t level) const; 217 Maybe<JsepTransceiver> GetTransceiverForMid(const std::string& mid) const; 218 Maybe<JsepTransceiver> GetTransceiverForLocal(size_t level); 219 Maybe<JsepTransceiver> GetTransceiverForRemote( 220 const SdpMediaSection& msection); 221 Maybe<JsepTransceiver> GetTransceiverWithTransport( 222 const std::string& transportId) const; 223 // The w3c and IETF specs have a lot of "magical" behavior that happens when 224 // addTrack is used. This was a deliberate design choice. Sadface. 225 Maybe<JsepTransceiver> FindUnassociatedTransceiver( 226 SdpMediaSection::MediaType type, bool magic); 227 // Called for rollback of local description 228 void RollbackLocalOffer(); 229 // Called for rollback of remote description 230 void RollbackRemoteOffer(); 231 nsresult HandleNegotiatedSession(const UniquePtr<Sdp>& local, 232 const UniquePtr<Sdp>& remote); 233 nsresult AddTransportAttributes(SdpMediaSection* msection, 234 SdpSetupAttribute::Role dtlsRole); 235 nsresult CopyPreviousTransportParams(const Sdp& oldAnswer, 236 const Sdp& offerersPreviousSdp, 237 const Sdp& newOffer, Sdp* newLocal); 238 void EnsureMsid(Sdp* remote); 239 void SetupBundle(Sdp* sdp) const; 240 nsresult CreateOfferMsection(const JsepOfferOptions& options, 241 JsepTransceiver& transceiver, Sdp* local); 242 nsresult CreateAnswerMsection(const JsepAnswerOptions& options, 243 JsepTransceiver& transceiver, 244 const SdpMediaSection& remoteMsection, 245 Sdp* sdp); 246 nsresult DetermineAnswererSetupRole(const SdpMediaSection& remoteMsection, 247 SdpSetupAttribute::Role* rolep); 248 nsresult MakeNegotiatedTransceiver(const SdpMediaSection& remote, 249 const SdpMediaSection& local, 250 JsepTransceiver& transceiverOut); 251 void EnsureHasOwnTransport(const SdpMediaSection& msection, 252 JsepTransceiver& transceiver); 253 void CopyBundleTransports(); 254 255 nsresult FinalizeTransport(const SdpAttributeList& remote, 256 const SdpAttributeList& answer, 257 JsepTransport* transport) const; 258 259 nsresult GetNegotiatedBundledMids(SdpHelper::BundledMids* bundledMids); 260 261 mozilla::Sdp* GetParsedLocalDescription( 262 JsepDescriptionPendingOrCurrent type) const; 263 mozilla::Sdp* GetParsedRemoteDescription( 264 JsepDescriptionPendingOrCurrent type) const; 265 const Sdp* GetAnswer() const; 266 void SetIceRestarting(bool restarting); 267 268 void InitTransceiver(JsepTransceiver& aTransceiver); 269 270 UniquePtr<JsepUuidGenerator> mUuidGen; 271 UniquePtr<Sdp> mGeneratedOffer; // Created but not set. 272 UniquePtr<Sdp> mGeneratedAnswer; // Created but not set. 273 UniquePtr<Sdp> mCurrentLocalDescription; 274 UniquePtr<Sdp> mCurrentRemoteDescription; 275 UniquePtr<Sdp> mPendingLocalDescription; 276 UniquePtr<Sdp> mPendingRemoteDescription; 277 std::vector<UniquePtr<JsepCodecDescription>> mSupportedCodecs; 278 SdpHelper mSdpHelper; 279 UniquePtr<SdpParser> mParser; 280 }; 281 282 } // namespace mozilla 283 284 #endif