blob: 331c8df45cb7e05a9a43ce68903d524b74480e01 [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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "pc/rtcpmuxfilter.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
17RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) {
18}
19
deadbeef23d947d2016-08-22 23:00:3020bool 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:3628bool RtcpMuxFilter::IsActive() const {
deadbeef23d947d2016-08-22 23:00:3029 return IsFullyActive() || IsProvisionallyActive();
henrike@webrtc.org28e20752013-07-10 00:45:3630}
31
Peter Thatcheraf55ccc2015-05-21 14:48:4132void RtcpMuxFilter::SetActive() {
33 state_ = ST_ACTIVE;
34}
35
henrike@webrtc.org28e20752013-07-10 00:45:3636bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 14:48:4137 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:3642 if (!ExpectOffer(offer_enable, src)) {
Mirko Bonadei675513b2017-11-09 10:09:2543 RTC_LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer";
henrike@webrtc.org28e20752013-07-10 00:45:3644 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 14:48:4154 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:3659 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 10:09:2560 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:3661 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 10:09:2581 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer";
henrike@webrtc.org28e20752013-07-10 00:45:3682 return false;
83 }
84
85 return true;
86}
87
88bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) {
Peter Thatcheraf55ccc2015-05-21 14:48:4189 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:3694 if (!ExpectAnswer(src)) {
Mirko Bonadei675513b2017-11-09 10:09:2595 RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:3696 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 10:09:25103 RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer";
henrike@webrtc.org28e20752013-07-10 00:45:36104 return false;
105 } else {
106 state_ = ST_INIT;
107 }
108 return true;
109}
110
henrike@webrtc.org28e20752013-07-10 00:45:36111bool 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