blob: a8cf717b289d573d9f53d5bcdcb592e3cf7e4df4 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellander65c7f672016-02-12 08:05:012 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellander65c7f672016-02-12 08:05:014 * 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:369 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/rtcp_mux_filter.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:3614
15namespace cricket {
16
Yves Gerey665174f2018-06-19 13:03:0517RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:3618
deadbeef23d947d2016-08-22 23:00:3019bool 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:3627bool RtcpMuxFilter::IsActive() const {
deadbeef23d947d2016-08-22 23:00:3028 return IsFullyActive() || IsProvisionallyActive();
henrike@webrtc.org28e20752013-07-10 00:45:3629}
30
Peter Thatcheraf55ccc2015-05-21 14:48:4131void RtcpMuxFilter::SetActive() {
32 state_ = ST_ACTIVE;
33}
34
henrike@webrtc.org28e20752013-07-10 00:45:3635bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 14:48:4136 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:3641 if (!ExpectOffer(offer_enable, src)) {
Mirko Bonadei675513b2017-11-09 10:09:2542 RTC_LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer";
henrike@webrtc.org28e20752013-07-10 00:45:3643 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 14:48:4153 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:3658 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 10:09:2559 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:3660 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 10:09:2580 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:3681 return false;
82 }
83
84 return true;
85}
86
87bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 14:48:4188 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:3693 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 10:09:2594 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:3695 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 10:09:25102 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:36103 return false;
104 } else {
105 state_ = ST_INIT;
106 }
107 return true;
108}
109
henrike@webrtc.org28e20752013-07-10 00:45:36110bool 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