blob: 9e03171a7414568f708383ec68e8419990f3417c [file] [log] [blame]
Harald Alvestrand38b768c2020-09-29 11:54:051/*
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 Alvestrand5761e7b2021-01-29 14:45:0814#include <stddef.h>
15
Harald Alvestrand38b768c2020-09-29 11:54:0516#include <algorithm>
17#include <map>
Florent Castelli8037fc62024-08-29 13:00:4018#include <optional>
Harald Alvestrand38b768c2020-09-29 11:54:0519#include <string>
20#include <vector>
21
Harald Alvestrand5761e7b2021-01-29 14:45:0822#include "api/media_types.h"
23#include "api/rtc_error.h"
Harald Alvestrand85466662021-04-19 21:21:3624#include "api/rtp_parameters.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0825#include "api/rtp_sender_interface.h"
26#include "api/scoped_refptr.h"
Harald Alvestrand85466662021-04-19 21:21:3627#include "api/sequence_checker.h"
Harald Alvestrand38b768c2020-09-29 11:54:0528#include "pc/rtp_transceiver.h"
Harald Alvestrand85466662021-04-19 21:21:3629#include "rtc_base/checks.h"
30#include "rtc_base/system/no_unique_address.h"
31#include "rtc_base/thread_annotations.h"
Harald Alvestrand38b768c2020-09-29 11:54:0532
33namespace webrtc {
34
35typedef rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
36 RtpTransceiverProxyRefPtr;
37
38// Captures partial state to be used for rollback. Applicable only in
39// Unified Plan.
40class TransceiverStableState {
41 public:
42 TransceiverStableState() {}
43 void set_newly_created();
Florent Castelli8037fc62024-08-29 13:00:4044 void SetMSectionIfUnset(std::optional<std::string> mid,
45 std::optional<size_t> mline_index);
Henrik Boström0a162762022-05-02 13:47:5246 void SetRemoteStreamIds(const std::vector<std::string>& ids);
Eldar Rello950d6b92021-04-06 19:38:0047 void SetInitSendEncodings(
48 const std::vector<RtpEncodingParameters>& encodings);
Henrik Boström0a162762022-05-02 13:47:5249 void SetFiredDirection(
Florent Castelli8037fc62024-08-29 13:00:4050 std::optional<RtpTransceiverDirection> fired_direction) {
Henrik Boström0a162762022-05-02 13:47:5251 fired_direction_ = fired_direction;
52 }
Florent Castelli8037fc62024-08-29 13:00:4053 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 Alvestrand38b768c2020-09-29 11:54:0556 return remote_stream_ids_;
57 }
Florent Castelli8037fc62024-08-29 13:00:4058 std::optional<std::vector<RtpEncodingParameters>> init_send_encodings()
Eldar Rello950d6b92021-04-06 19:38:0059 const {
60 return init_send_encodings_;
61 }
Harald Alvestrand38b768c2020-09-29 11:54:0562 bool has_m_section() const { return has_m_section_; }
63 bool newly_created() const { return newly_created_; }
Henrik Boström0a162762022-05-02 13:47:5264 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 Castelli8037fc62024-08-29 13:00:4067 std::optional<RtpTransceiverDirection> fired_direction() const {
Henrik Boström0a162762022-05-02 13:47:5268 RTC_DCHECK(did_set_fired_direction());
69 return fired_direction_.value();
70 }
Harald Alvestrand38b768c2020-09-29 11:54:0571
72 private:
Florent Castelli8037fc62024-08-29 13:00:4073 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 Alvestrand38b768c2020-09-29 11:54:0577 // 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öm0a162762022-05-02 13:47:5285 // `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 Castelli8037fc62024-08-29 13:00:4087 std::optional<std::optional<RtpTransceiverDirection>> fired_direction_;
Harald Alvestrand38b768c2020-09-29 11:54:0588};
89
Harald Alvestrand85466662021-04-19 21:21:3690// 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 Alvestrand38b768c2020-09-29 11:54:0594class TransceiverList {
95 public:
Harald Alvestrand85466662021-04-19 21:21:3696 // 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 Alvestrand38b768c2020-09-29 11:54:05113
114 void Add(RtpTransceiverProxyRefPtr transceiver) {
Harald Alvestrand85466662021-04-19 21:21:36115 RTC_DCHECK_RUN_ON(&sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05116 transceivers_.push_back(transceiver);
117 }
118 void Remove(RtpTransceiverProxyRefPtr transceiver) {
Harald Alvestrand85466662021-04-19 21:21:36119 RTC_DCHECK_RUN_ON(&sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05120 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 Alvestrand85466662021-04-19 21:21:36131 RTC_DCHECK_RUN_ON(&sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05132 return &(transceiver_stable_states_by_transceivers_[transceiver]);
133 }
134
135 void DiscardStableStates() {
Harald Alvestrand85466662021-04-19 21:21:36136 RTC_DCHECK_RUN_ON(&sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05137 transceiver_stable_states_by_transceivers_.clear();
138 }
139
140 std::map<RtpTransceiverProxyRefPtr, TransceiverStableState>& StableStates() {
Harald Alvestrand85466662021-04-19 21:21:36141 RTC_DCHECK_RUN_ON(&sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05142 return transceiver_stable_states_by_transceivers_;
143 }
144
145 private:
Harald Alvestrand85466662021-04-19 21:21:36146 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
Harald Alvestrand38b768c2020-09-29 11:54:05147 std::vector<RtpTransceiverProxyRefPtr> transceivers_;
Harald Alvestrand85466662021-04-19 21:21:36148 // TODO(bugs.webrtc.org/12692): Add RTC_GUARDED_BY(sequence_checker_);
149
Harald Alvestrand38b768c2020-09-29 11:54:05150 // 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 Alvestrand85466662021-04-19 21:21:36153 transceiver_stable_states_by_transceivers_
154 RTC_GUARDED_BY(sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05155 // Holds remote stream ids for transceivers from stable state.
156 std::map<RtpTransceiverProxyRefPtr, std::vector<std::string>>
Harald Alvestrand85466662021-04-19 21:21:36157 remote_stream_ids_by_transceivers_ RTC_GUARDED_BY(sequence_checker_);
Harald Alvestrand38b768c2020-09-29 11:54:05158};
159
160} // namespace webrtc
161
162#endif // PC_TRANSCEIVER_LIST_H_