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