henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/rtcpmuxfilter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
| 15 | namespace cricket { |
| 16 | |
| 17 | RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) { |
| 18 | } |
| 19 | |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 20 | bool RtcpMuxFilter::IsFullyActive() const { |
| 21 | return state_ == ST_ACTIVE; |
| 22 | } |
| 23 | |
| 24 | bool RtcpMuxFilter::IsProvisionallyActive() const { |
| 25 | return state_ == ST_SENTPRANSWER || state_ == ST_RECEIVEDPRANSWER; |
| 26 | } |
| 27 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | bool RtcpMuxFilter::IsActive() const { |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 29 | return IsFullyActive() || IsProvisionallyActive(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 32 | void RtcpMuxFilter::SetActive() { |
| 33 | state_ = ST_ACTIVE; |
| 34 | } |
| 35 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) { |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 37 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | if (!ExpectOffer(offer_enable, src)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 43 | RTC_LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | offer_enable_ = offer_enable; |
| 48 | state_ = (src == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool RtcpMuxFilter::SetProvisionalAnswer(bool answer_enable, |
| 53 | ContentSource src) { |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 54 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | if (!ExpectAnswer(src)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 60 | RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 81 | RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) { |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 89 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | if (!ExpectAnswer(src)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 95 | RTC_LOG(LS_ERROR) << "Invalid state for RTCP mux answer"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 103 | RTC_LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } else { |
| 106 | state_ = ST_INIT; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | bool 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 | |
| 118 | bool 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 |