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