blob: 4166a60dd3171917773a4d1f88a3cebee178b4ee [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032// TODO(juberti): re-evaluate this include
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010033#include "webrtc/pc/audiomonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036class Buffer;
37class RateLimiter;
38class Timing;
39}
40
Tommif888bb52015-12-12 01:37:01 +010041namespace webrtc {
42class AudioSinkInterface;
43}
44
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045namespace cricket {
46
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080047class AudioSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048class ScreencastId;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049class VideoCapturer;
nisse08582ff2016-02-04 01:24:52 -080050class VideoFrame;
tommi1d5c19d2015-12-13 22:54:29 -080051struct RtpHeader;
52struct VideoFormat;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54const int kMinRtpHeaderExtensionId = 1;
55const int kMaxRtpHeaderExtensionId = 255;
56const int kScreencastDefaultFps = 5;
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058template <class T>
Karl Wibergbe579832015-11-10 22:34:18 +010059static std::string ToStringIfSet(const char* key, const rtc::Optional<T>& val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 std::string str;
kwiberg102c6a62015-10-30 02:47:38 -070061 if (val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 str = key;
63 str += ": ";
kwiberg102c6a62015-10-30 02:47:38 -070064 str += val ? rtc::ToString(*val) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 str += ", ";
66 }
67 return str;
68}
69
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070070template <class T>
71static std::string VectorToString(const std::vector<T>& vals) {
72 std::ostringstream ost;
73 ost << "[";
74 for (size_t i = 0; i < vals.size(); ++i) {
75 if (i > 0) {
76 ost << ", ";
77 }
78 ost << vals[i].ToString();
79 }
80 ost << "]";
81 return ost.str();
82}
83
skvladdc1c62c2016-03-16 19:07:43 -070084template <typename T>
85static T MinPositive(T a, T b) {
86 if (a <= 0) {
87 return b;
88 }
89 if (b <= 0) {
90 return a;
91 }
92 return std::min(a, b);
93}
94
nisse51542be2016-02-12 02:27:06 -080095// Construction-time settings, passed to
96// MediaControllerInterface::Create, and passed on when creating
97// MediaChannels.
98struct MediaConfig {
99 // Set DSCP value on packets. This flag comes from the
100 // PeerConnection constraint 'googDscp'.
101 bool enable_dscp = false;
102
nisse0db023a2016-03-01 04:29:59 -0800103 // Video-specific config.
104 struct Video {
105 // Enable WebRTC CPU Overuse Detection. This flag comes from the
106 // PeerConnection constraint 'googCpuOveruseDetection' and is
107 // checked in WebRtcVideoChannel2::OnLoadUpdate, where it's passed
108 // to VideoCapturer::video_adapter()->OnCpuResolutionRequest.
109 bool enable_cpu_overuse_detection = true;
nisse51542be2016-02-12 02:27:06 -0800110
nisse0db023a2016-03-01 04:29:59 -0800111 // Enable WebRTC suspension of video. No video frames will be sent
112 // when the bitrate is below the configured minimum bitrate. This
113 // flag comes from the PeerConnection constraint
114 // 'googSuspendBelowMinBitrate', and WebRtcVideoChannel2 copies it
115 // to VideoSendStream::Config::suspend_below_min_bitrate.
116 bool suspend_below_min_bitrate = false;
nisse51542be2016-02-12 02:27:06 -0800117
nisse0db023a2016-03-01 04:29:59 -0800118 // Set to true if the renderer has an algorithm of frame selection.
119 // If the value is true, then WebRTC will hand over a frame as soon as
120 // possible without delay, and rendering smoothness is completely the duty
121 // of the renderer;
122 // If the value is false, then WebRTC is responsible to delay frame release
123 // in order to increase rendering smoothness.
124 //
125 // This flag comes from PeerConnection's RtcConfiguration, but is
126 // currently only set by the command line flag
127 // 'disable-rtc-smoothness-algorithm'.
128 // WebRtcVideoChannel2::AddRecvStream copies it to the created
129 // WebRtcVideoReceiveStream, where it is returned by the
130 // SmoothsRenderedFrames method. This method is used by the
131 // VideoReceiveStream, where the value is passed on to the
132 // IncomingVideoStream constructor.
133 bool disable_prerenderer_smoothing = false;
134 } video;
nisse51542be2016-02-12 02:27:06 -0800135};
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137// Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine.
138// Used to be flags, but that makes it hard to selectively apply options.
139// We are moving all of the setting of options to structs like this,
140// but some things currently still use flags.
141struct AudioOptions {
142 void SetAll(const AudioOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700143 SetFrom(&echo_cancellation, change.echo_cancellation);
144 SetFrom(&auto_gain_control, change.auto_gain_control);
145 SetFrom(&noise_suppression, change.noise_suppression);
146 SetFrom(&highpass_filter, change.highpass_filter);
147 SetFrom(&stereo_swapping, change.stereo_swapping);
148 SetFrom(&audio_jitter_buffer_max_packets,
149 change.audio_jitter_buffer_max_packets);
150 SetFrom(&audio_jitter_buffer_fast_accelerate,
151 change.audio_jitter_buffer_fast_accelerate);
152 SetFrom(&typing_detection, change.typing_detection);
153 SetFrom(&aecm_generate_comfort_noise, change.aecm_generate_comfort_noise);
kwiberg102c6a62015-10-30 02:47:38 -0700154 SetFrom(&adjust_agc_delta, change.adjust_agc_delta);
155 SetFrom(&experimental_agc, change.experimental_agc);
156 SetFrom(&extended_filter_aec, change.extended_filter_aec);
157 SetFrom(&delay_agnostic_aec, change.delay_agnostic_aec);
158 SetFrom(&experimental_ns, change.experimental_ns);
kwiberg102c6a62015-10-30 02:47:38 -0700159 SetFrom(&tx_agc_target_dbov, change.tx_agc_target_dbov);
160 SetFrom(&tx_agc_digital_compression_gain,
161 change.tx_agc_digital_compression_gain);
162 SetFrom(&tx_agc_limiter, change.tx_agc_limiter);
163 SetFrom(&recording_sample_rate, change.recording_sample_rate);
164 SetFrom(&playout_sample_rate, change.playout_sample_rate);
kwiberg102c6a62015-10-30 02:47:38 -0700165 SetFrom(&combined_audio_video_bwe, change.combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 }
167
168 bool operator==(const AudioOptions& o) const {
169 return echo_cancellation == o.echo_cancellation &&
170 auto_gain_control == o.auto_gain_control &&
171 noise_suppression == o.noise_suppression &&
172 highpass_filter == o.highpass_filter &&
173 stereo_swapping == o.stereo_swapping &&
Henrik Lundin64dad832015-05-11 12:44:23 +0200174 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200175 audio_jitter_buffer_fast_accelerate ==
176 o.audio_jitter_buffer_fast_accelerate &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 typing_detection == o.typing_detection &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000178 aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 experimental_agc == o.experimental_agc &&
Henrik Lundin441f6342015-06-09 16:03:13 +0200180 extended_filter_aec == o.extended_filter_aec &&
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100181 delay_agnostic_aec == o.delay_agnostic_aec &&
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000182 experimental_ns == o.experimental_ns &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 adjust_agc_delta == o.adjust_agc_delta &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000184 tx_agc_target_dbov == o.tx_agc_target_dbov &&
185 tx_agc_digital_compression_gain == o.tx_agc_digital_compression_gain &&
186 tx_agc_limiter == o.tx_agc_limiter &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000187 recording_sample_rate == o.recording_sample_rate &&
wu@webrtc.orgde305012013-10-31 15:40:38 +0000188 playout_sample_rate == o.playout_sample_rate &&
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000189 combined_audio_video_bwe == o.combined_audio_video_bwe;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 }
deadbeef119760a2016-04-04 11:43:27 -0700191 bool operator!=(const AudioOptions& o) const { return !(*this == o); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
193 std::string ToString() const {
194 std::ostringstream ost;
195 ost << "AudioOptions {";
196 ost << ToStringIfSet("aec", echo_cancellation);
197 ost << ToStringIfSet("agc", auto_gain_control);
198 ost << ToStringIfSet("ns", noise_suppression);
199 ost << ToStringIfSet("hf", highpass_filter);
200 ost << ToStringIfSet("swap", stereo_swapping);
Henrik Lundin64dad832015-05-11 12:44:23 +0200201 ost << ToStringIfSet("audio_jitter_buffer_max_packets",
202 audio_jitter_buffer_max_packets);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200203 ost << ToStringIfSet("audio_jitter_buffer_fast_accelerate",
204 audio_jitter_buffer_fast_accelerate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 ost << ToStringIfSet("typing", typing_detection);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000206 ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 ost << ToStringIfSet("agc_delta", adjust_agc_delta);
208 ost << ToStringIfSet("experimental_agc", experimental_agc);
Henrik Lundin441f6342015-06-09 16:03:13 +0200209 ost << ToStringIfSet("extended_filter_aec", extended_filter_aec);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100210 ost << ToStringIfSet("delay_agnostic_aec", delay_agnostic_aec);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000211 ost << ToStringIfSet("experimental_ns", experimental_ns);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000212 ost << ToStringIfSet("tx_agc_target_dbov", tx_agc_target_dbov);
213 ost << ToStringIfSet("tx_agc_digital_compression_gain",
214 tx_agc_digital_compression_gain);
215 ost << ToStringIfSet("tx_agc_limiter", tx_agc_limiter);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000216 ost << ToStringIfSet("recording_sample_rate", recording_sample_rate);
217 ost << ToStringIfSet("playout_sample_rate", playout_sample_rate);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000218 ost << ToStringIfSet("combined_audio_video_bwe", combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 ost << "}";
220 return ost.str();
221 }
222
223 // Audio processing that attempts to filter away the output signal from
224 // later inbound pickup.
Karl Wibergbe579832015-11-10 22:34:18 +0100225 rtc::Optional<bool> echo_cancellation;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 // Audio processing to adjust the sensitivity of the local mic dynamically.
Karl Wibergbe579832015-11-10 22:34:18 +0100227 rtc::Optional<bool> auto_gain_control;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 // Audio processing to filter out background noise.
Karl Wibergbe579832015-11-10 22:34:18 +0100229 rtc::Optional<bool> noise_suppression;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 // Audio processing to remove background noise of lower frequencies.
Karl Wibergbe579832015-11-10 22:34:18 +0100231 rtc::Optional<bool> highpass_filter;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 // Audio processing to swap the left and right channels.
Karl Wibergbe579832015-11-10 22:34:18 +0100233 rtc::Optional<bool> stereo_swapping;
Henrik Lundin64dad832015-05-11 12:44:23 +0200234 // Audio receiver jitter buffer (NetEq) max capacity in number of packets.
Karl Wibergbe579832015-11-10 22:34:18 +0100235 rtc::Optional<int> audio_jitter_buffer_max_packets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200236 // Audio receiver jitter buffer (NetEq) fast accelerate mode.
Karl Wibergbe579832015-11-10 22:34:18 +0100237 rtc::Optional<bool> audio_jitter_buffer_fast_accelerate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 // Audio processing to detect typing.
Karl Wibergbe579832015-11-10 22:34:18 +0100239 rtc::Optional<bool> typing_detection;
240 rtc::Optional<bool> aecm_generate_comfort_noise;
Karl Wibergbe579832015-11-10 22:34:18 +0100241 rtc::Optional<int> adjust_agc_delta;
242 rtc::Optional<bool> experimental_agc;
243 rtc::Optional<bool> extended_filter_aec;
244 rtc::Optional<bool> delay_agnostic_aec;
245 rtc::Optional<bool> experimental_ns;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000246 // Note that tx_agc_* only applies to non-experimental AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100247 rtc::Optional<uint16_t> tx_agc_target_dbov;
248 rtc::Optional<uint16_t> tx_agc_digital_compression_gain;
249 rtc::Optional<bool> tx_agc_limiter;
250 rtc::Optional<uint32_t> recording_sample_rate;
251 rtc::Optional<uint32_t> playout_sample_rate;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000252 // Enable combined audio+bandwidth BWE.
nisse51542be2016-02-12 02:27:06 -0800253 // TODO(pthatcher): This flag is set from the
254 // "googCombinedAudioVideoBwe", but not used anywhere. So delete it,
255 // and check if any other AudioOptions members are unused.
Karl Wibergbe579832015-11-10 22:34:18 +0100256 rtc::Optional<bool> combined_audio_video_bwe;
kwiberg102c6a62015-10-30 02:47:38 -0700257
258 private:
259 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100260 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700261 if (o) {
262 *s = o;
263 }
264 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265};
266
267// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
268// Used to be flags, but that makes it hard to selectively apply options.
269// We are moving all of the setting of options to structs like this,
270// but some things currently still use flags.
271struct VideoOptions {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 void SetAll(const VideoOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700273 SetFrom(&video_noise_reduction, change.video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800274 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100275 SetFrom(&is_screencast, change.is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277
278 bool operator==(const VideoOptions& o) const {
nisseb163c3f2016-01-29 01:14:38 -0800279 return video_noise_reduction == o.video_noise_reduction &&
Niels Möller60653ba2016-03-02 11:41:36 +0100280 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps &&
281 is_screencast == o.is_screencast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 }
deadbeef119760a2016-04-04 11:43:27 -0700283 bool operator!=(const VideoOptions& o) const { return !(*this == o); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
285 std::string ToString() const {
286 std::ostringstream ost;
287 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 ost << ToStringIfSet("noise reduction", video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800289 ost << ToStringIfSet("screencast min bitrate kbps",
290 screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100291 ost << ToStringIfSet("is_screencast ", is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 ost << "}";
293 return ost.str();
294 }
295
nisseb163c3f2016-01-29 01:14:38 -0800296 // Enable denoising? This flag comes from the getUserMedia
297 // constraint 'googNoiseReduction', and WebRtcVideoEngine2 passes it
298 // on to the codec options. Disabled by default.
Karl Wibergbe579832015-11-10 22:34:18 +0100299 rtc::Optional<bool> video_noise_reduction;
nisseb163c3f2016-01-29 01:14:38 -0800300 // Force screencast to use a minimum bitrate. This flag comes from
301 // the PeerConnection constraint 'googScreencastMinBitrate'. It is
302 // copied to the encoder config by WebRtcVideoChannel2.
303 rtc::Optional<int> screencast_min_bitrate_kbps;
Niels Möller60653ba2016-03-02 11:41:36 +0100304 // Set by screencast sources. Implies selection of encoding settings
305 // suitable for screencast. Most likely not the right way to do
306 // things, e.g., screencast of a text document and screencast of a
307 // youtube video have different needs.
308 rtc::Optional<bool> is_screencast;
kwiberg102c6a62015-10-30 02:47:38 -0700309
310 private:
311 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100312 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700313 if (o) {
314 *s = o;
315 }
316 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317};
318
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319struct RtpHeaderExtension {
320 RtpHeaderExtension() : id(0) {}
321 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322
323 bool operator==(const RtpHeaderExtension& ext) const {
324 // id is a reserved word in objective-c. Therefore the id attribute has to
325 // be a fully qualified name in order to compile on IOS.
326 return this->id == ext.id &&
327 uri == ext.uri;
328 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700329
330 std::string ToString() const {
331 std::ostringstream ost;
332 ost << "{";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700333 ost << "uri: " << uri;
solenberg7e4e01a2015-12-02 08:05:01 -0800334 ost << ", id: " << id;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700335 ost << "}";
336 return ost.str();
337 }
338
339 std::string uri;
340 int id;
341 // TODO(juberti): SendRecv direction;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342};
343
344// Returns the named header extension if found among all extensions, NULL
345// otherwise.
346inline const RtpHeaderExtension* FindHeaderExtension(
347 const std::vector<RtpHeaderExtension>& extensions,
348 const std::string& name) {
349 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
350 it != extensions.end(); ++it) {
351 if (it->uri == name)
352 return &(*it);
353 }
354 return NULL;
355}
356
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357class MediaChannel : public sigslot::has_slots<> {
358 public:
359 class NetworkInterface {
360 public:
361 enum SocketType { ST_RTP, ST_RTCP };
jbaucheec21bd2016-03-20 06:15:43 -0700362 virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700363 const rtc::PacketOptions& options) = 0;
jbaucheec21bd2016-03-20 06:15:43 -0700364 virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700365 const rtc::PacketOptions& options) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 virtual int SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 int option) = 0;
368 virtual ~NetworkInterface() {}
369 };
370
nisse51542be2016-02-12 02:27:06 -0800371 MediaChannel(const MediaConfig& config)
372 : enable_dscp_(config.enable_dscp), network_interface_(NULL) {}
373 MediaChannel() : enable_dscp_(false), network_interface_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 virtual ~MediaChannel() {}
375
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000376 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 virtual void SetInterface(NetworkInterface *iface) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000378 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 network_interface_ = iface;
nisse51542be2016-02-12 02:27:06 -0800380 SetDscp(enable_dscp_ ? PreferredDscp() : rtc::DSCP_DEFAULT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 }
nisse51542be2016-02-12 02:27:06 -0800382 virtual rtc::DiffServCodePoint PreferredDscp() const {
383 return rtc::DSCP_DEFAULT;
384 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 // Called when a RTP packet is received.
jbaucheec21bd2016-03-20 06:15:43 -0700386 virtual void OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000387 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 // Called when a RTCP packet is received.
jbaucheec21bd2016-03-20 06:15:43 -0700389 virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000390 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 // Called when the socket's ability to send has changed.
392 virtual void OnReadyToSend(bool ready) = 0;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700393 // Called when the network route used for sending packets changed.
394 virtual void OnNetworkRouteChanged(const std::string& transport_name,
395 const NetworkRoute& network_route) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 // Creates a new outgoing media stream with SSRCs and CNAME as described
397 // by sp.
398 virtual bool AddSendStream(const StreamParams& sp) = 0;
399 // Removes an outgoing media stream.
400 // ssrc must be the first SSRC of the media stream if the stream uses
401 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200402 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 // Creates a new incoming media stream with SSRCs and CNAME as described
404 // by sp.
405 virtual bool AddRecvStream(const StreamParams& sp) = 0;
406 // Removes an incoming media stream.
407 // ssrc must be the first SSRC of the media stream if the stream uses
408 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200409 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000411 // Returns the absoulte sendtime extension id value from media channel.
412 virtual int GetRtpSendTimeExtnId() const {
413 return -1;
414 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000416 // Base method to send packet using NetworkInterface.
jbaucheec21bd2016-03-20 06:15:43 -0700417 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
418 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700419 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000420 }
421
jbaucheec21bd2016-03-20 06:15:43 -0700422 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
423 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700424 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000425 }
426
427 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000428 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000429 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000431 if (!network_interface_)
432 return -1;
433
434 return network_interface_->SetOption(type, opt, option);
435 }
436
nisse51542be2016-02-12 02:27:06 -0800437 private:
wu@webrtc.orgde305012013-10-31 15:40:38 +0000438 // This method sets DSCP |value| on both RTP and RTCP channels.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000439 int SetDscp(rtc::DiffServCodePoint value) {
wu@webrtc.orgde305012013-10-31 15:40:38 +0000440 int ret;
441 ret = SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000442 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000443 value);
444 if (ret == 0) {
445 ret = SetOption(NetworkInterface::ST_RTCP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000446 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000447 value);
448 }
449 return ret;
450 }
451
jbaucheec21bd2016-03-20 06:15:43 -0700452 bool DoSendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700453 bool rtcp,
454 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000455 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000456 if (!network_interface_)
457 return false;
458
stefanc1aeaf02015-10-15 07:26:07 -0700459 return (!rtcp) ? network_interface_->SendPacket(packet, options)
460 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000461 }
462
nisse51542be2016-02-12 02:27:06 -0800463 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000464 // |network_interface_| can be accessed from the worker_thread and
465 // from any MediaEngine threads. This critical section is to protect accessing
466 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000467 rtc::CriticalSection network_interface_crit_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000468 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469};
470
wu@webrtc.org97077a32013-10-25 21:18:33 +0000471// The stats information is structured as follows:
472// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
473// Media contains a vector of SSRC infos that are exclusively used by this
474// media. (SSRCs shared between media streams can't be represented.)
475
476// Information about an SSRC.
477// This data may be locally recorded, or received in an RTCP SR or RR.
478struct SsrcSenderInfo {
479 SsrcSenderInfo()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 : ssrc(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000481 timestamp(0) {
482 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200483 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000484 double timestamp; // NTP timestamp, represented as seconds since epoch.
485};
486
487struct SsrcReceiverInfo {
488 SsrcReceiverInfo()
489 : ssrc(0),
490 timestamp(0) {
491 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200492 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000493 double timestamp;
494};
495
496struct MediaSenderInfo {
497 MediaSenderInfo()
498 : bytes_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 packets_sent(0),
500 packets_lost(0),
501 fraction_lost(0.0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000502 rtt_ms(0) {
503 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000504 void add_ssrc(const SsrcSenderInfo& stat) {
505 local_stats.push_back(stat);
506 }
507 // Temporary utility function for call sites that only provide SSRC.
508 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200509 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000510 SsrcSenderInfo stat;
511 stat.ssrc = ssrc;
512 add_ssrc(stat);
513 }
514 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200515 std::vector<uint32_t> ssrcs() const {
516 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000517 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
518 it != local_stats.end(); ++it) {
519 retval.push_back(it->ssrc);
520 }
521 return retval;
522 }
523 // Utility accessor for clients that make the assumption only one ssrc
524 // exists per media.
525 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200526 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000527 if (local_stats.size() > 0) {
528 return local_stats[0].ssrc;
529 } else {
530 return 0;
531 }
532 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200533 int64_t bytes_sent;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000534 int packets_sent;
535 int packets_lost;
536 float fraction_lost;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000537 int64_t rtt_ms;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000538 std::string codec_name;
539 std::vector<SsrcSenderInfo> local_stats;
540 std::vector<SsrcReceiverInfo> remote_stats;
541};
542
543struct MediaReceiverInfo {
544 MediaReceiverInfo()
545 : bytes_rcvd(0),
546 packets_rcvd(0),
547 packets_lost(0),
548 fraction_lost(0.0) {
549 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000550 void add_ssrc(const SsrcReceiverInfo& stat) {
551 local_stats.push_back(stat);
552 }
553 // Temporary utility function for call sites that only provide SSRC.
554 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200555 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000556 SsrcReceiverInfo stat;
557 stat.ssrc = ssrc;
558 add_ssrc(stat);
559 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200560 std::vector<uint32_t> ssrcs() const {
561 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000562 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
563 it != local_stats.end(); ++it) {
564 retval.push_back(it->ssrc);
565 }
566 return retval;
567 }
568 // Utility accessor for clients that make the assumption only one ssrc
569 // exists per media.
570 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200571 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000572 if (local_stats.size() > 0) {
573 return local_stats[0].ssrc;
574 } else {
575 return 0;
576 }
577 }
578
Peter Boström0c4e06b2015-10-07 12:23:21 +0200579 int64_t bytes_rcvd;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000580 int packets_rcvd;
581 int packets_lost;
582 float fraction_lost;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000583 std::string codec_name;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000584 std::vector<SsrcReceiverInfo> local_stats;
585 std::vector<SsrcSenderInfo> remote_stats;
586};
587
588struct VoiceSenderInfo : public MediaSenderInfo {
589 VoiceSenderInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000590 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 jitter_ms(0),
592 audio_level(0),
593 aec_quality_min(0.0),
594 echo_delay_median_ms(0),
595 echo_delay_std_ms(0),
596 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000597 echo_return_loss_enhancement(0),
598 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 }
600
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 int ext_seqnum;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 int jitter_ms;
603 int audio_level;
604 float aec_quality_min;
605 int echo_delay_median_ms;
606 int echo_delay_std_ms;
607 int echo_return_loss;
608 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000609 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610};
611
wu@webrtc.org97077a32013-10-25 21:18:33 +0000612struct VoiceReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 VoiceReceiverInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000614 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 jitter_ms(0),
616 jitter_buffer_ms(0),
617 jitter_buffer_preferred_ms(0),
618 delay_estimate_ms(0),
619 audio_level(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000620 expand_rate(0),
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000621 speech_expand_rate(0),
622 secondary_decoded_rate(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200623 accelerate_rate(0),
624 preemptive_expand_rate(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000625 decoding_calls_to_silence_generator(0),
626 decoding_calls_to_neteq(0),
627 decoding_normal(0),
628 decoding_plc(0),
629 decoding_cng(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000630 decoding_plc_cng(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200631 capture_start_ntp_time_ms(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 int ext_seqnum;
634 int jitter_ms;
635 int jitter_buffer_ms;
636 int jitter_buffer_preferred_ms;
637 int delay_estimate_ms;
638 int audio_level;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000639 // fraction of synthesized audio inserted through expansion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 float expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000641 // fraction of synthesized speech inserted through expansion.
642 float speech_expand_rate;
643 // fraction of data out of secondary decoding, including FEC and RED.
644 float secondary_decoded_rate;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200645 // Fraction of data removed through time compression.
646 float accelerate_rate;
647 // Fraction of data inserted through time stretching.
648 float preemptive_expand_rate;
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000649 int decoding_calls_to_silence_generator;
650 int decoding_calls_to_neteq;
651 int decoding_normal;
652 int decoding_plc;
653 int decoding_cng;
654 int decoding_plc_cng;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000655 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200656 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657};
658
wu@webrtc.org97077a32013-10-25 21:18:33 +0000659struct VideoSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 VideoSenderInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000661 : packets_cached(0),
662 firs_rcvd(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000663 plis_rcvd(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 nacks_rcvd(0),
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000665 send_frame_width(0),
666 send_frame_height(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 framerate_input(0),
668 framerate_sent(0),
669 nominal_bitrate(0),
670 preferred_bitrate(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000671 adapt_reason(0),
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000672 adapt_changes(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000673 avg_encode_ms(0),
Peter Boström8ed6a4b2015-03-27 10:01:02 +0100674 encode_usage_percent(0) {
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000675 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000677 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100678 std::string encoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000679 int packets_cached;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 int firs_rcvd;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000681 int plis_rcvd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 int nacks_rcvd;
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000683 int send_frame_width;
684 int send_frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 int framerate_input;
686 int framerate_sent;
687 int nominal_bitrate;
688 int preferred_bitrate;
689 int adapt_reason;
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000690 int adapt_changes;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000691 int avg_encode_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000692 int encode_usage_percent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693};
694
wu@webrtc.org97077a32013-10-25 21:18:33 +0000695struct VideoReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 VideoReceiverInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000697 : packets_concealed(0),
698 firs_sent(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000699 plis_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 nacks_sent(0),
701 frame_width(0),
702 frame_height(0),
703 framerate_rcvd(0),
704 framerate_decoded(0),
705 framerate_output(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000706 framerate_render_input(0),
707 framerate_render_output(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000708 decode_ms(0),
709 max_decode_ms(0),
710 jitter_buffer_ms(0),
711 min_playout_delay_ms(0),
712 render_delay_ms(0),
713 target_delay_ms(0),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000714 current_delay_ms(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000715 capture_start_ntp_time_ms(-1) {
716 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000718 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100719 std::string decoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000720 int packets_concealed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 int firs_sent;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000722 int plis_sent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 int nacks_sent;
724 int frame_width;
725 int frame_height;
726 int framerate_rcvd;
727 int framerate_decoded;
728 int framerate_output;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000729 // Framerate as sent to the renderer.
730 int framerate_render_input;
731 // Framerate that the renderer reports.
732 int framerate_render_output;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000733
734 // All stats below are gathered per-VideoReceiver, but some will be correlated
735 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
736 // structures, reflect this in the new layout.
737
738 // Current frame decode latency.
739 int decode_ms;
740 // Maximum observed frame decode latency.
741 int max_decode_ms;
742 // Jitter (network-related) latency.
743 int jitter_buffer_ms;
744 // Requested minimum playout latency.
745 int min_playout_delay_ms;
746 // Requested latency to account for rendering delay.
747 int render_delay_ms;
748 // Target overall delay: network+decode+render, accounting for
749 // min_playout_delay_ms.
750 int target_delay_ms;
751 // Current overall delay, possibly ramping towards target_delay_ms.
752 int current_delay_ms;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000753
754 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200755 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756};
757
wu@webrtc.org97077a32013-10-25 21:18:33 +0000758struct DataSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 DataSenderInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000760 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 }
762
Peter Boström0c4e06b2015-10-07 12:23:21 +0200763 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764};
765
wu@webrtc.org97077a32013-10-25 21:18:33 +0000766struct DataReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 DataReceiverInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000768 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 }
770
Peter Boström0c4e06b2015-10-07 12:23:21 +0200771 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772};
773
774struct BandwidthEstimationInfo {
775 BandwidthEstimationInfo()
776 : available_send_bandwidth(0),
777 available_recv_bandwidth(0),
778 target_enc_bitrate(0),
779 actual_enc_bitrate(0),
780 retransmit_bitrate(0),
781 transmit_bitrate(0),
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000782 bucket_delay(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 }
784
785 int available_send_bandwidth;
786 int available_recv_bandwidth;
787 int target_enc_bitrate;
788 int actual_enc_bitrate;
789 int retransmit_bitrate;
790 int transmit_bitrate;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000791 int64_t bucket_delay;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792};
793
794struct VoiceMediaInfo {
795 void Clear() {
796 senders.clear();
797 receivers.clear();
798 }
799 std::vector<VoiceSenderInfo> senders;
800 std::vector<VoiceReceiverInfo> receivers;
801};
802
803struct VideoMediaInfo {
804 void Clear() {
805 senders.clear();
806 receivers.clear();
807 bw_estimations.clear();
808 }
809 std::vector<VideoSenderInfo> senders;
810 std::vector<VideoReceiverInfo> receivers;
811 std::vector<BandwidthEstimationInfo> bw_estimations;
812};
813
814struct DataMediaInfo {
815 void Clear() {
816 senders.clear();
817 receivers.clear();
818 }
819 std::vector<DataSenderInfo> senders;
820 std::vector<DataReceiverInfo> receivers;
821};
822
deadbeef13871492015-12-09 12:37:51 -0800823struct RtcpParameters {
824 bool reduced_size = false;
825};
826
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700827template <class Codec>
828struct RtpParameters {
solenberg7e4e01a2015-12-02 08:05:01 -0800829 virtual std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700830 std::ostringstream ost;
831 ost << "{";
832 ost << "codecs: " << VectorToString(codecs) << ", ";
833 ost << "extensions: " << VectorToString(extensions);
834 ost << "}";
835 return ost.str();
836 }
837
838 std::vector<Codec> codecs;
839 std::vector<RtpHeaderExtension> extensions;
840 // TODO(pthatcher): Add streams.
deadbeef13871492015-12-09 12:37:51 -0800841 RtcpParameters rtcp;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700842};
843
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700844// TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
845// encapsulate all the parameters needed for an RtpSender.
nisse05103312016-03-16 02:22:50 -0700846template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700847struct RtpSendParameters : RtpParameters<Codec> {
solenberg7e4e01a2015-12-02 08:05:01 -0800848 std::string ToString() const override {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700849 std::ostringstream ost;
850 ost << "{";
851 ost << "codecs: " << VectorToString(this->codecs) << ", ";
852 ost << "extensions: " << VectorToString(this->extensions) << ", ";
pbos378dc772016-01-28 15:58:41 -0800853 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
nisse05103312016-03-16 02:22:50 -0700854 ost << "}";
855 return ost.str();
856 }
857
858 int max_bandwidth_bps = -1;
859};
860
861struct AudioSendParameters : RtpSendParameters<AudioCodec> {
862 std::string ToString() const override {
863 std::ostringstream ost;
864 ost << "{";
865 ost << "codecs: " << VectorToString(this->codecs) << ", ";
866 ost << "extensions: " << VectorToString(this->extensions) << ", ";
867 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700868 ost << "options: " << options.ToString();
869 ost << "}";
870 return ost.str();
871 }
872
nisse05103312016-03-16 02:22:50 -0700873 AudioOptions options;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700874};
875
876struct AudioRecvParameters : RtpParameters<AudioCodec> {
877};
878
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879class VoiceMediaChannel : public MediaChannel {
880 public:
881 enum Error {
882 ERROR_NONE = 0, // No error.
883 ERROR_OTHER, // Other errors.
884 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
885 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
886 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
887 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
888 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
889 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
890 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
891 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
892 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
893 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
894 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
895 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
896 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
897 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
898 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
899 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
900 };
901
902 VoiceMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800903 VoiceMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 virtual ~VoiceMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200905 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
906 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
skvlade0d46372016-04-07 22:59:22 -0700907 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
908 virtual bool SetRtpParameters(uint32_t ssrc,
909 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 // Starts or stops playout of received audio.
911 virtual bool SetPlayout(bool playout) = 0;
912 // Starts or stops sending (and potentially capture) of local audio.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800913 virtual void SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700914 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200915 virtual bool SetAudioSend(uint32_t ssrc,
916 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700917 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800918 AudioSource* source) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 // Gets current energy levels for all incoming streams.
920 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
921 // Get the current energy level of the stream sent to the speaker.
922 virtual int GetOutputLevel() = 0;
923 // Get the time in milliseconds since last recorded keystroke, or negative.
924 virtual int GetTimeSinceLastTyping() = 0;
925 // Temporarily exposed field for tuning typing detect options.
926 virtual void SetTypingDetectionParameters(int time_window,
927 int cost_per_typing, int reporting_threshold, int penalty_decay,
928 int type_event_delay) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700929 // Set speaker output volume of the specified ssrc.
930 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800932 virtual bool CanInsertDtmf() = 0;
933 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000935 // The valid value for the |event| are 0 to 15 which corresponding to
936 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800937 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 // Gets quality stats for the channel.
939 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100940
941 virtual void SetRawAudioSink(
942 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800943 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944};
945
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700946// TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
947// encapsulate all the parameters needed for a video RtpSender.
nisse05103312016-03-16 02:22:50 -0700948struct VideoSendParameters : RtpSendParameters<VideoCodec> {
nisse4b4dc862016-02-17 05:25:36 -0800949 // Use conference mode? This flag comes from the remote
950 // description's SDP line 'a=x-google-flag:conference', copied over
951 // by VideoChannel::SetRemoteContent_w, and ultimately used by
952 // conference mode screencast logic in
953 // WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig.
954 // The special screencast behaviour is disabled by default.
955 bool conference_mode = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700956};
957
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700958// TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
959// encapsulate all the parameters needed for a video RtpReceiver.
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700960struct VideoRecvParameters : RtpParameters<VideoCodec> {
961};
962
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963class VideoMediaChannel : public MediaChannel {
964 public:
965 enum Error {
966 ERROR_NONE = 0, // No error.
967 ERROR_OTHER, // Other errors.
968 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
969 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
970 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
971 ERROR_REC_DEVICE_REMOVED, // Device is removed.
972 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
973 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
974 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
975 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
976 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
977 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
978 };
979
nisse08582ff2016-02-04 01:24:52 -0800980 VideoMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800981 VideoMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 virtual ~VideoMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200983
984 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
985 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
skvladdc1c62c2016-03-16 19:07:43 -0700986 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
987 virtual bool SetRtpParameters(uint32_t ssrc,
988 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 // Gets the currently set codecs/payload types to be used for outgoing media.
990 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 // Starts or stops transmission (and potentially capture) of local video.
992 virtual bool SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700993 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200994 virtual bool SetVideoSend(uint32_t ssrc,
995 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -0700996 const VideoOptions* options) = 0;
nisse08582ff2016-02-04 01:24:52 -0800997 // Sets the sink object to be used for the specified stream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 // If SSRC is 0, the renderer is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -0800999 virtual bool SetSink(uint32_t ssrc,
1000 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 // If |ssrc| is 0, replace the default capturer (engine capturer) with
1002 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001003 virtual bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001005 virtual bool GetStats(VideoMediaInfo* info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006};
1007
1008enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001009 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
1010 // values.
1011 DMT_NONE = 0,
1012 DMT_CONTROL = 1,
1013 DMT_BINARY = 2,
1014 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015};
1016
1017// Info about data received in DataMediaChannel. For use in
1018// DataMediaChannel::SignalDataReceived and in all of the signals that
1019// signal fires, on up the chain.
1020struct ReceiveDataParams {
1021 // The in-packet stream indentifier.
1022 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001023 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 // The type of message (binary, text, or control).
1025 DataMessageType type;
1026 // A per-stream value incremented per packet in the stream.
1027 int seq_num;
1028 // A per-stream value monotonically increasing with time.
1029 int timestamp;
1030
1031 ReceiveDataParams() :
1032 ssrc(0),
1033 type(DMT_TEXT),
1034 seq_num(0),
1035 timestamp(0) {
1036 }
1037};
1038
1039struct SendDataParams {
1040 // The in-packet stream indentifier.
1041 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001042 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 // The type of message (binary, text, or control).
1044 DataMessageType type;
1045
1046 // For SCTP, whether to send messages flagged as ordered or not.
1047 // If false, messages can be received out of order.
1048 bool ordered;
1049 // For SCTP, whether the messages are sent reliably or not.
1050 // If false, messages may be lost.
1051 bool reliable;
1052 // For SCTP, if reliable == false, provide partial reliability by
1053 // resending up to this many times. Either count or millis
1054 // is supported, not both at the same time.
1055 int max_rtx_count;
1056 // For SCTP, if reliable == false, provide partial reliability by
1057 // resending for up to this many milliseconds. Either count or millis
1058 // is supported, not both at the same time.
1059 int max_rtx_ms;
1060
1061 SendDataParams() :
1062 ssrc(0),
1063 type(DMT_TEXT),
1064 // TODO(pthatcher): Make these true by default?
1065 ordered(false),
1066 reliable(false),
1067 max_rtx_count(0),
1068 max_rtx_ms(0) {
1069 }
1070};
1071
1072enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
1073
nisse05103312016-03-16 02:22:50 -07001074struct DataSendParameters : RtpSendParameters<DataCodec> {
solenberg7e4e01a2015-12-02 08:05:01 -08001075 std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001076 std::ostringstream ost;
1077 // Options and extensions aren't used.
1078 ost << "{";
1079 ost << "codecs: " << VectorToString(codecs) << ", ";
pbos378dc772016-01-28 15:58:41 -08001080 ost << "max_bandwidth_bps: " << max_bandwidth_bps;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001081 ost << "}";
1082 return ost.str();
1083 }
1084};
1085
1086struct DataRecvParameters : RtpParameters<DataCodec> {
1087};
1088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089class DataMediaChannel : public MediaChannel {
1090 public:
1091 enum Error {
1092 ERROR_NONE = 0, // No error.
1093 ERROR_OTHER, // Other errors.
1094 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
1095 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1096 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
1097 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1098 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
1099 };
1100
1101 virtual ~DataMediaChannel() {}
1102
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001103 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
1104 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 // TODO(pthatcher): Implement this.
1107 virtual bool GetStats(DataMediaInfo* info) { return true; }
1108
1109 virtual bool SetSend(bool send) = 0;
1110 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111
Honghai Zhangcc411c02016-03-29 17:27:21 -07001112 virtual void OnNetworkRouteChanged(const std::string& transport_name,
1113 const NetworkRoute& network_route) {}
1114
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 virtual bool SendData(
1116 const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001117 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 SendDataResult* result = NULL) = 0;
1119 // Signals when data is received (params, data, len)
1120 sigslot::signal3<const ReceiveDataParams&,
1121 const char*,
1122 size_t> SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001123 // Signal when the media channel is ready to send the stream. Arguments are:
1124 // writable(bool)
1125 sigslot::signal1<bool> SignalReadyToSend;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001126 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001127 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128};
1129
1130} // namespace cricket
1131
kjellandera96e2d72016-02-04 23:52:28 -08001132#endif // WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_