blob: f5eba64e96f9d234f30e2f06039231ee69125213 [file] [log] [blame]
Harald Alvestrand9dea3932021-06-10 06:03:061/*
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 Alvestrandde22ab22021-06-17 10:45:3314#include <functional>
Harald Alvestrand9dea3932021-06-10 06:03:0615#include <map>
16#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
Harald Alvestrandc24a2182022-02-23 13:44:5921#include "api/jsep.h"
Taylor Brandstetter8591eff2021-08-11 21:56:3822#include "api/peer_connection_interface.h"
Harald Alvestrandde22ab22021-06-17 10:45:3323#include "api/sequence_checker.h"
Harald Alvestrand9dea3932021-06-10 06:03:0624#include "pc/jsep_transport.h"
25#include "pc/session_description.h"
Harald Alvestrandde22ab22021-06-17 10:45:3326#include "rtc_base/checks.h"
27#include "rtc_base/system/no_unique_address.h"
28#include "rtc_base/thread_annotations.h"
Harald Alvestrand9dea3932021-06-10 06:03:0629
30namespace 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.
44class BundleManager {
45 public:
Taylor Brandstetter8591eff2021-08-11 21:56:3846 explicit BundleManager(PeerConnectionInterface::BundlePolicy bundle_policy)
Tommic8482682023-03-23 21:20:3947 : bundle_policy_(bundle_policy) {}
Harald Alvestrand9dea3932021-06-10 06:03:0648 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 Alvestrand11b92cf2021-06-17 14:03:0953 // 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 Alvestrand9dea3932021-06-10 06:03:0659 // Update the groups description. This completely replaces the group
60 // description with the one from the SessionDescription.
Taylor Brandstetter8591eff2021-08-11 21:56:3861 void Update(const cricket::SessionDescription* description, SdpType type);
Harald Alvestrand9dea3932021-06-10 06:03:0662 // 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 Brandstetter8591eff2021-08-11 21:56:3867 // Roll back to previous stable state.
68 void Rollback();
69 // Commit current bundle groups.
70 void Commit();
Harald Alvestrand9dea3932021-06-10 06:03:0671
72 private:
Taylor Brandstetter8591eff2021-08-11 21:56:3873 // Recalculate established_bundle_groups_by_mid_ from bundle_groups_.
74 void RefreshEstablishedBundleGroupsByMid() RTC_RUN_ON(sequence_checker_);
75
Tommic8482682023-03-23 21:20:3976 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_{
77 SequenceChecker::kDetached};
Taylor Brandstetter8591eff2021-08-11 21:56:3878 PeerConnectionInterface::BundlePolicy bundle_policy_;
Harald Alvestrand9dea3932021-06-10 06:03:0679 std::vector<std::unique_ptr<cricket::ContentGroup>> bundle_groups_
80 RTC_GUARDED_BY(sequence_checker_);
Taylor Brandstetter8591eff2021-08-11 21:56:3881 std::vector<std::unique_ptr<cricket::ContentGroup>> stable_bundle_groups_
82 RTC_GUARDED_BY(sequence_checker_);
Harald Alvestrand11b92cf2021-06-17 14:03:0983 std::map<std::string, cricket::ContentGroup*>
84 established_bundle_groups_by_mid_;
Harald Alvestrand9dea3932021-06-10 06:03:0685};
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.
91class 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),
Tommic8482682023-03-23 21:20:3998 state_change_callback_(state_change_callback) {}
Harald Alvestrand9dea3932021-06-10 06:03:0699
100 void RegisterTransport(const std::string& mid,
101 std::unique_ptr<cricket::JsepTransport> transport);
Taylor Brandstetter8591eff2021-08-11 21:56:38102 // Returns all transports, including those not currently mapped to any MID
103 // because they're being kept alive in case of rollback.
Harald Alvestrand9dea3932021-06-10 06:03:06104 std::vector<cricket::JsepTransport*> Transports();
Taylor Brandstetter8591eff2021-08-11 21:56:38105 // Only returns transports currently mapped to a MID.
106 std::vector<cricket::JsepTransport*> ActiveTransports();
Harald Alvestrand9dea3932021-06-10 06:03:06107 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 Alvestrand8f429922022-05-04 10:32:30116 cricket::JsepTransport* GetTransportForMid(absl::string_view mid);
117 const cricket::JsepTransport* GetTransportForMid(absl::string_view mid) const;
Harald Alvestrand4bb81ac2021-06-11 13:04:59118 // Set transport for a MID. This may destroy a transport if it is no
119 // longer in use.
Harald Alvestrand9dea3932021-06-10 06:03:06120 bool SetTransportForMid(const std::string& mid,
121 cricket::JsepTransport* jsep_transport);
Harald Alvestrand4bb81ac2021-06-11 13:04:59122 // Remove a transport for a MID. This may destroy a transport if it is
123 // no longer in use.
Harald Alvestrand9dea3932021-06-10 06:03:06124 void RemoveTransportForMid(const std::string& mid);
Taylor Brandstetter8591eff2021-08-11 21:56:38125 // 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 Alvestrand9dea3932021-06-10 06:03:06130 void CommitTransports();
Taylor Brandstetter8591eff2021-08-11 21:56:38131
132 private:
Björn Tereliusdc364e52021-07-27 22:03:35133 // Returns true if any mid currently maps to this transport.
Taylor Brandstetterd2b885f2021-07-23 00:41:55134 bool TransportInUse(cricket::JsepTransport* jsep_transport) const;
135
Taylor Brandstetter8591eff2021-08-11 21:56:38136 // 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 Alvestrand4bb81ac2021-06-11 13:04:59142 void MaybeDestroyJsepTransport(cricket::JsepTransport* transport);
143
Taylor Brandstetter8591eff2021-08-11 21:56:38144 // Destroys all transports that are no longer in use.
145 void DestroyUnusedTransports();
146
Harald Alvestrand4bb81ac2021-06-11 13:04:59147 bool IsConsistent(); // For testing only: Verify internal structure.
148
Tommic8482682023-03-23 21:20:39149 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_{
150 SequenceChecker::kDetached};
Harald Alvestrand9dea3932021-06-10 06:03:06151 // 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 Brandstetter8591eff2021-08-11 21:56:38159 // 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 Alvestrand9dea3932021-06-10 06:03:06163 // 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_