1 /* 2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef WEBRTC_PC_CHANNEL_H_ 12 #define WEBRTC_PC_CHANNEL_H_ 13 14 #include <map> 15 #include <memory> 16 #include <set> 17 #include <string> 18 #include <utility> 19 #include <vector> 20 21 #include "webrtc/api/call/audio_sink.h" 22 #include "webrtc/base/asyncinvoker.h" 23 #include "webrtc/base/asyncudpsocket.h" 24 #include "webrtc/base/criticalsection.h" 25 #include "webrtc/base/network.h" 26 #include "webrtc/base/sigslot.h" 27 #include "webrtc/base/window.h" 28 #include "webrtc/media/base/mediachannel.h" 29 #include "webrtc/media/base/mediaengine.h" 30 #include "webrtc/media/base/streamparams.h" 31 #include "webrtc/media/base/videosinkinterface.h" 32 #include "webrtc/media/base/videosourceinterface.h" 33 #include "webrtc/p2p/base/transportcontroller.h" 34 #include "webrtc/p2p/client/socketmonitor.h" 35 #include "webrtc/pc/audiomonitor.h" 36 #include "webrtc/pc/bundlefilter.h" 37 #include "webrtc/pc/mediamonitor.h" 38 #include "webrtc/pc/mediasession.h" 39 #include "webrtc/pc/rtcpmuxfilter.h" 40 #include "webrtc/pc/srtpfilter.h" 41 42 namespace rtc { 43 class PacketTransportInterface; 44 } 45 46 namespace webrtc { 47 class AudioSinkInterface; 48 } // namespace webrtc 49 50 namespace cricket { 51 52 struct CryptoParams; 53 class MediaContentDescription; 54 55 // BaseChannel contains logic common to voice and video, including enable, 56 // marshaling calls to a worker and network threads, and connection and media 57 // monitors. 58 // 59 // BaseChannel assumes signaling and other threads are allowed to make 60 // synchronous calls to the worker thread, the worker thread makes synchronous 61 // calls only to the network thread, and the network thread can't be blocked by 62 // other threads. 63 // All methods with _n suffix must be called on network thread, 64 // methods with _w suffix on worker thread 65 // and methods with _s suffix on signaling thread. 66 // Network and worker threads may be the same thread. 67 // 68 // WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS! 69 // This is required to avoid a data race between the destructor modifying the 70 // vtable, and the media channel's thread using BaseChannel as the 71 // NetworkInterface. 72 73 class BaseChannel 74 : public rtc::MessageHandler, public sigslot::has_slots<>, 75 public MediaChannel::NetworkInterface, 76 public ConnectionStatsGetter { 77 public: 78 // |rtcp| represents whether or not this channel uses RTCP. 79 // If |srtp_required| is true, the channel will not send or receive any 80 // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP). 81 BaseChannel(rtc::Thread* worker_thread, 82 rtc::Thread* network_thread, 83 rtc::Thread* signaling_thread, 84 MediaChannel* channel, 85 const std::string& content_name, 86 bool rtcp_mux_required, 87 bool srtp_required); 88 virtual ~BaseChannel(); 89 bool Init_w(TransportChannel* rtp_transport, 90 TransportChannel* rtcp_transport); 91 // Deinit may be called multiple times and is simply ignored if it's already 92 // done. 93 void Deinit(); 94 worker_thread()95 rtc::Thread* worker_thread() const { return worker_thread_; } network_thread()96 rtc::Thread* network_thread() const { return network_thread_; } content_name()97 const std::string& content_name() const { return content_name_; } transport_name()98 const std::string& transport_name() const { return transport_name_; } enabled()99 bool enabled() const { return enabled_; } 100 101 // This function returns true if we are using SRTP. secure()102 bool secure() const { return srtp_filter_.IsActive(); } 103 // The following function returns true if we are using 104 // DTLS-based keying. If you turned off SRTP later, however 105 // you could have secure() == false and dtls_secure() == true. secure_dtls()106 bool secure_dtls() const { return dtls_keyed_; } 107 writable()108 bool writable() const { return writable_; } 109 110 bool SetTransport(TransportChannel* rtp_transport, 111 TransportChannel* rtcp_transport); 112 bool PushdownLocalDescription(const SessionDescription* local_desc, 113 ContentAction action, 114 std::string* error_desc); 115 bool PushdownRemoteDescription(const SessionDescription* remote_desc, 116 ContentAction action, 117 std::string* error_desc); 118 // Channel control 119 bool SetLocalContent(const MediaContentDescription* content, 120 ContentAction action, 121 std::string* error_desc); 122 bool SetRemoteContent(const MediaContentDescription* content, 123 ContentAction action, 124 std::string* error_desc); 125 126 bool Enable(bool enable); 127 128 // Multiplexing 129 bool AddRecvStream(const StreamParams& sp); 130 bool RemoveRecvStream(uint32_t ssrc); 131 bool AddSendStream(const StreamParams& sp); 132 bool RemoveSendStream(uint32_t ssrc); 133 134 // Monitoring 135 void StartConnectionMonitor(int cms); 136 void StopConnectionMonitor(); 137 // For ConnectionStatsGetter, used by ConnectionMonitor 138 bool GetConnectionStats(ConnectionInfos* infos) override; 139 bundle_filter()140 BundleFilter* bundle_filter() { return &bundle_filter_; } 141 local_streams()142 const std::vector<StreamParams>& local_streams() const { 143 return local_streams_; 144 } remote_streams()145 const std::vector<StreamParams>& remote_streams() const { 146 return remote_streams_; 147 } 148 149 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure; 150 void SignalDtlsSrtpSetupFailure_n(bool rtcp); 151 void SignalDtlsSrtpSetupFailure_s(bool rtcp); 152 153 // Used for latency measurements. 154 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived; 155 156 // Forward TransportChannel SignalSentPacket to worker thread. 157 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket; 158 159 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can 160 // be destroyed. 161 // Fired on the network thread. 162 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive; 163 rtp_transport()164 TransportChannel* rtp_transport() const { return rtp_transport_; } rtcp_transport()165 TransportChannel* rtcp_transport() const { return rtcp_transport_; } 166 167 bool NeedsRtcpTransport(); 168 169 // Made public for easier testing. 170 // 171 // Updates "ready to send" for an individual channel, and informs the media 172 // channel that the transport is ready to send if each channel (in use) is 173 // ready to send. This is more specific than just "writable"; it means the 174 // last send didn't return ENOTCONN. 175 // 176 // This should be called whenever a channel's ready-to-send state changes, 177 // or when RTCP muxing becomes active/inactive. 178 void SetTransportChannelReadyToSend(bool rtcp, bool ready); 179 180 // Only public for unit tests. Otherwise, consider protected. 181 int SetOption(SocketType type, rtc::Socket::Option o, int val) 182 override; 183 int SetOption_n(SocketType type, rtc::Socket::Option o, int val); 184 srtp_filter()185 SrtpFilter* srtp_filter() { return &srtp_filter_; } 186 187 virtual cricket::MediaType media_type() = 0; 188 189 bool SetCryptoOptions(const rtc::CryptoOptions& crypto_options); 190 191 // This function returns true if we require SRTP for call setup. srtp_required_for_testing()192 bool srtp_required_for_testing() const { return srtp_required_; } 193 194 protected: media_channel()195 virtual MediaChannel* media_channel() const { return media_channel_; } 196 197 // Sets the |rtp_transport_| (and |rtcp_transport_|, if 198 // |rtcp_enabled_| is true). 199 // This method also updates writability and "ready-to-send" state. 200 bool SetTransport_n(TransportChannel* rtp_transport, 201 TransportChannel* rtcp_transport); 202 203 // This does not update writability or "ready-to-send" state; it just 204 // disconnects from the old channel and connects to the new one. 205 void SetTransportChannel_n(bool rtcp, TransportChannel* new_transport); 206 was_ever_writable()207 bool was_ever_writable() const { return was_ever_writable_; } set_local_content_direction(MediaContentDirection direction)208 void set_local_content_direction(MediaContentDirection direction) { 209 local_content_direction_ = direction; 210 } set_remote_content_direction(MediaContentDirection direction)211 void set_remote_content_direction(MediaContentDirection direction) { 212 remote_content_direction_ = direction; 213 } 214 // These methods verify that: 215 // * The required content description directions have been set. 216 // * The channel is enabled. 217 // * And for sending: 218 // - The SRTP filter is active if it's needed. 219 // - The transport has been writable before, meaning it should be at least 220 // possible to succeed in sending a packet. 221 // 222 // When any of these properties change, UpdateMediaSendRecvState_w should be 223 // called. 224 bool IsReadyToReceiveMedia_w() const; 225 bool IsReadyToSendMedia_w() const; signaling_thread()226 rtc::Thread* signaling_thread() { return signaling_thread_; } 227 228 void ConnectToTransportChannel(TransportChannel* tc); 229 void DisconnectFromTransportChannel(TransportChannel* tc); 230 231 void FlushRtcpMessages_n(); 232 233 // NetworkInterface implementation, called by MediaEngine 234 bool SendPacket(rtc::CopyOnWriteBuffer* packet, 235 const rtc::PacketOptions& options) override; 236 bool SendRtcp(rtc::CopyOnWriteBuffer* packet, 237 const rtc::PacketOptions& options) override; 238 239 // From TransportChannel 240 void OnWritableState(rtc::PacketTransportInterface* transport); 241 virtual void OnPacketRead(rtc::PacketTransportInterface* transport, 242 const char* data, 243 size_t len, 244 const rtc::PacketTime& packet_time, 245 int flags); 246 void OnReadyToSend(rtc::PacketTransportInterface* transport); 247 248 void OnDtlsState(TransportChannel* channel, DtlsTransportState state); 249 250 void OnSelectedCandidatePairChanged( 251 TransportChannel* channel, 252 CandidatePairInterface* selected_candidate_pair, 253 int last_sent_packet_id, 254 bool ready_to_send); 255 256 bool PacketIsRtcp(const rtc::PacketTransportInterface* transport, 257 const char* data, 258 size_t len); 259 bool SendPacket(bool rtcp, 260 rtc::CopyOnWriteBuffer* packet, 261 const rtc::PacketOptions& options); 262 263 bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet); 264 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet, 265 const rtc::PacketTime& packet_time); 266 void OnPacketReceived(bool rtcp, 267 const rtc::CopyOnWriteBuffer& packet, 268 const rtc::PacketTime& packet_time); 269 270 void EnableMedia_w(); 271 void DisableMedia_w(); 272 273 // Performs actions if the RTP/RTCP writable state changed. This should 274 // be called whenever a channel's writable state changes or when RTCP muxing 275 // becomes active/inactive. 276 void UpdateWritableState_n(); 277 void ChannelWritable_n(); 278 void ChannelNotWritable_n(); 279 280 bool AddRecvStream_w(const StreamParams& sp); 281 bool RemoveRecvStream_w(uint32_t ssrc); 282 bool AddSendStream_w(const StreamParams& sp); 283 bool RemoveSendStream_w(uint32_t ssrc); 284 bool ShouldSetupDtlsSrtp_n() const; 285 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters. 286 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter. 287 bool SetupDtlsSrtp_n(bool rtcp_channel); 288 void MaybeSetupDtlsSrtp_n(); 289 // Set the DTLS-SRTP cipher policy on this channel as appropriate. 290 bool SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp); 291 292 // Should be called whenever the conditions for 293 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied). 294 // Updates the send/recv state of the media channel. 295 void UpdateMediaSendRecvState(); 296 virtual void UpdateMediaSendRecvState_w() = 0; 297 298 // Gets the content info appropriate to the channel (audio or video). 299 virtual const ContentInfo* GetFirstContent( 300 const SessionDescription* sdesc) = 0; 301 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams, 302 ContentAction action, 303 std::string* error_desc); 304 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams, 305 ContentAction action, 306 std::string* error_desc); 307 virtual bool SetLocalContent_w(const MediaContentDescription* content, 308 ContentAction action, 309 std::string* error_desc) = 0; 310 virtual bool SetRemoteContent_w(const MediaContentDescription* content, 311 ContentAction action, 312 std::string* error_desc) = 0; 313 bool SetRtpTransportParameters(const MediaContentDescription* content, 314 ContentAction action, 315 ContentSource src, 316 std::string* error_desc); 317 bool SetRtpTransportParameters_n(const MediaContentDescription* content, 318 ContentAction action, 319 ContentSource src, 320 std::string* error_desc); 321 322 // Helper method to get RTP Absoulute SendTime extension header id if 323 // present in remote supported extensions list. 324 void MaybeCacheRtpAbsSendTimeHeaderExtension_w( 325 const std::vector<webrtc::RtpExtension>& extensions); 326 327 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos, 328 bool* dtls, 329 std::string* error_desc); 330 bool SetSrtp_n(const std::vector<CryptoParams>& params, 331 ContentAction action, 332 ContentSource src, 333 std::string* error_desc); 334 bool SetRtcpMux_n(bool enable, 335 ContentAction action, 336 ContentSource src, 337 std::string* error_desc); 338 339 // From MessageHandler 340 void OnMessage(rtc::Message* pmsg) override; 341 crypto_options()342 const rtc::CryptoOptions& crypto_options() const { 343 return crypto_options_; 344 } 345 346 // Handled in derived classes 347 // Get the SRTP crypto suites to use for RTP media 348 virtual void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const = 0; 349 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor, 350 const std::vector<ConnectionInfo>& infos) = 0; 351 352 // Helper function for invoking bool-returning methods on the worker thread. 353 template <class FunctorT> InvokeOnWorker(const rtc::Location & posted_from,const FunctorT & functor)354 bool InvokeOnWorker(const rtc::Location& posted_from, 355 const FunctorT& functor) { 356 return worker_thread_->Invoke<bool>(posted_from, functor); 357 } 358 359 private: 360 bool InitNetwork_n(TransportChannel* rtp_transport, 361 TransportChannel* rtcp_transport); 362 void DisconnectTransportChannels_n(); 363 void SignalSentPacket_n(rtc::PacketTransportInterface* transport, 364 const rtc::SentPacket& sent_packet); 365 void SignalSentPacket_w(const rtc::SentPacket& sent_packet); 366 bool IsReadyToSendMedia_n() const; 367 void CacheRtpAbsSendTimeHeaderExtension_n(int rtp_abs_sendtime_extn_id); 368 int GetTransportOverheadPerPacket() const; 369 void UpdateTransportOverhead(); 370 371 rtc::Thread* const worker_thread_; 372 rtc::Thread* const network_thread_; 373 rtc::Thread* const signaling_thread_; 374 rtc::AsyncInvoker invoker_; 375 376 const std::string content_name_; 377 std::unique_ptr<ConnectionMonitor> connection_monitor_; 378 379 std::string transport_name_; 380 // True if RTCP-multiplexing is required. In other words, no standalone RTCP 381 // transport will ever be used for this channel. 382 const bool rtcp_mux_required_; 383 // TODO(johan): Replace TransportChannel* with rtc::PacketTransportInterface*. 384 TransportChannel* rtp_transport_ = nullptr; 385 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_; 386 TransportChannel* rtcp_transport_ = nullptr; 387 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_; 388 SrtpFilter srtp_filter_; 389 RtcpMuxFilter rtcp_mux_filter_; 390 BundleFilter bundle_filter_; 391 bool rtp_ready_to_send_ = false; 392 bool rtcp_ready_to_send_ = false; 393 bool writable_ = false; 394 bool was_ever_writable_ = false; 395 bool has_received_packet_ = false; 396 bool dtls_keyed_ = false; 397 const bool srtp_required_ = true; 398 rtc::CryptoOptions crypto_options_; 399 int rtp_abs_sendtime_extn_id_ = -1; 400 401 // MediaChannel related members that should be accessed from the worker 402 // thread. 403 MediaChannel* const media_channel_; 404 // Currently the |enabled_| flag is accessed from the signaling thread as 405 // well, but it can be changed only when signaling thread does a synchronous 406 // call to the worker thread, so it should be safe. 407 bool enabled_ = false; 408 std::vector<StreamParams> local_streams_; 409 std::vector<StreamParams> remote_streams_; 410 MediaContentDirection local_content_direction_ = MD_INACTIVE; 411 MediaContentDirection remote_content_direction_ = MD_INACTIVE; 412 CandidatePairInterface* selected_candidate_pair_; 413 }; 414 415 // VoiceChannel is a specialization that adds support for early media, DTMF, 416 // and input/output level monitoring. 417 class VoiceChannel : public BaseChannel { 418 public: 419 VoiceChannel(rtc::Thread* worker_thread, 420 rtc::Thread* network_thread, 421 rtc::Thread* signaling_thread, 422 MediaEngineInterface* media_engine, 423 VoiceMediaChannel* channel, 424 const std::string& content_name, 425 bool rtcp_mux_required, 426 bool srtp_required); 427 ~VoiceChannel(); 428 bool Init_w(TransportChannel* rtp_transport, 429 TransportChannel* rtcp_transport); 430 431 // Configure sending media on the stream with SSRC |ssrc| 432 // If there is only one sending stream SSRC 0 can be used. 433 bool SetAudioSend(uint32_t ssrc, 434 bool enable, 435 const AudioOptions* options, 436 AudioSource* source); 437 438 // downcasts a MediaChannel media_channel()439 VoiceMediaChannel* media_channel() const override { 440 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel()); 441 } 442 443 void SetEarlyMedia(bool enable); 444 // This signal is emitted when we have gone a period of time without 445 // receiving early media. When received, a UI should start playing its 446 // own ringing sound 447 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout; 448 449 // Returns if the telephone-event has been negotiated. 450 bool CanInsertDtmf(); 451 // Send and/or play a DTMF |event| according to the |flags|. 452 // The DTMF out-of-band signal will be used on sending. 453 // The |ssrc| should be either 0 or a valid send stream ssrc. 454 // The valid value for the |event| are 0 which corresponding to DTMF 455 // event 0-9, *, #, A-D. 456 bool InsertDtmf(uint32_t ssrc, int event_code, int duration); 457 bool SetOutputVolume(uint32_t ssrc, double volume); 458 void SetRawAudioSink(uint32_t ssrc, 459 std::unique_ptr<webrtc::AudioSinkInterface> sink); 460 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const; 461 bool SetRtpSendParameters(uint32_t ssrc, 462 const webrtc::RtpParameters& parameters); 463 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const; 464 bool SetRtpReceiveParameters(uint32_t ssrc, 465 const webrtc::RtpParameters& parameters); 466 467 // Get statistics about the current media session. 468 bool GetStats(VoiceMediaInfo* stats); 469 470 // Monitoring functions 471 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&> 472 SignalConnectionMonitor; 473 474 void StartMediaMonitor(int cms); 475 void StopMediaMonitor(); 476 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor; 477 478 void StartAudioMonitor(int cms); 479 void StopAudioMonitor(); 480 bool IsAudioMonitorRunning() const; 481 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor; 482 483 int GetInputLevel_w(); 484 int GetOutputLevel_w(); 485 void GetActiveStreams_w(AudioInfo::StreamList* actives); 486 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const; 487 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters); 488 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const; 489 bool SetRtpReceiveParameters_w(uint32_t ssrc, 490 webrtc::RtpParameters parameters); media_type()491 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; } 492 493 private: 494 // overrides from BaseChannel 495 void OnPacketRead(rtc::PacketTransportInterface* transport, 496 const char* data, 497 size_t len, 498 const rtc::PacketTime& packet_time, 499 int flags) override; 500 void UpdateMediaSendRecvState_w() override; 501 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override; 502 bool SetLocalContent_w(const MediaContentDescription* content, 503 ContentAction action, 504 std::string* error_desc) override; 505 bool SetRemoteContent_w(const MediaContentDescription* content, 506 ContentAction action, 507 std::string* error_desc) override; 508 void HandleEarlyMediaTimeout(); 509 bool InsertDtmf_w(uint32_t ssrc, int event, int duration); 510 bool SetOutputVolume_w(uint32_t ssrc, double volume); 511 bool GetStats_w(VoiceMediaInfo* stats); 512 513 void OnMessage(rtc::Message* pmsg) override; 514 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override; 515 void OnConnectionMonitorUpdate( 516 ConnectionMonitor* monitor, 517 const std::vector<ConnectionInfo>& infos) override; 518 void OnMediaMonitorUpdate(VoiceMediaChannel* media_channel, 519 const VoiceMediaInfo& info); 520 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info); 521 522 static const int kEarlyMediaTimeout = 1000; 523 MediaEngineInterface* media_engine_; 524 bool received_media_; 525 std::unique_ptr<VoiceMediaMonitor> media_monitor_; 526 std::unique_ptr<AudioMonitor> audio_monitor_; 527 528 // Last AudioSendParameters sent down to the media_channel() via 529 // SetSendParameters. 530 AudioSendParameters last_send_params_; 531 // Last AudioRecvParameters sent down to the media_channel() via 532 // SetRecvParameters. 533 AudioRecvParameters last_recv_params_; 534 }; 535 536 // VideoChannel is a specialization for video. 537 class VideoChannel : public BaseChannel { 538 public: 539 VideoChannel(rtc::Thread* worker_thread, 540 rtc::Thread* network_thread, 541 rtc::Thread* signaling_thread, 542 VideoMediaChannel* channel, 543 const std::string& content_name, 544 bool rtcp_mux_required, 545 bool srtp_required); 546 ~VideoChannel(); 547 bool Init_w(TransportChannel* rtp_transport, 548 TransportChannel* rtcp_transport); 549 550 // downcasts a MediaChannel media_channel()551 VideoMediaChannel* media_channel() const override { 552 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel()); 553 } 554 555 bool SetSink(uint32_t ssrc, 556 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink); 557 // Get statistics about the current media session. 558 bool GetStats(VideoMediaInfo* stats); 559 560 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&> 561 SignalConnectionMonitor; 562 563 void StartMediaMonitor(int cms); 564 void StopMediaMonitor(); 565 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor; 566 567 // Register a source and set options. 568 // The |ssrc| must correspond to a registered send stream. 569 bool SetVideoSend(uint32_t ssrc, 570 bool enable, 571 const VideoOptions* options, 572 rtc::VideoSourceInterface<webrtc::VideoFrame>* source); 573 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const; 574 bool SetRtpSendParameters(uint32_t ssrc, 575 const webrtc::RtpParameters& parameters); 576 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const; 577 bool SetRtpReceiveParameters(uint32_t ssrc, 578 const webrtc::RtpParameters& parameters); media_type()579 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; } 580 581 private: 582 // overrides from BaseChannel 583 void UpdateMediaSendRecvState_w() override; 584 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override; 585 bool SetLocalContent_w(const MediaContentDescription* content, 586 ContentAction action, 587 std::string* error_desc) override; 588 bool SetRemoteContent_w(const MediaContentDescription* content, 589 ContentAction action, 590 std::string* error_desc) override; 591 bool GetStats_w(VideoMediaInfo* stats); 592 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const; 593 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters); 594 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const; 595 bool SetRtpReceiveParameters_w(uint32_t ssrc, 596 webrtc::RtpParameters parameters); 597 598 void OnMessage(rtc::Message* pmsg) override; 599 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override; 600 void OnConnectionMonitorUpdate( 601 ConnectionMonitor* monitor, 602 const std::vector<ConnectionInfo>& infos) override; 603 void OnMediaMonitorUpdate(VideoMediaChannel* media_channel, 604 const VideoMediaInfo& info); 605 606 std::unique_ptr<VideoMediaMonitor> media_monitor_; 607 608 // Last VideoSendParameters sent down to the media_channel() via 609 // SetSendParameters. 610 VideoSendParameters last_send_params_; 611 // Last VideoRecvParameters sent down to the media_channel() via 612 // SetRecvParameters. 613 VideoRecvParameters last_recv_params_; 614 }; 615 616 // RtpDataChannel is a specialization for data. 617 class RtpDataChannel : public BaseChannel { 618 public: 619 RtpDataChannel(rtc::Thread* worker_thread, 620 rtc::Thread* network_thread, 621 rtc::Thread* signaling_thread, 622 DataMediaChannel* channel, 623 const std::string& content_name, 624 bool rtcp_mux_required, 625 bool srtp_required); 626 ~RtpDataChannel(); 627 bool Init_w(TransportChannel* rtp_transport, 628 TransportChannel* rtcp_transport); 629 630 virtual bool SendData(const SendDataParams& params, 631 const rtc::CopyOnWriteBuffer& payload, 632 SendDataResult* result); 633 634 void StartMediaMonitor(int cms); 635 void StopMediaMonitor(); 636 637 // Should be called on the signaling thread only. ready_to_send_data()638 bool ready_to_send_data() const { 639 return ready_to_send_data_; 640 } 641 642 sigslot::signal2<RtpDataChannel*, const DataMediaInfo&> SignalMediaMonitor; 643 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&> 644 SignalConnectionMonitor; 645 646 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&> 647 SignalDataReceived; 648 // Signal for notifying when the channel becomes ready to send data. 649 // That occurs when the channel is enabled, the transport is writable, 650 // both local and remote descriptions are set, and the channel is unblocked. 651 sigslot::signal1<bool> SignalReadyToSendData; media_type()652 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; } 653 654 protected: 655 // downcasts a MediaChannel. media_channel()656 DataMediaChannel* media_channel() const override { 657 return static_cast<DataMediaChannel*>(BaseChannel::media_channel()); 658 } 659 660 private: 661 struct SendDataMessageData : public rtc::MessageData { SendDataMessageDataSendDataMessageData662 SendDataMessageData(const SendDataParams& params, 663 const rtc::CopyOnWriteBuffer* payload, 664 SendDataResult* result) 665 : params(params), 666 payload(payload), 667 result(result), 668 succeeded(false) { 669 } 670 671 const SendDataParams& params; 672 const rtc::CopyOnWriteBuffer* payload; 673 SendDataResult* result; 674 bool succeeded; 675 }; 676 677 struct DataReceivedMessageData : public rtc::MessageData { 678 // We copy the data because the data will become invalid after we 679 // handle DataMediaChannel::SignalDataReceived but before we fire 680 // SignalDataReceived. DataReceivedMessageDataDataReceivedMessageData681 DataReceivedMessageData( 682 const ReceiveDataParams& params, const char* data, size_t len) 683 : params(params), 684 payload(data, len) { 685 } 686 const ReceiveDataParams params; 687 const rtc::CopyOnWriteBuffer payload; 688 }; 689 690 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData; 691 692 // overrides from BaseChannel 693 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override; 694 // Checks that data channel type is RTP. 695 bool CheckDataChannelTypeFromContent(const DataContentDescription* content, 696 std::string* error_desc); 697 bool SetLocalContent_w(const MediaContentDescription* content, 698 ContentAction action, 699 std::string* error_desc) override; 700 bool SetRemoteContent_w(const MediaContentDescription* content, 701 ContentAction action, 702 std::string* error_desc) override; 703 void UpdateMediaSendRecvState_w() override; 704 705 void OnMessage(rtc::Message* pmsg) override; 706 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override; 707 void OnConnectionMonitorUpdate( 708 ConnectionMonitor* monitor, 709 const std::vector<ConnectionInfo>& infos) override; 710 void OnMediaMonitorUpdate(DataMediaChannel* media_channel, 711 const DataMediaInfo& info); 712 void OnDataReceived( 713 const ReceiveDataParams& params, const char* data, size_t len); 714 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error); 715 void OnDataChannelReadyToSend(bool writable); 716 717 std::unique_ptr<DataMediaMonitor> media_monitor_; 718 bool ready_to_send_data_ = false; 719 720 // Last DataSendParameters sent down to the media_channel() via 721 // SetSendParameters. 722 DataSendParameters last_send_params_; 723 // Last DataRecvParameters sent down to the media_channel() via 724 // SetRecvParameters. 725 DataRecvParameters last_recv_params_; 726 }; 727 728 } // namespace cricket 729 730 #endif // WEBRTC_PC_CHANNEL_H_ 731