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