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