blob: 904777e1754a2db5d2fea525b7aca066fc713c11 [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();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000083 bool Init();
wu@webrtc.org78187522013-10-07 23:32:02 +000084 // Deinit may be called multiple times and is simply ignored if it's alreay
85 // done.
86 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 BaseSession* session() const { return session_; }
90 const std::string& content_name() { return content_name_; }
91 TransportChannel* transport_channel() const {
92 return transport_channel_;
93 }
94 TransportChannel* rtcp_transport_channel() const {
95 return rtcp_transport_channel_;
96 }
97 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
99 // This function returns true if we are using SRTP.
100 bool secure() const { return srtp_filter_.IsActive(); }
101 // The following function returns true if we are using
102 // DTLS-based keying. If you turned off SRTP later, however
103 // you could have secure() == false and dtls_secure() == true.
104 bool secure_dtls() const { return dtls_keyed_; }
105 // This function returns true if we require secure channel for call setup.
106 bool secure_required() const { return secure_required_; }
107
108 bool writable() const { return writable_; }
109 bool IsStreamMuted(uint32 ssrc);
110
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700111 // Activate RTCP mux, regardless of the state so far. Once
112 // activated, it can not be deactivated, and if the remote
113 // description doesn't support RTCP mux, setting the remote
114 // description will fail.
115 void ActivateRtcpMux();
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000116 bool PushdownLocalDescription(const SessionDescription* local_desc,
117 ContentAction action,
118 std::string* error_desc);
119 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
120 ContentAction action,
121 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 // Channel control
123 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000124 ContentAction action,
125 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000127 ContentAction action,
128 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 bool Enable(bool enable);
131 // Mute sending media on the stream with SSRC |ssrc|
132 // If there is only one sending stream SSRC 0 can be used.
133 bool MuteStream(uint32 ssrc, bool mute);
134
135 // Multiplexing
136 bool AddRecvStream(const StreamParams& sp);
137 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000138 bool AddSendStream(const StreamParams& sp);
139 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
141 // Monitoring
142 void StartConnectionMonitor(int cms);
143 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000144 // For ConnectionStatsGetter, used by ConnectionMonitor
145 virtual bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
147 void set_srtp_signal_silent_time(uint32 silent_time) {
148 srtp_filter_.set_signal_silent_time(silent_time);
149 }
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 template <class T>
152 void RegisterSendSink(T* sink,
153 void (T::*OnPacket)(const void*, size_t, bool),
154 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000155 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 if (SINK_POST_CRYPTO == type) {
157 SignalSendPacketPostCrypto.disconnect(sink);
158 SignalSendPacketPostCrypto.connect(sink, OnPacket);
159 } else {
160 SignalSendPacketPreCrypto.disconnect(sink);
161 SignalSendPacketPreCrypto.connect(sink, OnPacket);
162 }
163 }
164
165 void UnregisterSendSink(sigslot::has_slots<>* sink,
166 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000167 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 if (SINK_POST_CRYPTO == type) {
169 SignalSendPacketPostCrypto.disconnect(sink);
170 } else {
171 SignalSendPacketPreCrypto.disconnect(sink);
172 }
173 }
174
175 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 if (SINK_POST_CRYPTO == type) {
178 return !SignalSendPacketPostCrypto.is_empty();
179 } else {
180 return !SignalSendPacketPreCrypto.is_empty();
181 }
182 }
183
184 template <class T>
185 void RegisterRecvSink(T* sink,
186 void (T::*OnPacket)(const void*, size_t, bool),
187 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 if (SINK_POST_CRYPTO == type) {
190 SignalRecvPacketPostCrypto.disconnect(sink);
191 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
192 } else {
193 SignalRecvPacketPreCrypto.disconnect(sink);
194 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
195 }
196 }
197
198 void UnregisterRecvSink(sigslot::has_slots<>* sink,
199 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 if (SINK_POST_CRYPTO == type) {
202 SignalRecvPacketPostCrypto.disconnect(sink);
203 } else {
204 SignalRecvPacketPreCrypto.disconnect(sink);
205 }
206 }
207
208 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000209 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 if (SINK_POST_CRYPTO == type) {
211 return !SignalRecvPacketPostCrypto.is_empty();
212 } else {
213 return !SignalRecvPacketPreCrypto.is_empty();
214 }
215 }
216
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000217 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 const std::vector<StreamParams>& local_streams() const {
220 return local_streams_;
221 }
222 const std::vector<StreamParams>& remote_streams() const {
223 return remote_streams_;
224 }
225
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000226 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
227 void SignalDtlsSetupFailure_w(bool rtcp);
228 void SignalDtlsSetupFailure_s(bool rtcp);
229
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000230 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
232
233 // Used to alert UI when the muted status changes, perhaps autonomously.
234 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
235
236 // Made public for easier testing.
237 void SetReadyToSend(TransportChannel* channel, bool ready);
238
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000239 // Only public for unit tests. Otherwise, consider protected.
240 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
241
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 protected:
243 MediaEngineInterface* media_engine() const { return media_engine_; }
244 virtual MediaChannel* media_channel() const { return media_channel_; }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000245 // Sets the transport_channel_ and rtcp_transport_channel_. If
246 // |rtcp| is false, set rtcp_transport_channel_ is set to NULL. Get
247 // the transport channels from |session|.
248 // TODO(pthatcher): Pass in a Transport instead of a BaseSession.
249 bool SetTransportChannels(BaseSession* session, bool rtcp);
250 bool SetTransportChannels_w(BaseSession* session, bool rtcp);
251 void set_transport_channel(TransportChannel* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 void set_rtcp_transport_channel(TransportChannel* transport);
253 bool was_ever_writable() const { return was_ever_writable_; }
254 void set_local_content_direction(MediaContentDirection direction) {
255 local_content_direction_ = direction;
256 }
257 void set_remote_content_direction(MediaContentDirection direction) {
258 remote_content_direction_ = direction;
259 }
260 bool IsReadyToReceive() const;
261 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 SrtpFilter* srtp_filter() { return &srtp_filter_; }
264 bool rtcp() const { return rtcp_; }
265
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000266 void ConnectToTransportChannel(TransportChannel* tc);
267 void DisconnectFromTransportChannel(TransportChannel* tc);
268
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 void FlushRtcpMessages();
270
271 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 virtual bool SendPacket(rtc::Buffer* packet,
273 rtc::DiffServCodePoint dscp);
274 virtual bool SendRtcp(rtc::Buffer* packet,
275 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276
277 // From TransportChannel
278 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000279 virtual void OnChannelRead(TransportChannel* channel,
280 const char* data,
281 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000282 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000283 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 void OnReadyToSend(TransportChannel* channel);
285
286 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
287 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000288 bool SendPacket(bool rtcp, rtc::Buffer* packet,
289 rtc::DiffServCodePoint dscp);
290 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
291 void HandlePacket(bool rtcp, rtc::Buffer* packet,
292 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293
294 // Apply the new local/remote session description.
295 void OnNewLocalDescription(BaseSession* session, ContentAction action);
296 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
297
298 void EnableMedia_w();
299 void DisableMedia_w();
300 virtual bool MuteStream_w(uint32 ssrc, bool mute);
301 bool IsStreamMuted_w(uint32 ssrc);
302 void ChannelWritable_w();
303 void ChannelNotWritable_w();
304 bool AddRecvStream_w(const StreamParams& sp);
305 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000306 bool AddSendStream_w(const StreamParams& sp);
307 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 virtual bool ShouldSetupDtlsSrtp() const;
309 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
310 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
311 bool SetupDtlsSrtp(bool rtcp_channel);
312 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
313 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
314
315 virtual void ChangeState() = 0;
316
317 // Gets the content info appropriate to the channel (audio or video).
318 virtual const ContentInfo* GetFirstContent(
319 const SessionDescription* sdesc) = 0;
320 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000321 ContentAction action,
322 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324 ContentAction action,
325 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000327 ContentAction action,
328 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330 ContentAction action,
331 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 ContentAction action,
334 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000336 ContentAction action,
337 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000339 // Helper method to get RTP Absoulute SendTime extension header id if
340 // present in remote supported extensions list.
341 void MaybeCacheRtpAbsSendTimeHeaderExtension(
342 const std::vector<RtpHeaderExtension>& extensions);
343
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000344 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
345 MediaChannel* media_channel,
346 std::string* error_desc);
347 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
348 MediaChannel* media_channel,
349 std::string* error_desc);
350
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
352 bool* dtls,
353 std::string* error_desc);
354 bool SetSrtp_w(const std::vector<CryptoParams>& params,
355 ContentAction action,
356 ContentSource src,
357 std::string* error_desc);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700358 void ActivateRtcpMux_w();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000359 bool SetRtcpMux_w(bool enable,
360 ContentAction action,
361 ContentSource src,
362 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363
364 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366
367 // Handled in derived classes
368 // Get the SRTP ciphers to use for RTP media
369 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000370 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 const std::vector<ConnectionInfo>& infos) = 0;
372
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000373 // Helper function for invoking bool-returning methods on the worker thread.
374 template <class FunctorT>
375 bool InvokeOnWorker(const FunctorT& functor) {
376 return worker_thread_->Invoke<bool>(functor);
377 }
378
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 private:
380 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
381 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
382 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
383 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000384 rtc::CriticalSection signal_send_packet_cs_;
385 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000387 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 MediaEngineInterface* media_engine_;
389 BaseSession* session_;
390 MediaChannel* media_channel_;
391 std::vector<StreamParams> local_streams_;
392 std::vector<StreamParams> remote_streams_;
393
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000394 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 bool rtcp_;
396 TransportChannel* transport_channel_;
397 TransportChannel* rtcp_transport_channel_;
398 SrtpFilter srtp_filter_;
399 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000400 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000401 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 bool enabled_;
403 bool writable_;
404 bool rtp_ready_to_send_;
405 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 bool was_ever_writable_;
407 MediaContentDirection local_content_direction_;
408 MediaContentDirection remote_content_direction_;
409 std::set<uint32> muted_streams_;
410 bool has_received_packet_;
411 bool dtls_keyed_;
412 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000413 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414};
415
416// VoiceChannel is a specialization that adds support for early media, DTMF,
417// and input/output level monitoring.
418class VoiceChannel : public BaseChannel {
419 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000420 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 VoiceMediaChannel* channel, BaseSession* session,
422 const std::string& content_name, bool rtcp);
423 ~VoiceChannel();
424 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000425 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
426 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
428 // downcasts a MediaChannel
429 virtual VoiceMediaChannel* media_channel() const {
430 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
431 }
432
433 bool SetRingbackTone(const void* buf, int len);
434 void SetEarlyMedia(bool enable);
435 // This signal is emitted when we have gone a period of time without
436 // receiving early media. When received, a UI should start playing its
437 // own ringing sound
438 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
439
440 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
441 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
442 bool PressDTMF(int digit, bool playout);
443 // Returns if the telephone-event has been negotiated.
444 bool CanInsertDtmf();
445 // Send and/or play a DTMF |event| according to the |flags|.
446 // The DTMF out-of-band signal will be used on sending.
447 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000448 // The valid value for the |event| are 0 which corresponding to DTMF
449 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
451 bool SetOutputScaling(uint32 ssrc, double left, double right);
452 // Get statistics about the current media session.
453 bool GetStats(VoiceMediaInfo* stats);
454
455 // Monitoring functions
456 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
457 SignalConnectionMonitor;
458
459 void StartMediaMonitor(int cms);
460 void StopMediaMonitor();
461 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
462
463 void StartAudioMonitor(int cms);
464 void StopAudioMonitor();
465 bool IsAudioMonitorRunning() const;
466 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
467
468 void StartTypingMonitor(const TypingMonitorOptions& settings);
469 void StopTypingMonitor();
470 bool IsTypingMonitorRunning() const;
471
472 // Overrides BaseChannel::MuteStream_w.
473 virtual bool MuteStream_w(uint32 ssrc, bool mute);
474
475 int GetInputLevel_w();
476 int GetOutputLevel_w();
477 void GetActiveStreams_w(AudioInfo::StreamList* actives);
478
479 // Signal errors from VoiceMediaChannel. Arguments are:
480 // ssrc(uint32), and error(VoiceMediaChannel::Error).
481 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
482 SignalMediaError;
483
484 // Configuration and setting.
485 bool SetChannelOptions(const AudioOptions& options);
486
487 private:
488 // overrides from BaseChannel
489 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000490 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000491 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000492 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 virtual void ChangeState();
494 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
495 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000496 ContentAction action,
497 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000499 ContentAction action,
500 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 bool SetRingbackTone_w(const void* buf, int len);
502 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
503 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
505 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
506 bool GetStats_w(VoiceMediaInfo* stats);
507
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000508 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
510 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000511 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 virtual void OnMediaMonitorUpdate(
513 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
514 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
515 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
516 void SendLastMediaError();
517 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518
519 static const int kEarlyMediaTimeout = 1000;
520 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000521 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
522 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
523 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524};
525
526// VideoChannel is a specialization for video.
527class VideoChannel : public BaseChannel {
528 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000529 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 VideoMediaChannel* channel, BaseSession* session,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +0200531 const std::string& content_name, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 ~VideoChannel();
533 bool Init();
534
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200535 // downcasts a MediaChannel
536 virtual VideoMediaChannel* media_channel() const {
537 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
538 }
539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
541 bool ApplyViewRequest(const ViewRequest& request);
542
543 // TODO(pthatcher): Refactor to use a "capture id" instead of an
544 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000545 // Passes ownership of the capturer to the channel.
546 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
548 bool RemoveScreencast(uint32 ssrc);
549 // True if we've added a screencast. Doesn't matter if the capturer
550 // has been started or not.
551 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000552 int GetScreencastFps(uint32 ssrc);
553 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000555 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556
557 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
558 SignalConnectionMonitor;
559
560 void StartMediaMonitor(int cms);
561 void StopMediaMonitor();
562 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564
565 bool SendIntraFrame();
566 bool RequestIntraFrame();
567 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
568 SignalMediaError;
569
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 // Configuration and setting.
571 bool SetChannelOptions(const VideoOptions& options);
572
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 private:
574 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000575 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576
577 // overrides from BaseChannel
578 virtual void ChangeState();
579 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
580 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000581 ContentAction action,
582 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000584 ContentAction action,
585 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000588 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000590 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000592 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 bool GetStats_w(VideoMediaInfo* stats);
594
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
597 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000598 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 virtual void OnMediaMonitorUpdate(
600 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
601 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
604 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
605
606 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
607 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000613 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614};
615
616// DataChannel is a specialization for data.
617class DataChannel : public BaseChannel {
618 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000619 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 DataMediaChannel* media_channel,
621 BaseSession* session,
622 const std::string& content_name,
623 bool rtcp);
624 ~DataChannel();
625 bool Init();
626
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000628 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000629 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
631 void StartMediaMonitor(int cms);
632 void StopMediaMonitor();
633
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000634 // Should be called on the signaling thread only.
635 bool ready_to_send_data() const {
636 return ready_to_send_data_;
637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
640 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
641 SignalConnectionMonitor;
642 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
643 SignalMediaError;
644 sigslot::signal3<DataChannel*,
645 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000646 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 SignalDataReceived;
648 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000649 // That occurs when the channel is enabled, the transport is writable,
650 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000652 // Signal for notifying that the remote side has closed the DataChannel.
653 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000655 protected:
656 // downcasts a MediaChannel.
657 virtual DataMediaChannel* media_channel() const {
658 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
659 }
660
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000664 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 SendDataResult* result)
666 : params(params),
667 payload(payload),
668 result(result),
669 succeeded(false) {
670 }
671
672 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000673 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 SendDataResult* result;
675 bool succeeded;
676 };
677
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000678 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 // We copy the data because the data will become invalid after we
680 // handle DataMediaChannel::SignalDataReceived but before we fire
681 // SignalDataReceived.
682 DataReceivedMessageData(
683 const ReceiveDataParams& params, const char* data, size_t len)
684 : params(params),
685 payload(data, len) {
686 }
687 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000688 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 };
690
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000691 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 // overrides from BaseChannel
694 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
695 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
696 // it's the same as what was set previously. Returns false if it's
697 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000698 bool SetDataChannelType(DataChannelType new_data_channel_type,
699 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 // Same as SetDataChannelType, but extracts the type from the
701 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000702 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
703 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000705 ContentAction action,
706 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000708 ContentAction action,
709 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000711 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000713 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
715 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000716 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 virtual void OnMediaMonitorUpdate(
718 DataMediaChannel* media_channel, const DataMediaInfo& info);
719 virtual bool ShouldSetupDtlsSrtp() const;
720 void OnDataReceived(
721 const ReceiveDataParams& params, const char* data, size_t len);
722 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000723 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000725 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000727 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 // TODO(pthatcher): Make a separate SctpDataChannel and
729 // RtpDataChannel instead of using this.
730 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000731 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732};
733
734} // namespace cricket
735
736#endif // TALK_SESSION_MEDIA_CHANNEL_H_