blob: 7431bc1001f7e56b5918a30a945ef4e033ee7167 [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"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000036#include "talk/base/dscp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/base/logging.h"
38#include "talk/base/sigslot.h"
39#include "talk/base/socket.h"
40#include "talk/base/window.h"
41#include "talk/media/base/codec.h"
42#include "talk/media/base/constants.h"
43#include "talk/media/base/streamparams.h"
44// TODO(juberti): re-evaluate this include
45#include "talk/session/media/audiomonitor.h"
46
47namespace talk_base {
48class Buffer;
49class RateLimiter;
50class Timing;
51}
52
53namespace cricket {
54
55class AudioRenderer;
56struct RtpHeader;
57class ScreencastId;
58struct VideoFormat;
59class VideoCapturer;
60class VideoRenderer;
61
62const int kMinRtpHeaderExtensionId = 1;
63const int kMaxRtpHeaderExtensionId = 255;
64const int kScreencastDefaultFps = 5;
65
66// Used in AudioOptions and VideoOptions to signify "unset" values.
67template <class T>
68class Settable {
69 public:
70 Settable() : set_(false), val_() {}
71 explicit Settable(T val) : set_(true), val_(val) {}
72
73 bool IsSet() const {
74 return set_;
75 }
76
77 bool Get(T* out) const {
78 *out = val_;
79 return set_;
80 }
81
82 T GetWithDefaultIfUnset(const T& default_value) const {
83 return set_ ? val_ : default_value;
84 }
85
86 virtual void Set(T val) {
87 set_ = true;
88 val_ = val;
89 }
90
91 void Clear() {
92 Set(T());
93 set_ = false;
94 }
95
96 void SetFrom(const Settable<T>& o) {
97 // Set this value based on the value of o, iff o is set. If this value is
98 // set and o is unset, the current value will be unchanged.
99 T val;
100 if (o.Get(&val)) {
101 Set(val);
102 }
103 }
104
105 std::string ToString() const {
106 return set_ ? talk_base::ToString(val_) : "";
107 }
108
109 bool operator==(const Settable<T>& o) const {
110 // Equal if both are unset with any value or both set with the same value.
111 return (set_ == o.set_) && (!set_ || (val_ == o.val_));
112 }
113
114 bool operator!=(const Settable<T>& o) const {
115 return !operator==(o);
116 }
117
118 protected:
119 void InitializeValue(const T &val) {
120 val_ = val;
121 }
122
123 private:
124 bool set_;
125 T val_;
126};
127
128class SettablePercent : public Settable<float> {
129 public:
130 virtual void Set(float val) {
131 if (val < 0) {
132 val = 0;
133 }
134 if (val > 1.0) {
135 val = 1.0;
136 }
137 Settable<float>::Set(val);
138 }
139};
140
141template <class T>
142static std::string ToStringIfSet(const char* key, const Settable<T>& val) {
143 std::string str;
144 if (val.IsSet()) {
145 str = key;
146 str += ": ";
147 str += val.ToString();
148 str += ", ";
149 }
150 return str;
151}
152
153// Options that can be applied to a VoiceMediaChannel or a VoiceMediaEngine.
154// Used to be flags, but that makes it hard to selectively apply options.
155// We are moving all of the setting of options to structs like this,
156// but some things currently still use flags.
157struct AudioOptions {
158 void SetAll(const AudioOptions& change) {
159 echo_cancellation.SetFrom(change.echo_cancellation);
160 auto_gain_control.SetFrom(change.auto_gain_control);
161 noise_suppression.SetFrom(change.noise_suppression);
162 highpass_filter.SetFrom(change.highpass_filter);
163 stereo_swapping.SetFrom(change.stereo_swapping);
164 typing_detection.SetFrom(change.typing_detection);
165 conference_mode.SetFrom(change.conference_mode);
166 adjust_agc_delta.SetFrom(change.adjust_agc_delta);
167 experimental_agc.SetFrom(change.experimental_agc);
168 experimental_aec.SetFrom(change.experimental_aec);
169 aec_dump.SetFrom(change.aec_dump);
170 }
171
172 bool operator==(const AudioOptions& o) const {
173 return echo_cancellation == o.echo_cancellation &&
174 auto_gain_control == o.auto_gain_control &&
175 noise_suppression == o.noise_suppression &&
176 highpass_filter == o.highpass_filter &&
177 stereo_swapping == o.stereo_swapping &&
178 typing_detection == o.typing_detection &&
179 conference_mode == o.conference_mode &&
180 experimental_agc == o.experimental_agc &&
181 experimental_aec == o.experimental_aec &&
182 adjust_agc_delta == o.adjust_agc_delta &&
183 aec_dump == o.aec_dump;
184 }
185
186 std::string ToString() const {
187 std::ostringstream ost;
188 ost << "AudioOptions {";
189 ost << ToStringIfSet("aec", echo_cancellation);
190 ost << ToStringIfSet("agc", auto_gain_control);
191 ost << ToStringIfSet("ns", noise_suppression);
192 ost << ToStringIfSet("hf", highpass_filter);
193 ost << ToStringIfSet("swap", stereo_swapping);
194 ost << ToStringIfSet("typing", typing_detection);
195 ost << ToStringIfSet("conference", conference_mode);
196 ost << ToStringIfSet("agc_delta", adjust_agc_delta);
197 ost << ToStringIfSet("experimental_agc", experimental_agc);
198 ost << ToStringIfSet("experimental_aec", experimental_aec);
199 ost << ToStringIfSet("aec_dump", aec_dump);
200 ost << "}";
201 return ost.str();
202 }
203
204 // Audio processing that attempts to filter away the output signal from
205 // later inbound pickup.
206 Settable<bool> echo_cancellation;
207 // Audio processing to adjust the sensitivity of the local mic dynamically.
208 Settable<bool> auto_gain_control;
209 // Audio processing to filter out background noise.
210 Settable<bool> noise_suppression;
211 // Audio processing to remove background noise of lower frequencies.
212 Settable<bool> highpass_filter;
213 // Audio processing to swap the left and right channels.
214 Settable<bool> stereo_swapping;
215 // Audio processing to detect typing.
216 Settable<bool> typing_detection;
217 Settable<bool> conference_mode;
218 Settable<int> adjust_agc_delta;
219 Settable<bool> experimental_agc;
220 Settable<bool> experimental_aec;
221 Settable<bool> aec_dump;
222};
223
224// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
225// Used to be flags, but that makes it hard to selectively apply options.
226// We are moving all of the setting of options to structs like this,
227// but some things currently still use flags.
228struct VideoOptions {
229 VideoOptions() {
230 process_adaptation_threshhold.Set(kProcessCpuThreshold);
231 system_low_adaptation_threshhold.Set(kLowSystemCpuThreshold);
232 system_high_adaptation_threshhold.Set(kHighSystemCpuThreshold);
233 }
234
235 void SetAll(const VideoOptions& change) {
236 adapt_input_to_encoder.SetFrom(change.adapt_input_to_encoder);
237 adapt_input_to_cpu_usage.SetFrom(change.adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000238 adapt_cpu_with_smoothing.SetFrom(change.adapt_cpu_with_smoothing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 adapt_view_switch.SetFrom(change.adapt_view_switch);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000240 video_adapt_third.SetFrom(change.video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 video_noise_reduction.SetFrom(change.video_noise_reduction);
242 video_three_layers.SetFrom(change.video_three_layers);
243 video_enable_camera_list.SetFrom(change.video_enable_camera_list);
244 video_one_layer_screencast.SetFrom(change.video_one_layer_screencast);
245 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 &&
270 video_high_bitrate == o.video_high_bitrate &&
271 video_watermark == o.video_watermark &&
272 video_temporal_layer_screencast == o.video_temporal_layer_screencast &&
273 video_leaky_bucket == o.video_leaky_bucket &&
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000274 cpu_overuse_detection == o.cpu_overuse_detection &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 conference_mode == o.conference_mode &&
276 process_adaptation_threshhold == o.process_adaptation_threshhold &&
277 system_low_adaptation_threshhold ==
278 o.system_low_adaptation_threshhold &&
279 system_high_adaptation_threshhold ==
280 o.system_high_adaptation_threshhold &&
281 buffered_mode_latency == o.buffered_mode_latency;
282 }
283
284 std::string ToString() const {
285 std::ostringstream ost;
286 ost << "VideoOptions {";
287 ost << ToStringIfSet("encoder adaption", adapt_input_to_encoder);
288 ost << ToStringIfSet("cpu adaption", adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000289 ost << ToStringIfSet("cpu adaptation smoothing", adapt_cpu_with_smoothing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 ost << ToStringIfSet("adapt view switch", adapt_view_switch);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000291 ost << ToStringIfSet("video adapt third", video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 ost << ToStringIfSet("noise reduction", video_noise_reduction);
293 ost << ToStringIfSet("3 layers", video_three_layers);
294 ost << ToStringIfSet("camera list", video_enable_camera_list);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000295 ost << ToStringIfSet("1 layer screencast", video_one_layer_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 ost << ToStringIfSet("high bitrate", video_high_bitrate);
297 ost << ToStringIfSet("watermark", video_watermark);
298 ost << ToStringIfSet("video temporal layer screencast",
299 video_temporal_layer_screencast);
300 ost << ToStringIfSet("leaky bucket", video_leaky_bucket);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000301 ost << ToStringIfSet("cpu overuse detection", cpu_overuse_detection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 ost << ToStringIfSet("conference mode", conference_mode);
303 ost << ToStringIfSet("process", process_adaptation_threshhold);
304 ost << ToStringIfSet("low", system_low_adaptation_threshhold);
305 ost << ToStringIfSet("high", system_high_adaptation_threshhold);
306 ost << ToStringIfSet("buffered mode latency", buffered_mode_latency);
307 ost << "}";
308 return ost.str();
309 }
310
311 // Encoder adaption, which is the gd callback in LMI, and TBA in WebRTC.
312 Settable<bool> adapt_input_to_encoder;
313 // Enable CPU adaptation?
314 Settable<bool> adapt_input_to_cpu_usage;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000315 // Enable CPU adaptation smoothing?
316 Settable<bool> adapt_cpu_with_smoothing;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 // Enable Adapt View Switch?
318 Settable<bool> adapt_view_switch;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000319 // Enable video adapt third?
320 Settable<bool> video_adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 // Enable denoising?
322 Settable<bool> video_noise_reduction;
323 // Experimental: Enable multi layer?
324 Settable<bool> video_three_layers;
325 // Experimental: Enable camera list?
326 Settable<bool> video_enable_camera_list;
327 // Experimental: Enable one layer screencast?
328 Settable<bool> video_one_layer_screencast;
329 // Experimental: Enable WebRtc higher bitrate?
330 Settable<bool> video_high_bitrate;
331 // Experimental: Add watermark to the rendered video image.
332 Settable<bool> video_watermark;
333 // Experimental: Enable WebRTC layered screencast.
334 Settable<bool> video_temporal_layer_screencast;
335 // Enable WebRTC leaky bucket when sending media packets.
336 Settable<bool> video_leaky_bucket;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000337 // Enable WebRTC Cpu Overuse Detection, which is a new version of the CPU
338 // adaptation algorithm. So this option will override the
339 // |adapt_input_to_cpu_usage|.
340 Settable<bool> cpu_overuse_detection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 // Use conference mode?
342 Settable<bool> conference_mode;
343 // Threshhold for process cpu adaptation. (Process limit)
344 SettablePercent process_adaptation_threshhold;
345 // Low threshhold for cpu adaptation. (Adapt up)
346 SettablePercent system_low_adaptation_threshhold;
347 // High threshhold for cpu adaptation. (Adapt down)
348 SettablePercent system_high_adaptation_threshhold;
349 // Specify buffered mode latency in milliseconds.
350 Settable<int> buffered_mode_latency;
351};
352
353// A class for playing out soundclips.
354class SoundclipMedia {
355 public:
356 enum SoundclipFlags {
357 SF_LOOP = 1,
358 };
359
360 virtual ~SoundclipMedia() {}
361
362 // Plays a sound out to the speakers with the given audio stream. The stream
363 // must be 16-bit little-endian 16 kHz PCM. If a stream is already playing
364 // on this SoundclipMedia, it is stopped. If clip is NULL, nothing is played.
365 // Returns whether it was successful.
366 virtual bool PlaySound(const char *clip, int len, int flags) = 0;
367};
368
369struct RtpHeaderExtension {
370 RtpHeaderExtension() : id(0) {}
371 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
372 std::string uri;
373 int id;
374 // TODO(juberti): SendRecv direction;
375
376 bool operator==(const RtpHeaderExtension& ext) const {
377 // id is a reserved word in objective-c. Therefore the id attribute has to
378 // be a fully qualified name in order to compile on IOS.
379 return this->id == ext.id &&
380 uri == ext.uri;
381 }
382};
383
384// Returns the named header extension if found among all extensions, NULL
385// otherwise.
386inline const RtpHeaderExtension* FindHeaderExtension(
387 const std::vector<RtpHeaderExtension>& extensions,
388 const std::string& name) {
389 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
390 it != extensions.end(); ++it) {
391 if (it->uri == name)
392 return &(*it);
393 }
394 return NULL;
395}
396
397enum MediaChannelOptions {
398 // Tune the stream for conference mode.
399 OPT_CONFERENCE = 0x0001
400};
401
402enum VoiceMediaChannelOptions {
403 // Tune the audio stream for vcs with different target levels.
404 OPT_AGC_MINUS_10DB = 0x80000000
405};
406
407// DTMF flags to control if a DTMF tone should be played and/or sent.
408enum DtmfFlags {
409 DF_PLAY = 0x01,
410 DF_SEND = 0x02,
411};
412
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413class MediaChannel : public sigslot::has_slots<> {
414 public:
415 class NetworkInterface {
416 public:
417 enum SocketType { ST_RTP, ST_RTCP };
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000418 virtual bool SendPacket(
419 talk_base::Buffer* packet,
420 talk_base::DiffServCodePoint dscp = talk_base::DSCP_NO_CHANGE) = 0;
421 virtual bool SendRtcp(
422 talk_base::Buffer* packet,
423 talk_base::DiffServCodePoint dscp = talk_base::DSCP_NO_CHANGE) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 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),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000527 echo_return_loss_enhancement(0),
528 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 }
530
531 uint32 ssrc;
532 std::string codec_name;
533 int64 bytes_sent;
534 int packets_sent;
535 int packets_lost;
536 float fraction_lost;
537 int ext_seqnum;
538 int rtt_ms;
539 int jitter_ms;
540 int audio_level;
541 float aec_quality_min;
542 int echo_delay_median_ms;
543 int echo_delay_std_ms;
544 int echo_return_loss;
545 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000546 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547};
548
549struct VoiceReceiverInfo {
550 VoiceReceiverInfo()
551 : ssrc(0),
552 bytes_rcvd(0),
553 packets_rcvd(0),
554 packets_lost(0),
555 fraction_lost(0.0),
556 ext_seqnum(0),
557 jitter_ms(0),
558 jitter_buffer_ms(0),
559 jitter_buffer_preferred_ms(0),
560 delay_estimate_ms(0),
561 audio_level(0),
562 expand_rate(0) {
563 }
564
565 uint32 ssrc;
566 int64 bytes_rcvd;
567 int packets_rcvd;
568 int packets_lost;
569 float fraction_lost;
570 int ext_seqnum;
571 int jitter_ms;
572 int jitter_buffer_ms;
573 int jitter_buffer_preferred_ms;
574 int delay_estimate_ms;
575 int audio_level;
576 // fraction of synthesized speech inserted through pre-emptive expansion
577 float expand_rate;
578};
579
580struct VideoSenderInfo {
581 VideoSenderInfo()
582 : bytes_sent(0),
583 packets_sent(0),
584 packets_cached(0),
585 packets_lost(0),
586 fraction_lost(0.0),
587 firs_rcvd(0),
588 nacks_rcvd(0),
589 rtt_ms(0),
590 frame_width(0),
591 frame_height(0),
592 framerate_input(0),
593 framerate_sent(0),
594 nominal_bitrate(0),
595 preferred_bitrate(0),
596 adapt_reason(0) {
597 }
598
599 std::vector<uint32> ssrcs;
600 std::vector<SsrcGroup> ssrc_groups;
601 std::string codec_name;
602 int64 bytes_sent;
603 int packets_sent;
604 int packets_cached;
605 int packets_lost;
606 float fraction_lost;
607 int firs_rcvd;
608 int nacks_rcvd;
609 int rtt_ms;
610 int frame_width;
611 int frame_height;
612 int framerate_input;
613 int framerate_sent;
614 int nominal_bitrate;
615 int preferred_bitrate;
616 int adapt_reason;
617};
618
619struct VideoReceiverInfo {
620 VideoReceiverInfo()
621 : bytes_rcvd(0),
622 packets_rcvd(0),
623 packets_lost(0),
624 packets_concealed(0),
625 fraction_lost(0.0),
626 firs_sent(0),
627 nacks_sent(0),
628 frame_width(0),
629 frame_height(0),
630 framerate_rcvd(0),
631 framerate_decoded(0),
632 framerate_output(0),
633 framerate_render_input(0),
634 framerate_render_output(0) {
635 }
636
637 std::vector<uint32> ssrcs;
638 std::vector<SsrcGroup> ssrc_groups;
639 int64 bytes_rcvd;
640 // vector<int> layer_bytes_rcvd;
641 int packets_rcvd;
642 int packets_lost;
643 int packets_concealed;
644 float fraction_lost;
645 int firs_sent;
646 int nacks_sent;
647 int frame_width;
648 int frame_height;
649 int framerate_rcvd;
650 int framerate_decoded;
651 int framerate_output;
652 // Framerate as sent to the renderer.
653 int framerate_render_input;
654 // Framerate that the renderer reports.
655 int framerate_render_output;
656};
657
658struct DataSenderInfo {
659 DataSenderInfo()
660 : ssrc(0),
661 bytes_sent(0),
662 packets_sent(0) {
663 }
664
665 uint32 ssrc;
666 std::string codec_name;
667 int64 bytes_sent;
668 int packets_sent;
669};
670
671struct DataReceiverInfo {
672 DataReceiverInfo()
673 : ssrc(0),
674 bytes_rcvd(0),
675 packets_rcvd(0) {
676 }
677
678 uint32 ssrc;
679 int64 bytes_rcvd;
680 int packets_rcvd;
681};
682
683struct BandwidthEstimationInfo {
684 BandwidthEstimationInfo()
685 : available_send_bandwidth(0),
686 available_recv_bandwidth(0),
687 target_enc_bitrate(0),
688 actual_enc_bitrate(0),
689 retransmit_bitrate(0),
690 transmit_bitrate(0),
691 bucket_delay(0) {
692 }
693
694 int available_send_bandwidth;
695 int available_recv_bandwidth;
696 int target_enc_bitrate;
697 int actual_enc_bitrate;
698 int retransmit_bitrate;
699 int transmit_bitrate;
700 int bucket_delay;
701};
702
703struct VoiceMediaInfo {
704 void Clear() {
705 senders.clear();
706 receivers.clear();
707 }
708 std::vector<VoiceSenderInfo> senders;
709 std::vector<VoiceReceiverInfo> receivers;
710};
711
712struct VideoMediaInfo {
713 void Clear() {
714 senders.clear();
715 receivers.clear();
716 bw_estimations.clear();
717 }
718 std::vector<VideoSenderInfo> senders;
719 std::vector<VideoReceiverInfo> receivers;
720 std::vector<BandwidthEstimationInfo> bw_estimations;
721};
722
723struct DataMediaInfo {
724 void Clear() {
725 senders.clear();
726 receivers.clear();
727 }
728 std::vector<DataSenderInfo> senders;
729 std::vector<DataReceiverInfo> receivers;
730};
731
732class VoiceMediaChannel : public MediaChannel {
733 public:
734 enum Error {
735 ERROR_NONE = 0, // No error.
736 ERROR_OTHER, // Other errors.
737 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
738 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
739 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
740 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
741 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
742 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
743 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
744 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
745 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
746 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
747 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
748 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
749 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
750 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
751 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
752 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
753 };
754
755 VoiceMediaChannel() {}
756 virtual ~VoiceMediaChannel() {}
757 // Sets the codecs/payload types to be used for incoming media.
758 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) = 0;
759 // Sets the codecs/payload types to be used for outgoing media.
760 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) = 0;
761 // Starts or stops playout of received audio.
762 virtual bool SetPlayout(bool playout) = 0;
763 // Starts or stops sending (and potentially capture) of local audio.
764 virtual bool SetSend(SendFlags flag) = 0;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000765 // Sets the renderer object to be used for the specified remote audio stream.
766 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
767 // Sets the renderer object to be used for the specified local audio stream.
768 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 // Gets current energy levels for all incoming streams.
770 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
771 // Get the current energy level of the stream sent to the speaker.
772 virtual int GetOutputLevel() = 0;
773 // Get the time in milliseconds since last recorded keystroke, or negative.
774 virtual int GetTimeSinceLastTyping() = 0;
775 // Temporarily exposed field for tuning typing detect options.
776 virtual void SetTypingDetectionParameters(int time_window,
777 int cost_per_typing, int reporting_threshold, int penalty_decay,
778 int type_event_delay) = 0;
779 // Set left and right scale for speaker output volume of the specified ssrc.
780 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) = 0;
781 // Get left and right scale for speaker output volume of the specified ssrc.
782 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) = 0;
783 // Specifies a ringback tone to be played during call setup.
784 virtual bool SetRingbackTone(const char *buf, int len) = 0;
785 // Plays or stops the aforementioned ringback tone
786 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) = 0;
787 // Returns if the telephone-event has been negotiated.
788 virtual bool CanInsertDtmf() { return false; }
789 // Send and/or play a DTMF |event| according to the |flags|.
790 // The DTMF out-of-band signal will be used on sending.
791 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000792 // The valid value for the |event| are 0 to 15 which corresponding to
793 // DTMF event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) = 0;
795 // Gets quality stats for the channel.
796 virtual bool GetStats(VoiceMediaInfo* info) = 0;
797 // Gets last reported error for this media channel.
798 virtual void GetLastMediaError(uint32* ssrc,
799 VoiceMediaChannel::Error* error) {
800 ASSERT(error != NULL);
801 *error = ERROR_NONE;
802 }
803 // Sets the media options to use.
804 virtual bool SetOptions(const AudioOptions& options) = 0;
805 virtual bool GetOptions(AudioOptions* options) const = 0;
806
807 // Signal errors from MediaChannel. Arguments are:
808 // ssrc(uint32), and error(VoiceMediaChannel::Error).
809 sigslot::signal2<uint32, VoiceMediaChannel::Error> SignalMediaError;
810};
811
812class VideoMediaChannel : public MediaChannel {
813 public:
814 enum Error {
815 ERROR_NONE = 0, // No error.
816 ERROR_OTHER, // Other errors.
817 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
818 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
819 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
820 ERROR_REC_DEVICE_REMOVED, // Device is removed.
821 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
822 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
823 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
824 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
825 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
826 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
827 };
828
829 VideoMediaChannel() : renderer_(NULL) {}
830 virtual ~VideoMediaChannel() {}
831 // Sets the codecs/payload types to be used for incoming media.
832 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) = 0;
833 // Sets the codecs/payload types to be used for outgoing media.
834 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) = 0;
835 // Gets the currently set codecs/payload types to be used for outgoing media.
836 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
837 // Sets the format of a specified outgoing stream.
838 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) = 0;
839 // Starts or stops playout of received video.
840 virtual bool SetRender(bool render) = 0;
841 // Starts or stops transmission (and potentially capture) of local video.
842 virtual bool SetSend(bool send) = 0;
843 // Sets the renderer object to be used for the specified stream.
844 // If SSRC is 0, the renderer is used for the 'default' stream.
845 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) = 0;
846 // If |ssrc| is 0, replace the default capturer (engine capturer) with
847 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
848 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) = 0;
849 // Gets quality stats for the channel.
850 virtual bool GetStats(VideoMediaInfo* info) = 0;
851
852 // Send an intra frame to the receivers.
853 virtual bool SendIntraFrame() = 0;
854 // Reuqest each of the remote senders to send an intra frame.
855 virtual bool RequestIntraFrame() = 0;
856 // Sets the media options to use.
857 virtual bool SetOptions(const VideoOptions& options) = 0;
858 virtual bool GetOptions(VideoOptions* options) const = 0;
859 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) = 0;
860
861 // Signal errors from MediaChannel. Arguments are:
862 // ssrc(uint32), and error(VideoMediaChannel::Error).
863 sigslot::signal2<uint32, Error> SignalMediaError;
864
865 protected:
866 VideoRenderer *renderer_;
867};
868
869enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000870 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
871 // values.
872 DMT_NONE = 0,
873 DMT_CONTROL = 1,
874 DMT_BINARY = 2,
875 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876};
877
878// Info about data received in DataMediaChannel. For use in
879// DataMediaChannel::SignalDataReceived and in all of the signals that
880// signal fires, on up the chain.
881struct ReceiveDataParams {
882 // The in-packet stream indentifier.
883 // For SCTP, this is really SID, not SSRC.
884 uint32 ssrc;
885 // The type of message (binary, text, or control).
886 DataMessageType type;
887 // A per-stream value incremented per packet in the stream.
888 int seq_num;
889 // A per-stream value monotonically increasing with time.
890 int timestamp;
891
892 ReceiveDataParams() :
893 ssrc(0),
894 type(DMT_TEXT),
895 seq_num(0),
896 timestamp(0) {
897 }
898};
899
900struct SendDataParams {
901 // The in-packet stream indentifier.
902 // For SCTP, this is really SID, not SSRC.
903 uint32 ssrc;
904 // The type of message (binary, text, or control).
905 DataMessageType type;
906
907 // For SCTP, whether to send messages flagged as ordered or not.
908 // If false, messages can be received out of order.
909 bool ordered;
910 // For SCTP, whether the messages are sent reliably or not.
911 // If false, messages may be lost.
912 bool reliable;
913 // For SCTP, if reliable == false, provide partial reliability by
914 // resending up to this many times. Either count or millis
915 // is supported, not both at the same time.
916 int max_rtx_count;
917 // For SCTP, if reliable == false, provide partial reliability by
918 // resending for up to this many milliseconds. Either count or millis
919 // is supported, not both at the same time.
920 int max_rtx_ms;
921
922 SendDataParams() :
923 ssrc(0),
924 type(DMT_TEXT),
925 // TODO(pthatcher): Make these true by default?
926 ordered(false),
927 reliable(false),
928 max_rtx_count(0),
929 max_rtx_ms(0) {
930 }
931};
932
933enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
934
935class DataMediaChannel : public MediaChannel {
936 public:
937 enum Error {
938 ERROR_NONE = 0, // No error.
939 ERROR_OTHER, // Other errors.
940 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
941 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
942 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
943 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
944 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
945 };
946
947 virtual ~DataMediaChannel() {}
948
949 virtual bool SetSendBandwidth(bool autobw, int bps) = 0;
950 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) = 0;
951 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) = 0;
952 virtual bool SetRecvRtpHeaderExtensions(
953 const std::vector<RtpHeaderExtension>& extensions) = 0;
954 virtual bool SetSendRtpHeaderExtensions(
955 const std::vector<RtpHeaderExtension>& extensions) = 0;
956 virtual bool AddSendStream(const StreamParams& sp) = 0;
957 virtual bool RemoveSendStream(uint32 ssrc) = 0;
958 virtual bool AddRecvStream(const StreamParams& sp) = 0;
959 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
960 virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
961 // TODO(pthatcher): Implement this.
962 virtual bool GetStats(DataMediaInfo* info) { return true; }
963
964 virtual bool SetSend(bool send) = 0;
965 virtual bool SetReceive(bool receive) = 0;
966 virtual void OnPacketReceived(talk_base::Buffer* packet) = 0;
967 virtual void OnRtcpReceived(talk_base::Buffer* packet) = 0;
968
969 virtual bool SendData(
970 const SendDataParams& params,
971 const talk_base::Buffer& payload,
972 SendDataResult* result = NULL) = 0;
973 // Signals when data is received (params, data, len)
974 sigslot::signal3<const ReceiveDataParams&,
975 const char*,
976 size_t> SignalDataReceived;
977 // Signal errors from MediaChannel. Arguments are:
978 // ssrc(uint32), and error(DataMediaChannel::Error).
979 sigslot::signal2<uint32, DataMediaChannel::Error> SignalMediaError;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000980 // Signal when the media channel is ready to send the stream. Arguments are:
981 // writable(bool)
982 sigslot::signal1<bool> SignalReadyToSend;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983};
984
985} // namespace cricket
986
987#endif // TALK_MEDIA_BASE_MEDIACHANNEL_H_