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