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