blob: 441fe64b6842e1bf3e2bcd3205bd5dc4ea23288b [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
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000111 bool PushdownLocalDescription(const SessionDescription* local_desc,
112 ContentAction action,
113 std::string* error_desc);
114 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
115 ContentAction action,
116 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 // Channel control
118 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000119 ContentAction action,
120 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000122 ContentAction action,
123 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
125 bool Enable(bool enable);
126 // Mute sending media on the stream with SSRC |ssrc|
127 // If there is only one sending stream SSRC 0 can be used.
128 bool MuteStream(uint32 ssrc, bool mute);
129
130 // Multiplexing
131 bool AddRecvStream(const StreamParams& sp);
132 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000133 bool AddSendStream(const StreamParams& sp);
134 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
136 // Monitoring
137 void StartConnectionMonitor(int cms);
138 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000139 // For ConnectionStatsGetter, used by ConnectionMonitor
140 virtual bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
142 void set_srtp_signal_silent_time(uint32 silent_time) {
143 srtp_filter_.set_signal_silent_time(silent_time);
144 }
145
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 template <class T>
147 void RegisterSendSink(T* sink,
148 void (T::*OnPacket)(const void*, size_t, bool),
149 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000150 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 if (SINK_POST_CRYPTO == type) {
152 SignalSendPacketPostCrypto.disconnect(sink);
153 SignalSendPacketPostCrypto.connect(sink, OnPacket);
154 } else {
155 SignalSendPacketPreCrypto.disconnect(sink);
156 SignalSendPacketPreCrypto.connect(sink, OnPacket);
157 }
158 }
159
160 void UnregisterSendSink(sigslot::has_slots<>* sink,
161 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000162 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 if (SINK_POST_CRYPTO == type) {
164 SignalSendPacketPostCrypto.disconnect(sink);
165 } else {
166 SignalSendPacketPreCrypto.disconnect(sink);
167 }
168 }
169
170 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 if (SINK_POST_CRYPTO == type) {
173 return !SignalSendPacketPostCrypto.is_empty();
174 } else {
175 return !SignalSendPacketPreCrypto.is_empty();
176 }
177 }
178
179 template <class T>
180 void RegisterRecvSink(T* sink,
181 void (T::*OnPacket)(const void*, size_t, bool),
182 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 if (SINK_POST_CRYPTO == type) {
185 SignalRecvPacketPostCrypto.disconnect(sink);
186 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
187 } else {
188 SignalRecvPacketPreCrypto.disconnect(sink);
189 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
190 }
191 }
192
193 void UnregisterRecvSink(sigslot::has_slots<>* sink,
194 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000195 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 if (SINK_POST_CRYPTO == type) {
197 SignalRecvPacketPostCrypto.disconnect(sink);
198 } else {
199 SignalRecvPacketPreCrypto.disconnect(sink);
200 }
201 }
202
203 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 if (SINK_POST_CRYPTO == type) {
206 return !SignalRecvPacketPostCrypto.is_empty();
207 } else {
208 return !SignalRecvPacketPreCrypto.is_empty();
209 }
210 }
211
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000212 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213
214 const std::vector<StreamParams>& local_streams() const {
215 return local_streams_;
216 }
217 const std::vector<StreamParams>& remote_streams() const {
218 return remote_streams_;
219 }
220
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000221 sigslot::signal2<BaseChannel*, bool> SignalDtlsSetupFailure;
222 void SignalDtlsSetupFailure_w(bool rtcp);
223 void SignalDtlsSetupFailure_s(bool rtcp);
224
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000225 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
227
228 // Used to alert UI when the muted status changes, perhaps autonomously.
229 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
230
231 // Made public for easier testing.
232 void SetReadyToSend(TransportChannel* channel, bool ready);
233
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000234 // Only public for unit tests. Otherwise, consider protected.
235 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 protected:
238 MediaEngineInterface* media_engine() const { return media_engine_; }
239 virtual MediaChannel* media_channel() const { return media_channel_; }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240 // Sets the transport_channel_ and rtcp_transport_channel_. If
241 // |rtcp| is false, set rtcp_transport_channel_ is set to NULL. Get
242 // the transport channels from |session|.
243 // TODO(pthatcher): Pass in a Transport instead of a BaseSession.
244 bool SetTransportChannels(BaseSession* session, bool rtcp);
245 bool SetTransportChannels_w(BaseSession* session, bool rtcp);
246 void set_transport_channel(TransportChannel* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 void set_rtcp_transport_channel(TransportChannel* transport);
248 bool was_ever_writable() const { return was_ever_writable_; }
249 void set_local_content_direction(MediaContentDirection direction) {
250 local_content_direction_ = direction;
251 }
252 void set_remote_content_direction(MediaContentDirection direction) {
253 remote_content_direction_ = direction;
254 }
255 bool IsReadyToReceive() const;
256 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 SrtpFilter* srtp_filter() { return &srtp_filter_; }
259 bool rtcp() const { return rtcp_; }
260
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000261 void ConnectToTransportChannel(TransportChannel* tc);
262 void DisconnectFromTransportChannel(TransportChannel* tc);
263
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 void FlushRtcpMessages();
265
266 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 virtual bool SendPacket(rtc::Buffer* packet,
268 rtc::DiffServCodePoint dscp);
269 virtual bool SendRtcp(rtc::Buffer* packet,
270 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271
272 // From TransportChannel
273 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000274 virtual void OnChannelRead(TransportChannel* channel,
275 const char* data,
276 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000278 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 void OnReadyToSend(TransportChannel* channel);
280
281 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
282 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000283 bool SendPacket(bool rtcp, rtc::Buffer* packet,
284 rtc::DiffServCodePoint dscp);
285 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
286 void HandlePacket(bool rtcp, rtc::Buffer* packet,
287 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
289 // Apply the new local/remote session description.
290 void OnNewLocalDescription(BaseSession* session, ContentAction action);
291 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
292
293 void EnableMedia_w();
294 void DisableMedia_w();
295 virtual bool MuteStream_w(uint32 ssrc, bool mute);
296 bool IsStreamMuted_w(uint32 ssrc);
297 void ChannelWritable_w();
298 void ChannelNotWritable_w();
299 bool AddRecvStream_w(const StreamParams& sp);
300 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000301 bool AddSendStream_w(const StreamParams& sp);
302 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 virtual bool ShouldSetupDtlsSrtp() const;
304 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
305 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
306 bool SetupDtlsSrtp(bool rtcp_channel);
307 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
308 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
309
310 virtual void ChangeState() = 0;
311
312 // Gets the content info appropriate to the channel (audio or video).
313 virtual const ContentInfo* GetFirstContent(
314 const SessionDescription* sdesc) = 0;
315 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000316 ContentAction action,
317 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319 ContentAction action,
320 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000322 ContentAction action,
323 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325 ContentAction action,
326 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 ContentAction action,
329 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331 ContentAction action,
332 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000334 // Helper method to get RTP Absoulute SendTime extension header id if
335 // present in remote supported extensions list.
336 void MaybeCacheRtpAbsSendTimeHeaderExtension(
337 const std::vector<RtpHeaderExtension>& extensions);
338
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000339 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
340 MediaChannel* media_channel,
341 std::string* error_desc);
342 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
343 MediaChannel* media_channel,
344 std::string* error_desc);
345
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000346 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
347 bool* dtls,
348 std::string* error_desc);
349 bool SetSrtp_w(const std::vector<CryptoParams>& params,
350 ContentAction action,
351 ContentSource src,
352 std::string* error_desc);
353 bool SetRtcpMux_w(bool enable,
354 ContentAction action,
355 ContentSource src,
356 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
358 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360
361 // Handled in derived classes
362 // Get the SRTP ciphers to use for RTP media
363 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000364 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 const std::vector<ConnectionInfo>& infos) = 0;
366
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000367 // Helper function for invoking bool-returning methods on the worker thread.
368 template <class FunctorT>
369 bool InvokeOnWorker(const FunctorT& functor) {
370 return worker_thread_->Invoke<bool>(functor);
371 }
372
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 private:
374 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
375 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
376 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
377 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000378 rtc::CriticalSection signal_send_packet_cs_;
379 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000381 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 MediaEngineInterface* media_engine_;
383 BaseSession* session_;
384 MediaChannel* media_channel_;
385 std::vector<StreamParams> local_streams_;
386 std::vector<StreamParams> remote_streams_;
387
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000388 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 bool rtcp_;
390 TransportChannel* transport_channel_;
391 TransportChannel* rtcp_transport_channel_;
392 SrtpFilter srtp_filter_;
393 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000394 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000395 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 bool enabled_;
397 bool writable_;
398 bool rtp_ready_to_send_;
399 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 bool was_ever_writable_;
401 MediaContentDirection local_content_direction_;
402 MediaContentDirection remote_content_direction_;
403 std::set<uint32> muted_streams_;
404 bool has_received_packet_;
405 bool dtls_keyed_;
406 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000407 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408};
409
410// VoiceChannel is a specialization that adds support for early media, DTMF,
411// and input/output level monitoring.
412class VoiceChannel : public BaseChannel {
413 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000414 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 VoiceMediaChannel* channel, BaseSession* session,
416 const std::string& content_name, bool rtcp);
417 ~VoiceChannel();
418 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000419 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
420 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421
422 // downcasts a MediaChannel
423 virtual VoiceMediaChannel* media_channel() const {
424 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
425 }
426
427 bool SetRingbackTone(const void* buf, int len);
428 void SetEarlyMedia(bool enable);
429 // This signal is emitted when we have gone a period of time without
430 // receiving early media. When received, a UI should start playing its
431 // own ringing sound
432 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
433
434 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
435 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
436 bool PressDTMF(int digit, bool playout);
437 // Returns if the telephone-event has been negotiated.
438 bool CanInsertDtmf();
439 // Send and/or play a DTMF |event| according to the |flags|.
440 // The DTMF out-of-band signal will be used on sending.
441 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000442 // The valid value for the |event| are 0 which corresponding to DTMF
443 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
445 bool SetOutputScaling(uint32 ssrc, double left, double right);
446 // Get statistics about the current media session.
447 bool GetStats(VoiceMediaInfo* stats);
448
449 // Monitoring functions
450 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
451 SignalConnectionMonitor;
452
453 void StartMediaMonitor(int cms);
454 void StopMediaMonitor();
455 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
456
457 void StartAudioMonitor(int cms);
458 void StopAudioMonitor();
459 bool IsAudioMonitorRunning() const;
460 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
461
462 void StartTypingMonitor(const TypingMonitorOptions& settings);
463 void StopTypingMonitor();
464 bool IsTypingMonitorRunning() const;
465
466 // Overrides BaseChannel::MuteStream_w.
467 virtual bool MuteStream_w(uint32 ssrc, bool mute);
468
469 int GetInputLevel_w();
470 int GetOutputLevel_w();
471 void GetActiveStreams_w(AudioInfo::StreamList* actives);
472
473 // Signal errors from VoiceMediaChannel. Arguments are:
474 // ssrc(uint32), and error(VoiceMediaChannel::Error).
475 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
476 SignalMediaError;
477
478 // Configuration and setting.
479 bool SetChannelOptions(const AudioOptions& options);
480
481 private:
482 // overrides from BaseChannel
483 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000484 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000485 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000486 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 virtual void ChangeState();
488 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
489 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000490 ContentAction action,
491 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000493 ContentAction action,
494 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 bool SetRingbackTone_w(const void* buf, int len);
496 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
497 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
499 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
500 bool GetStats_w(VoiceMediaInfo* stats);
501
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000502 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
504 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000505 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 virtual void OnMediaMonitorUpdate(
507 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
508 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
509 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
510 void SendLastMediaError();
511 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
513 static const int kEarlyMediaTimeout = 1000;
514 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000515 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
516 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
517 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518};
519
520// VideoChannel is a specialization for video.
521class VideoChannel : public BaseChannel {
522 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000523 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 VideoMediaChannel* channel, BaseSession* session,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +0200525 const std::string& content_name, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 ~VideoChannel();
527 bool Init();
528
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200529 // downcasts a MediaChannel
530 virtual VideoMediaChannel* media_channel() const {
531 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
532 }
533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
535 bool ApplyViewRequest(const ViewRequest& request);
536
537 // TODO(pthatcher): Refactor to use a "capture id" instead of an
538 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000539 // Passes ownership of the capturer to the channel.
540 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
542 bool RemoveScreencast(uint32 ssrc);
543 // True if we've added a screencast. Doesn't matter if the capturer
544 // has been started or not.
545 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000546 int GetScreencastFps(uint32 ssrc);
547 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000549 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550
551 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
552 SignalConnectionMonitor;
553
554 void StartMediaMonitor(int cms);
555 void StopMediaMonitor();
556 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558
559 bool SendIntraFrame();
560 bool RequestIntraFrame();
561 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
562 SignalMediaError;
563
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 // Configuration and setting.
565 bool SetChannelOptions(const VideoOptions& options);
566
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 private:
568 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000569 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570
571 // overrides from BaseChannel
572 virtual void ChangeState();
573 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
574 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000575 ContentAction action,
576 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000578 ContentAction action,
579 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000582 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000584 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000586 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 bool GetStats_w(VideoMediaInfo* stats);
588
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
591 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000592 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 virtual void OnMediaMonitorUpdate(
594 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
595 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000596 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
598 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
599
600 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
601 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000605 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000607 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608};
609
610// DataChannel is a specialization for data.
611class DataChannel : public BaseChannel {
612 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000613 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 DataMediaChannel* media_channel,
615 BaseSession* session,
616 const std::string& content_name,
617 bool rtcp);
618 ~DataChannel();
619 bool Init();
620
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000621 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000622 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000623 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624
625 void StartMediaMonitor(int cms);
626 void StopMediaMonitor();
627
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000628 // Should be called on the signaling thread only.
629 bool ready_to_send_data() const {
630 return ready_to_send_data_;
631 }
632
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
634 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
635 SignalConnectionMonitor;
636 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
637 SignalMediaError;
638 sigslot::signal3<DataChannel*,
639 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000640 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 SignalDataReceived;
642 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000643 // That occurs when the channel is enabled, the transport is writable,
644 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000646 // Signal for notifying that the remote side has closed the DataChannel.
647 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000649 protected:
650 // downcasts a MediaChannel.
651 virtual DataMediaChannel* media_channel() const {
652 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
653 }
654
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000656 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000658 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 SendDataResult* result)
660 : params(params),
661 payload(payload),
662 result(result),
663 succeeded(false) {
664 }
665
666 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000667 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 SendDataResult* result;
669 bool succeeded;
670 };
671
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 // We copy the data because the data will become invalid after we
674 // handle DataMediaChannel::SignalDataReceived but before we fire
675 // SignalDataReceived.
676 DataReceivedMessageData(
677 const ReceiveDataParams& params, const char* data, size_t len)
678 : params(params),
679 payload(data, len) {
680 }
681 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000682 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 };
684
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000685 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000686
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 // overrides from BaseChannel
688 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
689 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
690 // it's the same as what was set previously. Returns false if it's
691 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000692 bool SetDataChannelType(DataChannelType new_data_channel_type,
693 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 // Same as SetDataChannelType, but extracts the type from the
695 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000696 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
697 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000699 ContentAction action,
700 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000702 ContentAction action,
703 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000705 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000707 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
709 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000710 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 virtual void OnMediaMonitorUpdate(
712 DataMediaChannel* media_channel, const DataMediaInfo& info);
713 virtual bool ShouldSetupDtlsSrtp() const;
714 void OnDataReceived(
715 const ReceiveDataParams& params, const char* data, size_t len);
716 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000717 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000719 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000721 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 // TODO(pthatcher): Make a separate SctpDataChannel and
723 // RtpDataChannel instead of using this.
724 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000725 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726};
727
728} // namespace cricket
729
730#endif // TALK_SESSION_MEDIA_CHANNEL_H_