OLD | NEW |
| (Empty) |
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 "webrtc/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 void RtcpMuxFilter::SetActive() { | |
44 state_ = ST_ACTIVE; | |
45 } | |
46 | |
47 bool RtcpMuxFilter::SetOffer(bool offer_enable, ContentSource src) { | |
48 if (state_ == ST_ACTIVE) { | |
49 // Fail if we try to deactivate and no-op if we try and activate. | |
50 return offer_enable; | |
51 } | |
52 | |
53 if (!ExpectOffer(offer_enable, src)) { | |
54 LOG(LS_ERROR) << "Invalid state for change of RTCP mux offer"; | |
55 return false; | |
56 } | |
57 | |
58 offer_enable_ = offer_enable; | |
59 state_ = (src == CS_LOCAL) ? ST_SENTOFFER : ST_RECEIVEDOFFER; | |
60 return true; | |
61 } | |
62 | |
63 bool RtcpMuxFilter::SetProvisionalAnswer(bool answer_enable, | |
64 ContentSource src) { | |
65 if (state_ == ST_ACTIVE) { | |
66 // Fail if we try to deactivate and no-op if we try and activate. | |
67 return answer_enable; | |
68 } | |
69 | |
70 if (!ExpectAnswer(src)) { | |
71 LOG(LS_ERROR) << "Invalid state for RTCP mux provisional answer"; | |
72 return false; | |
73 } | |
74 | |
75 if (offer_enable_) { | |
76 if (answer_enable) { | |
77 if (src == CS_REMOTE) | |
78 state_ = ST_RECEIVEDPRANSWER; | |
79 else // CS_LOCAL | |
80 state_ = ST_SENTPRANSWER; | |
81 } else { | |
82 // The provisional answer doesn't want to use RTCP mux. | |
83 // Go back to the original state after the offer was set and wait for next | |
84 // provisional or final answer. | |
85 if (src == CS_REMOTE) | |
86 state_ = ST_SENTOFFER; | |
87 else // CS_LOCAL | |
88 state_ = ST_RECEIVEDOFFER; | |
89 } | |
90 } else if (answer_enable) { | |
91 // If the offer didn't specify RTCP mux, the answer shouldn't either. | |
92 LOG(LS_WARNING) << "Invalid parameters in RTCP mux provisional answer"; | |
93 return false; | |
94 } | |
95 | |
96 return true; | |
97 } | |
98 | |
99 bool RtcpMuxFilter::SetAnswer(bool answer_enable, ContentSource src) { | |
100 if (state_ == ST_ACTIVE) { | |
101 // Fail if we try to deactivate and no-op if we try and activate. | |
102 return answer_enable; | |
103 } | |
104 | |
105 if (!ExpectAnswer(src)) { | |
106 LOG(LS_ERROR) << "Invalid state for RTCP mux answer"; | |
107 return false; | |
108 } | |
109 | |
110 if (offer_enable_ && answer_enable) { | |
111 state_ = ST_ACTIVE; | |
112 } else if (answer_enable) { | |
113 // If the offer didn't specify RTCP mux, the answer shouldn't either. | |
114 LOG(LS_WARNING) << "Invalid parameters in RTCP mux answer"; | |
115 return false; | |
116 } else { | |
117 state_ = ST_INIT; | |
118 } | |
119 return true; | |
120 } | |
121 | |
122 // Check the RTP payload type. If 63 < payload type < 96, it's RTCP. | |
123 // For additional details, see http://tools.ietf.org/html/rfc5761. | |
124 bool IsRtcp(const char* data, int len) { | |
125 if (len < 2) { | |
126 return false; | |
127 } | |
128 char pt = data[1] & 0x7F; | |
129 return (63 < pt) && (pt < 96); | |
130 } | |
131 | |
132 bool RtcpMuxFilter::DemuxRtcp(const char* data, int len) { | |
133 // If we're muxing RTP/RTCP, we must inspect each packet delivered | |
134 // and determine whether it is RTP or RTCP. We do so by looking at | |
135 // the RTP payload type (see IsRtcp). Note that if we offer RTCP | |
136 // mux, we may receive muxed RTCP before we receive the answer, so | |
137 // we operate in that state too. | |
138 bool offered_mux = ((state_ == ST_SENTOFFER) && offer_enable_); | |
139 return (IsActive() || offered_mux) && IsRtcp(data, len); | |
140 } | |
141 | |
142 bool RtcpMuxFilter::ExpectOffer(bool offer_enable, ContentSource source) { | |
143 return ((state_ == ST_INIT) || | |
144 (state_ == ST_ACTIVE && offer_enable == offer_enable_) || | |
145 (state_ == ST_SENTOFFER && source == CS_LOCAL) || | |
146 (state_ == ST_RECEIVEDOFFER && source == CS_REMOTE)); | |
147 } | |
148 | |
149 bool RtcpMuxFilter::ExpectAnswer(ContentSource source) { | |
150 return ((state_ == ST_SENTOFFER && source == CS_REMOTE) || | |
151 (state_ == ST_RECEIVEDOFFER && source == CS_LOCAL) || | |
152 (state_ == ST_SENTPRANSWER && source == CS_LOCAL) || | |
153 (state_ == ST_RECEIVEDPRANSWER && source == CS_REMOTE)); | |
154 } | |
155 | |
156 } // namespace cricket | |
OLD | NEW |