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