blob: a8cf717b289d573d9f53d5bcdcb592e3cf7e4df4 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/rtcp_mux_filter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15namespace cricket {
16
Yves Gerey665174f2018-06-19 15:03:05 +020017RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
deadbeef23d947d2016-08-22 16:00:30 -070019bool RtcpMuxFilter::IsFullyActive() const {
20 return state_ == ST_ACTIVE;
21}
22
23bool RtcpMuxFilter::IsProvisionallyActive() const {
24 return state_ == ST_SENTPRANSWER || state_ == ST_RECEIVEDPRANSWER;
25}
26
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027bool RtcpMuxFilter::IsActive() const {
deadbeef23d947d2016-08-22 16:00:30 -070028 return IsFullyActive() || IsProvisionallyActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029}
30
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070031void RtcpMuxFilter::SetActive() {
32 state_ = ST_ACTIVE;
33}
34
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070036 if (state_ == ST_ACTIVE) {
37 // Fail if we try to deactivate and no-op if we try and activate.
38 return offer_enable;
39 }
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 if (!ExpectOffer(offer_enable, src)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010042 RTC_LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 return false;
44 }
45
46 offer_enable_ = offer_enable;
47 state_ = (src == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER;
48 return true;
49}
50
51bool RtcpMuxFilter::SetProvisionalAnswer(bool answer_enable,
52 ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070053 if (state_ == ST_ACTIVE) {
54 // Fail if we try to deactivate and no-op if we try and activate.
55 return answer_enable;
56 }
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 return false;
61 }
62
63 if (offer_enable_) {
64 if (answer_enable) {
65 if (src == CS_REMOTE)
66 state_ = ST_RECEIVEDPRANSWER;
67 else // CS_LOCAL
68 state_ = ST_SENTPRANSWER;
69 } else {
70 // The provisional answer doesn't want to use RTCP mux.
71 // Go back to the original state after the offer was set and wait for next
72 // provisional or final answer.
73 if (src == CS_REMOTE)
74 state_ = ST_SENTOFFER;
75 else // CS_LOCAL
76 state_ = ST_RECEIVEDOFFER;
77 }
78 } else if (answer_enable) {
79 // If the offer didn't specify RTCP mux, the answer shouldn't either.
Mirko Bonadei675513b2017-11-09 11:09:25 +010080 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 return false;
82 }
83
84 return true;
85}
86
87bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070088 if (state_ == ST_ACTIVE) {
89 // Fail if we try to deactivate and no-op if we try and activate.
90 return answer_enable;
91 }
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 return false;
96 }
97
98 if (offer_enable_ && answer_enable) {
99 state_ = ST_ACTIVE;
100 } else if (answer_enable) {
101 // If the offer didn't specify RTCP mux, the answer shouldn't either.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100102 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 return false;
104 } else {
105 state_ = ST_INIT;
106 }
107 return true;
108}
109
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110bool RtcpMuxFilter::ExpectOffer(bool offer_enable, ContentSource source) {
111 return ((state_ == ST_INIT) ||
112 (state_ == ST_ACTIVE && offer_enable == offer_enable_) ||
113 (state_ == ST_SENTOFFER && source == CS_LOCAL) ||
114 (state_ == ST_RECEIVEDOFFER && source == CS_REMOTE));
115}
116
117bool RtcpMuxFilter::ExpectAnswer(ContentSource source) {
118 return ((state_ == ST_SENTOFFER && source == CS_REMOTE) ||
119 (state_ == ST_RECEIVEDOFFER && source == CS_LOCAL) ||
120 (state_ == ST_SENTPRANSWER && source == CS_LOCAL) ||
121 (state_ == ST_RECEIVEDPRANSWER && source == CS_REMOTE));
122}
123
124} // namespace cricket