blob: 7c054b6f3a69b872f771f4301cb4628d376da1b1 [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"
kwiberga4ac4782016-04-29 08:00:22 -070020#include "webrtc/base/buffer.h"
jbaucheec21bd2016-03-20 06:15:43 -070021#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000022#include "webrtc/base/dscp.h"
23#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070024#include "webrtc/base/networkroute.h"
Karl Wibergbe579832015-11-10 22:34:18 +010025#include "webrtc/base/optional.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/sigslot.h"
27#include "webrtc/base/socket.h"
28#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080029#include "webrtc/media/base/codec.h"
kjellanderf4752772016-03-02 05:42:30 -080030#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080031#include "webrtc/media/base/streamparams.h"
nisse08582ff2016-02-04 01:24:52 -080032#include "webrtc/media/base/videosinkinterface.h"
nisse2ded9b12016-04-08 02:23:55 -070033#include "webrtc/media/base/videosourceinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034// TODO(juberti): re-evaluate this include
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010035#include "webrtc/pc/audiomonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038class 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.
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700395 virtual void OnNetworkRouteChanged(
396 const std::string& transport_name,
397 const rtc::NetworkRoute& network_route) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 // Creates a new outgoing media stream with SSRCs and CNAME as described
399 // by sp.
400 virtual bool AddSendStream(const StreamParams& sp) = 0;
401 // Removes an outgoing media stream.
402 // ssrc must be the first SSRC of the media stream if the stream uses
403 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200404 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 // Creates a new incoming media stream with SSRCs and CNAME as described
406 // by sp.
407 virtual bool AddRecvStream(const StreamParams& sp) = 0;
408 // Removes an incoming media stream.
409 // ssrc must be the first SSRC of the media stream if the stream uses
410 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200411 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000413 // Returns the absoulte sendtime extension id value from media channel.
414 virtual int GetRtpSendTimeExtnId() const {
415 return -1;
416 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000418 // Base method to send packet using NetworkInterface.
jbaucheec21bd2016-03-20 06:15:43 -0700419 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
420 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700421 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000422 }
423
jbaucheec21bd2016-03-20 06:15:43 -0700424 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
425 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700426 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000427 }
428
429 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000431 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000432 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000433 if (!network_interface_)
434 return -1;
435
436 return network_interface_->SetOption(type, opt, option);
437 }
438
nisse51542be2016-02-12 02:27:06 -0800439 private:
wu@webrtc.orgde305012013-10-31 15:40:38 +0000440 // This method sets DSCP |value| on both RTP and RTCP channels.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000441 int SetDscp(rtc::DiffServCodePoint value) {
wu@webrtc.orgde305012013-10-31 15:40:38 +0000442 int ret;
443 ret = SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000444 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000445 value);
446 if (ret == 0) {
447 ret = SetOption(NetworkInterface::ST_RTCP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000448 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000449 value);
450 }
451 return ret;
452 }
453
jbaucheec21bd2016-03-20 06:15:43 -0700454 bool DoSendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700455 bool rtcp,
456 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000457 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000458 if (!network_interface_)
459 return false;
460
stefanc1aeaf02015-10-15 07:26:07 -0700461 return (!rtcp) ? network_interface_->SendPacket(packet, options)
462 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000463 }
464
nisse51542be2016-02-12 02:27:06 -0800465 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000466 // |network_interface_| can be accessed from the worker_thread and
467 // from any MediaEngine threads. This critical section is to protect accessing
468 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000469 rtc::CriticalSection network_interface_crit_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000470 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471};
472
wu@webrtc.org97077a32013-10-25 21:18:33 +0000473// The stats information is structured as follows:
474// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
475// Media contains a vector of SSRC infos that are exclusively used by this
476// media. (SSRCs shared between media streams can't be represented.)
477
478// Information about an SSRC.
479// This data may be locally recorded, or received in an RTCP SR or RR.
480struct SsrcSenderInfo {
481 SsrcSenderInfo()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 : ssrc(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000483 timestamp(0) {
484 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200485 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000486 double timestamp; // NTP timestamp, represented as seconds since epoch.
487};
488
489struct SsrcReceiverInfo {
490 SsrcReceiverInfo()
491 : ssrc(0),
492 timestamp(0) {
493 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200494 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000495 double timestamp;
496};
497
498struct MediaSenderInfo {
499 MediaSenderInfo()
500 : bytes_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 packets_sent(0),
502 packets_lost(0),
503 fraction_lost(0.0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000504 rtt_ms(0) {
505 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000506 void add_ssrc(const SsrcSenderInfo& stat) {
507 local_stats.push_back(stat);
508 }
509 // Temporary utility function for call sites that only provide SSRC.
510 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200511 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000512 SsrcSenderInfo stat;
513 stat.ssrc = ssrc;
514 add_ssrc(stat);
515 }
516 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200517 std::vector<uint32_t> ssrcs() const {
518 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000519 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
520 it != local_stats.end(); ++it) {
521 retval.push_back(it->ssrc);
522 }
523 return retval;
524 }
525 // Utility accessor for clients that make the assumption only one ssrc
526 // exists per media.
527 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200528 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000529 if (local_stats.size() > 0) {
530 return local_stats[0].ssrc;
531 } else {
532 return 0;
533 }
534 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200535 int64_t bytes_sent;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536 int packets_sent;
537 int packets_lost;
538 float fraction_lost;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000539 int64_t rtt_ms;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000540 std::string codec_name;
541 std::vector<SsrcSenderInfo> local_stats;
542 std::vector<SsrcReceiverInfo> remote_stats;
543};
544
545struct MediaReceiverInfo {
546 MediaReceiverInfo()
547 : bytes_rcvd(0),
548 packets_rcvd(0),
549 packets_lost(0),
550 fraction_lost(0.0) {
551 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000552 void add_ssrc(const SsrcReceiverInfo& stat) {
553 local_stats.push_back(stat);
554 }
555 // Temporary utility function for call sites that only provide SSRC.
556 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200557 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000558 SsrcReceiverInfo stat;
559 stat.ssrc = ssrc;
560 add_ssrc(stat);
561 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200562 std::vector<uint32_t> ssrcs() const {
563 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000564 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
565 it != local_stats.end(); ++it) {
566 retval.push_back(it->ssrc);
567 }
568 return retval;
569 }
570 // Utility accessor for clients that make the assumption only one ssrc
571 // exists per media.
572 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200573 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000574 if (local_stats.size() > 0) {
575 return local_stats[0].ssrc;
576 } else {
577 return 0;
578 }
579 }
580
Peter Boström0c4e06b2015-10-07 12:23:21 +0200581 int64_t bytes_rcvd;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000582 int packets_rcvd;
583 int packets_lost;
584 float fraction_lost;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000585 std::string codec_name;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000586 std::vector<SsrcReceiverInfo> local_stats;
587 std::vector<SsrcSenderInfo> remote_stats;
588};
589
590struct VoiceSenderInfo : public MediaSenderInfo {
591 VoiceSenderInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000592 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 jitter_ms(0),
594 audio_level(0),
595 aec_quality_min(0.0),
596 echo_delay_median_ms(0),
597 echo_delay_std_ms(0),
598 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000599 echo_return_loss_enhancement(0),
600 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 }
602
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 int ext_seqnum;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 int jitter_ms;
605 int audio_level;
606 float aec_quality_min;
607 int echo_delay_median_ms;
608 int echo_delay_std_ms;
609 int echo_return_loss;
610 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000611 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612};
613
wu@webrtc.org97077a32013-10-25 21:18:33 +0000614struct VoiceReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 VoiceReceiverInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000616 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 jitter_ms(0),
618 jitter_buffer_ms(0),
619 jitter_buffer_preferred_ms(0),
620 delay_estimate_ms(0),
621 audio_level(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000622 expand_rate(0),
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000623 speech_expand_rate(0),
624 secondary_decoded_rate(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200625 accelerate_rate(0),
626 preemptive_expand_rate(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000627 decoding_calls_to_silence_generator(0),
628 decoding_calls_to_neteq(0),
629 decoding_normal(0),
630 decoding_plc(0),
631 decoding_cng(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000632 decoding_plc_cng(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200633 capture_start_ntp_time_ms(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 int ext_seqnum;
636 int jitter_ms;
637 int jitter_buffer_ms;
638 int jitter_buffer_preferred_ms;
639 int delay_estimate_ms;
640 int audio_level;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000641 // fraction of synthesized audio inserted through expansion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 float expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000643 // fraction of synthesized speech inserted through expansion.
644 float speech_expand_rate;
645 // fraction of data out of secondary decoding, including FEC and RED.
646 float secondary_decoded_rate;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200647 // Fraction of data removed through time compression.
648 float accelerate_rate;
649 // Fraction of data inserted through time stretching.
650 float preemptive_expand_rate;
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000651 int decoding_calls_to_silence_generator;
652 int decoding_calls_to_neteq;
653 int decoding_normal;
654 int decoding_plc;
655 int decoding_cng;
656 int decoding_plc_cng;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000657 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200658 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659};
660
wu@webrtc.org97077a32013-10-25 21:18:33 +0000661struct VideoSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 VideoSenderInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000663 : packets_cached(0),
664 firs_rcvd(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000665 plis_rcvd(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 nacks_rcvd(0),
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000667 send_frame_width(0),
668 send_frame_height(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 framerate_input(0),
670 framerate_sent(0),
671 nominal_bitrate(0),
672 preferred_bitrate(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000673 adapt_reason(0),
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000674 adapt_changes(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000675 avg_encode_ms(0),
Peter Boström8ed6a4b2015-03-27 10:01:02 +0100676 encode_usage_percent(0) {
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000677 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000679 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100680 std::string encoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000681 int packets_cached;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 int firs_rcvd;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000683 int plis_rcvd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 int nacks_rcvd;
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000685 int send_frame_width;
686 int send_frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 int framerate_input;
688 int framerate_sent;
689 int nominal_bitrate;
690 int preferred_bitrate;
691 int adapt_reason;
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000692 int adapt_changes;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000693 int avg_encode_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000694 int encode_usage_percent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695};
696
wu@webrtc.org97077a32013-10-25 21:18:33 +0000697struct VideoReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 VideoReceiverInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000699 : packets_concealed(0),
700 firs_sent(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000701 plis_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 nacks_sent(0),
703 frame_width(0),
704 frame_height(0),
705 framerate_rcvd(0),
706 framerate_decoded(0),
707 framerate_output(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000708 framerate_render_input(0),
709 framerate_render_output(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000710 decode_ms(0),
711 max_decode_ms(0),
712 jitter_buffer_ms(0),
713 min_playout_delay_ms(0),
714 render_delay_ms(0),
715 target_delay_ms(0),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000716 current_delay_ms(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000717 capture_start_ntp_time_ms(-1) {
718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000720 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100721 std::string decoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000722 int packets_concealed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 int firs_sent;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000724 int plis_sent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 int nacks_sent;
726 int frame_width;
727 int frame_height;
728 int framerate_rcvd;
729 int framerate_decoded;
730 int framerate_output;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000731 // Framerate as sent to the renderer.
732 int framerate_render_input;
733 // Framerate that the renderer reports.
734 int framerate_render_output;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000735
736 // All stats below are gathered per-VideoReceiver, but some will be correlated
737 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
738 // structures, reflect this in the new layout.
739
740 // Current frame decode latency.
741 int decode_ms;
742 // Maximum observed frame decode latency.
743 int max_decode_ms;
744 // Jitter (network-related) latency.
745 int jitter_buffer_ms;
746 // Requested minimum playout latency.
747 int min_playout_delay_ms;
748 // Requested latency to account for rendering delay.
749 int render_delay_ms;
750 // Target overall delay: network+decode+render, accounting for
751 // min_playout_delay_ms.
752 int target_delay_ms;
753 // Current overall delay, possibly ramping towards target_delay_ms.
754 int current_delay_ms;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000755
756 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200757 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758};
759
wu@webrtc.org97077a32013-10-25 21:18:33 +0000760struct DataSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 DataSenderInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000762 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 }
764
Peter Boström0c4e06b2015-10-07 12:23:21 +0200765 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766};
767
wu@webrtc.org97077a32013-10-25 21:18:33 +0000768struct DataReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 DataReceiverInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000770 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 }
772
Peter Boström0c4e06b2015-10-07 12:23:21 +0200773 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774};
775
776struct BandwidthEstimationInfo {
777 BandwidthEstimationInfo()
778 : available_send_bandwidth(0),
779 available_recv_bandwidth(0),
780 target_enc_bitrate(0),
781 actual_enc_bitrate(0),
782 retransmit_bitrate(0),
783 transmit_bitrate(0),
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000784 bucket_delay(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 }
786
787 int available_send_bandwidth;
788 int available_recv_bandwidth;
789 int target_enc_bitrate;
790 int actual_enc_bitrate;
791 int retransmit_bitrate;
792 int transmit_bitrate;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000793 int64_t bucket_delay;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794};
795
796struct VoiceMediaInfo {
797 void Clear() {
798 senders.clear();
799 receivers.clear();
800 }
801 std::vector<VoiceSenderInfo> senders;
802 std::vector<VoiceReceiverInfo> receivers;
803};
804
805struct VideoMediaInfo {
806 void Clear() {
807 senders.clear();
808 receivers.clear();
809 bw_estimations.clear();
810 }
811 std::vector<VideoSenderInfo> senders;
812 std::vector<VideoReceiverInfo> receivers;
813 std::vector<BandwidthEstimationInfo> bw_estimations;
814};
815
816struct DataMediaInfo {
817 void Clear() {
818 senders.clear();
819 receivers.clear();
820 }
821 std::vector<DataSenderInfo> senders;
822 std::vector<DataReceiverInfo> receivers;
823};
824
deadbeef13871492015-12-09 12:37:51 -0800825struct RtcpParameters {
826 bool reduced_size = false;
827};
828
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700829template <class Codec>
830struct RtpParameters {
solenberg7e4e01a2015-12-02 08:05:01 -0800831 virtual std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700832 std::ostringstream ost;
833 ost << "{";
834 ost << "codecs: " << VectorToString(codecs) << ", ";
835 ost << "extensions: " << VectorToString(extensions);
836 ost << "}";
837 return ost.str();
838 }
839
840 std::vector<Codec> codecs;
841 std::vector<RtpHeaderExtension> extensions;
842 // TODO(pthatcher): Add streams.
deadbeef13871492015-12-09 12:37:51 -0800843 RtcpParameters rtcp;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700844};
845
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700846// TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
847// encapsulate all the parameters needed for an RtpSender.
nisse05103312016-03-16 02:22:50 -0700848template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700849struct RtpSendParameters : RtpParameters<Codec> {
solenberg7e4e01a2015-12-02 08:05:01 -0800850 std::string ToString() const override {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700851 std::ostringstream ost;
852 ost << "{";
853 ost << "codecs: " << VectorToString(this->codecs) << ", ";
854 ost << "extensions: " << VectorToString(this->extensions) << ", ";
pbos378dc772016-01-28 15:58:41 -0800855 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
nisse05103312016-03-16 02:22:50 -0700856 ost << "}";
857 return ost.str();
858 }
859
860 int max_bandwidth_bps = -1;
861};
862
863struct AudioSendParameters : RtpSendParameters<AudioCodec> {
864 std::string ToString() const override {
865 std::ostringstream ost;
866 ost << "{";
867 ost << "codecs: " << VectorToString(this->codecs) << ", ";
868 ost << "extensions: " << VectorToString(this->extensions) << ", ";
869 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700870 ost << "options: " << options.ToString();
871 ost << "}";
872 return ost.str();
873 }
874
nisse05103312016-03-16 02:22:50 -0700875 AudioOptions options;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700876};
877
878struct AudioRecvParameters : RtpParameters<AudioCodec> {
879};
880
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881class VoiceMediaChannel : public MediaChannel {
882 public:
883 enum Error {
884 ERROR_NONE = 0, // No error.
885 ERROR_OTHER, // Other errors.
886 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
887 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
888 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
889 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
890 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
891 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
892 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
893 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
894 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
895 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
896 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
897 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
898 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
899 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
900 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
901 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
902 };
903
904 VoiceMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800905 VoiceMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 virtual ~VoiceMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200907 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
908 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
skvlade0d46372016-04-07 22:59:22 -0700909 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
910 virtual bool SetRtpParameters(uint32_t ssrc,
911 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 // Starts or stops playout of received audio.
913 virtual bool SetPlayout(bool playout) = 0;
914 // Starts or stops sending (and potentially capture) of local audio.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800915 virtual void SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700916 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200917 virtual bool SetAudioSend(uint32_t ssrc,
918 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700919 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800920 AudioSource* source) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 // Gets current energy levels for all incoming streams.
922 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
923 // Get the current energy level of the stream sent to the speaker.
924 virtual int GetOutputLevel() = 0;
925 // Get the time in milliseconds since last recorded keystroke, or negative.
926 virtual int GetTimeSinceLastTyping() = 0;
927 // Temporarily exposed field for tuning typing detect options.
928 virtual void SetTypingDetectionParameters(int time_window,
929 int cost_per_typing, int reporting_threshold, int penalty_decay,
930 int type_event_delay) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700931 // Set speaker output volume of the specified ssrc.
932 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800934 virtual bool CanInsertDtmf() = 0;
935 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000937 // The valid value for the |event| are 0 to 15 which corresponding to
938 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800939 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 // Gets quality stats for the channel.
941 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100942
943 virtual void SetRawAudioSink(
944 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800945 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946};
947
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700948// TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
949// encapsulate all the parameters needed for a video RtpSender.
nisse05103312016-03-16 02:22:50 -0700950struct VideoSendParameters : RtpSendParameters<VideoCodec> {
nisse4b4dc862016-02-17 05:25:36 -0800951 // Use conference mode? This flag comes from the remote
952 // description's SDP line 'a=x-google-flag:conference', copied over
953 // by VideoChannel::SetRemoteContent_w, and ultimately used by
954 // conference mode screencast logic in
955 // WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig.
956 // The special screencast behaviour is disabled by default.
957 bool conference_mode = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700958};
959
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700960// TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
961// encapsulate all the parameters needed for a video RtpReceiver.
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700962struct VideoRecvParameters : RtpParameters<VideoCodec> {
963};
964
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965class VideoMediaChannel : public MediaChannel {
966 public:
967 enum Error {
968 ERROR_NONE = 0, // No error.
969 ERROR_OTHER, // Other errors.
970 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
971 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
972 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
973 ERROR_REC_DEVICE_REMOVED, // Device is removed.
974 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
975 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
976 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
977 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
978 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
979 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
980 };
981
nisse08582ff2016-02-04 01:24:52 -0800982 VideoMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800983 VideoMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 virtual ~VideoMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200985
986 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
987 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
skvladdc1c62c2016-03-16 19:07:43 -0700988 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
989 virtual bool SetRtpParameters(uint32_t ssrc,
990 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 // Gets the currently set codecs/payload types to be used for outgoing media.
992 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 // Starts or stops transmission (and potentially capture) of local video.
994 virtual bool SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700995 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200996 virtual bool SetVideoSend(uint32_t ssrc,
997 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -0700998 const VideoOptions* options) = 0;
nisse08582ff2016-02-04 01:24:52 -0800999 // Sets the sink object to be used for the specified stream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 // If SSRC is 0, the renderer is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -08001001 virtual bool SetSink(uint32_t ssrc,
1002 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) = 0;
nisse2ded9b12016-04-08 02:23:55 -07001003 // Register a source. The |ssrc| must correspond to a registered send stream.
1004 virtual void SetSource(
1005 uint32_t ssrc,
1006 rtc::VideoSourceInterface<cricket::VideoFrame>* source) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001008 virtual bool GetStats(VideoMediaInfo* info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009};
1010
1011enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001012 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
1013 // values.
1014 DMT_NONE = 0,
1015 DMT_CONTROL = 1,
1016 DMT_BINARY = 2,
1017 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018};
1019
1020// Info about data received in DataMediaChannel. For use in
1021// DataMediaChannel::SignalDataReceived and in all of the signals that
1022// signal fires, on up the chain.
1023struct ReceiveDataParams {
1024 // The in-packet stream indentifier.
1025 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001026 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 // The type of message (binary, text, or control).
1028 DataMessageType type;
1029 // A per-stream value incremented per packet in the stream.
1030 int seq_num;
1031 // A per-stream value monotonically increasing with time.
1032 int timestamp;
1033
1034 ReceiveDataParams() :
1035 ssrc(0),
1036 type(DMT_TEXT),
1037 seq_num(0),
1038 timestamp(0) {
1039 }
1040};
1041
1042struct SendDataParams {
1043 // The in-packet stream indentifier.
1044 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001045 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 // The type of message (binary, text, or control).
1047 DataMessageType type;
1048
1049 // For SCTP, whether to send messages flagged as ordered or not.
1050 // If false, messages can be received out of order.
1051 bool ordered;
1052 // For SCTP, whether the messages are sent reliably or not.
1053 // If false, messages may be lost.
1054 bool reliable;
1055 // For SCTP, if reliable == false, provide partial reliability by
1056 // resending up to this many times. Either count or millis
1057 // is supported, not both at the same time.
1058 int max_rtx_count;
1059 // For SCTP, if reliable == false, provide partial reliability by
1060 // resending for up to this many milliseconds. Either count or millis
1061 // is supported, not both at the same time.
1062 int max_rtx_ms;
1063
1064 SendDataParams() :
1065 ssrc(0),
1066 type(DMT_TEXT),
1067 // TODO(pthatcher): Make these true by default?
1068 ordered(false),
1069 reliable(false),
1070 max_rtx_count(0),
1071 max_rtx_ms(0) {
1072 }
1073};
1074
1075enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
1076
nisse05103312016-03-16 02:22:50 -07001077struct DataSendParameters : RtpSendParameters<DataCodec> {
solenberg7e4e01a2015-12-02 08:05:01 -08001078 std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001079 std::ostringstream ost;
1080 // Options and extensions aren't used.
1081 ost << "{";
1082 ost << "codecs: " << VectorToString(codecs) << ", ";
pbos378dc772016-01-28 15:58:41 -08001083 ost << "max_bandwidth_bps: " << max_bandwidth_bps;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001084 ost << "}";
1085 return ost.str();
1086 }
1087};
1088
1089struct DataRecvParameters : RtpParameters<DataCodec> {
1090};
1091
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092class DataMediaChannel : public MediaChannel {
1093 public:
1094 enum Error {
1095 ERROR_NONE = 0, // No error.
1096 ERROR_OTHER, // Other errors.
1097 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
1098 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1099 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
1100 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1101 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
1102 };
1103
1104 virtual ~DataMediaChannel() {}
1105
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001106 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
1107 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001108
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 // TODO(pthatcher): Implement this.
1110 virtual bool GetStats(DataMediaInfo* info) { return true; }
1111
1112 virtual bool SetSend(bool send) = 0;
1113 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114
Honghai Zhangcc411c02016-03-29 17:27:21 -07001115 virtual void OnNetworkRouteChanged(const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07001116 const rtc::NetworkRoute& network_route) {}
Honghai Zhangcc411c02016-03-29 17:27:21 -07001117
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 virtual bool SendData(
1119 const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001120 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 SendDataResult* result = NULL) = 0;
1122 // Signals when data is received (params, data, len)
1123 sigslot::signal3<const ReceiveDataParams&,
1124 const char*,
1125 size_t> SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001126 // Signal when the media channel is ready to send the stream. Arguments are:
1127 // writable(bool)
1128 sigslot::signal1<bool> SignalReadyToSend;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001129 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001130 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131};
1132
1133} // namespace cricket
1134
kjellandera96e2d72016-02-04 23:52:28 -08001135#endif // WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_