henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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/channel.h" |
| 29 | |
buildbot@webrtc.org | 5b1ebac | 2014-08-07 17:18:00 +0000 | [diff] [blame] | 30 | #include "talk/media/base/constants.h" |
| 31 | #include "talk/media/base/rtputils.h" |
buildbot@webrtc.org | 5b1ebac | 2014-08-07 17:18:00 +0000 | [diff] [blame] | 32 | #include "talk/session/media/channelmanager.h" |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 33 | #include "webrtc/base/bind.h" |
| 34 | #include "webrtc/base/buffer.h" |
| 35 | #include "webrtc/base/byteorder.h" |
| 36 | #include "webrtc/base/common.h" |
| 37 | #include "webrtc/base/dscp.h" |
| 38 | #include "webrtc/base/logging.h" |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 39 | #include "webrtc/base/trace_event.h" |
| 40 | #include "webrtc/p2p/base/transportchannel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | |
| 42 | namespace cricket { |
| 43 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 44 | using rtc::Bind; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 45 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | enum { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 47 | MSG_EARLYMEDIATIMEOUT = 1, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | MSG_SCREENCASTWINDOWEVENT, |
| 49 | MSG_RTPPACKET, |
| 50 | MSG_RTCPPACKET, |
| 51 | MSG_CHANNEL_ERROR, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | MSG_READYTOSENDDATA, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | MSG_DATARECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | MSG_FIRSTPACKETRECEIVED, |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 55 | MSG_STREAMCLOSEDREMOTELY, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | // Value specified in RFC 5764. |
| 59 | static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp"; |
| 60 | |
| 61 | static const int kAgcMinus10db = -10; |
| 62 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 63 | static void SafeSetError(const std::string& message, std::string* error_desc) { |
| 64 | if (error_desc) { |
| 65 | *error_desc = message; |
| 66 | } |
| 67 | } |
| 68 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 69 | struct PacketMessageData : public rtc::MessageData { |
| 70 | rtc::Buffer packet; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 71 | rtc::PacketOptions options; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 74 | struct ScreencastEventMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 75 | ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we) |
| 76 | : ssrc(s), event(we) {} |
| 77 | uint32_t ssrc; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 78 | rtc::WindowEvent event; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | struct VoiceChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 82 | VoiceChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | VoiceMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | : ssrc(in_ssrc), error(in_error) {} |
| 85 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | VoiceMediaChannel::Error error; |
| 87 | }; |
| 88 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 89 | struct VideoChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 90 | VideoChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | VideoMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 92 | : ssrc(in_ssrc), error(in_error) {} |
| 93 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | VideoMediaChannel::Error error; |
| 95 | }; |
| 96 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 97 | struct DataChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 98 | DataChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | DataMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 100 | : ssrc(in_ssrc), error(in_error) {} |
| 101 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | DataMediaChannel::Error error; |
| 103 | }; |
| 104 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 106 | struct VideoChannel::ScreencastDetailsData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 107 | explicit ScreencastDetailsData(uint32_t s) |
| 108 | : ssrc(s), fps(0), screencast_max_pixels(0) {} |
| 109 | uint32_t ssrc; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 110 | int fps; |
| 111 | int screencast_max_pixels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | static const char* PacketType(bool rtcp) { |
| 115 | return (!rtcp) ? "RTP" : "RTCP"; |
| 116 | } |
| 117 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 118 | static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | // Check the packet size. We could check the header too if needed. |
| 120 | return (packet && |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 121 | packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) && |
| 122 | packet->size() <= kMaxRtpPacketLen); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static bool IsReceiveContentDirection(MediaContentDirection direction) { |
| 126 | return direction == MD_SENDRECV || direction == MD_RECVONLY; |
| 127 | } |
| 128 | |
| 129 | static bool IsSendContentDirection(MediaContentDirection direction) { |
| 130 | return direction == MD_SENDRECV || direction == MD_SENDONLY; |
| 131 | } |
| 132 | |
| 133 | static const MediaContentDescription* GetContentDescription( |
| 134 | const ContentInfo* cinfo) { |
| 135 | if (cinfo == NULL) |
| 136 | return NULL; |
| 137 | return static_cast<const MediaContentDescription*>(cinfo->description); |
| 138 | } |
| 139 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 140 | template <class Codec> |
| 141 | void RtpParametersFromMediaDescription( |
| 142 | const MediaContentDescriptionImpl<Codec>* desc, |
| 143 | RtpParameters<Codec>* params) { |
| 144 | // TODO(pthatcher): Remove this once we're sure no one will give us |
| 145 | // a description without codecs (currently a CA_UPDATE with just |
| 146 | // streams can). |
| 147 | if (desc->has_codecs()) { |
| 148 | params->codecs = desc->codecs(); |
| 149 | } |
| 150 | // TODO(pthatcher): See if we really need |
| 151 | // rtp_header_extensions_set() and remove it if we don't. |
| 152 | if (desc->rtp_header_extensions_set()) { |
| 153 | params->extensions = desc->rtp_header_extensions(); |
| 154 | } |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 155 | params->rtcp.reduced_size = desc->rtcp_reduced_size(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <class Codec, class Options> |
| 159 | void RtpSendParametersFromMediaDescription( |
| 160 | const MediaContentDescriptionImpl<Codec>* desc, |
| 161 | RtpSendParameters<Codec, Options>* send_params) { |
| 162 | RtpParametersFromMediaDescription(desc, send_params); |
| 163 | send_params->max_bandwidth_bps = desc->bandwidth(); |
| 164 | } |
| 165 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 166 | BaseChannel::BaseChannel(rtc::Thread* thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 167 | MediaChannel* media_channel, |
| 168 | TransportController* transport_controller, |
| 169 | const std::string& content_name, |
| 170 | bool rtcp) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | : worker_thread_(thread), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 172 | transport_controller_(transport_controller), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | media_channel_(media_channel), |
| 174 | content_name_(content_name), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 175 | rtcp_transport_enabled_(rtcp), |
| 176 | transport_channel_(nullptr), |
| 177 | rtcp_transport_channel_(nullptr), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | enabled_(false), |
| 179 | writable_(false), |
| 180 | rtp_ready_to_send_(false), |
| 181 | rtcp_ready_to_send_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | was_ever_writable_(false), |
| 183 | local_content_direction_(MD_INACTIVE), |
| 184 | remote_content_direction_(MD_INACTIVE), |
| 185 | has_received_packet_(false), |
| 186 | dtls_keyed_(false), |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 187 | secure_required_(false), |
| 188 | rtp_abs_sendtime_extn_id_(-1) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 189 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | LOG(LS_INFO) << "Created channel for " << content_name; |
| 191 | } |
| 192 | |
| 193 | BaseChannel::~BaseChannel() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 194 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 195 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 196 | StopConnectionMonitor(); |
| 197 | FlushRtcpMessages(); // Send any outstanding RTCP packets. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 198 | worker_thread_->Clear(this); // eats any outstanding messages or packets |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | // We must destroy the media channel before the transport channel, otherwise |
| 200 | // the media channel may try to send on the dead transport channel. NULLing |
| 201 | // is not an effective strategy since the sends will come on another thread. |
| 202 | delete media_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 203 | // Note that we don't just call set_transport_channel(nullptr) because that |
| 204 | // would call a pure virtual method which we can't do from a destructor. |
| 205 | if (transport_channel_) { |
| 206 | DisconnectFromTransportChannel(transport_channel_); |
| 207 | transport_controller_->DestroyTransportChannel_w( |
| 208 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 209 | } |
| 210 | if (rtcp_transport_channel_) { |
| 211 | DisconnectFromTransportChannel(rtcp_transport_channel_); |
| 212 | transport_controller_->DestroyTransportChannel_w( |
| 213 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 214 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | LOG(LS_INFO) << "Destroyed channel"; |
| 216 | } |
| 217 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 218 | bool BaseChannel::Init() { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 219 | if (!SetTransport(content_name())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 223 | if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 226 | if (rtcp_transport_enabled() && |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 227 | !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 231 | // Both RTP and RTCP channels are set, we can call SetInterface on |
| 232 | // media channel and it can set network options. |
| 233 | media_channel_->SetInterface(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 237 | void BaseChannel::Deinit() { |
| 238 | media_channel_->SetInterface(NULL); |
| 239 | } |
| 240 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 241 | bool BaseChannel::SetTransport(const std::string& transport_name) { |
| 242 | return worker_thread_->Invoke<bool>( |
| 243 | Bind(&BaseChannel::SetTransport_w, this, transport_name)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 244 | } |
| 245 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 246 | bool BaseChannel::SetTransport_w(const std::string& transport_name) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 247 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 248 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 249 | if (transport_name == transport_name_) { |
| 250 | // Nothing to do if transport name isn't changing |
| 251 | return true; |
| 252 | } |
| 253 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 254 | // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport |
| 255 | // changes and wait until the DTLS handshake is complete to set the newly |
| 256 | // negotiated parameters. |
| 257 | if (ShouldSetupDtlsSrtp()) { |
| 258 | srtp_filter_.ResetParams(); |
| 259 | } |
| 260 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 261 | set_transport_channel(transport_controller_->CreateTransportChannel_w( |
| 262 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 263 | if (!transport_channel()) { |
| 264 | return false; |
| 265 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 266 | if (rtcp_transport_enabled()) { |
| 267 | LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name() |
| 268 | << " on " << transport_name << " transport "; |
| 269 | set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w( |
| 270 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 271 | if (!rtcp_transport_channel()) { |
| 272 | return false; |
| 273 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 274 | } |
| 275 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 276 | transport_name_ = transport_name; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
| 280 | void BaseChannel::set_transport_channel(TransportChannel* new_tc) { |
| 281 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 282 | |
| 283 | TransportChannel* old_tc = transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 284 | if (!old_tc && !new_tc) { |
| 285 | // Nothing to do |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 288 | ASSERT(old_tc != new_tc); |
| 289 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 290 | if (old_tc) { |
| 291 | DisconnectFromTransportChannel(old_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 292 | transport_controller_->DestroyTransportChannel_w( |
| 293 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | transport_channel_ = new_tc; |
| 297 | |
| 298 | if (new_tc) { |
| 299 | ConnectToTransportChannel(new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 300 | for (const auto& pair : socket_options_) { |
| 301 | new_tc->SetOption(pair.first, pair.second); |
| 302 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 303 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 304 | |
| 305 | // Update aggregate writable/ready-to-send state between RTP and RTCP upon |
| 306 | // setting new channel |
| 307 | UpdateWritableState_w(); |
| 308 | SetReadyToSend(false, new_tc && new_tc->writable()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) { |
| 312 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 313 | |
| 314 | TransportChannel* old_tc = rtcp_transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 315 | if (!old_tc && !new_tc) { |
| 316 | // Nothing to do |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 317 | return; |
| 318 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 319 | ASSERT(old_tc != new_tc); |
| 320 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 321 | if (old_tc) { |
| 322 | DisconnectFromTransportChannel(old_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 323 | transport_controller_->DestroyTransportChannel_w( |
| 324 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | rtcp_transport_channel_ = new_tc; |
| 328 | |
| 329 | if (new_tc) { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 330 | RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive())) |
| 331 | << "Setting RTCP for DTLS/SRTP after SrtpFilter is active " |
| 332 | << "should never happen."; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 333 | ConnectToTransportChannel(new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 334 | for (const auto& pair : rtcp_socket_options_) { |
| 335 | new_tc->SetOption(pair.first, pair.second); |
| 336 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 337 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 338 | |
| 339 | // Update aggregate writable/ready-to-send state between RTP and RTCP upon |
| 340 | // setting new channel |
| 341 | UpdateWritableState_w(); |
| 342 | SetReadyToSend(true, new_tc && new_tc->writable()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) { |
| 346 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 347 | |
| 348 | tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState); |
| 349 | tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead); |
| 350 | tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 351 | tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) { |
| 355 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 356 | |
| 357 | tc->SignalWritableState.disconnect(this); |
| 358 | tc->SignalReadPacket.disconnect(this); |
| 359 | tc->SignalReadyToSend.disconnect(this); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 360 | tc->SignalDtlsState.disconnect(this); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 361 | } |
| 362 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 363 | bool BaseChannel::Enable(bool enable) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 364 | worker_thread_->Invoke<void>(Bind( |
| 365 | enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w, |
| 366 | this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 370 | bool BaseChannel::AddRecvStream(const StreamParams& sp) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 371 | return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 374 | bool BaseChannel::RemoveRecvStream(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 375 | return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 376 | } |
| 377 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 378 | bool BaseChannel::AddSendStream(const StreamParams& sp) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 379 | return InvokeOnWorker( |
| 380 | Bind(&MediaChannel::AddSendStream, media_channel(), sp)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 383 | bool BaseChannel::RemoveSendStream(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 384 | return InvokeOnWorker( |
| 385 | Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 386 | } |
| 387 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 388 | bool BaseChannel::SetLocalContent(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 389 | ContentAction action, |
| 390 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 391 | TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent"); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 392 | return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w, |
| 393 | this, content, action, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 397 | ContentAction action, |
| 398 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 399 | TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent"); |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 400 | return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w, |
| 401 | this, content, action, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 402 | } |
| 403 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 404 | void BaseChannel::StartConnectionMonitor(int cms) { |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 405 | // We pass in the BaseChannel instead of the transport_channel_ |
| 406 | // because if the transport_channel_ changes, the ConnectionMonitor |
| 407 | // would be pointing to the wrong TransportChannel. |
| 408 | connection_monitor_.reset(new ConnectionMonitor( |
| 409 | this, worker_thread(), rtc::Thread::Current())); |
| 410 | connection_monitor_->SignalUpdate.connect( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 411 | this, &BaseChannel::OnConnectionMonitorUpdate); |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 412 | connection_monitor_->Start(cms); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void BaseChannel::StopConnectionMonitor() { |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 416 | if (connection_monitor_) { |
| 417 | connection_monitor_->Stop(); |
| 418 | connection_monitor_.reset(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 422 | bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) { |
| 423 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 424 | return transport_channel_->GetStats(infos); |
| 425 | } |
| 426 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 427 | bool BaseChannel::IsReadyToReceive() const { |
| 428 | // Receive data if we are enabled and have local content, |
| 429 | return enabled() && IsReceiveContentDirection(local_content_direction_); |
| 430 | } |
| 431 | |
| 432 | bool BaseChannel::IsReadyToSend() const { |
| 433 | // Send outgoing data if we are enabled, have local and remote content, |
| 434 | // and we have had some form of connectivity. |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 435 | return enabled() && IsReceiveContentDirection(remote_content_direction_) && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | IsSendContentDirection(local_content_direction_) && |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 437 | was_ever_writable() && |
| 438 | (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 439 | } |
| 440 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 441 | bool BaseChannel::SendPacket(rtc::Buffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 442 | const rtc::PacketOptions& options) { |
| 443 | return SendPacket(false, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 444 | } |
| 445 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 446 | bool BaseChannel::SendRtcp(rtc::Buffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 447 | const rtc::PacketOptions& options) { |
| 448 | return SendPacket(true, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 449 | } |
| 450 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 451 | int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 452 | int value) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 453 | TransportChannel* channel = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 454 | switch (type) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 455 | case ST_RTP: |
| 456 | channel = transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 457 | socket_options_.push_back( |
| 458 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 459 | break; |
| 460 | case ST_RTCP: |
| 461 | channel = rtcp_transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 462 | rtcp_socket_options_.push_back( |
| 463 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 464 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 465 | } |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 466 | return channel ? channel->SetOption(opt, value) : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void BaseChannel::OnWritableState(TransportChannel* channel) { |
| 470 | ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 471 | UpdateWritableState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void BaseChannel::OnChannelRead(TransportChannel* channel, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 475 | const char* data, size_t len, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 476 | const rtc::PacketTime& packet_time, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 477 | int flags) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 478 | TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 479 | // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 480 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | |
| 482 | // When using RTCP multiplexing we might get RTCP packets on the RTP |
| 483 | // transport. We feed RTP traffic into the demuxer to determine if it is RTCP. |
| 484 | bool rtcp = PacketIsRtcp(channel, data, len); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 485 | rtc::Buffer packet(data, len); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 486 | HandlePacket(rtcp, &packet, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void BaseChannel::OnReadyToSend(TransportChannel* channel) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 490 | ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_); |
| 491 | SetReadyToSend(channel == rtcp_transport_channel_, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 494 | void BaseChannel::OnDtlsState(TransportChannel* channel, |
| 495 | DtlsTransportState state) { |
| 496 | if (!ShouldSetupDtlsSrtp()) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED |
| 501 | // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to |
| 502 | // cover other scenarios like the whole channel is writable (not just this |
| 503 | // TransportChannel) or when TransportChannel is attached after DTLS is |
| 504 | // negotiated. |
| 505 | if (state != DTLS_TRANSPORT_CONNECTED) { |
| 506 | srtp_filter_.ResetParams(); |
| 507 | } |
| 508 | } |
| 509 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 510 | void BaseChannel::SetReadyToSend(bool rtcp, bool ready) { |
| 511 | if (rtcp) { |
| 512 | rtcp_ready_to_send_ = ready; |
| 513 | } else { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 514 | rtp_ready_to_send_ = ready; |
| 515 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 516 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 517 | if (rtp_ready_to_send_ && |
| 518 | // In the case of rtcp mux |rtcp_transport_channel_| will be null. |
| 519 | (rtcp_ready_to_send_ || !rtcp_transport_channel_)) { |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 520 | // Notify the MediaChannel when both rtp and rtcp channel can send. |
| 521 | media_channel_->OnReadyToSend(true); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 522 | } else { |
| 523 | // Notify the MediaChannel when either rtp or rtcp channel can't send. |
| 524 | media_channel_->OnReadyToSend(false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | bool BaseChannel::PacketIsRtcp(const TransportChannel* channel, |
| 529 | const char* data, size_t len) { |
| 530 | return (channel == rtcp_transport_channel_ || |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 531 | rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 532 | } |
| 533 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 534 | bool BaseChannel::SendPacket(bool rtcp, |
| 535 | rtc::Buffer* packet, |
| 536 | const rtc::PacketOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 537 | // SendPacket gets called from MediaEngine, typically on an encoder thread. |
| 538 | // If the thread is not our worker thread, we will post to our worker |
| 539 | // so that the real work happens on our worker. This avoids us having to |
| 540 | // synchronize access to all the pieces of the send path, including |
| 541 | // SRTP and the inner workings of the transport channels. |
| 542 | // The only downside is that we can't return a proper failure code if |
| 543 | // needed. Since UDP is unreliable anyway, this should be a non-issue. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 544 | if (rtc::Thread::Current() != worker_thread_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 545 | // Avoid a copy by transferring the ownership of the packet data. |
| 546 | int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET; |
| 547 | PacketMessageData* data = new PacketMessageData; |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 548 | data->packet = packet->Pass(); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 549 | data->options = options; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 550 | worker_thread_->Post(this, message_id, data); |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | // Now that we are on the correct thread, ensure we have a place to send this |
| 555 | // packet before doing anything. (We might get RTCP packets that we don't |
| 556 | // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP |
| 557 | // transport. |
| 558 | TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ? |
| 559 | transport_channel_ : rtcp_transport_channel_; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 560 | if (!channel || !channel->writable()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 561 | return false; |
| 562 | } |
| 563 | |
| 564 | // Protect ourselves against crazy data. |
| 565 | if (!ValidPacket(rtcp, packet)) { |
| 566 | LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " " |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 567 | << PacketType(rtcp) |
| 568 | << " packet: wrong size=" << packet->size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 572 | rtc::PacketOptions updated_options; |
| 573 | updated_options = options; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 574 | // Protect if needed. |
| 575 | if (srtp_filter_.IsActive()) { |
| 576 | bool res; |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 577 | uint8_t* data = packet->data(); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 578 | int len = static_cast<int>(packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 579 | if (!rtcp) { |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 580 | // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done |
| 581 | // inside libsrtp for a RTP packet. A external HMAC module will be writing |
| 582 | // a fake HMAC value. This is ONLY done for a RTP packet. |
| 583 | // Socket layer will update rtp sendtime extension header if present in |
| 584 | // packet with current time before updating the HMAC. |
| 585 | #if !defined(ENABLE_EXTERNAL_AUTH) |
| 586 | res = srtp_filter_.ProtectRtp( |
| 587 | data, len, static_cast<int>(packet->capacity()), &len); |
| 588 | #else |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 589 | updated_options.packet_time_params.rtp_sendtime_extension_id = |
henrike@webrtc.org | 0537634 | 2014-03-10 15:53:12 +0000 | [diff] [blame] | 590 | rtp_abs_sendtime_extn_id_; |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 591 | res = srtp_filter_.ProtectRtp( |
| 592 | data, len, static_cast<int>(packet->capacity()), &len, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 593 | &updated_options.packet_time_params.srtp_packet_index); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 594 | // If protection succeeds, let's get auth params from srtp. |
| 595 | if (res) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 596 | uint8_t* auth_key = NULL; |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 597 | int key_len; |
| 598 | res = srtp_filter_.GetRtpAuthParams( |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 599 | &auth_key, &key_len, |
| 600 | &updated_options.packet_time_params.srtp_auth_tag_len); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 601 | if (res) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 602 | updated_options.packet_time_params.srtp_auth_key.resize(key_len); |
| 603 | updated_options.packet_time_params.srtp_auth_key.assign( |
| 604 | auth_key, auth_key + key_len); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 608 | if (!res) { |
| 609 | int seq_num = -1; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 610 | uint32_t ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 611 | GetRtpSeqNum(data, len, &seq_num); |
| 612 | GetRtpSsrc(data, len, &ssrc); |
| 613 | LOG(LS_ERROR) << "Failed to protect " << content_name_ |
| 614 | << " RTP packet: size=" << len |
| 615 | << ", seqnum=" << seq_num << ", SSRC=" << ssrc; |
| 616 | return false; |
| 617 | } |
| 618 | } else { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 619 | res = srtp_filter_.ProtectRtcp(data, len, |
| 620 | static_cast<int>(packet->capacity()), |
| 621 | &len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 622 | if (!res) { |
| 623 | int type = -1; |
| 624 | GetRtcpType(data, len, &type); |
| 625 | LOG(LS_ERROR) << "Failed to protect " << content_name_ |
| 626 | << " RTCP packet: size=" << len << ", type=" << type; |
| 627 | return false; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // Update the length of the packet now that we've added the auth tag. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 632 | packet->SetSize(len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 633 | } else if (secure_required_) { |
| 634 | // This is a double check for something that supposedly can't happen. |
| 635 | LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp) |
| 636 | << " packet when SRTP is inactive and crypto is required"; |
| 637 | |
| 638 | ASSERT(false); |
| 639 | return false; |
| 640 | } |
| 641 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 642 | // Bon voyage. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 643 | int ret = |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 644 | channel->SendPacket(packet->data<char>(), packet->size(), updated_options, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 645 | (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0); |
| 646 | if (ret != static_cast<int>(packet->size())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 647 | if (channel->GetError() == EWOULDBLOCK) { |
| 648 | LOG(LS_WARNING) << "Got EWOULDBLOCK from socket."; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 649 | SetReadyToSend(rtcp, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 650 | } |
| 651 | return false; |
| 652 | } |
| 653 | return true; |
| 654 | } |
| 655 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 656 | bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 657 | // Protect ourselves against crazy data. |
| 658 | if (!ValidPacket(rtcp, packet)) { |
| 659 | LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " " |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 660 | << PacketType(rtcp) |
| 661 | << " packet: wrong size=" << packet->size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 662 | return false; |
| 663 | } |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 664 | if (rtcp) { |
| 665 | // Permit all (seemingly valid) RTCP packets. |
| 666 | return true; |
| 667 | } |
| 668 | // Check whether we handle this payload. |
| 669 | return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 670 | } |
| 671 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 672 | void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet, |
| 673 | const rtc::PacketTime& packet_time) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 674 | if (!WantsPacket(rtcp, packet)) { |
| 675 | return; |
| 676 | } |
| 677 | |
honghaiz@google.com | a67ca1a | 2015-01-28 19:48:33 +0000 | [diff] [blame] | 678 | // We are only interested in the first rtp packet because that |
| 679 | // indicates the media has started flowing. |
| 680 | if (!has_received_packet_ && !rtcp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 681 | has_received_packet_ = true; |
| 682 | signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED); |
| 683 | } |
| 684 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 685 | // Unprotect the packet, if needed. |
| 686 | if (srtp_filter_.IsActive()) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 687 | char* data = packet->data<char>(); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 688 | int len = static_cast<int>(packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 689 | bool res; |
| 690 | if (!rtcp) { |
| 691 | res = srtp_filter_.UnprotectRtp(data, len, &len); |
| 692 | if (!res) { |
| 693 | int seq_num = -1; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 694 | uint32_t ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 695 | GetRtpSeqNum(data, len, &seq_num); |
| 696 | GetRtpSsrc(data, len, &ssrc); |
| 697 | LOG(LS_ERROR) << "Failed to unprotect " << content_name_ |
| 698 | << " RTP packet: size=" << len |
| 699 | << ", seqnum=" << seq_num << ", SSRC=" << ssrc; |
| 700 | return; |
| 701 | } |
| 702 | } else { |
| 703 | res = srtp_filter_.UnprotectRtcp(data, len, &len); |
| 704 | if (!res) { |
| 705 | int type = -1; |
| 706 | GetRtcpType(data, len, &type); |
| 707 | LOG(LS_ERROR) << "Failed to unprotect " << content_name_ |
| 708 | << " RTCP packet: size=" << len << ", type=" << type; |
| 709 | return; |
| 710 | } |
| 711 | } |
| 712 | |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 713 | packet->SetSize(len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 714 | } else if (secure_required_) { |
| 715 | // Our session description indicates that SRTP is required, but we got a |
| 716 | // packet before our SRTP filter is active. This means either that |
| 717 | // a) we got SRTP packets before we received the SDES keys, in which case |
| 718 | // we can't decrypt it anyway, or |
| 719 | // b) we got SRTP packets before DTLS completed on both the RTP and RTCP |
| 720 | // channels, so we haven't yet extracted keys, even if DTLS did complete |
| 721 | // on the channel that the packets are being sent on. It's really good |
| 722 | // practice to wait for both RTP and RTCP to be good to go before sending |
| 723 | // media, to prevent weird failure modes, so it's fine for us to just eat |
| 724 | // packets here. This is all sidestepped if RTCP mux is used anyway. |
| 725 | LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp) |
| 726 | << " packet when SRTP is inactive and crypto is required"; |
| 727 | return; |
| 728 | } |
| 729 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 730 | // Push it down to the media channel. |
| 731 | if (!rtcp) { |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 732 | media_channel_->OnPacketReceived(packet, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 733 | } else { |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 734 | media_channel_->OnRtcpReceived(packet, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 738 | bool BaseChannel::PushdownLocalDescription( |
| 739 | const SessionDescription* local_desc, ContentAction action, |
| 740 | std::string* error_desc) { |
| 741 | const ContentInfo* content_info = GetFirstContent(local_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 742 | const MediaContentDescription* content_desc = |
| 743 | GetContentDescription(content_info); |
| 744 | if (content_desc && content_info && !content_info->rejected && |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 745 | !SetLocalContent(content_desc, action, error_desc)) { |
| 746 | LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action; |
| 747 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 748 | } |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 749 | return true; |
| 750 | } |
| 751 | |
| 752 | bool BaseChannel::PushdownRemoteDescription( |
| 753 | const SessionDescription* remote_desc, ContentAction action, |
| 754 | std::string* error_desc) { |
| 755 | const ContentInfo* content_info = GetFirstContent(remote_desc); |
| 756 | const MediaContentDescription* content_desc = |
| 757 | GetContentDescription(content_info); |
| 758 | if (content_desc && content_info && !content_info->rejected && |
| 759 | !SetRemoteContent(content_desc, action, error_desc)) { |
| 760 | LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action; |
| 761 | return false; |
| 762 | } |
| 763 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void BaseChannel::EnableMedia_w() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 767 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 768 | if (enabled_) |
| 769 | return; |
| 770 | |
| 771 | LOG(LS_INFO) << "Channel enabled"; |
| 772 | enabled_ = true; |
| 773 | ChangeState(); |
| 774 | } |
| 775 | |
| 776 | void BaseChannel::DisableMedia_w() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 777 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 778 | if (!enabled_) |
| 779 | return; |
| 780 | |
| 781 | LOG(LS_INFO) << "Channel disabled"; |
| 782 | enabled_ = false; |
| 783 | ChangeState(); |
| 784 | } |
| 785 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 786 | void BaseChannel::UpdateWritableState_w() { |
| 787 | if (transport_channel_ && transport_channel_->writable() && |
| 788 | (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) { |
| 789 | ChannelWritable_w(); |
| 790 | } else { |
| 791 | ChannelNotWritable_w(); |
| 792 | } |
| 793 | } |
| 794 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 795 | void BaseChannel::ChannelWritable_w() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 796 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 797 | if (writable_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 798 | return; |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 799 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 800 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 801 | LOG(LS_INFO) << "Channel writable (" << content_name_ << ")" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 802 | << (was_ever_writable_ ? "" : " for the first time"); |
| 803 | |
| 804 | std::vector<ConnectionInfo> infos; |
| 805 | transport_channel_->GetStats(&infos); |
| 806 | for (std::vector<ConnectionInfo>::const_iterator it = infos.begin(); |
| 807 | it != infos.end(); ++it) { |
| 808 | if (it->best_connection) { |
| 809 | LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString() |
| 810 | << "->" << it->remote_candidate.ToSensitiveString(); |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 815 | was_ever_writable_ = true; |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 816 | MaybeSetupDtlsSrtp_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 817 | writable_ = true; |
| 818 | ChangeState(); |
| 819 | } |
| 820 | |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 821 | void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) { |
| 822 | ASSERT(worker_thread() == rtc::Thread::Current()); |
| 823 | signaling_thread()->Invoke<void>(Bind( |
| 824 | &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp)); |
| 825 | } |
| 826 | |
| 827 | void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) { |
| 828 | ASSERT(signaling_thread() == rtc::Thread::Current()); |
| 829 | SignalDtlsSetupFailure(this, rtcp); |
| 830 | } |
| 831 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 832 | bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) { |
| 833 | std::vector<int> crypto_suites; |
| 834 | // We always use the default SRTP crypto suites for RTCP, but we may use |
| 835 | // different crypto suites for RTP depending on the media type. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 836 | if (!rtcp) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 837 | GetSrtpCryptoSuites(&crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 838 | } else { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 839 | GetDefaultSrtpCryptoSuites(&crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 840 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 841 | return tc->SetSrtpCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | bool BaseChannel::ShouldSetupDtlsSrtp() const { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 845 | // Since DTLS is applied to all channels, checking RTP should be enough. |
| 846 | return transport_channel_ && transport_channel_->IsDtlsActive(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | // This function returns true if either DTLS-SRTP is not in use |
| 850 | // *or* DTLS-SRTP is successfully set up. |
| 851 | bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) { |
| 852 | bool ret = false; |
| 853 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 854 | TransportChannel* channel = |
| 855 | rtcp_channel ? rtcp_transport_channel_ : transport_channel_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 856 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 857 | RTC_DCHECK(channel->IsDtlsActive()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 858 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 859 | int selected_crypto_suite; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 860 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 861 | if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) { |
| 862 | LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 863 | return false; |
| 864 | } |
| 865 | |
| 866 | LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " |
| 867 | << content_name() << " " |
| 868 | << PacketType(rtcp_channel); |
| 869 | |
| 870 | // OK, we're now doing DTLS (RFC 5764) |
| 871 | std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 + |
| 872 | SRTP_MASTER_KEY_SALT_LEN * 2); |
| 873 | |
| 874 | // RFC 5705 exporter using the RFC 5764 parameters |
| 875 | if (!channel->ExportKeyingMaterial( |
| 876 | kDtlsSrtpExporterLabel, |
| 877 | NULL, 0, false, |
| 878 | &dtls_buffer[0], dtls_buffer.size())) { |
| 879 | LOG(LS_WARNING) << "DTLS-SRTP key export failed"; |
| 880 | ASSERT(false); // This should never happen |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | // Sync up the keys with the DTLS-SRTP interface |
| 885 | std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN + |
| 886 | SRTP_MASTER_KEY_SALT_LEN); |
| 887 | std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN + |
| 888 | SRTP_MASTER_KEY_SALT_LEN); |
| 889 | size_t offset = 0; |
| 890 | memcpy(&client_write_key[0], &dtls_buffer[offset], |
| 891 | SRTP_MASTER_KEY_KEY_LEN); |
| 892 | offset += SRTP_MASTER_KEY_KEY_LEN; |
| 893 | memcpy(&server_write_key[0], &dtls_buffer[offset], |
| 894 | SRTP_MASTER_KEY_KEY_LEN); |
| 895 | offset += SRTP_MASTER_KEY_KEY_LEN; |
| 896 | memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN], |
| 897 | &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN); |
| 898 | offset += SRTP_MASTER_KEY_SALT_LEN; |
| 899 | memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN], |
| 900 | &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN); |
| 901 | |
| 902 | std::vector<unsigned char> *send_key, *recv_key; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 903 | rtc::SSLRole role; |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 904 | if (!channel->GetSslRole(&role)) { |
| 905 | LOG(LS_WARNING) << "GetSslRole failed"; |
| 906 | return false; |
| 907 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 908 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 909 | if (role == rtc::SSL_SERVER) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 910 | send_key = &server_write_key; |
| 911 | recv_key = &client_write_key; |
| 912 | } else { |
| 913 | send_key = &client_write_key; |
| 914 | recv_key = &server_write_key; |
| 915 | } |
| 916 | |
| 917 | if (rtcp_channel) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 918 | ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0], |
| 919 | static_cast<int>(send_key->size()), |
| 920 | selected_crypto_suite, &(*recv_key)[0], |
| 921 | static_cast<int>(recv_key->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 922 | } else { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 923 | ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0], |
| 924 | static_cast<int>(send_key->size()), |
| 925 | selected_crypto_suite, &(*recv_key)[0], |
| 926 | static_cast<int>(recv_key->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | if (!ret) |
| 930 | LOG(LS_WARNING) << "DTLS-SRTP key installation failed"; |
| 931 | else |
| 932 | dtls_keyed_ = true; |
| 933 | |
| 934 | return ret; |
| 935 | } |
| 936 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 937 | void BaseChannel::MaybeSetupDtlsSrtp_w() { |
| 938 | if (srtp_filter_.IsActive()) { |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | if (!ShouldSetupDtlsSrtp()) { |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | if (!SetupDtlsSrtp(false)) { |
| 947 | SignalDtlsSetupFailure_w(false); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | if (rtcp_transport_channel_) { |
| 952 | if (!SetupDtlsSrtp(true)) { |
| 953 | SignalDtlsSetupFailure_w(true); |
| 954 | return; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 959 | void BaseChannel::ChannelNotWritable_w() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 960 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 961 | if (!writable_) |
| 962 | return; |
| 963 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 964 | LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 965 | writable_ = false; |
| 966 | ChangeState(); |
| 967 | } |
| 968 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 969 | bool BaseChannel::SetRtpTransportParameters_w( |
| 970 | const MediaContentDescription* content, |
| 971 | ContentAction action, |
| 972 | ContentSource src, |
| 973 | std::string* error_desc) { |
| 974 | if (action == CA_UPDATE) { |
| 975 | // These parameters never get changed by a CA_UDPATE. |
| 976 | return true; |
| 977 | } |
| 978 | |
| 979 | // Cache secure_required_ for belt and suspenders check on SendPacket |
| 980 | if (src == CS_LOCAL) { |
| 981 | set_secure_required(content->crypto_required() != CT_NONE); |
| 982 | } |
| 983 | |
| 984 | if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) { |
| 985 | return false; |
| 986 | } |
| 987 | |
| 988 | if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) { |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | return true; |
| 993 | } |
| 994 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 995 | // |dtls| will be set to true if DTLS is active for transport channel and |
| 996 | // crypto is empty. |
| 997 | bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 998 | bool* dtls, |
| 999 | std::string* error_desc) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1000 | *dtls = transport_channel_->IsDtlsActive(); |
| 1001 | if (*dtls && !cryptos.empty()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1002 | SafeSetError("Cryptos must be empty when DTLS is active.", |
| 1003 | error_desc); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1004 | return false; |
| 1005 | } |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1009 | bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1010 | ContentAction action, |
| 1011 | ContentSource src, |
| 1012 | std::string* error_desc) { |
| 1013 | if (action == CA_UPDATE) { |
| 1014 | // no crypto params. |
| 1015 | return true; |
| 1016 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1017 | bool ret = false; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1018 | bool dtls = false; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1019 | ret = CheckSrtpConfig(cryptos, &dtls, error_desc); |
| 1020 | if (!ret) { |
| 1021 | return false; |
| 1022 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1023 | switch (action) { |
| 1024 | case CA_OFFER: |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1025 | // If DTLS is already active on the channel, we could be renegotiating |
| 1026 | // here. We don't update the srtp filter. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1027 | if (!dtls) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1028 | ret = srtp_filter_.SetOffer(cryptos, src); |
| 1029 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1030 | break; |
| 1031 | case CA_PRANSWER: |
| 1032 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 1033 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1034 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1035 | ret = srtp_filter_.SetProvisionalAnswer(cryptos, src); |
| 1036 | } |
| 1037 | break; |
| 1038 | case CA_ANSWER: |
| 1039 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 1040 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1041 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1042 | ret = srtp_filter_.SetAnswer(cryptos, src); |
| 1043 | } |
| 1044 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1045 | default: |
| 1046 | break; |
| 1047 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1048 | if (!ret) { |
| 1049 | SafeSetError("Failed to setup SRTP filter.", error_desc); |
| 1050 | return false; |
| 1051 | } |
| 1052 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1055 | void BaseChannel::ActivateRtcpMux() { |
| 1056 | worker_thread_->Invoke<void>(Bind( |
| 1057 | &BaseChannel::ActivateRtcpMux_w, this)); |
| 1058 | } |
| 1059 | |
| 1060 | void BaseChannel::ActivateRtcpMux_w() { |
| 1061 | if (!rtcp_mux_filter_.IsActive()) { |
| 1062 | rtcp_mux_filter_.SetActive(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1063 | set_rtcp_transport_channel(nullptr); |
| 1064 | rtcp_transport_enabled_ = false; |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1068 | bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1069 | ContentSource src, |
| 1070 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1071 | bool ret = false; |
| 1072 | switch (action) { |
| 1073 | case CA_OFFER: |
| 1074 | ret = rtcp_mux_filter_.SetOffer(enable, src); |
| 1075 | break; |
| 1076 | case CA_PRANSWER: |
| 1077 | ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src); |
| 1078 | break; |
| 1079 | case CA_ANSWER: |
| 1080 | ret = rtcp_mux_filter_.SetAnswer(enable, src); |
| 1081 | if (ret && rtcp_mux_filter_.IsActive()) { |
| 1082 | // We activated RTCP mux, close down the RTCP transport. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1083 | LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name() |
| 1084 | << " by destroying RTCP transport channel for " |
| 1085 | << transport_name(); |
| 1086 | set_rtcp_transport_channel(nullptr); |
| 1087 | rtcp_transport_enabled_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1088 | } |
| 1089 | break; |
| 1090 | case CA_UPDATE: |
| 1091 | // No RTCP mux info. |
| 1092 | ret = true; |
Henrik Kjellander | 7c027b6 | 2015-04-22 13:21:30 +0200 | [diff] [blame] | 1093 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1094 | default: |
| 1095 | break; |
| 1096 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1097 | if (!ret) { |
| 1098 | SafeSetError("Failed to setup RTCP mux filter.", error_desc); |
| 1099 | return false; |
| 1100 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1101 | // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or |
| 1102 | // CA_ANSWER, but we only want to tear down the RTCP transport channel if we |
| 1103 | // received a final answer. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1104 | if (rtcp_mux_filter_.IsActive()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1105 | // If the RTP transport is already writable, then so are we. |
| 1106 | if (transport_channel_->writable()) { |
| 1107 | ChannelWritable_w(); |
| 1108 | } |
| 1109 | } |
| 1110 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1111 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | bool BaseChannel::AddRecvStream_w(const StreamParams& sp) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1115 | ASSERT(worker_thread() == rtc::Thread::Current()); |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 1116 | return media_channel()->AddRecvStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1119 | bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1120 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1121 | return media_channel()->RemoveRecvStream(ssrc); |
| 1122 | } |
| 1123 | |
| 1124 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1125 | ContentAction action, |
| 1126 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1127 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1128 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1129 | return false; |
| 1130 | |
| 1131 | // If this is an update, streams only contain streams that have changed. |
| 1132 | if (action == CA_UPDATE) { |
| 1133 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1134 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1135 | const StreamParams* existing_stream = |
| 1136 | GetStreamByIds(local_streams_, it->groupid, it->id); |
| 1137 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1138 | if (media_channel()->AddSendStream(*it)) { |
| 1139 | local_streams_.push_back(*it); |
| 1140 | LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc(); |
| 1141 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1142 | std::ostringstream desc; |
| 1143 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1144 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1145 | return false; |
| 1146 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1147 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1148 | if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1149 | std::ostringstream desc; |
| 1150 | desc << "Failed to remove send stream with ssrc " |
| 1151 | << it->first_ssrc() << "."; |
| 1152 | SafeSetError(desc.str(), error_desc); |
| 1153 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1154 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1155 | RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1156 | } else { |
| 1157 | LOG(LS_WARNING) << "Ignore unsupported stream update"; |
| 1158 | } |
| 1159 | } |
| 1160 | return true; |
| 1161 | } |
| 1162 | // Else streams are all the streams we want to send. |
| 1163 | |
| 1164 | // Check for streams that have been removed. |
| 1165 | bool ret = true; |
| 1166 | for (StreamParamsVec::const_iterator it = local_streams_.begin(); |
| 1167 | it != local_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1168 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1169 | if (!media_channel()->RemoveSendStream(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1170 | std::ostringstream desc; |
| 1171 | desc << "Failed to remove send stream with ssrc " |
| 1172 | << it->first_ssrc() << "."; |
| 1173 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1174 | ret = false; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | // Check for new streams. |
| 1179 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1180 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1181 | if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1182 | if (media_channel()->AddSendStream(*it)) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1183 | LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1184 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1185 | std::ostringstream desc; |
| 1186 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1187 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1188 | ret = false; |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | local_streams_ = streams; |
| 1193 | return ret; |
| 1194 | } |
| 1195 | |
| 1196 | bool BaseChannel::UpdateRemoteStreams_w( |
| 1197 | const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1198 | ContentAction action, |
| 1199 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1200 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1201 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1202 | return false; |
| 1203 | |
| 1204 | // If this is an update, streams only contain streams that have changed. |
| 1205 | if (action == CA_UPDATE) { |
| 1206 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1207 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1208 | const StreamParams* existing_stream = |
| 1209 | GetStreamByIds(remote_streams_, it->groupid, it->id); |
| 1210 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1211 | if (AddRecvStream_w(*it)) { |
| 1212 | remote_streams_.push_back(*it); |
| 1213 | LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc(); |
| 1214 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1215 | std::ostringstream desc; |
| 1216 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1217 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1218 | return false; |
| 1219 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1220 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1221 | if (!RemoveRecvStream_w(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1222 | std::ostringstream desc; |
| 1223 | desc << "Failed to remove remote stream with ssrc " |
| 1224 | << it->first_ssrc() << "."; |
| 1225 | SafeSetError(desc.str(), error_desc); |
| 1226 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1227 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1228 | RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1229 | } else { |
| 1230 | LOG(LS_WARNING) << "Ignore unsupported stream update." |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1231 | << " Stream exists? " << (existing_stream != nullptr) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1232 | << " new stream = " << it->ToString(); |
| 1233 | } |
| 1234 | } |
| 1235 | return true; |
| 1236 | } |
| 1237 | // Else streams are all the streams we want to receive. |
| 1238 | |
| 1239 | // Check for streams that have been removed. |
| 1240 | bool ret = true; |
| 1241 | for (StreamParamsVec::const_iterator it = remote_streams_.begin(); |
| 1242 | it != remote_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1243 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1244 | if (!RemoveRecvStream_w(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1245 | std::ostringstream desc; |
| 1246 | desc << "Failed to remove remote stream with ssrc " |
| 1247 | << it->first_ssrc() << "."; |
| 1248 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1249 | ret = false; |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | // Check for new streams. |
| 1254 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1255 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1256 | if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1257 | if (AddRecvStream_w(*it)) { |
| 1258 | LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0]; |
| 1259 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1260 | std::ostringstream desc; |
| 1261 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1262 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1263 | ret = false; |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | remote_streams_ = streams; |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1271 | void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension( |
| 1272 | const std::vector<RtpHeaderExtension>& extensions) { |
| 1273 | const RtpHeaderExtension* send_time_extension = |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1274 | FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1275 | rtp_abs_sendtime_extn_id_ = |
| 1276 | send_time_extension ? send_time_extension->id : -1; |
| 1277 | } |
| 1278 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1279 | void BaseChannel::OnMessage(rtc::Message *pmsg) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 1280 | TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1281 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1282 | case MSG_RTPPACKET: |
| 1283 | case MSG_RTCPPACKET: { |
| 1284 | PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1285 | SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, |
| 1286 | data->options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1287 | delete data; // because it is Posted |
| 1288 | break; |
| 1289 | } |
| 1290 | case MSG_FIRSTPACKETRECEIVED: { |
| 1291 | SignalFirstPacketReceived(this); |
| 1292 | break; |
| 1293 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1297 | void BaseChannel::FlushRtcpMessages() { |
| 1298 | // Flush all remaining RTCP messages. This should only be called in |
| 1299 | // destructor. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1300 | ASSERT(rtc::Thread::Current() == worker_thread_); |
| 1301 | rtc::MessageList rtcp_messages; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1302 | worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1303 | for (rtc::MessageList::iterator it = rtcp_messages.begin(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1304 | it != rtcp_messages.end(); ++it) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1305 | worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1309 | VoiceChannel::VoiceChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1310 | MediaEngineInterface* media_engine, |
| 1311 | VoiceMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1312 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1313 | const std::string& content_name, |
| 1314 | bool rtcp) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1315 | : BaseChannel(thread, |
| 1316 | media_channel, |
| 1317 | transport_controller, |
| 1318 | content_name, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1319 | rtcp), |
Fredrik Solenberg | 0c02264 | 2015-08-05 12:25:22 +0200 | [diff] [blame] | 1320 | media_engine_(media_engine), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1321 | received_media_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1322 | |
| 1323 | VoiceChannel::~VoiceChannel() { |
| 1324 | StopAudioMonitor(); |
| 1325 | StopMediaMonitor(); |
| 1326 | // this can't be done in the base class, since it calls a virtual |
| 1327 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1328 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | bool VoiceChannel::Init() { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 1332 | if (!BaseChannel::Init()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1333 | return false; |
| 1334 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1335 | return true; |
| 1336 | } |
| 1337 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1338 | bool VoiceChannel::SetAudioSend(uint32_t ssrc, |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1339 | bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1340 | const AudioOptions* options, |
| 1341 | AudioRenderer* renderer) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1342 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(), |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1343 | ssrc, enable, options, renderer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1346 | // TODO(juberti): Handle early media the right way. We should get an explicit |
| 1347 | // ringing message telling us to start playing local ringback, which we cancel |
| 1348 | // if any early media actually arrives. For now, we do the opposite, which is |
| 1349 | // to wait 1 second for early media, and start playing local ringback if none |
| 1350 | // arrives. |
| 1351 | void VoiceChannel::SetEarlyMedia(bool enable) { |
| 1352 | if (enable) { |
| 1353 | // Start the early media timeout |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1354 | worker_thread()->PostDelayed(kEarlyMediaTimeout, this, |
| 1355 | MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1356 | } else { |
| 1357 | // Stop the timeout if currently going. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1358 | worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1362 | bool VoiceChannel::CanInsertDtmf() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1363 | return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf, |
| 1364 | media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1367 | bool VoiceChannel::InsertDtmf(uint32_t ssrc, |
| 1368 | int event_code, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1369 | int duration) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1370 | return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1371 | ssrc, event_code, duration)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 1374 | bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) { |
| 1375 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume, |
| 1376 | media_channel(), ssrc, volume)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1377 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1378 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1379 | bool VoiceChannel::GetStats(VoiceMediaInfo* stats) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1380 | return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats, |
| 1381 | media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | void VoiceChannel::StartMediaMonitor(int cms) { |
| 1385 | media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1386 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1387 | media_monitor_->SignalUpdate.connect( |
| 1388 | this, &VoiceChannel::OnMediaMonitorUpdate); |
| 1389 | media_monitor_->Start(cms); |
| 1390 | } |
| 1391 | |
| 1392 | void VoiceChannel::StopMediaMonitor() { |
| 1393 | if (media_monitor_) { |
| 1394 | media_monitor_->Stop(); |
| 1395 | media_monitor_->SignalUpdate.disconnect(this); |
| 1396 | media_monitor_.reset(); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | void VoiceChannel::StartAudioMonitor(int cms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1401 | audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1402 | audio_monitor_ |
| 1403 | ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate); |
| 1404 | audio_monitor_->Start(cms); |
| 1405 | } |
| 1406 | |
| 1407 | void VoiceChannel::StopAudioMonitor() { |
| 1408 | if (audio_monitor_) { |
| 1409 | audio_monitor_->Stop(); |
| 1410 | audio_monitor_.reset(); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | bool VoiceChannel::IsAudioMonitorRunning() const { |
| 1415 | return (audio_monitor_.get() != NULL); |
| 1416 | } |
| 1417 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1418 | int VoiceChannel::GetInputLevel_w() { |
Fredrik Solenberg | 0c02264 | 2015-08-05 12:25:22 +0200 | [diff] [blame] | 1419 | return media_engine_->GetInputLevel(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | int VoiceChannel::GetOutputLevel_w() { |
| 1423 | return media_channel()->GetOutputLevel(); |
| 1424 | } |
| 1425 | |
| 1426 | void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) { |
| 1427 | media_channel()->GetActiveStreams(actives); |
| 1428 | } |
| 1429 | |
| 1430 | void VoiceChannel::OnChannelRead(TransportChannel* channel, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1431 | const char* data, size_t len, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1432 | const rtc::PacketTime& packet_time, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1433 | int flags) { |
| 1434 | BaseChannel::OnChannelRead(channel, data, len, packet_time, flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Set a flag when we've received an RTP packet. If we're waiting for early |
| 1437 | // media, this will disable the timeout. |
| 1438 | if (!received_media_ && !PacketIsRtcp(channel, data, len)) { |
| 1439 | received_media_ = true; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | void VoiceChannel::ChangeState() { |
| 1444 | // Render incoming data if we're the active call, and we have the local |
| 1445 | // content. We receive data on the default channel and multiplexed streams. |
| 1446 | bool recv = IsReadyToReceive(); |
solenberg | 5b14b42 | 2015-10-01 04:10:31 -0700 | [diff] [blame] | 1447 | media_channel()->SetPlayout(recv); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1448 | |
| 1449 | // Send outgoing data if we're the active call, we have the remote content, |
| 1450 | // and we have had some form of connectivity. |
| 1451 | bool send = IsReadyToSend(); |
| 1452 | SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING; |
| 1453 | if (!media_channel()->SetSend(send_flag)) { |
| 1454 | LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send; |
| 1458 | } |
| 1459 | |
| 1460 | const ContentInfo* VoiceChannel::GetFirstContent( |
| 1461 | const SessionDescription* sdesc) { |
| 1462 | return GetFirstAudioContent(sdesc); |
| 1463 | } |
| 1464 | |
| 1465 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1466 | ContentAction action, |
| 1467 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1468 | TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w"); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1469 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1470 | LOG(LS_INFO) << "Setting local voice description"; |
| 1471 | |
| 1472 | const AudioContentDescription* audio = |
| 1473 | static_cast<const AudioContentDescription*>(content); |
| 1474 | ASSERT(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1475 | if (!audio) { |
| 1476 | SafeSetError("Can't find audio content in local description.", error_desc); |
| 1477 | return false; |
| 1478 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1479 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1480 | if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) { |
| 1481 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1484 | AudioRecvParameters recv_params = last_recv_params_; |
| 1485 | RtpParametersFromMediaDescription(audio, &recv_params); |
| 1486 | if (!media_channel()->SetRecvParameters(recv_params)) { |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1487 | SafeSetError("Failed to set local audio description recv parameters.", |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1488 | error_desc); |
| 1489 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1490 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1491 | for (const AudioCodec& codec : audio->codecs()) { |
| 1492 | bundle_filter()->AddPayloadType(codec.id); |
| 1493 | } |
| 1494 | last_recv_params_ = recv_params; |
| 1495 | |
| 1496 | // TODO(pthatcher): Move local streams into AudioSendParameters, and |
| 1497 | // only give it to the media channel once we have a remote |
| 1498 | // description too (without a remote description, we won't be able |
| 1499 | // to send them anyway). |
| 1500 | if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) { |
| 1501 | SafeSetError("Failed to set local audio description streams.", error_desc); |
| 1502 | return false; |
| 1503 | } |
| 1504 | |
| 1505 | set_local_content_direction(content->direction()); |
| 1506 | ChangeState(); |
| 1507 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1511 | ContentAction action, |
| 1512 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1513 | TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w"); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1514 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1515 | LOG(LS_INFO) << "Setting remote voice description"; |
| 1516 | |
| 1517 | const AudioContentDescription* audio = |
| 1518 | static_cast<const AudioContentDescription*>(content); |
| 1519 | ASSERT(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1520 | if (!audio) { |
| 1521 | SafeSetError("Can't find audio content in remote description.", error_desc); |
| 1522 | return false; |
| 1523 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1524 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1525 | if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) { |
| 1526 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1529 | AudioSendParameters send_params = last_send_params_; |
| 1530 | RtpSendParametersFromMediaDescription(audio, &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1531 | if (audio->agc_minus_10db()) { |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 1532 | send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1533 | } |
| 1534 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1535 | SafeSetError("Failed to set remote audio description send parameters.", |
| 1536 | error_desc); |
| 1537 | return false; |
| 1538 | } |
| 1539 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1540 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1541 | // TODO(pthatcher): Move remote streams into AudioRecvParameters, |
| 1542 | // and only give it to the media channel once we have a local |
| 1543 | // description too (without a local description, we won't be able to |
| 1544 | // recv them anyway). |
| 1545 | if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) { |
| 1546 | SafeSetError("Failed to set remote audio description streams.", error_desc); |
| 1547 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1550 | if (audio->rtp_header_extensions_set()) { |
| 1551 | MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions()); |
| 1552 | } |
| 1553 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1554 | set_remote_content_direction(content->direction()); |
| 1555 | ChangeState(); |
| 1556 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1559 | void VoiceChannel::HandleEarlyMediaTimeout() { |
| 1560 | // This occurs on the main thread, not the worker thread. |
| 1561 | if (!received_media_) { |
| 1562 | LOG(LS_INFO) << "No early media received before timeout"; |
| 1563 | SignalEarlyMediaTimeout(this); |
| 1564 | } |
| 1565 | } |
| 1566 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1567 | bool VoiceChannel::InsertDtmf_w(uint32_t ssrc, |
| 1568 | int event, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1569 | int duration) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1570 | if (!enabled()) { |
| 1571 | return false; |
| 1572 | } |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1573 | return media_channel()->InsertDtmf(ssrc, event, duration); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1576 | void VoiceChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1577 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1578 | case MSG_EARLYMEDIATIMEOUT: |
| 1579 | HandleEarlyMediaTimeout(); |
| 1580 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1581 | case MSG_CHANNEL_ERROR: { |
| 1582 | VoiceChannelErrorMessageData* data = |
| 1583 | static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1584 | delete data; |
| 1585 | break; |
| 1586 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1587 | default: |
| 1588 | BaseChannel::OnMessage(pmsg); |
| 1589 | break; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | void VoiceChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 1594 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1595 | SignalConnectionMonitor(this, infos); |
| 1596 | } |
| 1597 | |
| 1598 | void VoiceChannel::OnMediaMonitorUpdate( |
| 1599 | VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) { |
| 1600 | ASSERT(media_channel == this->media_channel()); |
| 1601 | SignalMediaMonitor(this, info); |
| 1602 | } |
| 1603 | |
| 1604 | void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor, |
| 1605 | const AudioInfo& info) { |
| 1606 | SignalAudioMonitor(this, info); |
| 1607 | } |
| 1608 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1609 | void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const { |
| 1610 | GetSupportedAudioCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1613 | VideoChannel::VideoChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1614 | VideoMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1615 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1616 | const std::string& content_name, |
Fredrik Solenberg | 7fb711f | 2015-04-22 15:30:51 +0200 | [diff] [blame] | 1617 | bool rtcp) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1618 | : BaseChannel(thread, |
| 1619 | media_channel, |
| 1620 | transport_controller, |
| 1621 | content_name, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1622 | rtcp), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1623 | renderer_(NULL), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1624 | previous_we_(rtc::WE_CLOSE) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1625 | |
| 1626 | bool VideoChannel::Init() { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 1627 | if (!BaseChannel::Init()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1628 | return false; |
| 1629 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1630 | return true; |
| 1631 | } |
| 1632 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1633 | VideoChannel::~VideoChannel() { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1634 | std::vector<uint32_t> screencast_ssrcs; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1635 | ScreencastMap::iterator iter; |
| 1636 | while (!screencast_capturers_.empty()) { |
| 1637 | if (!RemoveScreencast(screencast_capturers_.begin()->first)) { |
| 1638 | LOG(LS_ERROR) << "Unable to delete screencast with ssrc " |
| 1639 | << screencast_capturers_.begin()->first; |
| 1640 | ASSERT(false); |
| 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | StopMediaMonitor(); |
| 1646 | // this can't be done in the base class, since it calls a virtual |
| 1647 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1648 | |
| 1649 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1652 | bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1653 | worker_thread()->Invoke<void>(Bind( |
| 1654 | &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1655 | return true; |
| 1656 | } |
| 1657 | |
| 1658 | bool VideoChannel::ApplyViewRequest(const ViewRequest& request) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1659 | return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1662 | bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) { |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1663 | return worker_thread()->Invoke<bool>(Bind( |
| 1664 | &VideoChannel::AddScreencast_w, this, ssrc, capturer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1667 | bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1668 | return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer, |
| 1669 | media_channel(), ssrc, capturer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1672 | bool VideoChannel::RemoveScreencast(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1673 | return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | bool VideoChannel::IsScreencasting() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1677 | return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1680 | int VideoChannel::GetScreencastFps(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1681 | ScreencastDetailsData data(ssrc); |
| 1682 | worker_thread()->Invoke<void>(Bind( |
| 1683 | &VideoChannel::GetScreencastDetails_w, this, &data)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1684 | return data.fps; |
| 1685 | } |
| 1686 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1687 | int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1688 | ScreencastDetailsData data(ssrc); |
| 1689 | worker_thread()->Invoke<void>(Bind( |
| 1690 | &VideoChannel::GetScreencastDetails_w, this, &data)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1691 | return data.screencast_max_pixels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | bool VideoChannel::SendIntraFrame() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1695 | worker_thread()->Invoke<void>(Bind( |
| 1696 | &VideoMediaChannel::SendIntraFrame, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1697 | return true; |
| 1698 | } |
| 1699 | |
| 1700 | bool VideoChannel::RequestIntraFrame() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1701 | worker_thread()->Invoke<void>(Bind( |
| 1702 | &VideoMediaChannel::RequestIntraFrame, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1703 | return true; |
| 1704 | } |
| 1705 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1706 | bool VideoChannel::SetVideoSend(uint32_t ssrc, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1707 | bool mute, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1708 | const VideoOptions* options) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1709 | return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(), |
| 1710 | ssrc, mute, options)); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1713 | void VideoChannel::ChangeState() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1714 | // Send outgoing data if we're the active call, we have the remote content, |
| 1715 | // and we have had some form of connectivity. |
| 1716 | bool send = IsReadyToSend(); |
| 1717 | if (!media_channel()->SetSend(send)) { |
| 1718 | LOG(LS_ERROR) << "Failed to SetSend on video channel"; |
| 1719 | // TODO(gangji): Report error back to server. |
| 1720 | } |
| 1721 | |
Peter Boström | 34fbfff | 2015-09-24 19:20:30 +0200 | [diff] [blame] | 1722 | LOG(LS_INFO) << "Changing video state, send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1725 | bool VideoChannel::GetStats(VideoMediaInfo* stats) { |
| 1726 | return InvokeOnWorker( |
| 1727 | Bind(&VideoMediaChannel::GetStats, media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | void VideoChannel::StartMediaMonitor(int cms) { |
| 1731 | media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1732 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1733 | media_monitor_->SignalUpdate.connect( |
| 1734 | this, &VideoChannel::OnMediaMonitorUpdate); |
| 1735 | media_monitor_->Start(cms); |
| 1736 | } |
| 1737 | |
| 1738 | void VideoChannel::StopMediaMonitor() { |
| 1739 | if (media_monitor_) { |
| 1740 | media_monitor_->Stop(); |
| 1741 | media_monitor_.reset(); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | const ContentInfo* VideoChannel::GetFirstContent( |
| 1746 | const SessionDescription* sdesc) { |
| 1747 | return GetFirstVideoContent(sdesc); |
| 1748 | } |
| 1749 | |
| 1750 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1751 | ContentAction action, |
| 1752 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1753 | TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w"); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1754 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1755 | LOG(LS_INFO) << "Setting local video description"; |
| 1756 | |
| 1757 | const VideoContentDescription* video = |
| 1758 | static_cast<const VideoContentDescription*>(content); |
| 1759 | ASSERT(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1760 | if (!video) { |
| 1761 | SafeSetError("Can't find video content in local description.", error_desc); |
| 1762 | return false; |
| 1763 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1764 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1765 | if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) { |
| 1766 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1769 | VideoRecvParameters recv_params = last_recv_params_; |
| 1770 | RtpParametersFromMediaDescription(video, &recv_params); |
| 1771 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 1772 | SafeSetError("Failed to set local video description recv parameters.", |
| 1773 | error_desc); |
| 1774 | return false; |
| 1775 | } |
| 1776 | for (const VideoCodec& codec : video->codecs()) { |
| 1777 | bundle_filter()->AddPayloadType(codec.id); |
| 1778 | } |
| 1779 | last_recv_params_ = recv_params; |
| 1780 | |
| 1781 | // TODO(pthatcher): Move local streams into VideoSendParameters, and |
| 1782 | // only give it to the media channel once we have a remote |
| 1783 | // description too (without a remote description, we won't be able |
| 1784 | // to send them anyway). |
| 1785 | if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) { |
| 1786 | SafeSetError("Failed to set local video description streams.", error_desc); |
| 1787 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1790 | set_local_content_direction(content->direction()); |
| 1791 | ChangeState(); |
| 1792 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1796 | ContentAction action, |
| 1797 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1798 | TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w"); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1799 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1800 | LOG(LS_INFO) << "Setting remote video description"; |
| 1801 | |
| 1802 | const VideoContentDescription* video = |
| 1803 | static_cast<const VideoContentDescription*>(content); |
| 1804 | ASSERT(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1805 | if (!video) { |
| 1806 | SafeSetError("Can't find video content in remote description.", error_desc); |
| 1807 | return false; |
| 1808 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1809 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1810 | |
| 1811 | if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) { |
| 1812 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1815 | VideoSendParameters send_params = last_send_params_; |
| 1816 | RtpSendParametersFromMediaDescription(video, &send_params); |
| 1817 | if (video->conference_mode()) { |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 1818 | send_params.options.conference_mode = rtc::Optional<bool>(true); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1819 | } |
| 1820 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1821 | SafeSetError("Failed to set remote video description send parameters.", |
| 1822 | error_desc); |
| 1823 | return false; |
| 1824 | } |
| 1825 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1826 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1827 | // TODO(pthatcher): Move remote streams into VideoRecvParameters, |
| 1828 | // and only give it to the media channel once we have a local |
| 1829 | // description too (without a local description, we won't be able to |
| 1830 | // recv them anyway). |
| 1831 | if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) { |
| 1832 | SafeSetError("Failed to set remote video description streams.", error_desc); |
| 1833 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1836 | if (video->rtp_header_extensions_set()) { |
| 1837 | MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1838 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1839 | |
| 1840 | set_remote_content_direction(content->direction()); |
| 1841 | ChangeState(); |
| 1842 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) { |
| 1846 | bool ret = true; |
| 1847 | // Set the send format for each of the local streams. If the view request |
| 1848 | // does not contain a local stream, set its send format to 0x0, which will |
| 1849 | // drop all frames. |
| 1850 | for (std::vector<StreamParams>::const_iterator it = local_streams().begin(); |
| 1851 | it != local_streams().end(); ++it) { |
| 1852 | VideoFormat format(0, 0, 0, cricket::FOURCC_I420); |
| 1853 | StaticVideoViews::const_iterator view; |
| 1854 | for (view = request.static_video_views.begin(); |
| 1855 | view != request.static_video_views.end(); ++view) { |
| 1856 | if (view->selector.Matches(*it)) { |
| 1857 | format.width = view->width; |
| 1858 | format.height = view->height; |
| 1859 | format.interval = cricket::VideoFormat::FpsToInterval(view->framerate); |
| 1860 | break; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format); |
| 1865 | } |
| 1866 | |
| 1867 | // Check if the view request has invalid streams. |
| 1868 | for (StaticVideoViews::const_iterator it = request.static_video_views.begin(); |
| 1869 | it != request.static_video_views.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1870 | if (!GetStream(local_streams(), it->selector)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1871 | LOG(LS_WARNING) << "View request for (" |
| 1872 | << it->selector.ssrc << ", '" |
| 1873 | << it->selector.groupid << "', '" |
| 1874 | << it->selector.streamid << "'" |
| 1875 | << ") is not in the local streams."; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | return ret; |
| 1880 | } |
| 1881 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1882 | bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1883 | if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) { |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1884 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1885 | } |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1886 | capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange); |
| 1887 | screencast_capturers_[ssrc] = capturer; |
| 1888 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1891 | bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1892 | ScreencastMap::iterator iter = screencast_capturers_.find(ssrc); |
| 1893 | if (iter == screencast_capturers_.end()) { |
| 1894 | return false; |
| 1895 | } |
| 1896 | // Clean up VideoCapturer. |
| 1897 | delete iter->second; |
| 1898 | screencast_capturers_.erase(iter); |
| 1899 | return true; |
| 1900 | } |
| 1901 | |
| 1902 | bool VideoChannel::IsScreencasting_w() const { |
| 1903 | return !screencast_capturers_.empty(); |
| 1904 | } |
| 1905 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1906 | void VideoChannel::GetScreencastDetails_w( |
| 1907 | ScreencastDetailsData* data) const { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1908 | ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1909 | if (iter == screencast_capturers_.end()) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1910 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1911 | } |
| 1912 | VideoCapturer* capturer = iter->second; |
| 1913 | const VideoFormat* video_format = capturer->GetCaptureFormat(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1914 | data->fps = VideoFormat::IntervalToFps(video_format->interval); |
| 1915 | data->screencast_max_pixels = capturer->screencast_max_pixels(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1918 | void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1919 | rtc::WindowEvent we) { |
| 1920 | ASSERT(signaling_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1921 | SignalScreencastWindowEvent(ssrc, we); |
| 1922 | } |
| 1923 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1924 | void VideoChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1925 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1926 | case MSG_SCREENCASTWINDOWEVENT: { |
| 1927 | const ScreencastEventMessageData* data = |
| 1928 | static_cast<ScreencastEventMessageData*>(pmsg->pdata); |
| 1929 | OnScreencastWindowEvent_s(data->ssrc, data->event); |
| 1930 | delete data; |
| 1931 | break; |
| 1932 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1933 | case MSG_CHANNEL_ERROR: { |
| 1934 | const VideoChannelErrorMessageData* data = |
| 1935 | static_cast<VideoChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1936 | delete data; |
| 1937 | break; |
| 1938 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1939 | default: |
| 1940 | BaseChannel::OnMessage(pmsg); |
| 1941 | break; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | void VideoChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 1946 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1947 | SignalConnectionMonitor(this, infos); |
| 1948 | } |
| 1949 | |
| 1950 | // TODO(pthatcher): Look into removing duplicate code between |
| 1951 | // audio, video, and data, perhaps by using templates. |
| 1952 | void VideoChannel::OnMediaMonitorUpdate( |
| 1953 | VideoMediaChannel* media_channel, const VideoMediaInfo &info) { |
| 1954 | ASSERT(media_channel == this->media_channel()); |
| 1955 | SignalMediaMonitor(this, info); |
| 1956 | } |
| 1957 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1958 | void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1959 | rtc::WindowEvent event) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1960 | ScreencastEventMessageData* pdata = |
| 1961 | new ScreencastEventMessageData(ssrc, event); |
| 1962 | signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata); |
| 1963 | } |
| 1964 | |
| 1965 | void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) { |
| 1966 | // Map capturer events to window events. In the future we may want to simply |
| 1967 | // pass these events up directly. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1968 | rtc::WindowEvent we; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1969 | if (ev == CS_STOPPED) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1970 | we = rtc::WE_CLOSE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1971 | } else if (ev == CS_PAUSED) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1972 | we = rtc::WE_MINIMIZE; |
| 1973 | } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) { |
| 1974 | we = rtc::WE_RESTORE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1975 | } else { |
| 1976 | return; |
| 1977 | } |
| 1978 | previous_we_ = we; |
| 1979 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1980 | uint32_t ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1981 | if (!GetLocalSsrc(capturer, &ssrc)) { |
| 1982 | return; |
| 1983 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1984 | |
| 1985 | OnScreencastWindowEvent(ssrc, we); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1988 | bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1989 | *ssrc = 0; |
| 1990 | for (ScreencastMap::iterator iter = screencast_capturers_.begin(); |
| 1991 | iter != screencast_capturers_.end(); ++iter) { |
| 1992 | if (iter->second == capturer) { |
| 1993 | *ssrc = iter->first; |
| 1994 | return true; |
| 1995 | } |
| 1996 | } |
| 1997 | return false; |
| 1998 | } |
| 1999 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 2000 | void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const { |
| 2001 | GetSupportedVideoCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2004 | DataChannel::DataChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2005 | DataMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2006 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2007 | const std::string& content_name, |
| 2008 | bool rtcp) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2009 | : BaseChannel(thread, |
| 2010 | media_channel, |
| 2011 | transport_controller, |
| 2012 | content_name, |
| 2013 | rtcp), |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2014 | data_channel_type_(cricket::DCT_NONE), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2015 | ready_to_send_data_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2016 | |
| 2017 | DataChannel::~DataChannel() { |
| 2018 | StopMediaMonitor(); |
| 2019 | // this can't be done in the base class, since it calls a virtual |
| 2020 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2021 | |
| 2022 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | bool DataChannel::Init() { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 2026 | if (!BaseChannel::Init()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2027 | return false; |
| 2028 | } |
| 2029 | media_channel()->SignalDataReceived.connect( |
| 2030 | this, &DataChannel::OnDataReceived); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2031 | media_channel()->SignalReadyToSend.connect( |
| 2032 | this, &DataChannel::OnDataChannelReadyToSend); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2033 | media_channel()->SignalStreamClosedRemotely.connect( |
| 2034 | this, &DataChannel::OnStreamClosedRemotely); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | bool DataChannel::SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2039 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2040 | SendDataResult* result) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2041 | return InvokeOnWorker(Bind(&DataMediaChannel::SendData, |
| 2042 | media_channel(), params, payload, result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | const ContentInfo* DataChannel::GetFirstContent( |
| 2046 | const SessionDescription* sdesc) { |
| 2047 | return GetFirstDataContent(sdesc); |
| 2048 | } |
| 2049 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2050 | bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2051 | if (data_channel_type_ == DCT_SCTP) { |
| 2052 | // TODO(pthatcher): Do this in a more robust way by checking for |
| 2053 | // SCTP or DTLS. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 2054 | return !IsRtpPacket(packet->data(), packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2055 | } else if (data_channel_type_ == DCT_RTP) { |
| 2056 | return BaseChannel::WantsPacket(rtcp, packet); |
| 2057 | } |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2061 | bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type, |
| 2062 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2063 | // It hasn't been set before, so set it now. |
| 2064 | if (data_channel_type_ == DCT_NONE) { |
| 2065 | data_channel_type_ = new_data_channel_type; |
| 2066 | return true; |
| 2067 | } |
| 2068 | |
| 2069 | // It's been set before, but doesn't match. That's bad. |
| 2070 | if (data_channel_type_ != new_data_channel_type) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2071 | std::ostringstream desc; |
| 2072 | desc << "Data channel type mismatch." |
| 2073 | << " Expected " << data_channel_type_ |
| 2074 | << " Got " << new_data_channel_type; |
| 2075 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | // It's hasn't changed. Nothing to do. |
| 2080 | return true; |
| 2081 | } |
| 2082 | |
| 2083 | bool DataChannel::SetDataChannelTypeFromContent( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2084 | const DataContentDescription* content, |
| 2085 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2086 | bool is_sctp = ((content->protocol() == kMediaProtocolSctp) || |
| 2087 | (content->protocol() == kMediaProtocolDtlsSctp)); |
| 2088 | DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2089 | return SetDataChannelType(data_channel_type, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | bool DataChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2093 | ContentAction action, |
| 2094 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 2095 | TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w"); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2096 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2097 | LOG(LS_INFO) << "Setting local data description"; |
| 2098 | |
| 2099 | const DataContentDescription* data = |
| 2100 | static_cast<const DataContentDescription*>(content); |
| 2101 | ASSERT(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2102 | if (!data) { |
| 2103 | SafeSetError("Can't find data content in local description.", error_desc); |
| 2104 | return false; |
| 2105 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2106 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2107 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2108 | return false; |
| 2109 | } |
| 2110 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2111 | if (data_channel_type_ == DCT_RTP) { |
| 2112 | if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) { |
| 2113 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2117 | // FYI: We send the SCTP port number (not to be confused with the |
| 2118 | // underlying UDP port number) as a codec parameter. So even SCTP |
| 2119 | // data channels need codecs. |
| 2120 | DataRecvParameters recv_params = last_recv_params_; |
| 2121 | RtpParametersFromMediaDescription(data, &recv_params); |
| 2122 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 2123 | SafeSetError("Failed to set remote data description recv parameters.", |
| 2124 | error_desc); |
| 2125 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2126 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2127 | if (data_channel_type_ == DCT_RTP) { |
| 2128 | for (const DataCodec& codec : data->codecs()) { |
| 2129 | bundle_filter()->AddPayloadType(codec.id); |
| 2130 | } |
| 2131 | } |
| 2132 | last_recv_params_ = recv_params; |
| 2133 | |
| 2134 | // TODO(pthatcher): Move local streams into DataSendParameters, and |
| 2135 | // only give it to the media channel once we have a remote |
| 2136 | // description too (without a remote description, we won't be able |
| 2137 | // to send them anyway). |
| 2138 | if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) { |
| 2139 | SafeSetError("Failed to set local data description streams.", error_desc); |
| 2140 | return false; |
| 2141 | } |
| 2142 | |
| 2143 | set_local_content_direction(content->direction()); |
| 2144 | ChangeState(); |
| 2145 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2149 | ContentAction action, |
| 2150 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 2151 | TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w"); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2152 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2153 | |
| 2154 | const DataContentDescription* data = |
| 2155 | static_cast<const DataContentDescription*>(content); |
| 2156 | ASSERT(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2157 | if (!data) { |
| 2158 | SafeSetError("Can't find data content in remote description.", error_desc); |
| 2159 | return false; |
| 2160 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2161 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2162 | // If the remote data doesn't have codecs and isn't an update, it |
| 2163 | // must be empty, so ignore it. |
| 2164 | if (!data->has_codecs() && action != CA_UPDATE) { |
| 2165 | return true; |
| 2166 | } |
| 2167 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2168 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2169 | return false; |
| 2170 | } |
| 2171 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2172 | LOG(LS_INFO) << "Setting remote data description"; |
| 2173 | if (data_channel_type_ == DCT_RTP && |
| 2174 | !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) { |
| 2175 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2178 | |
| 2179 | DataSendParameters send_params = last_send_params_; |
| 2180 | RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params); |
| 2181 | if (!media_channel()->SetSendParameters(send_params)) { |
| 2182 | SafeSetError("Failed to set remote data description send parameters.", |
| 2183 | error_desc); |
| 2184 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2185 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2186 | last_send_params_ = send_params; |
| 2187 | |
| 2188 | // TODO(pthatcher): Move remote streams into DataRecvParameters, |
| 2189 | // and only give it to the media channel once we have a local |
| 2190 | // description too (without a local description, we won't be able to |
| 2191 | // recv them anyway). |
| 2192 | if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) { |
| 2193 | SafeSetError("Failed to set remote data description streams.", |
| 2194 | error_desc); |
| 2195 | return false; |
| 2196 | } |
| 2197 | |
| 2198 | set_remote_content_direction(content->direction()); |
| 2199 | ChangeState(); |
| 2200 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | void DataChannel::ChangeState() { |
| 2204 | // Render incoming data if we're the active call, and we have the local |
| 2205 | // content. We receive data on the default channel and multiplexed streams. |
| 2206 | bool recv = IsReadyToReceive(); |
| 2207 | if (!media_channel()->SetReceive(recv)) { |
| 2208 | LOG(LS_ERROR) << "Failed to SetReceive on data channel"; |
| 2209 | } |
| 2210 | |
| 2211 | // Send outgoing data if we're the active call, we have the remote content, |
| 2212 | // and we have had some form of connectivity. |
| 2213 | bool send = IsReadyToSend(); |
| 2214 | if (!media_channel()->SetSend(send)) { |
| 2215 | LOG(LS_ERROR) << "Failed to SetSend on data channel"; |
| 2216 | } |
| 2217 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2218 | // Trigger SignalReadyToSendData asynchronously. |
| 2219 | OnDataChannelReadyToSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2220 | |
| 2221 | LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send; |
| 2222 | } |
| 2223 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2224 | void DataChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2225 | switch (pmsg->message_id) { |
| 2226 | case MSG_READYTOSENDDATA: { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2227 | DataChannelReadyToSendMessageData* data = |
| 2228 | static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2229 | ready_to_send_data_ = data->data(); |
| 2230 | SignalReadyToSendData(ready_to_send_data_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2231 | delete data; |
| 2232 | break; |
| 2233 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2234 | case MSG_DATARECEIVED: { |
| 2235 | DataReceivedMessageData* data = |
| 2236 | static_cast<DataReceivedMessageData*>(pmsg->pdata); |
| 2237 | SignalDataReceived(this, data->params, data->payload); |
| 2238 | delete data; |
| 2239 | break; |
| 2240 | } |
| 2241 | case MSG_CHANNEL_ERROR: { |
| 2242 | const DataChannelErrorMessageData* data = |
| 2243 | static_cast<DataChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2244 | delete data; |
| 2245 | break; |
| 2246 | } |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2247 | case MSG_STREAMCLOSEDREMOTELY: { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2248 | rtc::TypedMessageData<uint32_t>* data = |
| 2249 | static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2250 | SignalStreamClosedRemotely(data->data()); |
| 2251 | delete data; |
| 2252 | break; |
| 2253 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2254 | default: |
| 2255 | BaseChannel::OnMessage(pmsg); |
| 2256 | break; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | void DataChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 2261 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2262 | SignalConnectionMonitor(this, infos); |
| 2263 | } |
| 2264 | |
| 2265 | void DataChannel::StartMediaMonitor(int cms) { |
| 2266 | media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2267 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2268 | media_monitor_->SignalUpdate.connect( |
| 2269 | this, &DataChannel::OnMediaMonitorUpdate); |
| 2270 | media_monitor_->Start(cms); |
| 2271 | } |
| 2272 | |
| 2273 | void DataChannel::StopMediaMonitor() { |
| 2274 | if (media_monitor_) { |
| 2275 | media_monitor_->Stop(); |
| 2276 | media_monitor_->SignalUpdate.disconnect(this); |
| 2277 | media_monitor_.reset(); |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | void DataChannel::OnMediaMonitorUpdate( |
| 2282 | DataMediaChannel* media_channel, const DataMediaInfo& info) { |
| 2283 | ASSERT(media_channel == this->media_channel()); |
| 2284 | SignalMediaMonitor(this, info); |
| 2285 | } |
| 2286 | |
| 2287 | void DataChannel::OnDataReceived( |
| 2288 | const ReceiveDataParams& params, const char* data, size_t len) { |
| 2289 | DataReceivedMessageData* msg = new DataReceivedMessageData( |
| 2290 | params, data, len); |
| 2291 | signaling_thread()->Post(this, MSG_DATARECEIVED, msg); |
| 2292 | } |
| 2293 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2294 | void DataChannel::OnDataChannelError(uint32_t ssrc, |
| 2295 | DataMediaChannel::Error err) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2296 | DataChannelErrorMessageData* data = new DataChannelErrorMessageData( |
| 2297 | ssrc, err); |
| 2298 | signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data); |
| 2299 | } |
| 2300 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2301 | void DataChannel::OnDataChannelReadyToSend(bool writable) { |
| 2302 | // This is usded for congestion control to indicate that the stream is ready |
| 2303 | // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
| 2304 | // that the transport channel is ready. |
| 2305 | signaling_thread()->Post(this, MSG_READYTOSENDDATA, |
| 2306 | new DataChannelReadyToSendMessageData(writable)); |
| 2307 | } |
| 2308 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 2309 | void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const { |
| 2310 | GetSupportedDataCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | bool DataChannel::ShouldSetupDtlsSrtp() const { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 2314 | return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2317 | void DataChannel::OnStreamClosedRemotely(uint32_t sid) { |
| 2318 | rtc::TypedMessageData<uint32_t>* message = |
| 2319 | new rtc::TypedMessageData<uint32_t>(sid); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2320 | signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message); |
| 2321 | } |
| 2322 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2323 | } // namespace cricket |