blob: 127b2ed98b44c489edcccabcc5e5762c651afe24 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_BASE_MEDIACHANNEL_H_
29#define TALK_MEDIA_BASE_MEDIACHANNEL_H_
30
31#include <string>
32#include <vector>
33
34#include "talk/base/basictypes.h"
35#include "talk/base/buffer.h"
36#include "talk/base/logging.h"
37#include "talk/base/sigslot.h"
38#include "talk/base/socket.h"
39#include "talk/base/window.h"
40#include "talk/media/base/codec.h"
41#include "talk/media/base/constants.h"
42#include "talk/media/base/streamparams.h"
43// TODO(juberti): re-evaluate this include
44#include "talk/session/media/audiomonitor.h"
45
46namespace talk_base {
47class Buffer;
48class RateLimiter;
49class Timing;
50}
51
52namespace cricket {
53
54class AudioRenderer;
55struct RtpHeader;
56class ScreencastId;
57struct VideoFormat;
58class VideoCapturer;
59class VideoRenderer;
60
61const int kMinRtpHeaderExtensionId = 1;
62const int kMaxRtpHeaderExtensionId = 255;
63const int kScreencastDefaultFps = 5;
64
65// Used in AudioOptions and VideoOptions to signify "unset" values.
66template <class T>
67class Settable {
68 public:
69 Settable() : set_(false), val_() {}
70 explicit Settable(T val) : set_(true), val_(val) {}
71
72 bool IsSet() const {
73 return set_;
74 }
75
76 bool Get(T* out) const {
77 *out = val_;
78 return set_;
79 }
80
81 T GetWithDefaultIfUnset(const T& default_value) const {
82 return set_ ? val_ : default_value;
83 }
84
85 virtual void Set(T val) {
86 set_ = true;
87 val_ = val;
88 }
89
90 void Clear() {
91 Set(T());
92 set_ = false;
93 }
94
95 void SetFrom(const Settable<T>& o) {
96 // Set this value based on the value of o, iff o is set. If this value is
97 // set and o is unset, the current value will be unchanged.
98 T val;
99 if (o.Get(&val)) {
100 Set(val);
101 }
102 }
103
104 std::string ToString() const {
105 return set_ ? talk_base::ToString(val_) : "";
106 }
107
108 bool operator==(const Settable<T>& o) const {
109 // Equal if both are unset with any value or both set with the same value.
110 return (set_ == o.set_) && (!set_ || (val_ == o.val_));
111 }
112
113 bool operator!=(const Settable<T>& o) const {
114 return !operator==(o);
115 }
116
117 protected:
118 void InitializeValue(const T &val) {
119 val_ = val;
120 }
121
122 private:
123 bool set_;
124 T val_;
125};
126
127class SettablePercent : public Settable<float> {
128 public:
129 virtual void Set(float val) {
130 if (val < 0) {
131 val = 0;
132 }
133 if (val > 1.0) {
134 val = 1.0;
135 }
136 Settable<float>::Set(val);
137 }
138};
139
140template <class T>
141static std::string ToStringIfSet(const char* key, const Settable<T>& val) {
142 std::string str;
143 if (val.IsSet()) {
144 str = key;
145 str += ": ";
146 str += val.ToString();
147 str += ", ";
148 }
149 return str;
150}
151
152// Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine.
153// Used to be flags, but that makes it hard to selectively apply options.
154// We are moving all of the setting of options to structs like this,
155// but some things currently still use flags.
156struct AudioOptions {
157 void SetAll(const AudioOptions& change) {
158 echo_cancellation.SetFrom(change.echo_cancellation);
159 auto_gain_control.SetFrom(change.auto_gain_control);
160 noise_suppression.SetFrom(change.noise_suppression);
161 highpass_filter.SetFrom(change.highpass_filter);
162 stereo_swapping.SetFrom(change.stereo_swapping);
163 typing_detection.SetFrom(change.typing_detection);
164 conference_mode.SetFrom(change.conference_mode);
165 adjust_agc_delta.SetFrom(change.adjust_agc_delta);
166 experimental_agc.SetFrom(change.experimental_agc);
167 experimental_aec.SetFrom(change.experimental_aec);
168 aec_dump.SetFrom(change.aec_dump);
169 }
170
171 bool operator==(const AudioOptions& o) const {
172 return echo_cancellation == o.echo_cancellation &&
173 auto_gain_control == o.auto_gain_control &&
174 noise_suppression == o.noise_suppression &&
175 highpass_filter == o.highpass_filter &&
176 stereo_swapping == o.stereo_swapping &&
177 typing_detection == o.typing_detection &&
178 conference_mode == o.conference_mode &&
179 experimental_agc == o.experimental_agc &&
180 experimental_aec == o.experimental_aec &&
181 adjust_agc_delta == o.adjust_agc_delta &&
182 aec_dump == o.aec_dump;
183 }
184
185 std::string ToString() const {
186 std::ostringstream ost;
187 ost << "AudioOptions {";
188 ost << ToStringIfSet("aec", echo_cancellation);
189 ost << ToStringIfSet("agc", auto_gain_control);
190 ost << ToStringIfSet("ns", noise_suppression);
191 ost << ToStringIfSet("hf", highpass_filter);
192 ost << ToStringIfSet("swap", stereo_swapping);
193 ost << ToStringIfSet("typing", typing_detection);
194 ost << ToStringIfSet("conference", conference_mode);
195 ost << ToStringIfSet("agc_delta", adjust_agc_delta);
196 ost << ToStringIfSet("experimental_agc", experimental_agc);
197 ost << ToStringIfSet("experimental_aec", experimental_aec);
198 ost << ToStringIfSet("aec_dump", aec_dump);
199 ost << "}";
200 return ost.str();
201 }
202
203 // Audio processing that attempts to filter away the output signal from
204 // later inbound pickup.
205 Settable<bool> echo_cancellation;
206 // Audio processing to adjust the sensitivity of the local mic dynamically.
207 Settable<bool> auto_gain_control;
208 // Audio processing to filter out background noise.
209 Settable<bool> noise_suppression;
210 // Audio processing to remove background noise of lower frequencies.
211 Settable<bool> highpass_filter;
212 // Audio processing to swap the left and right channels.
213 Settable<bool> stereo_swapping;
214 // Audio processing to detect typing.
215 Settable<bool> typing_detection;
216 Settable<bool> conference_mode;
217 Settable<int> adjust_agc_delta;
218 Settable<bool> experimental_agc;
219 Settable<bool> experimental_aec;
220 Settable<bool> aec_dump;
221};
222
223// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
224// Used to be flags, but that makes it hard to selectively apply options.
225// We are moving all of the setting of options to structs like this,
226// but some things currently still use flags.
227struct VideoOptions {
228 VideoOptions() {
229 process_adaptation_threshhold.Set(kProcessCpuThreshold);
230 system_low_adaptation_threshhold.Set(kLowSystemCpuThreshold);
231 system_high_adaptation_threshhold.Set(kHighSystemCpuThreshold);
232 }
233
234 void SetAll(const VideoOptions& change) {
235 adapt_input_to_encoder.SetFrom(change.adapt_input_to_encoder);
236 adapt_input_to_cpu_usage.SetFrom(change.adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000237 adapt_cpu_with_smoothing.SetFrom(change.adapt_cpu_with_smoothing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 adapt_view_switch.SetFrom(change.adapt_view_switch);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000239 video_adapt_third.SetFrom(change.video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 video_noise_reduction.SetFrom(change.video_noise_reduction);
241 video_three_layers.SetFrom(change.video_three_layers);
242 video_enable_camera_list.SetFrom(change.video_enable_camera_list);
243 video_one_layer_screencast.SetFrom(change.video_one_layer_screencast);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000244 video_one_to_one.SetFrom(change.video_one_to_one);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 video_high_bitrate.SetFrom(change.video_high_bitrate);
246 video_watermark.SetFrom(change.video_watermark);
247 video_temporal_layer_screencast.SetFrom(
248 change.video_temporal_layer_screencast);
249 video_leaky_bucket.SetFrom(change.video_leaky_bucket);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000250 cpu_overuse_detection.SetFrom(change.cpu_overuse_detection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 conference_mode.SetFrom(change.conference_mode);
252 process_adaptation_threshhold.SetFrom(change.process_adaptation_threshhold);
253 system_low_adaptation_threshhold.SetFrom(
254 change.system_low_adaptation_threshhold);
255 system_high_adaptation_threshhold.SetFrom(
256 change.system_high_adaptation_threshhold);
257 buffered_mode_latency.SetFrom(change.buffered_mode_latency);
258 }
259
260 bool operator==(const VideoOptions& o) const {
261 return adapt_input_to_encoder == o.adapt_input_to_encoder &&
262 adapt_input_to_cpu_usage == o.adapt_input_to_cpu_usage &&
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000263 adapt_cpu_with_smoothing == o.adapt_cpu_with_smoothing &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 adapt_view_switch == o.adapt_view_switch &&
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000265 video_adapt_third == o.video_adapt_third &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 video_noise_reduction == o.video_noise_reduction &&
267 video_three_layers == o.video_three_layers &&
268 video_enable_camera_list == o.video_enable_camera_list &&
269 video_one_layer_screencast == o.video_one_layer_screencast &&
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000270 video_one_to_one == o.video_one_to_one &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 video_high_bitrate == o.video_high_bitrate &&
272 video_watermark == o.video_watermark &&
273 video_temporal_layer_screencast == o.video_temporal_layer_screencast &&
274 video_leaky_bucket == o.video_leaky_bucket &&
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000275 cpu_overuse_detection == o.cpu_overuse_detection &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 conference_mode == o.conference_mode &&
277 process_adaptation_threshhold == o.process_adaptation_threshhold &&
278 system_low_adaptation_threshhold ==
279 o.system_low_adaptation_threshhold &&
280 system_high_adaptation_threshhold ==
281 o.system_high_adaptation_threshhold &&
282 buffered_mode_latency == o.buffered_mode_latency;
283 }
284
285 std::string ToString() const {
286 std::ostringstream ost;
287 ost << "VideoOptions {";
288 ost << ToStringIfSet("encoder adaption", adapt_input_to_encoder);
289 ost << ToStringIfSet("cpu adaption", adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000290 ost << ToStringIfSet("cpu adaptation smoothing", adapt_cpu_with_smoothing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 ost << ToStringIfSet("adapt view switch", adapt_view_switch);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000292 ost << ToStringIfSet("video adapt third", video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 ost << ToStringIfSet("noise reduction", video_noise_reduction);
294 ost << ToStringIfSet("3 layers", video_three_layers);
295 ost << ToStringIfSet("camera list", video_enable_camera_list);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000296 ost << ToStringIfSet("1 layer screencast", video_one_layer_screencast);
297 ost << ToStringIfSet("1 to 1", video_one_to_one);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 ost << ToStringIfSet("high bitrate", video_high_bitrate);
299 ost << ToStringIfSet("watermark", video_watermark);
300 ost << ToStringIfSet("video temporal layer screencast",
301 video_temporal_layer_screencast);
302 ost << ToStringIfSet("leaky bucket", video_leaky_bucket);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000303 ost << ToStringIfSet("cpu overuse detection", cpu_overuse_detection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 ost << ToStringIfSet("conference mode", conference_mode);
305 ost << ToStringIfSet("process", process_adaptation_threshhold);
306 ost << ToStringIfSet("low", system_low_adaptation_threshhold);
307 ost << ToStringIfSet("high", system_high_adaptation_threshhold);
308 ost << ToStringIfSet("buffered mode latency", buffered_mode_latency);
309 ost << "}";
310 return ost.str();
311 }
312
313 // Encoder adaption, which is the gd callback in LMI, and TBA in WebRTC.
314 Settable<bool> adapt_input_to_encoder;
315 // Enable CPU adaptation?
316 Settable<bool> adapt_input_to_cpu_usage;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000317 // Enable CPU adaptation smoothing?
318 Settable<bool> adapt_cpu_with_smoothing;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 // Enable Adapt View Switch?
320 Settable<bool> adapt_view_switch;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000321 // Enable video adapt third?
322 Settable<bool> video_adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 // Enable denoising?
324 Settable<bool> video_noise_reduction;
325 // Experimental: Enable multi layer?
326 Settable<bool> video_three_layers;
327 // Experimental: Enable camera list?
328 Settable<bool> video_enable_camera_list;
329 // Experimental: Enable one layer screencast?
330 Settable<bool> video_one_layer_screencast;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000331 // Experimental: Enable one to one?
332 Settable<bool> video_one_to_one;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 // Experimental: Enable WebRtc higher bitrate?
334 Settable<bool> video_high_bitrate;
335 // Experimental: Add watermark to the rendered video image.
336 Settable<bool> video_watermark;
337 // Experimental: Enable WebRTC layered screencast.
338 Settable<bool> video_temporal_layer_screencast;
339 // Enable WebRTC leaky bucket when sending media packets.
340 Settable<bool> video_leaky_bucket;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000341 // Enable WebRTC Cpu Overuse Detection, which is a new version of the CPU
342 // adaptation algorithm. So this option will override the
343 // |adapt_input_to_cpu_usage|.
344 Settable<bool> cpu_overuse_detection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 // Use conference mode?
346 Settable<bool> conference_mode;
347 // Threshhold for process cpu adaptation. (Process limit)
348 SettablePercent process_adaptation_threshhold;
349 // Low threshhold for cpu adaptation. (Adapt up)
350 SettablePercent system_low_adaptation_threshhold;
351 // High threshhold for cpu adaptation. (Adapt down)
352 SettablePercent system_high_adaptation_threshhold;
353 // Specify buffered mode latency in milliseconds.
354 Settable<int> buffered_mode_latency;
355};
356
357// A class for playing out soundclips.
358class SoundclipMedia {
359 public:
360 enum SoundclipFlags {
361 SF_LOOP = 1,
362 };
363
364 virtual ~SoundclipMedia() {}
365
366 // Plays a sound out to the speakers with the given audio stream. The stream
367 // must be 16-bit little-endian 16 kHz PCM. If a stream is already playing
368 // on this SoundclipMedia, it is stopped. If clip is NULL, nothing is played.
369 // Returns whether it was successful.
370 virtual bool PlaySound(const char *clip, int len, int flags) = 0;
371};
372
373struct RtpHeaderExtension {
374 RtpHeaderExtension() : id(0) {}
375 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
376 std::string uri;
377 int id;
378 // TODO(juberti): SendRecv direction;
379
380 bool operator==(const RtpHeaderExtension& ext) const {
381 // id is a reserved word in objective-c. Therefore the id attribute has to
382 // be a fully qualified name in order to compile on IOS.
383 return this->id == ext.id &&
384 uri == ext.uri;
385 }
386};
387
388// Returns the named header extension if found among all extensions, NULL
389// otherwise.
390inline const RtpHeaderExtension* FindHeaderExtension(
391 const std::vector<RtpHeaderExtension>& extensions,
392 const std::string& name) {
393 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
394 it != extensions.end(); ++it) {
395 if (it->uri == name)
396 return &(*it);
397 }
398 return NULL;
399}
400
401enum MediaChannelOptions {
402 // Tune the stream for conference mode.
403 OPT_CONFERENCE = 0x0001
404};
405
406enum VoiceMediaChannelOptions {
407 // Tune the audio stream for vcs with different target levels.
408 OPT_AGC_MINUS_10DB = 0x80000000
409};
410
411// DTMF flags to control if a DTMF tone should be played and/or sent.
412enum DtmfFlags {
413 DF_PLAY = 0x01,
414 DF_SEND = 0x02,
415};
416
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417class MediaChannel : public sigslot::has_slots<> {
418 public:
419 class NetworkInterface {
420 public:
421 enum SocketType { ST_RTP, ST_RTCP };
422 virtual bool SendPacket(talk_base::Buffer* packet) = 0;
423 virtual bool SendRtcp(talk_base::Buffer* packet) = 0;
424 virtual int SetOption(SocketType type, talk_base::Socket::Option opt,
425 int option) = 0;
426 virtual ~NetworkInterface() {}
427 };
428
429 MediaChannel() : network_interface_(NULL) {}
430 virtual ~MediaChannel() {}
431
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000432 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 virtual void SetInterface(NetworkInterface *iface) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000434 talk_base::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 network_interface_ = iface;
436 }
437
438 // Called when a RTP packet is received.
439 virtual void OnPacketReceived(talk_base::Buffer* packet) = 0;
440 // Called when a RTCP packet is received.
441 virtual void OnRtcpReceived(talk_base::Buffer* packet) = 0;
442 // Called when the socket's ability to send has changed.
443 virtual void OnReadyToSend(bool ready) = 0;
444 // Creates a new outgoing media stream with SSRCs and CNAME as described
445 // by sp.
446 virtual bool AddSendStream(const StreamParams& sp) = 0;
447 // Removes an outgoing media stream.
448 // ssrc must be the first SSRC of the media stream if the stream uses
449 // multiple SSRCs.
450 virtual bool RemoveSendStream(uint32 ssrc) = 0;
451 // Creates a new incoming media stream with SSRCs and CNAME as described
452 // by sp.
453 virtual bool AddRecvStream(const StreamParams& sp) = 0;
454 // Removes an incoming media stream.
455 // ssrc must be the first SSRC of the media stream if the stream uses
456 // multiple SSRCs.
457 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
458
459 // Mutes the channel.
460 virtual bool MuteStream(uint32 ssrc, bool on) = 0;
461
462 // Sets the RTP extension headers and IDs to use when sending RTP.
463 virtual bool SetRecvRtpHeaderExtensions(
464 const std::vector<RtpHeaderExtension>& extensions) = 0;
465 virtual bool SetSendRtpHeaderExtensions(
466 const std::vector<RtpHeaderExtension>& extensions) = 0;
467 // Sets the rate control to use when sending data.
468 virtual bool SetSendBandwidth(bool autobw, int bps) = 0;
469
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000470 // Base method to send packet using NetworkInterface.
471 bool SendPacket(talk_base::Buffer* packet) {
472 return DoSendPacket(packet, false);
473 }
474
475 bool SendRtcp(talk_base::Buffer* packet) {
476 return DoSendPacket(packet, true);
477 }
478
479 int SetOption(NetworkInterface::SocketType type,
480 talk_base::Socket::Option opt,
481 int option) {
482 talk_base::CritScope cs(&network_interface_crit_);
483 if (!network_interface_)
484 return -1;
485
486 return network_interface_->SetOption(type, opt, option);
487 }
488
489 private:
490 bool DoSendPacket(talk_base::Buffer* packet, bool rtcp) {
491 talk_base::CritScope cs(&network_interface_crit_);
492 if (!network_interface_)
493 return false;
494
495 return (!rtcp) ? network_interface_->SendPacket(packet) :
496 network_interface_->SendRtcp(packet);
497 }
498
499 // |network_interface_| can be accessed from the worker_thread and
500 // from any MediaEngine threads. This critical section is to protect accessing
501 // of network_interface_ object.
502 talk_base::CriticalSection network_interface_crit_;
503 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504};
505
506enum SendFlags {
507 SEND_NOTHING,
508 SEND_RINGBACKTONE,
509 SEND_MICROPHONE
510};
511
512struct VoiceSenderInfo {
513 VoiceSenderInfo()
514 : ssrc(0),
515 bytes_sent(0),
516 packets_sent(0),
517 packets_lost(0),
518 fraction_lost(0.0),
519 ext_seqnum(0),
520 rtt_ms(0),
521 jitter_ms(0),
522 audio_level(0),
523 aec_quality_min(0.0),
524 echo_delay_median_ms(0),
525 echo_delay_std_ms(0),
526 echo_return_loss(0),
527 echo_return_loss_enhancement(0) {
528 }
529
530 uint32 ssrc;
531 std::string codec_name;
532 int64 bytes_sent;
533 int packets_sent;
534 int packets_lost;
535 float fraction_lost;
536 int ext_seqnum;
537 int rtt_ms;
538 int jitter_ms;
539 int audio_level;
540 float aec_quality_min;
541 int echo_delay_median_ms;
542 int echo_delay_std_ms;
543 int echo_return_loss;
544 int echo_return_loss_enhancement;
545};
546
547struct VoiceReceiverInfo {
548 VoiceReceiverInfo()
549 : ssrc(0),
550 bytes_rcvd(0),
551 packets_rcvd(0),
552 packets_lost(0),
553 fraction_lost(0.0),
554 ext_seqnum(0),
555 jitter_ms(0),
556 jitter_buffer_ms(0),
557 jitter_buffer_preferred_ms(0),
558 delay_estimate_ms(0),
559 audio_level(0),
560 expand_rate(0) {
561 }
562
563 uint32 ssrc;
564 int64 bytes_rcvd;
565 int packets_rcvd;
566 int packets_lost;
567 float fraction_lost;
568 int ext_seqnum;
569 int jitter_ms;
570 int jitter_buffer_ms;
571 int jitter_buffer_preferred_ms;
572 int delay_estimate_ms;
573 int audio_level;
574 // fraction of synthesized speech inserted through pre-emptive expansion
575 float expand_rate;
576};
577
578struct VideoSenderInfo {
579 VideoSenderInfo()
580 : bytes_sent(0),
581 packets_sent(0),
582 packets_cached(0),
583 packets_lost(0),
584 fraction_lost(0.0),
585 firs_rcvd(0),
586 nacks_rcvd(0),
587 rtt_ms(0),
588 frame_width(0),
589 frame_height(0),
590 framerate_input(0),
591 framerate_sent(0),
592 nominal_bitrate(0),
593 preferred_bitrate(0),
594 adapt_reason(0) {
595 }
596
597 std::vector<uint32> ssrcs;
598 std::vector<SsrcGroup> ssrc_groups;
599 std::string codec_name;
600 int64 bytes_sent;
601 int packets_sent;
602 int packets_cached;
603 int packets_lost;
604 float fraction_lost;
605 int firs_rcvd;
606 int nacks_rcvd;
607 int rtt_ms;
608 int frame_width;
609 int frame_height;
610 int framerate_input;
611 int framerate_sent;
612 int nominal_bitrate;
613 int preferred_bitrate;
614 int adapt_reason;
615};
616
617struct VideoReceiverInfo {
618 VideoReceiverInfo()
619 : bytes_rcvd(0),
620 packets_rcvd(0),
621 packets_lost(0),
622 packets_concealed(0),
623 fraction_lost(0.0),
624 firs_sent(0),
625 nacks_sent(0),
626 frame_width(0),
627 frame_height(0),
628 framerate_rcvd(0),
629 framerate_decoded(0),
630 framerate_output(0),
631 framerate_render_input(0),
632 framerate_render_output(0) {
633 }
634
635 std::vector<uint32> ssrcs;
636 std::vector<SsrcGroup> ssrc_groups;
637 int64 bytes_rcvd;
638 // vector<int> layer_bytes_rcvd;
639 int packets_rcvd;
640 int packets_lost;
641 int packets_concealed;
642 float fraction_lost;
643 int firs_sent;
644 int nacks_sent;
645 int frame_width;
646 int frame_height;
647 int framerate_rcvd;
648 int framerate_decoded;
649 int framerate_output;
650 // Framerate as sent to the renderer.
651 int framerate_render_input;
652 // Framerate that the renderer reports.
653 int framerate_render_output;
654};
655
656struct DataSenderInfo {
657 DataSenderInfo()
658 : ssrc(0),
659 bytes_sent(0),
660 packets_sent(0) {
661 }
662
663 uint32 ssrc;
664 std::string codec_name;
665 int64 bytes_sent;
666 int packets_sent;
667};
668
669struct DataReceiverInfo {
670 DataReceiverInfo()
671 : ssrc(0),
672 bytes_rcvd(0),
673 packets_rcvd(0) {
674 }
675
676 uint32 ssrc;
677 int64 bytes_rcvd;
678 int packets_rcvd;
679};
680
681struct BandwidthEstimationInfo {
682 BandwidthEstimationInfo()
683 : available_send_bandwidth(0),
684 available_recv_bandwidth(0),
685 target_enc_bitrate(0),
686 actual_enc_bitrate(0),
687 retransmit_bitrate(0),
688 transmit_bitrate(0),
689 bucket_delay(0) {
690 }
691
692 int available_send_bandwidth;
693 int available_recv_bandwidth;
694 int target_enc_bitrate;
695 int actual_enc_bitrate;
696 int retransmit_bitrate;
697 int transmit_bitrate;
698 int bucket_delay;
699};
700
701struct VoiceMediaInfo {
702 void Clear() {
703 senders.clear();
704 receivers.clear();
705 }
706 std::vector<VoiceSenderInfo> senders;
707 std::vector<VoiceReceiverInfo> receivers;
708};
709
710struct VideoMediaInfo {
711 void Clear() {
712 senders.clear();
713 receivers.clear();
714 bw_estimations.clear();
715 }
716 std::vector<VideoSenderInfo> senders;
717 std::vector<VideoReceiverInfo> receivers;
718 std::vector<BandwidthEstimationInfo> bw_estimations;
719};
720
721struct DataMediaInfo {
722 void Clear() {
723 senders.clear();
724 receivers.clear();
725 }
726 std::vector<DataSenderInfo> senders;
727 std::vector<DataReceiverInfo> receivers;
728};
729
730class VoiceMediaChannel : public MediaChannel {
731 public:
732 enum Error {
733 ERROR_NONE = 0, // No error.
734 ERROR_OTHER, // Other errors.
735 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
736 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
737 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
738 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
739 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
740 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
741 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
742 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
743 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
744 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
745 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
746 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
747 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
748 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
749 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
750 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
751 };
752
753 VoiceMediaChannel() {}
754 virtual ~VoiceMediaChannel() {}
755 // Sets the codecs/payload types to be used for incoming media.
756 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) = 0;
757 // Sets the codecs/payload types to be used for outgoing media.
758 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) = 0;
759 // Starts or stops playout of received audio.
760 virtual bool SetPlayout(bool playout) = 0;
761 // Starts or stops sending (and potentially capture) of local audio.
762 virtual bool SetSend(SendFlags flag) = 0;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000763 // Sets the renderer object to be used for the specified remote audio stream.
764 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
765 // Sets the renderer object to be used for the specified local audio stream.
766 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 // Gets current energy levels for all incoming streams.
768 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
769 // Get the current energy level of the stream sent to the speaker.
770 virtual int GetOutputLevel() = 0;
771 // Get the time in milliseconds since last recorded keystroke, or negative.
772 virtual int GetTimeSinceLastTyping() = 0;
773 // Temporarily exposed field for tuning typing detect options.
774 virtual void SetTypingDetectionParameters(int time_window,
775 int cost_per_typing, int reporting_threshold, int penalty_decay,
776 int type_event_delay) = 0;
777 // Set left and right scale for speaker output volume of the specified ssrc.
778 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) = 0;
779 // Get left and right scale for speaker output volume of the specified ssrc.
780 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) = 0;
781 // Specifies a ringback tone to be played during call setup.
782 virtual bool SetRingbackTone(const char *buf, int len) = 0;
783 // Plays or stops the aforementioned ringback tone
784 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) = 0;
785 // Returns if the telephone-event has been negotiated.
786 virtual bool CanInsertDtmf() { return false; }
787 // Send and/or play a DTMF |event| according to the |flags|.
788 // The DTMF out-of-band signal will be used on sending.
789 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000790 // The valid value for the |event| are 0 to 15 which corresponding to
791 // DTMF event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) = 0;
793 // Gets quality stats for the channel.
794 virtual bool GetStats(VoiceMediaInfo* info) = 0;
795 // Gets last reported error for this media channel.
796 virtual void GetLastMediaError(uint32* ssrc,
797 VoiceMediaChannel::Error* error) {
798 ASSERT(error != NULL);
799 *error = ERROR_NONE;
800 }
801 // Sets the media options to use.
802 virtual bool SetOptions(const AudioOptions& options) = 0;
803 virtual bool GetOptions(AudioOptions* options) const = 0;
804
805 // Signal errors from MediaChannel. Arguments are:
806 // ssrc(uint32), and error(VoiceMediaChannel::Error).
807 sigslot::signal2<uint32, VoiceMediaChannel::Error> SignalMediaError;
808};
809
810class VideoMediaChannel : public MediaChannel {
811 public:
812 enum Error {
813 ERROR_NONE = 0, // No error.
814 ERROR_OTHER, // Other errors.
815 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
816 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
817 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
818 ERROR_REC_DEVICE_REMOVED, // Device is removed.
819 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
820 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
821 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
822 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
823 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
824 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
825 };
826
827 VideoMediaChannel() : renderer_(NULL) {}
828 virtual ~VideoMediaChannel() {}
829 // Sets the codecs/payload types to be used for incoming media.
830 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) = 0;
831 // Sets the codecs/payload types to be used for outgoing media.
832 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) = 0;
833 // Gets the currently set codecs/payload types to be used for outgoing media.
834 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
835 // Sets the format of a specified outgoing stream.
836 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) = 0;
837 // Starts or stops playout of received video.
838 virtual bool SetRender(bool render) = 0;
839 // Starts or stops transmission (and potentially capture) of local video.
840 virtual bool SetSend(bool send) = 0;
841 // Sets the renderer object to be used for the specified stream.
842 // If SSRC is 0, the renderer is used for the 'default' stream.
843 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) = 0;
844 // If |ssrc| is 0, replace the default capturer (engine capturer) with
845 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
846 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) = 0;
847 // Gets quality stats for the channel.
848 virtual bool GetStats(VideoMediaInfo* info) = 0;
849
850 // Send an intra frame to the receivers.
851 virtual bool SendIntraFrame() = 0;
852 // Reuqest each of the remote senders to send an intra frame.
853 virtual bool RequestIntraFrame() = 0;
854 // Sets the media options to use.
855 virtual bool SetOptions(const VideoOptions& options) = 0;
856 virtual bool GetOptions(VideoOptions* options) const = 0;
857 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) = 0;
858
859 // Signal errors from MediaChannel. Arguments are:
860 // ssrc(uint32), and error(VideoMediaChannel::Error).
861 sigslot::signal2<uint32, Error> SignalMediaError;
862
863 protected:
864 VideoRenderer *renderer_;
865};
866
867enum DataMessageType {
868 // TODO(pthatcher): Make this enum match the SCTP PPIDs that WebRTC uses?
869 DMT_CONTROL = 0,
870 DMT_BINARY = 1,
871 DMT_TEXT = 2,
872};
873
874// Info about data received in DataMediaChannel. For use in
875// DataMediaChannel::SignalDataReceived and in all of the signals that
876// signal fires, on up the chain.
877struct ReceiveDataParams {
878 // The in-packet stream indentifier.
879 // For SCTP, this is really SID, not SSRC.
880 uint32 ssrc;
881 // The type of message (binary, text, or control).
882 DataMessageType type;
883 // A per-stream value incremented per packet in the stream.
884 int seq_num;
885 // A per-stream value monotonically increasing with time.
886 int timestamp;
887
888 ReceiveDataParams() :
889 ssrc(0),
890 type(DMT_TEXT),
891 seq_num(0),
892 timestamp(0) {
893 }
894};
895
896struct SendDataParams {
897 // The in-packet stream indentifier.
898 // For SCTP, this is really SID, not SSRC.
899 uint32 ssrc;
900 // The type of message (binary, text, or control).
901 DataMessageType type;
902
903 // For SCTP, whether to send messages flagged as ordered or not.
904 // If false, messages can be received out of order.
905 bool ordered;
906 // For SCTP, whether the messages are sent reliably or not.
907 // If false, messages may be lost.
908 bool reliable;
909 // For SCTP, if reliable == false, provide partial reliability by
910 // resending up to this many times. Either count or millis
911 // is supported, not both at the same time.
912 int max_rtx_count;
913 // For SCTP, if reliable == false, provide partial reliability by
914 // resending for up to this many milliseconds. Either count or millis
915 // is supported, not both at the same time.
916 int max_rtx_ms;
917
918 SendDataParams() :
919 ssrc(0),
920 type(DMT_TEXT),
921 // TODO(pthatcher): Make these true by default?
922 ordered(false),
923 reliable(false),
924 max_rtx_count(0),
925 max_rtx_ms(0) {
926 }
927};
928
929enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
930
931class DataMediaChannel : public MediaChannel {
932 public:
933 enum Error {
934 ERROR_NONE = 0, // No error.
935 ERROR_OTHER, // Other errors.
936 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
937 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
938 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
939 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
940 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
941 };
942
943 virtual ~DataMediaChannel() {}
944
945 virtual bool SetSendBandwidth(bool autobw, int bps) = 0;
946 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) = 0;
947 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) = 0;
948 virtual bool SetRecvRtpHeaderExtensions(
949 const std::vector<RtpHeaderExtension>& extensions) = 0;
950 virtual bool SetSendRtpHeaderExtensions(
951 const std::vector<RtpHeaderExtension>& extensions) = 0;
952 virtual bool AddSendStream(const StreamParams& sp) = 0;
953 virtual bool RemoveSendStream(uint32 ssrc) = 0;
954 virtual bool AddRecvStream(const StreamParams& sp) = 0;
955 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
956 virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
957 // TODO(pthatcher): Implement this.
958 virtual bool GetStats(DataMediaInfo* info) { return true; }
959
960 virtual bool SetSend(bool send) = 0;
961 virtual bool SetReceive(bool receive) = 0;
962 virtual void OnPacketReceived(talk_base::Buffer* packet) = 0;
963 virtual void OnRtcpReceived(talk_base::Buffer* packet) = 0;
964
965 virtual bool SendData(
966 const SendDataParams& params,
967 const talk_base::Buffer& payload,
968 SendDataResult* result = NULL) = 0;
969 // Signals when data is received (params, data, len)
970 sigslot::signal3<const ReceiveDataParams&,
971 const char*,
972 size_t> SignalDataReceived;
973 // Signal errors from MediaChannel. Arguments are:
974 // ssrc(uint32), and error(DataMediaChannel::Error).
975 sigslot::signal2<uint32, DataMediaChannel::Error> SignalMediaError;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000976 // Signal when the media channel is ready to send the stream. Arguments are:
977 // writable(bool)
978 sigslot::signal1<bool> SignalReadyToSend;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979};
980
981} // namespace cricket
982
983#endif // TALK_MEDIA_BASE_MEDIACHANNEL_H_