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