Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef PC_JSEP_TRANSPORT_COLLECTION_H_ |
| 12 | #define PC_JSEP_TRANSPORT_COLLECTION_H_ |
| 13 | |
Harald Alvestrand | de22ab2 | 2021-06-17 10:45:33 | [diff] [blame] | 14 | #include <functional> |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 21 | #include "api/jsep.h" |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 22 | #include "api/peer_connection_interface.h" |
Harald Alvestrand | de22ab2 | 2021-06-17 10:45:33 | [diff] [blame] | 23 | #include "api/sequence_checker.h" |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 24 | #include "pc/jsep_transport.h" |
| 25 | #include "pc/session_description.h" |
Harald Alvestrand | de22ab2 | 2021-06-17 10:45:33 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/system/no_unique_address.h" |
| 28 | #include "rtc_base/thread_annotations.h" |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | // This class manages information about RFC 8843 BUNDLE bundles |
| 33 | // in SDP descriptions. |
| 34 | |
| 35 | // This is a work-in-progress. Planned steps: |
| 36 | // 1) Move all Bundle-related data structures from JsepTransport |
| 37 | // into this class. |
| 38 | // 2) Move all Bundle-related functions into this class. |
| 39 | // 3) Move remaining Bundle-related logic into this class. |
| 40 | // Make data members private. |
| 41 | // 4) Refine interface to have comprehensible semantics. |
| 42 | // 5) Add unit tests. |
| 43 | // 6) Change the logic to do what's right. |
| 44 | class BundleManager { |
| 45 | public: |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 46 | explicit BundleManager(PeerConnectionInterface::BundlePolicy bundle_policy) |
Tommi | c848268 | 2023-03-23 21:20:39 | [diff] [blame] | 47 | : bundle_policy_(bundle_policy) {} |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 48 | const std::vector<std::unique_ptr<cricket::ContentGroup>>& bundle_groups() |
| 49 | const { |
| 50 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 51 | return bundle_groups_; |
| 52 | } |
Harald Alvestrand | 11b92cf | 2021-06-17 14:03:09 | [diff] [blame] | 53 | // Lookup a bundle group by a member mid name. |
| 54 | const cricket::ContentGroup* LookupGroupByMid(const std::string& mid) const; |
| 55 | cricket::ContentGroup* LookupGroupByMid(const std::string& mid); |
| 56 | // Returns true if the MID is the first item of a group, or if |
| 57 | // the MID is not a member of a group. |
| 58 | bool IsFirstMidInGroup(const std::string& mid) const; |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 59 | // Update the groups description. This completely replaces the group |
| 60 | // description with the one from the SessionDescription. |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 61 | void Update(const cricket::SessionDescription* description, SdpType type); |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 62 | // Delete a MID from the group that contains it. |
| 63 | void DeleteMid(const cricket::ContentGroup* bundle_group, |
| 64 | const std::string& mid); |
| 65 | // Delete a group. |
| 66 | void DeleteGroup(const cricket::ContentGroup* bundle_group); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 67 | // Roll back to previous stable state. |
| 68 | void Rollback(); |
| 69 | // Commit current bundle groups. |
| 70 | void Commit(); |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 71 | |
| 72 | private: |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 73 | // Recalculate established_bundle_groups_by_mid_ from bundle_groups_. |
| 74 | void RefreshEstablishedBundleGroupsByMid() RTC_RUN_ON(sequence_checker_); |
| 75 | |
Tommi | c848268 | 2023-03-23 21:20:39 | [diff] [blame] | 76 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_{ |
| 77 | SequenceChecker::kDetached}; |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 78 | PeerConnectionInterface::BundlePolicy bundle_policy_; |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 79 | std::vector<std::unique_ptr<cricket::ContentGroup>> bundle_groups_ |
| 80 | RTC_GUARDED_BY(sequence_checker_); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 81 | std::vector<std::unique_ptr<cricket::ContentGroup>> stable_bundle_groups_ |
| 82 | RTC_GUARDED_BY(sequence_checker_); |
Harald Alvestrand | 11b92cf | 2021-06-17 14:03:09 | [diff] [blame] | 83 | std::map<std::string, cricket::ContentGroup*> |
| 84 | established_bundle_groups_by_mid_; |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | // This class keeps the mapping of MIDs to transports. |
| 88 | // It is pulled out here because a lot of the code that deals with |
| 89 | // bundles end up modifying this map, and the two need to be consistent; |
| 90 | // the managers may merge. |
| 91 | class JsepTransportCollection { |
| 92 | public: |
| 93 | JsepTransportCollection(std::function<bool(const std::string& mid, |
| 94 | cricket::JsepTransport* transport)> |
| 95 | map_change_callback, |
| 96 | std::function<void()> state_change_callback) |
| 97 | : map_change_callback_(map_change_callback), |
Tommi | c848268 | 2023-03-23 21:20:39 | [diff] [blame] | 98 | state_change_callback_(state_change_callback) {} |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 99 | |
| 100 | void RegisterTransport(const std::string& mid, |
| 101 | std::unique_ptr<cricket::JsepTransport> transport); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 102 | // Returns all transports, including those not currently mapped to any MID |
| 103 | // because they're being kept alive in case of rollback. |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 104 | std::vector<cricket::JsepTransport*> Transports(); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 105 | // Only returns transports currently mapped to a MID. |
| 106 | std::vector<cricket::JsepTransport*> ActiveTransports(); |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 107 | void DestroyAllTransports(); |
| 108 | // Lookup a JsepTransport by the MID that was used to register it. |
| 109 | cricket::JsepTransport* GetTransportByName(const std::string& mid); |
| 110 | const cricket::JsepTransport* GetTransportByName( |
| 111 | const std::string& mid) const; |
| 112 | // Lookup a JsepTransport by any MID that refers to it. |
| 113 | cricket::JsepTransport* GetTransportForMid(const std::string& mid); |
| 114 | const cricket::JsepTransport* GetTransportForMid( |
| 115 | const std::string& mid) const; |
Harald Alvestrand | 8f42992 | 2022-05-04 10:32:30 | [diff] [blame] | 116 | cricket::JsepTransport* GetTransportForMid(absl::string_view mid); |
| 117 | const cricket::JsepTransport* GetTransportForMid(absl::string_view mid) const; |
Harald Alvestrand | 4bb81ac | 2021-06-11 13:04:59 | [diff] [blame] | 118 | // Set transport for a MID. This may destroy a transport if it is no |
| 119 | // longer in use. |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 120 | bool SetTransportForMid(const std::string& mid, |
| 121 | cricket::JsepTransport* jsep_transport); |
Harald Alvestrand | 4bb81ac | 2021-06-11 13:04:59 | [diff] [blame] | 122 | // Remove a transport for a MID. This may destroy a transport if it is |
| 123 | // no longer in use. |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 124 | void RemoveTransportForMid(const std::string& mid); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 125 | // Roll back to previous stable mid-to-transport mappings. |
| 126 | bool RollbackTransports(); |
| 127 | // Commit pending mid-transport mappings (rollback is no longer possible), |
| 128 | // and destroy unused transports because we know now we'll never need them |
| 129 | // again. |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 130 | void CommitTransports(); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 131 | |
| 132 | private: |
Björn Terelius | dc364e5 | 2021-07-27 22:03:35 | [diff] [blame] | 133 | // Returns true if any mid currently maps to this transport. |
Taylor Brandstetter | d2b885f | 2021-07-23 00:41:55 | [diff] [blame] | 134 | bool TransportInUse(cricket::JsepTransport* jsep_transport) const; |
| 135 | |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 136 | // Returns true if any mid in the last stable mapping maps to this transport, |
| 137 | // meaning it should be kept alive in case of rollback. |
| 138 | bool TransportNeededForRollback(cricket::JsepTransport* jsep_transport) const; |
| 139 | |
| 140 | // Destroy a transport if it's no longer in use. This includes whether it |
| 141 | // will be needed in case of rollback. |
Harald Alvestrand | 4bb81ac | 2021-06-11 13:04:59 | [diff] [blame] | 142 | void MaybeDestroyJsepTransport(cricket::JsepTransport* transport); |
| 143 | |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 144 | // Destroys all transports that are no longer in use. |
| 145 | void DestroyUnusedTransports(); |
| 146 | |
Harald Alvestrand | 4bb81ac | 2021-06-11 13:04:59 | [diff] [blame] | 147 | bool IsConsistent(); // For testing only: Verify internal structure. |
| 148 | |
Tommi | c848268 | 2023-03-23 21:20:39 | [diff] [blame] | 149 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_{ |
| 150 | SequenceChecker::kDetached}; |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 151 | // This member owns the JSEP transports. |
| 152 | std::map<std::string, std::unique_ptr<cricket::JsepTransport>> |
| 153 | jsep_transports_by_name_ RTC_GUARDED_BY(sequence_checker_); |
| 154 | |
| 155 | // This keeps track of the mapping between media section |
| 156 | // (BaseChannel/SctpTransport) and the JsepTransport underneath. |
| 157 | std::map<std::string, cricket::JsepTransport*> mid_to_transport_ |
| 158 | RTC_GUARDED_BY(sequence_checker_); |
Taylor Brandstetter | 8591eff | 2021-08-11 21:56:38 | [diff] [blame] | 159 | // A snapshot of mid_to_transport_ at the last stable state. Used for |
| 160 | // rollback. |
| 161 | std::map<std::string, cricket::JsepTransport*> stable_mid_to_transport_ |
| 162 | RTC_GUARDED_BY(sequence_checker_); |
Harald Alvestrand | 9dea393 | 2021-06-10 06:03:06 | [diff] [blame] | 163 | // Callback used to inform subscribers of altered transports. |
| 164 | const std::function<bool(const std::string& mid, |
| 165 | cricket::JsepTransport* transport)> |
| 166 | map_change_callback_; |
| 167 | // Callback used to inform subscribers of possibly altered state. |
| 168 | const std::function<void()> state_change_callback_; |
| 169 | }; |
| 170 | |
| 171 | } // namespace webrtc |
| 172 | |
| 173 | #endif // PC_JSEP_TRANSPORT_COLLECTION_H_ |