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