blob: ed565ee5b9df876080beb2e4afd5ff2f17246683 [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"
20#include "webrtc/base/buffer.h"
21#include "webrtc/base/dscp.h"
22#include "webrtc/base/logging.h"
Karl Wibergbe579832015-11-10 22:34:18 +010023#include "webrtc/base/optional.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/sigslot.h"
25#include "webrtc/base/socket.h"
26#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080027#include "webrtc/media/base/codec.h"
kjellanderf4752772016-03-02 05:42:30 -080028#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080029#include "webrtc/media/base/streamparams.h"
nisse08582ff2016-02-04 01:24:52 -080030#include "webrtc/media/base/videosinkinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031// TODO(juberti): re-evaluate this include
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010032#include "webrtc/pc/audiomonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035class Buffer;
36class RateLimiter;
37class Timing;
38}
39
Tommif888bb52015-12-12 01:37:01 +010040namespace webrtc {
41class AudioSinkInterface;
42}
43
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044namespace cricket {
45
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080046class AudioSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047class ScreencastId;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048class VideoCapturer;
nisse08582ff2016-02-04 01:24:52 -080049class VideoFrame;
tommi1d5c19d2015-12-13 22:54:29 -080050struct RtpHeader;
51struct VideoFormat;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53const int kMinRtpHeaderExtensionId = 1;
54const int kMaxRtpHeaderExtensionId = 255;
55const int kScreencastDefaultFps = 5;
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057template <class T>
Karl Wibergbe579832015-11-10 22:34:18 +010058static std::string ToStringIfSet(const char* key, const rtc::Optional<T>& val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 std::string str;
kwiberg102c6a62015-10-30 02:47:38 -070060 if (val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 str = key;
62 str += ": ";
kwiberg102c6a62015-10-30 02:47:38 -070063 str += val ? rtc::ToString(*val) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 str += ", ";
65 }
66 return str;
67}
68
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070069template <class T>
70static std::string VectorToString(const std::vector<T>& vals) {
71 std::ostringstream ost;
72 ost << "[";
73 for (size_t i = 0; i < vals.size(); ++i) {
74 if (i > 0) {
75 ost << ", ";
76 }
77 ost << vals[i].ToString();
78 }
79 ost << "]";
80 return ost.str();
81}
82
skvladdc1c62c2016-03-16 19:07:43 -070083template <typename T>
84static T MinPositive(T a, T b) {
85 if (a <= 0) {
86 return b;
87 }
88 if (b <= 0) {
89 return a;
90 }
91 return std::min(a, b);
92}
93
nisse51542be2016-02-12 02:27:06 -080094// Construction-time settings, passed to
95// MediaControllerInterface::Create, and passed on when creating
96// MediaChannels.
97struct MediaConfig {
98 // Set DSCP value on packets. This flag comes from the
99 // PeerConnection constraint 'googDscp'.
100 bool enable_dscp = false;
101
nisse0db023a2016-03-01 04:29:59 -0800102 // Video-specific config.
103 struct Video {
104 // Enable WebRTC CPU Overuse Detection. This flag comes from the
105 // PeerConnection constraint 'googCpuOveruseDetection' and is
106 // checked in WebRtcVideoChannel2::OnLoadUpdate, where it's passed
107 // to VideoCapturer::video_adapter()->OnCpuResolutionRequest.
108 bool enable_cpu_overuse_detection = true;
nisse51542be2016-02-12 02:27:06 -0800109
nisse0db023a2016-03-01 04:29:59 -0800110 // Enable WebRTC suspension of video. No video frames will be sent
111 // when the bitrate is below the configured minimum bitrate. This
112 // flag comes from the PeerConnection constraint
113 // 'googSuspendBelowMinBitrate', and WebRtcVideoChannel2 copies it
114 // to VideoSendStream::Config::suspend_below_min_bitrate.
115 bool suspend_below_min_bitrate = false;
nisse51542be2016-02-12 02:27:06 -0800116
nisse0db023a2016-03-01 04:29:59 -0800117 // Set to true if the renderer has an algorithm of frame selection.
118 // If the value is true, then WebRTC will hand over a frame as soon as
119 // possible without delay, and rendering smoothness is completely the duty
120 // of the renderer;
121 // If the value is false, then WebRTC is responsible to delay frame release
122 // in order to increase rendering smoothness.
123 //
124 // This flag comes from PeerConnection's RtcConfiguration, but is
125 // currently only set by the command line flag
126 // 'disable-rtc-smoothness-algorithm'.
127 // WebRtcVideoChannel2::AddRecvStream copies it to the created
128 // WebRtcVideoReceiveStream, where it is returned by the
129 // SmoothsRenderedFrames method. This method is used by the
130 // VideoReceiveStream, where the value is passed on to the
131 // IncomingVideoStream constructor.
132 bool disable_prerenderer_smoothing = false;
133 } video;
nisse51542be2016-02-12 02:27:06 -0800134};
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136// Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine.
137// Used to be flags, but that makes it hard to selectively apply options.
138// We are moving all of the setting of options to structs like this,
139// but some things currently still use flags.
140struct AudioOptions {
141 void SetAll(const AudioOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700142 SetFrom(&echo_cancellation, change.echo_cancellation);
143 SetFrom(&auto_gain_control, change.auto_gain_control);
144 SetFrom(&noise_suppression, change.noise_suppression);
145 SetFrom(&highpass_filter, change.highpass_filter);
146 SetFrom(&stereo_swapping, change.stereo_swapping);
147 SetFrom(&audio_jitter_buffer_max_packets,
148 change.audio_jitter_buffer_max_packets);
149 SetFrom(&audio_jitter_buffer_fast_accelerate,
150 change.audio_jitter_buffer_fast_accelerate);
151 SetFrom(&typing_detection, change.typing_detection);
152 SetFrom(&aecm_generate_comfort_noise, change.aecm_generate_comfort_noise);
kwiberg102c6a62015-10-30 02:47:38 -0700153 SetFrom(&adjust_agc_delta, change.adjust_agc_delta);
154 SetFrom(&experimental_agc, change.experimental_agc);
155 SetFrom(&extended_filter_aec, change.extended_filter_aec);
156 SetFrom(&delay_agnostic_aec, change.delay_agnostic_aec);
157 SetFrom(&experimental_ns, change.experimental_ns);
kwiberg102c6a62015-10-30 02:47:38 -0700158 SetFrom(&tx_agc_target_dbov, change.tx_agc_target_dbov);
159 SetFrom(&tx_agc_digital_compression_gain,
160 change.tx_agc_digital_compression_gain);
161 SetFrom(&tx_agc_limiter, change.tx_agc_limiter);
162 SetFrom(&recording_sample_rate, change.recording_sample_rate);
163 SetFrom(&playout_sample_rate, change.playout_sample_rate);
kwiberg102c6a62015-10-30 02:47:38 -0700164 SetFrom(&combined_audio_video_bwe, change.combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
166
167 bool operator==(const AudioOptions& o) const {
168 return echo_cancellation == o.echo_cancellation &&
169 auto_gain_control == o.auto_gain_control &&
170 noise_suppression == o.noise_suppression &&
171 highpass_filter == o.highpass_filter &&
172 stereo_swapping == o.stereo_swapping &&
Henrik Lundin64dad832015-05-11 12:44:23 +0200173 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200174 audio_jitter_buffer_fast_accelerate ==
175 o.audio_jitter_buffer_fast_accelerate &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 typing_detection == o.typing_detection &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000177 aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 experimental_agc == o.experimental_agc &&
Henrik Lundin441f6342015-06-09 16:03:13 +0200179 extended_filter_aec == o.extended_filter_aec &&
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100180 delay_agnostic_aec == o.delay_agnostic_aec &&
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000181 experimental_ns == o.experimental_ns &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 adjust_agc_delta == o.adjust_agc_delta &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000183 tx_agc_target_dbov == o.tx_agc_target_dbov &&
184 tx_agc_digital_compression_gain == o.tx_agc_digital_compression_gain &&
185 tx_agc_limiter == o.tx_agc_limiter &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000186 recording_sample_rate == o.recording_sample_rate &&
wu@webrtc.orgde305012013-10-31 15:40:38 +0000187 playout_sample_rate == o.playout_sample_rate &&
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000188 combined_audio_video_bwe == o.combined_audio_video_bwe;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 }
190
191 std::string ToString() const {
192 std::ostringstream ost;
193 ost << "AudioOptions {";
194 ost << ToStringIfSet("aec", echo_cancellation);
195 ost << ToStringIfSet("agc", auto_gain_control);
196 ost << ToStringIfSet("ns", noise_suppression);
197 ost << ToStringIfSet("hf", highpass_filter);
198 ost << ToStringIfSet("swap", stereo_swapping);
Henrik Lundin64dad832015-05-11 12:44:23 +0200199 ost << ToStringIfSet("audio_jitter_buffer_max_packets",
200 audio_jitter_buffer_max_packets);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200201 ost << ToStringIfSet("audio_jitter_buffer_fast_accelerate",
202 audio_jitter_buffer_fast_accelerate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 ost << ToStringIfSet("typing", typing_detection);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000204 ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 ost << ToStringIfSet("agc_delta", adjust_agc_delta);
206 ost << ToStringIfSet("experimental_agc", experimental_agc);
Henrik Lundin441f6342015-06-09 16:03:13 +0200207 ost << ToStringIfSet("extended_filter_aec", extended_filter_aec);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100208 ost << ToStringIfSet("delay_agnostic_aec", delay_agnostic_aec);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000209 ost << ToStringIfSet("experimental_ns", experimental_ns);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000210 ost << ToStringIfSet("tx_agc_target_dbov", tx_agc_target_dbov);
211 ost << ToStringIfSet("tx_agc_digital_compression_gain",
212 tx_agc_digital_compression_gain);
213 ost << ToStringIfSet("tx_agc_limiter", tx_agc_limiter);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000214 ost << ToStringIfSet("recording_sample_rate", recording_sample_rate);
215 ost << ToStringIfSet("playout_sample_rate", playout_sample_rate);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000216 ost << ToStringIfSet("combined_audio_video_bwe", combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 ost << "}";
218 return ost.str();
219 }
220
221 // Audio processing that attempts to filter away the output signal from
222 // later inbound pickup.
Karl Wibergbe579832015-11-10 22:34:18 +0100223 rtc::Optional<bool> echo_cancellation;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 // Audio processing to adjust the sensitivity of the local mic dynamically.
Karl Wibergbe579832015-11-10 22:34:18 +0100225 rtc::Optional<bool> auto_gain_control;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 // Audio processing to filter out background noise.
Karl Wibergbe579832015-11-10 22:34:18 +0100227 rtc::Optional<bool> noise_suppression;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 // Audio processing to remove background noise of lower frequencies.
Karl Wibergbe579832015-11-10 22:34:18 +0100229 rtc::Optional<bool> highpass_filter;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 // Audio processing to swap the left and right channels.
Karl Wibergbe579832015-11-10 22:34:18 +0100231 rtc::Optional<bool> stereo_swapping;
Henrik Lundin64dad832015-05-11 12:44:23 +0200232 // Audio receiver jitter buffer (NetEq) max capacity in number of packets.
Karl Wibergbe579832015-11-10 22:34:18 +0100233 rtc::Optional<int> audio_jitter_buffer_max_packets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200234 // Audio receiver jitter buffer (NetEq) fast accelerate mode.
Karl Wibergbe579832015-11-10 22:34:18 +0100235 rtc::Optional<bool> audio_jitter_buffer_fast_accelerate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 // Audio processing to detect typing.
Karl Wibergbe579832015-11-10 22:34:18 +0100237 rtc::Optional<bool> typing_detection;
238 rtc::Optional<bool> aecm_generate_comfort_noise;
Karl Wibergbe579832015-11-10 22:34:18 +0100239 rtc::Optional<int> adjust_agc_delta;
240 rtc::Optional<bool> experimental_agc;
241 rtc::Optional<bool> extended_filter_aec;
242 rtc::Optional<bool> delay_agnostic_aec;
243 rtc::Optional<bool> experimental_ns;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000244 // Note that tx_agc_* only applies to non-experimental AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100245 rtc::Optional<uint16_t> tx_agc_target_dbov;
246 rtc::Optional<uint16_t> tx_agc_digital_compression_gain;
247 rtc::Optional<bool> tx_agc_limiter;
248 rtc::Optional<uint32_t> recording_sample_rate;
249 rtc::Optional<uint32_t> playout_sample_rate;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000250 // Enable combined audio+bandwidth BWE.
nisse51542be2016-02-12 02:27:06 -0800251 // TODO(pthatcher): This flag is set from the
252 // "googCombinedAudioVideoBwe", but not used anywhere. So delete it,
253 // and check if any other AudioOptions members are unused.
Karl Wibergbe579832015-11-10 22:34:18 +0100254 rtc::Optional<bool> combined_audio_video_bwe;
kwiberg102c6a62015-10-30 02:47:38 -0700255
256 private:
257 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100258 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700259 if (o) {
260 *s = o;
261 }
262 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263};
264
265// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
266// Used to be flags, but that makes it hard to selectively apply options.
267// We are moving all of the setting of options to structs like this,
268// but some things currently still use flags.
269struct VideoOptions {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 void SetAll(const VideoOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700271 SetFrom(&video_noise_reduction, change.video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800272 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100273 SetFrom(&is_screencast, change.is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 }
275
276 bool operator==(const VideoOptions& o) const {
nisseb163c3f2016-01-29 01:14:38 -0800277 return video_noise_reduction == o.video_noise_reduction &&
Niels Möller60653ba2016-03-02 11:41:36 +0100278 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps &&
279 is_screencast == o.is_screencast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 }
281
282 std::string ToString() const {
283 std::ostringstream ost;
284 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 ost << ToStringIfSet("noise reduction", video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800286 ost << ToStringIfSet("screencast min bitrate kbps",
287 screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100288 ost << ToStringIfSet("is_screencast ", is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 ost << "}";
290 return ost.str();
291 }
292
nisseb163c3f2016-01-29 01:14:38 -0800293 // Enable denoising? This flag comes from the getUserMedia
294 // constraint 'googNoiseReduction', and WebRtcVideoEngine2 passes it
295 // on to the codec options. Disabled by default.
Karl Wibergbe579832015-11-10 22:34:18 +0100296 rtc::Optional<bool> video_noise_reduction;
nisseb163c3f2016-01-29 01:14:38 -0800297 // Force screencast to use a minimum bitrate. This flag comes from
298 // the PeerConnection constraint 'googScreencastMinBitrate'. It is
299 // copied to the encoder config by WebRtcVideoChannel2.
300 rtc::Optional<int> screencast_min_bitrate_kbps;
Niels Möller60653ba2016-03-02 11:41:36 +0100301 // Set by screencast sources. Implies selection of encoding settings
302 // suitable for screencast. Most likely not the right way to do
303 // things, e.g., screencast of a text document and screencast of a
304 // youtube video have different needs.
305 rtc::Optional<bool> is_screencast;
kwiberg102c6a62015-10-30 02:47:38 -0700306
307 private:
308 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100309 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700310 if (o) {
311 *s = o;
312 }
313 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314};
315
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316struct RtpHeaderExtension {
317 RtpHeaderExtension() : id(0) {}
318 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319
320 bool operator==(const RtpHeaderExtension& ext) const {
321 // id is a reserved word in objective-c. Therefore the id attribute has to
322 // be a fully qualified name in order to compile on IOS.
323 return this->id == ext.id &&
324 uri == ext.uri;
325 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700326
327 std::string ToString() const {
328 std::ostringstream ost;
329 ost << "{";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700330 ost << "uri: " << uri;
solenberg7e4e01a2015-12-02 08:05:01 -0800331 ost << ", id: " << id;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700332 ost << "}";
333 return ost.str();
334 }
335
336 std::string uri;
337 int id;
338 // TODO(juberti): SendRecv direction;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339};
340
341// Returns the named header extension if found among all extensions, NULL
342// otherwise.
343inline const RtpHeaderExtension* FindHeaderExtension(
344 const std::vector<RtpHeaderExtension>& extensions,
345 const std::string& name) {
346 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
347 it != extensions.end(); ++it) {
348 if (it->uri == name)
349 return &(*it);
350 }
351 return NULL;
352}
353
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354class MediaChannel : public sigslot::has_slots<> {
355 public:
356 class NetworkInterface {
357 public:
358 enum SocketType { ST_RTP, ST_RTCP };
stefanc1aeaf02015-10-15 07:26:07 -0700359 virtual bool SendPacket(rtc::Buffer* packet,
360 const rtc::PacketOptions& options) = 0;
361 virtual bool SendRtcp(rtc::Buffer* packet,
362 const rtc::PacketOptions& options) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000363 virtual int SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 int option) = 0;
365 virtual ~NetworkInterface() {}
366 };
367
nisse51542be2016-02-12 02:27:06 -0800368 MediaChannel(const MediaConfig& config)
369 : enable_dscp_(config.enable_dscp), network_interface_(NULL) {}
370 MediaChannel() : enable_dscp_(false), network_interface_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 virtual ~MediaChannel() {}
372
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000373 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 virtual void SetInterface(NetworkInterface *iface) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000375 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 network_interface_ = iface;
nisse51542be2016-02-12 02:27:06 -0800377 SetDscp(enable_dscp_ ? PreferredDscp() : rtc::DSCP_DEFAULT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 }
nisse51542be2016-02-12 02:27:06 -0800379 virtual rtc::DiffServCodePoint PreferredDscp() const {
380 return rtc::DSCP_DEFAULT;
381 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 // Called when a RTP packet is received.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000383 virtual void OnPacketReceived(rtc::Buffer* packet,
384 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 // Called when a RTCP packet is received.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000386 virtual void OnRtcpReceived(rtc::Buffer* packet,
387 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 // Called when the socket's ability to send has changed.
389 virtual void OnReadyToSend(bool ready) = 0;
390 // Creates a new outgoing media stream with SSRCs and CNAME as described
391 // by sp.
392 virtual bool AddSendStream(const StreamParams& sp) = 0;
393 // Removes an outgoing media stream.
394 // ssrc must be the first SSRC of the media stream if the stream uses
395 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200396 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 // Creates a new incoming media stream with SSRCs and CNAME as described
398 // by sp.
399 virtual bool AddRecvStream(const StreamParams& sp) = 0;
400 // Removes an incoming media stream.
401 // ssrc must be the first SSRC of the media stream if the stream uses
402 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000405 // Returns the absoulte sendtime extension id value from media channel.
406 virtual int GetRtpSendTimeExtnId() const {
407 return -1;
408 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000410 // Base method to send packet using NetworkInterface.
stefanc1aeaf02015-10-15 07:26:07 -0700411 bool SendPacket(rtc::Buffer* packet, const rtc::PacketOptions& options) {
412 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000413 }
414
stefanc1aeaf02015-10-15 07:26:07 -0700415 bool SendRtcp(rtc::Buffer* packet, const rtc::PacketOptions& options) {
416 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000417 }
418
419 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000420 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000421 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000422 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000423 if (!network_interface_)
424 return -1;
425
426 return network_interface_->SetOption(type, opt, option);
427 }
428
nisse51542be2016-02-12 02:27:06 -0800429 private:
wu@webrtc.orgde305012013-10-31 15:40:38 +0000430 // This method sets DSCP |value| on both RTP and RTCP channels.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000431 int SetDscp(rtc::DiffServCodePoint value) {
wu@webrtc.orgde305012013-10-31 15:40:38 +0000432 int ret;
433 ret = SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000434 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000435 value);
436 if (ret == 0) {
437 ret = SetOption(NetworkInterface::ST_RTCP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000439 value);
440 }
441 return ret;
442 }
443
stefanc1aeaf02015-10-15 07:26:07 -0700444 bool DoSendPacket(rtc::Buffer* packet,
445 bool rtcp,
446 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000447 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000448 if (!network_interface_)
449 return false;
450
stefanc1aeaf02015-10-15 07:26:07 -0700451 return (!rtcp) ? network_interface_->SendPacket(packet, options)
452 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000453 }
454
nisse51542be2016-02-12 02:27:06 -0800455 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000456 // |network_interface_| can be accessed from the worker_thread and
457 // from any MediaEngine threads. This critical section is to protect accessing
458 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000459 rtc::CriticalSection network_interface_crit_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000460 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461};
462
wu@webrtc.org97077a32013-10-25 21:18:33 +0000463// The stats information is structured as follows:
464// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
465// Media contains a vector of SSRC infos that are exclusively used by this
466// media. (SSRCs shared between media streams can't be represented.)
467
468// Information about an SSRC.
469// This data may be locally recorded, or received in an RTCP SR or RR.
470struct SsrcSenderInfo {
471 SsrcSenderInfo()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 : ssrc(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000473 timestamp(0) {
474 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200475 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000476 double timestamp; // NTP timestamp, represented as seconds since epoch.
477};
478
479struct SsrcReceiverInfo {
480 SsrcReceiverInfo()
481 : ssrc(0),
482 timestamp(0) {
483 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200484 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000485 double timestamp;
486};
487
488struct MediaSenderInfo {
489 MediaSenderInfo()
490 : bytes_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 packets_sent(0),
492 packets_lost(0),
493 fraction_lost(0.0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000494 rtt_ms(0) {
495 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000496 void add_ssrc(const SsrcSenderInfo& stat) {
497 local_stats.push_back(stat);
498 }
499 // Temporary utility function for call sites that only provide SSRC.
500 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200501 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000502 SsrcSenderInfo stat;
503 stat.ssrc = ssrc;
504 add_ssrc(stat);
505 }
506 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200507 std::vector<uint32_t> ssrcs() const {
508 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000509 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
510 it != local_stats.end(); ++it) {
511 retval.push_back(it->ssrc);
512 }
513 return retval;
514 }
515 // Utility accessor for clients that make the assumption only one ssrc
516 // exists per media.
517 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200518 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000519 if (local_stats.size() > 0) {
520 return local_stats[0].ssrc;
521 } else {
522 return 0;
523 }
524 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200525 int64_t bytes_sent;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000526 int packets_sent;
527 int packets_lost;
528 float fraction_lost;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000529 int64_t rtt_ms;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000530 std::string codec_name;
531 std::vector<SsrcSenderInfo> local_stats;
532 std::vector<SsrcReceiverInfo> remote_stats;
533};
534
535struct MediaReceiverInfo {
536 MediaReceiverInfo()
537 : bytes_rcvd(0),
538 packets_rcvd(0),
539 packets_lost(0),
540 fraction_lost(0.0) {
541 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000542 void add_ssrc(const SsrcReceiverInfo& stat) {
543 local_stats.push_back(stat);
544 }
545 // Temporary utility function for call sites that only provide SSRC.
546 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200547 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000548 SsrcReceiverInfo stat;
549 stat.ssrc = ssrc;
550 add_ssrc(stat);
551 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200552 std::vector<uint32_t> ssrcs() const {
553 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000554 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
555 it != local_stats.end(); ++it) {
556 retval.push_back(it->ssrc);
557 }
558 return retval;
559 }
560 // Utility accessor for clients that make the assumption only one ssrc
561 // exists per media.
562 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200563 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000564 if (local_stats.size() > 0) {
565 return local_stats[0].ssrc;
566 } else {
567 return 0;
568 }
569 }
570
Peter Boström0c4e06b2015-10-07 12:23:21 +0200571 int64_t bytes_rcvd;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000572 int packets_rcvd;
573 int packets_lost;
574 float fraction_lost;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000575 std::string codec_name;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000576 std::vector<SsrcReceiverInfo> local_stats;
577 std::vector<SsrcSenderInfo> remote_stats;
578};
579
580struct VoiceSenderInfo : public MediaSenderInfo {
581 VoiceSenderInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000582 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 jitter_ms(0),
584 audio_level(0),
585 aec_quality_min(0.0),
586 echo_delay_median_ms(0),
587 echo_delay_std_ms(0),
588 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000589 echo_return_loss_enhancement(0),
590 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 }
592
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 int ext_seqnum;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 int jitter_ms;
595 int audio_level;
596 float aec_quality_min;
597 int echo_delay_median_ms;
598 int echo_delay_std_ms;
599 int echo_return_loss;
600 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000601 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602};
603
wu@webrtc.org97077a32013-10-25 21:18:33 +0000604struct VoiceReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 VoiceReceiverInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000606 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 jitter_ms(0),
608 jitter_buffer_ms(0),
609 jitter_buffer_preferred_ms(0),
610 delay_estimate_ms(0),
611 audio_level(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000612 expand_rate(0),
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000613 speech_expand_rate(0),
614 secondary_decoded_rate(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200615 accelerate_rate(0),
616 preemptive_expand_rate(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000617 decoding_calls_to_silence_generator(0),
618 decoding_calls_to_neteq(0),
619 decoding_normal(0),
620 decoding_plc(0),
621 decoding_cng(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000622 decoding_plc_cng(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200623 capture_start_ntp_time_ms(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 int ext_seqnum;
626 int jitter_ms;
627 int jitter_buffer_ms;
628 int jitter_buffer_preferred_ms;
629 int delay_estimate_ms;
630 int audio_level;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000631 // fraction of synthesized audio inserted through expansion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 float expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000633 // fraction of synthesized speech inserted through expansion.
634 float speech_expand_rate;
635 // fraction of data out of secondary decoding, including FEC and RED.
636 float secondary_decoded_rate;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200637 // Fraction of data removed through time compression.
638 float accelerate_rate;
639 // Fraction of data inserted through time stretching.
640 float preemptive_expand_rate;
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000641 int decoding_calls_to_silence_generator;
642 int decoding_calls_to_neteq;
643 int decoding_normal;
644 int decoding_plc;
645 int decoding_cng;
646 int decoding_plc_cng;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000647 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200648 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649};
650
wu@webrtc.org97077a32013-10-25 21:18:33 +0000651struct VideoSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 VideoSenderInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000653 : packets_cached(0),
654 firs_rcvd(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000655 plis_rcvd(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 nacks_rcvd(0),
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000657 input_frame_width(0),
658 input_frame_height(0),
659 send_frame_width(0),
660 send_frame_height(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 framerate_input(0),
662 framerate_sent(0),
663 nominal_bitrate(0),
664 preferred_bitrate(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000665 adapt_reason(0),
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000666 adapt_changes(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000667 avg_encode_ms(0),
Peter Boström8ed6a4b2015-03-27 10:01:02 +0100668 encode_usage_percent(0) {
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000669 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000671 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100672 std::string encoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000673 int packets_cached;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 int firs_rcvd;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000675 int plis_rcvd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 int nacks_rcvd;
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000677 int input_frame_width;
678 int input_frame_height;
679 int send_frame_width;
680 int send_frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 int framerate_input;
682 int framerate_sent;
683 int nominal_bitrate;
684 int preferred_bitrate;
685 int adapt_reason;
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000686 int adapt_changes;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000687 int avg_encode_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000688 int encode_usage_percent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689};
690
wu@webrtc.org97077a32013-10-25 21:18:33 +0000691struct VideoReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 VideoReceiverInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000693 : packets_concealed(0),
694 firs_sent(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000695 plis_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 nacks_sent(0),
697 frame_width(0),
698 frame_height(0),
699 framerate_rcvd(0),
700 framerate_decoded(0),
701 framerate_output(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000702 framerate_render_input(0),
703 framerate_render_output(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000704 decode_ms(0),
705 max_decode_ms(0),
706 jitter_buffer_ms(0),
707 min_playout_delay_ms(0),
708 render_delay_ms(0),
709 target_delay_ms(0),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000710 current_delay_ms(0),
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000711 capture_start_ntp_time_ms(-1) {
712 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000714 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100715 std::string decoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000716 int packets_concealed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 int firs_sent;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000718 int plis_sent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 int nacks_sent;
720 int frame_width;
721 int frame_height;
722 int framerate_rcvd;
723 int framerate_decoded;
724 int framerate_output;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000725 // Framerate as sent to the renderer.
726 int framerate_render_input;
727 // Framerate that the renderer reports.
728 int framerate_render_output;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000729
730 // All stats below are gathered per-VideoReceiver, but some will be correlated
731 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
732 // structures, reflect this in the new layout.
733
734 // Current frame decode latency.
735 int decode_ms;
736 // Maximum observed frame decode latency.
737 int max_decode_ms;
738 // Jitter (network-related) latency.
739 int jitter_buffer_ms;
740 // Requested minimum playout latency.
741 int min_playout_delay_ms;
742 // Requested latency to account for rendering delay.
743 int render_delay_ms;
744 // Target overall delay: network+decode+render, accounting for
745 // min_playout_delay_ms.
746 int target_delay_ms;
747 // Current overall delay, possibly ramping towards target_delay_ms.
748 int current_delay_ms;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000749
750 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200751 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752};
753
wu@webrtc.org97077a32013-10-25 21:18:33 +0000754struct DataSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 DataSenderInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000756 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758
Peter Boström0c4e06b2015-10-07 12:23:21 +0200759 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760};
761
wu@webrtc.org97077a32013-10-25 21:18:33 +0000762struct DataReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 DataReceiverInfo()
wu@webrtc.org97077a32013-10-25 21:18:33 +0000764 : ssrc(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 }
766
Peter Boström0c4e06b2015-10-07 12:23:21 +0200767 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768};
769
770struct BandwidthEstimationInfo {
771 BandwidthEstimationInfo()
772 : available_send_bandwidth(0),
773 available_recv_bandwidth(0),
774 target_enc_bitrate(0),
775 actual_enc_bitrate(0),
776 retransmit_bitrate(0),
777 transmit_bitrate(0),
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000778 bucket_delay(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 }
780
781 int available_send_bandwidth;
782 int available_recv_bandwidth;
783 int target_enc_bitrate;
784 int actual_enc_bitrate;
785 int retransmit_bitrate;
786 int transmit_bitrate;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000787 int64_t bucket_delay;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788};
789
790struct VoiceMediaInfo {
791 void Clear() {
792 senders.clear();
793 receivers.clear();
794 }
795 std::vector<VoiceSenderInfo> senders;
796 std::vector<VoiceReceiverInfo> receivers;
797};
798
799struct VideoMediaInfo {
800 void Clear() {
801 senders.clear();
802 receivers.clear();
803 bw_estimations.clear();
804 }
805 std::vector<VideoSenderInfo> senders;
806 std::vector<VideoReceiverInfo> receivers;
807 std::vector<BandwidthEstimationInfo> bw_estimations;
808};
809
810struct DataMediaInfo {
811 void Clear() {
812 senders.clear();
813 receivers.clear();
814 }
815 std::vector<DataSenderInfo> senders;
816 std::vector<DataReceiverInfo> receivers;
817};
818
deadbeef13871492015-12-09 12:37:51 -0800819struct RtcpParameters {
820 bool reduced_size = false;
821};
822
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700823template <class Codec>
824struct RtpParameters {
solenberg7e4e01a2015-12-02 08:05:01 -0800825 virtual std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700826 std::ostringstream ost;
827 ost << "{";
828 ost << "codecs: " << VectorToString(codecs) << ", ";
829 ost << "extensions: " << VectorToString(extensions);
830 ost << "}";
831 return ost.str();
832 }
833
834 std::vector<Codec> codecs;
835 std::vector<RtpHeaderExtension> extensions;
836 // TODO(pthatcher): Add streams.
deadbeef13871492015-12-09 12:37:51 -0800837 RtcpParameters rtcp;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700838};
839
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700840// TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
841// encapsulate all the parameters needed for an RtpSender.
nisse05103312016-03-16 02:22:50 -0700842template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700843struct RtpSendParameters : RtpParameters<Codec> {
solenberg7e4e01a2015-12-02 08:05:01 -0800844 std::string ToString() const override {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700845 std::ostringstream ost;
846 ost << "{";
847 ost << "codecs: " << VectorToString(this->codecs) << ", ";
848 ost << "extensions: " << VectorToString(this->extensions) << ", ";
pbos378dc772016-01-28 15:58:41 -0800849 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
nisse05103312016-03-16 02:22:50 -0700850 ost << "}";
851 return ost.str();
852 }
853
854 int max_bandwidth_bps = -1;
855};
856
857struct AudioSendParameters : RtpSendParameters<AudioCodec> {
858 std::string ToString() const override {
859 std::ostringstream ost;
860 ost << "{";
861 ost << "codecs: " << VectorToString(this->codecs) << ", ";
862 ost << "extensions: " << VectorToString(this->extensions) << ", ";
863 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700864 ost << "options: " << options.ToString();
865 ost << "}";
866 return ost.str();
867 }
868
nisse05103312016-03-16 02:22:50 -0700869 AudioOptions options;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700870};
871
872struct AudioRecvParameters : RtpParameters<AudioCodec> {
873};
874
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875class VoiceMediaChannel : public MediaChannel {
876 public:
877 enum Error {
878 ERROR_NONE = 0, // No error.
879 ERROR_OTHER, // Other errors.
880 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
881 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
882 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
883 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
884 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
885 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
886 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
887 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
888 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
889 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
890 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
891 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
892 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
893 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
894 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
895 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
896 };
897
898 VoiceMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800899 VoiceMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 virtual ~VoiceMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200901 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
902 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 // Starts or stops playout of received audio.
904 virtual bool SetPlayout(bool playout) = 0;
905 // Starts or stops sending (and potentially capture) of local audio.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800906 virtual void SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700907 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200908 virtual bool SetAudioSend(uint32_t ssrc,
909 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700910 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800911 AudioSource* source) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 // Gets current energy levels for all incoming streams.
913 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
914 // Get the current energy level of the stream sent to the speaker.
915 virtual int GetOutputLevel() = 0;
916 // Get the time in milliseconds since last recorded keystroke, or negative.
917 virtual int GetTimeSinceLastTyping() = 0;
918 // Temporarily exposed field for tuning typing detect options.
919 virtual void SetTypingDetectionParameters(int time_window,
920 int cost_per_typing, int reporting_threshold, int penalty_decay,
921 int type_event_delay) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700922 // Set speaker output volume of the specified ssrc.
923 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800925 virtual bool CanInsertDtmf() = 0;
926 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000928 // The valid value for the |event| are 0 to 15 which corresponding to
929 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800930 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 // Gets quality stats for the channel.
932 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100933
934 virtual void SetRawAudioSink(
935 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800936 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937};
938
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700939// TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
940// encapsulate all the parameters needed for a video RtpSender.
nisse05103312016-03-16 02:22:50 -0700941struct VideoSendParameters : RtpSendParameters<VideoCodec> {
nisse4b4dc862016-02-17 05:25:36 -0800942 // Use conference mode? This flag comes from the remote
943 // description's SDP line 'a=x-google-flag:conference', copied over
944 // by VideoChannel::SetRemoteContent_w, and ultimately used by
945 // conference mode screencast logic in
946 // WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig.
947 // The special screencast behaviour is disabled by default.
948 bool conference_mode = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700949};
950
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700951// TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
952// encapsulate all the parameters needed for a video RtpReceiver.
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700953struct VideoRecvParameters : RtpParameters<VideoCodec> {
954};
955
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956class VideoMediaChannel : public MediaChannel {
957 public:
958 enum Error {
959 ERROR_NONE = 0, // No error.
960 ERROR_OTHER, // Other errors.
961 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
962 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
963 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
964 ERROR_REC_DEVICE_REMOVED, // Device is removed.
965 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
966 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
967 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
968 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
969 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
970 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
971 };
972
nisse08582ff2016-02-04 01:24:52 -0800973 VideoMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800974 VideoMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 virtual ~VideoMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200976
977 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
978 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
skvladdc1c62c2016-03-16 19:07:43 -0700979 virtual webrtc::RtpParameters GetRtpParameters(uint32_t ssrc) const = 0;
980 virtual bool SetRtpParameters(uint32_t ssrc,
981 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 // Gets the currently set codecs/payload types to be used for outgoing media.
983 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 // Starts or stops transmission (and potentially capture) of local video.
985 virtual bool SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700986 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200987 virtual bool SetVideoSend(uint32_t ssrc,
988 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -0700989 const VideoOptions* options) = 0;
nisse08582ff2016-02-04 01:24:52 -0800990 // Sets the sink object to be used for the specified stream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 // If SSRC is 0, the renderer is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -0800992 virtual bool SetSink(uint32_t ssrc,
993 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 // If |ssrc| is 0, replace the default capturer (engine capturer) with
995 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200996 virtual bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000998 virtual bool GetStats(VideoMediaInfo* info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999};
1000
1001enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001002 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
1003 // values.
1004 DMT_NONE = 0,
1005 DMT_CONTROL = 1,
1006 DMT_BINARY = 2,
1007 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008};
1009
1010// Info about data received in DataMediaChannel. For use in
1011// DataMediaChannel::SignalDataReceived and in all of the signals that
1012// signal fires, on up the chain.
1013struct ReceiveDataParams {
1014 // The in-packet stream indentifier.
1015 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001016 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 // The type of message (binary, text, or control).
1018 DataMessageType type;
1019 // A per-stream value incremented per packet in the stream.
1020 int seq_num;
1021 // A per-stream value monotonically increasing with time.
1022 int timestamp;
1023
1024 ReceiveDataParams() :
1025 ssrc(0),
1026 type(DMT_TEXT),
1027 seq_num(0),
1028 timestamp(0) {
1029 }
1030};
1031
1032struct SendDataParams {
1033 // The in-packet stream indentifier.
1034 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001035 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 // The type of message (binary, text, or control).
1037 DataMessageType type;
1038
1039 // For SCTP, whether to send messages flagged as ordered or not.
1040 // If false, messages can be received out of order.
1041 bool ordered;
1042 // For SCTP, whether the messages are sent reliably or not.
1043 // If false, messages may be lost.
1044 bool reliable;
1045 // For SCTP, if reliable == false, provide partial reliability by
1046 // resending up to this many times. Either count or millis
1047 // is supported, not both at the same time.
1048 int max_rtx_count;
1049 // For SCTP, if reliable == false, provide partial reliability by
1050 // resending for up to this many milliseconds. Either count or millis
1051 // is supported, not both at the same time.
1052 int max_rtx_ms;
1053
1054 SendDataParams() :
1055 ssrc(0),
1056 type(DMT_TEXT),
1057 // TODO(pthatcher): Make these true by default?
1058 ordered(false),
1059 reliable(false),
1060 max_rtx_count(0),
1061 max_rtx_ms(0) {
1062 }
1063};
1064
1065enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
1066
nisse05103312016-03-16 02:22:50 -07001067struct DataSendParameters : RtpSendParameters<DataCodec> {
solenberg7e4e01a2015-12-02 08:05:01 -08001068 std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001069 std::ostringstream ost;
1070 // Options and extensions aren't used.
1071 ost << "{";
1072 ost << "codecs: " << VectorToString(codecs) << ", ";
pbos378dc772016-01-28 15:58:41 -08001073 ost << "max_bandwidth_bps: " << max_bandwidth_bps;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001074 ost << "}";
1075 return ost.str();
1076 }
1077};
1078
1079struct DataRecvParameters : RtpParameters<DataCodec> {
1080};
1081
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082class DataMediaChannel : public MediaChannel {
1083 public:
1084 enum Error {
1085 ERROR_NONE = 0, // No error.
1086 ERROR_OTHER, // Other errors.
1087 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
1088 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1089 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
1090 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1091 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
1092 };
1093
1094 virtual ~DataMediaChannel() {}
1095
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001096 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
1097 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 // TODO(pthatcher): Implement this.
1100 virtual bool GetStats(DataMediaInfo* info) { return true; }
1101
1102 virtual bool SetSend(bool send) = 0;
1103 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104
1105 virtual bool SendData(
1106 const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001107 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 SendDataResult* result = NULL) = 0;
1109 // Signals when data is received (params, data, len)
1110 sigslot::signal3<const ReceiveDataParams&,
1111 const char*,
1112 size_t> SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001113 // Signal when the media channel is ready to send the stream. Arguments are:
1114 // writable(bool)
1115 sigslot::signal1<bool> SignalReadyToSend;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001116 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001117 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118};
1119
1120} // namespace cricket
1121
kjellandera96e2d72016-02-04 23:52:28 -08001122#endif // WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_