blob: 68dc8f494f647ea3f8253fa8562a46ed2068d3ee [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kjellandera96e2d72016-02-04 23:52:28 -080011#ifndef WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_
12#define WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
kwiberg686a8ef2016-02-26 03:00:35 -080014#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <string>
16#include <vector>
17
skvladdc1c62c2016-03-16 19:07:43 -070018#include "webrtc/api/rtpparameters.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/basictypes.h"
jbaucheec21bd2016-03-20 06:15:43 -070020#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000021#include "webrtc/base/dscp.h"
22#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070023#include "webrtc/base/networkroute.h"
Karl Wibergbe579832015-11-10 22:34:18 +010024#include "webrtc/base/optional.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025#include "webrtc/base/sigslot.h"
26#include "webrtc/base/socket.h"
27#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/codec.h"
kjellanderf4752772016-03-02 05:42:30 -080029#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080030#include "webrtc/media/base/streamparams.h"
nisse08582ff2016-02-04 01:24:52 -080031#include "webrtc/media/base/videosinkinterface.h"
nisse2ded9b12016-04-08 02:23:55 -070032#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033// TODO(juberti): re-evaluate this include
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010034#include "webrtc/pc/audiomonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037class Buffer;
38class RateLimiter;
39class Timing;
40}
41
Tommif888bb52015-12-12 01:37:01 +010042namespace webrtc {
43class AudioSinkInterface;
44}
45
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046namespace cricket {
47
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080048class AudioSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049class ScreencastId;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050class VideoCapturer;
nisse08582ff2016-02-04 01:24:52 -080051class VideoFrame;
tommi1d5c19d2015-12-13 22:54:29 -080052struct RtpHeader;
53struct VideoFormat;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55const int kMinRtpHeaderExtensionId = 1;
56const int kMaxRtpHeaderExtensionId = 255;
57const int kScreencastDefaultFps = 5;
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059template <class T>
Karl Wibergbe579832015-11-10 22:34:18 +010060static std::string ToStringIfSet(const char* key, const rtc::Optional<T>& val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 std::string str;
kwiberg102c6a62015-10-30 02:47:38 -070062 if (val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 str = key;
64 str += ": ";
kwiberg102c6a62015-10-30 02:47:38 -070065 str += val ? rtc::ToString(*val) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 str += ", ";
67 }
68 return str;
69}
70
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070071template <class T>
72static std::string VectorToString(const std::vector<T>& vals) {
73 std::ostringstream ost;
74 ost << "[";
75 for (size_t i = 0; i < vals.size(); ++i) {
76 if (i > 0) {
77 ost << ", ";
78 }
79 ost << vals[i].ToString();
80 }
81 ost << "]";
82 return ost.str();
83}
84
skvladdc1c62c2016-03-16 19:07:43 -070085template <typename T>
86static T MinPositive(T a, T b) {
87 if (a <= 0) {
88 return b;
89 }
90 if (b <= 0) {
91 return a;
92 }
93 return std::min(a, b);
94}
95
nisse51542be2016-02-12 02:27:06 -080096// Construction-time settings, passed to
97// MediaControllerInterface::Create, and passed on when creating
98// MediaChannels.
99struct MediaConfig {
100 // Set DSCP value on packets. This flag comes from the
101 // PeerConnection constraint 'googDscp'.
102 bool enable_dscp = false;
103
nisse0db023a2016-03-01 04:29:59 -0800104 // Video-specific config.
105 struct Video {
106 // Enable WebRTC CPU Overuse Detection. This flag comes from the
107 // PeerConnection constraint 'googCpuOveruseDetection' and is
108 // checked in WebRtcVideoChannel2::OnLoadUpdate, where it's passed
109 // to VideoCapturer::video_adapter()->OnCpuResolutionRequest.
110 bool enable_cpu_overuse_detection = true;
nisse51542be2016-02-12 02:27:06 -0800111
nisse0db023a2016-03-01 04:29:59 -0800112 // Enable WebRTC suspension of video. No video frames will be sent
113 // when the bitrate is below the configured minimum bitrate. This
114 // flag comes from the PeerConnection constraint
115 // 'googSuspendBelowMinBitrate', and WebRtcVideoChannel2 copies it
116 // to VideoSendStream::Config::suspend_below_min_bitrate.
117 bool suspend_below_min_bitrate = false;
nisse51542be2016-02-12 02:27:06 -0800118
nisse0db023a2016-03-01 04:29:59 -0800119 // Set to true if the renderer has an algorithm of frame selection.
120 // If the value is true, then WebRTC will hand over a frame as soon as
121 // possible without delay, and rendering smoothness is completely the duty
122 // of the renderer;
123 // If the value is false, then WebRTC is responsible to delay frame release
124 // in order to increase rendering smoothness.
125 //
126 // This flag comes from PeerConnection's RtcConfiguration, but is
127 // currently only set by the command line flag
128 // 'disable-rtc-smoothness-algorithm'.
129 // WebRtcVideoChannel2::AddRecvStream copies it to the created
130 // WebRtcVideoReceiveStream, where it is returned by the
131 // SmoothsRenderedFrames method. This method is used by the
132 // VideoReceiveStream, where the value is passed on to the
133 // IncomingVideoStream constructor.
134 bool disable_prerenderer_smoothing = false;
135 } video;
nisse51542be2016-02-12 02:27:06 -0800136};
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138// Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine.
139// Used to be flags, but that makes it hard to selectively apply options.
140// We are moving all of the setting of options to structs like this,
141// but some things currently still use flags.
142struct AudioOptions {
143 void SetAll(const AudioOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700144 SetFrom(&echo_cancellation, change.echo_cancellation);
145 SetFrom(&auto_gain_control, change.auto_gain_control);
146 SetFrom(&noise_suppression, change.noise_suppression);
147 SetFrom(&highpass_filter, change.highpass_filter);
148 SetFrom(&stereo_swapping, change.stereo_swapping);
149 SetFrom(&audio_jitter_buffer_max_packets,
150 change.audio_jitter_buffer_max_packets);
151 SetFrom(&audio_jitter_buffer_fast_accelerate,
152 change.audio_jitter_buffer_fast_accelerate);
153 SetFrom(&typing_detection, change.typing_detection);
154 SetFrom(&aecm_generate_comfort_noise, change.aecm_generate_comfort_noise);
kwiberg102c6a62015-10-30 02:47:38 -0700155 SetFrom(&adjust_agc_delta, change.adjust_agc_delta);
156 SetFrom(&experimental_agc, change.experimental_agc);
157 SetFrom(&extended_filter_aec, change.extended_filter_aec);
158 SetFrom(&delay_agnostic_aec, change.delay_agnostic_aec);
159 SetFrom(&experimental_ns, change.experimental_ns);
kwiberg102c6a62015-10-30 02:47:38 -0700160 SetFrom(&tx_agc_target_dbov, change.tx_agc_target_dbov);
161 SetFrom(&tx_agc_digital_compression_gain,
162 change.tx_agc_digital_compression_gain);
163 SetFrom(&tx_agc_limiter, change.tx_agc_limiter);
164 SetFrom(&recording_sample_rate, change.recording_sample_rate);
165 SetFrom(&playout_sample_rate, change.playout_sample_rate);
kwiberg102c6a62015-10-30 02:47:38 -0700166 SetFrom(&combined_audio_video_bwe, change.combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
168
169 bool operator==(const AudioOptions& o) const {
170 return echo_cancellation == o.echo_cancellation &&
171 auto_gain_control == o.auto_gain_control &&
172 noise_suppression == o.noise_suppression &&
173 highpass_filter == o.highpass_filter &&
174 stereo_swapping == o.stereo_swapping &&
Henrik Lundin64dad832015-05-11 12:44:23 +0200175 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200176 audio_jitter_buffer_fast_accelerate ==
177 o.audio_jitter_buffer_fast_accelerate &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 typing_detection == o.typing_detection &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000179 aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 experimental_agc == o.experimental_agc &&
Henrik Lundin441f6342015-06-09 16:03:13 +0200181 extended_filter_aec == o.extended_filter_aec &&
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100182 delay_agnostic_aec == o.delay_agnostic_aec &&
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000183 experimental_ns == o.experimental_ns &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 adjust_agc_delta == o.adjust_agc_delta &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000185 tx_agc_target_dbov == o.tx_agc_target_dbov &&
186 tx_agc_digital_compression_gain == o.tx_agc_digital_compression_gain &&
187 tx_agc_limiter == o.tx_agc_limiter &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000188 recording_sample_rate == o.recording_sample_rate &&
wu@webrtc.orgde305012013-10-31 15:40:38 +0000189 playout_sample_rate == o.playout_sample_rate &&
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000190 combined_audio_video_bwe == o.combined_audio_video_bwe;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 }
deadbeef119760a2016-04-04 11:43:27 -0700192 bool operator!=(const AudioOptions& o) const { return !(*this == o); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
194 std::string ToString() const {
195 std::ostringstream ost;
196 ost << "AudioOptions {";
197 ost << ToStringIfSet("aec", echo_cancellation);
198 ost << ToStringIfSet("agc", auto_gain_control);
199 ost << ToStringIfSet("ns", noise_suppression);
200 ost << ToStringIfSet("hf", highpass_filter);
201 ost << ToStringIfSet("swap", stereo_swapping);
Henrik Lundin64dad832015-05-11 12:44:23 +0200202 ost << ToStringIfSet("audio_jitter_buffer_max_packets",
203 audio_jitter_buffer_max_packets);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200204 ost << ToStringIfSet("audio_jitter_buffer_fast_accelerate",
205 audio_jitter_buffer_fast_accelerate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 ost << ToStringIfSet("typing", typing_detection);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000207 ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 ost << ToStringIfSet("agc_delta", adjust_agc_delta);
209 ost << ToStringIfSet("experimental_agc", experimental_agc);
Henrik Lundin441f6342015-06-09 16:03:13 +0200210 ost << ToStringIfSet("extended_filter_aec", extended_filter_aec);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100211 ost << ToStringIfSet("delay_agnostic_aec", delay_agnostic_aec);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000212 ost << ToStringIfSet("experimental_ns", experimental_ns);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000213 ost << ToStringIfSet("tx_agc_target_dbov", tx_agc_target_dbov);
214 ost << ToStringIfSet("tx_agc_digital_compression_gain",
215 tx_agc_digital_compression_gain);
216 ost << ToStringIfSet("tx_agc_limiter", tx_agc_limiter);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000217 ost << ToStringIfSet("recording_sample_rate", recording_sample_rate);
218 ost << ToStringIfSet("playout_sample_rate", playout_sample_rate);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000219 ost << ToStringIfSet("combined_audio_video_bwe", combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 ost << "}";
221 return ost.str();
222 }
223
224 // Audio processing that attempts to filter away the output signal from
225 // later inbound pickup.
Karl Wibergbe579832015-11-10 22:34:18 +0100226 rtc::Optional<bool> echo_cancellation;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 // Audio processing to adjust the sensitivity of the local mic dynamically.
Karl Wibergbe579832015-11-10 22:34:18 +0100228 rtc::Optional<bool> auto_gain_control;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 // Audio processing to filter out background noise.
Karl Wibergbe579832015-11-10 22:34:18 +0100230 rtc::Optional<bool> noise_suppression;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 // Audio processing to remove background noise of lower frequencies.
Karl Wibergbe579832015-11-10 22:34:18 +0100232 rtc::Optional<bool> highpass_filter;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 // Audio processing to swap the left and right channels.
Karl Wibergbe579832015-11-10 22:34:18 +0100234 rtc::Optional<bool> stereo_swapping;
Henrik Lundin64dad832015-05-11 12:44:23 +0200235 // Audio receiver jitter buffer (NetEq) max capacity in number of packets.
Karl Wibergbe579832015-11-10 22:34:18 +0100236 rtc::Optional<int> audio_jitter_buffer_max_packets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200237 // Audio receiver jitter buffer (NetEq) fast accelerate mode.
Karl Wibergbe579832015-11-10 22:34:18 +0100238 rtc::Optional<bool> audio_jitter_buffer_fast_accelerate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 // Audio processing to detect typing.
Karl Wibergbe579832015-11-10 22:34:18 +0100240 rtc::Optional<bool> typing_detection;
241 rtc::Optional<bool> aecm_generate_comfort_noise;
Karl Wibergbe579832015-11-10 22:34:18 +0100242 rtc::Optional<int> adjust_agc_delta;
243 rtc::Optional<bool> experimental_agc;
244 rtc::Optional<bool> extended_filter_aec;
245 rtc::Optional<bool> delay_agnostic_aec;
246 rtc::Optional<bool> experimental_ns;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000247 // Note that tx_agc_* only applies to non-experimental AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100248 rtc::Optional<uint16_t> tx_agc_target_dbov;
249 rtc::Optional<uint16_t> tx_agc_digital_compression_gain;
250 rtc::Optional<bool> tx_agc_limiter;
251 rtc::Optional<uint32_t> recording_sample_rate;
252 rtc::Optional<uint32_t> playout_sample_rate;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000253 // Enable combined audio+bandwidth BWE.
nisse51542be2016-02-12 02:27:06 -0800254 // TODO(pthatcher): This flag is set from the
255 // "googCombinedAudioVideoBwe", but not used anywhere. So delete it,
256 // and check if any other AudioOptions members are unused.
Karl Wibergbe579832015-11-10 22:34:18 +0100257 rtc::Optional<bool> combined_audio_video_bwe;
kwiberg102c6a62015-10-30 02:47:38 -0700258
259 private:
260 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100261 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700262 if (o) {
263 *s = o;
264 }
265 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266};
267
268// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
269// Used to be flags, but that makes it hard to selectively apply options.
270// We are moving all of the setting of options to structs like this,
271// but some things currently still use flags.
272struct VideoOptions {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 void SetAll(const VideoOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700274 SetFrom(&video_noise_reduction, change.video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800275 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100276 SetFrom(&is_screencast, change.is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 }
278
279 bool operator==(const VideoOptions& o) const {
nisseb163c3f2016-01-29 01:14:38 -0800280 return video_noise_reduction == o.video_noise_reduction &&
Niels Möller60653ba2016-03-02 11:41:36 +0100281 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps &&
282 is_screencast == o.is_screencast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 }
deadbeef119760a2016-04-04 11:43:27 -0700284 bool operator!=(const VideoOptions& o) const { return !(*this == o); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285
286 std::string ToString() const {
287 std::ostringstream ost;
288 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 ost << ToStringIfSet("noise reduction", video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800290 ost << ToStringIfSet("screencast min bitrate kbps",
291 screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100292 ost << ToStringIfSet("is_screencast ", is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 ost << "}";
294 return ost.str();
295 }
296
nisseb163c3f2016-01-29 01:14:38 -0800297 // Enable denoising? This flag comes from the getUserMedia
298 // constraint 'googNoiseReduction', and WebRtcVideoEngine2 passes it
299 // on to the codec options. Disabled by default.
Karl Wibergbe579832015-11-10 22:34:18 +0100300 rtc::Optional<bool> video_noise_reduction;
nisseb163c3f2016-01-29 01:14:38 -0800301 // Force screencast to use a minimum bitrate. This flag comes from
302 // the PeerConnection constraint 'googScreencastMinBitrate'. It is
303 // copied to the encoder config by WebRtcVideoChannel2.
304 rtc::Optional<int> screencast_min_bitrate_kbps;
Niels Möller60653ba2016-03-02 11:41:36 +0100305 // Set by screencast sources. Implies selection of encoding settings
306 // suitable for screencast. Most likely not the right way to do
307 // things, e.g., screencast of a text document and screencast of a
308 // youtube video have different needs.
309 rtc::Optional<bool> is_screencast;
kwiberg102c6a62015-10-30 02:47:38 -0700310
311 private:
312 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100313 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700314 if (o) {
315 *s = o;
316 }
317 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318};
319
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320struct RtpHeaderExtension {
321 RtpHeaderExtension() : id(0) {}
322 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323
324 bool operator==(const RtpHeaderExtension& ext) const {
325 // id is a reserved word in objective-c. Therefore the id attribute has to
326 // be a fully qualified name in order to compile on IOS.
327 return this->id == ext.id &&
328 uri == ext.uri;
329 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700330
331 std::string ToString() const {
332 std::ostringstream ost;
333 ost << "{";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700334 ost << "uri: " << uri;
solenberg7e4e01a2015-12-02 08:05:01 -0800335 ost << ", id: " << id;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700336 ost << "}";
337 return ost.str();
338 }
339
340 std::string uri;
341 int id;
342 // TODO(juberti): SendRecv direction;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343};
344
345// Returns the named header extension if found among all extensions, NULL
346// otherwise.
347inline const RtpHeaderExtension* FindHeaderExtension(
348 const std::vector<RtpHeaderExtension>& extensions,
349 const std::string& name) {
350 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
351 it != extensions.end(); ++it) {
352 if (it->uri == name)
353 return &(*it);
354 }
355 return NULL;
356}
357
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358class MediaChannel : public sigslot::has_slots<> {
359 public:
360 class NetworkInterface {
361 public:
362 enum SocketType { ST_RTP, ST_RTCP };
jbaucheec21bd2016-03-20 06:15:43 -0700363 virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700364 const rtc::PacketOptions& options) = 0;
jbaucheec21bd2016-03-20 06:15:43 -0700365 virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700366 const rtc::PacketOptions& options) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000367 virtual int SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 int option) = 0;
369 virtual ~NetworkInterface() {}
370 };
371
nisse51542be2016-02-12 02:27:06 -0800372 MediaChannel(const MediaConfig& config)
373 : enable_dscp_(config.enable_dscp), network_interface_(NULL) {}
374 MediaChannel() : enable_dscp_(false), network_interface_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375 virtual ~MediaChannel() {}
376
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000377 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 virtual void SetInterface(NetworkInterface *iface) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000379 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 network_interface_ = iface;
nisse51542be2016-02-12 02:27:06 -0800381 SetDscp(enable_dscp_ ? PreferredDscp() : rtc::DSCP_DEFAULT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 }
nisse51542be2016-02-12 02:27:06 -0800383 virtual rtc::DiffServCodePoint PreferredDscp() const {
384 return rtc::DSCP_DEFAULT;
385 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 // Called when a RTP packet is received.
jbaucheec21bd2016-03-20 06:15:43 -0700387 virtual void OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000388 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 // Called when a RTCP packet is received.
jbaucheec21bd2016-03-20 06:15:43 -0700390 virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000391 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 // Called when the socket's ability to send has changed.
393 virtual void OnReadyToSend(bool ready) = 0;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700394 // Called when the network route used for sending packets changed.
395 virtual void OnNetworkRouteChanged(const std::string& transport_name,
396 const NetworkRoute& network_route) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 // Creates a new outgoing media stream with SSRCs and CNAME as described
398 // by sp.
399 virtual bool AddSendStream(const StreamParams& sp) = 0;
400 // Removes an outgoing media stream.
401 // ssrc must be the first SSRC of the media stream if the stream uses
402 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 // Creates a new incoming media stream with SSRCs and CNAME as described
405 // by sp.
406 virtual bool AddRecvStream(const StreamParams& sp) = 0;
407 // Removes an incoming media stream.
408 // ssrc must be the first SSRC of the media stream if the stream uses
409 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200410 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000412 // Returns the absoulte sendtime extension id value from media channel.
413 virtual int GetRtpSendTimeExtnId() const {
414 return -1;
415 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000417 // Base method to send packet using NetworkInterface.
jbaucheec21bd2016-03-20 06:15:43 -0700418 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
419 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700420 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000421 }
422
jbaucheec21bd2016-03-20 06:15:43 -0700423 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
424 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700425 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000426 }
427
428 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000429 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000430 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000431 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000432 if (!network_interface_)
433 return -1;
434
435 return network_interface_->SetOption(type, opt, option);
436 }
437
nisse51542be2016-02-12 02:27:06 -0800438 private:
wu@webrtc.orgde305012013-10-31 15:40:38 +0000439 // This method sets DSCP |value| on both RTP and RTCP channels.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000440 int SetDscp(rtc::DiffServCodePoint value) {
wu@webrtc.orgde305012013-10-31 15:40:38 +0000441 int ret;
442 ret = SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000443 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000444 value);
445 if (ret == 0) {
446 ret = SetOption(NetworkInterface::ST_RTCP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000447 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000448 value);
449 }
450 return ret;
451 }
452
jbaucheec21bd2016-03-20 06:15:43 -0700453 bool DoSendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700454 bool rtcp,
455 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000457 if (!network_interface_)
458 return false;
459
stefanc1aeaf02015-10-15 07:26:07 -0700460 return (!rtcp) ? network_interface_->SendPacket(packet, options)
461 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000462 }
463
nisse51542be2016-02-12 02:27:06 -0800464 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000465 // |network_interface_| can be accessed from the worker_thread and
466 // from any MediaEngine threads. This critical section is to protect accessing
467 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000468 rtc::CriticalSection network_interface_crit_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000469 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470};
471
wu@webrtc.org97077a32013-10-25 21:18:33 +0000472// The stats information is structured as follows:
473// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
474// Media contains a vector of SSRC infos that are exclusively used by this
475// media. (SSRCs shared between media streams can't be represented.)
476
477// Information about an SSRC.
478// This data may be locally recorded, or received in an RTCP SR or RR.
479struct SsrcSenderInfo {
480 SsrcSenderInfo()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 : ssrc(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000482 timestamp(0) {
483 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200484 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000485 double timestamp; // NTP timestamp, represented as seconds since epoch.
486};
487
488struct SsrcReceiverInfo {
489 SsrcReceiverInfo()
490 : ssrc(0),
491 timestamp(0) {
492 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200493 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000494 double timestamp;
495};
496
497struct MediaSenderInfo {
498 MediaSenderInfo()
499 : bytes_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 packets_sent(0),
501 packets_lost(0),
502 fraction_lost(0.0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000503 rtt_ms(0) {
504 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000505 void add_ssrc(const SsrcSenderInfo& stat) {
506 local_stats.push_back(stat);
507 }
508 // Temporary utility function for call sites that only provide SSRC.
509 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200510 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000511 SsrcSenderInfo stat;
512 stat.ssrc = ssrc;
513 add_ssrc(stat);
514 }
515 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200516 std::vector<uint32_t> ssrcs() const {
517 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000518 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
519 it != local_stats.end(); ++it) {
520 retval.push_back(it->ssrc);
521 }
522 return retval;
523 }
524 // Utility accessor for clients that make the assumption only one ssrc
525 // exists per media.
526 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200527 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000528 if (local_stats.size() > 0) {
529 return local_stats[0].ssrc;
530 } else {
531 return 0;
532 }
533 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200534 int64_t bytes_sent;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000535 int packets_sent;
536 int packets_lost;
537 float fraction_lost;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000538 int64_t rtt_ms;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000539 std::string codec_name;
540 std::vector<SsrcSenderInfo> local_stats;
541 std::vector<SsrcReceiverInfo> remote_stats;
542};
543
544struct MediaReceiverInfo {
545 MediaReceiverInfo()
546 : bytes_rcvd(0),
547 packets_rcvd(0),
548 packets_lost(0),
549 fraction_lost(0.0) {
550 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000551 void add_ssrc(const SsrcReceiverInfo& stat) {
552 local_stats.push_back(stat);
553 }
554 // Temporary utility function for call sites that only provide SSRC.
555 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200556 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000557 SsrcReceiverInfo stat;
558 stat.ssrc = ssrc;
559 add_ssrc(stat);
560 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200561 std::vector<uint32_t> ssrcs() const {
562 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000563 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
564 it != local_stats.end(); ++it) {
565 retval.push_back(it->ssrc);
566 }
567 return retval;
568 }
569 // Utility accessor for clients that make the assumption only one ssrc
570 // exists per media.
571 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200572 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000573 if (local_stats.size() > 0) {
574 return local_stats[0].ssrc;
575 } else {
576 return 0;
577 }
578 }
579
Peter Boström0c4e06b2015-10-07 12:23:21 +0200580 int64_t bytes_rcvd;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000581 int packets_rcvd;
582 int packets_lost;
583 float fraction_lost;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000584 std::string codec_name;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000585 std::vector<SsrcReceiverInfo> local_stats;
586 std::vector<SsrcSenderInfo> remote_stats;
587};
588
589struct VoiceSenderInfo : public MediaSenderInfo {
590 VoiceSenderInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000591 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 jitter_ms(0),
593 audio_level(0),
594 aec_quality_min(0.0),
595 echo_delay_median_ms(0),
596 echo_delay_std_ms(0),
597 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000598 echo_return_loss_enhancement(0),
599 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 }
601
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 int ext_seqnum;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 int jitter_ms;
604 int audio_level;
605 float aec_quality_min;
606 int echo_delay_median_ms;
607 int echo_delay_std_ms;
608 int echo_return_loss;
609 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000610 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611};
612
wu@webrtc.org97077a32013-10-25 21:18:33 +0000613struct VoiceReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 VoiceReceiverInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000615 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 jitter_ms(0),
617 jitter_buffer_ms(0),
618 jitter_buffer_preferred_ms(0),
619 delay_estimate_ms(0),
620 audio_level(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000621 expand_rate(0),
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000622 speech_expand_rate(0),
623 secondary_decoded_rate(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200624 accelerate_rate(0),
625 preemptive_expand_rate(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000626 decoding_calls_to_silence_generator(0),
627 decoding_calls_to_neteq(0),
628 decoding_normal(0),
629 decoding_plc(0),
630 decoding_cng(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000631 decoding_plc_cng(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200632 capture_start_ntp_time_ms(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 int ext_seqnum;
635 int jitter_ms;
636 int jitter_buffer_ms;
637 int jitter_buffer_preferred_ms;
638 int delay_estimate_ms;
639 int audio_level;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000640 // fraction of synthesized audio inserted through expansion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 float expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000642 // fraction of synthesized speech inserted through expansion.
643 float speech_expand_rate;
644 // fraction of data out of secondary decoding, including FEC and RED.
645 float secondary_decoded_rate;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200646 // Fraction of data removed through time compression.
647 float accelerate_rate;
648 // Fraction of data inserted through time stretching.
649 float preemptive_expand_rate;
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000650 int decoding_calls_to_silence_generator;
651 int decoding_calls_to_neteq;
652 int decoding_normal;
653 int decoding_plc;
654 int decoding_cng;
655 int decoding_plc_cng;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000656 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200657 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658};
659
wu@webrtc.org97077a32013-10-25 21:18:33 +0000660struct VideoSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 VideoSenderInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000662 : packets_cached(0),
663 firs_rcvd(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000664 plis_rcvd(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 nacks_rcvd(0),
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000666 send_frame_width(0),
667 send_frame_height(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 framerate_input(0),
669 framerate_sent(0),
670 nominal_bitrate(0),
671 preferred_bitrate(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000672 adapt_reason(0),
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000673 adapt_changes(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000674 avg_encode_ms(0),
Peter Boström8ed6a4b2015-03-27 10:01:02 +0100675 encode_usage_percent(0) {
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000676 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000678 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100679 std::string encoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000680 int packets_cached;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 int firs_rcvd;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000682 int plis_rcvd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 int nacks_rcvd;
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000684 int send_frame_width;
685 int send_frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 int framerate_input;
687 int framerate_sent;
688 int nominal_bitrate;
689 int preferred_bitrate;
690 int adapt_reason;
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000691 int adapt_changes;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000692 int avg_encode_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000693 int encode_usage_percent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694};
695
wu@webrtc.org97077a32013-10-25 21:18:33 +0000696struct VideoReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 VideoReceiverInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000698 : packets_concealed(0),
699 firs_sent(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000700 plis_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 nacks_sent(0),
702 frame_width(0),
703 frame_height(0),
704 framerate_rcvd(0),
705 framerate_decoded(0),
706 framerate_output(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000707 framerate_render_input(0),
708 framerate_render_output(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000709 decode_ms(0),
710 max_decode_ms(0),
711 jitter_buffer_ms(0),
712 min_playout_delay_ms(0),
713 render_delay_ms(0),
714 target_delay_ms(0),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000715 current_delay_ms(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000716 capture_start_ntp_time_ms(-1) {
717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000719 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100720 std::string decoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000721 int packets_concealed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 int firs_sent;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000723 int plis_sent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 int nacks_sent;
725 int frame_width;
726 int frame_height;
727 int framerate_rcvd;
728 int framerate_decoded;
729 int framerate_output;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000730 // Framerate as sent to the renderer.
731 int framerate_render_input;
732 // Framerate that the renderer reports.
733 int framerate_render_output;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000734
735 // All stats below are gathered per-VideoReceiver, but some will be correlated
736 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
737 // structures, reflect this in the new layout.
738
739 // Current frame decode latency.
740 int decode_ms;
741 // Maximum observed frame decode latency.
742 int max_decode_ms;
743 // Jitter (network-related) latency.
744 int jitter_buffer_ms;
745 // Requested minimum playout latency.
746 int min_playout_delay_ms;
747 // Requested latency to account for rendering delay.
748 int render_delay_ms;
749 // Target overall delay: network+decode+render, accounting for
750 // min_playout_delay_ms.
751 int target_delay_ms;
752 // Current overall delay, possibly ramping towards target_delay_ms.
753 int current_delay_ms;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000754
755 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200756 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757};
758
wu@webrtc.org97077a32013-10-25 21:18:33 +0000759struct DataSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 DataSenderInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000761 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 }
763
Peter Boström0c4e06b2015-10-07 12:23:21 +0200764 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765};
766
wu@webrtc.org97077a32013-10-25 21:18:33 +0000767struct DataReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 DataReceiverInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000769 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 }
771
Peter Boström0c4e06b2015-10-07 12:23:21 +0200772 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773};
774
775struct BandwidthEstimationInfo {
776 BandwidthEstimationInfo()
777 : available_send_bandwidth(0),
778 available_recv_bandwidth(0),
779 target_enc_bitrate(0),
780 actual_enc_bitrate(0),
781 retransmit_bitrate(0),
782 transmit_bitrate(0),
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000783 bucket_delay(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 }
785
786 int available_send_bandwidth;
787 int available_recv_bandwidth;
788 int target_enc_bitrate;
789 int actual_enc_bitrate;
790 int retransmit_bitrate;
791 int transmit_bitrate;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000792 int64_t bucket_delay;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793};
794
795struct VoiceMediaInfo {
796 void Clear() {
797 senders.clear();
798 receivers.clear();
799 }
800 std::vector<VoiceSenderInfo> senders;
801 std::vector<VoiceReceiverInfo> receivers;
802};
803
804struct VideoMediaInfo {
805 void Clear() {
806 senders.clear();
807 receivers.clear();
808 bw_estimations.clear();
809 }
810 std::vector<VideoSenderInfo> senders;
811 std::vector<VideoReceiverInfo> receivers;
812 std::vector<BandwidthEstimationInfo> bw_estimations;
813};
814
815struct DataMediaInfo {
816 void Clear() {
817 senders.clear();
818 receivers.clear();
819 }
820 std::vector<DataSenderInfo> senders;
821 std::vector<DataReceiverInfo> receivers;
822};
823
deadbeef13871492015-12-09 12:37:51 -0800824struct RtcpParameters {
825 bool reduced_size = false;
826};
827
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700828template <class Codec>
829struct RtpParameters {
solenberg7e4e01a2015-12-02 08:05:01 -0800830 virtual std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700831 std::ostringstream ost;
832 ost << "{";
833 ost << "codecs: " << VectorToString(codecs) << ", ";
834 ost << "extensions: " << VectorToString(extensions);
835 ost << "}";
836 return ost.str();
837 }
838
839 std::vector<Codec> codecs;
840 std::vector<RtpHeaderExtension> extensions;
841 // TODO(pthatcher): Add streams.
deadbeef13871492015-12-09 12:37:51 -0800842 RtcpParameters rtcp;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700843};
844
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700845// TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
846// encapsulate all the parameters needed for an RtpSender.
nisse05103312016-03-16 02:22:50 -0700847template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700848struct RtpSendParameters : RtpParameters<Codec> {
solenberg7e4e01a2015-12-02 08:05:01 -0800849 std::string ToString() const override {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700850 std::ostringstream ost;
851 ost << "{";
852 ost << "codecs: " << VectorToString(this->codecs) << ", ";
853 ost << "extensions: " << VectorToString(this->extensions) << ", ";
pbos378dc772016-01-28 15:58:41 -0800854 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
nisse05103312016-03-16 02:22:50 -0700855 ost << "}";
856 return ost.str();
857 }
858
859 int max_bandwidth_bps = -1;
860};
861
862struct AudioSendParameters : RtpSendParameters<AudioCodec> {
863 std::string ToString() const override {
864 std::ostringstream ost;
865 ost << "{";
866 ost << "codecs: " << VectorToString(this->codecs) << ", ";
867 ost << "extensions: " << VectorToString(this->extensions) << ", ";
868 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700869 ost << "options: " << options.ToString();
870 ost << "}";
871 return ost.str();
872 }
873
nisse05103312016-03-16 02:22:50 -0700874 AudioOptions options;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700875};
876
877struct AudioRecvParameters : RtpParameters<AudioCodec> {
878};
879
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880class VoiceMediaChannel : public MediaChannel {
881 public:
882 enum Error {
883 ERROR_NONE = 0, // No error.
884 ERROR_OTHER, // Other errors.
885 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
886 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
887 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
888 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
889 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
890 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
891 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
892 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
893 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
894 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
895 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
896 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
897 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
898 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
899 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
900 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
901 };
902
903 VoiceMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800904 VoiceMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 virtual ~VoiceMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200906 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
907 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
skvlade0d46372016-04-07 22:59:22 -0700908 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
909 virtual bool SetRtpParameters(uint32_t ssrc,
910 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 // Starts or stops playout of received audio.
912 virtual bool SetPlayout(bool playout) = 0;
913 // Starts or stops sending (and potentially capture) of local audio.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800914 virtual void SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700915 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200916 virtual bool SetAudioSend(uint32_t ssrc,
917 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700918 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800919 AudioSource* source) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 // Gets current energy levels for all incoming streams.
921 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
922 // Get the current energy level of the stream sent to the speaker.
923 virtual int GetOutputLevel() = 0;
924 // Get the time in milliseconds since last recorded keystroke, or negative.
925 virtual int GetTimeSinceLastTyping() = 0;
926 // Temporarily exposed field for tuning typing detect options.
927 virtual void SetTypingDetectionParameters(int time_window,
928 int cost_per_typing, int reporting_threshold, int penalty_decay,
929 int type_event_delay) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700930 // Set speaker output volume of the specified ssrc.
931 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800933 virtual bool CanInsertDtmf() = 0;
934 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000936 // The valid value for the |event| are 0 to 15 which corresponding to
937 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800938 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 // Gets quality stats for the channel.
940 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100941
942 virtual void SetRawAudioSink(
943 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800944 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945};
946
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700947// TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
948// encapsulate all the parameters needed for a video RtpSender.
nisse05103312016-03-16 02:22:50 -0700949struct VideoSendParameters : RtpSendParameters<VideoCodec> {
nisse4b4dc862016-02-17 05:25:36 -0800950 // Use conference mode? This flag comes from the remote
951 // description's SDP line 'a=x-google-flag:conference', copied over
952 // by VideoChannel::SetRemoteContent_w, and ultimately used by
953 // conference mode screencast logic in
954 // WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig.
955 // The special screencast behaviour is disabled by default.
956 bool conference_mode = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700957};
958
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700959// TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
960// encapsulate all the parameters needed for a video RtpReceiver.
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700961struct VideoRecvParameters : RtpParameters<VideoCodec> {
962};
963
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964class VideoMediaChannel : public MediaChannel {
965 public:
966 enum Error {
967 ERROR_NONE = 0, // No error.
968 ERROR_OTHER, // Other errors.
969 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
970 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
971 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
972 ERROR_REC_DEVICE_REMOVED, // Device is removed.
973 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
974 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
975 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
976 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
977 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
978 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
979 };
980
nisse08582ff2016-02-04 01:24:52 -0800981 VideoMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800982 VideoMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 virtual ~VideoMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200984
985 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
986 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
skvladdc1c62c2016-03-16 19:07:43 -0700987 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
988 virtual bool SetRtpParameters(uint32_t ssrc,
989 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 // Gets the currently set codecs/payload types to be used for outgoing media.
991 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 // Starts or stops transmission (and potentially capture) of local video.
993 virtual bool SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700994 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200995 virtual bool SetVideoSend(uint32_t ssrc,
996 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -0700997 const VideoOptions* options) = 0;
nisse08582ff2016-02-04 01:24:52 -0800998 // Sets the sink object to be used for the specified stream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 // If SSRC is 0, the renderer is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -08001000 virtual bool SetSink(uint32_t ssrc,
1001 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) = 0;
nisse2ded9b12016-04-08 02:23:55 -07001002 // Register a source. The |ssrc| must correspond to a registered send stream.
1003 virtual void SetSource(
1004 uint32_t ssrc,
1005 rtc::VideoSourceInterface<cricket::VideoFrame>* source) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001007 virtual bool GetStats(VideoMediaInfo* info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008};
1009
1010enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001011 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
1012 // values.
1013 DMT_NONE = 0,
1014 DMT_CONTROL = 1,
1015 DMT_BINARY = 2,
1016 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017};
1018
1019// Info about data received in DataMediaChannel. For use in
1020// DataMediaChannel::SignalDataReceived and in all of the signals that
1021// signal fires, on up the chain.
1022struct ReceiveDataParams {
1023 // The in-packet stream indentifier.
1024 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001025 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 // The type of message (binary, text, or control).
1027 DataMessageType type;
1028 // A per-stream value incremented per packet in the stream.
1029 int seq_num;
1030 // A per-stream value monotonically increasing with time.
1031 int timestamp;
1032
1033 ReceiveDataParams() :
1034 ssrc(0),
1035 type(DMT_TEXT),
1036 seq_num(0),
1037 timestamp(0) {
1038 }
1039};
1040
1041struct SendDataParams {
1042 // The in-packet stream indentifier.
1043 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001044 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 // The type of message (binary, text, or control).
1046 DataMessageType type;
1047
1048 // For SCTP, whether to send messages flagged as ordered or not.
1049 // If false, messages can be received out of order.
1050 bool ordered;
1051 // For SCTP, whether the messages are sent reliably or not.
1052 // If false, messages may be lost.
1053 bool reliable;
1054 // For SCTP, if reliable == false, provide partial reliability by
1055 // resending up to this many times. Either count or millis
1056 // is supported, not both at the same time.
1057 int max_rtx_count;
1058 // For SCTP, if reliable == false, provide partial reliability by
1059 // resending for up to this many milliseconds. Either count or millis
1060 // is supported, not both at the same time.
1061 int max_rtx_ms;
1062
1063 SendDataParams() :
1064 ssrc(0),
1065 type(DMT_TEXT),
1066 // TODO(pthatcher): Make these true by default?
1067 ordered(false),
1068 reliable(false),
1069 max_rtx_count(0),
1070 max_rtx_ms(0) {
1071 }
1072};
1073
1074enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
1075
nisse05103312016-03-16 02:22:50 -07001076struct DataSendParameters : RtpSendParameters<DataCodec> {
solenberg7e4e01a2015-12-02 08:05:01 -08001077 std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001078 std::ostringstream ost;
1079 // Options and extensions aren't used.
1080 ost << "{";
1081 ost << "codecs: " << VectorToString(codecs) << ", ";
pbos378dc772016-01-28 15:58:41 -08001082 ost << "max_bandwidth_bps: " << max_bandwidth_bps;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001083 ost << "}";
1084 return ost.str();
1085 }
1086};
1087
1088struct DataRecvParameters : RtpParameters<DataCodec> {
1089};
1090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091class DataMediaChannel : public MediaChannel {
1092 public:
1093 enum Error {
1094 ERROR_NONE = 0, // No error.
1095 ERROR_OTHER, // Other errors.
1096 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
1097 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1098 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
1099 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1100 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
1101 };
1102
1103 virtual ~DataMediaChannel() {}
1104
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001105 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
1106 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001107
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 // TODO(pthatcher): Implement this.
1109 virtual bool GetStats(DataMediaInfo* info) { return true; }
1110
1111 virtual bool SetSend(bool send) = 0;
1112 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113
Honghai Zhangcc411c02016-03-29 17:27:21 -07001114 virtual void OnNetworkRouteChanged(const std::string& transport_name,
1115 const NetworkRoute& network_route) {}
1116
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 virtual bool SendData(
1118 const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001119 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 SendDataResult* result = NULL) = 0;
1121 // Signals when data is received (params, data, len)
1122 sigslot::signal3<const ReceiveDataParams&,
1123 const char*,
1124 size_t> SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001125 // Signal when the media channel is ready to send the stream. Arguments are:
1126 // writable(bool)
1127 sigslot::signal1<bool> SignalReadyToSend;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001128 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001129 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130};
1131
1132} // namespace cricket
1133
kjellandera96e2d72016-02-04 23:52:28 -08001134#endif // WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_