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