blob: 81cfc2be1670a8134d3d0e2f17ebe4a79e8f4598 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000034#include "talk/media/base/codec.h"
35#include "talk/media/base/constants.h"
36#include "talk/media/base/streamparams.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037#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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044// TODO(juberti): re-evaluate this include
45#include "talk/session/media/audiomonitor.h"
46
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048class Buffer;
49class RateLimiter;
50class Timing;
51}
52
53namespace cricket {
54
55class AudioRenderer;
56struct RtpHeader;
57class ScreencastId;
58struct VideoFormat;
59class VideoCapturer;
60class VideoRenderer;
61
62const int kMinRtpHeaderExtensionId = 1;
63const int kMaxRtpHeaderExtensionId = 255;
64const int kScreencastDefaultFps = 5;
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +000065const int kHighStartBitrate = 1500;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67// Used in AudioOptions and VideoOptions to signify "unset" values.
68template <class T>
69class 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 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 return set_ ? rtc::ToString(val_) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 }
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
129class 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
142template <class T>
143static 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.
158struct AudioOptions {
159 void SetAll(const AudioOptions& change) {
160 echo_cancellation.SetFrom(change.echo_cancellation);
161 auto_gain_control.SetFrom(change.auto_gain_control);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000162 rx_auto_gain_control.SetFrom(change.rx_auto_gain_control);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 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);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000167 aecm_generate_comfort_noise.SetFrom(change.aecm_generate_comfort_noise);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 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);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000172 experimental_ns.SetFrom(change.experimental_ns);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 aec_dump.SetFrom(change.aec_dump);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000174 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);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000184 dscp.SetFrom(change.dscp);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000185 combined_audio_video_bwe.SetFrom(change.combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 }
187
188 bool operator==(const AudioOptions& o) const {
189 return echo_cancellation == o.echo_cancellation &&
190 auto_gain_control == o.auto_gain_control &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000191 rx_auto_gain_control == o.rx_auto_gain_control &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 noise_suppression == o.noise_suppression &&
193 highpass_filter == o.highpass_filter &&
194 stereo_swapping == o.stereo_swapping &&
195 typing_detection == o.typing_detection &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000196 aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 conference_mode == o.conference_mode &&
198 experimental_agc == o.experimental_agc &&
199 experimental_aec == o.experimental_aec &&
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000200 experimental_ns == o.experimental_ns &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 adjust_agc_delta == o.adjust_agc_delta &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000202 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 &&
wu@webrtc.orgde305012013-10-31 15:40:38 +0000210 playout_sample_rate == o.playout_sample_rate &&
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000211 dscp == o.dscp &&
212 combined_audio_video_bwe == o.combined_audio_video_bwe;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 }
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);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000220 ost << ToStringIfSet("rx_agc", rx_auto_gain_control);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 ost << ToStringIfSet("ns", noise_suppression);
222 ost << ToStringIfSet("hf", highpass_filter);
223 ost << ToStringIfSet("swap", stereo_swapping);
224 ost << ToStringIfSet("typing", typing_detection);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000225 ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 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);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000230 ost << ToStringIfSet("experimental_ns", experimental_ns);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 ost << ToStringIfSet("aec_dump", aec_dump);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000232 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);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000242 ost << ToStringIfSet("dscp", dscp);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000243 ost << ToStringIfSet("combined_audio_video_bwe", combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 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;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000253 // Audio processing to apply gain to the remote audio.
254 Settable<bool> rx_auto_gain_control;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 // 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;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000263 Settable<bool> aecm_generate_comfort_noise;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 Settable<bool> conference_mode;
265 Settable<int> adjust_agc_delta;
266 Settable<bool> experimental_agc;
267 Settable<bool> experimental_aec;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000268 Settable<bool> experimental_ns;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 Settable<bool> aec_dump;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000270 // 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;
wu@webrtc.orgde305012013-10-31 15:40:38 +0000279 // Set DSCP value for packet sent from audio channel.
280 Settable<bool> dscp;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000281 // Enable combined audio+bandwidth BWE.
282 Settable<bool> combined_audio_video_bwe;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283};
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.
289struct VideoOptions {
henrike@webrtc.orgf45a5502014-03-13 18:51:34 +0000290 enum HighestBitrate {
291 NORMAL,
292 HIGH,
293 VERY_HIGH
294 };
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 VideoOptions() {
297 process_adaptation_threshhold.Set(kProcessCpuThreshold);
298 system_low_adaptation_threshhold.Set(kLowSystemCpuThreshold);
299 system_high_adaptation_threshhold.Set(kHighSystemCpuThreshold);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000300 unsignalled_recv_stream_limit.Set(kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 }
302
303 void SetAll(const VideoOptions& change) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 adapt_input_to_cpu_usage.SetFrom(change.adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000305 adapt_cpu_with_smoothing.SetFrom(change.adapt_cpu_with_smoothing);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000306 video_adapt_third.SetFrom(change.video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 video_noise_reduction.SetFrom(change.video_noise_reduction);
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +0000308 video_start_bitrate.SetFrom(change.video_start_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 video_temporal_layer_screencast.SetFrom(
310 change.video_temporal_layer_screencast);
311 video_leaky_bucket.SetFrom(change.video_leaky_bucket);
henrike@webrtc.orgf45a5502014-03-13 18:51:34 +0000312 video_highest_bitrate.SetFrom(change.video_highest_bitrate);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000313 cpu_overuse_detection.SetFrom(change.cpu_overuse_detection);
henrike@webrtc.orge9793ab2014-03-18 14:36:23 +0000314 cpu_underuse_threshold.SetFrom(change.cpu_underuse_threshold);
315 cpu_overuse_threshold.SetFrom(change.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000316 cpu_underuse_encode_rsd_threshold.SetFrom(
317 change.cpu_underuse_encode_rsd_threshold);
318 cpu_overuse_encode_rsd_threshold.SetFrom(
319 change.cpu_overuse_encode_rsd_threshold);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000320 cpu_overuse_encode_usage.SetFrom(change.cpu_overuse_encode_usage);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 conference_mode.SetFrom(change.conference_mode);
322 process_adaptation_threshhold.SetFrom(change.process_adaptation_threshhold);
323 system_low_adaptation_threshhold.SetFrom(
324 change.system_low_adaptation_threshhold);
325 system_high_adaptation_threshhold.SetFrom(
326 change.system_high_adaptation_threshhold);
327 buffered_mode_latency.SetFrom(change.buffered_mode_latency);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000328 dscp.SetFrom(change.dscp);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000329 suspend_below_min_bitrate.SetFrom(change.suspend_below_min_bitrate);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000330 unsignalled_recv_stream_limit.SetFrom(change.unsignalled_recv_stream_limit);
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +0000331 use_simulcast_adapter.SetFrom(change.use_simulcast_adapter);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000332 screencast_min_bitrate.SetFrom(change.screencast_min_bitrate);
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000333 use_payload_padding.SetFrom(change.use_payload_padding);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 }
335
336 bool operator==(const VideoOptions& o) const {
pbos@webrtc.org43336b62014-10-14 19:12:06 +0000337 return adapt_input_to_cpu_usage == o.adapt_input_to_cpu_usage &&
338 adapt_cpu_with_smoothing == o.adapt_cpu_with_smoothing &&
339 video_adapt_third == o.video_adapt_third &&
340 video_noise_reduction == o.video_noise_reduction &&
341 video_start_bitrate == o.video_start_bitrate &&
342 video_temporal_layer_screencast ==
343 o.video_temporal_layer_screencast &&
344 video_leaky_bucket == o.video_leaky_bucket &&
345 video_highest_bitrate == o.video_highest_bitrate &&
346 cpu_overuse_detection == o.cpu_overuse_detection &&
347 cpu_underuse_threshold == o.cpu_underuse_threshold &&
348 cpu_overuse_threshold == o.cpu_overuse_threshold &&
349 cpu_underuse_encode_rsd_threshold ==
350 o.cpu_underuse_encode_rsd_threshold &&
351 cpu_overuse_encode_rsd_threshold ==
352 o.cpu_overuse_encode_rsd_threshold &&
353 cpu_overuse_encode_usage == o.cpu_overuse_encode_usage &&
354 conference_mode == o.conference_mode &&
355 process_adaptation_threshhold == o.process_adaptation_threshhold &&
356 system_low_adaptation_threshhold ==
357 o.system_low_adaptation_threshhold &&
358 system_high_adaptation_threshhold ==
359 o.system_high_adaptation_threshhold &&
360 buffered_mode_latency == o.buffered_mode_latency && dscp == o.dscp &&
361 suspend_below_min_bitrate == o.suspend_below_min_bitrate &&
362 unsignalled_recv_stream_limit == o.unsignalled_recv_stream_limit &&
363 use_simulcast_adapter == o.use_simulcast_adapter &&
364 screencast_min_bitrate == o.screencast_min_bitrate &&
365 use_payload_padding == o.use_payload_padding;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 }
367
368 std::string ToString() const {
369 std::ostringstream ost;
370 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 ost << ToStringIfSet("cpu adaption", adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000372 ost << ToStringIfSet("cpu adaptation smoothing", adapt_cpu_with_smoothing);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000373 ost << ToStringIfSet("video adapt third", video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 ost << ToStringIfSet("noise reduction", video_noise_reduction);
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +0000375 ost << ToStringIfSet("start bitrate", video_start_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 ost << ToStringIfSet("video temporal layer screencast",
377 video_temporal_layer_screencast);
378 ost << ToStringIfSet("leaky bucket", video_leaky_bucket);
henrike@webrtc.orgf45a5502014-03-13 18:51:34 +0000379 ost << ToStringIfSet("highest video bitrate", video_highest_bitrate);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000380 ost << ToStringIfSet("cpu overuse detection", cpu_overuse_detection);
henrike@webrtc.orge9793ab2014-03-18 14:36:23 +0000381 ost << ToStringIfSet("cpu underuse threshold", cpu_underuse_threshold);
382 ost << ToStringIfSet("cpu overuse threshold", cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000383 ost << ToStringIfSet("cpu underuse encode rsd threshold",
384 cpu_underuse_encode_rsd_threshold);
385 ost << ToStringIfSet("cpu overuse encode rsd threshold",
386 cpu_overuse_encode_rsd_threshold);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000387 ost << ToStringIfSet("cpu overuse encode usage",
388 cpu_overuse_encode_usage);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 ost << ToStringIfSet("conference mode", conference_mode);
390 ost << ToStringIfSet("process", process_adaptation_threshhold);
391 ost << ToStringIfSet("low", system_low_adaptation_threshhold);
392 ost << ToStringIfSet("high", system_high_adaptation_threshhold);
393 ost << ToStringIfSet("buffered mode latency", buffered_mode_latency);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000394 ost << ToStringIfSet("dscp", dscp);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000395 ost << ToStringIfSet("suspend below min bitrate",
396 suspend_below_min_bitrate);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000397 ost << ToStringIfSet("num channels for early receive",
398 unsignalled_recv_stream_limit);
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +0000399 ost << ToStringIfSet("use simulcast adapter", use_simulcast_adapter);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000400 ost << ToStringIfSet("screencast min bitrate", screencast_min_bitrate);
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000401 ost << ToStringIfSet("payload padding", use_payload_padding);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 ost << "}";
403 return ost.str();
404 }
405
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 // Enable CPU adaptation?
407 Settable<bool> adapt_input_to_cpu_usage;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000408 // Enable CPU adaptation smoothing?
409 Settable<bool> adapt_cpu_with_smoothing;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000410 // Enable video adapt third?
411 Settable<bool> video_adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 // Enable denoising?
413 Settable<bool> video_noise_reduction;
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +0000414 // Experimental: Enable WebRtc higher start bitrate?
415 Settable<int> video_start_bitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 // Experimental: Enable WebRTC layered screencast.
417 Settable<bool> video_temporal_layer_screencast;
418 // Enable WebRTC leaky bucket when sending media packets.
419 Settable<bool> video_leaky_bucket;
henrike@webrtc.orgf45a5502014-03-13 18:51:34 +0000420 // Set highest bitrate mode for video.
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000421 Settable<HighestBitrate> video_highest_bitrate;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000422 // Enable WebRTC Cpu Overuse Detection, which is a new version of the CPU
423 // adaptation algorithm. So this option will override the
424 // |adapt_input_to_cpu_usage|.
425 Settable<bool> cpu_overuse_detection;
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000426 // Low threshold (t1) for cpu overuse adaptation. (Adapt up)
427 // Metric: encode usage (m1). m1 < t1 => underuse.
henrike@webrtc.orge9793ab2014-03-18 14:36:23 +0000428 Settable<int> cpu_underuse_threshold;
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000429 // High threshold (t1) for cpu overuse adaptation. (Adapt down)
430 // Metric: encode usage (m1). m1 > t1 => overuse.
henrike@webrtc.orge9793ab2014-03-18 14:36:23 +0000431 Settable<int> cpu_overuse_threshold;
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000432 // Low threshold (t2) for cpu overuse adaptation. (Adapt up)
433 // Metric: relative standard deviation of encode time (m2).
434 // Optional threshold. If set, (m1 < t1 && m2 < t2) => underuse.
435 // Note: t2 will have no effect if t1 is not set.
436 Settable<int> cpu_underuse_encode_rsd_threshold;
437 // High threshold (t2) for cpu overuse adaptation. (Adapt down)
438 // Metric: relative standard deviation of encode time (m2).
439 // Optional threshold. If set, (m1 > t1 || m2 > t2) => overuse.
440 // Note: t2 will have no effect if t1 is not set.
441 Settable<int> cpu_overuse_encode_rsd_threshold;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000442 // Use encode usage for cpu detection.
443 Settable<bool> cpu_overuse_encode_usage;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 // Use conference mode?
445 Settable<bool> conference_mode;
446 // Threshhold for process cpu adaptation. (Process limit)
447 SettablePercent process_adaptation_threshhold;
448 // Low threshhold for cpu adaptation. (Adapt up)
449 SettablePercent system_low_adaptation_threshhold;
450 // High threshhold for cpu adaptation. (Adapt down)
451 SettablePercent system_high_adaptation_threshhold;
452 // Specify buffered mode latency in milliseconds.
453 Settable<int> buffered_mode_latency;
wu@webrtc.orgde305012013-10-31 15:40:38 +0000454 // Set DSCP value for packet sent from video channel.
455 Settable<bool> dscp;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000456 // Enable WebRTC suspension of video. No video frames will be sent when the
457 // bitrate is below the configured minimum bitrate.
458 Settable<bool> suspend_below_min_bitrate;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000459 // Limit on the number of early receive channels that can be created.
460 Settable<int> unsignalled_recv_stream_limit;
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +0000461 // Enable use of simulcast adapter.
462 Settable<bool> use_simulcast_adapter;
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000463 // Force screencast to use a minimum bitrate
464 Settable<int> screencast_min_bitrate;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000465 // Enable payload padding.
466 Settable<bool> use_payload_padding;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467};
468
469// A class for playing out soundclips.
470class SoundclipMedia {
471 public:
472 enum SoundclipFlags {
473 SF_LOOP = 1,
474 };
475
476 virtual ~SoundclipMedia() {}
477
478 // Plays a sound out to the speakers with the given audio stream. The stream
479 // must be 16-bit little-endian 16 kHz PCM. If a stream is already playing
480 // on this SoundclipMedia, it is stopped. If clip is NULL, nothing is played.
481 // Returns whether it was successful.
482 virtual bool PlaySound(const char *clip, int len, int flags) = 0;
483};
484
485struct RtpHeaderExtension {
486 RtpHeaderExtension() : id(0) {}
487 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
488 std::string uri;
489 int id;
490 // TODO(juberti): SendRecv direction;
491
492 bool operator==(const RtpHeaderExtension& ext) const {
493 // id is a reserved word in objective-c. Therefore the id attribute has to
494 // be a fully qualified name in order to compile on IOS.
495 return this->id == ext.id &&
496 uri == ext.uri;
497 }
498};
499
500// Returns the named header extension if found among all extensions, NULL
501// otherwise.
502inline const RtpHeaderExtension* FindHeaderExtension(
503 const std::vector<RtpHeaderExtension>& extensions,
504 const std::string& name) {
505 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
506 it != extensions.end(); ++it) {
507 if (it->uri == name)
508 return &(*it);
509 }
510 return NULL;
511}
512
513enum MediaChannelOptions {
514 // Tune the stream for conference mode.
515 OPT_CONFERENCE = 0x0001
516};
517
518enum VoiceMediaChannelOptions {
519 // Tune the audio stream for vcs with different target levels.
520 OPT_AGC_MINUS_10DB = 0x80000000
521};
522
523// DTMF flags to control if a DTMF tone should be played and/or sent.
524enum DtmfFlags {
525 DF_PLAY = 0x01,
526 DF_SEND = 0x02,
527};
528
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529class MediaChannel : public sigslot::has_slots<> {
530 public:
531 class NetworkInterface {
532 public:
533 enum SocketType { ST_RTP, ST_RTCP };
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000534 virtual bool SendPacket(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535 rtc::Buffer* packet,
536 rtc::DiffServCodePoint dscp = rtc::DSCP_NO_CHANGE) = 0;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000537 virtual bool SendRtcp(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538 rtc::Buffer* packet,
539 rtc::DiffServCodePoint dscp = rtc::DSCP_NO_CHANGE) = 0;
540 virtual int SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 int option) = 0;
542 virtual ~NetworkInterface() {}
543 };
544
545 MediaChannel() : network_interface_(NULL) {}
546 virtual ~MediaChannel() {}
547
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000548 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 virtual void SetInterface(NetworkInterface *iface) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 network_interface_ = iface;
552 }
553
554 // Called when a RTP packet is received.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 virtual void OnPacketReceived(rtc::Buffer* packet,
556 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 // Called when a RTCP packet is received.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558 virtual void OnRtcpReceived(rtc::Buffer* packet,
559 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 // Called when the socket's ability to send has changed.
561 virtual void OnReadyToSend(bool ready) = 0;
562 // Creates a new outgoing media stream with SSRCs and CNAME as described
563 // by sp.
564 virtual bool AddSendStream(const StreamParams& sp) = 0;
565 // Removes an outgoing media stream.
566 // ssrc must be the first SSRC of the media stream if the stream uses
567 // multiple SSRCs.
568 virtual bool RemoveSendStream(uint32 ssrc) = 0;
569 // Creates a new incoming media stream with SSRCs and CNAME as described
570 // by sp.
571 virtual bool AddRecvStream(const StreamParams& sp) = 0;
572 // Removes an incoming media stream.
573 // ssrc must be the first SSRC of the media stream if the stream uses
574 // multiple SSRCs.
575 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
576
577 // Mutes the channel.
578 virtual bool MuteStream(uint32 ssrc, bool on) = 0;
579
580 // Sets the RTP extension headers and IDs to use when sending RTP.
581 virtual bool SetRecvRtpHeaderExtensions(
582 const std::vector<RtpHeaderExtension>& extensions) = 0;
583 virtual bool SetSendRtpHeaderExtensions(
584 const std::vector<RtpHeaderExtension>& extensions) = 0;
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000585 // Returns the absoulte sendtime extension id value from media channel.
586 virtual int GetRtpSendTimeExtnId() const {
587 return -1;
588 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000589 // Sets the maximum allowed bandwidth to use when sending data.
590 virtual bool SetMaxSendBandwidth(int bps) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000592 // Base method to send packet using NetworkInterface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000593 bool SendPacket(rtc::Buffer* packet) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000594 return DoSendPacket(packet, false);
595 }
596
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 bool SendRtcp(rtc::Buffer* packet) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000598 return DoSendPacket(packet, true);
599 }
600
601 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000603 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000604 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000605 if (!network_interface_)
606 return -1;
607
608 return network_interface_->SetOption(type, opt, option);
609 }
610
wu@webrtc.orgde305012013-10-31 15:40:38 +0000611 protected:
612 // This method sets DSCP |value| on both RTP and RTCP channels.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000613 int SetDscp(rtc::DiffServCodePoint value) {
wu@webrtc.orgde305012013-10-31 15:40:38 +0000614 int ret;
615 ret = SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000616 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000617 value);
618 if (ret == 0) {
619 ret = SetOption(NetworkInterface::ST_RTCP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000620 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000621 value);
622 }
623 return ret;
624 }
625
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000626 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000627 bool DoSendPacket(rtc::Buffer* packet, bool rtcp) {
628 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000629 if (!network_interface_)
630 return false;
631
632 return (!rtcp) ? network_interface_->SendPacket(packet) :
633 network_interface_->SendRtcp(packet);
634 }
635
636 // |network_interface_| can be accessed from the worker_thread and
637 // from any MediaEngine threads. This critical section is to protect accessing
638 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000639 rtc::CriticalSection network_interface_crit_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000640 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641};
642
643enum SendFlags {
644 SEND_NOTHING,
645 SEND_RINGBACKTONE,
646 SEND_MICROPHONE
647};
648
wu@webrtc.org97077a32013-10-25 21:18:33 +0000649// The stats information is structured as follows:
650// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
651// Media contains a vector of SSRC infos that are exclusively used by this
652// media. (SSRCs shared between media streams can't be represented.)
653
654// Information about an SSRC.
655// This data may be locally recorded, or received in an RTCP SR or RR.
656struct SsrcSenderInfo {
657 SsrcSenderInfo()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 : ssrc(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000659 timestamp(0) {
660 }
661 uint32 ssrc;
662 double timestamp; // NTP timestamp, represented as seconds since epoch.
663};
664
665struct SsrcReceiverInfo {
666 SsrcReceiverInfo()
667 : ssrc(0),
668 timestamp(0) {
669 }
670 uint32 ssrc;
671 double timestamp;
672};
673
674struct MediaSenderInfo {
675 MediaSenderInfo()
676 : bytes_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 packets_sent(0),
678 packets_lost(0),
679 fraction_lost(0.0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000680 rtt_ms(0) {
681 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000682 void add_ssrc(const SsrcSenderInfo& stat) {
683 local_stats.push_back(stat);
684 }
685 // Temporary utility function for call sites that only provide SSRC.
686 // As more info is added into SsrcSenderInfo, this function should go away.
687 void add_ssrc(uint32 ssrc) {
688 SsrcSenderInfo stat;
689 stat.ssrc = ssrc;
690 add_ssrc(stat);
691 }
692 // Utility accessor for clients that are only interested in ssrc numbers.
693 std::vector<uint32> ssrcs() const {
694 std::vector<uint32> retval;
695 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
696 it != local_stats.end(); ++it) {
697 retval.push_back(it->ssrc);
698 }
699 return retval;
700 }
701 // Utility accessor for clients that make the assumption only one ssrc
702 // exists per media.
703 // This will eventually go away.
704 uint32 ssrc() const {
705 if (local_stats.size() > 0) {
706 return local_stats[0].ssrc;
707 } else {
708 return 0;
709 }
710 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000711 int64 bytes_sent;
712 int packets_sent;
713 int packets_lost;
714 float fraction_lost;
715 int rtt_ms;
716 std::string codec_name;
717 std::vector<SsrcSenderInfo> local_stats;
718 std::vector<SsrcReceiverInfo> remote_stats;
719};
720
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000721template<class T>
722struct VariableInfo {
723 VariableInfo()
724 : min_val(),
725 mean(0.0),
726 max_val(),
727 variance(0.0) {
728 }
729 T min_val;
730 double mean;
731 T max_val;
732 double variance;
733};
734
wu@webrtc.org97077a32013-10-25 21:18:33 +0000735struct MediaReceiverInfo {
736 MediaReceiverInfo()
737 : bytes_rcvd(0),
738 packets_rcvd(0),
739 packets_lost(0),
740 fraction_lost(0.0) {
741 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000742 void add_ssrc(const SsrcReceiverInfo& stat) {
743 local_stats.push_back(stat);
744 }
745 // Temporary utility function for call sites that only provide SSRC.
746 // As more info is added into SsrcSenderInfo, this function should go away.
747 void add_ssrc(uint32 ssrc) {
748 SsrcReceiverInfo stat;
749 stat.ssrc = ssrc;
750 add_ssrc(stat);
751 }
752 std::vector<uint32> ssrcs() const {
753 std::vector<uint32> retval;
754 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
755 it != local_stats.end(); ++it) {
756 retval.push_back(it->ssrc);
757 }
758 return retval;
759 }
760 // Utility accessor for clients that make the assumption only one ssrc
761 // exists per media.
762 // This will eventually go away.
763 uint32 ssrc() const {
764 if (local_stats.size() > 0) {
765 return local_stats[0].ssrc;
766 } else {
767 return 0;
768 }
769 }
770
wu@webrtc.org97077a32013-10-25 21:18:33 +0000771 int64 bytes_rcvd;
772 int packets_rcvd;
773 int packets_lost;
774 float fraction_lost;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000775 std::string codec_name;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000776 std::vector<SsrcReceiverInfo> local_stats;
777 std::vector<SsrcSenderInfo> remote_stats;
778};
779
780struct VoiceSenderInfo : public MediaSenderInfo {
781 VoiceSenderInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000782 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 jitter_ms(0),
784 audio_level(0),
785 aec_quality_min(0.0),
786 echo_delay_median_ms(0),
787 echo_delay_std_ms(0),
788 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000789 echo_return_loss_enhancement(0),
790 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 }
792
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 int ext_seqnum;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 int jitter_ms;
795 int audio_level;
796 float aec_quality_min;
797 int echo_delay_median_ms;
798 int echo_delay_std_ms;
799 int echo_return_loss;
800 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000801 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802};
803
wu@webrtc.org97077a32013-10-25 21:18:33 +0000804struct VoiceReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 VoiceReceiverInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000806 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 jitter_ms(0),
808 jitter_buffer_ms(0),
809 jitter_buffer_preferred_ms(0),
810 delay_estimate_ms(0),
811 audio_level(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000812 expand_rate(0),
813 decoding_calls_to_silence_generator(0),
814 decoding_calls_to_neteq(0),
815 decoding_normal(0),
816 decoding_plc(0),
817 decoding_cng(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000818 decoding_plc_cng(0),
819 capture_start_ntp_time_ms(-1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
821
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 int ext_seqnum;
823 int jitter_ms;
824 int jitter_buffer_ms;
825 int jitter_buffer_preferred_ms;
826 int delay_estimate_ms;
827 int audio_level;
828 // fraction of synthesized speech inserted through pre-emptive expansion
829 float expand_rate;
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000830 int decoding_calls_to_silence_generator;
831 int decoding_calls_to_neteq;
832 int decoding_normal;
833 int decoding_plc;
834 int decoding_cng;
835 int decoding_plc_cng;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000836 // Estimated capture start time in NTP time in ms.
837 int64 capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838};
839
wu@webrtc.org97077a32013-10-25 21:18:33 +0000840struct VideoSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 VideoSenderInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000842 : packets_cached(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 firs_rcvd(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000844 plis_rcvd(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 nacks_rcvd(0),
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000846 input_frame_width(0),
847 input_frame_height(0),
848 send_frame_width(0),
849 send_frame_height(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 framerate_input(0),
851 framerate_sent(0),
852 nominal_bitrate(0),
853 preferred_bitrate(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000854 adapt_reason(0),
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000855 adapt_changes(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000856 capture_jitter_ms(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000857 avg_encode_ms(0),
858 encode_usage_percent(0),
buildbot@webrtc.orgc800c1c2014-06-13 07:56:17 +0000859 encode_rsd(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000860 capture_queue_delay_ms_per_s(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 }
862
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 std::vector<SsrcGroup> ssrc_groups;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 int packets_cached;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 int firs_rcvd;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000866 int plis_rcvd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 int nacks_rcvd;
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000868 int input_frame_width;
869 int input_frame_height;
870 int send_frame_width;
871 int send_frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 int framerate_input;
873 int framerate_sent;
874 int nominal_bitrate;
875 int preferred_bitrate;
876 int adapt_reason;
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000877 int adapt_changes;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000878 int capture_jitter_ms;
879 int avg_encode_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000880 int encode_usage_percent;
buildbot@webrtc.orgc800c1c2014-06-13 07:56:17 +0000881 int encode_rsd;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000882 int capture_queue_delay_ms_per_s;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000883 VariableInfo<int> adapt_frame_drops;
884 VariableInfo<int> effects_frame_drops;
885 VariableInfo<double> capturer_frame_time;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886};
887
wu@webrtc.org97077a32013-10-25 21:18:33 +0000888struct VideoReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 VideoReceiverInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000890 : packets_concealed(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 firs_sent(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000892 plis_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 nacks_sent(0),
894 frame_width(0),
895 frame_height(0),
896 framerate_rcvd(0),
897 framerate_decoded(0),
898 framerate_output(0),
899 framerate_render_input(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000900 framerate_render_output(0),
901 decode_ms(0),
902 max_decode_ms(0),
903 jitter_buffer_ms(0),
904 min_playout_delay_ms(0),
905 render_delay_ms(0),
906 target_delay_ms(0),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000907 current_delay_ms(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000908 capture_start_ntp_time_ms(-1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
910
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 std::vector<SsrcGroup> ssrc_groups;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 int packets_concealed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 int firs_sent;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000914 int plis_sent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 int nacks_sent;
916 int frame_width;
917 int frame_height;
918 int framerate_rcvd;
919 int framerate_decoded;
920 int framerate_output;
921 // Framerate as sent to the renderer.
922 int framerate_render_input;
923 // Framerate that the renderer reports.
924 int framerate_render_output;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000925
926 // All stats below are gathered per-VideoReceiver, but some will be correlated
927 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
928 // structures, reflect this in the new layout.
929
930 // Current frame decode latency.
931 int decode_ms;
932 // Maximum observed frame decode latency.
933 int max_decode_ms;
934 // Jitter (network-related) latency.
935 int jitter_buffer_ms;
936 // Requested minimum playout latency.
937 int min_playout_delay_ms;
938 // Requested latency to account for rendering delay.
939 int render_delay_ms;
940 // Target overall delay: network+decode+render, accounting for
941 // min_playout_delay_ms.
942 int target_delay_ms;
943 // Current overall delay, possibly ramping towards target_delay_ms.
944 int current_delay_ms;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000945
946 // Estimated capture start time in NTP time in ms.
947 int64 capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948};
949
wu@webrtc.org97077a32013-10-25 21:18:33 +0000950struct DataSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 DataSenderInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000952 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 }
954
955 uint32 ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956};
957
wu@webrtc.org97077a32013-10-25 21:18:33 +0000958struct DataReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 DataReceiverInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000960 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 }
962
963 uint32 ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964};
965
966struct BandwidthEstimationInfo {
967 BandwidthEstimationInfo()
968 : available_send_bandwidth(0),
969 available_recv_bandwidth(0),
970 target_enc_bitrate(0),
971 actual_enc_bitrate(0),
972 retransmit_bitrate(0),
973 transmit_bitrate(0),
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000974 bucket_delay(0),
975 total_received_propagation_delta_ms(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 }
977
978 int available_send_bandwidth;
979 int available_recv_bandwidth;
980 int target_enc_bitrate;
981 int actual_enc_bitrate;
982 int retransmit_bitrate;
983 int transmit_bitrate;
984 int bucket_delay;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000985 // The following stats are only valid when
986 // StatsOptions::include_received_propagation_stats is true.
987 int total_received_propagation_delta_ms;
988 std::vector<int> recent_received_propagation_delta_ms;
989 std::vector<int64> recent_received_packet_group_arrival_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990};
991
992struct VoiceMediaInfo {
993 void Clear() {
994 senders.clear();
995 receivers.clear();
996 }
997 std::vector<VoiceSenderInfo> senders;
998 std::vector<VoiceReceiverInfo> receivers;
999};
1000
1001struct VideoMediaInfo {
1002 void Clear() {
1003 senders.clear();
1004 receivers.clear();
1005 bw_estimations.clear();
1006 }
1007 std::vector<VideoSenderInfo> senders;
1008 std::vector<VideoReceiverInfo> receivers;
1009 std::vector<BandwidthEstimationInfo> bw_estimations;
1010};
1011
1012struct DataMediaInfo {
1013 void Clear() {
1014 senders.clear();
1015 receivers.clear();
1016 }
1017 std::vector<DataSenderInfo> senders;
1018 std::vector<DataReceiverInfo> receivers;
1019};
1020
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001021struct StatsOptions {
1022 StatsOptions() : include_received_propagation_stats(false) {}
1023
1024 bool include_received_propagation_stats;
1025};
1026
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027class VoiceMediaChannel : public MediaChannel {
1028 public:
1029 enum Error {
1030 ERROR_NONE = 0, // No error.
1031 ERROR_OTHER, // Other errors.
1032 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
1033 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
1034 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
1035 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
1036 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
1037 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
1038 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
1039 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1040 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
1041 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
1042 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
1043 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
1044 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
1045 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
1046 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1047 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
1048 };
1049
1050 VoiceMediaChannel() {}
1051 virtual ~VoiceMediaChannel() {}
1052 // Sets the codecs/payload types to be used for incoming media.
1053 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) = 0;
1054 // Sets the codecs/payload types to be used for outgoing media.
1055 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) = 0;
1056 // Starts or stops playout of received audio.
1057 virtual bool SetPlayout(bool playout) = 0;
1058 // Starts or stops sending (and potentially capture) of local audio.
1059 virtual bool SetSend(SendFlags flag) = 0;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001060 // Sets the renderer object to be used for the specified remote audio stream.
1061 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
1062 // Sets the renderer object to be used for the specified local audio stream.
1063 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 // Gets current energy levels for all incoming streams.
1065 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
1066 // Get the current energy level of the stream sent to the speaker.
1067 virtual int GetOutputLevel() = 0;
1068 // Get the time in milliseconds since last recorded keystroke, or negative.
1069 virtual int GetTimeSinceLastTyping() = 0;
1070 // Temporarily exposed field for tuning typing detect options.
1071 virtual void SetTypingDetectionParameters(int time_window,
1072 int cost_per_typing, int reporting_threshold, int penalty_decay,
1073 int type_event_delay) = 0;
1074 // Set left and right scale for speaker output volume of the specified ssrc.
1075 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) = 0;
1076 // Get left and right scale for speaker output volume of the specified ssrc.
1077 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) = 0;
1078 // Specifies a ringback tone to be played during call setup.
1079 virtual bool SetRingbackTone(const char *buf, int len) = 0;
1080 // Plays or stops the aforementioned ringback tone
1081 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) = 0;
1082 // Returns if the telephone-event has been negotiated.
1083 virtual bool CanInsertDtmf() { return false; }
1084 // Send and/or play a DTMF |event| according to the |flags|.
1085 // The DTMF out-of-band signal will be used on sending.
1086 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +00001087 // The valid value for the |event| are 0 to 15 which corresponding to
1088 // DTMF event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) = 0;
1090 // Gets quality stats for the channel.
1091 virtual bool GetStats(VoiceMediaInfo* info) = 0;
1092 // Gets last reported error for this media channel.
1093 virtual void GetLastMediaError(uint32* ssrc,
1094 VoiceMediaChannel::Error* error) {
1095 ASSERT(error != NULL);
1096 *error = ERROR_NONE;
1097 }
1098 // Sets the media options to use.
1099 virtual bool SetOptions(const AudioOptions& options) = 0;
1100 virtual bool GetOptions(AudioOptions* options) const = 0;
1101
1102 // Signal errors from MediaChannel. Arguments are:
1103 // ssrc(uint32), and error(VoiceMediaChannel::Error).
1104 sigslot::signal2<uint32, VoiceMediaChannel::Error> SignalMediaError;
1105};
1106
1107class VideoMediaChannel : public MediaChannel {
1108 public:
1109 enum Error {
1110 ERROR_NONE = 0, // No error.
1111 ERROR_OTHER, // Other errors.
1112 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
1113 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
1114 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
1115 ERROR_REC_DEVICE_REMOVED, // Device is removed.
1116 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
1117 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1118 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
1119 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
1120 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1121 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
1122 };
1123
1124 VideoMediaChannel() : renderer_(NULL) {}
1125 virtual ~VideoMediaChannel() {}
1126 // Sets the codecs/payload types to be used for incoming media.
1127 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) = 0;
1128 // Sets the codecs/payload types to be used for outgoing media.
1129 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) = 0;
1130 // Gets the currently set codecs/payload types to be used for outgoing media.
1131 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
1132 // Sets the format of a specified outgoing stream.
1133 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) = 0;
1134 // Starts or stops playout of received video.
1135 virtual bool SetRender(bool render) = 0;
1136 // Starts or stops transmission (and potentially capture) of local video.
1137 virtual bool SetSend(bool send) = 0;
1138 // Sets the renderer object to be used for the specified stream.
1139 // If SSRC is 0, the renderer is used for the 'default' stream.
1140 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) = 0;
1141 // If |ssrc| is 0, replace the default capturer (engine capturer) with
1142 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
1143 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) = 0;
1144 // Gets quality stats for the channel.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001145 virtual bool GetStats(const StatsOptions& options, VideoMediaInfo* info) = 0;
1146 // This is needed for MediaMonitor to use the same template for voice, video
1147 // and data MediaChannels.
1148 bool GetStats(VideoMediaInfo* info) {
1149 return GetStats(StatsOptions(), info);
1150 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151
1152 // Send an intra frame to the receivers.
1153 virtual bool SendIntraFrame() = 0;
1154 // Reuqest each of the remote senders to send an intra frame.
1155 virtual bool RequestIntraFrame() = 0;
1156 // Sets the media options to use.
1157 virtual bool SetOptions(const VideoOptions& options) = 0;
1158 virtual bool GetOptions(VideoOptions* options) const = 0;
1159 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) = 0;
1160
1161 // Signal errors from MediaChannel. Arguments are:
1162 // ssrc(uint32), and error(VideoMediaChannel::Error).
1163 sigslot::signal2<uint32, Error> SignalMediaError;
1164
1165 protected:
1166 VideoRenderer *renderer_;
1167};
1168
1169enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001170 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
1171 // values.
1172 DMT_NONE = 0,
1173 DMT_CONTROL = 1,
1174 DMT_BINARY = 2,
1175 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176};
1177
1178// Info about data received in DataMediaChannel. For use in
1179// DataMediaChannel::SignalDataReceived and in all of the signals that
1180// signal fires, on up the chain.
1181struct ReceiveDataParams {
1182 // The in-packet stream indentifier.
1183 // For SCTP, this is really SID, not SSRC.
1184 uint32 ssrc;
1185 // The type of message (binary, text, or control).
1186 DataMessageType type;
1187 // A per-stream value incremented per packet in the stream.
1188 int seq_num;
1189 // A per-stream value monotonically increasing with time.
1190 int timestamp;
1191
1192 ReceiveDataParams() :
1193 ssrc(0),
1194 type(DMT_TEXT),
1195 seq_num(0),
1196 timestamp(0) {
1197 }
1198};
1199
1200struct SendDataParams {
1201 // The in-packet stream indentifier.
1202 // For SCTP, this is really SID, not SSRC.
1203 uint32 ssrc;
1204 // The type of message (binary, text, or control).
1205 DataMessageType type;
1206
1207 // For SCTP, whether to send messages flagged as ordered or not.
1208 // If false, messages can be received out of order.
1209 bool ordered;
1210 // For SCTP, whether the messages are sent reliably or not.
1211 // If false, messages may be lost.
1212 bool reliable;
1213 // For SCTP, if reliable == false, provide partial reliability by
1214 // resending up to this many times. Either count or millis
1215 // is supported, not both at the same time.
1216 int max_rtx_count;
1217 // For SCTP, if reliable == false, provide partial reliability by
1218 // resending for up to this many milliseconds. Either count or millis
1219 // is supported, not both at the same time.
1220 int max_rtx_ms;
1221
1222 SendDataParams() :
1223 ssrc(0),
1224 type(DMT_TEXT),
1225 // TODO(pthatcher): Make these true by default?
1226 ordered(false),
1227 reliable(false),
1228 max_rtx_count(0),
1229 max_rtx_ms(0) {
1230 }
1231};
1232
1233enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
1234
1235class DataMediaChannel : public MediaChannel {
1236 public:
1237 enum Error {
1238 ERROR_NONE = 0, // No error.
1239 ERROR_OTHER, // Other errors.
1240 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
1241 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1242 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
1243 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1244 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
1245 };
1246
1247 virtual ~DataMediaChannel() {}
1248
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) = 0;
1250 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001251
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
1253 // TODO(pthatcher): Implement this.
1254 virtual bool GetStats(DataMediaInfo* info) { return true; }
1255
1256 virtual bool SetSend(bool send) = 0;
1257 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258
1259 virtual bool SendData(
1260 const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 SendDataResult* result = NULL) = 0;
1263 // Signals when data is received (params, data, len)
1264 sigslot::signal3<const ReceiveDataParams&,
1265 const char*,
1266 size_t> SignalDataReceived;
1267 // Signal errors from MediaChannel. Arguments are:
1268 // ssrc(uint32), and error(DataMediaChannel::Error).
1269 sigslot::signal2<uint32, DataMediaChannel::Error> SignalMediaError;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001270 // Signal when the media channel is ready to send the stream. Arguments are:
1271 // writable(bool)
1272 sigslot::signal1<bool> SignalReadyToSend;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001273 // Signal for notifying that the remote side has closed the DataChannel.
1274 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275};
1276
1277} // namespace cricket
1278
1279#endif // TALK_MEDIA_BASE_MEDIACHANNEL_H_