blob: 2480f451e889f077902ba8c0511c137b9408867d [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/asyncudpsocket.h"
35#include "webrtc/base/criticalsection.h"
36#include "webrtc/base/network.h"
37#include "webrtc/base/sigslot.h"
38#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/mediachannel.h"
40#include "talk/media/base/mediaengine.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000041#include "talk/media/base/screencastid.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/media/base/streamparams.h"
43#include "talk/media/base/videocapturer.h"
44#include "talk/p2p/base/session.h"
45#include "talk/p2p/client/socketmonitor.h"
46#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +000047#include "talk/session/media/bundlefilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/session/media/mediamonitor.h"
49#include "talk/session/media/mediasession.h"
50#include "talk/session/media/rtcpmuxfilter.h"
51#include "talk/session/media/srtpfilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53namespace cricket {
54
55struct CryptoParams;
56class MediaContentDescription;
57struct TypingMonitorOptions;
58class TypingMonitor;
59struct ViewRequest;
60
61enum SinkType {
62 SINK_PRE_CRYPTO, // Sink packets before encryption or after decryption.
63 SINK_POST_CRYPTO // Sink packets after encryption or before decryption.
64};
65
66// BaseChannel contains logic common to voice and video, including
67// enable/mute, marshaling calls to a worker thread, and
68// connection and media monitors.
wu@webrtc.org78187522013-10-07 23:32:02 +000069//
70// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
71// This is required to avoid a data race between the destructor modifying the
72// vtable, and the media channel's thread using BaseChannel as the
73// NetworkInterface.
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 : public rtc::MessageHandler, public sigslot::has_slots<>,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 public MediaChannel::NetworkInterface {
78 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();
134
135 void set_srtp_signal_silent_time(uint32 silent_time) {
136 srtp_filter_.set_signal_silent_time(silent_time);
137 }
138
139 void set_content_name(const std::string& content_name) {
140 ASSERT(signaling_thread()->IsCurrent());
141 ASSERT(!writable_);
142 if (session_->state() != BaseSession::STATE_INIT) {
143 LOG(LS_ERROR) << "Content name for a channel can be changed only "
144 << "when BaseSession is in STATE_INIT state.";
145 return;
146 }
147 content_name_ = content_name;
148 }
149
150 template <class T>
151 void RegisterSendSink(T* sink,
152 void (T::*OnPacket)(const void*, size_t, bool),
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 SignalSendPacketPostCrypto.connect(sink, OnPacket);
158 } else {
159 SignalSendPacketPreCrypto.disconnect(sink);
160 SignalSendPacketPreCrypto.connect(sink, OnPacket);
161 }
162 }
163
164 void UnregisterSendSink(sigslot::has_slots<>* sink,
165 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 SignalSendPacketPostCrypto.disconnect(sink);
169 } else {
170 SignalSendPacketPreCrypto.disconnect(sink);
171 }
172 }
173
174 bool HasSendSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000175 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 if (SINK_POST_CRYPTO == type) {
177 return !SignalSendPacketPostCrypto.is_empty();
178 } else {
179 return !SignalSendPacketPreCrypto.is_empty();
180 }
181 }
182
183 template <class T>
184 void RegisterRecvSink(T* sink,
185 void (T::*OnPacket)(const void*, size_t, bool),
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 SignalRecvPacketPostCrypto.connect(sink, OnPacket);
191 } else {
192 SignalRecvPacketPreCrypto.disconnect(sink);
193 SignalRecvPacketPreCrypto.connect(sink, OnPacket);
194 }
195 }
196
197 void UnregisterRecvSink(sigslot::has_slots<>* sink,
198 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 SignalRecvPacketPostCrypto.disconnect(sink);
202 } else {
203 SignalRecvPacketPreCrypto.disconnect(sink);
204 }
205 }
206
207 bool HasRecvSinks(SinkType type) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000208 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 if (SINK_POST_CRYPTO == type) {
210 return !SignalRecvPacketPostCrypto.is_empty();
211 } else {
212 return !SignalRecvPacketPreCrypto.is_empty();
213 }
214 }
215
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000216 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217
218 const std::vector<StreamParams>& local_streams() const {
219 return local_streams_;
220 }
221 const std::vector<StreamParams>& remote_streams() const {
222 return remote_streams_;
223 }
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
234 protected:
235 MediaEngineInterface* media_engine() const { return media_engine_; }
236 virtual MediaChannel* media_channel() const { return media_channel_; }
237 void set_rtcp_transport_channel(TransportChannel* transport);
238 bool was_ever_writable() const { return was_ever_writable_; }
239 void set_local_content_direction(MediaContentDirection direction) {
240 local_content_direction_ = direction;
241 }
242 void set_remote_content_direction(MediaContentDirection direction) {
243 remote_content_direction_ = direction;
244 }
245 bool IsReadyToReceive() const;
246 bool IsReadyToSend() const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000247 rtc::Thread* signaling_thread() { return session_->signaling_thread(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 SrtpFilter* srtp_filter() { return &srtp_filter_; }
249 bool rtcp() const { return rtcp_; }
250
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 void FlushRtcpMessages();
252
253 // NetworkInterface implementation, called by MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000254 virtual bool SendPacket(rtc::Buffer* packet,
255 rtc::DiffServCodePoint dscp);
256 virtual bool SendRtcp(rtc::Buffer* packet,
257 rtc::DiffServCodePoint dscp);
258 virtual int SetOption(SocketType type, rtc::Socket::Option o, int val);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259
260 // From TransportChannel
261 void OnWritableState(TransportChannel* channel);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000262 virtual void OnChannelRead(TransportChannel* channel,
263 const char* data,
264 size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000266 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 void OnReadyToSend(TransportChannel* channel);
268
269 bool PacketIsRtcp(const TransportChannel* channel, const char* data,
270 size_t len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000271 bool SendPacket(bool rtcp, rtc::Buffer* packet,
272 rtc::DiffServCodePoint dscp);
273 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
274 void HandlePacket(bool rtcp, rtc::Buffer* packet,
275 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276
277 // Apply the new local/remote session description.
278 void OnNewLocalDescription(BaseSession* session, ContentAction action);
279 void OnNewRemoteDescription(BaseSession* session, ContentAction action);
280
281 void EnableMedia_w();
282 void DisableMedia_w();
283 virtual bool MuteStream_w(uint32 ssrc, bool mute);
284 bool IsStreamMuted_w(uint32 ssrc);
285 void ChannelWritable_w();
286 void ChannelNotWritable_w();
287 bool AddRecvStream_w(const StreamParams& sp);
288 bool RemoveRecvStream_w(uint32 ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000289 bool AddSendStream_w(const StreamParams& sp);
290 bool RemoveSendStream_w(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 virtual bool ShouldSetupDtlsSrtp() const;
292 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
293 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
294 bool SetupDtlsSrtp(bool rtcp_channel);
295 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
296 bool SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp);
297
298 virtual void ChangeState() = 0;
299
300 // Gets the content info appropriate to the channel (audio or video).
301 virtual const ContentInfo* GetFirstContent(
302 const SessionDescription* sdesc) = 0;
303 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000304 ContentAction action,
305 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000307 ContentAction action,
308 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 bool SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000310 ContentAction action,
311 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313 ContentAction action,
314 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 bool SetBaseRemoteContent_w(const MediaContentDescription* content,
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 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319 ContentAction action,
320 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000322 // Helper method to get RTP Absoulute SendTime extension header id if
323 // present in remote supported extensions list.
324 void MaybeCacheRtpAbsSendTimeHeaderExtension(
325 const std::vector<RtpHeaderExtension>& extensions);
326
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000327 bool SetRecvRtpHeaderExtensions_w(const MediaContentDescription* content,
328 MediaChannel* media_channel,
329 std::string* error_desc);
330 bool SetSendRtpHeaderExtensions_w(const MediaContentDescription* content,
331 MediaChannel* media_channel,
332 std::string* error_desc);
333
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000334 bool CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
335 bool* dtls,
336 std::string* error_desc);
337 bool SetSrtp_w(const std::vector<CryptoParams>& params,
338 ContentAction action,
339 ContentSource src,
340 std::string* error_desc);
341 bool SetRtcpMux_w(bool enable,
342 ContentAction action,
343 ContentSource src,
344 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345
346 // From MessageHandler
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000347 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348
349 // Handled in derived classes
350 // Get the SRTP ciphers to use for RTP media
351 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const = 0;
352 virtual void OnConnectionMonitorUpdate(SocketMonitor* monitor,
353 const std::vector<ConnectionInfo>& infos) = 0;
354
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000355 // Helper function for invoking bool-returning methods on the worker thread.
356 template <class FunctorT>
357 bool InvokeOnWorker(const FunctorT& functor) {
358 return worker_thread_->Invoke<bool>(functor);
359 }
360
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 private:
362 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPreCrypto;
363 sigslot::signal3<const void*, size_t, bool> SignalSendPacketPostCrypto;
364 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPreCrypto;
365 sigslot::signal3<const void*, size_t, bool> SignalRecvPacketPostCrypto;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::CriticalSection signal_send_packet_cs_;
367 rtc::CriticalSection signal_recv_packet_cs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000369 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 MediaEngineInterface* media_engine_;
371 BaseSession* session_;
372 MediaChannel* media_channel_;
373 std::vector<StreamParams> local_streams_;
374 std::vector<StreamParams> remote_streams_;
375
376 std::string content_name_;
377 bool rtcp_;
378 TransportChannel* transport_channel_;
379 TransportChannel* rtcp_transport_channel_;
380 SrtpFilter srtp_filter_;
381 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000382 BundleFilter bundle_filter_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000383 rtc::scoped_ptr<SocketMonitor> socket_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 bool enabled_;
385 bool writable_;
386 bool rtp_ready_to_send_;
387 bool rtcp_ready_to_send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 bool was_ever_writable_;
389 MediaContentDirection local_content_direction_;
390 MediaContentDirection remote_content_direction_;
391 std::set<uint32> muted_streams_;
392 bool has_received_packet_;
393 bool dtls_keyed_;
394 bool secure_required_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000395 int rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396};
397
398// VoiceChannel is a specialization that adds support for early media, DTMF,
399// and input/output level monitoring.
400class VoiceChannel : public BaseChannel {
401 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000402 VoiceChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 VoiceMediaChannel* channel, BaseSession* session,
404 const std::string& content_name, bool rtcp);
405 ~VoiceChannel();
406 bool Init();
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000407 bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer);
408 bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409
410 // downcasts a MediaChannel
411 virtual VoiceMediaChannel* media_channel() const {
412 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
413 }
414
415 bool SetRingbackTone(const void* buf, int len);
416 void SetEarlyMedia(bool enable);
417 // This signal is emitted when we have gone a period of time without
418 // receiving early media. When received, a UI should start playing its
419 // own ringing sound
420 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
421
422 bool PlayRingbackTone(uint32 ssrc, bool play, bool loop);
423 // TODO(ronghuawu): Replace PressDTMF with InsertDtmf.
424 bool PressDTMF(int digit, bool playout);
425 // Returns if the telephone-event has been negotiated.
426 bool CanInsertDtmf();
427 // Send and/or play a DTMF |event| according to the |flags|.
428 // The DTMF out-of-band signal will be used on sending.
429 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000430 // The valid value for the |event| are 0 which corresponding to DTMF
431 // event 0-9, *, #, A-D.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 bool InsertDtmf(uint32 ssrc, int event_code, int duration, int flags);
433 bool SetOutputScaling(uint32 ssrc, double left, double right);
434 // Get statistics about the current media session.
435 bool GetStats(VoiceMediaInfo* stats);
436
437 // Monitoring functions
438 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
439 SignalConnectionMonitor;
440
441 void StartMediaMonitor(int cms);
442 void StopMediaMonitor();
443 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
444
445 void StartAudioMonitor(int cms);
446 void StopAudioMonitor();
447 bool IsAudioMonitorRunning() const;
448 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
449
450 void StartTypingMonitor(const TypingMonitorOptions& settings);
451 void StopTypingMonitor();
452 bool IsTypingMonitorRunning() const;
453
454 // Overrides BaseChannel::MuteStream_w.
455 virtual bool MuteStream_w(uint32 ssrc, bool mute);
456
457 int GetInputLevel_w();
458 int GetOutputLevel_w();
459 void GetActiveStreams_w(AudioInfo::StreamList* actives);
460
461 // Signal errors from VoiceMediaChannel. Arguments are:
462 // ssrc(uint32), and error(VoiceMediaChannel::Error).
463 sigslot::signal3<VoiceChannel*, uint32, VoiceMediaChannel::Error>
464 SignalMediaError;
465
466 // Configuration and setting.
467 bool SetChannelOptions(const AudioOptions& options);
468
469 private:
470 // overrides from BaseChannel
471 virtual void OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000472 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000473 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000474 int flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 virtual void ChangeState();
476 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
477 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000478 ContentAction action,
479 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000481 ContentAction action,
482 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 bool SetRingbackTone_w(const void* buf, int len);
484 bool PlayRingbackTone_w(uint32 ssrc, bool play, bool loop);
485 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 bool InsertDtmf_w(uint32 ssrc, int event, int duration, int flags);
487 bool SetOutputScaling_w(uint32 ssrc, double left, double right);
488 bool GetStats_w(VoiceMediaInfo* stats);
489
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000490 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
492 virtual void OnConnectionMonitorUpdate(
493 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
494 virtual void OnMediaMonitorUpdate(
495 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info);
496 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
497 void OnVoiceChannelError(uint32 ssrc, VoiceMediaChannel::Error error);
498 void SendLastMediaError();
499 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500
501 static const int kEarlyMediaTimeout = 1000;
502 bool received_media_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503 rtc::scoped_ptr<VoiceMediaMonitor> media_monitor_;
504 rtc::scoped_ptr<AudioMonitor> audio_monitor_;
505 rtc::scoped_ptr<TypingMonitor> typing_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506};
507
508// VideoChannel is a specialization for video.
509class VideoChannel : public BaseChannel {
510 public:
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +0000511 // Make screen capturer virtual so that it can be overriden in testing.
512 // E.g. used to test that window events are triggered correctly.
513 class ScreenCapturerFactory {
514 public:
515 virtual VideoCapturer* CreateScreenCapturer(const ScreencastId& window) = 0;
516 virtual ~ScreenCapturerFactory() {}
517 };
518
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000519 VideoChannel(rtc::Thread* thread, MediaEngineInterface* media_engine,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 VideoMediaChannel* channel, BaseSession* session,
521 const std::string& content_name, bool rtcp,
522 VoiceChannel* voice_channel);
523 ~VideoChannel();
524 bool Init();
525
526 bool SetRenderer(uint32 ssrc, VideoRenderer* renderer);
527 bool ApplyViewRequest(const ViewRequest& request);
528
529 // TODO(pthatcher): Refactor to use a "capture id" instead of an
530 // ssrc here as the "key".
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +0000531 VideoCapturer* AddScreencast(uint32 ssrc, const ScreencastId& id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 bool SetCapturer(uint32 ssrc, VideoCapturer* capturer);
533 bool RemoveScreencast(uint32 ssrc);
534 // True if we've added a screencast. Doesn't matter if the capturer
535 // has been started or not.
536 bool IsScreencasting();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000537 int GetScreencastFps(uint32 ssrc);
538 int GetScreencastMaxPixels(uint32 ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // Get statistics about the current media session.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000540 bool GetStats(const StatsOptions& options, VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
542 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
543 SignalConnectionMonitor;
544
545 void StartMediaMonitor(int cms);
546 void StopMediaMonitor();
547 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000548 sigslot::signal2<uint32, rtc::WindowEvent> SignalScreencastWindowEvent;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549
550 bool SendIntraFrame();
551 bool RequestIntraFrame();
552 sigslot::signal3<VideoChannel*, uint32, VideoMediaChannel::Error>
553 SignalMediaError;
554
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +0000555 void SetScreenCaptureFactory(
556 ScreenCapturerFactory* screencapture_factory);
557
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 // Configuration and setting.
559 bool SetChannelOptions(const VideoOptions& options);
560
561 protected:
562 // downcasts a MediaChannel
563 virtual VideoMediaChannel* media_channel() const {
564 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
565 }
566
567 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.org5b1ebac2014-08-07 17:18:00 +0000582 VideoCapturer* AddScreencast_w(uint32 ssrc, const ScreencastId& id);
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;
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +0000587 void SetScreenCaptureFactory_w(
588 ScreenCapturerFactory* screencapture_factory);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 bool GetStats_w(VideoMediaInfo* stats);
590
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
593 virtual void OnConnectionMonitorUpdate(
594 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
595 virtual void OnMediaMonitorUpdate(
596 VideoMediaChannel* media_channel, const VideoMediaInfo& info);
597 virtual void OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000598 rtc::WindowEvent event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 virtual void OnStateChange(VideoCapturer* capturer, CaptureState ev);
600 bool GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc);
601
602 void OnVideoChannelError(uint32 ssrc, VideoMediaChannel::Error error);
603 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
605 VoiceChannel* voice_channel_;
606 VideoRenderer* renderer_;
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +0000607 rtc::scoped_ptr<ScreenCapturerFactory> screencapture_factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 ScreencastMap screencast_capturers_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000609 rtc::scoped_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 rtc::WindowEvent previous_we_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612};
613
614// DataChannel is a specialization for data.
615class DataChannel : public BaseChannel {
616 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000617 DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 DataMediaChannel* media_channel,
619 BaseSession* session,
620 const std::string& content_name,
621 bool rtcp);
622 ~DataChannel();
623 bool Init();
624
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000625 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000626 const rtc::Buffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000627 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
629 void StartMediaMonitor(int cms);
630 void StopMediaMonitor();
631
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000632 // Should be called on the signaling thread only.
633 bool ready_to_send_data() const {
634 return ready_to_send_data_;
635 }
636
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 sigslot::signal2<DataChannel*, const DataMediaInfo&> SignalMediaMonitor;
638 sigslot::signal2<DataChannel*, const std::vector<ConnectionInfo>&>
639 SignalConnectionMonitor;
640 sigslot::signal3<DataChannel*, uint32, DataMediaChannel::Error>
641 SignalMediaError;
642 sigslot::signal3<DataChannel*,
643 const ReceiveDataParams&,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000644 const rtc::Buffer&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 SignalDataReceived;
646 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000647 // That occurs when the channel is enabled, the transport is writable,
648 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 sigslot::signal1<bool> SignalReadyToSendData;
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000650 // Signal for notifying that the remote side has closed the DataChannel.
651 sigslot::signal1<uint32> SignalStreamClosedRemotely;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000653 protected:
654 // downcasts a MediaChannel.
655 virtual DataMediaChannel* media_channel() const {
656 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
657 }
658
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000660 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 SendDataMessageData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 const rtc::Buffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 SendDataResult* result)
664 : params(params),
665 payload(payload),
666 result(result),
667 succeeded(false) {
668 }
669
670 const SendDataParams& params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000671 const rtc::Buffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 SendDataResult* result;
673 bool succeeded;
674 };
675
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000676 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 // We copy the data because the data will become invalid after we
678 // handle DataMediaChannel::SignalDataReceived but before we fire
679 // SignalDataReceived.
680 DataReceivedMessageData(
681 const ReceiveDataParams& params, const char* data, size_t len)
682 : params(params),
683 payload(data, len) {
684 }
685 const ReceiveDataParams params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000686 const rtc::Buffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 };
688
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000689 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000690
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 // overrides from BaseChannel
692 virtual const ContentInfo* GetFirstContent(const SessionDescription* sdesc);
693 // If data_channel_type_ is DCT_NONE, set it. Otherwise, check that
694 // it's the same as what was set previously. Returns false if it's
695 // set to one type one type and changed to another type later.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000696 bool SetDataChannelType(DataChannelType new_data_channel_type,
697 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 // Same as SetDataChannelType, but extracts the type from the
699 // DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000700 bool SetDataChannelTypeFromContent(const DataContentDescription* content,
701 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000703 ContentAction action,
704 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000706 ContentAction action,
707 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 virtual void ChangeState();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000709 virtual bool WantsPacket(bool rtcp, rtc::Buffer* packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000711 virtual void OnMessage(rtc::Message* pmsg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 virtual void GetSrtpCiphers(std::vector<std::string>* ciphers) const;
713 virtual void OnConnectionMonitorUpdate(
714 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos);
715 virtual void OnMediaMonitorUpdate(
716 DataMediaChannel* media_channel, const DataMediaInfo& info);
717 virtual bool ShouldSetupDtlsSrtp() const;
718 void OnDataReceived(
719 const ReceiveDataParams& params, const char* data, size_t len);
720 void OnDataChannelError(uint32 ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000721 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 void OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, SrtpFilter::Error error);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +0000723 void OnStreamClosedRemotely(uint32 sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000725 rtc::scoped_ptr<DataMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 // TODO(pthatcher): Make a separate SctpDataChannel and
727 // RtpDataChannel instead of using this.
728 DataChannelType data_channel_type_;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000729 bool ready_to_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730};
731
732} // namespace cricket
733
734#endif // TALK_SESSION_MEDIA_CHANNEL_H_