blob: 7e96fe746624ff87c8ddd7d3ca88c1808056efa3 [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_SESSION_MEDIA_CHANNEL_H_
29#define TALK_SESSION_MEDIA_CHANNEL_H_
30
31#include <string>
32#include <vector>
33
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/media/base/mediachannel.h"
35#include "talk/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/media/base/streamparams.h"
37#include "talk/media/base/videocapturer.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038#include "webrtc/p2p/base/session.h"
39#include "webrtc/p2p/client/socketmonitor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000041#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/session/media/mediamonitor.h"
43#include "talk/session/media/mediasession.h"
44#include "talk/session/media/rtcpmuxfilter.h"
45#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "webrtc/base/asyncudpsocket.h"
47#include "webrtc/base/criticalsection.h"
48#include "webrtc/base/network.h"
49#include "webrtc/base/sigslot.h"
50#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52namespace cricket {
53
54struct CryptoParams;
55class MediaContentDescription;
56struct TypingMonitorOptions;
57class TypingMonitor;
58struct ViewRequest;
59
60enum SinkType {
61 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
62 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
63};
64
65// BaseChannel contains logic common to voice and video, including
66// enable/mute, marshaling calls to a worker thread, and
67// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000068//
69// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
70// This is required to avoid a data race between the destructor modifying the
71// vtable, and the media channel's thread using BaseChannel as the
72// NetworkInterface.
73
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000076 public MediaChannel::NetworkInterface,
77 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 BaseChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 MediaChannel* channel, BaseSession* session,
81 const std::string& content_name, bool rtcp);
82 virtual ~BaseChannel();
83 bool Init(TransportChannel* transport_channel,
84 TransportChannel* rtcp_transport_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +000085 // Deinit may be called multiple times and is simply ignored if it's alreay
86 // done.
87 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 BaseSession* session() const { return session_; }
91 const std::string& content_name() { return content_name_; }
92 TransportChannel* transport_channel() const {
93 return transport_channel_;
94 }
95 TransportChannel* rtcp_transport_channel() const {
96 return rtcp_transport_channel_;
97 }
98 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100 // This function returns true if we are using SRTP.
101 bool secure() const { return srtp_filter_.IsActive(); }
102 // The following function returns true if we are using
103 // DTLS-based keying. If you turned off SRTP later, however
104 // you could have secure() == false and dtls_secure() == true.
105 bool secure_dtls() const { return dtls_keyed_; }
106 // This function returns true if we require secure channel for call setup.
107 bool secure_required() const { return secure_required_; }
108
109 bool writable() const { return writable_; }
110 bool IsStreamMuted(uint32 ssrc);
111
112 // Channel control
113 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000114 ContentAction action,
115 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000117 ContentAction action,
118 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
120 bool Enable(bool enable);
121 // Mute sending media on the stream with SSRC |ssrc|
122 // If there is only one sending stream SSRC 0 can be used.
123 bool MuteStream(uint32 ssrc, bool mute);
124
125 // Multiplexing
126 bool AddRecvStream(const StreamParams& sp);
127 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000128 bool AddSendStream(const StreamParams& sp);
129 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 // Monitoring
132 void StartConnectionMonitor(int cms);
133 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000134 // For ConnectionStatsGetter, used by ConnectionMonitor
135 virtual bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
137 void set_srtp_signal_silent_time(uint32 silent_time) {
138 srtp_filter_.set_signal_silent_time(silent_time);
139 }
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 template <class T>
142 void RegisterSendSink(T* sink,
143 void (T::*OnPacket)(const void*, size_t, bool),
144 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000145 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 if (SINK_POST_CRYPTO == type) {
147 SignalSendPacketPostCrypto.disconnect(sink);
148 SignalSendPacketPostCrypto.connect(sink, OnPacket);
149 } else {
150 SignalSendPacketPreCrypto.disconnect(sink);
151 SignalSendPacketPreCrypto.connect(sink, OnPacket);
152 }
153 }
154
155 void UnregisterSendSink(sigslot::has_slots<>* sink,
156 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000157 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 if (SINK_POST_CRYPTO == type) {
159 SignalSendPacketPostCrypto.disconnect(sink);
160 } else {
161 SignalSendPacketPreCrypto.disconnect(sink);
162 }
163 }
164
165 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000166 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 if (SINK_POST_CRYPTO == type) {
168 return !SignalSendPacketPostCrypto.is_empty();
169 } else {
170 return !SignalSendPacketPreCrypto.is_empty();
171 }
172 }
173
174 template <class T>
175 void RegisterRecvSink(T* sink,
176 void (T::*OnPacket)(const void*, size_t, bool),
177 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000178 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 if (SINK_POST_CRYPTO == type) {
180 SignalRecvPacketPostCrypto.disconnect(sink);
181 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
182 } else {
183 SignalRecvPacketPreCrypto.disconnect(sink);
184 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
185 }
186 }
187
188 void UnregisterRecvSink(sigslot::has_slots<>* sink,
189 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000190 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 if (SINK_POST_CRYPTO == type) {
192 SignalRecvPacketPostCrypto.disconnect(sink);
193 } else {
194 SignalRecvPacketPreCrypto.disconnect(sink);
195 }
196 }
197
198 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000199 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 if (SINK_POST_CRYPTO == type) {
201 return !SignalRecvPacketPostCrypto.is_empty();
202 } else {
203 return !SignalRecvPacketPreCrypto.is_empty();
204 }
205 }
206
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000207 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 const std::vector<StreamParams>& local_streams() const {
210 return local_streams_;
211 }
212 const std::vector<StreamParams>& remote_streams() const {
213 return remote_streams_;
214 }
215
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000216 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
217 void SignalDtlsSetupFailure_w(bool rtcp);
218 void SignalDtlsSetupFailure_s(bool rtcp);
219
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000220 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
222
223 // Used to alert UI when the muted status changes, perhaps autonomously.
224 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
225
226 // Made public for easier testing.
227 void SetReadyToSend(TransportChannel* channel, bool ready);
228
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000229 // Only public for unit tests. Otherwise, consider protected.
230 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
231
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 protected:
233 MediaEngineInterface* media_engine() const { return media_engine_; }
234 virtual MediaChannel* media_channel() const { return media_channel_; }
235 void set_rtcp_transport_channel(TransportChannel* transport);
236 bool was_ever_writable() const { return was_ever_writable_; }
237 void set_local_content_direction(MediaContentDirection direction) {
238 local_content_direction_ = direction;
239 }
240 void set_remote_content_direction(MediaContentDirection direction) {
241 remote_content_direction_ = direction;
242 }
243 bool IsReadyToReceive() const;
244 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000245 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 SrtpFilter* srtp_filter() { return &srtp_filter_; }
247 bool rtcp() const { return rtcp_; }
248
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 void FlushRtcpMessages();
250
251 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 virtual bool SendPacket(rtc::Buffer* packet,
253 rtc::DiffServCodePoint dscp);
254 virtual bool SendRtcp(rtc::Buffer* packet,
255 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
257 // From TransportChannel
258 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000259 virtual void OnChannelRead(TransportChannel* channel,
260 const char* data,
261 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000263 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 void OnReadyToSend(TransportChannel* channel);
265
266 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
267 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268 bool SendPacket(bool rtcp, rtc::Buffer* packet,
269 rtc::DiffServCodePoint dscp);
270 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
271 void HandlePacket(bool rtcp, rtc::Buffer* packet,
272 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
274 // Apply the new local/remote session description.
275 void OnNewLocalDescription(BaseSession* session, ContentAction action);
276 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
277
278 void EnableMedia_w();
279 void DisableMedia_w();
280 virtual bool MuteStream_w(uint32 ssrc, bool mute);
281 bool IsStreamMuted_w(uint32 ssrc);
282 void ChannelWritable_w();
283 void ChannelNotWritable_w();
284 bool AddRecvStream_w(const StreamParams& sp);
285 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000286 bool AddSendStream_w(const StreamParams& sp);
287 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 virtual bool ShouldSetupDtlsSrtp() const;
289 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
290 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
291 bool SetupDtlsSrtp(bool rtcp_channel);
292 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
293 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
294
295 virtual void ChangeState() = 0;
296
297 // Gets the content info appropriate to the channel (audio or video).
298 virtual const ContentInfo* GetFirstContent(
299 const SessionDescription* sdesc) = 0;
300 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000301 ContentAction action,
302 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000304 ContentAction action,
305 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000307 ContentAction action,
308 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000310 ContentAction action,
311 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313 ContentAction action,
314 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000316 ContentAction action,
317 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000319 // Helper method to get RTP Absoulute SendTime extension header id if
320 // present in remote supported extensions list.
321 void MaybeCacheRtpAbsSendTimeHeaderExtension(
322 const std::vector<RtpHeaderExtension>& extensions);
323
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000324 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
325 MediaChannel* media_channel,
326 std::string* error_desc);
327 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
328 MediaChannel* media_channel,
329 std::string* error_desc);
330
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
332 bool* dtls,
333 std::string* error_desc);
334 bool SetSrtp_w(const std::vector<CryptoParams>& params,
335 ContentAction action,
336 ContentSource src,
337 std::string* error_desc);
338 bool SetRtcpMux_w(bool enable,
339 ContentAction action,
340 ContentSource src,
341 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342
343 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000344 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345
346 // Handled in derived classes
347 // Get the SRTP ciphers to use for RTP media
348 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000349 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 const std::vector<ConnectionInfo>& infos) = 0;
351
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000352 // Helper function for invoking bool-returning methods on the worker thread.
353 template <class FunctorT>
354 bool InvokeOnWorker(const FunctorT& functor) {
355 return worker_thread_->Invoke<bool>(functor);
356 }
357
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 private:
359 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
360 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
361 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
362 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000363 rtc::CriticalSection signal_send_packet_cs_;
364 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 MediaEngineInterface* media_engine_;
368 BaseSession* session_;
369 MediaChannel* media_channel_;
370 std::vector<StreamParams> local_streams_;
371 std::vector<StreamParams> remote_streams_;
372
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000373 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 bool rtcp_;
375 TransportChannel* transport_channel_;
376 TransportChannel* rtcp_transport_channel_;
377 SrtpFilter srtp_filter_;
378 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000379 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000380 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 bool enabled_;
382 bool writable_;
383 bool rtp_ready_to_send_;
384 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 bool was_ever_writable_;
386 MediaContentDirection local_content_direction_;
387 MediaContentDirection remote_content_direction_;
388 std::set<uint32> muted_streams_;
389 bool has_received_packet_;
390 bool dtls_keyed_;
391 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000392 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393};
394
395// VoiceChannel is a specialization that adds support for early media, DTMF,
396// and input/output level monitoring.
397class VoiceChannel : public BaseChannel {
398 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000399 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 VoiceMediaChannel* channel, BaseSession* session,
401 const std::string& content_name, bool rtcp);
402 ~VoiceChannel();
403 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000404 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
405 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406
407 // downcasts a MediaChannel
408 virtual VoiceMediaChannel* media_channel() const {
409 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
410 }
411
412 bool SetRingbackTone(const void* buf, int len);
413 void SetEarlyMedia(bool enable);
414 // This signal is emitted when we have gone a period of time without
415 // receiving early media. When received, a UI should start playing its
416 // own ringing sound
417 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
418
419 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
420 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
421 bool PressDTMF(int digit, bool playout);
422 // Returns if the telephone-event has been negotiated.
423 bool CanInsertDtmf();
424 // Send and/or play a DTMF |event| according to the |flags|.
425 // The DTMF out-of-band signal will be used on sending.
426 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000427 // The valid value for the |event| are 0 which corresponding to DTMF
428 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
430 bool SetOutputScaling(uint32 ssrc, double left, double right);
431 // Get statistics about the current media session.
432 bool GetStats(VoiceMediaInfo* stats);
433
434 // Monitoring functions
435 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
436 SignalConnectionMonitor;
437
438 void StartMediaMonitor(int cms);
439 void StopMediaMonitor();
440 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
441
442 void StartAudioMonitor(int cms);
443 void StopAudioMonitor();
444 bool IsAudioMonitorRunning() const;
445 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
446
447 void StartTypingMonitor(const TypingMonitorOptions& settings);
448 void StopTypingMonitor();
449 bool IsTypingMonitorRunning() const;
450
451 // Overrides BaseChannel::MuteStream_w.
452 virtual bool MuteStream_w(uint32 ssrc, bool mute);
453
454 int GetInputLevel_w();
455 int GetOutputLevel_w();
456 void GetActiveStreams_w(AudioInfo::StreamList* actives);
457
458 // Signal errors from VoiceMediaChannel. Arguments are:
459 // ssrc(uint32), and error(VoiceMediaChannel::Error).
460 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
461 SignalMediaError;
462
463 // Configuration and setting.
464 bool SetChannelOptions(const AudioOptions& options);
465
466 private:
467 // overrides from BaseChannel
468 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000469 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000470 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000471 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 virtual void ChangeState();
473 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
474 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000475 ContentAction action,
476 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000478 ContentAction action,
479 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 bool SetRingbackTone_w(const void* buf, int len);
481 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
482 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
484 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
485 bool GetStats_w(VoiceMediaInfo* stats);
486
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
489 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000490 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 virtual void OnMediaMonitorUpdate(
492 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
493 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
494 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
495 void SendLastMediaError();
496 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497
498 static const int kEarlyMediaTimeout = 1000;
499 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000500 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
501 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
502 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503};
504
505// VideoChannel is a specialization for video.
506class VideoChannel : public BaseChannel {
507 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000508 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 VideoMediaChannel* channel, BaseSession* session,
510 const std::string& content_name, bool rtcp,
511 VoiceChannel* voice_channel);
512 ~VideoChannel();
513 bool Init();
514
515 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
516 bool ApplyViewRequest(const ViewRequest& request);
517
518 // TODO(pthatcher): Refactor to use a "capture id" instead of an
519 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000520 // Passes ownership of the capturer to the channel.
521 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
523 bool RemoveScreencast(uint32 ssrc);
524 // True if we've added a screencast. Doesn't matter if the capturer
525 // has been started or not.
526 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000527 int GetScreencastFps(uint32 ssrc);
528 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000530 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
532 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
533 SignalConnectionMonitor;
534
535 void StartMediaMonitor(int cms);
536 void StopMediaMonitor();
537 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539
540 bool SendIntraFrame();
541 bool RequestIntraFrame();
542 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
543 SignalMediaError;
544
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 // Configuration and setting.
546 bool SetChannelOptions(const VideoOptions& options);
547
548 protected:
549 // downcasts a MediaChannel
550 virtual VideoMediaChannel* media_channel() const {
551 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
552 }
553
554 private:
555 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000556 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
558 // overrides from BaseChannel
559 virtual void ChangeState();
560 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
561 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000562 ContentAction action,
563 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000565 ContentAction action,
566 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000569 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000571 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000573 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 bool GetStats_w(VideoMediaInfo* stats);
575
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
578 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000579 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 virtual void OnMediaMonitorUpdate(
581 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
582 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000583 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
585 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
586
587 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
588 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589
590 VoiceChannel* voice_channel_;
591 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000593 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596};
597
598// DataChannel is a specialization for data.
599class DataChannel : public BaseChannel {
600 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000601 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 DataMediaChannel* media_channel,
603 BaseSession* session,
604 const std::string& content_name,
605 bool rtcp);
606 ~DataChannel();
607 bool Init();
608
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000609 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000610 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000611 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612
613 void StartMediaMonitor(int cms);
614 void StopMediaMonitor();
615
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000616 // Should be called on the signaling thread only.
617 bool ready_to_send_data() const {
618 return ready_to_send_data_;
619 }
620
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
622 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
623 SignalConnectionMonitor;
624 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
625 SignalMediaError;
626 sigslot::signal3<DataChannel*,
627 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000628 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 SignalDataReceived;
630 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000631 // That occurs when the channel is enabled, the transport is writable,
632 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000634 // Signal for notifying that the remote side has closed the DataChannel.
635 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000637 protected:
638 // downcasts a MediaChannel.
639 virtual DataMediaChannel* media_channel() const {
640 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
641 }
642
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000644 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000646 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 SendDataResult* result)
648 : params(params),
649 payload(payload),
650 result(result),
651 succeeded(false) {
652 }
653
654 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000655 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 SendDataResult* result;
657 bool succeeded;
658 };
659
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000660 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 // We copy the data because the data will become invalid after we
662 // handle DataMediaChannel::SignalDataReceived but before we fire
663 // SignalDataReceived.
664 DataReceivedMessageData(
665 const ReceiveDataParams& params, const char* data, size_t len)
666 : params(params),
667 payload(data, len) {
668 }
669 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000670 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 };
672
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000673 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000674
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 // overrides from BaseChannel
676 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
677 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
678 // it's the same as what was set previously. Returns false if it's
679 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000680 bool SetDataChannelType(DataChannelType new_data_channel_type,
681 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 // Same as SetDataChannelType, but extracts the type from the
683 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000684 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
685 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000687 ContentAction action,
688 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000690 ContentAction action,
691 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000693 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000695 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
697 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000698 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 virtual void OnMediaMonitorUpdate(
700 DataMediaChannel* media_channel, const DataMediaInfo& info);
701 virtual bool ShouldSetupDtlsSrtp() const;
702 void OnDataReceived(
703 const ReceiveDataParams& params, const char* data, size_t len);
704 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000705 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000707 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000709 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 // TODO(pthatcher): Make a separate SctpDataChannel and
711 // RtpDataChannel instead of using this.
712 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000713 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714};
715
716} // namespace cricket
717
718#endif // TALK_SESSION_MEDIA_CHANNEL_H_