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