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