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 #ifndef TALK_MEDIA_BASE_MEDIACHANNEL_H_ 29 #define TALK_MEDIA_BASE_MEDIACHANNEL_H_ 30 31 #include <string> 32 #include <vector> 33 34 #include "talk/base/basictypes.h" 35 #include "talk/base/buffer.h" 36 #include "talk/base/dscp.h" 37 #include "talk/base/logging.h" 38 #include "talk/base/sigslot.h" 39 #include "talk/base/socket.h" 40 #include "talk/base/window.h" 41 #include "talk/media/base/codec.h" 42 #include "talk/media/base/constants.h" 43 #include "talk/media/base/streamparams.h" 44 // TODO(juberti): re-evaluate this include 45 #include "talk/session/media/audiomonitor.h" 46 47 namespace talk_base { 48 class Buffer; 49 class RateLimiter; 50 class Timing; 51 } 52 53 namespace webrtc { 54 struct DataChannelInit; 55 } 56 57 namespace cricket { 58 59 class AudioRenderer; 60 struct RtpHeader; 61 class ScreencastId; 62 struct VideoFormat; 63 class VideoCapturer; 64 class VideoRenderer; 65 66 const int kMinRtpHeaderExtensionId = 1; 67 const int kMaxRtpHeaderExtensionId = 255; 68 const int kScreencastDefaultFps = 5; 69 70 // Used in AudioOptions and VideoOptions to signify "unset" values. 71 template <class T> 72 class Settable { 73 public: 74 Settable() : set_(false), val_() {} 75 explicit Settable(T val) : set_(true), val_(val) {} 76 77 bool IsSet() const { 78 return set_; 79 } 80 81 bool Get(T* out) const { 82 *out = val_; 83 return set_; 84 } 85 86 T GetWithDefaultIfUnset(const T& default_value) const { 87 return set_ ? val_ : default_value; 88 } 89 90 virtual void Set(T val) { 91 set_ = true; 92 val_ = val; 93 } 94 95 void Clear() { 96 Set(T()); 97 set_ = false; 98 } 99 100 void SetFrom(const Settable<T>& o) { 101 // Set this value based on the value of o, iff o is set. If this value is 102 // set and o is unset, the current value will be unchanged. 103 T val; 104 if (o.Get(&val)) { 105 Set(val); 106 } 107 } 108 109 std::string ToString() const { 110 return set_ ? talk_base::ToString(val_) : ""; 111 } 112 113 bool operator==(const Settable<T>& o) const { 114 // Equal if both are unset with any value or both set with the same value. 115 return (set_ == o.set_) && (!set_ || (val_ == o.val_)); 116 } 117 118 bool operator!=(const Settable<T>& o) const { 119 return !operator==(o); 120 } 121 122 protected: 123 void InitializeValue(const T &val) { 124 val_ = val; 125 } 126 127 private: 128 bool set_; 129 T val_; 130 }; 131 132 class SettablePercent : public Settable<float> { 133 public: 134 virtual void Set(float val) { 135 if (val < 0) { 136 val = 0; 137 } 138 if (val > 1.0) { 139 val = 1.0; 140 } 141 Settable<float>::Set(val); 142 } 143 }; 144 145 template <class T> 146 static std::string ToStringIfSet(const char* key, const Settable<T>& val) { 147 std::string str; 148 if (val.IsSet()) { 149 str = key; 150 str += ": "; 151 str += val.ToString(); 152 str += ", "; 153 } 154 return str; 155 } 156 157 // Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine. 158 // Used to be flags, but that makes it hard to selectively apply options. 159 // We are moving all of the setting of options to structs like this, 160 // but some things currently still use flags. 161 struct AudioOptions { 162 void SetAll(const AudioOptions& change) { 163 echo_cancellation.SetFrom(change.echo_cancellation); 164 auto_gain_control.SetFrom(change.auto_gain_control); 165 rx_auto_gain_control.SetFrom(change.rx_auto_gain_control); 166 noise_suppression.SetFrom(change.noise_suppression); 167 highpass_filter.SetFrom(change.highpass_filter); 168 stereo_swapping.SetFrom(change.stereo_swapping); 169 typing_detection.SetFrom(change.typing_detection); 170 aecm_generate_comfort_noise.SetFrom(change.aecm_generate_comfort_noise); 171 conference_mode.SetFrom(change.conference_mode); 172 adjust_agc_delta.SetFrom(change.adjust_agc_delta); 173 experimental_agc.SetFrom(change.experimental_agc); 174 experimental_aec.SetFrom(change.experimental_aec); 175 aec_dump.SetFrom(change.aec_dump); 176 experimental_acm.SetFrom(change.experimental_acm); 177 tx_agc_target_dbov.SetFrom(change.tx_agc_target_dbov); 178 tx_agc_digital_compression_gain.SetFrom( 179 change.tx_agc_digital_compression_gain); 180 tx_agc_limiter.SetFrom(change.tx_agc_limiter); 181 rx_agc_target_dbov.SetFrom(change.rx_agc_target_dbov); 182 rx_agc_digital_compression_gain.SetFrom( 183 change.rx_agc_digital_compression_gain); 184 rx_agc_limiter.SetFrom(change.rx_agc_limiter); 185 recording_sample_rate.SetFrom(change.recording_sample_rate); 186 playout_sample_rate.SetFrom(change.playout_sample_rate); 187 dscp.SetFrom(change.dscp); 188 } 189 190 bool operator==(const AudioOptions& o) const { 191 return echo_cancellation == o.echo_cancellation && 192 auto_gain_control == o.auto_gain_control && 193 rx_auto_gain_control == o.rx_auto_gain_control && 194 noise_suppression == o.noise_suppression && 195 highpass_filter == o.highpass_filter && 196 stereo_swapping == o.stereo_swapping && 197 typing_detection == o.typing_detection && 198 aecm_generate_comfort_noise == o.aecm_generate_comfort_noise && 199 conference_mode == o.conference_mode && 200 experimental_agc == o.experimental_agc && 201 experimental_aec == o.experimental_aec && 202 adjust_agc_delta == o.adjust_agc_delta && 203 aec_dump == o.aec_dump && 204 experimental_acm == o.experimental_acm && 205 tx_agc_target_dbov == o.tx_agc_target_dbov && 206 tx_agc_digital_compression_gain == o.tx_agc_digital_compression_gain && 207 tx_agc_limiter == o.tx_agc_limiter && 208 rx_agc_target_dbov == o.rx_agc_target_dbov && 209 rx_agc_digital_compression_gain == o.rx_agc_digital_compression_gain && 210 rx_agc_limiter == o.rx_agc_limiter && 211 recording_sample_rate == o.recording_sample_rate && 212 playout_sample_rate == o.playout_sample_rate && 213 dscp == o.dscp; 214 } 215 216 std::string ToString() const { 217 std::ostringstream ost; 218 ost << "AudioOptions {"; 219 ost << ToStringIfSet("aec", echo_cancellation); 220 ost << ToStringIfSet("agc", auto_gain_control); 221 ost << ToStringIfSet("rx_agc", rx_auto_gain_control); 222 ost << ToStringIfSet("ns", noise_suppression); 223 ost << ToStringIfSet("hf", highpass_filter); 224 ost << ToStringIfSet("swap", stereo_swapping); 225 ost << ToStringIfSet("typing", typing_detection); 226 ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise); 227 ost << ToStringIfSet("conference", conference_mode); 228 ost << ToStringIfSet("agc_delta", adjust_agc_delta); 229 ost << ToStringIfSet("experimental_agc", experimental_agc); 230 ost << ToStringIfSet("experimental_aec", experimental_aec); 231 ost << ToStringIfSet("aec_dump", aec_dump); 232 ost << ToStringIfSet("experimental_acm", experimental_acm); 233 ost << ToStringIfSet("tx_agc_target_dbov", tx_agc_target_dbov); 234 ost << ToStringIfSet("tx_agc_digital_compression_gain", 235 tx_agc_digital_compression_gain); 236 ost << ToStringIfSet("tx_agc_limiter", tx_agc_limiter); 237 ost << ToStringIfSet("rx_agc_target_dbov", rx_agc_target_dbov); 238 ost << ToStringIfSet("rx_agc_digital_compression_gain", 239 rx_agc_digital_compression_gain); 240 ost << ToStringIfSet("rx_agc_limiter", rx_agc_limiter); 241 ost << ToStringIfSet("recording_sample_rate", recording_sample_rate); 242 ost << ToStringIfSet("playout_sample_rate", playout_sample_rate); 243 ost << ToStringIfSet("dscp", dscp); 244 ost << "}"; 245 return ost.str(); 246 } 247 248 // Audio processing that attempts to filter away the output signal from 249 // later inbound pickup. 250 Settable<bool> echo_cancellation; 251 // Audio processing to adjust the sensitivity of the local mic dynamically. 252 Settable<bool> auto_gain_control; 253 // Audio processing to apply gain to the remote audio. 254 Settable<bool> rx_auto_gain_control; 255 // Audio processing to filter out background noise. 256 Settable<bool> noise_suppression; 257 // Audio processing to remove background noise of lower frequencies. 258 Settable<bool> highpass_filter; 259 // Audio processing to swap the left and right channels. 260 Settable<bool> stereo_swapping; 261 // Audio processing to detect typing. 262 Settable<bool> typing_detection; 263 Settable<bool> aecm_generate_comfort_noise; 264 Settable<bool> conference_mode; 265 Settable<int> adjust_agc_delta; 266 Settable<bool> experimental_agc; 267 Settable<bool> experimental_aec; 268 Settable<bool> aec_dump; 269 Settable<bool> experimental_acm; 270 // Note that tx_agc_* only applies to non-experimental AGC. 271 Settable<uint16> tx_agc_target_dbov; 272 Settable<uint16> tx_agc_digital_compression_gain; 273 Settable<bool> tx_agc_limiter; 274 Settable<uint16> rx_agc_target_dbov; 275 Settable<uint16> rx_agc_digital_compression_gain; 276 Settable<bool> rx_agc_limiter; 277 Settable<uint32> recording_sample_rate; 278 Settable<uint32> playout_sample_rate; 279 // Set DSCP value for packet sent from audio channel. 280 Settable<bool> dscp; 281 }; 282 283 // Options that can be applied to a VideoMediaChannel or a VideoMediaEngine. 284 // Used to be flags, but that makes it hard to selectively apply options. 285 // We are moving all of the setting of options to structs like this, 286 // but some things currently still use flags. 287 struct VideoOptions { 288 VideoOptions() { 289 process_adaptation_threshhold.Set(kProcessCpuThreshold); 290 system_low_adaptation_threshhold.Set(kLowSystemCpuThreshold); 291 system_high_adaptation_threshhold.Set(kHighSystemCpuThreshold); 292 } 293 294 void SetAll(const VideoOptions& change) { 295 adapt_input_to_encoder.SetFrom(change.adapt_input_to_encoder); 296 adapt_input_to_cpu_usage.SetFrom(change.adapt_input_to_cpu_usage); 297 adapt_cpu_with_smoothing.SetFrom(change.adapt_cpu_with_smoothing); 298 adapt_view_switch.SetFrom(change.adapt_view_switch); 299 video_adapt_third.SetFrom(change.video_adapt_third); 300 video_noise_reduction.SetFrom(change.video_noise_reduction); 301 video_one_layer_screencast.SetFrom(change.video_one_layer_screencast); 302 video_high_bitrate.SetFrom(change.video_high_bitrate); 303 video_watermark.SetFrom(change.video_watermark); 304 video_temporal_layer_screencast.SetFrom( 305 change.video_temporal_layer_screencast); 306 video_temporal_layer_realtime.SetFrom( 307 change.video_temporal_layer_realtime); 308 video_leaky_bucket.SetFrom(change.video_leaky_bucket); 309 cpu_overuse_detection.SetFrom(change.cpu_overuse_detection); 310 conference_mode.SetFrom(change.conference_mode); 311 process_adaptation_threshhold.SetFrom(change.process_adaptation_threshhold); 312 system_low_adaptation_threshhold.SetFrom( 313 change.system_low_adaptation_threshhold); 314 system_high_adaptation_threshhold.SetFrom( 315 change.system_high_adaptation_threshhold); 316 buffered_mode_latency.SetFrom(change.buffered_mode_latency); 317 lower_min_bitrate.SetFrom(change.lower_min_bitrate); 318 dscp.SetFrom(change.dscp); 319 suspend_below_min_bitrate.SetFrom(change.suspend_below_min_bitrate); 320 } 321 322 bool operator==(const VideoOptions& o) const { 323 return adapt_input_to_encoder == o.adapt_input_to_encoder && 324 adapt_input_to_cpu_usage == o.adapt_input_to_cpu_usage && 325 adapt_cpu_with_smoothing == o.adapt_cpu_with_smoothing && 326 adapt_view_switch == o.adapt_view_switch && 327 video_adapt_third == o.video_adapt_third && 328 video_noise_reduction == o.video_noise_reduction && 329 video_one_layer_screencast == o.video_one_layer_screencast && 330 video_high_bitrate == o.video_high_bitrate && 331 video_watermark == o.video_watermark && 332 video_temporal_layer_screencast == o.video_temporal_layer_screencast && 333 video_temporal_layer_realtime == o.video_temporal_layer_realtime && 334 video_leaky_bucket == o.video_leaky_bucket && 335 cpu_overuse_detection == o.cpu_overuse_detection && 336 conference_mode == o.conference_mode && 337 process_adaptation_threshhold == o.process_adaptation_threshhold && 338 system_low_adaptation_threshhold == 339 o.system_low_adaptation_threshhold && 340 system_high_adaptation_threshhold == 341 o.system_high_adaptation_threshhold && 342 buffered_mode_latency == o.buffered_mode_latency && 343 lower_min_bitrate == o.lower_min_bitrate && 344 dscp == o.dscp && 345 suspend_below_min_bitrate == o.suspend_below_min_bitrate; 346 } 347 348 std::string ToString() const { 349 std::ostringstream ost; 350 ost << "VideoOptions {"; 351 ost << ToStringIfSet("encoder adaption", adapt_input_to_encoder); 352 ost << ToStringIfSet("cpu adaption", adapt_input_to_cpu_usage); 353 ost << ToStringIfSet("cpu adaptation smoothing", adapt_cpu_with_smoothing); 354 ost << ToStringIfSet("adapt view switch", adapt_view_switch); 355 ost << ToStringIfSet("video adapt third", video_adapt_third); 356 ost << ToStringIfSet("noise reduction", video_noise_reduction); 357 ost << ToStringIfSet("1 layer screencast", video_one_layer_screencast); 358 ost << ToStringIfSet("high bitrate", video_high_bitrate); 359 ost << ToStringIfSet("watermark", video_watermark); 360 ost << ToStringIfSet("video temporal layer screencast", 361 video_temporal_layer_screencast); 362 ost << ToStringIfSet("video temporal layer realtime", 363 video_temporal_layer_realtime); 364 ost << ToStringIfSet("leaky bucket", video_leaky_bucket); 365 ost << ToStringIfSet("cpu overuse detection", cpu_overuse_detection); 366 ost << ToStringIfSet("conference mode", conference_mode); 367 ost << ToStringIfSet("process", process_adaptation_threshhold); 368 ost << ToStringIfSet("low", system_low_adaptation_threshhold); 369 ost << ToStringIfSet("high", system_high_adaptation_threshhold); 370 ost << ToStringIfSet("buffered mode latency", buffered_mode_latency); 371 ost << ToStringIfSet("lower min bitrate", lower_min_bitrate); 372 ost << ToStringIfSet("dscp", dscp); 373 ost << ToStringIfSet("suspend below min bitrate", 374 suspend_below_min_bitrate); 375 ost << "}"; 376 return ost.str(); 377 } 378 379 // Encoder adaption, which is the gd callback in LMI, and TBA in WebRTC. 380 Settable<bool> adapt_input_to_encoder; 381 // Enable CPU adaptation? 382 Settable<bool> adapt_input_to_cpu_usage; 383 // Enable CPU adaptation smoothing? 384 Settable<bool> adapt_cpu_with_smoothing; 385 // Enable Adapt View Switch? 386 Settable<bool> adapt_view_switch; 387 // Enable video adapt third? 388 Settable<bool> video_adapt_third; 389 // Enable denoising? 390 Settable<bool> video_noise_reduction; 391 // Experimental: Enable one layer screencast? 392 Settable<bool> video_one_layer_screencast; 393 // Experimental: Enable WebRtc higher bitrate? 394 Settable<bool> video_high_bitrate; 395 // Experimental: Add watermark to the rendered video image. 396 Settable<bool> video_watermark; 397 // Experimental: Enable WebRTC layered screencast. 398 Settable<bool> video_temporal_layer_screencast; 399 // Experimental: Enable WebRTC temporal layer strategy for realtime video. 400 Settable<bool> video_temporal_layer_realtime; 401 // Enable WebRTC leaky bucket when sending media packets. 402 Settable<bool> video_leaky_bucket; 403 // Enable WebRTC Cpu Overuse Detection, which is a new version of the CPU 404 // adaptation algorithm. So this option will override the 405 // |adapt_input_to_cpu_usage|. 406 Settable<bool> cpu_overuse_detection; 407 // Use conference mode? 408 Settable<bool> conference_mode; 409 // Threshhold for process cpu adaptation. (Process limit) 410 SettablePercent process_adaptation_threshhold; 411 // Low threshhold for cpu adaptation. (Adapt up) 412 SettablePercent system_low_adaptation_threshhold; 413 // High threshhold for cpu adaptation. (Adapt down) 414 SettablePercent system_high_adaptation_threshhold; 415 // Specify buffered mode latency in milliseconds. 416 Settable<int> buffered_mode_latency; 417 // Make minimum configured send bitrate even lower than usual, at 30kbit. 418 Settable<bool> lower_min_bitrate; 419 // Set DSCP value for packet sent from video channel. 420 Settable<bool> dscp; 421 // Enable WebRTC suspension of video. No video frames will be sent when the 422 // bitrate is below the configured minimum bitrate. 423 Settable<bool> suspend_below_min_bitrate; 424 }; 425 426 // A class for playing out soundclips. 427 class SoundclipMedia { 428 public: 429 enum SoundclipFlags { 430 SF_LOOP = 1, 431 }; 432 433 virtual ~SoundclipMedia() {} 434 435 // Plays a sound out to the speakers with the given audio stream. The stream 436 // must be 16-bit little-endian 16 kHz PCM. If a stream is already playing 437 // on this SoundclipMedia, it is stopped. If clip is NULL, nothing is played. 438 // Returns whether it was successful. 439 virtual bool PlaySound(const char *clip, int len, int flags) = 0; 440 }; 441 442 struct RtpHeaderExtension { 443 RtpHeaderExtension() : id(0) {} 444 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {} 445 std::string uri; 446 int id; 447 // TODO(juberti): SendRecv direction; 448 449 bool operator==(const RtpHeaderExtension& ext) const { 450 // id is a reserved word in objective-c. Therefore the id attribute has to 451 // be a fully qualified name in order to compile on IOS. 452 return this->id == ext.id && 453 uri == ext.uri; 454 } 455 }; 456 457 // Returns the named header extension if found among all extensions, NULL 458 // otherwise. 459 inline const RtpHeaderExtension* FindHeaderExtension( 460 const std::vector<RtpHeaderExtension>& extensions, 461 const std::string& name) { 462 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin(); 463 it != extensions.end(); ++it) { 464 if (it->uri == name) 465 return &(*it); 466 } 467 return NULL; 468 } 469 470 enum MediaChannelOptions { 471 // Tune the stream for conference mode. 472 OPT_CONFERENCE = 0x0001 473 }; 474 475 enum VoiceMediaChannelOptions { 476 // Tune the audio stream for vcs with different target levels. 477 OPT_AGC_MINUS_10DB = 0x80000000 478 }; 479 480 // DTMF flags to control if a DTMF tone should be played and/or sent. 481 enum DtmfFlags { 482 DF_PLAY = 0x01, 483 DF_SEND = 0x02, 484 }; 485 486 class MediaChannel : public sigslot::has_slots<> { 487 public: 488 class NetworkInterface { 489 public: 490 enum SocketType { ST_RTP, ST_RTCP }; 491 virtual bool SendPacket( 492 talk_base::Buffer* packet, 493 talk_base::DiffServCodePoint dscp = talk_base::DSCP_NO_CHANGE) = 0; 494 virtual bool SendRtcp( 495 talk_base::Buffer* packet, 496 talk_base::DiffServCodePoint dscp = talk_base::DSCP_NO_CHANGE) = 0; 497 virtual int SetOption(SocketType type, talk_base::Socket::Option opt, 498 int option) = 0; 499 virtual ~NetworkInterface() {} 500 }; 501 502 MediaChannel() : network_interface_(NULL) {} 503 virtual ~MediaChannel() {} 504 505 // Sets the abstract interface class for sending RTP/RTCP data. 506 virtual void SetInterface(NetworkInterface *iface) { 507 talk_base::CritScope cs(&network_interface_crit_); 508 network_interface_ = iface; 509 } 510 511 // Called when a RTP packet is received. 512 virtual void OnPacketReceived(talk_base::Buffer* packet, 513 const talk_base::PacketTime& packet_time) = 0; 514 // Called when a RTCP packet is received. 515 virtual void OnRtcpReceived(talk_base::Buffer* packet, 516 const talk_base::PacketTime& packet_time) = 0; 517 // Called when the socket's ability to send has changed. 518 virtual void OnReadyToSend(bool ready) = 0; 519 // Creates a new outgoing media stream with SSRCs and CNAME as described 520 // by sp. 521 virtual bool AddSendStream(const StreamParams& sp) = 0; 522 // Removes an outgoing media stream. 523 // ssrc must be the first SSRC of the media stream if the stream uses 524 // multiple SSRCs. 525 virtual bool RemoveSendStream(uint32 ssrc) = 0; 526 // Creates a new incoming media stream with SSRCs and CNAME as described 527 // by sp. 528 virtual bool AddRecvStream(const StreamParams& sp) = 0; 529 // Removes an incoming media stream. 530 // ssrc must be the first SSRC of the media stream if the stream uses 531 // multiple SSRCs. 532 virtual bool RemoveRecvStream(uint32 ssrc) = 0; 533 534 // Mutes the channel. 535 virtual bool MuteStream(uint32 ssrc, bool on) = 0; 536 537 // Sets the RTP extension headers and IDs to use when sending RTP. 538 virtual bool SetRecvRtpHeaderExtensions( 539 const std::vector<RtpHeaderExtension>& extensions) = 0; 540 virtual bool SetSendRtpHeaderExtensions( 541 const std::vector<RtpHeaderExtension>& extensions) = 0; 542 // Sets the rate control to use when sending data. 543 virtual bool SetSendBandwidth(bool autobw, int bps) = 0; 544 545 // Base method to send packet using NetworkInterface. 546 bool SendPacket(talk_base::Buffer* packet) { 547 return DoSendPacket(packet, false); 548 } 549 550 bool SendRtcp(talk_base::Buffer* packet) { 551 return DoSendPacket(packet, true); 552 } 553 554 int SetOption(NetworkInterface::SocketType type, 555 talk_base::Socket::Option opt, 556 int option) { 557 talk_base::CritScope cs(&network_interface_crit_); 558 if (!network_interface_) 559 return -1; 560 561 return network_interface_->SetOption(type, opt, option); 562 } 563 564 protected: 565 // This method sets DSCP |value| on both RTP and RTCP channels. 566 int SetDscp(talk_base::DiffServCodePoint value) { 567 int ret; 568 ret = SetOption(NetworkInterface::ST_RTP, 569 talk_base::Socket::OPT_DSCP, 570 value); 571 if (ret == 0) { 572 ret = SetOption(NetworkInterface::ST_RTCP, 573 talk_base::Socket::OPT_DSCP, 574 value); 575 } 576 return ret; 577 } 578 579 private: 580 bool DoSendPacket(talk_base::Buffer* packet, bool rtcp) { 581 talk_base::CritScope cs(&network_interface_crit_); 582 if (!network_interface_) 583 return false; 584 585 return (!rtcp) ? network_interface_->SendPacket(packet) : 586 network_interface_->SendRtcp(packet); 587 } 588 589 // |network_interface_| can be accessed from the worker_thread and 590 // from any MediaEngine threads. This critical section is to protect accessing 591 // of network_interface_ object. 592 talk_base::CriticalSection network_interface_crit_; 593 NetworkInterface* network_interface_; 594 }; 595 596 enum SendFlags { 597 SEND_NOTHING, 598 SEND_RINGBACKTONE, 599 SEND_MICROPHONE 600 }; 601 602 // The stats information is structured as follows: 603 // Media are represented by either MediaSenderInfo or MediaReceiverInfo. 604 // Media contains a vector of SSRC infos that are exclusively used by this 605 // media. (SSRCs shared between media streams can't be represented.) 606 607 // Information about an SSRC. 608 // This data may be locally recorded, or received in an RTCP SR or RR. 609 struct SsrcSenderInfo { 610 SsrcSenderInfo() 611 : ssrc(0), 612 timestamp(0) { 613 } 614 uint32 ssrc; 615 double timestamp; // NTP timestamp, represented as seconds since epoch. 616 }; 617 618 struct SsrcReceiverInfo { 619 SsrcReceiverInfo() 620 : ssrc(0), 621 timestamp(0) { 622 } 623 uint32 ssrc; 624 double timestamp; 625 }; 626 627 struct MediaSenderInfo { 628 MediaSenderInfo() 629 : bytes_sent(0), 630 packets_sent(0), 631 packets_lost(0), 632 fraction_lost(0.0), 633 rtt_ms(0) { 634 } 635 void add_ssrc(const SsrcSenderInfo& stat) { 636 local_stats.push_back(stat); 637 } 638 // Temporary utility function for call sites that only provide SSRC. 639 // As more info is added into SsrcSenderInfo, this function should go away. 640 void add_ssrc(uint32 ssrc) { 641 SsrcSenderInfo stat; 642 stat.ssrc = ssrc; 643 add_ssrc(stat); 644 } 645 // Utility accessor for clients that are only interested in ssrc numbers. 646 std::vector<uint32> ssrcs() const { 647 std::vector<uint32> retval; 648 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin(); 649 it != local_stats.end(); ++it) { 650 retval.push_back(it->ssrc); 651 } 652 return retval; 653 } 654 // Utility accessor for clients that make the assumption only one ssrc 655 // exists per media. 656 // This will eventually go away. 657 uint32 ssrc() const { 658 if (local_stats.size() > 0) { 659 return local_stats[0].ssrc; 660 } else { 661 return 0; 662 } 663 } 664 int64 bytes_sent; 665 int packets_sent; 666 int packets_lost; 667 float fraction_lost; 668 int rtt_ms; 669 std::string codec_name; 670 std::vector<SsrcSenderInfo> local_stats; 671 std::vector<SsrcReceiverInfo> remote_stats; 672 }; 673 674 struct MediaReceiverInfo { 675 MediaReceiverInfo() 676 : bytes_rcvd(0), 677 packets_rcvd(0), 678 packets_lost(0), 679 fraction_lost(0.0) { 680 } 681 void add_ssrc(const SsrcReceiverInfo& stat) { 682 local_stats.push_back(stat); 683 } 684 // Temporary utility function for call sites that only provide SSRC. 685 // As more info is added into SsrcSenderInfo, this function should go away. 686 void add_ssrc(uint32 ssrc) { 687 SsrcReceiverInfo stat; 688 stat.ssrc = ssrc; 689 add_ssrc(stat); 690 } 691 std::vector<uint32> ssrcs() const { 692 std::vector<uint32> retval; 693 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin(); 694 it != local_stats.end(); ++it) { 695 retval.push_back(it->ssrc); 696 } 697 return retval; 698 } 699 // Utility accessor for clients that make the assumption only one ssrc 700 // exists per media. 701 // This will eventually go away. 702 uint32 ssrc() const { 703 if (local_stats.size() > 0) { 704 return local_stats[0].ssrc; 705 } else { 706 return 0; 707 } 708 } 709 710 int64 bytes_rcvd; 711 int packets_rcvd; 712 int packets_lost; 713 float fraction_lost; 714 std::vector<SsrcReceiverInfo> local_stats; 715 std::vector<SsrcSenderInfo> remote_stats; 716 }; 717 718 struct VoiceSenderInfo : public MediaSenderInfo { 719 VoiceSenderInfo() 720 : ext_seqnum(0), 721 jitter_ms(0), 722 audio_level(0), 723 aec_quality_min(0.0), 724 echo_delay_median_ms(0), 725 echo_delay_std_ms(0), 726 echo_return_loss(0), 727 echo_return_loss_enhancement(0), 728 typing_noise_detected(false) { 729 } 730 731 int ext_seqnum; 732 int jitter_ms; 733 int audio_level; 734 float aec_quality_min; 735 int echo_delay_median_ms; 736 int echo_delay_std_ms; 737 int echo_return_loss; 738 int echo_return_loss_enhancement; 739 bool typing_noise_detected; 740 }; 741 742 struct VoiceReceiverInfo : public MediaReceiverInfo { 743 VoiceReceiverInfo() 744 : ext_seqnum(0), 745 jitter_ms(0), 746 jitter_buffer_ms(0), 747 jitter_buffer_preferred_ms(0), 748 delay_estimate_ms(0), 749 audio_level(0), 750 expand_rate(0) { 751 } 752 753 int ext_seqnum; 754 int jitter_ms; 755 int jitter_buffer_ms; 756 int jitter_buffer_preferred_ms; 757 int delay_estimate_ms; 758 int audio_level; 759 // fraction of synthesized speech inserted through pre-emptive expansion 760 float expand_rate; 761 }; 762 763 struct VideoSenderInfo : public MediaSenderInfo { 764 VideoSenderInfo() 765 : packets_cached(0), 766 firs_rcvd(0), 767 nacks_rcvd(0), 768 frame_width(0), 769 frame_height(0), 770 framerate_input(0), 771 framerate_sent(0), 772 nominal_bitrate(0), 773 preferred_bitrate(0), 774 adapt_reason(0), 775 capture_jitter_ms(0), 776 avg_encode_ms(0), 777 encode_usage_percent(0), 778 capture_queue_delay_ms_per_s(0) { 779 } 780 781 std::vector<SsrcGroup> ssrc_groups; 782 int packets_cached; 783 int firs_rcvd; 784 int nacks_rcvd; 785 int frame_width; 786 int frame_height; 787 int framerate_input; 788 int framerate_sent; 789 int nominal_bitrate; 790 int preferred_bitrate; 791 int adapt_reason; 792 int capture_jitter_ms; 793 int avg_encode_ms; 794 int encode_usage_percent; 795 int capture_queue_delay_ms_per_s; 796 }; 797 798 struct VideoReceiverInfo : public MediaReceiverInfo { 799 VideoReceiverInfo() 800 : packets_concealed(0), 801 firs_sent(0), 802 nacks_sent(0), 803 frame_width(0), 804 frame_height(0), 805 framerate_rcvd(0), 806 framerate_decoded(0), 807 framerate_output(0), 808 framerate_render_input(0), 809 framerate_render_output(0), 810 decode_ms(0), 811 max_decode_ms(0), 812 jitter_buffer_ms(0), 813 min_playout_delay_ms(0), 814 render_delay_ms(0), 815 target_delay_ms(0), 816 current_delay_ms(0) { 817 } 818 819 std::vector<SsrcGroup> ssrc_groups; 820 int packets_concealed; 821 int firs_sent; 822 int nacks_sent; 823 int frame_width; 824 int frame_height; 825 int framerate_rcvd; 826 int framerate_decoded; 827 int framerate_output; 828 // Framerate as sent to the renderer. 829 int framerate_render_input; 830 // Framerate that the renderer reports. 831 int framerate_render_output; 832 833 // All stats below are gathered per-VideoReceiver, but some will be correlated 834 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC 835 // structures, reflect this in the new layout. 836 837 // Current frame decode latency. 838 int decode_ms; 839 // Maximum observed frame decode latency. 840 int max_decode_ms; 841 // Jitter (network-related) latency. 842 int jitter_buffer_ms; 843 // Requested minimum playout latency. 844 int min_playout_delay_ms; 845 // Requested latency to account for rendering delay. 846 int render_delay_ms; 847 // Target overall delay: network+decode+render, accounting for 848 // min_playout_delay_ms. 849 int target_delay_ms; 850 // Current overall delay, possibly ramping towards target_delay_ms. 851 int current_delay_ms; 852 }; 853 854 struct DataSenderInfo : public MediaSenderInfo { 855 DataSenderInfo() 856 : ssrc(0) { 857 } 858 859 uint32 ssrc; 860 }; 861 862 struct DataReceiverInfo : public MediaReceiverInfo { 863 DataReceiverInfo() 864 : ssrc(0) { 865 } 866 867 uint32 ssrc; 868 }; 869 870 struct BandwidthEstimationInfo { 871 BandwidthEstimationInfo() 872 : available_send_bandwidth(0), 873 available_recv_bandwidth(0), 874 target_enc_bitrate(0), 875 actual_enc_bitrate(0), 876 retransmit_bitrate(0), 877 transmit_bitrate(0), 878 bucket_delay(0) { 879 } 880 881 int available_send_bandwidth; 882 int available_recv_bandwidth; 883 int target_enc_bitrate; 884 int actual_enc_bitrate; 885 int retransmit_bitrate; 886 int transmit_bitrate; 887 int bucket_delay; 888 }; 889 890 struct VoiceMediaInfo { 891 void Clear() { 892 senders.clear(); 893 receivers.clear(); 894 } 895 std::vector<VoiceSenderInfo> senders; 896 std::vector<VoiceReceiverInfo> receivers; 897 }; 898 899 struct VideoMediaInfo { 900 void Clear() { 901 senders.clear(); 902 receivers.clear(); 903 bw_estimations.clear(); 904 } 905 std::vector<VideoSenderInfo> senders; 906 std::vector<VideoReceiverInfo> receivers; 907 std::vector<BandwidthEstimationInfo> bw_estimations; 908 }; 909 910 struct DataMediaInfo { 911 void Clear() { 912 senders.clear(); 913 receivers.clear(); 914 } 915 std::vector<DataSenderInfo> senders; 916 std::vector<DataReceiverInfo> receivers; 917 }; 918 919 class VoiceMediaChannel : public MediaChannel { 920 public: 921 enum Error { 922 ERROR_NONE = 0, // No error. 923 ERROR_OTHER, // Other errors. 924 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic. 925 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS. 926 ERROR_REC_DEVICE_SILENT, // No background noise picked up. 927 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping. 928 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active. 929 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors. 930 ERROR_REC_SRTP_ERROR, // Generic SRTP failure. 931 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets. 932 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected. 933 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout. 934 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS. 935 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active. 936 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing. 937 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure. 938 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets. 939 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected. 940 }; 941 942 VoiceMediaChannel() {} 943 virtual ~VoiceMediaChannel() {} 944 // Sets the codecs/payload types to be used for incoming media. 945 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) = 0; 946 // Sets the codecs/payload types to be used for outgoing media. 947 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) = 0; 948 // Starts or stops playout of received audio. 949 virtual bool SetPlayout(bool playout) = 0; 950 // Starts or stops sending (and potentially capture) of local audio. 951 virtual bool SetSend(SendFlags flag) = 0; 952 // Sets the renderer object to be used for the specified remote audio stream. 953 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) = 0; 954 // Sets the renderer object to be used for the specified local audio stream. 955 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) = 0; 956 // Gets current energy levels for all incoming streams. 957 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0; 958 // Get the current energy level of the stream sent to the speaker. 959 virtual int GetOutputLevel() = 0; 960 // Get the time in milliseconds since last recorded keystroke, or negative. 961 virtual int GetTimeSinceLastTyping() = 0; 962 // Temporarily exposed field for tuning typing detect options. 963 virtual void SetTypingDetectionParameters(int time_window, 964 int cost_per_typing, int reporting_threshold, int penalty_decay, 965 int type_event_delay) = 0; 966 // Set left and right scale for speaker output volume of the specified ssrc. 967 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) = 0; 968 // Get left and right scale for speaker output volume of the specified ssrc. 969 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) = 0; 970 // Specifies a ringback tone to be played during call setup. 971 virtual bool SetRingbackTone(const char *buf, int len) = 0; 972 // Plays or stops the aforementioned ringback tone 973 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) = 0; 974 // Returns if the telephone-event has been negotiated. 975 virtual bool CanInsertDtmf() { return false; } 976 // Send and/or play a DTMF |event| according to the |flags|. 977 // The DTMF out-of-band signal will be used on sending. 978 // The |ssrc| should be either 0 or a valid send stream ssrc. 979 // The valid value for the |event| are 0 to 15 which corresponding to 980 // DTMF event 0-9, *, #, A-D. 981 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) = 0; 982 // Gets quality stats for the channel. 983 virtual bool GetStats(VoiceMediaInfo* info) = 0; 984 // Gets last reported error for this media channel. 985 virtual void GetLastMediaError(uint32* ssrc, 986 VoiceMediaChannel::Error* error) { 987 ASSERT(error != NULL); 988 *error = ERROR_NONE; 989 } 990 // Sets the media options to use. 991 virtual bool SetOptions(const AudioOptions& options) = 0; 992 virtual bool GetOptions(AudioOptions* options) const = 0; 993 994 // Signal errors from MediaChannel. Arguments are: 995 // ssrc(uint32), and error(VoiceMediaChannel::Error). 996 sigslot::signal2<uint32, VoiceMediaChannel::Error> SignalMediaError; 997 }; 998 999 class VideoMediaChannel : public MediaChannel { 1000 public: 1001 enum Error { 1002 ERROR_NONE = 0, // No error. 1003 ERROR_OTHER, // Other errors. 1004 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera. 1005 ERROR_REC_DEVICE_NO_DEVICE, // No camera. 1006 ERROR_REC_DEVICE_IN_USE, // Device is in already use. 1007 ERROR_REC_DEVICE_REMOVED, // Device is removed. 1008 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure. 1009 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets. 1010 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore. 1011 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure. 1012 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets. 1013 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected. 1014 }; 1015 1016 VideoMediaChannel() : renderer_(NULL) {} 1017 virtual ~VideoMediaChannel() {} 1018 // Sets the codecs/payload types to be used for incoming media. 1019 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) = 0; 1020 // Sets the codecs/payload types to be used for outgoing media. 1021 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) = 0; 1022 // Gets the currently set codecs/payload types to be used for outgoing media. 1023 virtual bool GetSendCodec(VideoCodec* send_codec) = 0; 1024 // Sets the format of a specified outgoing stream. 1025 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) = 0; 1026 // Starts or stops playout of received video. 1027 virtual bool SetRender(bool render) = 0; 1028 // Starts or stops transmission (and potentially capture) of local video. 1029 virtual bool SetSend(bool send) = 0; 1030 // Sets the renderer object to be used for the specified stream. 1031 // If SSRC is 0, the renderer is used for the 'default' stream. 1032 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) = 0; 1033 // If |ssrc| is 0, replace the default capturer (engine capturer) with 1034 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC. 1035 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) = 0; 1036 // Gets quality stats for the channel. 1037 virtual bool GetStats(VideoMediaInfo* info) = 0; 1038 1039 // Send an intra frame to the receivers. 1040 virtual bool SendIntraFrame() = 0; 1041 // Reuqest each of the remote senders to send an intra frame. 1042 virtual bool RequestIntraFrame() = 0; 1043 // Sets the media options to use. 1044 virtual bool SetOptions(const VideoOptions& options) = 0; 1045 virtual bool GetOptions(VideoOptions* options) const = 0; 1046 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) = 0; 1047 1048 // Signal errors from MediaChannel. Arguments are: 1049 // ssrc(uint32), and error(VideoMediaChannel::Error). 1050 sigslot::signal2<uint32, Error> SignalMediaError; 1051 1052 protected: 1053 VideoRenderer *renderer_; 1054 }; 1055 1056 enum DataMessageType { 1057 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID 1058 // values. 1059 DMT_NONE = 0, 1060 DMT_CONTROL = 1, 1061 DMT_BINARY = 2, 1062 DMT_TEXT = 3, 1063 }; 1064 1065 // Info about data received in DataMediaChannel. For use in 1066 // DataMediaChannel::SignalDataReceived and in all of the signals that 1067 // signal fires, on up the chain. 1068 struct ReceiveDataParams { 1069 // The in-packet stream indentifier. 1070 // For SCTP, this is really SID, not SSRC. 1071 uint32 ssrc; 1072 // The type of message (binary, text, or control). 1073 DataMessageType type; 1074 // A per-stream value incremented per packet in the stream. 1075 int seq_num; 1076 // A per-stream value monotonically increasing with time. 1077 int timestamp; 1078 1079 ReceiveDataParams() : 1080 ssrc(0), 1081 type(DMT_TEXT), 1082 seq_num(0), 1083 timestamp(0) { 1084 } 1085 }; 1086 1087 struct SendDataParams { 1088 // The in-packet stream indentifier. 1089 // For SCTP, this is really SID, not SSRC. 1090 uint32 ssrc; 1091 // The type of message (binary, text, or control). 1092 DataMessageType type; 1093 1094 // For SCTP, whether to send messages flagged as ordered or not. 1095 // If false, messages can be received out of order. 1096 bool ordered; 1097 // For SCTP, whether the messages are sent reliably or not. 1098 // If false, messages may be lost. 1099 bool reliable; 1100 // For SCTP, if reliable == false, provide partial reliability by 1101 // resending up to this many times. Either count or millis 1102 // is supported, not both at the same time. 1103 int max_rtx_count; 1104 // For SCTP, if reliable == false, provide partial reliability by 1105 // resending for up to this many milliseconds. Either count or millis 1106 // is supported, not both at the same time. 1107 int max_rtx_ms; 1108 1109 SendDataParams() : 1110 ssrc(0), 1111 type(DMT_TEXT), 1112 // TODO(pthatcher): Make these true by default? 1113 ordered(false), 1114 reliable(false), 1115 max_rtx_count(0), 1116 max_rtx_ms(0) { 1117 } 1118 }; 1119 1120 enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK }; 1121 1122 class DataMediaChannel : public MediaChannel { 1123 public: 1124 enum Error { 1125 ERROR_NONE = 0, // No error. 1126 ERROR_OTHER, // Other errors. 1127 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure. 1128 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets. 1129 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure. 1130 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets. 1131 ERROR_RECV_SRTP_REPLAY, // Packet replay detected. 1132 }; 1133 1134 virtual ~DataMediaChannel() {} 1135 1136 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) = 0; 1137 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) = 0; 1138 1139 virtual bool MuteStream(uint32 ssrc, bool on) { return false; } 1140 // TODO(pthatcher): Implement this. 1141 virtual bool GetStats(DataMediaInfo* info) { return true; } 1142 1143 virtual bool SetSend(bool send) = 0; 1144 virtual bool SetReceive(bool receive) = 0; 1145 1146 virtual bool SendData( 1147 const SendDataParams& params, 1148 const talk_base::Buffer& payload, 1149 SendDataResult* result = NULL) = 0; 1150 // Signals when data is received (params, data, len) 1151 sigslot::signal3<const ReceiveDataParams&, 1152 const char*, 1153 size_t> SignalDataReceived; 1154 // Signal errors from MediaChannel. Arguments are: 1155 // ssrc(uint32), and error(DataMediaChannel::Error). 1156 sigslot::signal2<uint32, DataMediaChannel::Error> SignalMediaError; 1157 // Signal when the media channel is ready to send the stream. Arguments are: 1158 // writable(bool) 1159 sigslot::signal1<bool> SignalReadyToSend; 1160 // Signal for notifying when a new stream is added from the remote side. Used 1161 // for the in-band negotioation through the OPEN message for SCTP data 1162 // channel. 1163 sigslot::signal2<const std::string&, const webrtc::DataChannelInit&> 1164 SignalNewStreamReceived; 1165 }; 1166 1167 } // namespace cricket 1168 1169 #endif // TALK_MEDIA_BASE_MEDIACHANNEL_H_ 1170