1 /*
2 * libjingle
3 * Copyright 2004--2010, 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/phone/rtcpmuxfilter.h"
29
30 #include "talk/base/logging.h"
31
32 namespace cricket {
33
RtcpMuxFilter()34 RtcpMuxFilter::RtcpMuxFilter() : state_(ST_INIT), offer_enable_(false) {
35 }
36
IsActive() const37 bool RtcpMuxFilter::IsActive() const {
38 // We can receive muxed media prior to the accept, so we have to be able to
39 // deal with that.
40 return (state_ == ST_SENTOFFER || state_ == ST_ACTIVE);
41 }
42
SetOffer(bool offer_enable,ContentSource source)43 bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource source) {
44 bool ret = false;
45 if (state_ == ST_INIT) {
46 offer_enable_ = offer_enable;
47 state_ = (source == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER;
48 ret = true;
49 } else {
50 LOG(LS_ERROR) << "Invalid state for RTCP mux offer";
51 }
52 return ret;
53 }
54
SetAnswer(bool answer_enable,ContentSource source)55 bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource source) {
56 bool ret = false;
57 if ((state_ == ST_SENTOFFER && source == CS_REMOTE) ||
58 (state_ == ST_RECEIVEDOFFER && source == CS_LOCAL)) {
59 if (offer_enable_) {
60 state_ = (answer_enable) ? ST_ACTIVE : ST_INIT;
61 ret = true;
62 } else {
63 // If the offer didn't specify RTCP mux, the answer shouldn't either.
64 if (!answer_enable) {
65 ret = true;
66 state_ = ST_INIT;
67 } else {
68 LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer";
69 }
70 }
71 } else {
72 LOG(LS_ERROR) << "Invalid state for RTCP mux answer";
73 }
74 return ret;
75 }
76
DemuxRtcp(const char * data,int len)77 bool RtcpMuxFilter::DemuxRtcp(const char* data, int len) {
78 // If we're muxing RTP/RTCP, we must inspect each packet delivered and
79 // determine whether it is RTP or RTCP. We do so by checking the packet type,
80 // and assuming RTP if type is 0-63 or 96-127. For additional details, see
81 // http://tools.ietf.org/html/rfc5761.
82 // Note that if we offer RTCP mux, we may receive muxed RTCP before we
83 // receive the answer, so we operate in that state too.
84 if (!IsActive()) {
85 return false;
86 }
87
88 int type = (len >= 2) ? (static_cast<uint8>(data[1]) & 0x7F) : 0;
89 return (type >= 64 && type < 96);
90 }
91
92 } // namespace cricket
93