blob: c73d20ae8de1a22f6d6753e11f9e20a0aa88a355 [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);
244 video_high_bitrate.SetFrom(change.video_high_bitrate);
245 video_watermark.SetFrom(change.video_watermark);
246 video_temporal_layer_screencast.SetFrom(
247 change.video_temporal_layer_screencast);
248 video_leaky_bucket.SetFrom(change.video_leaky_bucket);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000249 cpu_overuse_detection.SetFrom(change.cpu_overuse_detection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 conference_mode.SetFrom(change.conference_mode);
251 process_adaptation_threshhold.SetFrom(change.process_adaptation_threshhold);
252 system_low_adaptation_threshhold.SetFrom(
253 change.system_low_adaptation_threshhold);
254 system_high_adaptation_threshhold.SetFrom(
255 change.system_high_adaptation_threshhold);
256 buffered_mode_latency.SetFrom(change.buffered_mode_latency);
257 }
258
259 bool operator==(const VideoOptions& o) const {
260 return adapt_input_to_encoder == o.adapt_input_to_encoder &&
261 adapt_input_to_cpu_usage == o.adapt_input_to_cpu_usage &&
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000262 adapt_cpu_with_smoothing == o.adapt_cpu_with_smoothing &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 adapt_view_switch == o.adapt_view_switch &&
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000264 video_adapt_third == o.video_adapt_third &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 video_noise_reduction == o.video_noise_reduction &&
266 video_three_layers == o.video_three_layers &&
267 video_enable_camera_list == o.video_enable_camera_list &&
268 video_one_layer_screencast == o.video_one_layer_screencast &&
269 video_high_bitrate == o.video_high_bitrate &&
270 video_watermark == o.video_watermark &&
271 video_temporal_layer_screencast == o.video_temporal_layer_screencast &&
272 video_leaky_bucket == o.video_leaky_bucket &&
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000273 cpu_overuse_detection == o.cpu_overuse_detection &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 conference_mode == o.conference_mode &&
275 process_adaptation_threshhold == o.process_adaptation_threshhold &&
276 system_low_adaptation_threshhold ==
277 o.system_low_adaptation_threshhold &&
278 system_high_adaptation_threshhold ==
279 o.system_high_adaptation_threshhold &&
280 buffered_mode_latency == o.buffered_mode_latency;
281 }
282
283 std::string ToString() const {
284 std::ostringstream ost;
285 ost << "VideoOptions {";
286 ost << ToStringIfSet("encoder adaption", adapt_input_to_encoder);
287 ost << ToStringIfSet("cpu adaption", adapt_input_to_cpu_usage);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000288 ost << ToStringIfSet("cpu adaptation smoothing", adapt_cpu_with_smoothing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 ost << ToStringIfSet("adapt view switch", adapt_view_switch);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000290 ost << ToStringIfSet("video adapt third", video_adapt_third);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 ost << ToStringIfSet("noise reduction", video_noise_reduction);
292 ost << ToStringIfSet("3 layers", video_three_layers);
293 ost << ToStringIfSet("camera list", video_enable_camera_list);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000294 ost << ToStringIfSet("1 layer screencast", video_one_layer_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 ost << ToStringIfSet("high bitrate", video_high_bitrate);
296 ost << ToStringIfSet("watermark", video_watermark);
297 ost << ToStringIfSet("video temporal layer screencast",
298 video_temporal_layer_screencast);
299 ost << ToStringIfSet("leaky bucket", video_leaky_bucket);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000300 ost << ToStringIfSet("cpu overuse detection", cpu_overuse_detection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 ost << ToStringIfSet("conference mode", conference_mode);
302 ost << ToStringIfSet("process", process_adaptation_threshhold);
303 ost << ToStringIfSet("low", system_low_adaptation_threshhold);
304 ost << ToStringIfSet("high", system_high_adaptation_threshhold);
305 ost << ToStringIfSet("buffered mode latency", buffered_mode_latency);
306 ost << "}";
307 return ost.str();
308 }
309
310 // Encoder adaption, which is the gd callback in LMI, and TBA in WebRTC.
311 Settable<bool> adapt_input_to_encoder;
312 // Enable CPU adaptation?
313 Settable<bool> adapt_input_to_cpu_usage;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000314 // Enable CPU adaptation smoothing?
315 Settable<bool> adapt_cpu_with_smoothing;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 // Enable Adapt View Switch?
317 Settable<bool> adapt_view_switch;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000318 // Enable video adapt third?
319 Settable<bool> video_adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 // Enable denoising?
321 Settable<bool> video_noise_reduction;
322 // Experimental: Enable multi layer?
323 Settable<bool> video_three_layers;
324 // Experimental: Enable camera list?
325 Settable<bool> video_enable_camera_list;
326 // Experimental: Enable one layer screencast?
327 Settable<bool> video_one_layer_screencast;
328 // Experimental: Enable WebRtc higher bitrate?
329 Settable<bool> video_high_bitrate;
330 // Experimental: Add watermark to the rendered video image.
331 Settable<bool> video_watermark;
332 // Experimental: Enable WebRTC layered screencast.
333 Settable<bool> video_temporal_layer_screencast;
334 // Enable WebRTC leaky bucket when sending media packets.
335 Settable<bool> video_leaky_bucket;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000336 // Enable WebRTC Cpu Overuse Detection, which is a new version of the CPU
337 // adaptation algorithm. So this option will override the
338 // |adapt_input_to_cpu_usage|.
339 Settable<bool> cpu_overuse_detection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 // Use conference mode?
341 Settable<bool> conference_mode;
342 // Threshhold for process cpu adaptation. (Process limit)
343 SettablePercent process_adaptation_threshhold;
344 // Low threshhold for cpu adaptation. (Adapt up)
345 SettablePercent system_low_adaptation_threshhold;
346 // High threshhold for cpu adaptation. (Adapt down)
347 SettablePercent system_high_adaptation_threshhold;
348 // Specify buffered mode latency in milliseconds.
349 Settable<int> buffered_mode_latency;
350};
351
352// A class for playing out soundclips.
353class SoundclipMedia {
354 public:
355 enum SoundclipFlags {
356 SF_LOOP = 1,
357 };
358
359 virtual ~SoundclipMedia() {}
360
361 // Plays a sound out to the speakers with the given audio stream. The stream
362 // must be 16-bit little-endian 16 kHz PCM. If a stream is already playing
363 // on this SoundclipMedia, it is stopped. If clip is NULL, nothing is played.
364 // Returns whether it was successful.
365 virtual bool PlaySound(const char *clip, int len, int flags) = 0;
366};
367
368struct RtpHeaderExtension {
369 RtpHeaderExtension() : id(0) {}
370 RtpHeaderExtension(const std::string& u, int i) : uri(u), id(i) {}
371 std::string uri;
372 int id;
373 // TODO(juberti): SendRecv direction;
374
375 bool operator==(const RtpHeaderExtension& ext) const {
376 // id is a reserved word in objective-c. Therefore the id attribute has to
377 // be a fully qualified name in order to compile on IOS.
378 return this->id == ext.id &&
379 uri == ext.uri;
380 }
381};
382
383// Returns the named header extension if found among all extensions, NULL
384// otherwise.
385inline const RtpHeaderExtension* FindHeaderExtension(
386 const std::vector<RtpHeaderExtension>& extensions,
387 const std::string& name) {
388 for (std::vector<RtpHeaderExtension>::const_iterator it = extensions.begin();
389 it != extensions.end(); ++it) {
390 if (it->uri == name)
391 return &(*it);
392 }
393 return NULL;
394}
395
396enum MediaChannelOptions {
397 // Tune the stream for conference mode.
398 OPT_CONFERENCE = 0x0001
399};
400
401enum VoiceMediaChannelOptions {
402 // Tune the audio stream for vcs with different target levels.
403 OPT_AGC_MINUS_10DB = 0x80000000
404};
405
406// DTMF flags to control if a DTMF tone should be played and/or sent.
407enum DtmfFlags {
408 DF_PLAY = 0x01,
409 DF_SEND = 0x02,
410};
411
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412class MediaChannel : public sigslot::has_slots<> {
413 public:
414 class NetworkInterface {
415 public:
416 enum SocketType { ST_RTP, ST_RTCP };
417 virtual bool SendPacket(talk_base::Buffer* packet) = 0;
418 virtual bool SendRtcp(talk_base::Buffer* packet) = 0;
419 virtual int SetOption(SocketType type, talk_base::Socket::Option opt,
420 int option) = 0;
421 virtual ~NetworkInterface() {}
422 };
423
424 MediaChannel() : network_interface_(NULL) {}
425 virtual ~MediaChannel() {}
426
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000427 // Sets the abstract interface class for sending RTP/RTCP data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 virtual void SetInterface(NetworkInterface *iface) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000429 talk_base::CritScope cs(&network_interface_crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 network_interface_ = iface;
431 }
432
433 // Called when a RTP packet is received.
434 virtual void OnPacketReceived(talk_base::Buffer* packet) = 0;
435 // Called when a RTCP packet is received.
436 virtual void OnRtcpReceived(talk_base::Buffer* packet) = 0;
437 // Called when the socket's ability to send has changed.
438 virtual void OnReadyToSend(bool ready) = 0;
439 // Creates a new outgoing media stream with SSRCs and CNAME as described
440 // by sp.
441 virtual bool AddSendStream(const StreamParams& sp) = 0;
442 // Removes an outgoing media stream.
443 // ssrc must be the first SSRC of the media stream if the stream uses
444 // multiple SSRCs.
445 virtual bool RemoveSendStream(uint32 ssrc) = 0;
446 // Creates a new incoming media stream with SSRCs and CNAME as described
447 // by sp.
448 virtual bool AddRecvStream(const StreamParams& sp) = 0;
449 // Removes an incoming media stream.
450 // ssrc must be the first SSRC of the media stream if the stream uses
451 // multiple SSRCs.
452 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
453
454 // Mutes the channel.
455 virtual bool MuteStream(uint32 ssrc, bool on) = 0;
456
457 // Sets the RTP extension headers and IDs to use when sending RTP.
458 virtual bool SetRecvRtpHeaderExtensions(
459 const std::vector<RtpHeaderExtension>& extensions) = 0;
460 virtual bool SetSendRtpHeaderExtensions(
461 const std::vector<RtpHeaderExtension>& extensions) = 0;
462 // Sets the rate control to use when sending data.
463 virtual bool SetSendBandwidth(bool autobw, int bps) = 0;
464
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000465 // Base method to send packet using NetworkInterface.
466 bool SendPacket(talk_base::Buffer* packet) {
467 return DoSendPacket(packet, false);
468 }
469
470 bool SendRtcp(talk_base::Buffer* packet) {
471 return DoSendPacket(packet, true);
472 }
473
474 int SetOption(NetworkInterface::SocketType type,
475 talk_base::Socket::Option opt,
476 int option) {
477 talk_base::CritScope cs(&network_interface_crit_);
478 if (!network_interface_)
479 return -1;
480
481 return network_interface_->SetOption(type, opt, option);
482 }
483
484 private:
485 bool DoSendPacket(talk_base::Buffer* packet, bool rtcp) {
486 talk_base::CritScope cs(&network_interface_crit_);
487 if (!network_interface_)
488 return false;
489
490 return (!rtcp) ? network_interface_->SendPacket(packet) :
491 network_interface_->SendRtcp(packet);
492 }
493
494 // |network_interface_| can be accessed from the worker_thread and
495 // from any MediaEngine threads. This critical section is to protect accessing
496 // of network_interface_ object.
497 talk_base::CriticalSection network_interface_crit_;
498 NetworkInterface* network_interface_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499};
500
501enum SendFlags {
502 SEND_NOTHING,
503 SEND_RINGBACKTONE,
504 SEND_MICROPHONE
505};
506
507struct VoiceSenderInfo {
508 VoiceSenderInfo()
509 : ssrc(0),
510 bytes_sent(0),
511 packets_sent(0),
512 packets_lost(0),
513 fraction_lost(0.0),
514 ext_seqnum(0),
515 rtt_ms(0),
516 jitter_ms(0),
517 audio_level(0),
518 aec_quality_min(0.0),
519 echo_delay_median_ms(0),
520 echo_delay_std_ms(0),
521 echo_return_loss(0),
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000522 echo_return_loss_enhancement(0),
523 typing_noise_detected(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
525
526 uint32 ssrc;
527 std::string codec_name;
528 int64 bytes_sent;
529 int packets_sent;
530 int packets_lost;
531 float fraction_lost;
532 int ext_seqnum;
533 int rtt_ms;
534 int jitter_ms;
535 int audio_level;
536 float aec_quality_min;
537 int echo_delay_median_ms;
538 int echo_delay_std_ms;
539 int echo_return_loss;
540 int echo_return_loss_enhancement;
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000541 bool typing_noise_detected;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542};
543
544struct VoiceReceiverInfo {
545 VoiceReceiverInfo()
546 : ssrc(0),
547 bytes_rcvd(0),
548 packets_rcvd(0),
549 packets_lost(0),
550 fraction_lost(0.0),
551 ext_seqnum(0),
552 jitter_ms(0),
553 jitter_buffer_ms(0),
554 jitter_buffer_preferred_ms(0),
555 delay_estimate_ms(0),
556 audio_level(0),
557 expand_rate(0) {
558 }
559
560 uint32 ssrc;
561 int64 bytes_rcvd;
562 int packets_rcvd;
563 int packets_lost;
564 float fraction_lost;
565 int ext_seqnum;
566 int jitter_ms;
567 int jitter_buffer_ms;
568 int jitter_buffer_preferred_ms;
569 int delay_estimate_ms;
570 int audio_level;
571 // fraction of synthesized speech inserted through pre-emptive expansion
572 float expand_rate;
573};
574
575struct VideoSenderInfo {
576 VideoSenderInfo()
577 : bytes_sent(0),
578 packets_sent(0),
579 packets_cached(0),
580 packets_lost(0),
581 fraction_lost(0.0),
582 firs_rcvd(0),
583 nacks_rcvd(0),
584 rtt_ms(0),
585 frame_width(0),
586 frame_height(0),
587 framerate_input(0),
588 framerate_sent(0),
589 nominal_bitrate(0),
590 preferred_bitrate(0),
591 adapt_reason(0) {
592 }
593
594 std::vector<uint32> ssrcs;
595 std::vector<SsrcGroup> ssrc_groups;
596 std::string codec_name;
597 int64 bytes_sent;
598 int packets_sent;
599 int packets_cached;
600 int packets_lost;
601 float fraction_lost;
602 int firs_rcvd;
603 int nacks_rcvd;
604 int rtt_ms;
605 int frame_width;
606 int frame_height;
607 int framerate_input;
608 int framerate_sent;
609 int nominal_bitrate;
610 int preferred_bitrate;
611 int adapt_reason;
612};
613
614struct VideoReceiverInfo {
615 VideoReceiverInfo()
616 : bytes_rcvd(0),
617 packets_rcvd(0),
618 packets_lost(0),
619 packets_concealed(0),
620 fraction_lost(0.0),
621 firs_sent(0),
622 nacks_sent(0),
623 frame_width(0),
624 frame_height(0),
625 framerate_rcvd(0),
626 framerate_decoded(0),
627 framerate_output(0),
628 framerate_render_input(0),
629 framerate_render_output(0) {
630 }
631
632 std::vector<uint32> ssrcs;
633 std::vector<SsrcGroup> ssrc_groups;
634 int64 bytes_rcvd;
635 // vector<int> layer_bytes_rcvd;
636 int packets_rcvd;
637 int packets_lost;
638 int packets_concealed;
639 float fraction_lost;
640 int firs_sent;
641 int nacks_sent;
642 int frame_width;
643 int frame_height;
644 int framerate_rcvd;
645 int framerate_decoded;
646 int framerate_output;
647 // Framerate as sent to the renderer.
648 int framerate_render_input;
649 // Framerate that the renderer reports.
650 int framerate_render_output;
651};
652
653struct DataSenderInfo {
654 DataSenderInfo()
655 : ssrc(0),
656 bytes_sent(0),
657 packets_sent(0) {
658 }
659
660 uint32 ssrc;
661 std::string codec_name;
662 int64 bytes_sent;
663 int packets_sent;
664};
665
666struct DataReceiverInfo {
667 DataReceiverInfo()
668 : ssrc(0),
669 bytes_rcvd(0),
670 packets_rcvd(0) {
671 }
672
673 uint32 ssrc;
674 int64 bytes_rcvd;
675 int packets_rcvd;
676};
677
678struct BandwidthEstimationInfo {
679 BandwidthEstimationInfo()
680 : available_send_bandwidth(0),
681 available_recv_bandwidth(0),
682 target_enc_bitrate(0),
683 actual_enc_bitrate(0),
684 retransmit_bitrate(0),
685 transmit_bitrate(0),
686 bucket_delay(0) {
687 }
688
689 int available_send_bandwidth;
690 int available_recv_bandwidth;
691 int target_enc_bitrate;
692 int actual_enc_bitrate;
693 int retransmit_bitrate;
694 int transmit_bitrate;
695 int bucket_delay;
696};
697
698struct VoiceMediaInfo {
699 void Clear() {
700 senders.clear();
701 receivers.clear();
702 }
703 std::vector<VoiceSenderInfo> senders;
704 std::vector<VoiceReceiverInfo> receivers;
705};
706
707struct VideoMediaInfo {
708 void Clear() {
709 senders.clear();
710 receivers.clear();
711 bw_estimations.clear();
712 }
713 std::vector<VideoSenderInfo> senders;
714 std::vector<VideoReceiverInfo> receivers;
715 std::vector<BandwidthEstimationInfo> bw_estimations;
716};
717
718struct DataMediaInfo {
719 void Clear() {
720 senders.clear();
721 receivers.clear();
722 }
723 std::vector<DataSenderInfo> senders;
724 std::vector<DataReceiverInfo> receivers;
725};
726
727class VoiceMediaChannel : public MediaChannel {
728 public:
729 enum Error {
730 ERROR_NONE = 0, // No error.
731 ERROR_OTHER, // Other errors.
732 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
733 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
734 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
735 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
736 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
737 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
738 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
739 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
740 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
741 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
742 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
743 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
744 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
745 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
746 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
747 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
748 };
749
750 VoiceMediaChannel() {}
751 virtual ~VoiceMediaChannel() {}
752 // Sets the codecs/payload types to be used for incoming media.
753 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) = 0;
754 // Sets the codecs/payload types to be used for outgoing media.
755 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) = 0;
756 // Starts or stops playout of received audio.
757 virtual bool SetPlayout(bool playout) = 0;
758 // Starts or stops sending (and potentially capture) of local audio.
759 virtual bool SetSend(SendFlags flag) = 0;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000760 // Sets the renderer object to be used for the specified remote audio stream.
761 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
762 // Sets the renderer object to be used for the specified local audio stream.
763 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 // Gets current energy levels for all incoming streams.
765 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
766 // Get the current energy level of the stream sent to the speaker.
767 virtual int GetOutputLevel() = 0;
768 // Get the time in milliseconds since last recorded keystroke, or negative.
769 virtual int GetTimeSinceLastTyping() = 0;
770 // Temporarily exposed field for tuning typing detect options.
771 virtual void SetTypingDetectionParameters(int time_window,
772 int cost_per_typing, int reporting_threshold, int penalty_decay,
773 int type_event_delay) = 0;
774 // Set left and right scale for speaker output volume of the specified ssrc.
775 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) = 0;
776 // Get left and right scale for speaker output volume of the specified ssrc.
777 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) = 0;
778 // Specifies a ringback tone to be played during call setup.
779 virtual bool SetRingbackTone(const char *buf, int len) = 0;
780 // Plays or stops the aforementioned ringback tone
781 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) = 0;
782 // Returns if the telephone-event has been negotiated.
783 virtual bool CanInsertDtmf() { return false; }
784 // Send and/or play a DTMF |event| according to the |flags|.
785 // The DTMF out-of-band signal will be used on sending.
786 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000787 // The valid value for the |event| are 0 to 15 which corresponding to
788 // DTMF event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) = 0;
790 // Gets quality stats for the channel.
791 virtual bool GetStats(VoiceMediaInfo* info) = 0;
792 // Gets last reported error for this media channel.
793 virtual void GetLastMediaError(uint32* ssrc,
794 VoiceMediaChannel::Error* error) {
795 ASSERT(error != NULL);
796 *error = ERROR_NONE;
797 }
798 // Sets the media options to use.
799 virtual bool SetOptions(const AudioOptions& options) = 0;
800 virtual bool GetOptions(AudioOptions* options) const = 0;
801
802 // Signal errors from MediaChannel. Arguments are:
803 // ssrc(uint32), and error(VoiceMediaChannel::Error).
804 sigslot::signal2<uint32, VoiceMediaChannel::Error> SignalMediaError;
805};
806
807class VideoMediaChannel : public MediaChannel {
808 public:
809 enum Error {
810 ERROR_NONE = 0, // No error.
811 ERROR_OTHER, // Other errors.
812 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
813 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
814 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
815 ERROR_REC_DEVICE_REMOVED, // Device is removed.
816 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
817 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
818 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
819 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
820 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
821 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
822 };
823
824 VideoMediaChannel() : renderer_(NULL) {}
825 virtual ~VideoMediaChannel() {}
826 // Sets the codecs/payload types to be used for incoming media.
827 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) = 0;
828 // Sets the codecs/payload types to be used for outgoing media.
829 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) = 0;
830 // Gets the currently set codecs/payload types to be used for outgoing media.
831 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
832 // Sets the format of a specified outgoing stream.
833 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) = 0;
834 // Starts or stops playout of received video.
835 virtual bool SetRender(bool render) = 0;
836 // Starts or stops transmission (and potentially capture) of local video.
837 virtual bool SetSend(bool send) = 0;
838 // Sets the renderer object to be used for the specified stream.
839 // If SSRC is 0, the renderer is used for the 'default' stream.
840 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) = 0;
841 // If |ssrc| is 0, replace the default capturer (engine capturer) with
842 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
843 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) = 0;
844 // Gets quality stats for the channel.
845 virtual bool GetStats(VideoMediaInfo* info) = 0;
846
847 // Send an intra frame to the receivers.
848 virtual bool SendIntraFrame() = 0;
849 // Reuqest each of the remote senders to send an intra frame.
850 virtual bool RequestIntraFrame() = 0;
851 // Sets the media options to use.
852 virtual bool SetOptions(const VideoOptions& options) = 0;
853 virtual bool GetOptions(VideoOptions* options) const = 0;
854 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) = 0;
855
856 // Signal errors from MediaChannel. Arguments are:
857 // ssrc(uint32), and error(VideoMediaChannel::Error).
858 sigslot::signal2<uint32, Error> SignalMediaError;
859
860 protected:
861 VideoRenderer *renderer_;
862};
863
864enum DataMessageType {
865 // TODO(pthatcher): Make this enum match the SCTP PPIDs that WebRTC uses?
866 DMT_CONTROL = 0,
867 DMT_BINARY = 1,
868 DMT_TEXT = 2,
869};
870
871// Info about data received in DataMediaChannel. For use in
872// DataMediaChannel::SignalDataReceived and in all of the signals that
873// signal fires, on up the chain.
874struct ReceiveDataParams {
875 // The in-packet stream indentifier.
876 // For SCTP, this is really SID, not SSRC.
877 uint32 ssrc;
878 // The type of message (binary, text, or control).
879 DataMessageType type;
880 // A per-stream value incremented per packet in the stream.
881 int seq_num;
882 // A per-stream value monotonically increasing with time.
883 int timestamp;
884
885 ReceiveDataParams() :
886 ssrc(0),
887 type(DMT_TEXT),
888 seq_num(0),
889 timestamp(0) {
890 }
891};
892
893struct SendDataParams {
894 // The in-packet stream indentifier.
895 // For SCTP, this is really SID, not SSRC.
896 uint32 ssrc;
897 // The type of message (binary, text, or control).
898 DataMessageType type;
899
900 // For SCTP, whether to send messages flagged as ordered or not.
901 // If false, messages can be received out of order.
902 bool ordered;
903 // For SCTP, whether the messages are sent reliably or not.
904 // If false, messages may be lost.
905 bool reliable;
906 // For SCTP, if reliable == false, provide partial reliability by
907 // resending up to this many times. Either count or millis
908 // is supported, not both at the same time.
909 int max_rtx_count;
910 // For SCTP, if reliable == false, provide partial reliability by
911 // resending for up to this many milliseconds. Either count or millis
912 // is supported, not both at the same time.
913 int max_rtx_ms;
914
915 SendDataParams() :
916 ssrc(0),
917 type(DMT_TEXT),
918 // TODO(pthatcher): Make these true by default?
919 ordered(false),
920 reliable(false),
921 max_rtx_count(0),
922 max_rtx_ms(0) {
923 }
924};
925
926enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
927
928class DataMediaChannel : public MediaChannel {
929 public:
930 enum Error {
931 ERROR_NONE = 0, // No error.
932 ERROR_OTHER, // Other errors.
933 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
934 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
935 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
936 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
937 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
938 };
939
940 virtual ~DataMediaChannel() {}
941
942 virtual bool SetSendBandwidth(bool autobw, int bps) = 0;
943 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) = 0;
944 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) = 0;
945 virtual bool SetRecvRtpHeaderExtensions(
946 const std::vector<RtpHeaderExtension>& extensions) = 0;
947 virtual bool SetSendRtpHeaderExtensions(
948 const std::vector<RtpHeaderExtension>& extensions) = 0;
949 virtual bool AddSendStream(const StreamParams& sp) = 0;
950 virtual bool RemoveSendStream(uint32 ssrc) = 0;
951 virtual bool AddRecvStream(const StreamParams& sp) = 0;
952 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
953 virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
954 // TODO(pthatcher): Implement this.
955 virtual bool GetStats(DataMediaInfo* info) { return true; }
956
957 virtual bool SetSend(bool send) = 0;
958 virtual bool SetReceive(bool receive) = 0;
959 virtual void OnPacketReceived(talk_base::Buffer* packet) = 0;
960 virtual void OnRtcpReceived(talk_base::Buffer* packet) = 0;
961
962 virtual bool SendData(
963 const SendDataParams& params,
964 const talk_base::Buffer& payload,
965 SendDataResult* result = NULL) = 0;
966 // Signals when data is received (params, data, len)
967 sigslot::signal3<const ReceiveDataParams&,
968 const char*,
969 size_t> SignalDataReceived;
970 // Signal errors from MediaChannel. Arguments are:
971 // ssrc(uint32), and error(DataMediaChannel::Error).
972 sigslot::signal2<uint32, DataMediaChannel::Error> SignalMediaError;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000973 // Signal when the media channel is ready to send the stream. Arguments are:
974 // writable(bool)
975 sigslot::signal1<bool> SignalReadyToSend;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976};
977
978} // namespace cricket
979
980#endif // TALK_MEDIA_BASE_MEDIACHANNEL_H_