Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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_TRANSCEIVER_LIST_H_ |
| 12 | #define PC_TRANSCEIVER_LIST_H_ |
| 13 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <map> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 18 | #include <optional> |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 22 | #include "api/media_types.h" |
| 23 | #include "api/rtc_error.h" |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 24 | #include "api/rtp_parameters.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 25 | #include "api/rtp_sender_interface.h" |
| 26 | #include "api/scoped_refptr.h" |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 27 | #include "api/sequence_checker.h" |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 28 | #include "pc/rtp_transceiver.h" |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 29 | #include "rtc_base/checks.h" |
| 30 | #include "rtc_base/system/no_unique_address.h" |
| 31 | #include "rtc_base/thread_annotations.h" |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | typedef rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 36 | RtpTransceiverProxyRefPtr; |
| 37 | |
| 38 | // Captures partial state to be used for rollback. Applicable only in |
| 39 | // Unified Plan. |
| 40 | class TransceiverStableState { |
| 41 | public: |
| 42 | TransceiverStableState() {} |
| 43 | void set_newly_created(); |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 44 | void SetMSectionIfUnset(std::optional<std::string> mid, |
| 45 | std::optional<size_t> mline_index); |
Henrik Boström | 0a16276 | 2022-05-02 13:47:52 | [diff] [blame] | 46 | void SetRemoteStreamIds(const std::vector<std::string>& ids); |
Eldar Rello | 950d6b9 | 2021-04-06 19:38:00 | [diff] [blame] | 47 | void SetInitSendEncodings( |
| 48 | const std::vector<RtpEncodingParameters>& encodings); |
Henrik Boström | 0a16276 | 2022-05-02 13:47:52 | [diff] [blame] | 49 | void SetFiredDirection( |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 50 | std::optional<RtpTransceiverDirection> fired_direction) { |
Henrik Boström | 0a16276 | 2022-05-02 13:47:52 | [diff] [blame] | 51 | fired_direction_ = fired_direction; |
| 52 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 53 | std::optional<std::string> mid() const { return mid_; } |
| 54 | std::optional<size_t> mline_index() const { return mline_index_; } |
| 55 | std::optional<std::vector<std::string>> remote_stream_ids() const { |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 56 | return remote_stream_ids_; |
| 57 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 58 | std::optional<std::vector<RtpEncodingParameters>> init_send_encodings() |
Eldar Rello | 950d6b9 | 2021-04-06 19:38:00 | [diff] [blame] | 59 | const { |
| 60 | return init_send_encodings_; |
| 61 | } |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 62 | bool has_m_section() const { return has_m_section_; } |
| 63 | bool newly_created() const { return newly_created_; } |
Henrik Boström | 0a16276 | 2022-05-02 13:47:52 | [diff] [blame] | 64 | bool did_set_fired_direction() const { return fired_direction_.has_value(); } |
| 65 | // Because fired_direction() is nullable, did_set_fired_direction() is used to |
| 66 | // distinguish beteen "no value" and "null value". |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 67 | std::optional<RtpTransceiverDirection> fired_direction() const { |
Henrik Boström | 0a16276 | 2022-05-02 13:47:52 | [diff] [blame] | 68 | RTC_DCHECK(did_set_fired_direction()); |
| 69 | return fired_direction_.value(); |
| 70 | } |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 71 | |
| 72 | private: |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 73 | std::optional<std::string> mid_; |
| 74 | std::optional<size_t> mline_index_; |
| 75 | std::optional<std::vector<std::string>> remote_stream_ids_; |
| 76 | std::optional<std::vector<RtpEncodingParameters>> init_send_encodings_; |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 77 | // Indicates that mid value from stable state has been captured and |
| 78 | // that rollback has to restore the transceiver. Also protects against |
| 79 | // subsequent overwrites. |
| 80 | bool has_m_section_ = false; |
| 81 | // Indicates that the transceiver was created as part of applying a |
| 82 | // description to track potential need for removing transceiver during |
| 83 | // rollback. |
| 84 | bool newly_created_ = false; |
Henrik Boström | 0a16276 | 2022-05-02 13:47:52 | [diff] [blame] | 85 | // `fired_direction_` is nullable, so an optional of an optional is used to |
| 86 | // distinguish between null and not set (sorry if this hurts your eyes). |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 87 | std::optional<std::optional<RtpTransceiverDirection>> fired_direction_; |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 88 | }; |
| 89 | |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 90 | // This class encapsulates the active list of transceivers on a |
| 91 | // PeerConnection, and offers convenient functions on that list. |
| 92 | // It is a single-thread class; all operations must be performed |
| 93 | // on the same thread. |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 94 | class TransceiverList { |
| 95 | public: |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 96 | // Returns a copy of the currently active list of transceivers. The |
| 97 | // list consists of rtc::scoped_refptrs, which will keep the transceivers |
| 98 | // from being deallocated, even if they are removed from the TransceiverList. |
| 99 | std::vector<RtpTransceiverProxyRefPtr> List() const { |
| 100 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 101 | return transceivers_; |
| 102 | } |
| 103 | // As above, but does not check thread ownership. Unsafe. |
| 104 | // TODO(bugs.webrtc.org/12692): Refactor and remove |
| 105 | std::vector<RtpTransceiverProxyRefPtr> UnsafeList() const { |
| 106 | return transceivers_; |
| 107 | } |
| 108 | |
| 109 | // Returns a list of the internal() pointers of the currently active list |
| 110 | // of transceivers. These raw pointers are not thread-safe, so need to |
| 111 | // be consumed on the same thread. |
| 112 | std::vector<RtpTransceiver*> ListInternal() const; |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 113 | |
| 114 | void Add(RtpTransceiverProxyRefPtr transceiver) { |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 115 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 116 | transceivers_.push_back(transceiver); |
| 117 | } |
| 118 | void Remove(RtpTransceiverProxyRefPtr transceiver) { |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 119 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 120 | transceivers_.erase( |
| 121 | std::remove(transceivers_.begin(), transceivers_.end(), transceiver), |
| 122 | transceivers_.end()); |
| 123 | } |
| 124 | RtpTransceiverProxyRefPtr FindBySender( |
| 125 | rtc::scoped_refptr<RtpSenderInterface> sender) const; |
| 126 | RtpTransceiverProxyRefPtr FindByMid(const std::string& mid) const; |
| 127 | RtpTransceiverProxyRefPtr FindByMLineIndex(size_t mline_index) const; |
| 128 | |
| 129 | // Find or create the stable state for a transceiver. |
| 130 | TransceiverStableState* StableState(RtpTransceiverProxyRefPtr transceiver) { |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 131 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 132 | return &(transceiver_stable_states_by_transceivers_[transceiver]); |
| 133 | } |
| 134 | |
| 135 | void DiscardStableStates() { |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 136 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 137 | transceiver_stable_states_by_transceivers_.clear(); |
| 138 | } |
| 139 | |
| 140 | std::map<RtpTransceiverProxyRefPtr, TransceiverStableState>& StableStates() { |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 141 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 142 | return transceiver_stable_states_by_transceivers_; |
| 143 | } |
| 144 | |
| 145 | private: |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 146 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 147 | std::vector<RtpTransceiverProxyRefPtr> transceivers_; |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 148 | // TODO(bugs.webrtc.org/12692): Add RTC_GUARDED_BY(sequence_checker_); |
| 149 | |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 150 | // Holds changes made to transceivers during applying descriptors for |
| 151 | // potential rollback. Gets cleared once signaling state goes to stable. |
| 152 | std::map<RtpTransceiverProxyRefPtr, TransceiverStableState> |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 153 | transceiver_stable_states_by_transceivers_ |
| 154 | RTC_GUARDED_BY(sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 155 | // Holds remote stream ids for transceivers from stable state. |
| 156 | std::map<RtpTransceiverProxyRefPtr, std::vector<std::string>> |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 | [diff] [blame] | 157 | remote_stream_ids_by_transceivers_ RTC_GUARDED_BY(sequence_checker_); |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace webrtc |
| 161 | |
| 162 | #endif // PC_TRANSCEIVER_LIST_H_ |