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