blob: badec464cdbbb847a3eb624c1831ffa775a7488a [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/basictypes.h"
19#include "webrtc/base/buffer.h"
20#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Karl Wibergbe579832015-11-10 22:34:18 +010022#include "webrtc/base/optional.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/sigslot.h"
24#include "webrtc/base/socket.h"
25#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/codec.h"
27#include "webrtc/media/base/constants.h"
28#include "webrtc/media/base/streamparams.h"
nisse08582ff2016-02-04 01:24:52 -080029#include "webrtc/media/base/videosinkinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030// TODO(juberti): re-evaluate this include
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010031#include "webrtc/pc/audiomonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034class Buffer;
35class RateLimiter;
36class Timing;
37}
38
Tommif888bb52015-12-12 01:37:01 +010039namespace webrtc {
40class AudioSinkInterface;
41}
42
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043namespace cricket {
44
tommi1d5c19d2015-12-13 22:54:29 -080045class AudioRenderer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046class ScreencastId;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047class VideoCapturer;
nisse08582ff2016-02-04 01:24:52 -080048class VideoFrame;
tommi1d5c19d2015-12-13 22:54:29 -080049struct RtpHeader;
50struct VideoFormat;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52const int kMinRtpHeaderExtensionId = 1;
53const int kMaxRtpHeaderExtensionId = 255;
54const int kScreencastDefaultFps = 5;
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056template <class T>
Karl Wibergbe579832015-11-10 22:34:18 +010057static std::string ToStringIfSet(const char* key, const rtc::Optional<T>& val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 std::string str;
kwiberg102c6a62015-10-30 02:47:38 -070059 if (val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 str = key;
61 str += ": ";
kwiberg102c6a62015-10-30 02:47:38 -070062 str += val ? rtc::ToString(*val) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 str += ", ";
64 }
65 return str;
66}
67
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070068template <class T>
69static std::string VectorToString(const std::vector<T>& vals) {
70 std::ostringstream ost;
71 ost << "[";
72 for (size_t i = 0; i < vals.size(); ++i) {
73 if (i > 0) {
74 ost << ", ";
75 }
76 ost << vals[i].ToString();
77 }
78 ost << "]";
79 return ost.str();
80}
81
nisse51542be2016-02-12 02:27:06 -080082// Construction-time settings, passed to
83// MediaControllerInterface::Create, and passed on when creating
84// MediaChannels.
85struct MediaConfig {
86 // Set DSCP value on packets. This flag comes from the
87 // PeerConnection constraint 'googDscp'.
88 bool enable_dscp = false;
89
90 // Video-specific config
91
92 // Enable WebRTC CPU Overuse Detection. This flag comes from the
93 // PeerConnection constraint 'googCpuOveruseDetection' and is
94 // checked in WebRtcVideoChannel2::OnLoadUpdate, where it's passed
95 // to VideoCapturer::video_adapter()->OnCpuResolutionRequest.
96 bool enable_cpu_overuse_detection = true;
97
98 // Set to true if the renderer has an algorithm of frame selection.
99 // If the value is true, then WebRTC will hand over a frame as soon as
100 // possible without delay, and rendering smoothness is completely the duty
101 // of the renderer;
102 // If the value is false, then WebRTC is responsible to delay frame release
103 // in order to increase rendering smoothness.
104 //
105 // This flag comes from PeerConnection's RtcConfiguration, but is
106 // currently only set by the command line flag
107 // 'disable-rtc-smoothness-algorithm'.
108 // WebRtcVideoChannel2::AddRecvStream copies it to the created
109 // WebRtcVideoReceiveStream, where it is returned by the
110 // SmoothsRenderedFrames method. This method is used by the
111 // VideoReceiveStream, where the value is passed on to the
112 // IncomingVideoStream constructor.
113 bool disable_prerenderer_smoothing = false;
114};
115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116// Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine.
117// Used to be flags, but that makes it hard to selectively apply options.
118// We are moving all of the setting of options to structs like this,
119// but some things currently still use flags.
120struct AudioOptions {
121 void SetAll(const AudioOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700122 SetFrom(&echo_cancellation, change.echo_cancellation);
123 SetFrom(&auto_gain_control, change.auto_gain_control);
124 SetFrom(&noise_suppression, change.noise_suppression);
125 SetFrom(&highpass_filter, change.highpass_filter);
126 SetFrom(&stereo_swapping, change.stereo_swapping);
127 SetFrom(&audio_jitter_buffer_max_packets,
128 change.audio_jitter_buffer_max_packets);
129 SetFrom(&audio_jitter_buffer_fast_accelerate,
130 change.audio_jitter_buffer_fast_accelerate);
131 SetFrom(&typing_detection, change.typing_detection);
132 SetFrom(&aecm_generate_comfort_noise, change.aecm_generate_comfort_noise);
kwiberg102c6a62015-10-30 02:47:38 -0700133 SetFrom(&adjust_agc_delta, change.adjust_agc_delta);
134 SetFrom(&experimental_agc, change.experimental_agc);
135 SetFrom(&extended_filter_aec, change.extended_filter_aec);
136 SetFrom(&delay_agnostic_aec, change.delay_agnostic_aec);
137 SetFrom(&experimental_ns, change.experimental_ns);
kwiberg102c6a62015-10-30 02:47:38 -0700138 SetFrom(&tx_agc_target_dbov, change.tx_agc_target_dbov);
139 SetFrom(&tx_agc_digital_compression_gain,
140 change.tx_agc_digital_compression_gain);
141 SetFrom(&tx_agc_limiter, change.tx_agc_limiter);
142 SetFrom(&recording_sample_rate, change.recording_sample_rate);
143 SetFrom(&playout_sample_rate, change.playout_sample_rate);
kwiberg102c6a62015-10-30 02:47:38 -0700144 SetFrom(&combined_audio_video_bwe, change.combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 }
146
147 bool operator==(const AudioOptions& o) const {
148 return echo_cancellation == o.echo_cancellation &&
149 auto_gain_control == o.auto_gain_control &&
150 noise_suppression == o.noise_suppression &&
151 highpass_filter == o.highpass_filter &&
152 stereo_swapping == o.stereo_swapping &&
Henrik Lundin64dad832015-05-11 12:44:23 +0200153 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200154 audio_jitter_buffer_fast_accelerate ==
155 o.audio_jitter_buffer_fast_accelerate &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 typing_detection == o.typing_detection &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000157 aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 experimental_agc == o.experimental_agc &&
Henrik Lundin441f6342015-06-09 16:03:13 +0200159 extended_filter_aec == o.extended_filter_aec &&
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100160 delay_agnostic_aec == o.delay_agnostic_aec &&
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000161 experimental_ns == o.experimental_ns &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 adjust_agc_delta == o.adjust_agc_delta &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000163 tx_agc_target_dbov == o.tx_agc_target_dbov &&
164 tx_agc_digital_compression_gain == o.tx_agc_digital_compression_gain &&
165 tx_agc_limiter == o.tx_agc_limiter &&
wu@webrtc.org97077a32013-10-25 21:18:33 +0000166 recording_sample_rate == o.recording_sample_rate &&
wu@webrtc.orgde305012013-10-31 15:40:38 +0000167 playout_sample_rate == o.playout_sample_rate &&
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000168 combined_audio_video_bwe == o.combined_audio_video_bwe;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 }
170
171 std::string ToString() const {
172 std::ostringstream ost;
173 ost << "AudioOptions {";
174 ost << ToStringIfSet("aec", echo_cancellation);
175 ost << ToStringIfSet("agc", auto_gain_control);
176 ost << ToStringIfSet("ns", noise_suppression);
177 ost << ToStringIfSet("hf", highpass_filter);
178 ost << ToStringIfSet("swap", stereo_swapping);
Henrik Lundin64dad832015-05-11 12:44:23 +0200179 ost << ToStringIfSet("audio_jitter_buffer_max_packets",
180 audio_jitter_buffer_max_packets);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200181 ost << ToStringIfSet("audio_jitter_buffer_fast_accelerate",
182 audio_jitter_buffer_fast_accelerate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 ost << ToStringIfSet("typing", typing_detection);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000184 ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 ost << ToStringIfSet("agc_delta", adjust_agc_delta);
186 ost << ToStringIfSet("experimental_agc", experimental_agc);
Henrik Lundin441f6342015-06-09 16:03:13 +0200187 ost << ToStringIfSet("extended_filter_aec", extended_filter_aec);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100188 ost << ToStringIfSet("delay_agnostic_aec", delay_agnostic_aec);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000189 ost << ToStringIfSet("experimental_ns", experimental_ns);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000190 ost << ToStringIfSet("tx_agc_target_dbov", tx_agc_target_dbov);
191 ost << ToStringIfSet("tx_agc_digital_compression_gain",
192 tx_agc_digital_compression_gain);
193 ost << ToStringIfSet("tx_agc_limiter", tx_agc_limiter);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000194 ost << ToStringIfSet("recording_sample_rate", recording_sample_rate);
195 ost << ToStringIfSet("playout_sample_rate", playout_sample_rate);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000196 ost << ToStringIfSet("combined_audio_video_bwe", combined_audio_video_bwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 ost << "}";
198 return ost.str();
199 }
200
201 // Audio processing that attempts to filter away the output signal from
202 // later inbound pickup.
Karl Wibergbe579832015-11-10 22:34:18 +0100203 rtc::Optional<bool> echo_cancellation;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 // Audio processing to adjust the sensitivity of the local mic dynamically.
Karl Wibergbe579832015-11-10 22:34:18 +0100205 rtc::Optional<bool> auto_gain_control;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 // Audio processing to filter out background noise.
Karl Wibergbe579832015-11-10 22:34:18 +0100207 rtc::Optional<bool> noise_suppression;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 // Audio processing to remove background noise of lower frequencies.
Karl Wibergbe579832015-11-10 22:34:18 +0100209 rtc::Optional<bool> highpass_filter;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 // Audio processing to swap the left and right channels.
Karl Wibergbe579832015-11-10 22:34:18 +0100211 rtc::Optional<bool> stereo_swapping;
Henrik Lundin64dad832015-05-11 12:44:23 +0200212 // Audio receiver jitter buffer (NetEq) max capacity in number of packets.
Karl Wibergbe579832015-11-10 22:34:18 +0100213 rtc::Optional<int> audio_jitter_buffer_max_packets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200214 // Audio receiver jitter buffer (NetEq) fast accelerate mode.
Karl Wibergbe579832015-11-10 22:34:18 +0100215 rtc::Optional<bool> audio_jitter_buffer_fast_accelerate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 // Audio processing to detect typing.
Karl Wibergbe579832015-11-10 22:34:18 +0100217 rtc::Optional<bool> typing_detection;
218 rtc::Optional<bool> aecm_generate_comfort_noise;
Karl Wibergbe579832015-11-10 22:34:18 +0100219 rtc::Optional<int> adjust_agc_delta;
220 rtc::Optional<bool> experimental_agc;
221 rtc::Optional<bool> extended_filter_aec;
222 rtc::Optional<bool> delay_agnostic_aec;
223 rtc::Optional<bool> experimental_ns;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000224 // Note that tx_agc_* only applies to non-experimental AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100225 rtc::Optional<uint16_t> tx_agc_target_dbov;
226 rtc::Optional<uint16_t> tx_agc_digital_compression_gain;
227 rtc::Optional<bool> tx_agc_limiter;
228 rtc::Optional<uint32_t> recording_sample_rate;
229 rtc::Optional<uint32_t> playout_sample_rate;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000230 // Enable combined audio+bandwidth BWE.
nisse51542be2016-02-12 02:27:06 -0800231 // TODO(pthatcher): This flag is set from the
232 // "googCombinedAudioVideoBwe", but not used anywhere. So delete it,
233 // and check if any other AudioOptions members are unused.
Karl Wibergbe579832015-11-10 22:34:18 +0100234 rtc::Optional<bool> combined_audio_video_bwe;
kwiberg102c6a62015-10-30 02:47:38 -0700235
236 private:
237 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100238 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700239 if (o) {
240 *s = o;
241 }
242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243};
244
245// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
246// Used to be flags, but that makes it hard to selectively apply options.
247// We are moving all of the setting of options to structs like this,
248// but some things currently still use flags.
249struct VideoOptions {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 void SetAll(const VideoOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700251 SetFrom(&video_noise_reduction, change.video_noise_reduction);
kwiberg102c6a62015-10-30 02:47:38 -0700252 SetFrom(&suspend_below_min_bitrate, change.suspend_below_min_bitrate);
nisseb163c3f2016-01-29 01:14:38 -0800253 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 }
255
256 bool operator==(const VideoOptions& o) const {
nisseb163c3f2016-01-29 01:14:38 -0800257 return video_noise_reduction == o.video_noise_reduction &&
pbos@webrtc.org43336b62014-10-14 19:12:06 +0000258 suspend_below_min_bitrate == o.suspend_below_min_bitrate &&
nisse51542be2016-02-12 02:27:06 -0800259 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 }
261
262 std::string ToString() const {
263 std::ostringstream ost;
264 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 ost << ToStringIfSet("noise reduction", video_noise_reduction);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000266 ost << ToStringIfSet("suspend below min bitrate",
267 suspend_below_min_bitrate);
nisseb163c3f2016-01-29 01:14:38 -0800268 ost << ToStringIfSet("screencast min bitrate kbps",
269 screencast_min_bitrate_kbps);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 ost << "}";
271 return ost.str();
272 }
273
nisseb163c3f2016-01-29 01:14:38 -0800274 // Enable denoising? This flag comes from the getUserMedia
275 // constraint 'googNoiseReduction', and WebRtcVideoEngine2 passes it
276 // on to the codec options. Disabled by default.
Karl Wibergbe579832015-11-10 22:34:18 +0100277 rtc::Optional<bool> video_noise_reduction;
nisseb163c3f2016-01-29 01:14:38 -0800278 // Enable WebRTC suspension of video. No video frames will be sent
279 // when the bitrate is below the configured minimum bitrate. This
280 // flag comes from the PeerConnection constraint
281 // 'googSuspendBelowMinBitrate', and WebRtcVideoChannel2 copies it
282 // to VideoSendStream::Config::suspend_below_min_bitrate.
Karl Wibergbe579832015-11-10 22:34:18 +0100283 rtc::Optional<bool> suspend_below_min_bitrate;
nisseb163c3f2016-01-29 01:14:38 -0800284 // Force screencast to use a minimum bitrate. This flag comes from
285 // the PeerConnection constraint 'googScreencastMinBitrate'. It is
286 // copied to the encoder config by WebRtcVideoChannel2.
287 rtc::Optional<int> screencast_min_bitrate_kbps;
kwiberg102c6a62015-10-30 02:47:38 -0700288
289 private:
290 template <typename T>
Karl Wibergbe579832015-11-10 22:34:18 +0100291 static void SetFrom(rtc::Optional<T>* s, const rtc::Optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700292 if (o) {
293 *s = o;
294 }
295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296};
297
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298struct RtpHeaderExtension {
299 RtpHeaderExtension() : id(0) {}
300 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301
302 bool operator==(const RtpHeaderExtension& ext) const {
303 // id is a reserved word in objective-c. Therefore the id attribute has to
304 // be a fully qualified name in order to compile on IOS.
305 return this->id == ext.id &&
306 uri == ext.uri;
307 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700308
309 std::string ToString() const {
310 std::ostringstream ost;
311 ost << "{";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700312 ost << "uri: " << uri;
solenberg7e4e01a2015-12-02 08:05:01 -0800313 ost << ", id: " << id;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700314 ost << "}";
315 return ost.str();
316 }
317
318 std::string uri;
319 int id;
320 // TODO(juberti): SendRecv direction;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321};
322
323// Returns the named header extension if found among all extensions, NULL
324// otherwise.
325inline const RtpHeaderExtension* FindHeaderExtension(
326 const std::vector<RtpHeaderExtension>& extensions,
327 const std::string& name) {
328 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
329 it != extensions.end(); ++it) {
330 if (it->uri == name)
331 return &(*it);
332 }
333 return NULL;
334}
335
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336class MediaChannel : public sigslot::has_slots<> {
337 public:
338 class NetworkInterface {
339 public:
340 enum SocketType { ST_RTP, ST_RTCP };
stefanc1aeaf02015-10-15 07:26:07 -0700341 virtual bool SendPacket(rtc::Buffer* packet,
342 const rtc::PacketOptions& options) = 0;
343 virtual bool SendRtcp(rtc::Buffer* packet,
344 const rtc::PacketOptions& options) = 0;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000345 virtual int SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 int option) = 0;
347 virtual ~NetworkInterface() {}
348 };
349
nisse51542be2016-02-12 02:27:06 -0800350 MediaChannel(const MediaConfig& config)
351 : enable_dscp_(config.enable_dscp), network_interface_(NULL) {}
352 MediaChannel() : enable_dscp_(false), network_interface_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 virtual ~MediaChannel() {}
354
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000355 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 virtual void SetInterface(NetworkInterface *iface) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000357 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 network_interface_ = iface;
nisse51542be2016-02-12 02:27:06 -0800359 SetDscp(enable_dscp_ ? PreferredDscp() : rtc::DSCP_DEFAULT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 }
nisse51542be2016-02-12 02:27:06 -0800361 virtual rtc::DiffServCodePoint PreferredDscp() const {
362 return rtc::DSCP_DEFAULT;
363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 // Called when a RTP packet is received.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 virtual void OnPacketReceived(rtc::Buffer* packet,
366 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 // Called when a RTCP packet is received.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000368 virtual void OnRtcpReceived(rtc::Buffer* packet,
369 const rtc::PacketTime& packet_time) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 // Called when the socket's ability to send has changed.
371 virtual void OnReadyToSend(bool ready) = 0;
372 // Creates a new outgoing media stream with SSRCs and CNAME as described
373 // by sp.
374 virtual bool AddSendStream(const StreamParams& sp) = 0;
375 // Removes an outgoing media stream.
376 // ssrc must be the first SSRC of the media stream if the stream uses
377 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200378 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 // Creates a new incoming media stream with SSRCs and CNAME as described
380 // by sp.
381 virtual bool AddRecvStream(const StreamParams& sp) = 0;
382 // Removes an incoming media stream.
383 // ssrc must be the first SSRC of the media stream if the stream uses
384 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000387 // Returns the absoulte sendtime extension id value from media channel.
388 virtual int GetRtpSendTimeExtnId() const {
389 return -1;
390 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000392 // Base method to send packet using NetworkInterface.
stefanc1aeaf02015-10-15 07:26:07 -0700393 bool SendPacket(rtc::Buffer* packet, const rtc::PacketOptions& options) {
394 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000395 }
396
stefanc1aeaf02015-10-15 07:26:07 -0700397 bool SendRtcp(rtc::Buffer* packet, const rtc::PacketOptions& options) {
398 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000399 }
400
401 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000402 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000403 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000404 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000405 if (!network_interface_)
406 return -1;
407
408 return network_interface_->SetOption(type, opt, option);
409 }
410
nisse51542be2016-02-12 02:27:06 -0800411 private:
wu@webrtc.orgde305012013-10-31 15:40:38 +0000412 // This method sets DSCP |value| on both RTP and RTCP channels.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000413 int SetDscp(rtc::DiffServCodePoint value) {
wu@webrtc.orgde305012013-10-31 15:40:38 +0000414 int ret;
415 ret = SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000416 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000417 value);
418 if (ret == 0) {
419 ret = SetOption(NetworkInterface::ST_RTCP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000420 rtc::Socket::OPT_DSCP,
wu@webrtc.orgde305012013-10-31 15:40:38 +0000421 value);
422 }
423 return ret;
424 }
425
stefanc1aeaf02015-10-15 07:26:07 -0700426 bool DoSendPacket(rtc::Buffer* packet,
427 bool rtcp,
428 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000429 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000430 if (!network_interface_)
431 return false;
432
stefanc1aeaf02015-10-15 07:26:07 -0700433 return (!rtcp) ? network_interface_->SendPacket(packet, options)
434 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000435 }
436
nisse51542be2016-02-12 02:27:06 -0800437 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000438 // |network_interface_| can be accessed from the worker_thread and
439 // from any MediaEngine threads. This critical section is to protect accessing
440 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000441 rtc::CriticalSection network_interface_crit_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000442 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443};
444
445enum SendFlags {
446 SEND_NOTHING,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 SEND_MICROPHONE
448};
449
wu@webrtc.org97077a32013-10-25 21:18:33 +0000450// The stats information is structured as follows:
451// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
452// Media contains a vector of SSRC infos that are exclusively used by this
453// media. (SSRCs shared between media streams can't be represented.)
454
455// Information about an SSRC.
456// This data may be locally recorded, or received in an RTCP SR or RR.
457struct SsrcSenderInfo {
458 SsrcSenderInfo()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 : ssrc(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000460 timestamp(0) {
461 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000463 double timestamp; // NTP timestamp, represented as seconds since epoch.
464};
465
466struct SsrcReceiverInfo {
467 SsrcReceiverInfo()
468 : ssrc(0),
469 timestamp(0) {
470 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200471 uint32_t ssrc;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000472 double timestamp;
473};
474
475struct MediaSenderInfo {
476 MediaSenderInfo()
477 : bytes_sent(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 packets_sent(0),
479 packets_lost(0),
480 fraction_lost(0.0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000481 rtt_ms(0) {
482 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000483 void add_ssrc(const SsrcSenderInfo& stat) {
484 local_stats.push_back(stat);
485 }
486 // Temporary utility function for call sites that only provide SSRC.
487 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200488 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000489 SsrcSenderInfo stat;
490 stat.ssrc = ssrc;
491 add_ssrc(stat);
492 }
493 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200494 std::vector<uint32_t> ssrcs() const {
495 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000496 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
497 it != local_stats.end(); ++it) {
498 retval.push_back(it->ssrc);
499 }
500 return retval;
501 }
502 // Utility accessor for clients that make the assumption only one ssrc
503 // exists per media.
504 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200505 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000506 if (local_stats.size() > 0) {
507 return local_stats[0].ssrc;
508 } else {
509 return 0;
510 }
511 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200512 int64_t bytes_sent;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000513 int packets_sent;
514 int packets_lost;
515 float fraction_lost;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000516 int64_t rtt_ms;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000517 std::string codec_name;
518 std::vector<SsrcSenderInfo> local_stats;
519 std::vector<SsrcReceiverInfo> remote_stats;
520};
521
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000522template<class T>
523struct VariableInfo {
524 VariableInfo()
525 : min_val(),
526 mean(0.0),
527 max_val(),
528 variance(0.0) {
529 }
530 T min_val;
531 double mean;
532 T max_val;
533 double variance;
534};
535
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536struct MediaReceiverInfo {
537 MediaReceiverInfo()
538 : bytes_rcvd(0),
539 packets_rcvd(0),
540 packets_lost(0),
541 fraction_lost(0.0) {
542 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000543 void add_ssrc(const SsrcReceiverInfo& stat) {
544 local_stats.push_back(stat);
545 }
546 // Temporary utility function for call sites that only provide SSRC.
547 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200548 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000549 SsrcReceiverInfo stat;
550 stat.ssrc = ssrc;
551 add_ssrc(stat);
552 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200553 std::vector<uint32_t> ssrcs() const {
554 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000555 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
556 it != local_stats.end(); ++it) {
557 retval.push_back(it->ssrc);
558 }
559 return retval;
560 }
561 // Utility accessor for clients that make the assumption only one ssrc
562 // exists per media.
563 // This will eventually go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200564 uint32_t ssrc() const {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000565 if (local_stats.size() > 0) {
566 return local_stats[0].ssrc;
567 } else {
568 return 0;
569 }
570 }
571
Peter Boström0c4e06b2015-10-07 12:23:21 +0200572 int64_t bytes_rcvd;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000573 int packets_rcvd;
574 int packets_lost;
575 float fraction_lost;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000576 std::string codec_name;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000577 std::vector<SsrcReceiverInfo> local_stats;
578 std::vector<SsrcSenderInfo> remote_stats;
579};
580
581struct VoiceSenderInfo : public MediaSenderInfo {
582 VoiceSenderInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000583 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 jitter_ms(0),
585 audio_level(0),
586 aec_quality_min(0.0),
587 echo_delay_median_ms(0),
588 echo_delay_std_ms(0),
589 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000590 echo_return_loss_enhancement(0),
591 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 }
593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 int ext_seqnum;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 int jitter_ms;
596 int audio_level;
597 float aec_quality_min;
598 int echo_delay_median_ms;
599 int echo_delay_std_ms;
600 int echo_return_loss;
601 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000602 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603};
604
wu@webrtc.org97077a32013-10-25 21:18:33 +0000605struct VoiceReceiverInfo : public MediaReceiverInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 VoiceReceiverInfo()
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000607 : ext_seqnum(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 jitter_ms(0),
609 jitter_buffer_ms(0),
610 jitter_buffer_preferred_ms(0),
611 delay_estimate_ms(0),
612 audio_level(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000613 expand_rate(0),
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000614 speech_expand_rate(0),
615 secondary_decoded_rate(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200616 accelerate_rate(0),
617 preemptive_expand_rate(0),
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000618 decoding_calls_to_silence_generator(0),
619 decoding_calls_to_neteq(0),
620 decoding_normal(0),
621 decoding_plc(0),
622 decoding_cng(0),
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000623 decoding_plc_cng(0),
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200624 capture_start_ntp_time_ms(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 int ext_seqnum;
627 int jitter_ms;
628 int jitter_buffer_ms;
629 int jitter_buffer_preferred_ms;
630 int delay_estimate_ms;
631 int audio_level;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000632 // fraction of synthesized audio inserted through expansion.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 float expand_rate;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000634 // fraction of synthesized speech inserted through expansion.
635 float speech_expand_rate;
636 // fraction of data out of secondary decoding, including FEC and RED.
637 float secondary_decoded_rate;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200638 // Fraction of data removed through time compression.
639 float accelerate_rate;
640 // Fraction of data inserted through time stretching.
641 float preemptive_expand_rate;
henrike@webrtc.orgb8c254a2014-02-14 23:38:45 +0000642 int decoding_calls_to_silence_generator;
643 int decoding_calls_to_neteq;
644 int decoding_normal;
645 int decoding_plc;
646 int decoding_cng;
647 int decoding_plc_cng;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000648 // Estimated capture start time in NTP time in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200649 int64_t capture_start_ntp_time_ms;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650};
651
wu@webrtc.org97077a32013-10-25 21:18:33 +0000652struct VideoSenderInfo : public MediaSenderInfo {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 VideoSenderInfo()
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000654 : packets_cached(0),
655 firs_rcvd(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000656 plis_rcvd(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 nacks_rcvd(0),
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000658 input_frame_width(0),
659 input_frame_height(0),
660 send_frame_width(0),
661 send_frame_height(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 framerate_input(0),
663 framerate_sent(0),
664 nominal_bitrate(0),
665 preferred_bitrate(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000666 adapt_reason(0),
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000667 adapt_changes(0),
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000668 avg_encode_ms(0),
Peter Boström8ed6a4b2015-03-27 10:01:02 +0100669 encode_usage_percent(0) {
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000670 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000672 std::vector<SsrcGroup> ssrc_groups;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100673 std::string encoder_implementation_name;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000674 int packets_cached;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 int firs_rcvd;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000676 int plis_rcvd;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 int nacks_rcvd;
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000678 int input_frame_width;
679 int input_frame_height;
680 int send_frame_width;
681 int send_frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 int framerate_input;
683 int framerate_sent;
684 int nominal_bitrate;
685 int preferred_bitrate;
686 int adapt_reason;
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000687 int adapt_changes;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000688 int avg_encode_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000689 int encode_usage_percent;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000690 VariableInfo<int> adapt_frame_drops;
691 VariableInfo<int> effects_frame_drops;
692 VariableInfo<double> capturer_frame_time;
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
844template <class Codec, class Options>
845struct RtpSendParameters : RtpParameters<Codec> {
solenberg7e4e01a2015-12-02 08:05:01 -0800846 std::string ToString() const override {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700847 std::ostringstream ost;
848 ost << "{";
849 ost << "codecs: " << VectorToString(this->codecs) << ", ";
850 ost << "extensions: " << VectorToString(this->extensions) << ", ";
pbos378dc772016-01-28 15:58:41 -0800851 ost << "max_bandwidth_bps: " << max_bandwidth_bps << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700852 ost << "options: " << options.ToString();
853 ost << "}";
854 return ost.str();
855 }
856
857 int max_bandwidth_bps = -1;
858 Options options;
859};
860
861struct AudioSendParameters : RtpSendParameters<AudioCodec, AudioOptions> {
862};
863
864struct AudioRecvParameters : RtpParameters<AudioCodec> {
865};
866
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867class VoiceMediaChannel : public MediaChannel {
868 public:
869 enum Error {
870 ERROR_NONE = 0, // No error.
871 ERROR_OTHER, // Other errors.
872 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
873 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
874 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
875 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
876 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
877 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
878 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
879 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
880 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
881 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
882 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
883 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
884 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
885 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
886 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
887 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
888 };
889
890 VoiceMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800891 VoiceMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 virtual ~VoiceMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200893 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
894 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 // Starts or stops playout of received audio.
896 virtual bool SetPlayout(bool playout) = 0;
897 // Starts or stops sending (and potentially capture) of local audio.
898 virtual bool SetSend(SendFlags flag) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700899 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200900 virtual bool SetAudioSend(uint32_t ssrc,
901 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700902 const AudioOptions* options,
solenberg1dd98f32015-09-10 01:57:14 -0700903 AudioRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 // Gets current energy levels for all incoming streams.
905 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
906 // Get the current energy level of the stream sent to the speaker.
907 virtual int GetOutputLevel() = 0;
908 // Get the time in milliseconds since last recorded keystroke, or negative.
909 virtual int GetTimeSinceLastTyping() = 0;
910 // Temporarily exposed field for tuning typing detect options.
911 virtual void SetTypingDetectionParameters(int time_window,
912 int cost_per_typing, int reporting_threshold, int penalty_decay,
913 int type_event_delay) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700914 // Set speaker output volume of the specified ssrc.
915 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800917 virtual bool CanInsertDtmf() = 0;
918 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000920 // The valid value for the |event| are 0 to 15 which corresponding to
921 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800922 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 // Gets quality stats for the channel.
924 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100925
926 virtual void SetRawAudioSink(
927 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800928 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929};
930
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700931struct VideoSendParameters : RtpSendParameters<VideoCodec, VideoOptions> {
nisse4b4dc862016-02-17 05:25:36 -0800932 // Use conference mode? This flag comes from the remote
933 // description's SDP line 'a=x-google-flag:conference', copied over
934 // by VideoChannel::SetRemoteContent_w, and ultimately used by
935 // conference mode screencast logic in
936 // WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig.
937 // The special screencast behaviour is disabled by default.
938 bool conference_mode = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700939};
940
941struct VideoRecvParameters : RtpParameters<VideoCodec> {
942};
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944class VideoMediaChannel : public MediaChannel {
945 public:
946 enum Error {
947 ERROR_NONE = 0, // No error.
948 ERROR_OTHER, // Other errors.
949 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
950 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
951 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
952 ERROR_REC_DEVICE_REMOVED, // Device is removed.
953 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
954 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
955 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
956 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
957 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
958 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
959 };
960
nisse08582ff2016-02-04 01:24:52 -0800961 VideoMediaChannel() {}
nisse51542be2016-02-12 02:27:06 -0800962 VideoMediaChannel(const MediaConfig& config) : MediaChannel(config) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 virtual ~VideoMediaChannel() {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200964
965 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
966 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 // Gets the currently set codecs/payload types to be used for outgoing media.
968 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 // Starts or stops transmission (and potentially capture) of local video.
970 virtual bool SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700971 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200972 virtual bool SetVideoSend(uint32_t ssrc,
973 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -0700974 const VideoOptions* options) = 0;
nisse08582ff2016-02-04 01:24:52 -0800975 // Sets the sink object to be used for the specified stream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 // If SSRC is 0, the renderer is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -0800977 virtual bool SetSink(uint32_t ssrc,
978 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 // If |ssrc| is 0, replace the default capturer (engine capturer) with
980 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200981 virtual bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000983 virtual bool GetStats(VideoMediaInfo* info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984};
985
986enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000987 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
988 // values.
989 DMT_NONE = 0,
990 DMT_CONTROL = 1,
991 DMT_BINARY = 2,
992 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993};
994
995// Info about data received in DataMediaChannel. For use in
996// DataMediaChannel::SignalDataReceived and in all of the signals that
997// signal fires, on up the chain.
998struct ReceiveDataParams {
999 // The in-packet stream indentifier.
1000 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001001 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 // The type of message (binary, text, or control).
1003 DataMessageType type;
1004 // A per-stream value incremented per packet in the stream.
1005 int seq_num;
1006 // A per-stream value monotonically increasing with time.
1007 int timestamp;
1008
1009 ReceiveDataParams() :
1010 ssrc(0),
1011 type(DMT_TEXT),
1012 seq_num(0),
1013 timestamp(0) {
1014 }
1015};
1016
1017struct SendDataParams {
1018 // The in-packet stream indentifier.
1019 // For SCTP, this is really SID, not SSRC.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001020 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 // The type of message (binary, text, or control).
1022 DataMessageType type;
1023
1024 // For SCTP, whether to send messages flagged as ordered or not.
1025 // If false, messages can be received out of order.
1026 bool ordered;
1027 // For SCTP, whether the messages are sent reliably or not.
1028 // If false, messages may be lost.
1029 bool reliable;
1030 // For SCTP, if reliable == false, provide partial reliability by
1031 // resending up to this many times. Either count or millis
1032 // is supported, not both at the same time.
1033 int max_rtx_count;
1034 // For SCTP, if reliable == false, provide partial reliability by
1035 // resending for up to this many milliseconds. Either count or millis
1036 // is supported, not both at the same time.
1037 int max_rtx_ms;
1038
1039 SendDataParams() :
1040 ssrc(0),
1041 type(DMT_TEXT),
1042 // TODO(pthatcher): Make these true by default?
1043 ordered(false),
1044 reliable(false),
1045 max_rtx_count(0),
1046 max_rtx_ms(0) {
1047 }
1048};
1049
1050enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
1051
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001052struct DataOptions {
solenberg7e4e01a2015-12-02 08:05:01 -08001053 std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001054 return "{}";
1055 }
1056};
1057
1058struct DataSendParameters : RtpSendParameters<DataCodec, DataOptions> {
solenberg7e4e01a2015-12-02 08:05:01 -08001059 std::string ToString() const {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001060 std::ostringstream ost;
1061 // Options and extensions aren't used.
1062 ost << "{";
1063 ost << "codecs: " << VectorToString(codecs) << ", ";
pbos378dc772016-01-28 15:58:41 -08001064 ost << "max_bandwidth_bps: " << max_bandwidth_bps;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001065 ost << "}";
1066 return ost.str();
1067 }
1068};
1069
1070struct DataRecvParameters : RtpParameters<DataCodec> {
1071};
1072
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073class DataMediaChannel : public MediaChannel {
1074 public:
1075 enum Error {
1076 ERROR_NONE = 0, // No error.
1077 ERROR_OTHER, // Other errors.
1078 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
1079 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1080 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
1081 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
1082 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
1083 };
1084
1085 virtual ~DataMediaChannel() {}
1086
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001087 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
1088 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 // TODO(pthatcher): Implement this.
1091 virtual bool GetStats(DataMediaInfo* info) { return true; }
1092
1093 virtual bool SetSend(bool send) = 0;
1094 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095
1096 virtual bool SendData(
1097 const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001098 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 SendDataResult* result = NULL) = 0;
1100 // Signals when data is received (params, data, len)
1101 sigslot::signal3<const ReceiveDataParams&,
1102 const char*,
1103 size_t> SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001104 // Signal when the media channel is ready to send the stream. Arguments are:
1105 // writable(bool)
1106 sigslot::signal1<bool> SignalReadyToSend;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001107 // Signal for notifying that the remote side has closed the DataChannel.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001108 sigslot::signal1<uint32_t> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109};
1110
1111} // namespace cricket
1112
kjellandera96e2d72016-02-04 23:52:28 -08001113#endif // WEBRTC_MEDIA_BASE_MEDIACHANNEL_H_