3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/session/media/rtcpmuxfilter.h"
30 #include "webrtc/base/logging.h"
34 RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) {
37 bool RtcpMuxFilter::IsActive() const {
38 return state_ == ST_SENTPRANSWER ||
39 state_ == ST_RECEIVEDPRANSWER ||
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";
49 offer_enable_ = offer_enable;
50 state_ = (src == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER;
54 bool RtcpMuxFilter::SetProvisionalAnswer(bool answer_enable,
56 if (!ExpectAnswer(src)) {
57 LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer";
64 state_ = ST_RECEIVEDPRANSWER;
66 state_ = ST_SENTPRANSWER;
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.
72 state_ = ST_SENTOFFER;
74 state_ = ST_RECEIVEDOFFER;
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";
85 bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) {
86 if (!ExpectAnswer(src)) {
87 LOG(LS_ERROR) << "Invalid state for RTCP mux answer";
91 if (offer_enable_ && answer_enable) {
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";
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) {
114 int type = (len >= 2) ? (static_cast<uint8>(data[1]) & 0x7F) : 0;
115 return (type >= 64 && type < 96);
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));
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));
132 } // namespace cricket