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