1 /* 2 * libjingle 3 * Copyright 2004 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "talk/session/media/rtcpmuxfilter.h" 29 30 #include "talk/base/logging.h" 31 32 namespace cricket { 33 34 RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) { 35 } 36 37 bool RtcpMuxFilter::IsActive() const { 38 return state_ == ST_SENTPRANSWER || 39 state_ == ST_RECEIVEDPRANSWER || 40 state_ == ST_ACTIVE; 41 } 42 43 bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) { 44 if (!ExpectOffer(offer_enable, src)) { 45 LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer"; 46 return false; 47 } 48 49 offer_enable_ = offer_enable; 50 state_ = (src == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER; 51 return true; 52 } 53 54 bool RtcpMuxFilter::SetProvisionalAnswer(bool answer_enable, 55 ContentSource src) { 56 if (!ExpectAnswer(src)) { 57 LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer"; 58 return false; 59 } 60 61 if (offer_enable_) { 62 if (answer_enable) { 63 if (src == CS_REMOTE) 64 state_ = ST_RECEIVEDPRANSWER; 65 else // CS_LOCAL 66 state_ = ST_SENTPRANSWER; 67 } else { 68 // The provisional answer doesn't want to use RTCP mux. 69 // Go back to the original state after the offer was set and wait for next 70 // provisional or final answer. 71 if (src == CS_REMOTE) 72 state_ = ST_SENTOFFER; 73 else // CS_LOCAL 74 state_ = ST_RECEIVEDOFFER; 75 } 76 } else if (answer_enable) { 77 // If the offer didn't specify RTCP mux, the answer shouldn't either. 78 LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer"; 79 return false; 80 } 81 82 return true; 83 } 84 85 bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) { 86 if (!ExpectAnswer(src)) { 87 LOG(LS_ERROR) << "Invalid state for RTCP mux answer"; 88 return false; 89 } 90 91 if (offer_enable_ && answer_enable) { 92 state_ = ST_ACTIVE; 93 } else if (answer_enable) { 94 // If the offer didn't specify RTCP mux, the answer shouldn't either. 95 LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer"; 96 return false; 97 } else { 98 state_ = ST_INIT; 99 } 100 return true; 101 } 102 103 bool RtcpMuxFilter::DemuxRtcp(const char* data, int len) { 104 // If we're muxing RTP/RTCP, we must inspect each packet delivered and 105 // determine whether it is RTP or RTCP. We do so by checking the packet type, 106 // and assuming RTP if type is 0-63 or 96-127. For additional details, see 107 // http://tools.ietf.org/html/rfc5761. 108 // Note that if we offer RTCP mux, we may receive muxed RTCP before we 109 // receive the answer, so we operate in that state too. 110 if (!offer_enable_ || state_ < ST_SENTOFFER) { 111 return false; 112 } 113 114 int type = (len >= 2) ? (static_cast<uint8>(data[1]) & 0x7F) : 0; 115 return (type >= 64 && type < 96); 116 } 117 118 bool RtcpMuxFilter::ExpectOffer(bool offer_enable, ContentSource source) { 119 return ((state_ == ST_INIT) || 120 (state_ == ST_ACTIVE && offer_enable == offer_enable_) || 121 (state_ == ST_SENTOFFER && source == CS_LOCAL) || 122 (state_ == ST_RECEIVEDOFFER && source == CS_REMOTE)); 123 } 124 125 bool RtcpMuxFilter::ExpectAnswer(ContentSource source) { 126 return ((state_ == ST_SENTOFFER && source == CS_REMOTE) || 127 (state_ == ST_RECEIVEDOFFER && source == CS_LOCAL) || 128 (state_ == ST_SENTPRANSWER && source == CS_LOCAL) || 129 (state_ == ST_RECEIVEDPRANSWER && source == CS_REMOTE)); 130 } 131 132 } // namespace cricket 133