blob: 6256b35700e4d4407f62a02718dcac99fbc003f2 [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<>,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public MediaChannel::NetworkInterface {
77 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 BaseChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 MediaChannel* channel, BaseSession* session,
80 const std::string& content_name, bool rtcp);
81 virtual ~BaseChannel();
82 bool Init(TransportChannel* transport_channel,
83 TransportChannel* rtcp_transport_channel);
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
111 // Channel control
112 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000113 ContentAction action,
114 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000116 ContentAction action,
117 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 bool Enable(bool enable);
120 // Mute sending media on the stream with SSRC |ssrc|
121 // If there is only one sending stream SSRC 0 can be used.
122 bool MuteStream(uint32 ssrc, bool mute);
123
124 // Multiplexing
125 bool AddRecvStream(const StreamParams& sp);
126 bool RemoveRecvStream(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000127 bool AddSendStream(const StreamParams& sp);
128 bool RemoveSendStream(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Monitoring
131 void StartConnectionMonitor(int cms);
132 void StopConnectionMonitor();
133
134 void set_srtp_signal_silent_time(uint32 silent_time) {
135 srtp_filter_.set_signal_silent_time(silent_time);
136 }
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 template <class T>
139 void RegisterSendSink(T* sink,
140 void (T::*OnPacket)(const void*, size_t, bool),
141 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000142 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 if (SINK_POST_CRYPTO == type) {
144 SignalSendPacketPostCrypto.disconnect(sink);
145 SignalSendPacketPostCrypto.connect(sink, OnPacket);
146 } else {
147 SignalSendPacketPreCrypto.disconnect(sink);
148 SignalSendPacketPreCrypto.connect(sink, OnPacket);
149 }
150 }
151
152 void UnregisterSendSink(sigslot::has_slots<>* sink,
153 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000154 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 if (SINK_POST_CRYPTO == type) {
156 SignalSendPacketPostCrypto.disconnect(sink);
157 } else {
158 SignalSendPacketPreCrypto.disconnect(sink);
159 }
160 }
161
162 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000163 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 if (SINK_POST_CRYPTO == type) {
165 return !SignalSendPacketPostCrypto.is_empty();
166 } else {
167 return !SignalSendPacketPreCrypto.is_empty();
168 }
169 }
170
171 template <class T>
172 void RegisterRecvSink(T* sink,
173 void (T::*OnPacket)(const void*, size_t, bool),
174 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000175 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 if (SINK_POST_CRYPTO == type) {
177 SignalRecvPacketPostCrypto.disconnect(sink);
178 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
179 } else {
180 SignalRecvPacketPreCrypto.disconnect(sink);
181 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
182 }
183 }
184
185 void UnregisterRecvSink(sigslot::has_slots<>* sink,
186 SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 if (SINK_POST_CRYPTO == type) {
189 SignalRecvPacketPostCrypto.disconnect(sink);
190 } else {
191 SignalRecvPacketPreCrypto.disconnect(sink);
192 }
193 }
194
195 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000196 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 if (SINK_POST_CRYPTO == type) {
198 return !SignalRecvPacketPostCrypto.is_empty();
199 } else {
200 return !SignalRecvPacketPreCrypto.is_empty();
201 }
202 }
203
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000204 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205
206 const std::vector<StreamParams>& local_streams() const {
207 return local_streams_;
208 }
209 const std::vector<StreamParams>& remote_streams() const {
210 return remote_streams_;
211 }
212
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000213 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
215
216 // Used to alert UI when the muted status changes, perhaps autonomously.
217 sigslot::repeater2<BaseChannel*, bool> SignalAutoMuted;
218
219 // Made public for easier testing.
220 void SetReadyToSend(TransportChannel* channel, bool ready);
221
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000222 // Only public for unit tests. Otherwise, consider protected.
223 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 protected:
226 MediaEngineInterface* media_engine() const { return media_engine_; }
227 virtual MediaChannel* media_channel() const { return media_channel_; }
228 void set_rtcp_transport_channel(TransportChannel* transport);
229 bool was_ever_writable() const { return was_ever_writable_; }
230 void set_local_content_direction(MediaContentDirection direction) {
231 local_content_direction_ = direction;
232 }
233 void set_remote_content_direction(MediaContentDirection direction) {
234 remote_content_direction_ = direction;
235 }
236 bool IsReadyToReceive() const;
237 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000238 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 SrtpFilter* srtp_filter() { return &srtp_filter_; }
240 bool rtcp() const { return rtcp_; }
241
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 void FlushRtcpMessages();
243
244 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000245 virtual bool SendPacket(rtc::Buffer* packet,
246 rtc::DiffServCodePoint dscp);
247 virtual bool SendRtcp(rtc::Buffer* packet,
248 rtc::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
250 // From TransportChannel
251 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000252 virtual void OnChannelRead(TransportChannel* channel,
253 const char* data,
254 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000255 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000256 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 void OnReadyToSend(TransportChannel* channel);
258
259 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
260 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000261 bool SendPacket(bool rtcp, rtc::Buffer* packet,
262 rtc::DiffServCodePoint dscp);
263 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
264 void HandlePacket(bool rtcp, rtc::Buffer* packet,
265 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266
267 // Apply the new local/remote session description.
268 void OnNewLocalDescription(BaseSession* session, ContentAction action);
269 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
270
271 void EnableMedia_w();
272 void DisableMedia_w();
273 virtual bool MuteStream_w(uint32 ssrc, bool mute);
274 bool IsStreamMuted_w(uint32 ssrc);
275 void ChannelWritable_w();
276 void ChannelNotWritable_w();
277 bool AddRecvStream_w(const StreamParams& sp);
278 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000279 bool AddSendStream_w(const StreamParams& sp);
280 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 virtual bool ShouldSetupDtlsSrtp() const;
282 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
283 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
284 bool SetupDtlsSrtp(bool rtcp_channel);
285 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
286 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
287
288 virtual void ChangeState() = 0;
289
290 // Gets the content info appropriate to the channel (audio or video).
291 virtual const ContentInfo* GetFirstContent(
292 const SessionDescription* sdesc) = 0;
293 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000294 ContentAction action,
295 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 bool UpdateRemoteStreams_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 SetBaseLocalContent_w(const MediaContentDescription* content,
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 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000303 ContentAction action,
304 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000306 ContentAction action,
307 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000309 ContentAction action,
310 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000312 // Helper method to get RTP Absoulute SendTime extension header id if
313 // present in remote supported extensions list.
314 void MaybeCacheRtpAbsSendTimeHeaderExtension(
315 const std::vector<RtpHeaderExtension>& extensions);
316
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000317 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
318 MediaChannel* media_channel,
319 std::string* error_desc);
320 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
321 MediaChannel* media_channel,
322 std::string* error_desc);
323
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
325 bool* dtls,
326 std::string* error_desc);
327 bool SetSrtp_w(const std::vector<CryptoParams>& params,
328 ContentAction action,
329 ContentSource src,
330 std::string* error_desc);
331 bool SetRtcpMux_w(bool enable,
332 ContentAction action,
333 ContentSource src,
334 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335
336 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000337 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
339 // Handled in derived classes
340 // Get the SRTP ciphers to use for RTP media
341 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
342 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
343 const std::vector<ConnectionInfo>& infos) = 0;
344
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000345 // Helper function for invoking bool-returning methods on the worker thread.
346 template <class FunctorT>
347 bool InvokeOnWorker(const FunctorT& functor) {
348 return worker_thread_->Invoke<bool>(functor);
349 }
350
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 private:
352 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
353 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
354 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
355 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000356 rtc::CriticalSection signal_send_packet_cs_;
357 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 MediaEngineInterface* media_engine_;
361 BaseSession* session_;
362 MediaChannel* media_channel_;
363 std::vector<StreamParams> local_streams_;
364 std::vector<StreamParams> remote_streams_;
365
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000366 const std::string content_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 bool rtcp_;
368 TransportChannel* transport_channel_;
369 TransportChannel* rtcp_transport_channel_;
370 SrtpFilter srtp_filter_;
371 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000372 BundleFilter bundle_filter_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000373 rtc::scoped_ptr<SocketMonitor> socket_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 bool enabled_;
375 bool writable_;
376 bool rtp_ready_to_send_;
377 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 bool was_ever_writable_;
379 MediaContentDirection local_content_direction_;
380 MediaContentDirection remote_content_direction_;
381 std::set<uint32> muted_streams_;
382 bool has_received_packet_;
383 bool dtls_keyed_;
384 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000385 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386};
387
388// VoiceChannel is a specialization that adds support for early media, DTMF,
389// and input/output level monitoring.
390class VoiceChannel : public BaseChannel {
391 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000392 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 VoiceMediaChannel* channel, BaseSession* session,
394 const std::string& content_name, bool rtcp);
395 ~VoiceChannel();
396 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000397 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
398 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399
400 // downcasts a MediaChannel
401 virtual VoiceMediaChannel* media_channel() const {
402 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
403 }
404
405 bool SetRingbackTone(const void* buf, int len);
406 void SetEarlyMedia(bool enable);
407 // This signal is emitted when we have gone a period of time without
408 // receiving early media. When received, a UI should start playing its
409 // own ringing sound
410 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
411
412 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
413 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
414 bool PressDTMF(int digit, bool playout);
415 // Returns if the telephone-event has been negotiated.
416 bool CanInsertDtmf();
417 // Send and/or play a DTMF |event| according to the |flags|.
418 // The DTMF out-of-band signal will be used on sending.
419 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000420 // The valid value for the |event| are 0 which corresponding to DTMF
421 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
423 bool SetOutputScaling(uint32 ssrc, double left, double right);
424 // Get statistics about the current media session.
425 bool GetStats(VoiceMediaInfo* stats);
426
427 // Monitoring functions
428 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
429 SignalConnectionMonitor;
430
431 void StartMediaMonitor(int cms);
432 void StopMediaMonitor();
433 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
434
435 void StartAudioMonitor(int cms);
436 void StopAudioMonitor();
437 bool IsAudioMonitorRunning() const;
438 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
439
440 void StartTypingMonitor(const TypingMonitorOptions& settings);
441 void StopTypingMonitor();
442 bool IsTypingMonitorRunning() const;
443
444 // Overrides BaseChannel::MuteStream_w.
445 virtual bool MuteStream_w(uint32 ssrc, bool mute);
446
447 int GetInputLevel_w();
448 int GetOutputLevel_w();
449 void GetActiveStreams_w(AudioInfo::StreamList* actives);
450
451 // Signal errors from VoiceMediaChannel. Arguments are:
452 // ssrc(uint32), and error(VoiceMediaChannel::Error).
453 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
454 SignalMediaError;
455
456 // Configuration and setting.
457 bool SetChannelOptions(const AudioOptions& options);
458
459 private:
460 // overrides from BaseChannel
461 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000462 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000463 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000464 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 virtual void ChangeState();
466 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
467 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000468 ContentAction action,
469 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 virtual bool SetRemoteContent_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 bool SetRingbackTone_w(const void* buf, int len);
474 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
475 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
477 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
478 bool GetStats_w(VoiceMediaInfo* stats);
479
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000480 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
482 virtual void OnConnectionMonitorUpdate(
483 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
484 virtual void OnMediaMonitorUpdate(
485 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
486 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
487 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
488 void SendLastMediaError();
489 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490
491 static const int kEarlyMediaTimeout = 1000;
492 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
494 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
495 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496};
497
498// VideoChannel is a specialization for video.
499class VideoChannel : public BaseChannel {
500 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000501 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 VideoMediaChannel* channel, BaseSession* session,
503 const std::string& content_name, bool rtcp,
504 VoiceChannel* voice_channel);
505 ~VideoChannel();
506 bool Init();
507
508 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
509 bool ApplyViewRequest(const ViewRequest& request);
510
511 // TODO(pthatcher): Refactor to use a "capture id" instead of an
512 // ssrc here as the "key".
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000513 // Passes ownership of the capturer to the channel.
514 bool AddScreencast(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
516 bool RemoveScreencast(uint32 ssrc);
517 // True if we've added a screencast. Doesn't matter if the capturer
518 // has been started or not.
519 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000520 int GetScreencastFps(uint32 ssrc);
521 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000523 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
525 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
526 SignalConnectionMonitor;
527
528 void StartMediaMonitor(int cms);
529 void StopMediaMonitor();
530 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000531 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
533 bool SendIntraFrame();
534 bool RequestIntraFrame();
535 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
536 SignalMediaError;
537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 // Configuration and setting.
539 bool SetChannelOptions(const VideoOptions& options);
540
541 protected:
542 // downcasts a MediaChannel
543 virtual VideoMediaChannel* media_channel() const {
544 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
545 }
546
547 private:
548 typedef std::map<uint32, VideoCapturer*> ScreencastMap;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000549 struct ScreencastDetailsData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550
551 // overrides from BaseChannel
552 virtual void ChangeState();
553 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
554 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000555 ContentAction action,
556 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 virtual bool SetRemoteContent_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 bool ApplyViewRequest_w(const ViewRequest& request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +0000562 bool AddScreencast_w(uint32 ssrc, VideoCapturer* capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 bool RemoveScreencast_w(uint32 ssrc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000564 void OnScreencastWindowEvent_s(uint32 ssrc, rtc::WindowEvent we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 bool IsScreencasting_w() const;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000566 void GetScreencastDetails_w(ScreencastDetailsData* d) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 bool GetStats_w(VideoMediaInfo* stats);
568
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000569 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
571 virtual void OnConnectionMonitorUpdate(
572 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
573 virtual void OnMediaMonitorUpdate(
574 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
575 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
578 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
579
580 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
581 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582
583 VoiceChannel* voice_channel_;
584 VideoRenderer* renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000586 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000588 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589};
590
591// DataChannel is a specialization for data.
592class DataChannel : public BaseChannel {
593 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 DataMediaChannel* media_channel,
596 BaseSession* session,
597 const std::string& content_name,
598 bool rtcp);
599 ~DataChannel();
600 bool Init();
601
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000602 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000604 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605
606 void StartMediaMonitor(int cms);
607 void StopMediaMonitor();
608
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000609 // Should be called on the signaling thread only.
610 bool ready_to_send_data() const {
611 return ready_to_send_data_;
612 }
613
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
615 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
616 SignalConnectionMonitor;
617 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
618 SignalMediaError;
619 sigslot::signal3<DataChannel*,
620 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000621 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 SignalDataReceived;
623 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000624 // That occurs when the channel is enabled, the transport is writable,
625 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000627 // Signal for notifying that the remote side has closed the DataChannel.
628 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000630 protected:
631 // downcasts a MediaChannel.
632 virtual DataMediaChannel* media_channel() const {
633 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
634 }
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000637 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000639 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 SendDataResult* result)
641 : params(params),
642 payload(payload),
643 result(result),
644 succeeded(false) {
645 }
646
647 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000648 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 SendDataResult* result;
650 bool succeeded;
651 };
652
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000653 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // We copy the data because the data will become invalid after we
655 // handle DataMediaChannel::SignalDataReceived but before we fire
656 // SignalDataReceived.
657 DataReceivedMessageData(
658 const ReceiveDataParams& params, const char* data, size_t len)
659 : params(params),
660 payload(data, len) {
661 }
662 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000663 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 };
665
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000666 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000667
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 // overrides from BaseChannel
669 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
670 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
671 // it's the same as what was set previously. Returns false if it's
672 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000673 bool SetDataChannelType(DataChannelType new_data_channel_type,
674 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 // Same as SetDataChannelType, but extracts the type from the
676 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000677 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
678 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000680 ContentAction action,
681 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 virtual bool SetRemoteContent_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 void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000686 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000688 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
690 virtual void OnConnectionMonitorUpdate(
691 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
692 virtual void OnMediaMonitorUpdate(
693 DataMediaChannel* media_channel, const DataMediaInfo& info);
694 virtual bool ShouldSetupDtlsSrtp() const;
695 void OnDataReceived(
696 const ReceiveDataParams& params, const char* data, size_t len);
697 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000698 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000700 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000702 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 // TODO(pthatcher): Make a separate SctpDataChannel and
704 // RtpDataChannel instead of using this.
705 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000706 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707};
708
709} // namespace cricket
710
711#endif // TALK_SESSION_MEDIA_CHANNEL_H_