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