blob: 331c8df45cb7e05a9a43ce68903d524b74480e01 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/rtcpmuxfilter.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
17RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) {
18}
19
deadbeef23d947d2016-08-22 16:00:30 -070020bool RtcpMuxFilter::IsFullyActive() const {
21 return state_ == ST_ACTIVE;
22}
23
24bool RtcpMuxFilter::IsProvisionallyActive() const {
25 return state_ == ST_SENTPRANSWER || state_ == ST_RECEIVEDPRANSWER;
26}
27
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028bool RtcpMuxFilter::IsActive() const {
deadbeef23d947d2016-08-22 16:00:30 -070029 return IsFullyActive() || IsProvisionallyActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030}
31
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070032void RtcpMuxFilter::SetActive() {
33 state_ = ST_ACTIVE;
34}
35
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070037 if (state_ == ST_ACTIVE) {
38 // Fail if we try to deactivate and no-op if we try and activate.
39 return offer_enable;
40 }
41
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042 if (!ExpectOffer(offer_enable, src)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010043 RTC_LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 return false;
45 }
46
47 offer_enable_ = offer_enable;
48 state_ = (src == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER;
49 return true;
50}
51
52bool RtcpMuxFilter::SetProvisionalAnswer(bool answer_enable,
53 ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070054 if (state_ == ST_ACTIVE) {
55 // Fail if we try to deactivate and no-op if we try and activate.
56 return answer_enable;
57 }
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010060 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 return false;
62 }
63
64 if (offer_enable_) {
65 if (answer_enable) {
66 if (src == CS_REMOTE)
67 state_ = ST_RECEIVEDPRANSWER;
68 else // CS_LOCAL
69 state_ = ST_SENTPRANSWER;
70 } else {
71 // The provisional answer doesn't want to use RTCP mux.
72 // Go back to the original state after the offer was set and wait for next
73 // provisional or final answer.
74 if (src == CS_REMOTE)
75 state_ = ST_SENTOFFER;
76 else // CS_LOCAL
77 state_ = ST_RECEIVEDOFFER;
78 }
79 } else if (answer_enable) {
80 // If the offer didn't specify RTCP mux, the answer shouldn't either.
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 return false;
83 }
84
85 return true;
86}
87
88bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -070089 if (state_ == ST_ACTIVE) {
90 // Fail if we try to deactivate and no-op if we try and activate.
91 return answer_enable;
92 }
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010095 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 return false;
97 }
98
99 if (offer_enable_ && answer_enable) {
100 state_ = ST_ACTIVE;
101 } else if (answer_enable) {
102 // If the offer didn't specify RTCP mux, the answer shouldn't either.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 return false;
105 } else {
106 state_ = ST_INIT;
107 }
108 return true;
109}
110
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111bool RtcpMuxFilter::ExpectOffer(bool offer_enable, ContentSource source) {
112 return ((state_ == ST_INIT) ||
113 (state_ == ST_ACTIVE && offer_enable == offer_enable_) ||
114 (state_ == ST_SENTOFFER && source == CS_LOCAL) ||
115 (state_ == ST_RECEIVEDOFFER && source == CS_REMOTE));
116}
117
118bool RtcpMuxFilter::ExpectAnswer(ContentSource source) {
119 return ((state_ == ST_SENTOFFER && source == CS_REMOTE) ||
120 (state_ == ST_RECEIVEDOFFER && source == CS_LOCAL) ||
121 (state_ == ST_SENTPRANSWER && source == CS_LOCAL) ||
122 (state_ == ST_RECEIVEDPRANSWER && source == CS_REMOTE));
123}
124
125} // namespace cricket