blob: bb88c2ff7a811b3f2a18b13a3dc86e0ebe1fa6ff [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
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000216 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
218
219 // Used to alert UI when the muted status changes, perhaps autonomously.
220 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
221
222 // Made public for easier testing.
223 void SetReadyToSend(TransportChannel* channel, bool ready);
224
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000225 // Only public for unit tests. Otherwise, consider protected.
226 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
227
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 protected:
229 MediaEngineInterface* media_engine() const { return media_engine_; }
230 virtual MediaChannel* media_channel() const { return media_channel_; }
231 void set_rtcp_transport_channel(TransportChannel* transport);
232 bool was_ever_writable() const { return was_ever_writable_; }
233 void set_local_content_direction(MediaContentDirection direction) {
234 local_content_direction_ = direction;
235 }
236 void set_remote_content_direction(MediaContentDirection direction) {
237 remote_content_direction_ = direction;
238 }
239 bool IsReadyToReceive() const;
240 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 SrtpFilter* srtp_filter() { return &srtp_filter_; }
243 bool rtcp() const { return rtcp_; }
244
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 void FlushRtcpMessages();
246
247 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000248 virtual bool SendPacket(rtc::Buffer* packet,
249 rtc::DiffServCodePoint dscp);
250 virtual bool SendRtcp(rtc::Buffer* packet,
251 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
253 // From TransportChannel
254 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000255 virtual void OnChannelRead(TransportChannel* channel,
256 const char* data,
257 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000258 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000259 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 void OnReadyToSend(TransportChannel* channel);
261
262 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
263 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264 bool SendPacket(bool rtcp, rtc::Buffer* packet,
265 rtc::DiffServCodePoint dscp);
266 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
267 void HandlePacket(bool rtcp, rtc::Buffer* packet,
268 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
270 // Apply the new local/remote session description.
271 void OnNewLocalDescription(BaseSession* session, ContentAction action);
272 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
273
274 void EnableMedia_w();
275 void DisableMedia_w();
276 virtual bool MuteStream_w(uint32 ssrc, bool mute);
277 bool IsStreamMuted_w(uint32 ssrc);
278 void ChannelWritable_w();
279 void ChannelNotWritable_w();
280 bool AddRecvStream_w(const StreamParams& sp);
281 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000282 bool AddSendStream_w(const StreamParams& sp);
283 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 virtual bool ShouldSetupDtlsSrtp() const;
285 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
286 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
287 bool SetupDtlsSrtp(bool rtcp_channel);
288 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
289 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
290
291 virtual void ChangeState() = 0;
292
293 // Gets the content info appropriate to the channel (audio or video).
294 virtual const ContentInfo* GetFirstContent(
295 const SessionDescription* sdesc) = 0;
296 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000297 ContentAction action,
298 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000300 ContentAction action,
301 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000303 ContentAction action,
304 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000306 ContentAction action,
307 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000309 ContentAction action,
310 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000312 ContentAction action,
313 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000315 // Helper method to get RTP Absoulute SendTime extension header id if
316 // present in remote supported extensions list.
317 void MaybeCacheRtpAbsSendTimeHeaderExtension(
318 const std::vector<RtpHeaderExtension>& extensions);
319
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000320 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
321 MediaChannel* media_channel,
322 std::string* error_desc);
323 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
324 MediaChannel* media_channel,
325 std::string* error_desc);
326
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000327 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
328 bool* dtls,
329 std::string* error_desc);
330 bool SetSrtp_w(const std::vector<CryptoParams>& params,
331 ContentAction action,
332 ContentSource src,
333 std::string* error_desc);
334 bool SetRtcpMux_w(bool enable,
335 ContentAction action,
336 ContentSource src,
337 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
339 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000340 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341
342 // Handled in derived classes
343 // Get the SRTP ciphers to use for RTP media
344 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000345 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 const std::vector<ConnectionInfo>& infos) = 0;
347
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000348 // Helper function for invoking bool-returning methods on the worker thread.
349 template <class FunctorT>
350 bool InvokeOnWorker(const FunctorT& functor) {
351 return worker_thread_->Invoke<bool>(functor);
352 }
353
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 private:
355 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
356 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
357 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
358 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359 rtc::CriticalSection signal_send_packet_cs_;
360 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000362 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 MediaEngineInterface* media_engine_;
364 BaseSession* session_;
365 MediaChannel* media_channel_;
366 std::vector<StreamParams> local_streams_;
367 std::vector<StreamParams> remote_streams_;
368
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000369 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 bool rtcp_;
371 TransportChannel* transport_channel_;
372 TransportChannel* rtcp_transport_channel_;
373 SrtpFilter srtp_filter_;
374 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000375 BundleFilter bundle_filter_;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000376 rtc::scoped_ptr<ConnectionMonitor> connection_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 bool enabled_;
378 bool writable_;
379 bool rtp_ready_to_send_;
380 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 bool was_ever_writable_;
382 MediaContentDirection local_content_direction_;
383 MediaContentDirection remote_content_direction_;
384 std::set<uint32> muted_streams_;
385 bool has_received_packet_;
386 bool dtls_keyed_;
387 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000388 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389};
390
391// VoiceChannel is a specialization that adds support for early media, DTMF,
392// and input/output level monitoring.
393class VoiceChannel : public BaseChannel {
394 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000395 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 VoiceMediaChannel* channel, BaseSession* session,
397 const std::string& content_name, bool rtcp);
398 ~VoiceChannel();
399 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000400 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
401 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402
403 // downcasts a MediaChannel
404 virtual VoiceMediaChannel* media_channel() const {
405 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
406 }
407
408 bool SetRingbackTone(const void* buf, int len);
409 void SetEarlyMedia(bool enable);
410 // This signal is emitted when we have gone a period of time without
411 // receiving early media. When received, a UI should start playing its
412 // own ringing sound
413 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
414
415 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
416 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
417 bool PressDTMF(int digit, bool playout);
418 // Returns if the telephone-event has been negotiated.
419 bool CanInsertDtmf();
420 // Send and/or play a DTMF |event| according to the |flags|.
421 // The DTMF out-of-band signal will be used on sending.
422 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000423 // The valid value for the |event| are 0 which corresponding to DTMF
424 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
426 bool SetOutputScaling(uint32 ssrc, double left, double right);
427 // Get statistics about the current media session.
428 bool GetStats(VoiceMediaInfo* stats);
429
430 // Monitoring functions
431 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
432 SignalConnectionMonitor;
433
434 void StartMediaMonitor(int cms);
435 void StopMediaMonitor();
436 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
437
438 void StartAudioMonitor(int cms);
439 void StopAudioMonitor();
440 bool IsAudioMonitorRunning() const;
441 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
442
443 void StartTypingMonitor(const TypingMonitorOptions& settings);
444 void StopTypingMonitor();
445 bool IsTypingMonitorRunning() const;
446
447 // Overrides BaseChannel::MuteStream_w.
448 virtual bool MuteStream_w(uint32 ssrc, bool mute);
449
450 int GetInputLevel_w();
451 int GetOutputLevel_w();
452 void GetActiveStreams_w(AudioInfo::StreamList* actives);
453
454 // Signal errors from VoiceMediaChannel. Arguments are:
455 // ssrc(uint32), and error(VoiceMediaChannel::Error).
456 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
457 SignalMediaError;
458
459 // Configuration and setting.
460 bool SetChannelOptions(const AudioOptions& options);
461
462 private:
463 // overrides from BaseChannel
464 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000465 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000466 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000467 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 virtual void ChangeState();
469 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
470 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000471 ContentAction action,
472 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000474 ContentAction action,
475 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 bool SetRingbackTone_w(const void* buf, int len);
477 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
478 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
480 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
481 bool GetStats_w(VoiceMediaInfo* stats);
482
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000483 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
485 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000486 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 virtual void OnMediaMonitorUpdate(
488 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
489 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
490 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
491 void SendLastMediaError();
492 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493
494 static const int kEarlyMediaTimeout = 1000;
495 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000496 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
497 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
498 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499};
500
501// VideoChannel is a specialization for video.
502class VideoChannel : public BaseChannel {
503 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000504 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 VideoMediaChannel* channel, BaseSession* session,
506 const std::string& content_name, bool rtcp,
507 VoiceChannel* voice_channel);
508 ~VideoChannel();
509 bool Init();
510
511 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
512 bool ApplyViewRequest(const ViewRequest& request);
513
514 // TODO(pthatcher): Refactor to use a "capture id" instead of an
515 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000516 // Passes ownership of the capturer to the channel.
517 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
519 bool RemoveScreencast(uint32 ssrc);
520 // True if we've added a screencast. Doesn't matter if the capturer
521 // has been started or not.
522 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000523 int GetScreencastFps(uint32 ssrc);
524 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000526 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
528 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
529 SignalConnectionMonitor;
530
531 void StartMediaMonitor(int cms);
532 void StopMediaMonitor();
533 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000534 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
536 bool SendIntraFrame();
537 bool RequestIntraFrame();
538 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
539 SignalMediaError;
540
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // Configuration and setting.
542 bool SetChannelOptions(const VideoOptions& options);
543
544 protected:
545 // downcasts a MediaChannel
546 virtual VideoMediaChannel* media_channel() const {
547 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
548 }
549
550 private:
551 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000552 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553
554 // overrides from BaseChannel
555 virtual void ChangeState();
556 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
557 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000558 ContentAction action,
559 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000561 ContentAction action,
562 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000565 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000567 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000569 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 bool GetStats_w(VideoMediaInfo* stats);
571
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000572 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
574 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000575 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 virtual void OnMediaMonitorUpdate(
577 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
578 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000579 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
581 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
582
583 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
584 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585
586 VoiceChannel* voice_channel_;
587 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592};
593
594// DataChannel is a specialization for data.
595class DataChannel : public BaseChannel {
596 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 DataMediaChannel* media_channel,
599 BaseSession* session,
600 const std::string& content_name,
601 bool rtcp);
602 ~DataChannel();
603 bool Init();
604
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000605 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000606 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000607 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
609 void StartMediaMonitor(int cms);
610 void StopMediaMonitor();
611
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000612 // Should be called on the signaling thread only.
613 bool ready_to_send_data() const {
614 return ready_to_send_data_;
615 }
616
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
618 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
619 SignalConnectionMonitor;
620 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
621 SignalMediaError;
622 sigslot::signal3<DataChannel*,
623 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 SignalDataReceived;
626 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 // That occurs when the channel is enabled, the transport is writable,
628 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000630 // Signal for notifying that the remote side has closed the DataChannel.
631 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000633 protected:
634 // downcasts a MediaChannel.
635 virtual DataMediaChannel* media_channel() const {
636 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000640 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000642 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 SendDataResult* result)
644 : params(params),
645 payload(payload),
646 result(result),
647 succeeded(false) {
648 }
649
650 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000651 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 SendDataResult* result;
653 bool succeeded;
654 };
655
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000656 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 // We copy the data because the data will become invalid after we
658 // handle DataMediaChannel::SignalDataReceived but before we fire
659 // SignalDataReceived.
660 DataReceivedMessageData(
661 const ReceiveDataParams& params, const char* data, size_t len)
662 : params(params),
663 payload(data, len) {
664 }
665 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000666 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 };
668
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000669 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000670
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 // overrides from BaseChannel
672 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
673 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
674 // it's the same as what was set previously. Returns false if it's
675 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000676 bool SetDataChannelType(DataChannelType new_data_channel_type,
677 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 // Same as SetDataChannelType, but extracts the type from the
679 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000680 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
681 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000683 ContentAction action,
684 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000686 ContentAction action,
687 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000689 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000691 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
693 virtual void OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000694 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 virtual void OnMediaMonitorUpdate(
696 DataMediaChannel* media_channel, const DataMediaInfo& info);
697 virtual bool ShouldSetupDtlsSrtp() const;
698 void OnDataReceived(
699 const ReceiveDataParams& params, const char* data, size_t len);
700 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000701 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000703 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000705 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 // TODO(pthatcher): Make a separate SctpDataChannel and
707 // RtpDataChannel instead of using this.
708 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000709 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710};
711
712} // namespace cricket
713
714#endif // TALK_SESSION_MEDIA_CHANNEL_H_