blob: 35c3bb04c28ab388cf3c72ebf4ecacaca406639b [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),
522 echo_return_loss_enhancement(0) {
523 }
524
525 uint32 ssrc;
526 std::string codec_name;
527 int64 bytes_sent;
528 int packets_sent;
529 int packets_lost;
530 float fraction_lost;
531 int ext_seqnum;
532 int rtt_ms;
533 int jitter_ms;
534 int audio_level;
535 float aec_quality_min;
536 int echo_delay_median_ms;
537 int echo_delay_std_ms;
538 int echo_return_loss;
539 int echo_return_loss_enhancement;
540};
541
542struct VoiceReceiverInfo {
543 VoiceReceiverInfo()
544 : ssrc(0),
545 bytes_rcvd(0),
546 packets_rcvd(0),
547 packets_lost(0),
548 fraction_lost(0.0),
549 ext_seqnum(0),
550 jitter_ms(0),
551 jitter_buffer_ms(0),
552 jitter_buffer_preferred_ms(0),
553 delay_estimate_ms(0),
554 audio_level(0),
555 expand_rate(0) {
556 }
557
558 uint32 ssrc;
559 int64 bytes_rcvd;
560 int packets_rcvd;
561 int packets_lost;
562 float fraction_lost;
563 int ext_seqnum;
564 int jitter_ms;
565 int jitter_buffer_ms;
566 int jitter_buffer_preferred_ms;
567 int delay_estimate_ms;
568 int audio_level;
569 // fraction of synthesized speech inserted through pre-emptive expansion
570 float expand_rate;
571};
572
573struct VideoSenderInfo {
574 VideoSenderInfo()
575 : bytes_sent(0),
576 packets_sent(0),
577 packets_cached(0),
578 packets_lost(0),
579 fraction_lost(0.0),
580 firs_rcvd(0),
581 nacks_rcvd(0),
582 rtt_ms(0),
583 frame_width(0),
584 frame_height(0),
585 framerate_input(0),
586 framerate_sent(0),
587 nominal_bitrate(0),
588 preferred_bitrate(0),
589 adapt_reason(0) {
590 }
591
592 std::vector<uint32> ssrcs;
593 std::vector<SsrcGroup> ssrc_groups;
594 std::string codec_name;
595 int64 bytes_sent;
596 int packets_sent;
597 int packets_cached;
598 int packets_lost;
599 float fraction_lost;
600 int firs_rcvd;
601 int nacks_rcvd;
602 int rtt_ms;
603 int frame_width;
604 int frame_height;
605 int framerate_input;
606 int framerate_sent;
607 int nominal_bitrate;
608 int preferred_bitrate;
609 int adapt_reason;
610};
611
612struct VideoReceiverInfo {
613 VideoReceiverInfo()
614 : bytes_rcvd(0),
615 packets_rcvd(0),
616 packets_lost(0),
617 packets_concealed(0),
618 fraction_lost(0.0),
619 firs_sent(0),
620 nacks_sent(0),
621 frame_width(0),
622 frame_height(0),
623 framerate_rcvd(0),
624 framerate_decoded(0),
625 framerate_output(0),
626 framerate_render_input(0),
627 framerate_render_output(0) {
628 }
629
630 std::vector<uint32> ssrcs;
631 std::vector<SsrcGroup> ssrc_groups;
632 int64 bytes_rcvd;
633 // vector<int> layer_bytes_rcvd;
634 int packets_rcvd;
635 int packets_lost;
636 int packets_concealed;
637 float fraction_lost;
638 int firs_sent;
639 int nacks_sent;
640 int frame_width;
641 int frame_height;
642 int framerate_rcvd;
643 int framerate_decoded;
644 int framerate_output;
645 // Framerate as sent to the renderer.
646 int framerate_render_input;
647 // Framerate that the renderer reports.
648 int framerate_render_output;
649};
650
651struct DataSenderInfo {
652 DataSenderInfo()
653 : ssrc(0),
654 bytes_sent(0),
655 packets_sent(0) {
656 }
657
658 uint32 ssrc;
659 std::string codec_name;
660 int64 bytes_sent;
661 int packets_sent;
662};
663
664struct DataReceiverInfo {
665 DataReceiverInfo()
666 : ssrc(0),
667 bytes_rcvd(0),
668 packets_rcvd(0) {
669 }
670
671 uint32 ssrc;
672 int64 bytes_rcvd;
673 int packets_rcvd;
674};
675
676struct BandwidthEstimationInfo {
677 BandwidthEstimationInfo()
678 : available_send_bandwidth(0),
679 available_recv_bandwidth(0),
680 target_enc_bitrate(0),
681 actual_enc_bitrate(0),
682 retransmit_bitrate(0),
683 transmit_bitrate(0),
684 bucket_delay(0) {
685 }
686
687 int available_send_bandwidth;
688 int available_recv_bandwidth;
689 int target_enc_bitrate;
690 int actual_enc_bitrate;
691 int retransmit_bitrate;
692 int transmit_bitrate;
693 int bucket_delay;
694};
695
696struct VoiceMediaInfo {
697 void Clear() {
698 senders.clear();
699 receivers.clear();
700 }
701 std::vector<VoiceSenderInfo> senders;
702 std::vector<VoiceReceiverInfo> receivers;
703};
704
705struct VideoMediaInfo {
706 void Clear() {
707 senders.clear();
708 receivers.clear();
709 bw_estimations.clear();
710 }
711 std::vector<VideoSenderInfo> senders;
712 std::vector<VideoReceiverInfo> receivers;
713 std::vector<BandwidthEstimationInfo> bw_estimations;
714};
715
716struct DataMediaInfo {
717 void Clear() {
718 senders.clear();
719 receivers.clear();
720 }
721 std::vector<DataSenderInfo> senders;
722 std::vector<DataReceiverInfo> receivers;
723};
724
725class VoiceMediaChannel : public MediaChannel {
726 public:
727 enum Error {
728 ERROR_NONE = 0, // No error.
729 ERROR_OTHER, // Other errors.
730 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open mic.
731 ERROR_REC_DEVICE_MUTED, // Mic was muted by OS.
732 ERROR_REC_DEVICE_SILENT, // No background noise picked up.
733 ERROR_REC_DEVICE_SATURATION, // Mic input is clipping.
734 ERROR_REC_DEVICE_REMOVED, // Mic was removed while active.
735 ERROR_REC_RUNTIME_ERROR, // Processing is encountering errors.
736 ERROR_REC_SRTP_ERROR, // Generic SRTP failure.
737 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
738 ERROR_REC_TYPING_NOISE_DETECTED, // Typing noise is detected.
739 ERROR_PLAY_DEVICE_OPEN_FAILED = 200, // Could not open playout.
740 ERROR_PLAY_DEVICE_MUTED, // Playout muted by OS.
741 ERROR_PLAY_DEVICE_REMOVED, // Playout removed while active.
742 ERROR_PLAY_RUNTIME_ERROR, // Errors in voice processing.
743 ERROR_PLAY_SRTP_ERROR, // Generic SRTP failure.
744 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
745 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
746 };
747
748 VoiceMediaChannel() {}
749 virtual ~VoiceMediaChannel() {}
750 // Sets the codecs/payload types to be used for incoming media.
751 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) = 0;
752 // Sets the codecs/payload types to be used for outgoing media.
753 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) = 0;
754 // Starts or stops playout of received audio.
755 virtual bool SetPlayout(bool playout) = 0;
756 // Starts or stops sending (and potentially capture) of local audio.
757 virtual bool SetSend(SendFlags flag) = 0;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000758 // Sets the renderer object to be used for the specified remote audio stream.
759 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
760 // Sets the renderer object to be used for the specified local audio stream.
761 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 // Gets current energy levels for all incoming streams.
763 virtual bool GetActiveStreams(AudioInfo::StreamList* actives) = 0;
764 // Get the current energy level of the stream sent to the speaker.
765 virtual int GetOutputLevel() = 0;
766 // Get the time in milliseconds since last recorded keystroke, or negative.
767 virtual int GetTimeSinceLastTyping() = 0;
768 // Temporarily exposed field for tuning typing detect options.
769 virtual void SetTypingDetectionParameters(int time_window,
770 int cost_per_typing, int reporting_threshold, int penalty_decay,
771 int type_event_delay) = 0;
772 // Set left and right scale for speaker output volume of the specified ssrc.
773 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) = 0;
774 // Get left and right scale for speaker output volume of the specified ssrc.
775 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) = 0;
776 // Specifies a ringback tone to be played during call setup.
777 virtual bool SetRingbackTone(const char *buf, int len) = 0;
778 // Plays or stops the aforementioned ringback tone
779 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) = 0;
780 // Returns if the telephone-event has been negotiated.
781 virtual bool CanInsertDtmf() { return false; }
782 // Send and/or play a DTMF |event| according to the |flags|.
783 // The DTMF out-of-band signal will be used on sending.
784 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000785 // The valid value for the |event| are 0 to 15 which corresponding to
786 // DTMF event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 virtual bool InsertDtmf(uint32 ssrc, int event, int duration, int flags) = 0;
788 // Gets quality stats for the channel.
789 virtual bool GetStats(VoiceMediaInfo* info) = 0;
790 // Gets last reported error for this media channel.
791 virtual void GetLastMediaError(uint32* ssrc,
792 VoiceMediaChannel::Error* error) {
793 ASSERT(error != NULL);
794 *error = ERROR_NONE;
795 }
796 // Sets the media options to use.
797 virtual bool SetOptions(const AudioOptions& options) = 0;
798 virtual bool GetOptions(AudioOptions* options) const = 0;
799
800 // Signal errors from MediaChannel. Arguments are:
801 // ssrc(uint32), and error(VoiceMediaChannel::Error).
802 sigslot::signal2<uint32, VoiceMediaChannel::Error> SignalMediaError;
803};
804
805class VideoMediaChannel : public MediaChannel {
806 public:
807 enum Error {
808 ERROR_NONE = 0, // No error.
809 ERROR_OTHER, // Other errors.
810 ERROR_REC_DEVICE_OPEN_FAILED = 100, // Could not open camera.
811 ERROR_REC_DEVICE_NO_DEVICE, // No camera.
812 ERROR_REC_DEVICE_IN_USE, // Device is in already use.
813 ERROR_REC_DEVICE_REMOVED, // Device is removed.
814 ERROR_REC_SRTP_ERROR, // Generic sender SRTP failure.
815 ERROR_REC_SRTP_AUTH_FAILED, // Failed to authenticate packets.
816 ERROR_REC_CPU_MAX_CANT_DOWNGRADE, // Can't downgrade capture anymore.
817 ERROR_PLAY_SRTP_ERROR = 200, // Generic receiver SRTP failure.
818 ERROR_PLAY_SRTP_AUTH_FAILED, // Failed to authenticate packets.
819 ERROR_PLAY_SRTP_REPLAY, // Packet replay detected.
820 };
821
822 VideoMediaChannel() : renderer_(NULL) {}
823 virtual ~VideoMediaChannel() {}
824 // Sets the codecs/payload types to be used for incoming media.
825 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) = 0;
826 // Sets the codecs/payload types to be used for outgoing media.
827 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) = 0;
828 // Gets the currently set codecs/payload types to be used for outgoing media.
829 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
830 // Sets the format of a specified outgoing stream.
831 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) = 0;
832 // Starts or stops playout of received video.
833 virtual bool SetRender(bool render) = 0;
834 // Starts or stops transmission (and potentially capture) of local video.
835 virtual bool SetSend(bool send) = 0;
836 // Sets the renderer object to be used for the specified stream.
837 // If SSRC is 0, the renderer is used for the 'default' stream.
838 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* renderer) = 0;
839 // If |ssrc| is 0, replace the default capturer (engine capturer) with
840 // |capturer|. If |ssrc| is non zero create a new stream with |ssrc| as SSRC.
841 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) = 0;
842 // Gets quality stats for the channel.
843 virtual bool GetStats(VideoMediaInfo* info) = 0;
844
845 // Send an intra frame to the receivers.
846 virtual bool SendIntraFrame() = 0;
847 // Reuqest each of the remote senders to send an intra frame.
848 virtual bool RequestIntraFrame() = 0;
849 // Sets the media options to use.
850 virtual bool SetOptions(const VideoOptions& options) = 0;
851 virtual bool GetOptions(VideoOptions* options) const = 0;
852 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) = 0;
853
854 // Signal errors from MediaChannel. Arguments are:
855 // ssrc(uint32), and error(VideoMediaChannel::Error).
856 sigslot::signal2<uint32, Error> SignalMediaError;
857
858 protected:
859 VideoRenderer *renderer_;
860};
861
862enum DataMessageType {
863 // TODO(pthatcher): Make this enum match the SCTP PPIDs that WebRTC uses?
864 DMT_CONTROL = 0,
865 DMT_BINARY = 1,
866 DMT_TEXT = 2,
867};
868
869// Info about data received in DataMediaChannel. For use in
870// DataMediaChannel::SignalDataReceived and in all of the signals that
871// signal fires, on up the chain.
872struct ReceiveDataParams {
873 // The in-packet stream indentifier.
874 // For SCTP, this is really SID, not SSRC.
875 uint32 ssrc;
876 // The type of message (binary, text, or control).
877 DataMessageType type;
878 // A per-stream value incremented per packet in the stream.
879 int seq_num;
880 // A per-stream value monotonically increasing with time.
881 int timestamp;
882
883 ReceiveDataParams() :
884 ssrc(0),
885 type(DMT_TEXT),
886 seq_num(0),
887 timestamp(0) {
888 }
889};
890
891struct SendDataParams {
892 // The in-packet stream indentifier.
893 // For SCTP, this is really SID, not SSRC.
894 uint32 ssrc;
895 // The type of message (binary, text, or control).
896 DataMessageType type;
897
898 // For SCTP, whether to send messages flagged as ordered or not.
899 // If false, messages can be received out of order.
900 bool ordered;
901 // For SCTP, whether the messages are sent reliably or not.
902 // If false, messages may be lost.
903 bool reliable;
904 // For SCTP, if reliable == false, provide partial reliability by
905 // resending up to this many times. Either count or millis
906 // is supported, not both at the same time.
907 int max_rtx_count;
908 // For SCTP, if reliable == false, provide partial reliability by
909 // resending for up to this many milliseconds. Either count or millis
910 // is supported, not both at the same time.
911 int max_rtx_ms;
912
913 SendDataParams() :
914 ssrc(0),
915 type(DMT_TEXT),
916 // TODO(pthatcher): Make these true by default?
917 ordered(false),
918 reliable(false),
919 max_rtx_count(0),
920 max_rtx_ms(0) {
921 }
922};
923
924enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
925
926class DataMediaChannel : public MediaChannel {
927 public:
928 enum Error {
929 ERROR_NONE = 0, // No error.
930 ERROR_OTHER, // Other errors.
931 ERROR_SEND_SRTP_ERROR = 200, // Generic SRTP failure.
932 ERROR_SEND_SRTP_AUTH_FAILED, // Failed to authenticate packets.
933 ERROR_RECV_SRTP_ERROR, // Generic SRTP failure.
934 ERROR_RECV_SRTP_AUTH_FAILED, // Failed to authenticate packets.
935 ERROR_RECV_SRTP_REPLAY, // Packet replay detected.
936 };
937
938 virtual ~DataMediaChannel() {}
939
940 virtual bool SetSendBandwidth(bool autobw, int bps) = 0;
941 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) = 0;
942 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) = 0;
943 virtual bool SetRecvRtpHeaderExtensions(
944 const std::vector<RtpHeaderExtension>& extensions) = 0;
945 virtual bool SetSendRtpHeaderExtensions(
946 const std::vector<RtpHeaderExtension>& extensions) = 0;
947 virtual bool AddSendStream(const StreamParams& sp) = 0;
948 virtual bool RemoveSendStream(uint32 ssrc) = 0;
949 virtual bool AddRecvStream(const StreamParams& sp) = 0;
950 virtual bool RemoveRecvStream(uint32 ssrc) = 0;
951 virtual bool MuteStream(uint32 ssrc, bool on) { return false; }
952 // TODO(pthatcher): Implement this.
953 virtual bool GetStats(DataMediaInfo* info) { return true; }
954
955 virtual bool SetSend(bool send) = 0;
956 virtual bool SetReceive(bool receive) = 0;
957 virtual void OnPacketReceived(talk_base::Buffer* packet) = 0;
958 virtual void OnRtcpReceived(talk_base::Buffer* packet) = 0;
959
960 virtual bool SendData(
961 const SendDataParams& params,
962 const talk_base::Buffer& payload,
963 SendDataResult* result = NULL) = 0;
964 // Signals when data is received (params, data, len)
965 sigslot::signal3<const ReceiveDataParams&,
966 const char*,
967 size_t> SignalDataReceived;
968 // Signal errors from MediaChannel. Arguments are:
969 // ssrc(uint32), and error(DataMediaChannel::Error).
970 sigslot::signal2<uint32, DataMediaChannel::Error> SignalMediaError;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000971 // Signal when the media channel is ready to send the stream. Arguments are:
972 // writable(bool)
973 sigslot::signal1<bool> SignalReadyToSend;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974};
975
976} // namespace cricket
977
978#endif // TALK_MEDIA_BASE_MEDIACHANNEL_H_