blob: 4364c3c2b241cd34db9528442068e619fbaf7c04 [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>
18#include <string>
19#include <vector>
20
Harald Alvestrand5761e7b2021-01-29 14:45:0821#include "absl/types/optional.h"
22#include "api/media_types.h"
23#include "api/rtc_error.h"
24#include "api/rtp_sender_interface.h"
25#include "api/scoped_refptr.h"
Harald Alvestrand38b768c2020-09-29 11:54:0526#include "pc/rtp_transceiver.h"
27
28namespace webrtc {
29
30typedef rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
31 RtpTransceiverProxyRefPtr;
32
33// Captures partial state to be used for rollback. Applicable only in
34// Unified Plan.
35class TransceiverStableState {
36 public:
37 TransceiverStableState() {}
38 void set_newly_created();
39 void SetMSectionIfUnset(absl::optional<std::string> mid,
40 absl::optional<size_t> mline_index);
41 void SetRemoteStreamIdsIfUnset(const std::vector<std::string>& ids);
42 absl::optional<std::string> mid() const { return mid_; }
43 absl::optional<size_t> mline_index() const { return mline_index_; }
44 absl::optional<std::vector<std::string>> remote_stream_ids() const {
45 return remote_stream_ids_;
46 }
47 bool has_m_section() const { return has_m_section_; }
48 bool newly_created() const { return newly_created_; }
49
50 private:
51 absl::optional<std::string> mid_;
52 absl::optional<size_t> mline_index_;
53 absl::optional<std::vector<std::string>> remote_stream_ids_;
54 // Indicates that mid value from stable state has been captured and
55 // that rollback has to restore the transceiver. Also protects against
56 // subsequent overwrites.
57 bool has_m_section_ = false;
58 // Indicates that the transceiver was created as part of applying a
59 // description to track potential need for removing transceiver during
60 // rollback.
61 bool newly_created_ = false;
62};
63
64class TransceiverList {
65 public:
66 std::vector<RtpTransceiverProxyRefPtr> List() const { return transceivers_; }
67
68 void Add(RtpTransceiverProxyRefPtr transceiver) {
69 transceivers_.push_back(transceiver);
70 }
71 void Remove(RtpTransceiverProxyRefPtr transceiver) {
72 transceivers_.erase(
73 std::remove(transceivers_.begin(), transceivers_.end(), transceiver),
74 transceivers_.end());
75 }
76 RtpTransceiverProxyRefPtr FindBySender(
77 rtc::scoped_refptr<RtpSenderInterface> sender) const;
78 RtpTransceiverProxyRefPtr FindByMid(const std::string& mid) const;
79 RtpTransceiverProxyRefPtr FindByMLineIndex(size_t mline_index) const;
80
81 // Find or create the stable state for a transceiver.
82 TransceiverStableState* StableState(RtpTransceiverProxyRefPtr transceiver) {
83 return &(transceiver_stable_states_by_transceivers_[transceiver]);
84 }
85
86 void DiscardStableStates() {
87 transceiver_stable_states_by_transceivers_.clear();
88 }
89
90 std::map<RtpTransceiverProxyRefPtr, TransceiverStableState>& StableStates() {
91 return transceiver_stable_states_by_transceivers_;
92 }
93
94 private:
95 std::vector<RtpTransceiverProxyRefPtr> transceivers_;
96 // Holds changes made to transceivers during applying descriptors for
97 // potential rollback. Gets cleared once signaling state goes to stable.
98 std::map<RtpTransceiverProxyRefPtr, TransceiverStableState>
99 transceiver_stable_states_by_transceivers_;
100 // Holds remote stream ids for transceivers from stable state.
101 std::map<RtpTransceiverProxyRefPtr, std::vector<std::string>>
102 remote_stream_ids_by_transceivers_;
103};
104
105} // namespace webrtc
106
107#endif // PC_TRANSCEIVER_LIST_H_