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